This is an automated email from the ASF dual-hosted git repository.
jackietien pushed a commit to branch ty/TableModelGrammar
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/ty/TableModelGrammar by this
push:
new ded234f3cdd add parser submodule
ded234f3cdd is described below
commit ded234f3cddab2d964b97e7d3ba60e30b460a885
Author: JackieTien97 <[email protected]>
AuthorDate: Wed Feb 7 12:26:31 2024 +0800
add parser submodule
---
iotdb-core/pom.xml | 1 +
iotdb-core/{ => relational-parser}/pom.xml | 33 +++++-----
.../iotdb/db/relational/sql/parser/AstBuilder.java | 64 +++++++++++++++++++
.../iotdb/db/relational/sql/tree/AstVisitor.java | 37 +++++++++++
.../apache/iotdb/db/relational/sql/tree/Node.java | 71 ++++++++++++++++++++++
.../iotdb/db/relational/sql/tree/NodeLocation.java | 68 +++++++++++++++++++++
6 files changed, 260 insertions(+), 14 deletions(-)
diff --git a/iotdb-core/pom.xml b/iotdb-core/pom.xml
index 8151e23ce3c..e7c71147829 100644
--- a/iotdb-core/pom.xml
+++ b/iotdb-core/pom.xml
@@ -38,5 +38,6 @@
<module>node-commons</module>
<module>tsfile</module>
<module>relational-grammar</module>
+ <module>relational-parser</module>
</modules>
</project>
diff --git a/iotdb-core/pom.xml b/iotdb-core/relational-parser/pom.xml
similarity index 63%
copy from iotdb-core/pom.xml
copy to iotdb-core/relational-parser/pom.xml
index 8151e23ce3c..098788e8675 100644
--- a/iotdb-core/pom.xml
+++ b/iotdb-core/relational-parser/pom.xml
@@ -23,20 +23,25 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.iotdb</groupId>
- <artifactId>iotdb-parent</artifactId>
+ <artifactId>iotdb-core</artifactId>
<version>1.3.1-SNAPSHOT</version>
</parent>
- <artifactId>iotdb-core</artifactId>
- <packaging>pom</packaging>
- <name>IoTDB: Core</name>
- <modules>
- <module>antlr</module>
- <module>confignode</module>
- <module>consensus</module>
- <module>datanode</module>
- <module>metrics</module>
- <module>node-commons</module>
- <module>tsfile</module>
- <module>relational-grammar</module>
- </modules>
+ <artifactId>iotdb-relational-parser</artifactId>
+ <name>IoTDB: Core: Relational-Parser</name>
+ <description>Relational parser for IoTDB.</description>
+ <dependencies>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.antlr</groupId>
+ <artifactId>antlr4-runtime</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.iotdb</groupId>
+ <artifactId>iotdb-relational-grammar</artifactId>
+ <version>1.3.1-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
</project>
diff --git
a/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/parser/AstBuilder.java
b/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/parser/AstBuilder.java
new file mode 100644
index 00000000000..66654fdb83c
--- /dev/null
+++
b/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/parser/AstBuilder.java
@@ -0,0 +1,64 @@
+/*
+ * 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.relational.sql.parser;
+
+import org.apache.iotdb.db.relational.grammar.sql.RelationalSqlBaseVisitor;
+import org.apache.iotdb.db.relational.sql.tree.Node;
+import org.apache.iotdb.db.relational.sql.tree.NodeLocation;
+
+import org.antlr.v4.runtime.ParserRuleContext;
+import org.antlr.v4.runtime.Token;
+import org.antlr.v4.runtime.tree.TerminalNode;
+
+import javax.annotation.Nullable;
+
+import static java.util.Objects.requireNonNull;
+
+public class AstBuilder extends RelationalSqlBaseVisitor<Node> {
+
+ private int parameterPosition;
+
+ @Nullable private final NodeLocation baseLocation;
+
+ AstBuilder(@Nullable NodeLocation baseLocation) {
+ this.baseLocation = baseLocation;
+ }
+
+ private NodeLocation getLocation(TerminalNode terminalNode) {
+ requireNonNull(terminalNode, "terminalNode is null");
+ return getLocation(terminalNode.getSymbol());
+ }
+
+ private NodeLocation getLocation(ParserRuleContext parserRuleContext) {
+ requireNonNull(parserRuleContext, "parserRuleContext is null");
+ return getLocation(parserRuleContext.getStart());
+ }
+
+ private NodeLocation getLocation(Token token) {
+ requireNonNull(token, "token is null");
+ return baseLocation != null
+ ? new NodeLocation(
+ token.getLine() + baseLocation.getLineNumber() - 1,
+ token.getCharPositionInLine()
+ + 1
+ + (token.getLine() == 1 ? baseLocation.getColumnNumber() : 0))
+ : new NodeLocation(token.getLine(), token.getCharPositionInLine() + 1);
+ }
+}
diff --git
a/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/tree/AstVisitor.java
b/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/tree/AstVisitor.java
new file mode 100644
index 00000000000..b8e0c07973a
--- /dev/null
+++
b/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/tree/AstVisitor.java
@@ -0,0 +1,37 @@
+/*
+ * 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.relational.sql.tree;
+
+import javax.annotation.Nullable;
+
+public abstract class AstVisitor<R, C> {
+
+ public R process(Node node) {
+ return process(node, null);
+ }
+
+ public R process(Node node, @Nullable C context) {
+ return node.accept(this, context);
+ }
+
+ protected R visitNode(Node node, C context) {
+ return null;
+ }
+}
diff --git
a/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/tree/Node.java
b/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/tree/Node.java
new file mode 100644
index 00000000000..0c3ec9b8e32
--- /dev/null
+++
b/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/tree/Node.java
@@ -0,0 +1,71 @@
+/*
+ * 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.relational.sql.tree;
+
+import javax.annotation.Nullable;
+
+import java.util.List;
+import java.util.Optional;
+
+public abstract class Node {
+
+ @Nullable private final NodeLocation location;
+
+ protected Node(@Nullable NodeLocation location) {
+ this.location = location;
+ }
+
+ /** Accessible for {@link AstVisitor}, use {@link AstVisitor#process(Node,
Object)} instead. */
+ protected <R, C> R accept(AstVisitor<R, C> visitor, C context) {
+ return visitor.visitNode(this, context);
+ }
+
+ public Optional<NodeLocation> getLocation() {
+ return Optional.ofNullable(location);
+ }
+
+ public abstract List<Node> getChildren();
+
+ // Force subclasses to have a proper equals and hashcode implementation
+ @Override
+ public abstract int hashCode();
+
+ @Override
+ public abstract boolean equals(Object obj);
+
+ @Override
+ public abstract String toString();
+
+ /**
+ * Compare with another node by considering internal state excluding any
Node returned by
+ * getChildren()
+ */
+ public boolean shallowEquals(Node other) {
+ throw new UnsupportedOperationException("not yet implemented: " +
getClass().getName());
+ }
+
+ static boolean sameClass(Node left, Node right) {
+ if (left == right) {
+ return true;
+ }
+
+ return left.getClass() == right.getClass();
+ }
+}
diff --git
a/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/tree/NodeLocation.java
b/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/tree/NodeLocation.java
new file mode 100644
index 00000000000..ceeb87ff5f1
--- /dev/null
+++
b/iotdb-core/relational-parser/src/main/java/org/apache/iotdb/db/relational/sql/tree/NodeLocation.java
@@ -0,0 +1,68 @@
+/*
+ * 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.relational.sql.tree;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+public final class NodeLocation {
+
+ private final int line;
+ private final int column;
+
+ public NodeLocation(int line, int column) {
+ checkArgument(line >= 1, "line must be at least one, got: %s", line);
+ checkArgument(column >= 1, "column must be at least one, got: %s", column);
+
+ this.line = line;
+ this.column = column;
+ }
+
+ public int getLineNumber() {
+ return line;
+ }
+
+ public int getColumnNumber() {
+ return column;
+ }
+
+ @Override
+ public String toString() {
+ return line + ":" + column;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ NodeLocation that = (NodeLocation) o;
+ return line == that.line && column == that.column;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(line, column);
+ }
+}