somu-imply commented on a change in pull request #12254: URL: https://github.com/apache/druid/pull/12254#discussion_r804077429
########## File path: processing/src/test/java/org/apache/druid/query/expression/VectorExpressionsSanityTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.query.expression; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.apache.druid.common.config.NullHandling; +import org.apache.druid.java.util.common.DateTimes; +import org.apache.druid.java.util.common.NonnullPair; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.math.expr.Expr; +import org.apache.druid.math.expr.ExprEval; +import org.apache.druid.math.expr.ExpressionProcessing; +import org.apache.druid.math.expr.ExpressionType; +import org.apache.druid.math.expr.SettableObjectBinding; +import org.apache.druid.math.expr.vector.ExprEvalVector; +import org.apache.druid.testing.InitializedNullHandlingTest; +import org.joda.time.DateTime; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; +import java.util.function.BooleanSupplier; +import java.util.function.DoubleSupplier; +import java.util.function.LongSupplier; +import java.util.function.Supplier; + +/** + * randomize inputs to various vector expressions and make sure the results match nonvectorized expressions + * <p> + * this is not a replacement for correctness tests, but will ensure that vectorized and non-vectorized expression + * evaluation is at least self consistent... + */ +public class VectorExpressionsSanityTest extends InitializedNullHandlingTest +{ + private static final Logger log = new Logger(VectorExpressionsSanityTest.class); + private static final int NUM_ITERATIONS = 10; + private static final int VECTOR_SIZE = 512; + private static final TimestampShiftExprMacro TIMESTAMP_SHIFT_EXPR_MACRO = new TimestampShiftExprMacro(); + private static final DateTime DATE_TIME = DateTimes.of("2020-11-05T04:05:06"); + + final Map<String, ExpressionType> types = ImmutableMap.<String, ExpressionType>builder() + .put("l1", ExpressionType.LONG) + .put("l2", ExpressionType.LONG) + .put("d1", ExpressionType.DOUBLE) + .put("d2", ExpressionType.DOUBLE) + .put("s1", ExpressionType.STRING) + .put("s2", ExpressionType.STRING) + .put("boolString1", ExpressionType.STRING) + .put("boolString2", ExpressionType.STRING) + .build(); + + @BeforeClass + public static void setupTests() + { + ExpressionProcessing.initializeForStrictBooleansTests(true); + } + + @AfterClass + public static void teardownTests() + { + ExpressionProcessing.initializeForTests(null); + } + + static void testExpression(String expr, Expr parsed, Map<String, ExpressionType> types) + { + log.debug("[%s]", expr); + NonnullPair<Expr.ObjectBinding[], Expr.VectorInputBinding> bindings; + for (int iterations = 0; iterations < NUM_ITERATIONS; iterations++) { + bindings = makeRandomizedBindings(VECTOR_SIZE, types); + testExpressionWithBindings(expr, parsed, bindings); + } + bindings = makeSequentialBinding(VECTOR_SIZE, types); + testExpressionWithBindings(expr, parsed, bindings); + } + + private static void testExpressionWithBindings( + String expr, + Expr parsed, + NonnullPair<Expr.ObjectBinding[], Expr.VectorInputBinding> bindings + ) + { + Assert.assertTrue(StringUtils.format("Cannot vectorize %s", expr), parsed.canVectorize(bindings.rhs)); + ExpressionType outputType = parsed.getOutputType(bindings.rhs); + ExprEvalVector<?> vectorEval = parsed.buildVectorized(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(outputType, vectorEval.getType()); + } + 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); + } + Assert.assertEquals( + StringUtils.format("Values do not match for row %s for expression %s", i, expr), + eval.value(), + vectorEval.get(i) + ); + } + } + + static NonnullPair<Expr.ObjectBinding[], Expr.VectorInputBinding> makeRandomizedBindings( + int vectorSize, + Map<String, ExpressionType> types + ) + { + + final ThreadLocalRandom r = ThreadLocalRandom.current(); + return makeBindings( + vectorSize, + types, + () -> r.nextLong(Integer.MAX_VALUE - 1), + r::nextDouble, + r::nextBoolean, + () -> String.valueOf(r.nextInt()) + ); + } + + static NonnullPair<Expr.ObjectBinding[], Expr.VectorInputBinding> makeSequentialBinding( + int vectorSize, + Map<String, ExpressionType> types + ) + { + + return makeBindings( + vectorSize, + types, + new LongSupplier() + { + int counter = 1; + + @Override + public long getAsLong() + { + return counter++; + } + }, + new DoubleSupplier() + { + int counter = 1; + + @Override + public double getAsDouble() + { + return counter++; + } + }, + () -> ThreadLocalRandom.current().nextBoolean(), + new Supplier<String>() + { + int counter = 1; + + @Override + public String get() + { + return String.valueOf(counter++); + } + } + ); + } + + static NonnullPair<Expr.ObjectBinding[], Expr.VectorInputBinding> makeBindings( Review comment: re-used, line coverage bumped up a bit :) -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
