55
66package org .opensearch .sql .calcite .validate ;
77
8+ import org .apache .calcite .rel .core .Correlate ;
9+ import org .apache .calcite .rel .core .JoinRelType ;
810import org .apache .calcite .rel .rel2sql .RelToSqlConverter ;
11+ import org .apache .calcite .sql .JoinConditionType ;
12+ import org .apache .calcite .sql .JoinType ;
913import org .apache .calcite .sql .SqlDialect ;
14+ import org .apache .calcite .sql .SqlJoin ;
15+ import org .apache .calcite .sql .SqlLiteral ;
16+ import org .apache .calcite .sql .SqlNode ;
1017
1118/**
1219 * An extension of {@link RelToSqlConverter} to convert a relation algebra tree, translated from a
@@ -24,4 +31,26 @@ public class PplRelToSqlNodeConverter extends RelToSqlConverter {
2431 public PplRelToSqlNodeConverter (SqlDialect dialect ) {
2532 super (dialect );
2633 }
34+
35+ /** Override Correlate visitor to pass on join type */
36+ @ Override
37+ public Result visit (Correlate e ) {
38+ Result result = super .visit (e );
39+ SqlNode from = result .asSelect ().getFrom ();
40+ if (e .getJoinType () != JoinRelType .INNER && from instanceof SqlJoin join ) {
41+ JoinType joinType ;
42+ try {
43+ joinType = JoinType .valueOf (e .getJoinType ().name ());
44+ } catch (IllegalArgumentException ignored ) {
45+ return result ;
46+ }
47+ join .setOperand (2 , joinType .symbol (POS ));
48+ // INNER, LEFT, RIGHT, FULL, or ASOF join requires a condition
49+ // Use ON TRUE to satisfy SQL syntax because the actual correlation condition logic is inside
50+ // the subquery's WHERE clause
51+ join .setOperand (4 , JoinConditionType .ON .symbol (POS ));
52+ join .setOperand (5 , SqlLiteral .createBoolean (true , POS ));
53+ }
54+ return result ;
55+ }
2756}
0 commit comments