Github user StevenMPhillips commented on a diff in the pull request:

    https://github.com/apache/drill/pull/207#discussion_r43563065
  
    --- 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 --
    
    I don't think this is a problem. In this case, the case statement will not 
coerce to a type unless one of the branches output is that type. So as long as 
there is no Union type in the input, there won't be a union in the output.


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

Reply via email to