Skip to content

Commit

Permalink
shorten the map declaration
Browse files Browse the repository at this point in the history
Signed-off-by: Lantao Jin <[email protected]>
  • Loading branch information
LantaoJin committed Aug 2, 2024
1 parent 8843a87 commit f81c1b3
Showing 1 changed file with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,40 +41,42 @@ public interface BuiltinFunctionTranslator {
/**
* The name mapping between PPL builtin functions to Spark builtin functions.
*/
static final Map<String, String> SPARK_BUILTIN_FUNCTION_NAME_MAPPING = new ImmutableMap.Builder<String, String>()
// 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<BuiltinFunctionName, String> SPARK_BUILTIN_FUNCTION_NAME_MAPPING
= new ImmutableMap.Builder<BuiltinFunctionName, String>()
// 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<Expression> args) {
if (BuiltinFunctionName.of(function.getFuncName()).isEmpty()) {
// TODO change it when UDF is supported
// 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);
}
}
Expand Down

0 comments on commit f81c1b3

Please sign in to comment.