asolimando commented on code in PR #3367:
URL: https://github.com/apache/calcite/pull/3367#discussion_r1311847209


##########
core/src/main/java/org/apache/calcite/rel/rules/MinusToDistinctRule.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.rel.rules;
+
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Minus;
+import org.apache.calcite.rel.logical.LogicalMinus;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.tools.RelBuilderFactory;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.Util;
+
+import com.google.common.collect.ImmutableList;
+
+import org.immutables.value.Value;
+
+import java.math.BigDecimal;
+
+/**
+ * Planner rule that translates a distinct
+ * {@link org.apache.calcite.rel.core.Minus}
+ * (<code>all</code> = <code>false</code>)
+ * into a group of operators composed of
+ * {@link org.apache.calcite.rel.core.Union},
+ * {@link org.apache.calcite.rel.core.Aggregate},
+ * {@link org.apache.calcite.rel.core.Filter},etc.
+ *
+ * <p>For example, the query plan
+
+ * <blockquote><pre>{@code
+ *  LogicalMinus(all=[false])
+ *    LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2])
+ *      LogicalFilter(condition=[=($7, 10)])
+ *        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ *    LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2])
+ *      LogicalFilter(condition=[=($7, 20)])
+ *        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * }</pre></blockquote>
+ *
+ * <p> will convert to
+ *
+ * <blockquote><pre>{@code
+ *  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2])
+ *    LogicalFilter(condition=[AND(>($3, 0), =($4, 0))])
+ *      LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT() FILTER $3], 
agg#1=[COUNT() FILTER $4])
+ *        LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], $f3=[=($3, 0)], 
$f4=[=($3, 1)])
+ *          LogicalUnion(all=[true])
+ *            LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], $f3=[0])
+ *              LogicalFilter(condition=[=($7, 10)])
+ *                LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ *            LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], $f3=[1])
+ *              LogicalFilter(condition=[=($7, 20)])
+ *                LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * }</pre></blockquote>
+ *
+ * @see CoreRules#MINUS_TO_DISTINCT
+ */
+@Value.Enclosing
+public class MinusToDistinctRule
+    extends RelRule<MinusToDistinctRule.Config>
+    implements TransformationRule {
+
+  protected MinusToDistinctRule(Config config) {
+    super(config);
+  }
+
+  @Deprecated // to be removed before 2.0
+  public MinusToDistinctRule(Class<? extends Minus> minusClass,
+      RelBuilderFactory relBuilderFactory) {
+    
this(MinusToDistinctRule.Config.DEFAULT.withRelBuilderFactory(relBuilderFactory)
+        .as(MinusToDistinctRule.Config.class)
+        .withOperandFor(minusClass));
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    Minus minus = call.rel(0);
+
+    if (minus.all) {
+      // Nothing we can do
+      return;
+    }
+
+    final RelOptCluster cluster = minus.getCluster();
+    final RelBuilder relBuilder = call.builder();
+    final RexBuilder rexBuilder = cluster.getRexBuilder();
+    final int branchCount = minus.getInputs().size();
+
+    // For each child branch in minus,add a column which indicates branch index
+    //
+    // e.g. select EMPNO from emp -> select EMPNO,0 from emp
+    // 0 indicates that it comes from minus the first child branch
+    for (int i = 0; i < branchCount; i++) {
+      relBuilder.push(minus.getInput(i));
+      relBuilder.projectPlus(relBuilder.literal(new BigDecimal(i)));

Review Comment:
   Nit: since this is a counter for the branch numbers, maybe `BigDecimal` is 
an overkill?
   
   EDIT: never mind, it's implemented the same way in `IntersectToDistinctRule` 
so let's leave it this way



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

To unsubscribe, e-mail: commits-unsubscr...@calcite.apache.org

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

Reply via email to