You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2019/06/24 22:09:26 UTC
[GitHub] [incubator-druid] clintropolis commented on a change in pull
request #7950: Add array_slice and array_unshift function expr
clintropolis commented on a change in pull request #7950: Add array_slice and array_unshift function expr
URL: https://github.com/apache/incubator-druid/pull/7950#discussion_r296935909
##########
File path: core/src/main/java/org/apache/druid/math/expr/Function.java
##########
@@ -2062,4 +2062,111 @@ ExprEval doApply(ExprEval lhsExpr, ExprEval rhsExpr)
return ExprEval.bestEffortOf(any);
}
}
+
+ class ArraySliceFunction implements Function
+ {
+ @Override
+ public String name()
+ {
+ return "array_slice";
+ }
+
+ @Override
+ public void validateArguments(List<Expr> args)
+ {
+ if (args.size() != 2 && args.size() != 3) {
+ throw new IAE("Function[%s] needs 2 or 3 arguments", name());
+ }
+ }
+
+ @Override
+ public ExprEval apply(List<Expr> args, Expr.ObjectBinding bindings)
+ {
+ final ExprEval expr = args.get(0).eval(bindings);
+ final Object[] array = expr.asArray();
+ if (array == null) {
+ return ExprEval.of(null);
+ }
+
+ final int start = args.get(1).eval(bindings).asInt();
+ int end = array.length;
+ if (args.size() == 3) {
+ end = args.get(2).eval(bindings).asInt();
+ }
+
+ if (start < 0 || start > array.length || start > end) {
+ // Arrays.copyOfRange will throw exception in these cases
+ return ExprEval.of(null);
+ }
+
+ switch (expr.type()) {
+ case STRING:
+ case STRING_ARRAY:
+ return ExprEval.ofStringArray(Arrays.copyOfRange(expr.asStringArray(), start, end));
+ case LONG:
+ case LONG_ARRAY:
+ return ExprEval.ofLongArray(Arrays.copyOfRange(expr.asLongArray(), start, end));
+ case DOUBLE:
+ case DOUBLE_ARRAY:
+ return ExprEval.ofDoubleArray(Arrays.copyOfRange(expr.asDoubleArray(), start, end));
+ }
+ throw new RE("Unable to slice to unknown type %s", expr.type());
+ }
+
+ @Override
+ public Set<Expr> getScalarInputs(List<Expr> args)
+ {
+ return ImmutableSet.of(args.get(1), args.get(2));
Review comment:
this should probably check if args length is 2 or 3?
----------------------------------------------------------------
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:
users@infra.apache.org
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org