You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gearpump.apache.org by ma...@apache.org on 2017/07/09 00:18:31 UTC

[18/20] incubator-gearpump git commit: Add SQLNode for test purpose

Add SQLNode for test purpose


Project: http://git-wip-us.apache.org/repos/asf/incubator-gearpump/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-gearpump/commit/9a1a7c24
Tree: http://git-wip-us.apache.org/repos/asf/incubator-gearpump/tree/9a1a7c24
Diff: http://git-wip-us.apache.org/repos/asf/incubator-gearpump/diff/9a1a7c24

Branch: refs/heads/sql
Commit: 9a1a7c2483c8f2d55f0d1d777d1ac2fb0ebc4c74
Parents: e379789
Author: Buddhi Ayesha <bu...@cse.mrt.ac.lk>
Authored: Thu Jun 15 00:57:53 2017 +0530
Committer: manuzhang <ow...@gmail.com>
Committed: Sun Jul 9 07:52:48 2017 +0800

----------------------------------------------------------------------
 experiments/sql/pom.xml                         |   6 +
 .../main/java/org/apache/calcite/SQLNode.java   | 112 +++++++++++++++++++
 2 files changed, 118 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-gearpump/blob/9a1a7c24/experiments/sql/pom.xml
----------------------------------------------------------------------
diff --git a/experiments/sql/pom.xml b/experiments/sql/pom.xml
index 7ace8b8..793ae57 100644
--- a/experiments/sql/pom.xml
+++ b/experiments/sql/pom.xml
@@ -71,6 +71,12 @@ under the License.
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>${junit.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
         </dependency>
     </dependencies>
 

http://git-wip-us.apache.org/repos/asf/incubator-gearpump/blob/9a1a7c24/experiments/sql/src/main/java/org/apache/calcite/SQLNode.java
----------------------------------------------------------------------
diff --git a/experiments/sql/src/main/java/org/apache/calcite/SQLNode.java b/experiments/sql/src/main/java/org/apache/calcite/SQLNode.java
new file mode 100644
index 0000000..c092bf0
--- /dev/null
+++ b/experiments/sql/src/main/java/org/apache/calcite/SQLNode.java
@@ -0,0 +1,112 @@
+package org.apache.calcite;
+
+import org.apache.calcite.avatica.util.Casing;
+import org.apache.calcite.avatica.util.Quoting;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.fun.SqlCase;
+import org.apache.calcite.sql.parser.SqlParseException;
+import org.apache.calcite.sql.parser.SqlParser;
+import org.apache.calcite.sql.parser.impl.SqlParserImpl;
+import org.apache.calcite.sql.validate.SqlConformanceEnum;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+import org.apache.calcite.util.Util;
+import org.junit.ComparisonFailure;
+
+import java.util.regex.Pattern;
+
+/**
+ * Created by Buddhi on 6/8/2017.
+ */
+public class SQLNode {
+
+    private static final Pattern LINE_BREAK_PATTERN = Pattern.compile("\r\n|\r|\n");
+
+    private static final Pattern TAB_PATTERN = Pattern.compile("\t");
+
+    private static final String LINE_BREAK = "\\\\n\"" + Util.LINE_SEPARATOR + " + \"";
+
+    private static final ThreadLocal<boolean[]> LINUXIFY = new ThreadLocal<boolean[]>() {
+        @Override
+        protected boolean[] initialValue() {
+            return new boolean[]{true};
+        }
+    };
+
+
+    protected SqlParser getSqlParser(String sql) {
+        return SqlParser.create(sql,
+                SqlParser.configBuilder()
+                        .setParserFactory(SqlParserImpl.FACTORY)
+                        .setQuoting(Quoting.DOUBLE_QUOTE)
+                        .setUnquotedCasing(Casing.TO_UPPER)
+                        .setQuotedCasing(Casing.UNCHANGED)
+                        .setConformance(SqlConformanceEnum.DEFAULT)
+                        .build());
+    }
+
+    public static String toJavaString(String s) {
+        s = Util.replace(s, "\"", "\\\"");
+        s = LINE_BREAK_PATTERN.matcher(s).replaceAll(LINE_BREAK);
+        s = TAB_PATTERN.matcher(s).replaceAll("\\\\t");
+        s = "\"" + s + "\"";
+        String spurious = "\n \\+ \"\"";
+        if (s.endsWith(spurious)) {
+            s = s.substring(0, s.length() - spurious.length());
+        }
+        return s;
+    }
+
+    public static void assertEqualsVerbose(String expected, String actual) {
+        if (actual == null) {
+            if (expected == null) {
+                return;
+            } else {
+                String message = "Expected:\n" + expected + "\nActual: null";
+                throw new ComparisonFailure(message, expected, null);
+            }
+        }
+        if ((expected != null) && expected.equals(actual)) {
+            return;
+        }
+        String s = toJavaString(actual);
+        String message = "Expected:\n" + expected + "\nActual:\n" +
+                actual + "\nActual java:\n" + s + '\n';
+
+        throw new ComparisonFailure(message, expected, actual);
+    }
+
+    public void check(String sql, String expected) {
+        final SqlNode sqlNode;
+        try {
+            sqlNode = getSqlParser(sql).parseStmt();
+        } catch (SqlParseException e) {
+            throw new RuntimeException("Error while parsing SQL: " + sql, e);
+        }
+
+        String actual = sqlNode.toSqlString(null, true).getSql();
+        if (LINUXIFY.get()[0]) {
+            actual = Util.toLinux(actual);
+        }
+        assertEqualsVerbose(expected, actual);
+    }
+
+    public void validateCall(SqlCall call, SqlValidator validator, SqlValidatorScope operandScope) {
+        SqlCase sqlCase = (SqlCase) call;
+        SqlNodeList whenOperands = sqlCase.getWhenOperands();
+        SqlNodeList thenOperands = sqlCase.getThenOperands();
+        SqlNode elseOperand = sqlCase.getElseOperand();
+        for (SqlNode operand : whenOperands) {
+            operand.validateExpr(validator, operandScope);
+        }
+        for (SqlNode operand : thenOperands) {
+            operand.validateExpr(validator, operandScope);
+        }
+        if (elseOperand != null) {
+            elseOperand.validateExpr(validator, operandScope);
+        }
+    }
+
+}