danny0405 commented on a change in pull request #2125:
URL: https://github.com/apache/calcite/pull/2125#discussion_r482688085



##########
File path: core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
##########
@@ -885,14 +886,22 @@ public TrimResult trimFields(
 
   /**
    * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
-   * {@link org.apache.calcite.rel.core.SetOp} (including UNION and UNION ALL).
+   * {@link org.apache.calcite.rel.core.SetOp} (Only UNION ALL is supported).
    */
   public TrimResult trimFields(
       SetOp setOp,
       ImmutableBitSet fieldsUsed,
       Set<RelDataTypeField> extraFields) {
     final RelDataType rowType = setOp.getRowType();
     final int fieldCount = rowType.getFieldCount();
+
+    // Trim fields only for UNION ALL.
+    // Operator UNION/INTERSECT/INTERSECT ALL/EXCEPT/EXCEPT ALL
+    // will check duplication. We can not trim fields on them.
+    if (!(setOp.kind == SqlKind.UNION && setOp.all)) {
+      return result(setOp, Mappings.createIdentity(fieldCount));

Review comment:
       My test shows that INTERSECT/EXCEPT ALL does not distinct values, so we 
can trim fields for them.
   Here is my test query:
   ```sql
   create table a (
     a1 int,
     a2 int
   );
   
   insert into a values (1, 2);
   insert into a values (3, 4);
   insert into a values (3, 4);
   insert into a values (1, 2);
   insert into a values (5, 6);
   insert into a values (5, 6);
   
   create table b (
     b1 int,
     b2 int
   );
   
   insert into b values (1, 2);
   insert into b values (3, 4);
   insert into b values (3, 4);
   ```
   
   ```sql
   select * from a except all select * from b;
   ```




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

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


Reply via email to