Aaaaaaron commented on a change in pull request #2125:
URL: https://github.com/apache/calcite/pull/2125#discussion_r482726098
##########
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:
INTERSECT/EXCEPT ALL does not distinct values, but we still can not trim
fields for them.
Take the pic below, for example, q1 is the trimmed form of q2, and we can
the result in the pic, they are different:
q1:
```
select name from
(select name from emp0
intersect all
select name from emp1)
```
q2:
```
select name from
(select name, deptno from emp0
intersect all
select name, deptno from emp1)
```

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