|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.calcite.validate; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import org.apache.calcite.plan.RelOptCluster; |
| 10 | +import org.apache.calcite.plan.RelOptTable; |
| 11 | +import org.apache.calcite.prepare.Prepare; |
| 12 | +import org.apache.calcite.rel.RelNode; |
| 13 | +import org.apache.calcite.rel.core.JoinRelType; |
| 14 | +import org.apache.calcite.rel.logical.LogicalJoin; |
| 15 | +import org.apache.calcite.rel.logical.LogicalProject; |
| 16 | +import org.apache.calcite.sql.JoinType; |
| 17 | +import org.apache.calcite.sql.SqlJoin; |
| 18 | +import org.apache.calcite.sql.SqlNode; |
| 19 | +import org.apache.calcite.sql.validate.SqlValidator; |
| 20 | +import org.apache.calcite.sql2rel.SqlRexConvertletTable; |
| 21 | +import org.apache.calcite.sql2rel.SqlToRelConverter; |
| 22 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 23 | + |
| 24 | +public class PplSqlToRelConverter extends SqlToRelConverter { |
| 25 | + public PplSqlToRelConverter( |
| 26 | + RelOptTable.ViewExpander viewExpander, |
| 27 | + @Nullable SqlValidator validator, |
| 28 | + Prepare.CatalogReader catalogReader, |
| 29 | + RelOptCluster cluster, |
| 30 | + SqlRexConvertletTable convertletTable, |
| 31 | + Config config) { |
| 32 | + super(viewExpander, validator, catalogReader, cluster, convertletTable, config); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Override to support Spark SQL's LEFT ANTI JOIN and LEFT SEMI JOIN conversion to RelNode. |
| 37 | + * |
| 38 | + * <p>The default implementation in {@link SqlToRelConverter#convertJoinType} does not expect |
| 39 | + * LEFT_ANTI_JOIN and LEFT_SEMI_JOIN. This override works around the limitation by first |
| 40 | + * temporarily changing LEFT_ANTI_JOIN/LEFT_SEMI_JOIN to LEFT join in the SqlJoin node, then |
| 41 | + * calling {@code super.convertFrom()} to perform normal conversion, finally substituting the join |
| 42 | + * type in the resulting RelNode to ANTI/SEMI. |
| 43 | + * |
| 44 | + * @param bb Scope within which to resolve identifiers |
| 45 | + * @param from FROM clause of a query. |
| 46 | + * @param fieldNames Field aliases, usually come from AS clause, or null |
| 47 | + */ |
| 48 | + @Override |
| 49 | + protected void convertFrom( |
| 50 | + Blackboard bb, @Nullable SqlNode from, @Nullable List<String> fieldNames) { |
| 51 | + JoinType originalJoinType = null; |
| 52 | + if (from instanceof SqlJoin join) { |
| 53 | + JoinType joinType = join.getJoinType(); |
| 54 | + if (joinType == JoinType.LEFT_SEMI_JOIN || joinType == JoinType.LEFT_ANTI_JOIN) { |
| 55 | + join.setOperand(2, JoinType.LEFT.symbol(from.getParserPosition())); |
| 56 | + originalJoinType = joinType; |
| 57 | + } |
| 58 | + } |
| 59 | + super.convertFrom(bb, from, fieldNames); |
| 60 | + if (originalJoinType != null) { |
| 61 | + RelNode root = bb.root(); |
| 62 | + if (root != null) { |
| 63 | + JoinRelType correctJoinType = |
| 64 | + originalJoinType == JoinType.LEFT_SEMI_JOIN ? JoinRelType.SEMI : JoinRelType.ANTI; |
| 65 | + RelNode fixedRoot = modifyJoinType(root, correctJoinType); |
| 66 | + bb.setRoot(fixedRoot, false); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private RelNode modifyJoinType(RelNode root, JoinRelType correctJoinType) { |
| 72 | + if (root instanceof LogicalProject project) { |
| 73 | + RelNode input = project.getInput(); |
| 74 | + RelNode fixedInput = modifyJoinType(input, correctJoinType); |
| 75 | + if (fixedInput != input) { |
| 76 | + return project.copy( |
| 77 | + project.getTraitSet(), fixedInput, project.getProjects(), project.getRowType()); |
| 78 | + } |
| 79 | + } else if (root instanceof LogicalJoin join) { |
| 80 | + if (join.getJoinType() == JoinRelType.LEFT) { |
| 81 | + return join.copy( |
| 82 | + join.getTraitSet(), |
| 83 | + join.getCondition(), |
| 84 | + join.getLeft(), |
| 85 | + join.getRight(), |
| 86 | + correctJoinType, |
| 87 | + join.isSemiJoinDone()); |
| 88 | + } |
| 89 | + } |
| 90 | + return root; |
| 91 | + } |
| 92 | +} |
0 commit comments