jon-wei commented on a change in pull request #7588: multi-value string column
support for expressions
URL: https://github.com/apache/incubator-druid/pull/7588#discussion_r290541208
##########
File path: core/src/main/java/org/apache/druid/math/expr/Function.java
##########
@@ -1453,4 +1515,425 @@ public ExprEval apply(List<Expr> args,
Expr.ObjectBinding bindings)
}
}
+ class StringToArrayFunction implements Function
+ {
+ @Override
+ public String name()
+ {
+ return "string_to_array";
+ }
+
+ void validateArguments(List<Expr> args)
+ {
+ if (args.size() != 2) {
+ throw new IAE("Function[%s] needs 2 argument", name());
+ }
+ }
+
+ @Override
+ public ExprEval apply(List<Expr> args, Expr.ObjectBinding bindings)
+ {
+ validateArguments(args);
+
+ final ExprEval expr = args.get(0).eval(bindings);
+ final String arrayString = expr.asString();
+ if (arrayString == null) {
+ return ExprEval.of(null);
+ }
+
+ final String split = args.get(1).eval(bindings).asString();
+ return ExprEval.ofStringArray(arrayString.split(split != null ? split :
""));
+ }
+
+ @Override
+ public Set<Expr> getScalarInputs(List<Expr> args)
+ {
+ validateArguments(args);
+ return ImmutableSet.copyOf(args);
+ }
+ }
+
+ /**
+ * Function that operates on array typed operands
+ */
+ interface ArrayFunction extends Function
+ {
+ default void validateArguments(List<Expr> args)
+ {
+ if (args.size() != 1) {
+ throw new IAE("Function[%s] needs 1 argument", name());
+ }
+ }
+
+ @Override
+ default Set<Expr> getArrayInputs(List<Expr> args)
+ {
+ validateArguments(args);
+ return ImmutableSet.of(args.get(0));
+ }
+
+ @Override
+ default Set<Expr> getScalarInputs(List<Expr> args)
+ {
+ return Collections.emptySet();
+ }
+ }
+
+ /**
+ * {@link ArraysFunction} that takes 1 array operand and 1 scalar operand
+ */
+ abstract class ArrayScalarFunction implements ArrayFunction
+ {
+ @Override
+ public void validateArguments(List<Expr> args)
+ {
+ if (args.size() != 2) {
+ throw new IAE("Function[%s] needs 2 argument", name());
+ }
+ }
+
+ @Override
+ public Set<Expr> getScalarInputs(List<Expr> args)
+ {
+ validateArguments(args);
+ return ImmutableSet.of(args.get(1));
+ }
+
+ @Override
+ public Set<Expr> getArrayInputs(List<Expr> args)
+ {
+ return ImmutableSet.of(args.get(0));
+ }
+
+ @Override
+ public ExprEval apply(List<Expr> args, Expr.ObjectBinding bindings)
+ {
+ validateArguments(args);
+ final ExprEval arrayExpr = args.get(0).eval(bindings);
+ final ExprEval scalarExpr = args.get(1).eval(bindings);
+ if (arrayExpr.asArray() == null) {
+ return ExprEval.of(null);
+ }
+ return doApply(arrayExpr, scalarExpr);
+ }
+
+ abstract ExprEval doApply(ExprEval arrayExpr, ExprEval scalarExpr);
+ }
+
+ /**
+ * {@link ArraysFunction} that takes 2 array operands
+ */
+ abstract class ArraysFunction implements ArrayFunction
+ {
+ @Override
+ public void validateArguments(List<Expr> args)
+ {
+ if (args.size() != 2) {
Review comment:
should this and other array-handling functions check `Expr.isArray()` in
`validateArguments`?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]