@@ -842,34 +842,40 @@ public RelNode visitExpand(Expand expand, CalcitePlanContext context) {
842842
843843 var relBuilder = context .relBuilder ;
844844
845- // 3 . Get the field to expand
845+ // 2 . Get the field to expand
846846 Field arrayField = expand .getField ();
847847
848- // 5 . Unnest the array field
848+ // 3 . Unnest the array field
849849 // Analyze the array field to get its RexNode
850- RexNode arrayFieldRex = rexVisitor .analyze (arrayField , context );
851-
850+ RexInputRef arrayFieldRex = (RexInputRef ) rexVisitor .analyze (arrayField , context );
852851 // Push the original table to the RelBuilder stack
853852 RelNode originalTable = relBuilder .peek ();
854853 // No alias is provided in the expand command, so we remove the original array field,
855854 // then replace it with the unnest result.
856- relBuilder .projectExcept (arrayFieldRex );
857- relBuilder .push (originalTable );
855+ // relBuilder.projectExcept(arrayFieldRex);
858856
859- // Join on ROW_NUMBER_COLUMN_NAME
857+ // Capture the outer row in a CorrelationId
860858 Holder <RexCorrelVariable > correlVariable = Holder .empty ();
861859 relBuilder .variable (correlVariable ::set );
862860
863- relBuilder .project (List .of (arrayFieldRex ), List .of (), false , List .of (correlVariable .get ().id ));
861+ relBuilder .push (originalTable );
862+
863+ RexNode correlArrayField =
864+ relBuilder .field (
865+ context .rexBuilder .makeCorrel (originalTable .getRowType (), correlVariable .get ().id ),
866+ arrayFieldRex .getIndex ());
867+
868+ relBuilder .project (
869+ List .of (correlArrayField ),
870+ List .of (arrayField .getField ().toString ()),
871+ false ,
872+ List .of (correlVariable .get ().id ));
873+
864874 // Alias is not supported in expand yet, we pass in an empty list
865875 relBuilder .uncollect (List .of (), false );
866876
867- List <RexNode > allFields =
868- relBuilder .peek ().getRowType ().getFieldList ().stream ()
869- .map (f -> (RexNode ) relBuilder .field (f .getName ()))
870- .toList ();
871-
872- relBuilder .correlate (JoinRelType .INNER , correlVariable .get ().id , relBuilder .fields ());
877+ // ImmutableBitSet requiredFields = originalTable.getRowType().getFieldList()
878+ relBuilder .correlate (JoinRelType .INNER , correlVariable .get ().id , arrayFieldRex );
873879
874880 return relBuilder .peek ();
875881 }
0 commit comments