Search in sources :

Example 1 with ExprMacroTable

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

the class CalciteQueryTest method testExpressionAggregations.

@Test
public void testExpressionAggregations() throws Exception {
    // Cannot vectorize due to expressions.
    cannotVectorize();
    final ExprMacroTable macroTable = CalciteTests.createExprMacroTable();
    testQuery("SELECT\n" + "  SUM(cnt * 3),\n" + "  LN(SUM(cnt) + SUM(m1)),\n" + "  MOD(SUM(cnt), 4),\n" + "  SUM(CHARACTER_LENGTH(CAST(cnt * 10 AS VARCHAR))),\n" + "  MAX(CHARACTER_LENGTH(dim2) + LN(m1))\n" + "FROM druid.foo", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).aggregators(aggregators(new LongSumAggregatorFactory("a0", null, "(\"cnt\" * 3)", macroTable), new LongSumAggregatorFactory("a1", "cnt"), new DoubleSumAggregatorFactory("a2", "m1"), new LongSumAggregatorFactory("a3", null, "strlen(CAST((\"cnt\" * 10), 'STRING'))", macroTable), new DoubleMaxAggregatorFactory("a4", null, "(strlen(\"dim2\") + log(\"m1\"))", macroTable))).postAggregators(expressionPostAgg("p0", "log((\"a1\" + \"a2\"))"), expressionPostAgg("p1", "(\"a1\" % 4)")).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 18L, 3.295836866004329, 2, 12L, 3f + (Math.log(5.0)) }));
}
Also used : DoubleMaxAggregatorFactory(org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) TestExprMacroTable(org.apache.druid.query.expression.TestExprMacroTable) ExprMacroTable(org.apache.druid.math.expr.ExprMacroTable) Test(org.junit.Test)

Example 2 with ExprMacroTable

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

the class LookupExprMacroTest method testCacheKeyChangesWhenLookupChanges.

@Test
public void testCacheKeyChangesWhenLookupChanges() {
    final String expression = "lookup(x, 'lookyloo')";
    final Expr expr = Parser.parse(expression, LookupEnabledTestExprMacroTable.INSTANCE);
    final Expr exprSameLookup = Parser.parse(expression, LookupEnabledTestExprMacroTable.INSTANCE);
    final Expr exprChangedLookup = Parser.parse(expression, new ExprMacroTable(LookupEnabledTestExprMacroTable.makeTestMacros(ImmutableMap.of("x", "y", "a", "b"))));
    // same should have same cache key
    Assert.assertArrayEquals(expr.getCacheKey(), exprSameLookup.getCacheKey());
    // different should not have same key
    final byte[] exprBytes = expr.getCacheKey();
    final byte[] expr2Bytes = exprChangedLookup.getCacheKey();
    if (exprBytes.length == expr2Bytes.length) {
        // only check for equality if lengths are equal
        boolean allEqual = true;
        for (int i = 0; i < exprBytes.length; i++) {
            allEqual = allEqual && (exprBytes[i] == expr2Bytes[i]);
        }
        Assert.assertFalse(allEqual);
    }
}
Also used : Expr(org.apache.druid.math.expr.Expr) ExprMacroTable(org.apache.druid.math.expr.ExprMacroTable) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 3 with ExprMacroTable

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

the class DruidAvaticaHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    walker = CalciteTests.createMockWalker(conglomerate, temporaryFolder.newFolder());
    final PlannerConfig plannerConfig = new PlannerConfig();
    final DruidOperatorTable operatorTable = CalciteTests.createOperatorTable();
    final ExprMacroTable macroTable = CalciteTests.createExprMacroTable();
    final DruidSchemaCatalog rootSchema = CalciteTests.createMockRootSchema(conglomerate, walker, plannerConfig, CalciteTests.TEST_AUTHORIZER_MAPPER);
    testRequestLogger = new TestRequestLogger();
    injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(new Module() {

        @Override
        public void configure(Binder binder) {
            binder.bindConstant().annotatedWith(Names.named("serviceName")).to("test");
            binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
            binder.bindConstant().annotatedWith(Names.named("tlsServicePort")).to(-1);
            binder.bind(AuthenticatorMapper.class).toInstance(CalciteTests.TEST_AUTHENTICATOR_MAPPER);
            binder.bind(AuthorizerMapper.class).toInstance(CalciteTests.TEST_AUTHORIZER_MAPPER);
            binder.bind(Escalator.class).toInstance(CalciteTests.TEST_AUTHENTICATOR_ESCALATOR);
            binder.bind(RequestLogger.class).toInstance(testRequestLogger);
            binder.bind(DruidSchemaCatalog.class).toInstance(rootSchema);
            for (NamedSchema schema : rootSchema.getNamedSchemas().values()) {
                Multibinder.newSetBinder(binder, NamedSchema.class).addBinding().toInstance(schema);
            }
            binder.bind(QueryLifecycleFactory.class).toInstance(CalciteTests.createMockQueryLifecycleFactory(walker, conglomerate));
            binder.bind(DruidOperatorTable.class).toInstance(operatorTable);
            binder.bind(ExprMacroTable.class).toInstance(macroTable);
            binder.bind(PlannerConfig.class).toInstance(plannerConfig);
            binder.bind(String.class).annotatedWith(DruidSchemaName.class).toInstance(CalciteTests.DRUID_SCHEMA_NAME);
            binder.bind(AvaticaServerConfig.class).toInstance(AVATICA_CONFIG);
            binder.bind(ServiceEmitter.class).to(NoopServiceEmitter.class);
            binder.bind(QuerySchedulerProvider.class).in(LazySingleton.class);
            binder.bind(QueryScheduler.class).toProvider(QuerySchedulerProvider.class).in(LazySingleton.class);
            binder.bind(QueryMakerFactory.class).to(NativeQueryMakerFactory.class);
        }
    }));
    druidMeta = injector.getInstance(DruidMeta.class);
    final AbstractAvaticaHandler handler = this.getAvaticaHandler(druidMeta);
    final int port = ThreadLocalRandom.current().nextInt(9999) + 10000;
    server = new Server(new InetSocketAddress("127.0.0.1", port));
    server.setHandler(handler);
    server.start();
    url = this.getJdbcConnectionString(port);
    client = DriverManager.getConnection(url, "regularUser", "druid");
    superuserClient = DriverManager.getConnection(url, CalciteTests.TEST_SUPERUSER_NAME, "druid");
    clientNoTrailingSlash = DriverManager.getConnection(StringUtils.maybeRemoveTrailingSlash(url), CalciteTests.TEST_SUPERUSER_NAME, "druid");
    final Properties propertiesLosAngeles = new Properties();
    propertiesLosAngeles.setProperty("sqlTimeZone", "America/Los_Angeles");
    propertiesLosAngeles.setProperty("user", "regularUserLA");
    propertiesLosAngeles.setProperty(BaseQuery.SQL_QUERY_ID, DUMMY_SQL_QUERY_ID);
    clientLosAngeles = DriverManager.getConnection(url, propertiesLosAngeles);
}
Also used : QueryScheduler(org.apache.druid.server.QueryScheduler) Server(org.eclipse.jetty.server.Server) InetSocketAddress(java.net.InetSocketAddress) AbstractAvaticaHandler(org.apache.calcite.avatica.server.AbstractAvaticaHandler) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) NamedSchema(org.apache.druid.sql.calcite.schema.NamedSchema) Properties(java.util.Properties) DruidOperatorTable(org.apache.druid.sql.calcite.planner.DruidOperatorTable) ExprMacroTable(org.apache.druid.math.expr.ExprMacroTable) TestRequestLogger(org.apache.druid.server.log.TestRequestLogger) Binder(com.google.inject.Binder) LazySingleton(org.apache.druid.guice.LazySingleton) PlannerConfig(org.apache.druid.sql.calcite.planner.PlannerConfig) DruidSchemaCatalog(org.apache.druid.sql.calcite.schema.DruidSchemaCatalog) NativeQueryMakerFactory(org.apache.druid.sql.calcite.run.NativeQueryMakerFactory) Module(com.google.inject.Module) Before(org.junit.Before)

Example 4 with ExprMacroTable

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

the class SqlResourceTest method setUp.

@Before
public void setUp() throws Exception {
    final QueryScheduler scheduler = new QueryScheduler(5, ManualQueryPrioritizationStrategy.INSTANCE, new HiLoQueryLaningStrategy(40), new ServerConfig()) {

        @Override
        public <T> Sequence<T> run(Query<?> query, Sequence<T> resultSequence) {
            return super.run(query, new LazySequence<T>(() -> {
                if (sleep) {
                    try {
                        // pretend to be a query that is waiting on results
                        Thread.sleep(500);
                    } catch (InterruptedException ignored) {
                    }
                }
                return resultSequence;
            }));
        }
    };
    executorService = MoreExecutors.listeningDecorator(Execs.multiThreaded(8, "test_sql_resource_%s"));
    walker = CalciteTests.createMockWalker(conglomerate, temporaryFolder.newFolder(), scheduler);
    final PlannerConfig plannerConfig = new PlannerConfig() {

        @Override
        public boolean shouldSerializeComplexValues() {
            return false;
        }
    };
    final DruidSchemaCatalog rootSchema = CalciteTests.createMockRootSchema(conglomerate, walker, plannerConfig, CalciteTests.TEST_AUTHORIZER_MAPPER);
    final DruidOperatorTable operatorTable = CalciteTests.createOperatorTable();
    final ExprMacroTable macroTable = CalciteTests.createExprMacroTable();
    req = EasyMock.createStrictMock(HttpServletRequest.class);
    EasyMock.expect(req.getRemoteAddr()).andReturn(null).once();
    EasyMock.expect(req.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)).andReturn(CalciteTests.REGULAR_USER_AUTH_RESULT).anyTimes();
    EasyMock.expect(req.getAttribute(AuthConfig.DRUID_ALLOW_UNSECURED_PATH)).andReturn(null).anyTimes();
    EasyMock.expect(req.getAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED)).andReturn(null).anyTimes();
    EasyMock.expect(req.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)).andReturn(CalciteTests.REGULAR_USER_AUTH_RESULT).anyTimes();
    req.setAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED, true);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.expect(req.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)).andReturn(CalciteTests.REGULAR_USER_AUTH_RESULT).anyTimes();
    EasyMock.replay(req);
    testRequestLogger = new TestRequestLogger();
    final PlannerFactory plannerFactory = new PlannerFactory(rootSchema, CalciteTests.createMockQueryMakerFactory(walker, conglomerate), operatorTable, macroTable, plannerConfig, CalciteTests.TEST_AUTHORIZER_MAPPER, CalciteTests.getJsonMapper(), CalciteTests.DRUID_SCHEMA_NAME);
    lifecycleManager = new SqlLifecycleManager() {

        @Override
        public void add(String sqlQueryId, SqlLifecycle lifecycle) {
            super.add(sqlQueryId, lifecycle);
            if (lifecycleAddLatch != null) {
                lifecycleAddLatch.countDown();
            }
        }
    };
    final ServiceEmitter emitter = new NoopServiceEmitter();
    sqlLifecycleFactory = new SqlLifecycleFactory(plannerFactory, emitter, testRequestLogger, scheduler) {

        @Override
        public SqlLifecycle factorize() {
            return new TestSqlLifecycle(plannerFactory, emitter, testRequestLogger, scheduler, System.currentTimeMillis(), System.nanoTime(), validateAndAuthorizeLatchSupplier, planLatchSupplier, executeLatchSupplier, sequenceMapFnSupplier);
        }
    };
    resource = new SqlResource(JSON_MAPPER, CalciteTests.TEST_AUTHORIZER_MAPPER, sqlLifecycleFactory, lifecycleManager, new ServerConfig());
}
Also used : SqlLifecycleManager(org.apache.druid.sql.SqlLifecycleManager) ServiceEmitter(org.apache.druid.java.util.emitter.service.ServiceEmitter) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) QueryScheduler(org.apache.druid.server.QueryScheduler) BaseQuery(org.apache.druid.query.BaseQuery) Query(org.apache.druid.query.Query) HiLoQueryLaningStrategy(org.apache.druid.server.scheduling.HiLoQueryLaningStrategy) SqlLifecycle(org.apache.druid.sql.SqlLifecycle) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) Sequence(org.apache.druid.java.util.common.guava.Sequence) LazySequence(org.apache.druid.java.util.common.guava.LazySequence) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) DruidOperatorTable(org.apache.druid.sql.calcite.planner.DruidOperatorTable) ExprMacroTable(org.apache.druid.math.expr.ExprMacroTable) TestRequestLogger(org.apache.druid.server.log.TestRequestLogger) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerConfig(org.apache.druid.server.initialization.ServerConfig) PlannerConfig(org.apache.druid.sql.calcite.planner.PlannerConfig) DruidSchemaCatalog(org.apache.druid.sql.calcite.schema.DruidSchemaCatalog) PlannerFactory(org.apache.druid.sql.calcite.planner.PlannerFactory) SqlLifecycleFactory(org.apache.druid.sql.SqlLifecycleFactory) Before(org.junit.Before)

Example 5 with ExprMacroTable

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

the class SimpleSqlAggregator method toDruidAggregation.

@Nullable
@Override
public Aggregation toDruidAggregation(final PlannerContext plannerContext, final RowSignature rowSignature, final VirtualColumnRegistry virtualColumnRegistry, final RexBuilder rexBuilder, final String name, final AggregateCall aggregateCall, final Project project, final List<Aggregation> existingAggregations, final boolean finalizeAggregations) {
    if (aggregateCall.isDistinct()) {
        return null;
    }
    final List<DruidExpression> arguments = Aggregations.getArgumentsForSimpleAggregator(plannerContext, rowSignature, aggregateCall, project);
    if (arguments == null) {
        return null;
    }
    final DruidExpression arg = Iterables.getOnlyElement(arguments);
    final ExprMacroTable macroTable = plannerContext.getExprMacroTable();
    final String fieldName;
    final String expression;
    if (arg.isDirectColumnAccess()) {
        fieldName = arg.getDirectColumn();
        expression = null;
    } else {
        fieldName = null;
        expression = arg.getExpression();
    }
    return getAggregation(name, aggregateCall, macroTable, fieldName, expression);
}
Also used : DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) ExprMacroTable(org.apache.druid.math.expr.ExprMacroTable) Nullable(javax.annotation.Nullable)

Aggregations

ExprMacroTable (org.apache.druid.math.expr.ExprMacroTable)16 Nullable (javax.annotation.Nullable)7 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)7 ImmutableSet (com.google.common.collect.ImmutableSet)5 DruidOperatorTable (org.apache.druid.sql.calcite.planner.DruidOperatorTable)5 PlannerConfig (org.apache.druid.sql.calcite.planner.PlannerConfig)5 DruidSchemaCatalog (org.apache.druid.sql.calcite.schema.DruidSchemaCatalog)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 AggregateCall (org.apache.calcite.rel.core.AggregateCall)4 Project (org.apache.calcite.rel.core.Project)4 RexBuilder (org.apache.calcite.rex.RexBuilder)4 RexNode (org.apache.calcite.rex.RexNode)4 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)4 SqlFunctionCategory (org.apache.calcite.sql.SqlFunctionCategory)4 SqlKind (org.apache.calcite.sql.SqlKind)4 InferTypes (org.apache.calcite.sql.type.InferTypes)4 OperandTypes (org.apache.calcite.sql.type.OperandTypes)4 Optionality (org.apache.calcite.util.Optionality)4