5454import org .apache .calcite .rel .type .RelDataTypeField ;
5555import org .apache .calcite .rex .RexBuilder ;
5656import org .apache .calcite .rex .RexCall ;
57+ import org .apache .calcite .rex .RexCorrelVariable ;
58+ import org .apache .calcite .rex .RexFieldAccess ;
5759import org .apache .calcite .rex .RexInputRef ;
5860import org .apache .calcite .rex .RexLiteral ;
5961import org .apache .calcite .rex .RexLocalRef ;
6062import org .apache .calcite .rex .RexNode ;
6163import org .apache .calcite .rex .RexProgram ;
64+ import org .apache .calcite .rex .RexSubQuery ;
6265import org .apache .calcite .sql .JoinConditionType ;
6366import org .apache .calcite .sql .JoinType ;
6467import org .apache .calcite .sql .SqlAsofJoin ;
@@ -557,6 +560,7 @@ public Result visit(Filter e) {
557560 visitInput (e , 0 , isAnon (), ignoreClauses ,
558561 ImmutableSet .of (Clause .HAVING ));
559562 parseCorrelTable (e , x );
563+ parseCorrelVariables (e .getCondition ());
560564 final Builder builder = x .builder (e );
561565 x .asSelect ().setHaving (
562566 SqlUtil .andExpressions (x .asSelect ().getHaving (),
@@ -569,6 +573,7 @@ public Result visit(Filter e) {
569573 }
570574 parseCorrelTable (e , x );
571575 final Builder builder = x .builder (e );
576+ parseCorrelVariables (e .getCondition ());
572577 if (input instanceof Join ) {
573578 final Context context = x .qualifiedContext ();
574579 if (selectListRequired (context )) {
@@ -604,6 +609,31 @@ private static boolean selectListRequired(Context context) {
604609 return false ;
605610 }
606611
612+ private void parseCorrelVariables (RexNode rexNode ) {
613+ if (rexNode instanceof RexCorrelVariable ) {
614+ final RexCorrelVariable correl = (RexCorrelVariable ) rexNode ;
615+ correlTableMap .putIfAbsent (
616+ correl .id ,
617+ aliasContext (ImmutableMap .of (correl .id .getName (), correl .getType ()), true ));
618+ return ;
619+ }
620+ if (rexNode instanceof RexSubQuery ) {
621+ for (RexNode operand : ((RexSubQuery ) rexNode ).operands ) {
622+ parseCorrelVariables (operand );
623+ }
624+ return ;
625+ }
626+ if (rexNode instanceof RexFieldAccess ) {
627+ parseCorrelVariables (((RexFieldAccess ) rexNode ).getReferenceExpr ());
628+ return ;
629+ }
630+ if (rexNode instanceof RexCall ) {
631+ for (RexNode operand : ((RexCall ) rexNode ).operands ) {
632+ parseCorrelVariables (operand );
633+ }
634+ }
635+ }
636+
607637 /**
608638 * Extracts the table name from a SqlNode if it represents a simple table reference.
609639 * Returns null for complex nodes like subqueries or joins.
@@ -772,6 +802,7 @@ private Builder visitAggregate(Aggregate e, List<Integer> groupKeyList,
772802 // "select a, b, sum(x) from ( ... ) group by a, b"
773803 final boolean ignoreClauses = e .getInput () instanceof Project ;
774804 final Result x = visitInput (e , 0 , isAnon (), ignoreClauses , clauseSet );
805+ parseCorrelTable (e , x );
775806 final Builder builder = x .builder (e );
776807 final List <SqlNode > selectList = new ArrayList <>();
777808 final List <SqlNode > groupByList =
0 commit comments