Search in sources :

Example 1 with RegisteredLookupExtractionFn

use of org.apache.druid.query.lookup.RegisteredLookupExtractionFn in project druid by druid-io.

the class CalciteQueryTest method testFilterAndGroupByLookup.

@Test
public void testFilterAndGroupByLookup() throws Exception {
    // Cannot vectorize due to extraction dimension specs.
    cannotVectorize();
    final RegisteredLookupExtractionFn extractionFn = new RegisteredLookupExtractionFn(null, "lookyloo", false, null, null, true);
    testQuery("SELECT LOOKUP(dim1, 'lookyloo'), COUNT(*) FROM foo\n" + "WHERE LOOKUP(dim1, 'lookyloo') <> 'xxx'\n" + "GROUP BY LOOKUP(dim1, 'lookyloo')", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setDimFilter(not(selector("dim1", "xxx", extractionFn))).setDimensions(dimensions(new ExtractionDimensionSpec("dim1", "d0", ColumnType.STRING, extractionFn))).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { NULL_STRING, 5L }, new Object[] { "xabc", 1L }));
}
Also used : CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) RegisteredLookupExtractionFn(org.apache.druid.query.lookup.RegisteredLookupExtractionFn) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) Test(org.junit.Test)

Example 2 with RegisteredLookupExtractionFn

use of org.apache.druid.query.lookup.RegisteredLookupExtractionFn in project druid by druid-io.

the class LookupExprMacro method apply.

@Override
public Expr apply(final List<Expr> args) {
    if (args.size() != 2) {
        throw new IAE("Function[%s] must have 2 arguments", name());
    }
    final Expr arg = args.get(0);
    final Expr lookupExpr = args.get(1);
    if (!lookupExpr.isLiteral() || lookupExpr.getLiteralValue() == null) {
        throw new IAE("Function[%s] second argument must be a registered lookup name", name());
    }
    final String lookupName = lookupExpr.getLiteralValue().toString();
    final RegisteredLookupExtractionFn extractionFn = new RegisteredLookupExtractionFn(lookupExtractorFactoryContainerProvider, lookupName, false, null, false, null);
    class LookupExpr extends ExprMacroTable.BaseScalarUnivariateMacroFunctionExpr {

        private LookupExpr(Expr arg) {
            super(FN_NAME, arg);
        }

        @Nonnull
        @Override
        public ExprEval eval(final ObjectBinding bindings) {
            return ExprEval.of(extractionFn.apply(NullHandling.emptyToNullIfNeeded(arg.eval(bindings).asString())));
        }

        @Override
        public Expr visit(Shuttle shuttle) {
            return shuttle.visit(apply(shuttle.visitAll(args)));
        }

        @Nullable
        @Override
        public ExpressionType getOutputType(InputBindingInspector inspector) {
            return ExpressionType.STRING;
        }

        @Override
        public String stringify() {
            return StringUtils.format("%s(%s, %s)", FN_NAME, arg.stringify(), lookupExpr.stringify());
        }

        @Override
        public byte[] getCacheKey() {
            return new CacheKeyBuilder(Exprs.LOOKUP_EXPR_CACHE_KEY).appendString(stringify()).appendCacheable(extractionFn).build();
        }
    }
    return new LookupExpr(arg);
}
Also used : Expr(org.apache.druid.math.expr.Expr) RegisteredLookupExtractionFn(org.apache.druid.query.lookup.RegisteredLookupExtractionFn) CacheKeyBuilder(org.apache.druid.query.cache.CacheKeyBuilder) IAE(org.apache.druid.java.util.common.IAE)

Example 3 with RegisteredLookupExtractionFn

use of org.apache.druid.query.lookup.RegisteredLookupExtractionFn in project druid by druid-io.

the class CalciteQueryTest method testCountDistinctOfLookup.

@Test
public void testCountDistinctOfLookup() throws Exception {
    // Cannot vectorize due to extraction dimension spec.
    cannotVectorize();
    final RegisteredLookupExtractionFn extractionFn = new RegisteredLookupExtractionFn(null, "lookyloo", false, null, null, true);
    testQuery("SELECT COUNT(DISTINCT LOOKUP(dim1, 'lookyloo')) FROM foo", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).aggregators(aggregators(new CardinalityAggregatorFactory("a0", null, ImmutableList.of(new ExtractionDimensionSpec("dim1", null, extractionFn)), false, true))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { NullHandling.replaceWithDefault() ? 2L : 1L }));
}
Also used : RegisteredLookupExtractionFn(org.apache.druid.query.lookup.RegisteredLookupExtractionFn) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) Test(org.junit.Test)

Example 4 with RegisteredLookupExtractionFn

use of org.apache.druid.query.lookup.RegisteredLookupExtractionFn in project druid by druid-io.

the class QueryLookupOperatorConversion method toDruidExpression.

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode) {
    return OperatorConversions.convertDirectCallWithExtraction(plannerContext, rowSignature, rexNode, StringUtils.toLowerCase(calciteOperator().getName()), inputExpressions -> {
        final DruidExpression arg = inputExpressions.get(0);
        final Expr lookupNameExpr = inputExpressions.get(1).parse(plannerContext.getExprMacroTable());
        if (arg.isSimpleExtraction() && lookupNameExpr.isLiteral()) {
            return arg.getSimpleExtraction().cascade(new RegisteredLookupExtractionFn(lookupExtractorFactoryContainerProvider, (String) lookupNameExpr.getLiteralValue(), false, null, null, true));
        } else {
            return null;
        }
    });
}
Also used : DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) Expr(org.apache.druid.math.expr.Expr) RegisteredLookupExtractionFn(org.apache.druid.query.lookup.RegisteredLookupExtractionFn)

Aggregations

RegisteredLookupExtractionFn (org.apache.druid.query.lookup.RegisteredLookupExtractionFn)4 Expr (org.apache.druid.math.expr.Expr)2 ExtractionDimensionSpec (org.apache.druid.query.dimension.ExtractionDimensionSpec)2 Test (org.junit.Test)2 IAE (org.apache.druid.java.util.common.IAE)1 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)1 CardinalityAggregatorFactory (org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory)1 CacheKeyBuilder (org.apache.druid.query.cache.CacheKeyBuilder)1 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)1