mihaibudiu commented on code in PR #3889:
URL: https://github.com/apache/calcite/pull/3889#discussion_r1712904628


##########
core/src/main/java/org/apache/calcite/rel/rules/UnionToValuesRule.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.plan.hep.HepRelVertex;
+import org.apache.calcite.plan.volcano.RelSubset;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Union;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rex.RexLiteral;
+
+import com.google.common.collect.ImmutableCollection;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+
+import org.immutables.value.Value;
+
+import java.util.List;
+
+/**
+ * Planner rule that convert
+ * {@link org.apache.calcite.rel.core.Union} with
+ * input only containing {@link org.apache.calcite.rel.core.Values}
+ * into an {@link org.apache.calcite.rel.core.Values}.
+ *
+ * <p>For example,
+ *
+ * <blockquote><pre>{@code
+ * Union(all=[true])
+ *     Values(tuples=[[{ 3, null }]])
+ *     Values(tuples=[[{ 7369, null }]])
+ *     Values(tuples=[[{ 1, 2 }]])
+ * }</pre></blockquote>
+ *
+ * <p>becomes
+ *
+ * <blockquote><pre>{@code
+ * Values(tuples=[[{ 3, null }, { 7369, null }, { 1, 2 }]])
+ * }</pre></blockquote>
+ *
+ * <p>If (<code>all</code> = <code>false</code>),
+ * {@link org.apache.calcite.rel.core.Values} value will be set list.
+ *
+ * <p>If {@link org.apache.calcite.rel.core.Union} only one input, will be 
handled by
+ * {@link org.apache.calcite.rel.rules.CoreRules#UNION_REMOVE}
+ *
+ */
[email protected]
+public class UnionToValuesRule extends RelRule<UnionToValuesRule.Config>
+    implements TransformationRule {
+
+  /**
+   * Creates a UnionToValuesRule.
+   */
+  protected UnionToValuesRule(UnionToValuesRule.Config config) {
+    super(config);
+  }
+
+  //~ Methods ----------------------------------------------------------------
+
+  @Override public boolean matches(RelOptRuleCall call) {
+    Union union = call.rel(0);
+    return union.getInputs().size() > 1;
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    Union union = call.rel(0);
+    List<RelNode> relNodeList = union.getInputs();
+    if (!relNodeList.stream().allMatch(UnionToValuesRule::isValues)) {
+      return;
+    }
+    ImmutableCollection.Builder<ImmutableList<RexLiteral>> tupleList =
+        getImmutableListBuilder(union.all, relNodeList);
+    Values values =
+        (Values) call.builder().values(tupleList.build(), 
union.getRowType()).build();
+    call.transformTo(values);
+  }
+
+  private static ImmutableCollection.Builder<ImmutableList<RexLiteral>> 
getImmutableListBuilder(
+      Boolean all, List<RelNode> relNodeList) {
+    ImmutableCollection.Builder<ImmutableList<RexLiteral>> tupleList;
+    if (all) {
+      tupleList = ImmutableList.builder();
+    } else {
+      tupleList = ImmutableSet.builder();

Review Comment:
   you could check to see if the type of all VALUES is the same, and then you 
can still apply the transformation as it is, otherwise you leave the UNION in 
the program. The transformation will probably be correct for UNION ALL.



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