File tree Expand file tree Collapse file tree
snapshots/test_tpch/test_compile Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -856,7 +856,27 @@ def selection(
856856 for relname , rel in rels :
857857 if relname == "aggregate" and rel .measures :
858858 mapping_counter = itertools .count (
859- len (rel .measures ) + len (rel .groupings )
859+ len (rel .measures )
860+ # Individual groups can have multiple grouping expressions
861+ # and we need to count all of these to properly index projections
862+ # e.g. for the following query
863+ # SELECT
864+ # t0.b,
865+ # t0.sum
866+ # FROM (
867+ # SELECT
868+ # t1.a AS a,
869+ # t1.b AS b,
870+ # SUM(t1.c) AS sum
871+ # FROM t AS t1
872+ # GROUP BY
873+ # t1.a,
874+ # t1.b
875+ # ) AS t0
876+ #
877+ # the two grouping keys (t1.a, t1.b) will be grouping
878+ # expressions in the first (and only) group.
879+ + sum (len (group .grouping_expressions ) for group in rel .groupings )
860880 )
861881 break
862882 elif output_mapping := rel .common .emit .output_mapping :
@@ -1092,9 +1112,10 @@ def aggregation(
10921112 input = input ,
10931113 groupings = [
10941114 stalg .AggregateRel .Grouping (
1095- grouping_expressions = [translate (by , compiler = compiler , ** kwargs )]
1115+ grouping_expressions = [
1116+ translate (by , compiler = compiler , ** kwargs ) for by in op .by
1117+ ]
10961118 )
1097- for by in op .by
10981119 ],
10991120 measures = [
11001121 stalg .AggregateRel .Measure (
Original file line number Diff line number Diff line change 243243 },
244244 "rootReference" : {}
245245 }
246- }
247- ]
248- },
249- {
250- "groupingExpressions" : [
246+ },
251247 {
252248 "selection" : {
253249 "directReference" : {
Original file line number Diff line number Diff line change 567567 },
568568 "rootReference" : {}
569569 }
570- }
571- ]
572- },
573- {
574- "groupingExpressions" : [
570+ },
575571 {
576572 "selection" : {
577573 "directReference" : {
581577 },
582578 "rootReference" : {}
583579 }
584- }
585- ]
586- },
587- {
588- "groupingExpressions" : [
580+ },
589581 {
590582 "selection" : {
591583 "directReference" : {
Original file line number Diff line number Diff line change 370370 }
371371 }
372372 },
373+ "groupings" : [
374+ {}
375+ ],
373376 "measures" : [
374377 {
375378 "measure" : {
Original file line number Diff line number Diff line change 11281128 },
11291129 "rootReference" : {}
11301130 }
1131- }
1132- ]
1133- },
1134- {
1135- "groupingExpressions" : [
1131+ },
11361132 {
11371133 "selection" : {
11381134 "directReference" : {
11421138 },
11431139 "rootReference" : {}
11441140 }
1145- }
1146- ]
1147- },
1148- {
1149- "groupingExpressions" : [
1141+ },
11501142 {
11511143 "selection" : {
11521144 "directReference" : {
Original file line number Diff line number Diff line change 10371037 },
10381038 "rootReference" : {}
10391039 }
1040- }
1041- ]
1042- },
1043- {
1044- "groupingExpressions" : [
1040+ },
10451041 {
10461042 "selection" : {
10471043 "directReference" : {
Original file line number Diff line number Diff line change 10251025 },
10261026 "rootReference" : {}
10271027 }
1028- }
1029- ]
1030- },
1031- {
1032- "groupingExpressions" : [
1028+ },
10331029 {
10341030 "selection" : {
10351031 "directReference" : {
Original file line number Diff line number Diff line change 651651 },
652652 "rootReference" : {}
653653 }
654- }
655- ]
656- },
657- {
658- "groupingExpressions" : [
654+ },
659655 {
660656 "selection" : {
661657 "directReference" : {
665661 },
666662 "rootReference" : {}
667663 }
668- }
669- ]
670- },
671- {
672- "groupingExpressions" : [
664+ },
673665 {
674666 "selection" : {
675667 "directReference" : {
679671 },
680672 "rootReference" : {}
681673 }
682- }
683- ]
684- },
685- {
686- "groupingExpressions" : [
674+ },
687675 {
688676 "selection" : {
689677 "directReference" : {
693681 },
694682 "rootReference" : {}
695683 }
696- }
697- ]
698- },
699- {
700- "groupingExpressions" : [
684+ },
701685 {
702686 "selection" : {
703687 "directReference" : {
707691 },
708692 "rootReference" : {}
709693 }
710- }
711- ]
712- },
713- {
714- "groupingExpressions" : [
694+ },
715695 {
716696 "selection" : {
717697 "directReference" : {
721701 },
722702 "rootReference" : {}
723703 }
724- }
725- ]
726- },
727- {
728- "groupingExpressions" : [
704+ },
729705 {
730706 "selection" : {
731707 "directReference" : {
Original file line number Diff line number Diff line change 427427 },
428428 "rootReference" : {}
429429 }
430- }
431- ]
432- },
433- {
434- "groupingExpressions" : [
430+ },
435431 {
436432 "selection" : {
437433 "directReference" : {
438434 "structField" : {}
439435 },
440436 "rootReference" : {}
441437 }
442- }
443- ]
444- },
445- {
446- "groupingExpressions" : [
438+ },
447439 {
448440 "selection" : {
449441 "directReference" : {
453445 },
454446 "rootReference" : {}
455447 }
456- }
457- ]
458- },
459- {
460- "groupingExpressions" : [
448+ },
461449 {
462450 "selection" : {
463451 "directReference" : {
467455 },
468456 "rootReference" : {}
469457 }
470- }
471- ]
472- },
473- {
474- "groupingExpressions" : [
458+ },
475459 {
476460 "selection" : {
477461 "directReference" : {
Original file line number Diff line number Diff line change 13221322 }
13231323 }
13241324 },
1325+ "groupings" : [
1326+ {}
1327+ ],
13251328 "measures" : [
13261329 {
13271330 "measure" : {
You can’t perform that action at this time.
0 commit comments