Search in sources :

Example 31 with Expr

use of org.apache.druid.math.expr.Expr in project druid by druid-io.

the class IPv4AddressParseExprMacroTest method testNullLongArg.

@Test
public void testNullLongArg() {
    Expr nullLong = ExprEval.ofLong(null).toExpr();
    Assert.assertEquals(NULL, eval(nullLong));
}
Also used : Expr(org.apache.druid.math.expr.Expr) Test(org.junit.Test)

Example 32 with Expr

use of org.apache.druid.math.expr.Expr in project druid by druid-io.

the class IPv4AddressParseExprMacroTest method eval.

private Object eval(Expr arg) {
    Expr expr = apply(Collections.singletonList(arg));
    ExprEval eval = expr.eval(InputBindings.nilBindings());
    return eval.value();
}
Also used : ExprEval(org.apache.druid.math.expr.ExprEval) Expr(org.apache.druid.math.expr.Expr)

Example 33 with Expr

use of org.apache.druid.math.expr.Expr in project druid by druid-io.

the class ExpressionVectorSelectorsTest method setup.

@Before
public void setup() {
    Expr parsed = Parser.parse(expression, ExprMacroTable.nil());
    outputType = parsed.getOutputType(new ColumnInspector() {

        @Nullable
        @Override
        public ColumnCapabilities getColumnCapabilities(String column) {
            return QueryableIndexStorageAdapter.getColumnCapabilities(INDEX, column);
        }
    });
    if (outputType == null) {
        outputType = ExpressionType.STRING;
    }
}
Also used : Expr(org.apache.druid.math.expr.Expr) ColumnInspector(org.apache.druid.segment.ColumnInspector) Before(org.junit.Before)

Example 34 with Expr

use of org.apache.druid.math.expr.Expr in project druid by druid-io.

the class Expressions method toQueryGranularity.

/**
 * Converts an expression to a Granularity, if possible. This is possible if, and only if, the expression
 * is a timestamp_floor function on the __time column with literal parameters for period, origin, and timeZone.
 *
 * @return granularity or null if not possible
 */
@Nullable
public static Granularity toQueryGranularity(final DruidExpression expression, final ExprMacroTable macroTable) {
    final TimestampFloorExprMacro.TimestampFloorExpr expr = asTimestampFloorExpr(expression, macroTable);
    if (expr == null) {
        return null;
    }
    final Expr arg = expr.getArg();
    final Granularity granularity = expr.getGranularity();
    if (ColumnHolder.TIME_COLUMN_NAME.equals(arg.getBindingIfIdentifier())) {
        return granularity;
    } else {
        return null;
    }
}
Also used : TimestampFloorExprMacro(org.apache.druid.query.expression.TimestampFloorExprMacro) Expr(org.apache.druid.math.expr.Expr) Granularity(org.apache.druid.java.util.common.granularity.Granularity) Nullable(javax.annotation.Nullable)

Example 35 with Expr

use of org.apache.druid.math.expr.Expr in project druid by druid-io.

the class ExpressionFilter method makeVectorMatcher.

@Override
public VectorValueMatcher makeVectorMatcher(VectorColumnSelectorFactory factory) {
    final Expr theExpr = expr.get();
    DruidPredicateFactory predicateFactory = new DruidPredicateFactory() {

        @Override
        public Predicate<String> makeStringPredicate() {
            return Evals::asBoolean;
        }

        @Override
        public DruidLongPredicate makeLongPredicate() {
            return Evals::asBoolean;
        }

        @Override
        public DruidFloatPredicate makeFloatPredicate() {
            return Evals::asBoolean;
        }

        @Override
        public DruidDoublePredicate makeDoublePredicate() {
            return Evals::asBoolean;
        }

        // The hashcode and equals are to make SubclassesMustOverrideEqualsAndHashCodeTest stop complaining..
        // DruidPredicateFactory currently doesn't really need equals or hashcode since 'toString' method that is actually
        // called when testing equality of DimensionPredicateFilter, so it's the truly required method, but that seems
        // a bit strange. DimensionPredicateFilter should probably be reworked to use equals from DruidPredicateFactory
        // instead of using toString.
        @Override
        public int hashCode() {
            return super.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            return super.equals(obj);
        }
    };
    final ExpressionType outputType = theExpr.getOutputType(factory);
    // effectively constant
    if (outputType == null) {
        // false matcher
        if (NullHandling.sqlCompatible()) {
            return BooleanVectorValueMatcher.of(factory.getReadableVectorInspector(), false);
        }
        // or not.
        return BooleanVectorValueMatcher.of(factory.getReadableVectorInspector(), theExpr.eval(InputBindings.nilBindings()).asBoolean());
    }
    // if we got here, we really have to evaluate the expressions to match
    switch(outputType.getType()) {
        case LONG:
            return VectorValueMatcherColumnProcessorFactory.instance().makeLongProcessor(ColumnCapabilitiesImpl.createSimpleNumericColumnCapabilities(ColumnType.LONG), ExpressionVectorSelectors.makeVectorValueSelector(factory, theExpr)).makeMatcher(predicateFactory);
        case DOUBLE:
            return VectorValueMatcherColumnProcessorFactory.instance().makeDoubleProcessor(ColumnCapabilitiesImpl.createSimpleNumericColumnCapabilities(ColumnType.DOUBLE), ExpressionVectorSelectors.makeVectorValueSelector(factory, theExpr)).makeMatcher(predicateFactory);
        case STRING:
            return VectorValueMatcherColumnProcessorFactory.instance().makeObjectProcessor(ColumnCapabilitiesImpl.createSimpleSingleValueStringColumnCapabilities(), ExpressionVectorSelectors.makeVectorObjectSelector(factory, theExpr)).makeMatcher(predicateFactory);
        default:
            throw new UOE("Vectorized expression matchers not implemented for type: [%s]", outputType);
    }
}
Also used : Expr(org.apache.druid.math.expr.Expr) UOE(org.apache.druid.java.util.common.UOE) ExpressionType(org.apache.druid.math.expr.ExpressionType) DruidPredicateFactory(org.apache.druid.query.filter.DruidPredicateFactory)

Aggregations

Expr (org.apache.druid.math.expr.Expr)104 Test (org.junit.Test)58 ExprEval (org.apache.druid.math.expr.ExprEval)18 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)17 IAE (org.apache.druid.java.util.common.IAE)14 ExpressionType (org.apache.druid.math.expr.ExpressionType)8 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)7 ArrayList (java.util.ArrayList)6 Nullable (javax.annotation.Nullable)6 HashSet (java.util.HashSet)5 List (java.util.List)4 HyperLogLogCollector (org.apache.druid.hll.HyperLogLogCollector)4 BloomKFilter (org.apache.druid.query.filter.BloomKFilter)4 InDimFilter (org.apache.druid.query.filter.InDimFilter)4 RexNode (org.apache.calcite.rex.RexNode)3 Filter (org.apache.druid.query.filter.Filter)3 VirtualColumn (org.apache.druid.segment.VirtualColumn)3 FalseFilter (org.apache.druid.segment.filter.FalseFilter)3 OrFilter (org.apache.druid.segment.filter.OrFilter)3 SelectorFilter (org.apache.druid.segment.filter.SelectorFilter)3