Return-Path: X-Original-To: apmail-drill-dev-archive@www.apache.org Delivered-To: apmail-drill-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3E543180D1 for ; Wed, 2 Mar 2016 18:19:06 +0000 (UTC) Received: (qmail 22140 invoked by uid 500); 2 Mar 2016 18:19:06 -0000 Delivered-To: apmail-drill-dev-archive@drill.apache.org Received: (qmail 22086 invoked by uid 500); 2 Mar 2016 18:19:06 -0000 Mailing-List: contact dev-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 dev@drill.apache.org Received: (qmail 22075 invoked by uid 99); 2 Mar 2016 18:19:05 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Mar 2016 18:19:05 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 9B6B7E05E1; Wed, 2 Mar 2016 18:19:05 +0000 (UTC) From: jacques-n To: dev@drill.apache.org Reply-To: dev@drill.apache.org References: In-Reply-To: Subject: [GitHub] drill pull request: Drill 4372 review Content-Type: text/plain Message-Id: <20160302181905.9B6B7E05E1@git1-us-west.apache.org> Date: Wed, 2 Mar 2016 18:19:05 +0000 (UTC) Github user jacques-n commented on a diff in the pull request: https://github.com/apache/drill/pull/397#discussion_r54765921 --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/DrillFunctionRegistry.java --- @@ -92,38 +94,110 @@ public DrillFunctionRegistry(ScanResult classpathScan) { } public int size(){ - return methods.size(); + return registeredFunctions.size(); } /** Returns functions with given name. Function name is case insensitive. */ public List getMethods(String name) { - return this.methods.get(name.toLowerCase()); + return this.registeredFunctions.get(name.toLowerCase()); + } + + public Collection getAllMethods() { + return Collections.unmodifiableCollection(registeredFunctions.values()); } public void register(DrillOperatorTable operatorTable) { - SqlOperator op; - for (Entry> function : methods.asMap().entrySet()) { - Set argCounts = Sets.newHashSet(); - String name = function.getKey().toUpperCase(); + for (Entry> function : registeredFunctions.asMap().entrySet()) { + final ArrayListMultimap, DrillFuncHolder> functions = ArrayListMultimap.create(); + final ArrayListMultimap aggregateFunctions = ArrayListMultimap.create(); + final String name = function.getKey().toUpperCase(); + boolean isDeterministic = true; for (DrillFuncHolder func : function.getValue()) { - if (argCounts.add(func.getParamCount())) { - if (func.isAggregating()) { - op = new DrillSqlAggOperator(name, func.getParamCount()); - } else { - boolean isDeterministic; - // prevent Drill from folding constant functions with types that cannot be materialized - // into literals - if (DrillConstExecutor.NON_REDUCIBLE_TYPES.contains(func.getReturnType().getMinorType())) { - isDeterministic = false; - } else { - isDeterministic = func.isDeterministic(); - } - op = new DrillSqlOperator(name, func.getParamCount(), func.getReturnType(), isDeterministic); - } - operatorTable.add(function.getKey(), op); + final int paramCount = func.getParamCount(); + if(func.isAggregating()) { + aggregateFunctions.put(paramCount, func); + } else { + final Pair argNumerRange = getArgNumerRange(name, func); + functions.put(argNumerRange, func); } + + if(!func.isDeterministic()) { + isDeterministic = false; + } + } + for (Entry, Collection> entry : functions.asMap().entrySet()) { + final DrillSqlOperator drillSqlOperator; + final Pair range = entry.getKey(); + final int max = range.getRight(); + final int min = range.getLeft(); + drillSqlOperator = new DrillSqlOperator( + name, + Lists.newArrayList(entry.getValue()), + min, + max, + isDeterministic); + operatorTable.add(name, drillSqlOperator); + } + for (Entry> entry : aggregateFunctions.asMap().entrySet()) { + operatorTable.add(name, new DrillSqlAggOperator(name, Lists.newArrayList(entry.getValue()), entry.getKey())); } } + + registerCalcitePlaceHolderFunction(operatorTable); + } + + /** + * These {@link DrillSqlOperator} merely act as a placeholder so that Calcite + * allows convert_to(), convert_from(), flatten(), date_part() functions in SQL. + */ + private void registerCalcitePlaceHolderFunction(DrillOperatorTable operatorTable) { + final String convert_to = "CONVERT_TO"; + final String convert_from = "CONVERT_FROM"; + final String flatten = "FLATTEN"; + final String date_part = "DATE_PART"; + + operatorTable.add(convert_to, + new DrillSqlOperator(convert_to, + 2, + true)); + operatorTable.add(convert_from, + new DrillSqlOperator(convert_from, + 2, + true)); + operatorTable.add(flatten, + new DrillSqlOperator(flatten, + 1, + true)); + operatorTable.add(date_part, + new DrillSqlOperator(date_part, + 2, + true)); } + private Pair getArgNumerRange(final String name, final DrillFuncHolder func) { + switch(name.toUpperCase()) { + case "CONCAT": + return Pair.of(1, Integer.MAX_VALUE); + + // Drill does not have a FunctionTemplate for the lpad/rpad with two arguments. + // It relies on DrillOptiq.java to add a third dummy argument to be acceptable + // by the FunctionTemplate in StringFunctions.java + case "LPAD": + case "RPAD": + return Pair.of(2, 3); + + // Similar to the reason above, DrillOptiq.java is used for rewritting + case "LTRIM": + case "RTRIM": + case "BTRIM": + return Pair.of(1, 2); + + // Similar to the reason above, DrillOptiq.java is used for rewritting + case "LENGTH": + return Pair.of(1, 2); + + default: + return Pair.of(func.getParamCount(), func.getParamCount()); + } + } --- End diff -- Your last suggestion is what I'm saying. Just implement a 2 argument implementation then everything works the same as everything else. The 2 argument version can share code/delegate the three argument version. --- 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 infrastructure@apache.org or file a JIRA ticket with INFRA. ---