Github user jacques-n commented on a diff in the pull request:
https://github.com/apache/drill/pull/207#discussion_r43471955
--- Diff:
exec/java-exec/src/main/java/org/apache/drill/exec/expr/ExpressionTreeMaterializer.java
---
@@ -323,23 +454,50 @@ public LogicalExpression
visitIfExpression(IfExpression ifExpr, FunctionLookupCo
MinorType thenType =
conditions.expression.getMajorType().getMinorType();
MinorType elseType = newElseExpr.getMajorType().getMinorType();
-
- // Check if we need a cast
- if (thenType != elseType && !(thenType == MinorType.NULL || elseType
== MinorType.NULL)) {
-
- MinorType leastRestrictive =
TypeCastRules.getLeastRestrictiveType((Arrays.asList(thenType, elseType)));
- if (leastRestrictive != thenType) {
- // Implicitly cast the then expression
+ boolean hasUnion = thenType == MinorType.UNION || elseType ==
MinorType.UNION;
+ if (unionTypeEnabled) {
+ if (thenType != elseType && !(thenType == MinorType.NULL ||
elseType == MinorType.NULL)) {
+
+ MinorType leastRestrictive = MinorType.UNION;
+ MajorType.Builder builder =
MajorType.newBuilder().setMinorType(MinorType.UNION).setMode(DataMode.OPTIONAL);
+ if (thenType == MinorType.UNION) {
+ for (MinorType subType :
conditions.expression.getMajorType().getSubTypeList()) {
+ builder.addSubType(subType);
+ }
+ } else {
+ builder.addSubType(thenType);
+ }
+ if (elseType == MinorType.UNION) {
+ for (MinorType subType :
newElseExpr.getMajorType().getSubTypeList()) {
+ builder.addSubType(subType);
+ }
+ } else {
+ builder.addSubType(elseType);
+ }
+ MajorType unionType = builder.build();
conditions = new IfExpression.IfCondition(newCondition,
- addCastExpression(conditions.expression,
newElseExpr.getMajorType(), functionLookupContext, errorCollector));
- } else if (leastRestrictive != elseType) {
- // Implicitly cast the else expression
- newElseExpr = addCastExpression(newElseExpr,
conditions.expression.getMajorType(), functionLookupContext, errorCollector);
- } else {
- /* Cannot cast one of the two expressions to make the output
type of if and else expression
- * to be the same. Raise error.
- */
- throw new DrillRuntimeException("Case expression should have
similar output type on all its branches");
+ addCastExpression(conditions.expression, unionType,
functionLookupContext, errorCollector));
+ newElseExpr = addCastExpression(newElseExpr, unionType,
functionLookupContext, errorCollector);
+ }
+
+ } else {
+ // Check if we need a cast
+ if (thenType != elseType && !(thenType == MinorType.NULL ||
elseType == MinorType.NULL)) {
+
+ MinorType leastRestrictive =
TypeCastRules.getLeastRestrictiveType((Arrays.asList(thenType, elseType)));
+ if (leastRestrictive != thenType) {
+ // Implicitly cast the then expression
+ conditions = new IfExpression.IfCondition(newCondition,
+ addCastExpression(conditions.expression,
newElseExpr.getMajorType(), functionLookupContext, errorCollector));
+ } else if (leastRestrictive != elseType) {
+ // Implicitly cast the else expression
+ newElseExpr = addCastExpression(newElseExpr,
conditions.expression.getMajorType(), functionLookupContext, errorCollector);
+ } else {
+ /* Cannot cast one of the two expressions to make the output
type of if and else expression
+ * to be the same. Raise error.
+ */
+ throw new DrillRuntimeException("Case expression should have
similar output type on all its branches");
--- End diff --
With union type enabled, we should never get here, right? It seems like
you're adding the Union implicit casts to the implicit casting map. Are we
going to get union casts even if union type is disabled because of the implicit
casting map?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---