From f81c1b3f38a38be7776cc5445678891b49484658 Mon Sep 17 00:00:00 2001 From: Lantao Jin Date: Fri, 2 Aug 2024 15:02:46 +0800 Subject: [PATCH] shorten the map declaration Signed-off-by: Lantao Jin --- .../ppl/utils/BuiltinFunctionTranslator.java | 77 ++++++++++++------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/ppl-spark-integration/src/main/java/org/opensearch/sql/ppl/utils/BuiltinFunctionTranslator.java b/ppl-spark-integration/src/main/java/org/opensearch/sql/ppl/utils/BuiltinFunctionTranslator.java index 6f17b1247..53c6673a8 100644 --- a/ppl-spark-integration/src/main/java/org/opensearch/sql/ppl/utils/BuiltinFunctionTranslator.java +++ b/ppl-spark-integration/src/main/java/org/opensearch/sql/ppl/utils/BuiltinFunctionTranslator.java @@ -11,9 +11,28 @@ import org.opensearch.sql.expression.function.BuiltinFunctionName; import java.util.List; -import java.util.Locale; import java.util.Map; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.ADD; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.SUBTRACT; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.MULTIPLY; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.DIVIDE; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.MODULUS; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.DAY_OF_WEEK; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.DAY_OF_MONTH; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.DAY_OF_YEAR; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.WEEK_OF_YEAR; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.WEEK; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.MONTH_OF_YEAR; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.HOUR_OF_DAY; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.MINUTE_OF_HOUR; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.SECOND_OF_MINUTE; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.SUBDATE; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.ADDDATE; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.DATEDIFF; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.LOCALTIME; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.IS_NULL; +import static org.opensearch.sql.expression.function.BuiltinFunctionName.IS_NOT_NULL; import static org.opensearch.sql.ppl.utils.DataTypeTransformer.seq; import static scala.Option.empty; @@ -22,31 +41,32 @@ public interface BuiltinFunctionTranslator { /** * The name mapping between PPL builtin functions to Spark builtin functions. */ - static final Map SPARK_BUILTIN_FUNCTION_NAME_MAPPING = new ImmutableMap.Builder() - // arithmetic operators - .put(BuiltinFunctionName.ADD.name().toLowerCase(Locale.ROOT), "+") - .put(BuiltinFunctionName.SUBTRACT.name().toLowerCase(Locale.ROOT), "-") - .put(BuiltinFunctionName.MULTIPLY.name().toLowerCase(Locale.ROOT), "*") - .put(BuiltinFunctionName.DIVIDE.name().toLowerCase(Locale.ROOT), "/") - .put(BuiltinFunctionName.MODULUS.name().toLowerCase(Locale.ROOT), "%") - // time functions - .put(BuiltinFunctionName.DAY_OF_WEEK.name().toLowerCase(Locale.ROOT), "dayofweek") - .put(BuiltinFunctionName.DAY_OF_MONTH.name().toLowerCase(Locale.ROOT), "dayofmonth") - .put(BuiltinFunctionName.DAY_OF_YEAR.name().toLowerCase(Locale.ROOT), "dayofyear") - .put(BuiltinFunctionName.WEEK_OF_YEAR.name().toLowerCase(Locale.ROOT), "weekofyear") - .put(BuiltinFunctionName.WEEK.name().toLowerCase(Locale.ROOT), "weekofyear") - .put(BuiltinFunctionName.MONTH_OF_YEAR.name().toLowerCase(Locale.ROOT), "month") - .put(BuiltinFunctionName.HOUR_OF_DAY.name().toLowerCase(Locale.ROOT), "hour") - .put(BuiltinFunctionName.MINUTE_OF_HOUR.name().toLowerCase(Locale.ROOT), "minute") - .put(BuiltinFunctionName.SECOND_OF_MINUTE.name().toLowerCase(Locale.ROOT), "second") - .put(BuiltinFunctionName.SUBDATE.name().toLowerCase(Locale.ROOT), "date_sub") // only maps subdate(date, days) - .put(BuiltinFunctionName.ADDDATE.name().toLowerCase(Locale.ROOT), "date_add") // only maps adddate(date, days) - .put(BuiltinFunctionName.DATEDIFF.name().toLowerCase(Locale.ROOT), "datediff") - .put(BuiltinFunctionName.LOCALTIME.name().toLowerCase(Locale.ROOT), "localtimestamp") - // condition functions - .put(BuiltinFunctionName.IS_NULL.name().toLowerCase(Locale.ROOT), "isnull") - .put(BuiltinFunctionName.IS_NOT_NULL.name().toLowerCase(Locale.ROOT), "isnotnull") - .build(); + static final Map SPARK_BUILTIN_FUNCTION_NAME_MAPPING + = new ImmutableMap.Builder() + // arithmetic operators + .put(ADD, "+") + .put(SUBTRACT, "-") + .put(MULTIPLY, "*") + .put(DIVIDE, "/") + .put(MODULUS, "%") + // time functions + .put(DAY_OF_WEEK, "dayofweek") + .put(DAY_OF_MONTH, "dayofmonth") + .put(DAY_OF_YEAR, "dayofyear") + .put(WEEK_OF_YEAR, "weekofyear") + .put(WEEK, "weekofyear") + .put(MONTH_OF_YEAR, "month") + .put(HOUR_OF_DAY, "hour") + .put(MINUTE_OF_HOUR, "minute") + .put(SECOND_OF_MINUTE, "second") + .put(SUBDATE, "date_sub") // only maps subdate(date, days) + .put(ADDDATE, "date_add") // only maps adddate(date, days) + .put(DATEDIFF, "datediff") + .put(LOCALTIME, "localtimestamp") + //condition functions + .put(IS_NULL, "isnull") + .put(IS_NOT_NULL, "isnotnull") + .build(); static Expression builtinFunction(org.opensearch.sql.ast.expression.Function function, List args) { if (BuiltinFunctionName.of(function.getFuncName()).isEmpty()) { @@ -54,8 +74,9 @@ static Expression builtinFunction(org.opensearch.sql.ast.expression.Function fun // TODO should we support more functions which are not PPL builtin functions. E.g Spark builtin functions throw new UnsupportedOperationException(function.getFuncName() + " is not a builtin function of PPL"); } else { - String name = BuiltinFunctionName.of(function.getFuncName()).get().name().toLowerCase(Locale.ROOT); - name = SPARK_BUILTIN_FUNCTION_NAME_MAPPING.getOrDefault(name, name); + BuiltinFunctionName builtin = BuiltinFunctionName.of(function.getFuncName()).get(); + String name = SPARK_BUILTIN_FUNCTION_NAME_MAPPING + .getOrDefault(builtin, builtin.getName().getFunctionName()); return new UnresolvedFunction(seq(name), seq(args), false, empty(),false); } }