[ https://issues.apache.org/jira/browse/DRILL-3232?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14983603#comment-14983603 ]
ASF GitHub Bot commented on DRILL-3232: --------------------------------------- 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. > Modify existing vectors to allow type promotion > ----------------------------------------------- > > Key: DRILL-3232 > URL: https://issues.apache.org/jira/browse/DRILL-3232 > Project: Apache Drill > Issue Type: Sub-task > Components: Execution - Codegen, Execution - Data Types, Execution - > Relational Operators, Functions - Drill > Reporter: Steven Phillips > Assignee: Hanifi Gunes > Fix For: 1.3.0 > > > Support the ability for existing vectors to be promoted similar to supported > implicit casting rules. > For example: > INT > DOUBLE > STRING > EMBEDDED -- This message was sent by Atlassian JIRA (v6.3.4#6332)