You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "gianm (via GitHub)" <gi...@apache.org> on 2025/09/19 06:21:55 UTC
[PR] Enable fallback vectorization by default. (druid)
gianm opened a new pull request, #18549:
URL: https://github.com/apache/druid/pull/18549
This patch updates the default value of druid.expressions.allowVectorizeFallback to true.
In addition, there are the following fixes and improvements:
1) Remove safe_divide's explicit "return false" override for canVectorize,
allowing it to fallback vectorize.
2) Fix getExponent's long-typed vectorized implementation. (It was casting
to float, so the exponent was inconsistent with the non-vector version,
which cast to double.)
3) Fix json_query_array's reported output type (was COMPLEX<json>, should
be ARRAY<COMPLEX<json>>).
4) FallbackVectorProcessor and Div now have their errors wrapped using
FunctionErrorReportingExprVectorProcessor, which handles errors
similarly to how FunctionExpr does it. (Div is the only already-vectorized
math function that we expect to throw errors).
5) ColumnarFrameCursorFactory now uses itself as the inspector for
canVectorize, rather than its signature. The signature returns
"unknown" for "hasMultipleValues", which leads to things being
non-vectorizable that really should be vectorizable.
6) Add many more functions to VectorExprResultConsistencyTest, and update
it to test negative numbers and zero. Because zero can lead to exceptions
in division functions, the test code is also updated to verify equivalence
of the thrown exceptions.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Enable fallback vectorization by default. (druid)
Posted by "clintropolis (via GitHub)" <gi...@apache.org>.
clintropolis commented on code in PR #18549:
URL: https://github.com/apache/druid/pull/18549#discussion_r2362000495
##########
processing/src/test/java/org/apache/druid/math/expr/VectorExprResultConsistencyTest.java:
##########
@@ -840,4 +961,62 @@ static NonnullPair<Expr.ObjectBinding[], Expr.VectorInputBinding> populateBindin
return new NonnullPair<>(objectBindings, vectorBinding);
}
+
+ private static Either<String, Object[]> evalVector(
+ Expr expr,
+ Expr.VectorInputBinding bindings,
+ @Nullable ExpressionType outputType
+ )
+ {
+ final ExprVectorProcessor<Object> processor = expr.asVectorProcessor(bindings);
+ final ExprEvalVector<?> vectorEval;
+ try {
+ vectorEval = processor.evalVector(bindings);
+ }
+ catch (ArithmeticException e) {
+ // After a few occasions, Java starts throwing ArithmeticException without a message for division by zero.
+ return Either.error(e.getClass().getName());
+ }
+ catch (Exception e) {
+ return Either.error(e.toString());
+ }
+
+ final Object[] vectorVals = vectorEval.getObjectVector();
+ // 'null' expressions can have an output type of null, but still evaluate in default mode, so skip type checks
Review Comment:
nit: i know these aren't new comments (there is another one a bit later), but consider removing reference to default value mode, i think i missed this, its just talking about how outputType isn't reliable for null values so it was skipping comparison
##########
processing/src/main/java/org/apache/druid/math/expr/vector/FunctionErrorReportingExprVectorProcessor.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.math.expr.vector;
+
+import org.apache.druid.error.DruidException;
+import org.apache.druid.math.expr.Expr;
+import org.apache.druid.math.expr.ExpressionProcessingException;
+import org.apache.druid.math.expr.ExpressionType;
+import org.apache.druid.math.expr.ExpressionValidationException;
+import org.apache.druid.segment.column.Types;
+
+/**
+ * Wrapper around {@link ExprVectorProcessor} that reports errors in the same style as {@code FunctionExpr}.
+ */
+public class FunctionErrorReportingExprVectorProcessor<T> implements ExprVectorProcessor<T>
+{
+ private final String functionName;
+ private final ExprVectorProcessor<T> delegate;
+
+ public FunctionErrorReportingExprVectorProcessor(String functionName, ExprVectorProcessor<T> delegate)
+ {
+ this.functionName = functionName;
+ this.delegate = delegate;
+ }
+
+ @Override
+ public ExprEvalVector<T> evalVector(Expr.VectorInputBinding bindings)
+ {
+ try {
+ return delegate.evalVector(bindings);
+ }
+ catch (ExpressionValidationException | ExpressionProcessingException e) {
+ // Already contain function name
+ throw DruidException.forPersona(DruidException.Persona.USER)
+ .ofCategory(DruidException.Category.INVALID_INPUT)
+ .build(e, e.getMessage());
+ }
+ catch (Types.InvalidCastException | Types.InvalidCastBooleanException e) {
+ throw DruidException.forPersona(DruidException.Persona.USER)
+ .ofCategory(DruidException.Category.INVALID_INPUT)
+ .build(e, "Function[%s] encountered exception: %s", functionName, e.getMessage());
+ }
+ catch (DruidException e) {
+ throw e;
Review Comment:
i guess there are probably some cases where this path loses the function name? I'm not sure if it matters all that much, but this is part of why i've been a bit hesitant on migrating to using `DruidException` inside expr processing in favor of still using the expression specific exceptions and catching them in the callers to turn into `DruidException`. I feel like its kind of hard to know if a more general exception like DruidException should be caught and wrapped or rethrown.
All that said, this is fine i think too, since this is mostly just my opinion
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Enable fallback vectorization by default. (druid)
Posted by "clintropolis (via GitHub)" <gi...@apache.org>.
clintropolis commented on code in PR #18549:
URL: https://github.com/apache/druid/pull/18549#discussion_r2362050144
##########
processing/src/main/java/org/apache/druid/math/expr/vector/FunctionErrorReportingExprVectorProcessor.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.math.expr.vector;
+
+import org.apache.druid.error.DruidException;
+import org.apache.druid.math.expr.Expr;
+import org.apache.druid.math.expr.ExpressionProcessingException;
+import org.apache.druid.math.expr.ExpressionType;
+import org.apache.druid.math.expr.ExpressionValidationException;
+import org.apache.druid.segment.column.Types;
+
+/**
+ * Wrapper around {@link ExprVectorProcessor} that reports errors in the same style as {@code FunctionExpr}.
+ */
+public class FunctionErrorReportingExprVectorProcessor<T> implements ExprVectorProcessor<T>
+{
+ private final String functionName;
+ private final ExprVectorProcessor<T> delegate;
+
+ public FunctionErrorReportingExprVectorProcessor(String functionName, ExprVectorProcessor<T> delegate)
+ {
+ this.functionName = functionName;
+ this.delegate = delegate;
+ }
+
+ @Override
+ public ExprEvalVector<T> evalVector(Expr.VectorInputBinding bindings)
+ {
+ try {
+ return delegate.evalVector(bindings);
+ }
+ catch (ExpressionValidationException | ExpressionProcessingException e) {
+ // Already contain function name
+ throw DruidException.forPersona(DruidException.Persona.USER)
+ .ofCategory(DruidException.Category.INVALID_INPUT)
+ .build(e, e.getMessage());
+ }
+ catch (Types.InvalidCastException | Types.InvalidCastBooleanException e) {
+ throw DruidException.forPersona(DruidException.Persona.USER)
+ .ofCategory(DruidException.Category.INVALID_INPUT)
+ .build(e, "Function[%s] encountered exception: %s", functionName, e.getMessage());
+ }
+ catch (DruidException e) {
+ throw e;
Review Comment:
ah looking around a bit more, is already a mix, so this is fine
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Enable fallback vectorization by default. (druid)
Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #18549:
URL: https://github.com/apache/druid/pull/18549#discussion_r2361984684
##########
processing/src/test/java/org/apache/druid/math/expr/VectorExprResultConsistencyTest.java:
##########
@@ -604,103 +754,74 @@
* runs expressions against bindings generated by increasing a counter
*/
public static void testExpressionSequentialBindings(
- String expr,
- Expr parsed,
- Map<String, ExpressionType> types,
- int numIterations
+ final String expr,
+ final Expr parsed,
+ final Map<String, ExpressionType> types,
+ final int numIterations
)
{
for (int iter = 0; iter < numIterations; iter++) {
- NonnullPair<Expr.ObjectBinding[], Expr.VectorInputBinding> bindings = makeSequentialBinding(
- VECTOR_SIZE,
- types,
- 1 + (iter * VECTOR_SIZE) // the plus 1 is because dividing by zero is sad
+ assertEvalsMatch(
+ expr,
+ parsed,
+ makeSequentialBinding(
+ VECTOR_SIZE,
+ types,
+ -2 + (iter * VECTOR_SIZE) // include negative numbers and zero
+ )
);
- Assert.assertTrue(StringUtils.format("Cannot vectorize %s", expr), parsed.canVectorize(bindings.rhs));
- ExpressionType outputType = parsed.getOutputType(bindings.rhs);
- ExprEvalVector<?> vectorEval = parsed.asVectorProcessor(bindings.rhs).evalVector(bindings.rhs);
- // 'null' expressions can have an output type of null, but still evaluate in default mode, so skip type checks
- if (outputType != null) {
- Assert.assertEquals(expr, outputType, vectorEval.getType());
- }
- final Object[] vectorVals = vectorEval.getObjectVector();
- for (int i = 0; i < VECTOR_SIZE; i++) {
- ExprEval<?> eval = parsed.eval(bindings.lhs[i]);
- // 'null' expressions can have an output type of null, but still evaluate in default mode, so skip type checks
- if (outputType != null && !eval.isNumericNull()) {
- Assert.assertEquals(eval.type(), outputType);
- }
- if (outputType != null && outputType.isArray()) {
- Assert.assertArrayEquals(
- StringUtils.format("Values do not match for row %s for expression %s", i, expr),
- (Object[]) eval.value(),
- (Object[]) vectorVals[i]
- );
- } else {
- Assert.assertEquals(
- StringUtils.format("Values do not match for row %s for expression %s", i, expr),
- eval.value(),
- vectorVals[i]
- );
- }
- }
}
}
public static void testExpressionRandomizedBindings(
- String expr,
- Expr parsed,
- Map<String, ExpressionType> types,
- int numIterations
+ final String expr,
+ final Expr parsed,
+ final Map<String, ExpressionType> types,
+ final int numIterations
)
{
- Expr.InputBindingInspector inspector = InputBindings.inspectorFromTypeMap(types);
- Expr.VectorInputBindingInspector vectorInputBindingInspector = new Expr.VectorInputBindingInspector()
- {
- @Override
- public int getMaxVectorSize()
- {
- return VECTOR_SIZE;
- }
-
- @Nullable
- @Override
- public ExpressionType getType(String name)
- {
- return inspector.getType(name);
- }
- };
- Assert.assertTrue(StringUtils.format("Cannot vectorize %s", expr), parsed.canVectorize(inspector));
- ExpressionType outputType = parsed.getOutputType(inspector);
- final ExprVectorProcessor processor = parsed.asVectorProcessor(vectorInputBindingInspector);
- // 'null' expressions can have an output type of null, but still evaluate in default mode, so skip type checks
- if (outputType != null) {
- Assert.assertEquals(expr, outputType, processor.getOutputType());
- }
for (int iterations = 0; iterations < numIterations; iterations++) {
- NonnullPair<Expr.ObjectBinding[], Expr.VectorInputBinding> bindings = makeRandomizedBindings(VECTOR_SIZE, types);
- ExprEvalVector<?> vectorEval = processor.evalVector(bindings.rhs);
- final Object[] vectorVals = vectorEval.getObjectVector();
- if (outputType != null) {
- Assert.assertEquals("vector eval type", outputType, vectorEval.getType());
- }
+ assertEvalsMatch(expr, parsed, makeRandomizedBindings(VECTOR_SIZE, types));
+ }
+ }
+
+ public static void assertEvalsMatch(
+ String exprString,
+ Expr expr,
+ NonnullPair<Expr.ObjectBinding[], Expr.VectorInputBinding> bindings
+ )
+ {
+ Assert.assertTrue(StringUtils.format("Cannot vectorize[%s]", expr), expr.canVectorize(bindings.rhs));
+
+ final ExpressionType outputType = expr.getOutputType(bindings.rhs);
+ final Either<String, Object[]> vectorEval = evalVector(expr, bindings.rhs, outputType);
+ final Either<String, Object[]> nonVectorEval = evalNonVector(expr, bindings.lhs, outputType);
+
+ Assert.assertEquals(
+ StringUtils.format("Errors do not match for expr[%s], bindings[%s]", exprString, bindings.lhs),
Review Comment:
## Implicit conversion from array to string
Implicit conversion from Array to String.
[Show more details](https://github.com/apache/druid/security/code-scanning/10384)
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Enable fallback vectorization by default. (druid)
Posted by "gianm (via GitHub)" <gi...@apache.org>.
gianm merged PR #18549:
URL: https://github.com/apache/druid/pull/18549
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org