Return-Path: X-Original-To: apmail-drill-issues-archive@minotaur.apache.org Delivered-To: apmail-drill-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 07F6617D2F for ; Fri, 30 Oct 2015 04:58:28 +0000 (UTC) Received: (qmail 85450 invoked by uid 500); 30 Oct 2015 04:58:27 -0000 Delivered-To: apmail-drill-issues-archive@drill.apache.org Received: (qmail 85416 invoked by uid 500); 30 Oct 2015 04:58:27 -0000 Mailing-List: contact issues-help@drill.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@drill.apache.org Delivered-To: mailing list issues@drill.apache.org Received: (qmail 85389 invoked by uid 99); 30 Oct 2015 04:58:27 -0000 Received: from arcas.apache.org (HELO arcas) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 30 Oct 2015 04:58:27 +0000 Received: from arcas.apache.org (localhost [127.0.0.1]) by arcas (Postfix) with ESMTP id AFE412C14E1 for ; Fri, 30 Oct 2015 04:58:27 +0000 (UTC) Date: Fri, 30 Oct 2015 04:58:27 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: issues@drill.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (DRILL-3232) Modify existing vectors to allow type promotion MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/DRILL-3232?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14981891#comment-14981891 ] ASF GitHub Bot commented on DRILL-3232: --------------------------------------- Github user jacques-n commented on a diff in the pull request: https://github.com/apache/drill/pull/207#discussion_r43471861 --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/ExpressionTreeMaterializer.java --- @@ -308,10 +334,115 @@ public LogicalExpression visitFunctionCall(FunctionCall call, FunctionLookupCont return matchedNonDrillFuncHolder.getExpr(call.getName(), extArgsWithCast, call.getPosition()); } + if (hasUnionInput(call)) { + return rewriteUnionFunction(call, functionLookupContext); + } + logFunctionResolutionError(errorCollector, call); return NullExpression.INSTANCE; } + private static final Set UNION_FUNCTIONS; + + static { + UNION_FUNCTIONS = new HashSet<>(); + for (MinorType t : MinorType.values()) { + UNION_FUNCTIONS.add("assert_" + t.name().toLowerCase()); + UNION_FUNCTIONS.add("is_" + t.name().toLowerCase()); + } + UNION_FUNCTIONS.add("typeof"); + } + + private boolean hasUnionInput(FunctionCall call) { + for (LogicalExpression arg : call.args) { + if (arg.getMajorType().getMinorType() == MinorType.UNION) { + return true; + } + } + return false; + } + + private LogicalExpression rewriteUnionFunction(FunctionCall call, FunctionLookupContext functionLookupContext) { + LogicalExpression[] args = new LogicalExpression[call.args.size()]; + call.args.toArray(args); + + for (int i = 0; i < args.length; i++) { + LogicalExpression arg = call.args.get(i); + MajorType majorType = arg.getMajorType(); + + if (majorType.getMinorType() != MinorType.UNION) { + continue; + } + + List subTypes = majorType.getSubTypeList(); + Preconditions.checkState(subTypes.size() > 0, "Union type has no subtypes"); + + Queue ifConditions = Lists.newLinkedList(); + + for (MinorType minorType : subTypes) { + LogicalExpression ifCondition = getIsTypeExpressionForType(minorType, arg.accept(new CloneVisitor(), null)); + args[i] = getUnionCastExpressionForType(minorType, arg.accept(new CloneVisitor(), null)); + + List newArgs = Lists.newArrayList(); + for (LogicalExpression e : args) { + newArgs.add(e.accept(new CloneVisitor(), null)); + } + + errorCollectors.push(errorCollector); + errorCollector = new ErrorCollectorImpl(); + + LogicalExpression thenExpression = new FunctionCall(call.getName(), newArgs, call.getPosition()).accept(this, functionLookupContext); + + if (errorCollector.hasErrors()) { + thenExpression = getExceptionFunction(errorCollector.toErrorString()); + } + + errorCollector = errorCollectors.pop(); + + IfExpression.IfCondition condition = new IfCondition(ifCondition, thenExpression); + ifConditions.add(condition); + } + + LogicalExpression ifExpression = ifConditions.poll().expression; + + while (!ifConditions.isEmpty()) { + ifExpression = IfExpression.newBuilder().setIfCondition(ifConditions.poll()).setElse(ifExpression).build(); + } + + args[i] = ifExpression; + return ifExpression.accept(this, functionLookupContext); + } + throw new UnsupportedOperationException("Did not find any Union input types"); + } + + private LogicalExpression getExceptionFunction(String message) { + QuotedString msg = new QuotedString(message, ExpressionPosition.UNKNOWN); + List args = Lists.newArrayList(); + args.add(msg); + FunctionCall call = new FunctionCall(ExceptionFunction.EXCEPTION_FUNCTION_NAME, args, ExpressionPosition.UNKNOWN); + return call; + } + + private LogicalExpression getUnionCastExpressionForType(MinorType type, LogicalExpression arg) { + if (type == MinorType.UNION) { + return arg; + } + if (type == MinorType.LIST || type == MinorType.MAP) { + return getExceptionFunction("Unable to cast union to " + type); + } + String castFuncName = String.format("assert_%s", type.toString()); + List args = Lists.newArrayList(); + args.add(arg); --- End diff -- Probably just use Collections.singletonList() in call. > 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)