You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jc...@apache.org on 2018/04/06 06:48:38 UTC
[1/2] calcite git commit: [CALCITE-2236] Druid adapter: Avoid
duplication of fields names during Druid query planing
Repository: calcite
Updated Branches:
refs/heads/master 8139acb78 -> 9e07f1332
[CALCITE-2236] Druid adapter: Avoid duplication of fields names during Druid query planing
Close apache/calcite#655
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/f94f4131
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/f94f4131
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/f94f4131
Branch: refs/heads/master
Commit: f94f4131f69e849fb5f15b090ad400c652b77689
Parents: 8139acb
Author: Slim <sl...@gmail.com>
Authored: Mon Apr 2 15:07:06 2018 -0700
Committer: Jesus Camacho Rodriguez <jc...@apache.org>
Committed: Thu Apr 5 23:33:54 2018 -0700
----------------------------------------------------------------------
.../java/org/apache/calcite/adapter/druid/DruidQuery.java | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/calcite/blob/f94f4131/druid/src/main/java/org/apache/calcite/adapter/druid/DruidQuery.java
----------------------------------------------------------------------
diff --git a/druid/src/main/java/org/apache/calcite/adapter/druid/DruidQuery.java b/druid/src/main/java/org/apache/calcite/adapter/druid/DruidQuery.java
index a7a3668..e26234b 100644
--- a/druid/src/main/java/org/apache/calcite/adapter/druid/DruidQuery.java
+++ b/druid/src/main/java/org/apache/calcite/adapter/druid/DruidQuery.java
@@ -1025,6 +1025,7 @@ public class DruidQuery extends AbstractRelNode implements BindableRel {
final RelDataType postAggInputRowType = getCluster().getTypeFactory()
.createStructType(Pair.right(postProject.getInput().getRowType().getFieldList()),
aggregateStageFieldNames);
+ final Set<String> existingAggFieldsNames = new HashSet<>(aggregateStageFieldNames);
// this is an index of existing columns coming out aggregate layer. Will use this index to:
// filter out any project down the road that doesn't change values e.g inputRef/identity cast
Map<String, String> existingProjects = Maps
@@ -1035,7 +1036,6 @@ public class DruidQuery extends AbstractRelNode implements BindableRel {
});
for (Pair<RexNode, String> pair : postProject.getNamedProjects()) {
final RexNode postProjectRexNode = pair.left;
- final String postProjectFieldName = pair.right;
String expression = DruidExpressions
.toDruidExpression(postProjectRexNode, postAggInputRowType, this);
final String existingFieldName = existingProjects.get(expression);
@@ -1043,8 +1043,11 @@ public class DruidQuery extends AbstractRelNode implements BindableRel {
// simple input ref or Druid runtime identity cast will skip it, since it is here already
postProjectDimListBuilder.add(existingFieldName);
} else {
- postAggs.add(new JsonExpressionPostAgg(postProjectFieldName, expression, null));
- postProjectDimListBuilder.add(postProjectFieldName);
+ final String uniquelyProjectFieldName = SqlValidatorUtil.uniquify(pair.right,
+ existingAggFieldsNames, SqlValidatorUtil.EXPR_SUGGESTER);
+ postAggs.add(new JsonExpressionPostAgg(uniquelyProjectFieldName, expression, null));
+ postProjectDimListBuilder.add(uniquelyProjectFieldName);
+ existingAggFieldsNames.add(uniquelyProjectFieldName);
}
}
postAggregateStageFieldNames = postProjectDimListBuilder;
[2/2] calcite git commit: [CALCITE-2240] Extend rule to push
predicates into CASE statement (Zoltan Haindrich)
Posted by jc...@apache.org.
[CALCITE-2240] Extend rule to push predicates into CASE statement (Zoltan Haindrich)
Close apache/calcite#658
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/9e07f133
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/9e07f133
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/9e07f133
Branch: refs/heads/master
Commit: 9e07f1332bfec6ac00e5b23e5d14a3295a5e12b0
Parents: f94f413
Author: Zoltan Haindrich <zh...@hortonworks.com>
Authored: Thu Apr 5 16:53:47 2018 +0200
Committer: Jesus Camacho Rodriguez <jc...@apache.org>
Committed: Thu Apr 5 23:36:53 2018 -0700
----------------------------------------------------------------------
.../calcite/rel/rules/ReduceExpressionsRule.java | 12 ++++++------
.../org/apache/calcite/test/RelOptRulesTest.java | 12 ++++++++++++
.../org/apache/calcite/test/RelOptRulesTest.xml | 19 +++++++++++++++++++
3 files changed, 37 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/calcite/blob/9e07f133/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java b/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java
index 264d123..296cf35 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java
@@ -558,8 +558,9 @@ public abstract class ReduceExpressionsRule extends RelOptRule {
protected static boolean reduceExpressionsInternal(RelNode rel,
RexSimplify simplify, List<RexNode> expList,
RelOptPredicateList predicates) {
+ boolean changed = false;
// Replace predicates on CASE to CASE on predicates.
- new CaseShuttle().mutate(expList);
+ changed |= new CaseShuttle().mutate(expList);
// Find reducible expressions.
final List<RexNode> constExps = Lists.newArrayList();
@@ -568,7 +569,7 @@ public abstract class ReduceExpressionsRule extends RelOptRule {
findReducibleExps(rel.getCluster().getTypeFactory(), expList,
predicates.constantMap, constExps, addCasts, removableCasts);
if (constExps.isEmpty() && removableCasts.isEmpty()) {
- return false;
+ return changed;
}
// Remove redundant casts before reducing constant expressions.
@@ -595,8 +596,7 @@ public abstract class ReduceExpressionsRule extends RelOptRule {
if (!predicates.constantMap.isEmpty()) {
//noinspection unchecked
final List<Map.Entry<RexNode, RexNode>> pairs =
- (List<Map.Entry<RexNode, RexNode>>) (List)
- Lists.newArrayList(predicates.constantMap.entrySet());
+ Lists.newArrayList(predicates.constantMap.entrySet());
RexReplacer replacer =
new RexReplacer(simplify, Pair.left(pairs), Pair.right(pairs),
Collections.nCopies(pairs.size(), false));
@@ -613,7 +613,7 @@ public abstract class ReduceExpressionsRule extends RelOptRule {
// final RexExecutorImpl executor =
// new RexExecutorImpl(Schemas.createDataContext(null));
// rootRel.getCluster().getPlanner().setExecutor(executor);
- return false;
+ return changed;
}
final List<RexNode> reducedValues = Lists.newArrayList();
@@ -622,7 +622,7 @@ public abstract class ReduceExpressionsRule extends RelOptRule {
// Use RexNode.digest to judge whether each newly generated RexNode
// is equivalent to the original one.
if (RexUtil.strings(constExps).equals(RexUtil.strings(reducedValues))) {
- return false;
+ return changed;
}
// For Project, we have to be sure to preserve the result
http://git-wip-us.apache.org/repos/asf/calcite/blob/9e07f133/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
index 682758e..9fa401f 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -1995,6 +1995,18 @@ public class RelOptRulesTest extends RelOptTestBase {
checkPlanning(program, sql);
}
+ @Test public void testCasePushIsAlwaysWorking() throws Exception {
+ HepProgram program = new HepProgramBuilder()
+ .addRuleInstance(ReduceExpressionsRule.FILTER_INSTANCE)
+ .addRuleInstance(ReduceExpressionsRule.CALC_INSTANCE)
+ .addRuleInstance(ReduceExpressionsRule.PROJECT_INSTANCE)
+ .build();
+
+ final String sql = "select empno from emp"
+ + " where case when sal > 1000 then empno else sal end = 1";
+ checkPlanning(program, sql);
+ }
+
@Ignore // Calcite does not support INSERT yet
@Test public void testReduceValuesNull() throws Exception {
// The NULL literal presents pitfalls for value-reduction. Only
http://git-wip-us.apache.org/repos/asf/calcite/blob/9e07f133/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
----------------------------------------------------------------------
diff --git a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
index 6dd5f5c..8fae1ff 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -456,6 +456,25 @@ LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$
]]>
</Resource>
</TestCase>
+ <TestCase name="testCasePushIsAlwaysWorking">
+ <Resource name="sql">
+ <![CDATA[select empno from emp where case when sal > 1000 then empno else sal end = 1]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(EMPNO=[$0])
+ LogicalFilter(condition=[=(CASE(>($5, 1000), $0, $5), 1)])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(EMPNO=[$0])
+ LogicalFilter(condition=[CASE(>($5, 1000), =($0, 1), =($5, 1))])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ </TestCase>
<TestCase name="testCorrelationScalarAggAndFilter">
<Resource name="sql">
<![CDATA[SELECT e1.empno