mdayakar commented on code in PR #5084: URL: https://github.com/apache/hive/pull/5084#discussion_r1507519217
########## hplsql/src/main/java/org/apache/hive/hplsql/functions/InMemoryFunctionRegistry.java: ########## @@ -132,39 +133,84 @@ private boolean execProc(String name, HplsqlParser.Expr_func_paramsContext ctx) /** * Set parameters for user-defined function call */ - public static void setCallParameters(String procName, HplsqlParser.Expr_func_paramsContext actual, ArrayList<Var> actualValues, - HplsqlParser.Create_routine_paramsContext formal, - HashMap<String, Var> out, - Exec exec) { - if (actual == null || actual.func_param() == null || actualValues == null) { + public static void setCallParameters(String procName, HplsqlParser.Expr_func_paramsContext actual, + ArrayList<Var> actualValues, HplsqlParser.Create_routine_paramsContext formal, HashMap<String, Var> out, + Exec exec) { + // if it is a non-parameter function then just return. + if (actual == null && formal == null) { return; } - int actualCnt = actualValues.size(); - int formalCnt = formal.create_routine_param_item().size(); - if (formalCnt != actualCnt) { - throw new ArityException(actual.getParent(), procName, formalCnt, actualCnt); + int actualCnt = (actualValues == null) ? 0 : actualValues.size(); + int passedParamCnt = actualCnt; + List<HplsqlParser.Create_routine_param_itemContext> routineParamItem = formal.create_routine_param_item(); + int formalCnt = routineParamItem.size(); + ParserRuleContext ruleContext = (actual == null) ? null : actual.getParent(); + if (actualCnt > formalCnt) { + throw new ArityException(ruleContext, procName, formalCnt, actualCnt); } - for (int i = 0; i < actualCnt; i++) { - HplsqlParser.ExprContext a = actual.func_param(i).expr(); - HplsqlParser.Create_routine_param_itemContext p = getCallParameter(actual, formal, i); - String name = p.ident().getText(); - String type = p.dtype().getText(); - String len = null; - String scale = null; - if (p.dtype_len() != null) { - len = p.dtype_len().L_INT(0).getText(); - if (p.dtype_len().L_INT(1) != null) { - scale = p.dtype_len().L_INT(1).getText(); - } + Map<String, Integer> defaultParamNamesVsIndexes = new HashMap<>(); + if (actualCnt != formalCnt) { + populateDefaultParamDetails(routineParamItem, formalCnt, defaultParamNamesVsIndexes); + actualCnt = actualCnt + defaultParamNamesVsIndexes.size(); + if (actualCnt < formalCnt) { + throw new ArityException(ruleContext, procName, formalCnt, passedParamCnt); + } + } + + HplsqlParser.ExprContext exprContext = null; + HplsqlParser.Create_routine_param_itemContext paramItemContext = null; + Var value = null; + // set the passed params + for (int i = 0; i < passedParamCnt; i++) { + exprContext = actual.func_param(i).expr(); + paramItemContext = getCallParameter(actual, formal, i); + value = actualValues.get(i); + // for any default param value is passed then remove it from default param list + defaultParamNamesVsIndexes.remove(paramItemContext.ident().getText()); + setCallParameter(actual, out, exec, exprContext, paramItemContext, value); + } + // set the remaining default params + for (int index : defaultParamNamesVsIndexes.values()) { + paramItemContext = formal.create_routine_param_item().get(index); + exprContext = paramItemContext.dtype_default().expr(); + value = exec.evalPop(paramItemContext.dtype_default().expr()); + setCallParameter(actual, out, exec, exprContext, paramItemContext, value); + } + // if actual param count + remaining default param count is lesser than formal param count then throw exception as some params are missing + if ((passedParamCnt + defaultParamNamesVsIndexes.size()) < formalCnt) { + throw new ArityException(ruleContext, procName, formalCnt, passedParamCnt); + } + } + + private static void populateDefaultParamDetails(List<HplsqlParser.Create_routine_param_itemContext> routineParamItem, int formalCnt, + Map<String, Integer> defaultParamNamesVsIndexes) { + for (int i = 0; i < formalCnt; i++) { Review Comment: Fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org