This is an automated email from the ASF dual-hosted git repository.

caogaofei pushed a commit to branch asof_join
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 46229828756351e8b83f193c35dad0b9638271a9
Author: Beyyes <[email protected]>
AuthorDate: Wed Feb 5 20:58:53 2025 +0800

    fix antlr
---
 .../plan/relational/sql/ast/AsofJoinOn.java        | 74 ++++++++++++++++++++++
 .../plan/relational/sql/parser/AstBuilder.java     |  8 ++-
 .../db/relational/grammar/sql/RelationalSql.g4     | 27 ++++++++
 3 files changed, 108 insertions(+), 1 deletion(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/AsofJoinOn.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/AsofJoinOn.java
new file mode 100644
index 00000000000..45d69bd2467
--- /dev/null
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/AsofJoinOn.java
@@ -0,0 +1,74 @@
+/*
+ * 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.iotdb.db.queryengine.plan.relational.sql.ast;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+public class AsofJoinOn extends JoinCriteria {
+
+  private final Expression expression;
+
+  private final long toleranceValue;
+
+  public AsofJoinOn(final Expression expression, final long toleranceValue) {
+    this.expression = expression;
+    this.toleranceValue = toleranceValue;
+  }
+
+  public Expression getExpression() {
+    return this.expression;
+  }
+
+  public long getToleranceValue() {
+    return this.toleranceValue;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj) {
+      return true;
+    }
+    if ((obj == null) || (getClass() != obj.getClass())) {
+      return false;
+    }
+    AsofJoinOn o = (AsofJoinOn) obj;
+    return Objects.equals(this.expression, o.expression) && 
this.toleranceValue == o.toleranceValue;
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hash(expression, toleranceValue);
+  }
+
+  @Override
+  public String toString() {
+    return 
toStringHelper(this).addValue(expression).addValue(toleranceValue).toString();
+  }
+
+  @Override
+  public List<Node> getNodes() {
+    return ImmutableList.of(expression);
+  }
+}
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
index 27465268674..b6ac6013f21 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
@@ -42,6 +42,7 @@ import 
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.AlterDB;
 import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.AlterPipe;
 import 
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ArithmeticBinaryExpression;
 import 
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ArithmeticUnaryExpression;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.AsofJoinOn;
 import 
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.BetweenPredicate;
 import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.BinaryLiteral;
 import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.BooleanLiteral;
@@ -2041,7 +2042,12 @@ public class AstBuilder extends 
RelationalSqlBaseVisitor<Node> {
     } else {
       right = (Relation) visit(ctx.rightRelation);
       if (ctx.joinCriteria().ON() != null) {
-        criteria = new JoinOn((Expression) 
visit(ctx.joinCriteria().booleanExpression()));
+        if (ctx.ASOF() != null) {
+          criteria = new AsofJoinOn((Expression) 
visit(ctx.joinCriteria().booleanExpression()), 0);
+          return new Join(getLocation(ctx), Join.Type.INNER, left, right, 
criteria);
+        } else {
+          criteria = new JoinOn((Expression) 
visit(ctx.joinCriteria().booleanExpression()));
+        }
       } else if (ctx.joinCriteria().USING() != null) {
         criteria = new JoinUsing(visit(ctx.joinCriteria().identifier(), 
Identifier.class));
       } else {
diff --git 
a/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4
 
b/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4
index 20180597005..be419e983d5 100644
--- 
a/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4
+++ 
b/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4
@@ -824,6 +824,7 @@ relation
       ( CROSS JOIN right=aliasedRelation
       | joinType JOIN rightRelation=relation joinCriteria
       | NATURAL joinType JOIN right=aliasedRelation
+      | ASOF (toleranceParameter)? JOIN rightRelation=relation joinCriteria
       )                                                     #joinRelation
     | aliasedRelation                                       #relationDefault
     ;
@@ -844,6 +845,31 @@ aliasedRelation
     : relationPrimary (AS? identifier columnAliases?)?
     ;
 
+toleranceParameter
+    : '(' 'tolerance' timeUnit ')'
+    ;
+
+toleranceValue
+    : numericLiteral_
+    | timeInterval
+    ;
+
+timeInterval
+    : numericLiteral_ timeUnit
+    ;
+
+timeUnit
+    : 's'       // seconds
+    | 'ms'      // milliseconds
+    | 'm'       // minutes
+    | 'h'       // hours
+    | 'd'       // days
+    ;
+
+numericLiteral_
+    : DIGIT+ ('.' DIGIT+)?  // Supports integers and floats
+    ;
+
 columnAliases
     : '(' identifier (',' identifier)* ')'
     ;
@@ -1098,6 +1124,7 @@ ANY: 'ANY';
 ARRAY: 'ARRAY';
 AS: 'AS';
 ASC: 'ASC';
+ASOF: 'ASOF';
 AT: 'AT';
 ATTRIBUTE: 'ATTRIBUTE';
 AUTHORIZATION: 'AUTHORIZATION';

Reply via email to