YiwenWu commented on code in PR #3883:
URL: https://github.com/apache/calcite/pull/3883#discussion_r1708398194


##########
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAsofJoin.java:
##########
@@ -0,0 +1,253 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.adapter.enumerable;
+
+import org.apache.calcite.linq4j.tree.BlockBuilder;
+import org.apache.calcite.linq4j.tree.Expression;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.plan.DeriveMode;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollationTraitDef;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.logical.LogicalAsofJoin;
+import org.apache.calcite.rel.metadata.RelMdCollation;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.util.BuiltInMethod;
+import org.apache.calcite.util.Pair;
+
+import com.google.common.collect.ImmutableList;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/** Implementation of {@link LogicalAsofJoin} in
+ * {@link EnumerableConvention enumerable calling convention}. */
+public class EnumerableAsofJoin extends Join implements EnumerableRel {
+  // This implementation is based on {@link EnumerableHashJoin}
+
+  /** Compared to standard joins, ASOF joins have an additional condition for 
comparing timestamps.
+   * This is the additional condition. */
+  final RexNode matchCondition;
+
+  /** Creates an EnumerableAsofJoin.
+   *
+   * <p>Use {@link #create} unless you know what you're doing. */
+  protected EnumerableAsofJoin(
+      RelOptCluster cluster,
+      RelTraitSet traits,
+      RelNode left,
+      RelNode right,
+      RexNode condition,
+      RexNode matchCondition,
+      Set<CorrelationId> variablesSet,
+      JoinRelType joinType) {
+    super(
+        cluster,
+        traits,
+        ImmutableList.of(),
+        left,
+        right,
+        condition,
+        variablesSet,
+        joinType);
+    this.matchCondition = matchCondition;
+  }
+
+  /** Creates an EnumerableAsofJoin. */
+  public static EnumerableAsofJoin create(
+      RelNode left,
+      RelNode right,
+      RexNode condition,
+      RexNode matchCondition,
+      Set<CorrelationId> variablesSet,
+      JoinRelType joinType) {
+    final RelOptCluster cluster = left.getCluster();
+    final RelMetadataQuery mq = cluster.getMetadataQuery();
+    final RelTraitSet traitSet =
+        cluster.traitSetOf(EnumerableConvention.INSTANCE)
+            .replaceIfs(RelCollationTraitDef.INSTANCE,
+                () -> RelMdCollation.enumerableHashJoin(mq, left, right, 
joinType));
+    return new EnumerableAsofJoin(cluster, traitSet, left, right, condition, 
matchCondition,
+        variablesSet, joinType);
+  }
+
+  @Override public EnumerableAsofJoin copy(RelTraitSet traitSet, RexNode 
condition,
+                                           RelNode left, RelNode right, 
JoinRelType joinType,
+                                           boolean semiJoinDone) {
+    // This method does not know about the matchCondition, so it should not be 
called
+    throw new RuntimeException("This method should not be called");
+  }
+
+  @Override public Join copy(RelTraitSet traitSet, List<RelNode> inputs) {
+    assert inputs.size() == 2;
+    return new EnumerableAsofJoin(
+        getCluster(), traitSet, inputs.get(0), inputs.get(1),
+            getCondition(), matchCondition, variablesSet, joinType);
+  }
+
+  @Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> 
passThroughTraits(
+      final RelTraitSet required) {
+    return EnumerableTraitsUtils.passThroughTraitsForJoin(
+        required, joinType, left.getRowType().getFieldCount(), getTraitSet());
+  }
+
+  @Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
+      final RelTraitSet childTraits, final int childId) {
+    // should only derive traits (limited to collation for now) from left join 
input.
+    return EnumerableTraitsUtils.deriveTraitsForJoin(
+        childTraits, childId, joinType, getTraitSet(), right.getTraitSet());
+  }
+
+  @Override public DeriveMode getDeriveMode() {
+    return DeriveMode.LEFT_FIRST;
+  }
+
+  @Override public @Nullable RelOptCost computeSelfCost(RelOptPlanner planner,
+      RelMetadataQuery mq) {
+    double rowCount = mq.getRowCount(this);
+    return planner.getCostFactory().makeCost(rowCount, 0, 0);
+  }
+
+  /** Generate the function that compares two rows from the right collection 
on their
+   * timestamp field.
+   *
+   * @param rightCollectionType  Type of data in right collection.
+   * @param kind                 Comparison kind.
+   * @param timestampFieldIndex  Index of the field that is the timestamp 
field.
+   */
+  private Expression generateTimestampComparator(
+      PhysType rightCollectionType, SqlKind kind, int timestampFieldIndex) {
+    RelFieldCollation.Direction direction;
+    switch (kind) {
+    case LESS_THAN:
+    case LESS_THAN_OR_EQUAL:
+      direction = RelFieldCollation.Direction.ASCENDING;
+      break;
+    case GREATER_THAN:
+    case GREATER_THAN_OR_EQUAL:
+      direction = RelFieldCollation.Direction.DESCENDING;
+      break;
+    default:
+      throw new RuntimeException("Unexpected timestamp comparison in ASOF join 
" + kind);
+    }
+
+    final List<RelFieldCollation> fieldCollations = new ArrayList<>(1);
+    fieldCollations.add(
+        new RelFieldCollation(timestampFieldIndex, direction,
+            RelFieldCollation.NullDirection.FIRST));
+    final RelCollation collation = RelCollations.of(fieldCollations);
+    return rightCollectionType.generateComparator(collation);
+  }
+
+  /** Extract from a comparison 'call' the index of the field from
+   * the inner collection that is used in the comparison. */
+  private int getTimestampFieldIndex(RexCall call) {
+    int timestampFieldIndex;
+    int leftFieldCount = left.getRowType().getFieldCount();
+    List<RexNode> operands = call.getOperands();
+    assert operands.size() == 2;
+    RexNode compareLeft = operands.get(0);
+    RexNode compareRight = operands.get(1);
+    assert compareLeft instanceof RexInputRef;
+    assert compareRight instanceof RexInputRef;
+    RexInputRef leftInputRef = (RexInputRef) compareLeft;
+    RexInputRef rightInputRef = (RexInputRef) compareRight;
+    // We know for sure that these two come from the inner and outer 
collection respectively,
+    // but we don't know which is which
+    if (leftInputRef.getIndex() < leftFieldCount) {
+      // Left input comes from the left collection
+      timestampFieldIndex = rightInputRef.getIndex() - leftFieldCount;
+    } else {
+      // Left input comes from the right collection
+      timestampFieldIndex = leftInputRef.getIndex() - leftFieldCount;
+    }
+    return timestampFieldIndex;
+  }
+
+  @Override public Result implement(EnumerableRelImplementor implementor, 
Prefer pref) {
+    BlockBuilder builder = new BlockBuilder();
+    final Result leftResult =
+        implementor.visitChild(this, 0, (EnumerableRel) left, pref);
+    Expression leftExpression =
+        builder.append(
+            "left", leftResult.block);
+    final Result rightResult =
+        implementor.visitChild(this, 1, (EnumerableRel) right, pref);
+    Expression rightExpression =
+        builder.append(
+            "right", rightResult.block);
+    final PhysType physType =
+        PhysTypeImpl.of(
+            implementor.getTypeFactory(), getRowType(), pref.preferArray());
+    // ASOF joins conditions are restricted to equalities
+    assert joinInfo.nonEquiConditions.isEmpty();
+
+    // From the match condition we need to find out the kind of comparison 
performed
+    // and the timestamp field in the right collection.
+    assert matchCondition instanceof RexCall;
+    RexCall call = (RexCall) matchCondition;
+    SqlKind kind = call.getKind();
+    int timestampFieldIndex = getTimestampFieldIndex(call);
+
+    Expression timestampComparator =
+        generateTimestampComparator(rightResult.physType, kind, 
timestampFieldIndex);
+
+    Expression matchPredicate =
+        EnumUtils.generatePredicate(implementor, getCluster().getRexBuilder(),
+            left, right, leftResult.physType, rightResult.physType, 
matchCondition);
+    return implementor.result(
+        physType,
+        builder.append(
+            Expressions.call(
+                leftExpression,
+                BuiltInMethod.ASOF_JOIN.method,
+                Expressions.list(
+                    rightExpression,
+                    // outer key selector
+                    
leftResult.physType.generateAccessorWithoutNulls(joinInfo.leftKeys),
+                    // inner key selector
+                    
rightResult.physType.generateAccessorWithoutNulls(joinInfo.rightKeys),
+                    // result selector
+                    EnumUtils.joinSelector(joinType,
+                        physType,
+                        ImmutableList.of(
+                            leftResult.physType, rightResult.physType)))
+                    // match comparator
+                    .append(matchPredicate)
+                    // timestamp comparator
+                    .append(timestampComparator)

Review Comment:
   Is the comparison limited to `timestamp` only?



##########
core/src/main/java/org/apache/calcite/rel/logical/LogicalAsofJoin.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.logical;
+
+import org.apache.calcite.plan.Convention;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelInput;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelShuttle;
+import org.apache.calcite.rel.RelWriter;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.hint.RelHint;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexNode;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Sub-class of {@link Join} encoding ASOF joins.
+ * Adapted from the {@link LogicalJoin} implementation.
+ */
+public final class LogicalAsofJoin extends Join {
+  //~ Instance fields --------------------------------------------------------
+
+  final RexNode matchCondition;
+  private final ImmutableList<RelDataTypeField> systemFieldList;
+
+  //~ Constructors -----------------------------------------------------------
+
+  /**
+   * Creates an AsofJoin.
+   *
+   * <p>Use {@link #create} unless you know what you're doing.
+   *
+   * @param cluster          Cluster
+   * @param traitSet         Trait set
+   * @param hints            Hints
+   * @param left             Left input
+   * @param right            Right input
+   * @param condition        Join condition
+   * @param matchCondition   Temporal condition
+   * @param systemFieldList  List of system fields that will be prefixed to
+   *                         output row type; typically empty but must not be 
null
+   */
+  public LogicalAsofJoin(
+      RelOptCluster cluster,
+      RelTraitSet traitSet,
+      List<RelHint> hints,
+      RelNode left,
+      RelNode right,
+      RexNode condition,
+      RexNode matchCondition,
+      JoinRelType joinType,
+      ImmutableList<RelDataTypeField> systemFieldList) {
+    super(cluster, traitSet, hints, left, right, condition, ImmutableSet.of(), 
joinType);
+    this.systemFieldList = requireNonNull(systemFieldList, "systemFieldList");
+    this.matchCondition = matchCondition;

Review Comment:
   limit `requireNonNull(matchCondition)`? 



##########
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAsofJoin.java:
##########
@@ -0,0 +1,253 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.adapter.enumerable;
+
+import org.apache.calcite.linq4j.tree.BlockBuilder;
+import org.apache.calcite.linq4j.tree.Expression;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.plan.DeriveMode;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollationTraitDef;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.logical.LogicalAsofJoin;
+import org.apache.calcite.rel.metadata.RelMdCollation;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.util.BuiltInMethod;
+import org.apache.calcite.util.Pair;
+
+import com.google.common.collect.ImmutableList;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/** Implementation of {@link LogicalAsofJoin} in
+ * {@link EnumerableConvention enumerable calling convention}. */
+public class EnumerableAsofJoin extends Join implements EnumerableRel {
+  // This implementation is based on {@link EnumerableHashJoin}
+
+  /** Compared to standard joins, ASOF joins have an additional condition for 
comparing timestamps.
+   * This is the additional condition. */
+  final RexNode matchCondition;
+
+  /** Creates an EnumerableAsofJoin.
+   *
+   * <p>Use {@link #create} unless you know what you're doing. */
+  protected EnumerableAsofJoin(
+      RelOptCluster cluster,
+      RelTraitSet traits,
+      RelNode left,
+      RelNode right,
+      RexNode condition,
+      RexNode matchCondition,
+      Set<CorrelationId> variablesSet,
+      JoinRelType joinType) {
+    super(
+        cluster,
+        traits,
+        ImmutableList.of(),
+        left,
+        right,
+        condition,
+        variablesSet,
+        joinType);
+    this.matchCondition = matchCondition;
+  }
+
+  /** Creates an EnumerableAsofJoin. */
+  public static EnumerableAsofJoin create(
+      RelNode left,
+      RelNode right,
+      RexNode condition,
+      RexNode matchCondition,
+      Set<CorrelationId> variablesSet,
+      JoinRelType joinType) {
+    final RelOptCluster cluster = left.getCluster();
+    final RelMetadataQuery mq = cluster.getMetadataQuery();
+    final RelTraitSet traitSet =
+        cluster.traitSetOf(EnumerableConvention.INSTANCE)
+            .replaceIfs(RelCollationTraitDef.INSTANCE,
+                () -> RelMdCollation.enumerableHashJoin(mq, left, right, 
joinType));
+    return new EnumerableAsofJoin(cluster, traitSet, left, right, condition, 
matchCondition,
+        variablesSet, joinType);
+  }
+
+  @Override public EnumerableAsofJoin copy(RelTraitSet traitSet, RexNode 
condition,
+                                           RelNode left, RelNode right, 
JoinRelType joinType,
+                                           boolean semiJoinDone) {
+    // This method does not know about the matchCondition, so it should not be 
called

Review Comment:
   Is it necessary to support the copy of changing `matchCondition`?
   



##########
core/src/main/java/org/apache/calcite/sql/JoinType.java:
##########
@@ -66,7 +66,28 @@ public enum JoinType implements Symbolizable {
    * where table expressions are specified with commas between them, and
    * join conditions are specified in the <code>WHERE</code> clause.
    */
-  COMMA;
+  COMMA,
+
+  /**
+   * An ASOF JOIN operation combines rows from two tables based on comparable 
timestamp values.
+   * For each row in the left table, the join finds a single row in the right 
table that has the

Review Comment:
   Is there any confusion in this description? From the description, it may be 
understood as `left join`.



##########
core/src/main/codegen/templates/Parser.jj:
##########
@@ -2095,13 +2099,32 @@ SqlNode JoinTable(SqlNode e) :
     //
     // We allow CROSS JOIN (joinType = CROSS_JOIN) to have a join condition,
     // even though that is not valid SQL; the validator will catch it.
-    LOOKAHEAD(3)
+    LOOKAHEAD(4)
     natural = Natural()
     joinType = JoinType()
     e2 = TableRef1(ExprContext.ACCEPT_QUERY_OR_JOIN)
     (
+        [ <MATCH_CONDITION> matchCondition = 
Expression(ExprContext.ACCEPT_SUB_QUERY) ]
         <ON> { on = JoinConditionType.ON.symbol(getPos()); }
         condition = Expression(ExprContext.ACCEPT_SUB_QUERY) {
+            JoinType type = joinType.getValueAs(JoinType.class);
+            if (matchCondition != null) {
+                if (type != JoinType.ASOF && type != JoinType.LEFT_ASOF) {
+                    throw SqlUtil.newContextException(getPos(), 
RESOURCE.matchConditionRequiresAsof());
+                }
+                return new SqlAsofJoin(joinType.getParserPosition(),
+                    e,
+                    natural,
+                    joinType,
+                    e2,
+                    on,
+                    condition,
+                    matchCondition);
+            } else {
+                if (type == JoinType.ASOF && type != JoinType.LEFT_ASOF) {

Review Comment:
   The condition is `type == JoinType.ASOF || type == JoinType.LEFT_ASOF` ?



##########
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAsofJoin.java:
##########
@@ -0,0 +1,253 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.adapter.enumerable;
+
+import org.apache.calcite.linq4j.tree.BlockBuilder;
+import org.apache.calcite.linq4j.tree.Expression;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.plan.DeriveMode;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollationTraitDef;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.logical.LogicalAsofJoin;
+import org.apache.calcite.rel.metadata.RelMdCollation;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.util.BuiltInMethod;
+import org.apache.calcite.util.Pair;
+
+import com.google.common.collect.ImmutableList;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/** Implementation of {@link LogicalAsofJoin} in

Review Comment:
   minor: `Implementation of {@link Join}` not `LogicalAsofJoin`



##########
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAsofJoin.java:
##########
@@ -0,0 +1,253 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.adapter.enumerable;
+
+import org.apache.calcite.linq4j.tree.BlockBuilder;
+import org.apache.calcite.linq4j.tree.Expression;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.plan.DeriveMode;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollationTraitDef;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.logical.LogicalAsofJoin;
+import org.apache.calcite.rel.metadata.RelMdCollation;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.util.BuiltInMethod;
+import org.apache.calcite.util.Pair;
+
+import com.google.common.collect.ImmutableList;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/** Implementation of {@link LogicalAsofJoin} in
+ * {@link EnumerableConvention enumerable calling convention}. */
+public class EnumerableAsofJoin extends Join implements EnumerableRel {
+  // This implementation is based on {@link EnumerableHashJoin}

Review Comment:
   `This implementation is based on {@link EnumerableHashJoin}`, 
   Can this class directly extends `EnumerableHashJoin`?



##########
core/src/main/java/org/apache/calcite/sql/SqlAsofJoin.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.sql;
+
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.util.SqlString;
+import org.apache.calcite.util.ImmutableNullableList;
+import org.apache.calcite.util.Util;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.List;
+import java.util.function.UnaryOperator;
+
+/**
+ * Parse tree node representing a {@code ASOF JOIN} clause.
+ */
+public class SqlAsofJoin extends SqlJoin {
+  SqlNode matchCondition;
+  static final SqlAsofJoinOperator ASOF_OPERATOR =
+      new SqlAsofJoinOperator("ASOF-JOIN", 16);
+
+  //~ Constructors -----------------------------------------------------------
+
+  public SqlAsofJoin(SqlParserPos pos, SqlNode left, SqlLiteral natural,
+                     SqlLiteral joinType, SqlNode right, SqlLiteral 
conditionType,
+                     @Nullable SqlNode condition, SqlNode matchCondition) {
+    super(pos, left, natural, joinType, right, conditionType, condition);
+    this.matchCondition = matchCondition;
+  }
+
+  @SuppressWarnings("nullness")
+  @Override public List<SqlNode> getOperandList() {
+    return ImmutableNullableList.of(left, natural, joinType, right,
+        conditionType, condition, matchCondition);
+  }
+
+  @Override public SqlOperator getOperator() {
+    return ASOF_OPERATOR;
+  }
+
+  @SuppressWarnings("assignment.type.incompatible")
+  @Override public void setOperand(int i, @Nullable SqlNode operand) {
+    switch (i) {
+    case 0:
+      left = operand;
+      break;
+    case 1:
+      natural = (SqlLiteral) operand;
+      break;
+    case 2:
+      joinType = (SqlLiteral) operand;
+      break;
+    case 3:
+      right = operand;
+      break;
+    case 4:
+      conditionType = (SqlLiteral) operand;
+      break;
+    case 5:
+      condition = operand;
+      break;
+    case 6:
+      matchCondition = operand;
+      break;
+    default:
+      throw new AssertionError(i);
+    }
+  }
+
+  /**
+   * The match condition of the ASOF JOIN.
+   *
+   * @return The match condition of the ASOF join.
+   */
+  public final SqlNode getMatchCondition() {
+    return matchCondition;
+  }
+
+  /**
+   * Describes the syntax of the SQL ASOF JOIN operator.
+   */
+  public static class SqlAsofJoinOperator extends SqlOperator {
+    //~ Constructors 
-----------------------------------------------------------
+
+    private SqlAsofJoinOperator(String name, int prec) {
+      super(name, SqlKind.JOIN, prec, true, null, null, null);
+    }
+
+    //~ Methods 
----------------------------------------------------------------
+
+    @Override public SqlSyntax getSyntax() {
+      return SqlSyntax.SPECIAL;
+    }
+
+    @SuppressWarnings("argument.type.incompatible")
+    @Override public SqlCall createCall(
+        @Nullable SqlLiteral functionQualifier,
+        SqlParserPos pos,
+        @Nullable SqlNode... operands) {
+      assert functionQualifier == null;
+      return new SqlAsofJoin(pos, operands[0], (SqlLiteral) operands[1],
+          (SqlLiteral) operands[2], operands[3], (SqlLiteral) operands[4],
+          operands[5], operands[6]);
+    }
+
+    @Override public void unparse(
+        SqlWriter writer,
+        SqlCall call,
+        int leftPrec,
+        int rightPrec) {
+      final SqlAsofJoin join = (SqlAsofJoin) call;
+
+      join.left.unparse(
+          writer,
+          leftPrec,
+          getLeftPrec());
+      if (join.getJoinType() == JoinType.LEFT_ASOF) {
+        writer.sep("LEFT ASOF JOIN");
+      } else {
+        writer.sep("ASOF JOIN");
+      }
+      join.right.unparse(writer, getRightPrec(), rightPrec);
+      SqlNode matchCondition = join.matchCondition;
+      writer.keyword("MATCH_CONDITION");
+      matchCondition.unparse(writer, 0, 0);
+
+      SqlNode joinCondition = join.condition;
+      if (joinCondition != null) {
+        switch (join.getConditionType()) {
+        case ON:
+          writer.keyword("ON");

Review Comment:
   Does this restriction need to be processed in sql `Parser.jj`?



##########
core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java:
##########
@@ -386,6 +386,8 @@ public static JoinType joinType(JoinRelType joinType) {
       return JoinType.INNER;
     case FULL:
       return JoinType.FULL;
+    case ASOF:
+      return JoinType.ASOF;

Review Comment:
   missing `LEFT_ASOF` case?



##########
core/src/main/java/org/apache/calcite/rel/core/RelFactories.java:
##########
@@ -398,6 +402,23 @@ RelNode createJoin(RelNode left, RelNode right, 
List<RelHint> hints,
         boolean semiJoinDone);
   }
 
+  /**
+   * Creates ASOF join of the appropriate type for a rule's calling convention.
+   */
+  public interface AsofJoinFactory {
+    /**
+     * Creates an ASOF join.
+     *
+     * @param left             Left input
+     * @param right            Right input
+     * @param hints            Hints
+     * @param condition        Join condition
+     * @param matchCondition   ASOF join match condition

Review Comment:
   missing `joinType`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to