You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2020/11/03 13:37:27 UTC

[GitHub] [calcite] qizhou92 commented on a change in pull request #2236: [CALCITE-3913] Test correctness using formal verification (Qi Zhou)

qizhou92 commented on a change in pull request #2236:
URL: https://github.com/apache/calcite/pull/2236#discussion_r516309298



##########
File path: core/src/test/java/org/apache/calcite/test/SmtLibTest.java
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.test;
+
+
+import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.logical.LogicalFilter;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.test.verifier.RexToSymbolicColumn;
+import org.apache.calcite.test.verifier.SymbolicColumn;
+
+import com.microsoft.z3.BoolExpr;
+import com.microsoft.z3.Context;
+import com.microsoft.z3.Expr;
+import com.microsoft.z3.Solver;
+import com.microsoft.z3.Status;
+
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Testing for integrating z3 with calcite to verify equivalence.
+ **/
+
+public class SmtLibTest {
+
+  static RelDataTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+  /**
+   * Check z3 dynamic library exits before run all other tests.
+   */
+  @BeforeAll static void testZ3Lib() throws Exception {
+    Context z3Context = new Context();
+    Expr x = z3Context.mkIntConst("x");
+    Expr one = z3Context.mkInt(1);
+    BoolExpr eq = z3Context.mkEq(x, one);
+    Solver solver = z3Context.mkSolver();
+    solver.add(eq);
+    assertEquals(solver.check(), Status.SATISFIABLE);
+  }
+
+  /** Converts a SQL string to a relational expression using mock schema. */
+  private static RelNode toRel(String sql) {
+    final SqlToRelTestBase test = new SqlToRelTestBase() {
+    };
+    return test.createTester().convertSqlToRel(sql).rel;
+  }
+
+  /** Converts a where condition string to a RexNode using mock schema on emp table. */
+  private static RexNode toRex(String cond) {
+    final String sql = "select *\n"
+        + "from emp where"
+        + cond;
+    final RelNode relNode = toRel(sql);
+    LogicalProject project = (LogicalProject) relNode;
+    LogicalFilter filter = (LogicalFilter) project.getInput();
+    return filter.getCondition();
+  }
+
+  private static boolean checkEqual(String cond1, String cond2) {
+    RexNode rexNode1 = toRex(cond1);
+    RexNode rexNode2 = toRex(cond2);
+    Context z3Context = new Context();
+    List<SymbolicColumn> inputSymbolicColumns = new ArrayList<>();
+    /** mock emp table, 8 columns with int type, since we only support numerical type for now **/
+    for (int i = 0; i < 8; i++) {
+      RelDataType intType = typeFactory.createJavaType(int.class);
+      SymbolicColumn inputColumn = SymbolicColumn.mkNewSymbolicColumn(z3Context, intType);
+      inputSymbolicColumns.add(inputColumn);

Review comment:
       Hi, thank you for your comments. I use **RexProgramBuilderBase**, which solves the problem. And **RexProgramTestBase** is not a public class. 

##########
File path: core/src/test/java/org/apache/calcite/test/SmtLibTest.java
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.test;
+
+
+import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.logical.LogicalFilter;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.test.verifier.RexToSymbolicColumn;
+import org.apache.calcite.test.verifier.SymbolicColumn;
+
+import com.microsoft.z3.BoolExpr;
+import com.microsoft.z3.Context;
+import com.microsoft.z3.Expr;
+import com.microsoft.z3.Solver;
+import com.microsoft.z3.Status;
+
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Testing for integrating z3 with calcite to verify equivalence.
+ **/
+
+public class SmtLibTest {
+
+  static RelDataTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+  /**
+   * Check z3 dynamic library exits before run all other tests.
+   */
+  @BeforeAll static void testZ3Lib() throws Exception {
+    Context z3Context = new Context();
+    Expr x = z3Context.mkIntConst("x");
+    Expr one = z3Context.mkInt(1);
+    BoolExpr eq = z3Context.mkEq(x, one);
+    Solver solver = z3Context.mkSolver();
+    solver.add(eq);
+    assertEquals(solver.check(), Status.SATISFIABLE);
+  }
+
+  /** Converts a SQL string to a relational expression using mock schema. */
+  private static RelNode toRel(String sql) {
+    final SqlToRelTestBase test = new SqlToRelTestBase() {
+    };
+    return test.createTester().convertSqlToRel(sql).rel;
+  }
+
+  /** Converts a where condition string to a RexNode using mock schema on emp table. */
+  private static RexNode toRex(String cond) {
+    final String sql = "select *\n"
+        + "from emp where"
+        + cond;
+    final RelNode relNode = toRel(sql);
+    LogicalProject project = (LogicalProject) relNode;
+    LogicalFilter filter = (LogicalFilter) project.getInput();
+    return filter.getCondition();
+  }
+
+  private static boolean checkEqual(String cond1, String cond2) {
+    RexNode rexNode1 = toRex(cond1);
+    RexNode rexNode2 = toRex(cond2);
+    Context z3Context = new Context();
+    List<SymbolicColumn> inputSymbolicColumns = new ArrayList<>();
+    /** mock emp table, 8 columns with int type, since we only support numerical type for now **/
+    for (int i = 0; i < 8; i++) {
+      RelDataType intType = typeFactory.createJavaType(int.class);
+      SymbolicColumn inputColumn = SymbolicColumn.mkNewSymbolicColumn(z3Context, intType);
+      inputSymbolicColumns.add(inputColumn);

Review comment:
       Sorry, I am a little bit confused about how to skip test cases if the z3 dynamic library is not loaded by using @BeforeAll. It seems even I throw errors in the BeforeAll methods, it still fail the test cases, which break the whole CI. 




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org