You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by vl...@apache.org on 2018/09/22 14:29:51 UTC
calcite git commit: Implement fuzzy generator for CASE expressions
Repository: calcite
Updated Branches:
refs/heads/master b40562bf6 -> b98a8e7af
Implement fuzzy generator for CASE expressions
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/b98a8e7a
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/b98a8e7a
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/b98a8e7a
Branch: refs/heads/master
Commit: b98a8e7af6504dc813a4c31c601a7003c9716775
Parents: b40562b
Author: Vladimir Sitnikov <si...@gmail.com>
Authored: Sat Sep 22 00:32:10 2018 +0300
Committer: Vladimir Sitnikov <si...@gmail.com>
Committed: Sat Sep 22 17:06:06 2018 +0300
----------------------------------------------------------------------
.../calcite/test/RexProgramBuilderBase.java | 6 ++-
.../apache/calcite/test/fuzzer/RexFuzzer.java | 49 +++++++++++++++++---
.../test/fuzzer/RexProgramFuzzyTest.java | 6 +--
.../test/fuzzer/RexToTestCodeShuttle.java | 1 +
4 files changed, 52 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/calcite/blob/b98a8e7a/core/src/test/java/org/apache/calcite/test/RexProgramBuilderBase.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/RexProgramBuilderBase.java b/core/src/test/java/org/apache/calcite/test/RexProgramBuilderBase.java
index ab2b739..750c899 100644
--- a/core/src/test/java/org/apache/calcite/test/RexProgramBuilderBase.java
+++ b/core/src/test/java/org/apache/calcite/test/RexProgramBuilderBase.java
@@ -228,7 +228,11 @@ public abstract class RexProgramBuilderBase {
}
protected RexNode case_(RexNode... nodes) {
- return rexBuilder.makeCall(SqlStdOperatorTable.CASE, nodes);
+ return case_(ImmutableList.copyOf(nodes));
+ }
+
+ protected RexNode case_(Iterable<? extends RexNode> nodes) {
+ return rexBuilder.makeCall(SqlStdOperatorTable.CASE, ImmutableList.copyOf(nodes));
}
/**
http://git-wip-us.apache.org/repos/asf/calcite/blob/b98a8e7a/core/src/test/java/org/apache/calcite/test/fuzzer/RexFuzzer.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/fuzzer/RexFuzzer.java b/core/src/test/java/org/apache/calcite/test/fuzzer/RexFuzzer.java
index 7851db2..d180f02 100644
--- a/core/src/test/java/org/apache/calcite/test/fuzzer/RexFuzzer.java
+++ b/core/src/test/java/org/apache/calcite/test/fuzzer/RexFuzzer.java
@@ -107,10 +107,6 @@ public class RexFuzzer extends RexProgramBuilderBase {
nullableIntType = typeFactory.createTypeWithNullability(intType, true);
}
- private int getInputRefId(int base, boolean nullable) {
- return base + (nullable ? 100 : 0);
- }
-
public RexNode getExpression(Random r, int depth) {
return getComparableExpression(r, depth);
}
@@ -155,7 +151,7 @@ public class RexFuzzer extends RexProgramBuilderBase {
}
public RexNode getBoolExpression(Random r, int depth) {
- int v = depth <= 0 ? 0 : r.nextInt(6);
+ int v = depth <= 0 ? 0 : r.nextInt(7);
switch (v) {
case 0:
return getSimpleBool(r);
@@ -172,6 +168,9 @@ public class RexFuzzer extends RexProgramBuilderBase {
case 5:
return fuzzOperator(r, BOOL_TO_BOOL_MULTI_ARG, r.nextInt(3) + 2,
x -> getBoolExpression(x, depth - 1));
+ case 6:
+ return fuzzCase(r, depth - 1,
+ x -> getBoolExpression(x, depth - 1));
}
throw new AssertionError("should not reach here");
}
@@ -195,7 +194,7 @@ public class RexFuzzer extends RexProgramBuilderBase {
}
public RexNode getIntExpression(Random r, int depth) {
- int v = depth <= 0 ? 0 : r.nextInt(4);
+ int v = depth <= 0 ? 0 : r.nextInt(5);
switch (v) {
case 0:
return getSimpleInt(r);
@@ -207,10 +206,48 @@ public class RexFuzzer extends RexProgramBuilderBase {
case 3:
return fuzzOperator(r, ANY_SAME_TYPE_MULTI_ARG, r.nextInt(3) + 2,
x -> getIntExpression(x, depth - 1));
+ case 4:
+ return fuzzCase(r, depth - 1,
+ x -> getIntExpression(x, depth - 1));
}
throw new AssertionError("should not reach here");
}
+ public RexNode fuzzCase(Random r, int depth, Function<Random, RexNode> resultFactory) {
+ boolean caseArgWhen = r.nextBoolean();
+ int caseBranches = 1 + (depth <= 0 ? 0 : r.nextInt(3));
+ List<RexNode> args = new ArrayList<>(caseBranches + 1);
+
+ Function<Random, RexNode> exprFactory;
+ if (!caseArgWhen) {
+ exprFactory = x -> getBoolExpression(x, depth - 1);
+ } else {
+ int type = r.nextInt(2);
+ RexNode arg;
+ Function<Random, RexNode> baseExprFactory;
+ switch (type) {
+ case 0:
+ baseExprFactory = x -> getBoolExpression(x, depth - 1);
+ break;
+ case 1:
+ baseExprFactory = x -> getIntExpression(x, depth - 1);
+ break;
+ default:
+ throw new AssertionError("should not reach here: " + type);
+ }
+ arg = baseExprFactory.apply(r);
+ // emulate case when arg=2 then .. when arg=4 then ...
+ exprFactory = x -> eq(arg, baseExprFactory.apply(x));
+ }
+
+ for (int i = 0; i < caseBranches; i++) {
+ args.add(exprFactory.apply(r)); // when
+ args.add(resultFactory.apply(r)); // then
+ }
+ args.add(resultFactory.apply(r)); // else
+ return case_(args);
+ }
+
}
// End RexFuzzer.java
http://git-wip-us.apache.org/repos/asf/calcite/blob/b98a8e7a/core/src/test/java/org/apache/calcite/test/fuzzer/RexProgramFuzzyTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/fuzzer/RexProgramFuzzyTest.java b/core/src/test/java/org/apache/calcite/test/fuzzer/RexProgramFuzzyTest.java
index d29a6bd..f89b586 100644
--- a/core/src/test/java/org/apache/calcite/test/fuzzer/RexProgramFuzzyTest.java
+++ b/core/src/test/java/org/apache/calcite/test/fuzzer/RexProgramFuzzyTest.java
@@ -63,7 +63,7 @@ public class RexProgramFuzzyTest extends RexProgramBuilderBase {
private static final Duration TEST_DURATION =
Duration.of(Integer.getInteger("rex.fuzzing.duration", 5), ChronoUnit.SECONDS);
- private static final long TEST_ITERATIONS = Long.getLong("rex.fuzzing.iterations", 20);
+ private static final long TEST_ITERATIONS = Long.getLong("rex.fuzzing.iterations", 18);
// Stop fuzzing after detecting MAX_FAILURES errors
private static final int MAX_FAILURES =
Integer.getInteger("rex.fuzzing.max.failures", 1);
@@ -73,7 +73,7 @@ public class RexProgramFuzzyTest extends RexProgramBuilderBase {
// 0 means use random seed
// 42 is used to make sure tests pass in CI
private static final long SEED =
- Long.getLong("rex.fuzzing.seed", 43);
+ Long.getLong("rex.fuzzing.seed", 44);
private PriorityQueue<SimplifyTask> slowestTasks;
@@ -290,7 +290,7 @@ public class RexProgramFuzzyTest extends RexProgramBuilderBase {
if (testDuration.toMillis() == 0) {
return;
}
- slowestTasks = new TopN<>(TOPN_SLOWEST);
+ slowestTasks = new TopN<>(topnSlowest > 0 ? topnSlowest : 1);
Random r = new Random();
if (startSeed != 0) {
LOGGER.info("Using seed {} for rex fuzzing", startSeed);
http://git-wip-us.apache.org/repos/asf/calcite/blob/b98a8e7a/core/src/test/java/org/apache/calcite/test/fuzzer/RexToTestCodeShuttle.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/fuzzer/RexToTestCodeShuttle.java b/core/src/test/java/org/apache/calcite/test/fuzzer/RexToTestCodeShuttle.java
index ad60eae..bfcb84a 100644
--- a/core/src/test/java/org/apache/calcite/test/fuzzer/RexToTestCodeShuttle.java
+++ b/core/src/test/java/org/apache/calcite/test/fuzzer/RexToTestCodeShuttle.java
@@ -42,6 +42,7 @@ public class RexToTestCodeShuttle extends RexVisitorImpl<String> {
ImmutableMap.<SqlOperator, String>builder()
.put(SqlStdOperatorTable.AND, "and")
.put(SqlStdOperatorTable.OR, "or")
+ .put(SqlStdOperatorTable.CASE, "case_")
.put(SqlStdOperatorTable.COALESCE, "coalesce")
.put(SqlStdOperatorTable.IS_NULL, "isNull")
.put(SqlStdOperatorTable.IS_NOT_NULL, "isNotNull")