You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ro...@apache.org on 2022/05/02 08:53:11 UTC

[iotdb] 01/02: add IT for in/like/regexp

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

rong pushed a commit to branch in-like-regexp
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit a03d4c6cfb14fc363eed6bc82332fe6247d4ac58
Author: Steve Yurong Su <ro...@apache.org>
AuthorDate: Mon May 2 16:30:01 2022 +0800

    add IT for in/like/regexp
---
 .../org/apache/iotdb/db/qp/sql/IoTDBSqlParser.g4   |  1 -
 .../iotdb/db/integration/IoTDBNestedQueryIT.java   |  6 +--
 .../apache/iotdb/db/mpp/sql/parser/ASTVisitor.java | 49 +++++++++++++---------
 .../apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java    | 49 +++++++++++++---------
 .../db/query/expression/unary/InExpression.java    |  5 ++-
 5 files changed, 65 insertions(+), 45 deletions(-)

diff --git a/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IoTDBSqlParser.g4 b/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IoTDBSqlParser.g4
index 6a3abb9c73..735a2f717d 100644
--- a/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IoTDBSqlParser.g4
+++ b/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IoTDBSqlParser.g4
@@ -919,7 +919,6 @@ resultColumn
     ;
 
 
-
 // From Clause
 
 fromClause
diff --git a/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBNestedQueryIT.java b/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBNestedQueryIT.java
index dbf69fbe81..c74ce71b6d 100644
--- a/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBNestedQueryIT.java
+++ b/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBNestedQueryIT.java
@@ -34,7 +34,6 @@ import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
 import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.BeforeClass;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
@@ -587,20 +586,19 @@ public class IoTDBNestedQueryIT {
   }
 
   @Test
-  @Ignore
   public void testRegularLikeInExpressions() {
     try (Connection connection =
             DriverManager.getConnection(
                 Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/", "root", "root");
         Statement statement = connection.createStatement()) {
       String query =
-          "SELECT ((CAST(s1, 'type'='TEXT') LIKE '_') REGEXP '*') IN ('4', '2', '3') "
+          "SELECT ((CAST(s1, 'type'='TEXT') LIKE '_') REGEXP '[0-9]') IN ('4', '2', '3') "
               + "FROM root.vehicle.d1";
       try (ResultSet rs = statement.executeQuery(query)) {
         for (int i = 2; i <= 4; i++) {
           Assert.assertTrue(rs.next());
           Assert.assertEquals(i, rs.getLong(1));
-          Assert.assertEquals(i, rs.getLong(2));
+          Assert.assertEquals(String.valueOf(i), rs.getString(2));
         }
         Assert.assertFalse(rs.next());
       }
diff --git a/server/src/main/java/org/apache/iotdb/db/mpp/sql/parser/ASTVisitor.java b/server/src/main/java/org/apache/iotdb/db/mpp/sql/parser/ASTVisitor.java
index d242aef347..624871333a 100644
--- a/server/src/main/java/org/apache/iotdb/db/mpp/sql/parser/ASTVisitor.java
+++ b/server/src/main/java/org/apache/iotdb/db/mpp/sql/parser/ASTVisitor.java
@@ -99,6 +99,8 @@ import org.apache.iotdb.db.query.expression.binary.SubtractionExpression;
 import org.apache.iotdb.db.query.expression.leaf.ConstantOperand;
 import org.apache.iotdb.db.query.expression.leaf.TimeSeriesOperand;
 import org.apache.iotdb.db.query.expression.multi.FunctionExpression;
+import org.apache.iotdb.db.query.expression.unary.InExpression;
+import org.apache.iotdb.db.query.expression.unary.LikeExpression;
 import org.apache.iotdb.db.query.expression.unary.LogicNotExpression;
 import org.apache.iotdb.db.query.expression.unary.NegationExpression;
 import org.apache.iotdb.db.query.expression.unary.RegularExpression;
@@ -115,6 +117,7 @@ import java.util.ArrayList;
 import java.util.EnumMap;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -1711,23 +1714,6 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
       return parseExpression(context.unaryInBracket, inWithoutNull);
     }
 
-    if (context.constant() != null && !context.constant().isEmpty()) {
-      return parseConstantOperand(context.constant(0));
-    }
-
-    if (context.time != null) {
-      throw new UnsupportedOperationException();
-    }
-
-    if (context.fullPathInExpression() != null) {
-      return new TimeSeriesOperand(
-          parseFullPathInExpression(context.fullPathInExpression(), inWithoutNull));
-    }
-
-    if (context.functionName() != null) {
-      return parseFunctionExpression(context, inWithoutNull);
-    }
-
     if (context.expressionAfterUnaryOperator != null) {
       if (context.MINUS() != null) {
         return new NegationExpression(
@@ -1799,6 +1785,23 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
       return parseInExpression(context, inWithoutNull);
     }
 
+    if (context.functionName() != null) {
+      return parseFunctionExpression(context, inWithoutNull);
+    }
+
+    if (context.fullPathInExpression() != null) {
+      return new TimeSeriesOperand(
+          parseFullPathInExpression(context.fullPathInExpression(), inWithoutNull));
+    }
+
+    if (context.time != null) {
+      throw new UnsupportedOperationException();
+    }
+
+    if (context.constant() != null && !context.constant().isEmpty()) {
+      return parseConstantOperand(context.constant(0));
+    }
+
     throw new UnsupportedOperationException();
   }
 
@@ -1843,11 +1846,19 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
   }
 
   private Expression parseLikeExpression(ExpressionContext context, boolean inWithoutNull) {
-    throw new UnsupportedOperationException();
+    return new LikeExpression(
+        parseExpression(context.unaryBeforeRegularOrLikeExpression, inWithoutNull),
+        parseStringLiteral(context.STRING_LITERAL().getText()));
   }
 
   private Expression parseInExpression(ExpressionContext context, boolean inWithoutNull) {
-    throw new UnsupportedOperationException();
+    Expression childExpression = parseExpression(context.unaryBeforeInExpression, inWithoutNull);
+    LinkedHashSet<String> values = new LinkedHashSet<>();
+    for (ConstantContext constantContext : context.constant()) {
+      String text = constantContext.getText();
+      values.add(constantContext.STRING_LITERAL() != null ? parseStringLiteral(text) : text);
+    }
+    return new InExpression(childExpression, values);
   }
 
   private Expression parseConstantOperand(ConstantContext constantContext) {
diff --git a/server/src/main/java/org/apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java b/server/src/main/java/org/apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java
index cb45bf2385..4b1b73dec2 100644
--- a/server/src/main/java/org/apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java
+++ b/server/src/main/java/org/apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java
@@ -145,6 +145,8 @@ import org.apache.iotdb.db.query.expression.binary.SubtractionExpression;
 import org.apache.iotdb.db.query.expression.leaf.ConstantOperand;
 import org.apache.iotdb.db.query.expression.leaf.TimeSeriesOperand;
 import org.apache.iotdb.db.query.expression.multi.FunctionExpression;
+import org.apache.iotdb.db.query.expression.unary.InExpression;
+import org.apache.iotdb.db.query.expression.unary.LikeExpression;
 import org.apache.iotdb.db.query.expression.unary.LogicNotExpression;
 import org.apache.iotdb.db.query.expression.unary.NegationExpression;
 import org.apache.iotdb.db.query.expression.unary.RegularExpression;
@@ -166,6 +168,7 @@ import java.util.Arrays;
 import java.util.EnumMap;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -2547,23 +2550,6 @@ public class IoTDBSqlVisitor extends IoTDBSqlParserBaseVisitor<Operator> {
       return parseExpression(context.unaryInBracket, inWithoutNull);
     }
 
-    if (context.constant() != null && !context.constant().isEmpty()) {
-      return parseConstantOperand(context.constant(0));
-    }
-
-    if (context.time != null) {
-      throw new UnsupportedOperationException();
-    }
-
-    if (context.fullPathInExpression() != null) {
-      return new TimeSeriesOperand(
-          parseFullPathInExpression(context.fullPathInExpression(), inWithoutNull));
-    }
-
-    if (context.functionName() != null) {
-      return parseFunctionExpression(context, inWithoutNull);
-    }
-
     if (context.expressionAfterUnaryOperator != null) {
       if (context.MINUS() != null) {
         return new NegationExpression(
@@ -2635,6 +2621,23 @@ public class IoTDBSqlVisitor extends IoTDBSqlParserBaseVisitor<Operator> {
       return parseInExpression(context, inWithoutNull);
     }
 
+    if (context.functionName() != null) {
+      return parseFunctionExpression(context, inWithoutNull);
+    }
+
+    if (context.fullPathInExpression() != null) {
+      return new TimeSeriesOperand(
+          parseFullPathInExpression(context.fullPathInExpression(), inWithoutNull));
+    }
+
+    if (context.time != null) {
+      throw new UnsupportedOperationException();
+    }
+
+    if (context.constant() != null && !context.constant().isEmpty()) {
+      return parseConstantOperand(context.constant(0));
+    }
+
     throw new UnsupportedOperationException();
   }
 
@@ -2679,11 +2682,19 @@ public class IoTDBSqlVisitor extends IoTDBSqlParserBaseVisitor<Operator> {
   }
 
   private Expression parseLikeExpression(ExpressionContext context, boolean inWithoutNull) {
-    throw new UnsupportedOperationException();
+    return new LikeExpression(
+        parseExpression(context.unaryBeforeRegularOrLikeExpression, inWithoutNull),
+        parseStringLiteral(context.STRING_LITERAL().getText()));
   }
 
   private Expression parseInExpression(ExpressionContext context, boolean inWithoutNull) {
-    throw new UnsupportedOperationException();
+    Expression childExpression = parseExpression(context.unaryBeforeInExpression, inWithoutNull);
+    LinkedHashSet<String> values = new LinkedHashSet<>();
+    for (ConstantContext constantContext : context.constant()) {
+      String text = constantContext.getText();
+      values.add(constantContext.STRING_LITERAL() != null ? parseStringLiteral(text) : text);
+    }
+    return new InExpression(childExpression, values);
   }
 
   private Expression parseConstantOperand(ConstantContext constantContext) {
diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/unary/InExpression.java b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/InExpression.java
index a5a5b97111..e78a975677 100644
--- a/server/src/main/java/org/apache/iotdb/db/query/expression/unary/InExpression.java
+++ b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/InExpression.java
@@ -23,6 +23,7 @@ import org.apache.iotdb.db.query.expression.Expression;
 import org.apache.iotdb.db.query.expression.ExpressionType;
 import org.apache.iotdb.db.query.udf.core.reader.LayerPointReader;
 import org.apache.iotdb.db.query.udf.core.transformer.Transformer;
+import org.apache.iotdb.db.query.udf.core.transformer.unary.InTransformer;
 import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils;
 
 import java.nio.ByteBuffer;
@@ -33,7 +34,7 @@ public class InExpression extends UnaryExpression {
 
   private final LinkedHashSet<String> values;
 
-  protected InExpression(Expression expression, LinkedHashSet<String> values) {
+  public InExpression(Expression expression, LinkedHashSet<String> values) {
     super(expression);
     this.values = values;
   }
@@ -67,7 +68,7 @@ public class InExpression extends UnaryExpression {
 
   @Override
   protected Transformer constructTransformer(LayerPointReader pointReader) {
-    throw new UnsupportedOperationException();
+    return new InTransformer(pointReader, values);
   }
 
   @Override