public Tuple function()

in legacy/src/main/java/org/opensearch/sql/legacy/utils/SQLFunctions.java [113:398]


    public Tuple<String, String> function(String methodName, List<KVValue> paramers, String name,
                                                 boolean returnValue) throws SqlParseException {
        Tuple<String, String> functionStr = null;
        switch (methodName.toLowerCase()) {
            case "cast": {
                SQLCastExpr castExpr = (SQLCastExpr) ((SQLIdentifierExpr) paramers.get(0).value).getParent();
                String typeName = castExpr.getDataType().getName();
                functionStr = cast(typeName, paramers);
                break;
            }
            case "lower": {
                functionStr = lower(
                        (SQLExpr) paramers.get(0).value,
                        getLocaleForCaseChangingFunction(paramers),
                        name
                );
                break;
            }
            case "upper": {
                functionStr = upper(
                        (SQLExpr) paramers.get(0).value,
                        getLocaleForCaseChangingFunction(paramers),
                        name);
                break;
            }

            // Split is currently not supported since its using .split() in painless which is not whitelisted
            case "split":
                if (paramers.size() == 3) {
                    functionStr = split((SQLExpr) paramers.get(0).value,
                            Util.expr2Object((SQLExpr) paramers.get(1).value).toString(),
                            Integer.parseInt(Util.expr2Object((SQLExpr) paramers.get(2).value).toString()), name);
                } else {
                    functionStr = split((SQLExpr) paramers.get(0).value,
                            paramers.get(1).value.toString(),
                            name);
                }

                break;

            case "concat_ws":
                List<SQLExpr> result = Lists.newArrayList();
                for (int i = 1; i < paramers.size(); i++) {
                    result.add((SQLExpr) paramers.get(i).value);
                }
                functionStr = concat_ws(paramers.get(0).value.toString(), result);

                break;


            case "date_format":
                functionStr = date_format(
                        (SQLExpr) paramers.get(0).value,
                        Util.expr2Object((SQLExpr) paramers.get(1).value).toString(),
                        paramers.size() > 2 ? Util.expr2Object((SQLExpr) paramers.get(2).value).toString() : null,
                        name);
                break;

            case "year":
                functionStr = dateFunctionTemplate("year", (SQLExpr) paramers.get(0).value);
                break;
            case "month_of_year":
            case "month":
                functionStr = dateFunctionTemplate("monthValue", (SQLExpr) paramers.get(0).value);
                break;
            case "monthname":
                functionStr = dateFunctionTemplate("month", (SQLExpr) paramers.get(0).value);
                break;
            case "week_of_year":
                functionStr = dateFunctionTemplate("weekOfWeekyear",
                                                   "get(WeekFields.ISO.weekOfWeekBasedYear())",
                                                   (SQLExpr) paramers.get(0).value);
                break;
            case "day_of_year":
                functionStr = dateFunctionTemplate("dayOfYear", (SQLExpr) paramers.get(0).value);
                break;
            case "day_of_month":
            case "dayofmonth":
                functionStr = dateFunctionTemplate("dayOfMonth", (SQLExpr) paramers.get(0).value);
                break;
            case "day_of_week":
                functionStr = dateFunctionTemplate("dayOfWeek",
                                                   "getDayOfWeekEnum().getValue()",
                                                   (SQLExpr) paramers.get(0).value);
                break;
            case "date":
                functionStr = date((SQLExpr) paramers.get(0).value);
                break;
            case "hour_of_day":
                functionStr = dateFunctionTemplate("hour", (SQLExpr) paramers.get(0).value);
                break;
            case "minute_of_day":
                functionStr = dateFunctionTemplate("minuteOfDay",
                                                   "get(ChronoField.MINUTE_OF_DAY)",
                                                   (SQLExpr) paramers.get(0).value);
                break;
            case "minute_of_hour":
                functionStr = dateFunctionTemplate("minute", (SQLExpr) paramers.get(0).value);
                break;
            case "second_of_minute":
                functionStr = dateFunctionTemplate("second", (SQLExpr) paramers.get(0).value);
                break;
            case "timestamp":
                functionStr = timestamp((SQLExpr) paramers.get(0).value);
                break;
            case "maketime":
                functionStr = maketime((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value,
                        (SQLExpr) paramers.get(2).value);
                break;
            case "now":
                functionStr = now();
                break;
            case "curdate":
                functionStr = curdate();
                break;

            case "e":
            case "pi":
                methodName = methodName.toUpperCase();
                functionStr = mathConstantTemplate("Math." + methodName, methodName);
                break;

            case "abs":
            case "round":
            case "floor":
            case "ceil":
            case "cbrt":
            case "rint":
            case "exp":
            case "expm1":
            case "sqrt":
            case "sin":
            case "cos":
            case "tan":
            case "asin":
            case "acos":
            case "atan":
            case "sinh":
            case "cosh":
                functionStr = mathSingleValueTemplate("Math." + methodName, methodName,
                        (SQLExpr) paramers.get(0).value, name);
                break;

            case "rand":
                if (paramers.isEmpty()) {
                    functionStr = rand();
                } else {
                    functionStr = rand((SQLExpr) paramers.get(0).value);
                }
                break;

            case "cot":
                // OpenSearch does not support the function name cot
                functionStr = mathSingleValueTemplate("1 / Math.tan", methodName,
                        (SQLExpr) paramers.get(0).value, name);
                break;

            case "sign":
            case "signum":
                methodName = "signum";
                functionStr = mathSingleValueTemplate("Math." + methodName, methodName,
                        (SQLExpr) paramers.get(0).value, name);
                break;

            case "pow":
            case "power":
                methodName = "pow";
                functionStr = mathDoubleValueTemplate("Math." + methodName, methodName,
                        (SQLExpr) paramers.get(0).value, Util.expr2Object((SQLExpr) paramers.get(1).value).toString(),
                        name);
                break;

            case "atan2":
                functionStr = mathDoubleValueTemplate("Math." + methodName, methodName,
                        (SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
                break;

            case "substring":
                functionStr = substring((SQLExpr) paramers.get(0).value,
                        Integer.parseInt(Util.expr2Object((SQLExpr) paramers.get(1).value).toString()),
                        Integer.parseInt(Util.expr2Object((SQLExpr) paramers.get(2).value).toString()));
                break;

            case "degrees":
                functionStr = degrees((SQLExpr) paramers.get(0).value, name);
                break;
            case "radians":
                functionStr = radians((SQLExpr) paramers.get(0).value, name);
                break;

            case "trim":
                functionStr = trim((SQLExpr) paramers.get(0).value, name);
                break;

            case "add":
                functionStr = add((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
                break;

            case "subtract":
                functionStr = subtract((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
                break;
            case "divide":
                functionStr = divide((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
                break;

            case "multiply":
                functionStr = multiply((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
                break;
            case "modulus":
                functionStr = modulus((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
                break;

            case "field":
                functionStr = field(Util.expr2Object((SQLExpr) paramers.get(0).value).toString());
                break;

            case "log2":
                functionStr = log(SQLUtils.toSQLExpr("2"), (SQLExpr) paramers.get(0).value, name);
                break;
            case "log10":
                functionStr = log10((SQLExpr) paramers.get(0).value);
                break;
            case "log":
                if (paramers.size() > 1) {
                    functionStr = log((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value, name);
                } else {
                    functionStr = ln((SQLExpr) paramers.get(0).value);
                }
                break;
            case "ln":
                functionStr = ln((SQLExpr) paramers.get(0).value);
                break;
            case "assign":
                functionStr = assign((SQLExpr) paramers.get(0).value);
                break;
            case "length":
                functionStr = length((SQLExpr) paramers.get(0).value);
                break;
            case "replace":
                functionStr = replace((SQLExpr) paramers.get(0).value, paramers.get(1).value.toString(),
                        paramers.get(2).value.toString());
                break;
            case "locate":
                int start = 0;
                if (paramers.size() > 2) {
                    start = Integer.parseInt(paramers.get(2).value.toString());
                }
                functionStr = locate(paramers.get(0).value.toString(), (SQLExpr) paramers.get(1).value, start);
                break;
            case "rtrim":
                functionStr = rtrim((SQLExpr) paramers.get(0).value);
                break;
            case "ltrim":
                functionStr = ltrim((SQLExpr) paramers.get(0).value);
                break;
            case "ascii":
                functionStr = ascii((SQLExpr) paramers.get(0).value);
                break;
            case "left":
                functionStr = left((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
                break;
            case "right":
                functionStr = right((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
                break;

            case "if":
                functionStr = ifFunc(paramers);
                break;
            case "ifnull":
                functionStr = ifnull((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
                break;
            case "isnull":
                functionStr = isnull((SQLExpr) paramers.get(0).value);
                break;

            default:

        }
        if (returnValue) {
            String generatedFieldName = functionStr.v1();
            String returnCommand = ";return " + generatedFieldName + ";";
            String newScript = functionStr.v2() + returnCommand;
            functionStr = new Tuple<>(generatedFieldName, newScript);
        }
        return functionStr;
    }