xiedeyantu commented on code in PR #4369:
URL: https://github.com/apache/calcite/pull/4369#discussion_r2085762148


##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateValueReduceRule.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.tools.RelBuilderFactory;
+import org.apache.calcite.util.Util;
+
+import com.google.common.collect.ImmutableList;
+
+import org.immutables.value.Value;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Rule that applies an {@link Aggregate} to a distinct {@link Values}.
+ *
+ * <p>This is useful because such as {@link SubQueryRemoveRule}
+ * doesn't distinct values for IN filter values.
+ *
+ * <p>Sample query where this matters:
+ *
+ * <blockquote><code>
+ * SELECT deptno, sal FROM EMP WHERE deptno IN 
(1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
+ * </code></blockquote>
+ *
+ * <p>should be converted to:
+ *
+ * <blockquote><code>
+ * SELECT deptno, sal FROM EMP WHERE deptno IN (1,3)
+ * </code></blockquote>
+ *
+ * @see CoreRules#AGGREGATE_VALUES_REDUCE
+ */
[email protected]
+public class AggregateValueReduceRule
+    extends RelRule<AggregateValueReduceRule.Config>
+    implements SubstitutionRule {
+
+  /** Creates an AggregateValuesRule. */
+  protected AggregateValueReduceRule(Config config) {
+    super(config);
+  }
+
+  public AggregateValueReduceRule(RelBuilderFactory relBuilderFactory) {
+    this(Config.DEFAULT
+        .withRelBuilderFactory(relBuilderFactory)
+        .as(Config.class));
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final Values values = call.rel(1);
+    Util.discard(values);

Review Comment:
   What is the purpose of this discard?



##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateValueReduceRule.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.tools.RelBuilderFactory;
+import org.apache.calcite.util.Util;
+
+import com.google.common.collect.ImmutableList;
+
+import org.immutables.value.Value;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Rule that applies an {@link Aggregate} to a distinct {@link Values}.
+ *
+ * <p>This is useful because such as {@link SubQueryRemoveRule}
+ * doesn't distinct values for IN filter values.
+ *
+ * <p>Sample query where this matters:
+ *
+ * <blockquote><code>
+ * SELECT deptno, sal FROM EMP WHERE deptno IN 
(1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
+ * </code></blockquote>
+ *
+ * <p>should be converted to:
+ *
+ * <blockquote><code>
+ * SELECT deptno, sal FROM EMP WHERE deptno IN (1,3)

Review Comment:
   Rule examples can be more universal. If it is difficult to describe with 
SQL, a simple plan can be used. For example:
   ```
   <p>Sample plan where this matters:
   
   <blockquote><code>
   LogicalAggregate(group=[{0}])
     LogicalValues(tuples=[[{ 1 }, { 1 }, { 3 }, { 1 }, { 1 }, { 1 }, { 1 }, { 
1 }, { 1 }, { 1 }, { 1 }, { 1 }, { 1 }, { 1 }, { 1 }, { 1 }, { 1 }, { 1 }, { 1 
}, { 1 }, { 1 }]])
   </code></blockquote>
   
   <p>should be converted to:
   
   <blockquote><code>
   LogicalValues(tuples=[[{ 1 }, { 3 }]])
   </code></blockquote>
   ```
   You can use this case to receive this result.
   ```
     @Test void testReduceInValues() {
       final Function<RelBuilder, RelNode> relFn = b -> b
           .values(new String[]{"v"}, 1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
           .distinct()
           .build();
       relFn(relFn)
           .withRule(CoreRules.AGGREGATE_VALUES_REDUCE)
           .check();
     }
   ```



##########
core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java:
##########
@@ -847,6 +847,10 @@ private CoreRules() {}
   public static final AggregateValuesRule AGGREGATE_VALUES =
       AggregateValuesRule.Config.DEFAULT.toRule();
 
+  /** Rule that applies an {@link Aggregate} to a distinct {@link Values}. */

Review Comment:
   /** Rule that applies {@link Aggregate} and {@link Values} to a distinct 
{@link Values}. */ Is that right?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to