@@ -246,3 +246,90 @@ fn test_duckdb_load_extension() {
246246 stmt
247247 ) ;
248248}
249+
250+ #[ test]
251+ fn test_duckdb_struct_literal ( ) {
252+ //struct literal syntax https://duckdb.org/docs/sql/data_types/struct#creating-structs
253+ //syntax: {'field_name': expr1[, ... ]}
254+ let sql = "SELECT {'a': 1, 'b': 2, 'c': 3}, [{'a': 'abc'}], {'a': 1, 'b': [t.str_col]}, {'a': 1, 'b': 'abc'}, {'abc': str_col}, {'a': {'aa': 1}}" ;
255+ let select = duckdb_and_generic ( ) . verified_only_select ( sql) ;
256+ assert_eq ! ( 6 , select. projection. len( ) ) ;
257+ assert_eq ! (
258+ & Expr :: Dictionary ( vec![
259+ DictionaryField {
260+ key: Ident :: with_quote( '\'' , "a" ) ,
261+ value: Box :: new( Expr :: Value ( number( "1" ) ) ) ,
262+ } ,
263+ DictionaryField {
264+ key: Ident :: with_quote( '\'' , "b" ) ,
265+ value: Box :: new( Expr :: Value ( number( "2" ) ) ) ,
266+ } ,
267+ DictionaryField {
268+ key: Ident :: with_quote( '\'' , "c" ) ,
269+ value: Box :: new( Expr :: Value ( number( "3" ) ) ) ,
270+ } ,
271+ ] , ) ,
272+ expr_from_projection( & select. projection[ 0 ] )
273+ ) ;
274+
275+ assert_eq ! (
276+ & Expr :: Array ( Array {
277+ elem: vec![ Expr :: Dictionary ( vec![ DictionaryField {
278+ key: Ident :: with_quote( '\'' , "a" ) ,
279+ value: Box :: new( Expr :: Value ( Value :: SingleQuotedString ( "abc" . to_string( ) ) ) ) ,
280+ } , ] , ) ] ,
281+ named: false
282+ } ) ,
283+ expr_from_projection( & select. projection[ 1 ] )
284+ ) ;
285+ assert_eq ! (
286+ & Expr :: Dictionary ( vec![
287+ DictionaryField {
288+ key: Ident :: with_quote( '\'' , "a" ) ,
289+ value: Box :: new( Expr :: Value ( number( "1" ) ) ) ,
290+ } ,
291+ DictionaryField {
292+ key: Ident :: with_quote( '\'' , "b" ) ,
293+ value: Box :: new( Expr :: Array ( Array {
294+ elem: vec![ Expr :: CompoundIdentifier ( vec![
295+ Ident :: from( "t" ) ,
296+ Ident :: from( "str_col" )
297+ ] ) ] ,
298+ named: false
299+ } ) ) ,
300+ } ,
301+ ] , ) ,
302+ expr_from_projection( & select. projection[ 2 ] )
303+ ) ;
304+ assert_eq ! (
305+ & Expr :: Dictionary ( vec![
306+ DictionaryField {
307+ key: Ident :: with_quote( '\'' , "a" ) ,
308+ value: Expr :: Value ( number( "1" ) ) . into( ) ,
309+ } ,
310+ DictionaryField {
311+ key: Ident :: with_quote( '\'' , "b" ) ,
312+ value: Expr :: Value ( Value :: SingleQuotedString ( "abc" . to_string( ) ) ) . into( ) ,
313+ } ,
314+ ] , ) ,
315+ expr_from_projection( & select. projection[ 3 ] )
316+ ) ;
317+ assert_eq ! (
318+ & Expr :: Dictionary ( vec![ DictionaryField {
319+ key: Ident :: with_quote( '\'' , "abc" ) ,
320+ value: Expr :: Identifier ( Ident :: from( "str_col" ) ) . into( ) ,
321+ } ] , ) ,
322+ expr_from_projection( & select. projection[ 4 ] )
323+ ) ;
324+ assert_eq ! (
325+ & Expr :: Dictionary ( vec![ DictionaryField {
326+ key: Ident :: with_quote( '\'' , "a" ) ,
327+ value: Expr :: Dictionary ( vec![ DictionaryField {
328+ key: Ident :: with_quote( '\'' , "aa" ) ,
329+ value: Expr :: Value ( number( "1" ) ) . into( ) ,
330+ } ] , )
331+ . into( ) ,
332+ } ] , ) ,
333+ expr_from_projection( & select. projection[ 5 ] )
334+ ) ;
335+ }
0 commit comments