@@ -481,4 +481,103 @@ class FlintSparkPPLScalarSubqueryITSuite
481
481
implicit val rowOrdering : Ordering [Row ] = Ordering .by[Row , Integer ](_.getAs[Integer ](0 ))
482
482
assert(results.sorted.sameElements(expectedResults.sorted))
483
483
}
484
+
485
+ test(" test nested scalar subquery with table alias" ) {
486
+ val frame = sql(s """
487
+ | source = $outerTable as o
488
+ | | where id = [
489
+ | source = $innerTable as i
490
+ | | where uid = [
491
+ | source = $nestedInnerTable as n
492
+ | | stats min(n.salary)
493
+ | ] + 1000
494
+ | | sort i.department
495
+ | | stats max(i.uid)
496
+ | ]
497
+ | | fields o.id, o.name
498
+ | """ .stripMargin)
499
+ val expectedResults : Array [Row ] = Array (Row (1000 , " Jake" ))
500
+ assertSameRows(expectedResults, frame)
501
+ }
502
+
503
+ test(" test correlated scalar subquery with table alias" ) {
504
+ val frame = sql(s """
505
+ | source = $outerTable as o
506
+ | | where id = [
507
+ | source = $innerTable as i | where o.id = i.uid | stats max(i.uid)
508
+ | ]
509
+ | | fields o.id, o.name
510
+ | """ .stripMargin)
511
+ val expectedResults : Array [Row ] = Array (
512
+ Row (1000 , " Jake" ),
513
+ Row (1002 , " John" ),
514
+ Row (1003 , " David" ),
515
+ Row (1005 , " Jane" ),
516
+ Row (1006 , " Tommy" ))
517
+ assertSameRows(expectedResults, frame)
518
+
519
+ val logicalPlan : LogicalPlan = frame.queryExecution.logical
520
+
521
+ val outer =
522
+ SubqueryAlias (" o" , UnresolvedRelation (Seq (" spark_catalog" , " default" , " flint_ppl_test1" )))
523
+ val inner =
524
+ SubqueryAlias (" i" , UnresolvedRelation (Seq (" spark_catalog" , " default" , " flint_ppl_test2" )))
525
+ val aggregateExpressions = Seq (
526
+ Alias (
527
+ UnresolvedFunction (Seq (" MAX" ), Seq (UnresolvedAttribute (" i.uid" )), isDistinct = false ),
528
+ " max(i.uid)" )())
529
+ val innerFilter =
530
+ Filter (EqualTo (UnresolvedAttribute (" o.id" ), UnresolvedAttribute (" i.uid" )), inner)
531
+ val aggregatePlan = Aggregate (Seq (), aggregateExpressions, innerFilter)
532
+ val scalarSubqueryExpr = ScalarSubquery (aggregatePlan)
533
+ val outerFilter = Filter (EqualTo (UnresolvedAttribute (" id" ), scalarSubqueryExpr), outer)
534
+ val expectedPlan =
535
+ Project (Seq (UnresolvedAttribute (" o.id" ), UnresolvedAttribute (" o.name" )), outerFilter)
536
+
537
+ comparePlans(expectedPlan, logicalPlan, checkAnalysis = false )
538
+ }
539
+
540
+ test(" test uncorrelated scalar subquery with table alias" ) {
541
+ val frame = sql(s """
542
+ | source = $outerTable as o
543
+ | | eval max_uid = [
544
+ | source = $innerTable as i | where i.department = 'DATA' | stats max(i.uid)
545
+ | ]
546
+ | | fields o.id, o.name, max_uid
547
+ | """ .stripMargin)
548
+ val expectedResults : Array [Row ] = Array (
549
+ Row (1000 , " Jake" , 1005 ),
550
+ Row (1001 , " Hello" , 1005 ),
551
+ Row (1002 , " John" , 1005 ),
552
+ Row (1003 , " David" , 1005 ),
553
+ Row (1004 , " David" , 1005 ),
554
+ Row (1005 , " Jane" , 1005 ),
555
+ Row (1006 , " Tommy" , 1005 ))
556
+ assertSameRows(expectedResults, frame)
557
+
558
+ val logicalPlan : LogicalPlan = frame.queryExecution.logical
559
+
560
+ val outer =
561
+ SubqueryAlias (" o" , UnresolvedRelation (Seq (" spark_catalog" , " default" , " flint_ppl_test1" )))
562
+ val inner =
563
+ SubqueryAlias (" i" , UnresolvedRelation (Seq (" spark_catalog" , " default" , " flint_ppl_test2" )))
564
+ val aggregateExpressions = Seq (
565
+ Alias (
566
+ UnresolvedFunction (Seq (" MAX" ), Seq (UnresolvedAttribute (" i.uid" )), isDistinct = false ),
567
+ " max(i.uid)" )())
568
+ val innerFilter =
569
+ Filter (EqualTo (UnresolvedAttribute (" i.department" ), Literal (" DATA" )), inner)
570
+ val aggregatePlan = Aggregate (Seq (), aggregateExpressions, innerFilter)
571
+ val scalarSubqueryExpr = Alias (ScalarSubquery (aggregatePlan), " max_uid" )()
572
+ val outerFilter = Project (Seq (UnresolvedStar (None ), scalarSubqueryExpr), outer)
573
+ val expectedPlan =
574
+ Project (
575
+ Seq (
576
+ UnresolvedAttribute (" o.id" ),
577
+ UnresolvedAttribute (" o.name" ),
578
+ UnresolvedAttribute (" max_uid" )),
579
+ outerFilter)
580
+
581
+ comparePlans(expectedPlan, logicalPlan, checkAnalysis = false )
582
+ }
484
583
}
0 commit comments