@@ -5,7 +5,7 @@ use datafusion_common::ScalarValue;
5
5
mod utils;
6
6
use datafusion_expr:: ColumnarValue ;
7
7
use datafusion_functions_json:: udfs:: json_get_str_udf;
8
- use utils:: { display_val, run_query, run_query_large, run_query_params} ;
8
+ use utils:: { display_val, logical_plan , run_query, run_query_large, run_query_params} ;
9
9
10
10
#[ tokio:: test]
11
11
async fn test_json_contains ( ) {
@@ -523,7 +523,7 @@ async fn test_json_get_union_scalar() {
523
523
}
524
524
525
525
#[ tokio:: test]
526
- async fn test_json_get_union_array ( ) {
526
+ async fn test_json_get_nested_collapsed ( ) {
527
527
let expected = [
528
528
"+------------------+---------+" ,
529
529
"| name | v |" ,
@@ -545,7 +545,130 @@ async fn test_json_get_union_array() {
545
545
}
546
546
547
547
#[ tokio:: test]
548
- async fn test_json_get_union_array_skip ( ) {
548
+ async fn test_json_get_cte ( ) {
549
+ // avoid auto-un-nesting with a CTE
550
+ let sql = r#"
551
+ with t as (select name, json_get(json_data, 'foo') j from test)
552
+ select name, json_get(j, 0) v from t
553
+ "# ;
554
+ let expected = [
555
+ "+------------------+---------+" ,
556
+ "| name | v |" ,
557
+ "+------------------+---------+" ,
558
+ "| object_foo | {null=} |" ,
559
+ "| object_foo_array | {int=1} |" ,
560
+ "| object_foo_obj | {null=} |" ,
561
+ "| object_foo_null | {null=} |" ,
562
+ "| object_bar | {null=} |" ,
563
+ "| list_foo | {null=} |" ,
564
+ "| invalid_json | {null=} |" ,
565
+ "+------------------+---------+" ,
566
+ ] ;
567
+
568
+ let batches = run_query ( sql) . await . unwrap ( ) ;
569
+ assert_batches_eq ! ( expected, & batches) ;
570
+ }
571
+
572
+ #[ tokio:: test]
573
+ async fn test_json_get_cte_plan ( ) {
574
+ // avoid auto-unnesting with a CTE
575
+ let sql = r#"
576
+ explain
577
+ with t as (select name, json_get(json_data, 'foo') j from test)
578
+ select name, json_get(j, 0) v from t
579
+ "# ;
580
+ let expected = [
581
+ "Projection: t.name, json_get(t.j, Int64(0)) AS v" ,
582
+ " SubqueryAlias: t" ,
583
+ " Projection: test.name, json_get(test.json_data, Utf8(\" foo\" )) AS j" ,
584
+ " TableScan: test projection=[name, json_data]" ,
585
+ ] ;
586
+
587
+ let plan_lines = logical_plan ( sql) . await ;
588
+ assert_eq ! ( plan_lines, expected) ;
589
+ }
590
+
591
+ #[ tokio:: test]
592
+ async fn test_json_get_unnest ( ) {
593
+ let sql = "select name, json_get(json_get(json_data, 'foo'), 0) v from test" ;
594
+
595
+ let expected = [
596
+ "+------------------+---------+" ,
597
+ "| name | v |" ,
598
+ "+------------------+---------+" ,
599
+ "| object_foo | {null=} |" ,
600
+ "| object_foo_array | {int=1} |" ,
601
+ "| object_foo_obj | {null=} |" ,
602
+ "| object_foo_null | {null=} |" ,
603
+ "| object_bar | {null=} |" ,
604
+ "| list_foo | {null=} |" ,
605
+ "| invalid_json | {null=} |" ,
606
+ "+------------------+---------+" ,
607
+ ] ;
608
+
609
+ let batches = run_query ( sql) . await . unwrap ( ) ;
610
+ assert_batches_eq ! ( expected, & batches) ;
611
+ }
612
+
613
+ #[ tokio:: test]
614
+ async fn test_json_get_unnest_plan ( ) {
615
+ let sql = "explain select json_get(json_get(json_data, 'foo'), 0) v from test" ;
616
+ let expected = [
617
+ "Projection: json_get(test.json_data, Utf8(\" foo\" ), Int64(0)) AS v" ,
618
+ " TableScan: test projection=[json_data]" ,
619
+ ] ;
620
+
621
+ let plan_lines = logical_plan ( sql) . await ;
622
+ assert_eq ! ( plan_lines, expected) ;
623
+ }
624
+
625
+ #[ tokio:: test]
626
+ async fn test_json_get_int_unnest ( ) {
627
+ let sql = "select name, json_get(json_get(json_data, 'foo'), 0)::int v from test" ;
628
+
629
+ let expected = [
630
+ "+------------------+---+" ,
631
+ "| name | v |" ,
632
+ "+------------------+---+" ,
633
+ "| object_foo | |" ,
634
+ "| object_foo_array | 1 |" ,
635
+ "| object_foo_obj | |" ,
636
+ "| object_foo_null | |" ,
637
+ "| object_bar | |" ,
638
+ "| list_foo | |" ,
639
+ "| invalid_json | |" ,
640
+ "+------------------+---+" ,
641
+ ] ;
642
+
643
+ let batches = run_query ( sql) . await . unwrap ( ) ;
644
+ assert_batches_eq ! ( expected, & batches) ;
645
+ }
646
+
647
+ #[ tokio:: test]
648
+ async fn test_json_get_int_unnest_plan ( ) {
649
+ let sql = "explain select json_get(json_get(json_data, 'foo'), 0)::int v from test" ;
650
+ let expected = [
651
+ "Projection: json_get_int(test.json_data, Utf8(\" foo\" ), Int64(0)) AS v" ,
652
+ " TableScan: test projection=[json_data]" ,
653
+ ] ;
654
+
655
+ let plan_lines = logical_plan ( sql) . await ;
656
+ assert_eq ! ( plan_lines, expected) ;
657
+ }
658
+
659
+ #[ tokio:: test]
660
+ async fn test_multiple_lookup_arrays ( ) {
661
+ let sql = "select json_get(json_data, str_key1, str_key2) v from more_nested" ;
662
+ let err = run_query ( sql) . await . unwrap_err ( ) ;
663
+ assert_eq ! (
664
+ err. to_string( ) ,
665
+ "Execution error: More than 1 path element is not supported when querying JSON using an array."
666
+ ) ;
667
+ }
668
+
669
+ #[ tokio:: test]
670
+ async fn test_json_get_union_array_nested ( ) {
671
+ let sql = "select json_get(json_get(json_data, str_key1), str_key2) v from more_nested" ;
549
672
let expected = [
550
673
"+-------------+" ,
551
674
"| v |" ,
@@ -556,13 +679,27 @@ async fn test_json_get_union_array_skip() {
556
679
"+-------------+" ,
557
680
] ;
558
681
559
- let sql = "select json_get(json_get(json_data, str_key1), str_key2) v from more_nested" ;
560
682
let batches = run_query ( sql) . await . unwrap ( ) ;
561
683
assert_batches_eq ! ( expected, & batches) ;
562
684
}
563
685
686
+ #[ tokio:: test]
687
+ async fn test_json_get_union_array_nested_plan ( ) {
688
+ let sql = "explain select json_get(json_get(json_data, str_key1), str_key2) v from more_nested" ;
689
+ // json_get is not un-nested because lookup types are not literals
690
+ let expected = [
691
+ "Projection: json_get(json_get(more_nested.json_data, more_nested.str_key1), more_nested.str_key2) AS v" ,
692
+ " TableScan: more_nested projection=[json_data, str_key1, str_key2]" ,
693
+ ] ;
694
+
695
+ let plan_lines = logical_plan ( sql) . await ;
696
+ assert_eq ! ( plan_lines, expected) ;
697
+ }
698
+
564
699
#[ tokio:: test]
565
700
async fn test_json_get_union_array_skip_double_nested ( ) {
701
+ let sql =
702
+ "select json_data, json_get_int(json_get(json_get(json_data, str_key1), str_key2), int_key) v from more_nested" ;
566
703
let expected = [
567
704
"+--------------------------+---+" ,
568
705
"| json_data | v |" ,
@@ -573,8 +710,6 @@ async fn test_json_get_union_array_skip_double_nested() {
573
710
"+--------------------------+---+" ,
574
711
] ;
575
712
576
- let sql =
577
- "select json_data, json_get_int(json_get(json_get(json_data, str_key1), str_key2), int_key) v from more_nested" ;
578
713
let batches = run_query ( sql) . await . unwrap ( ) ;
579
714
assert_batches_eq ! ( expected, & batches) ;
580
715
}
0 commit comments