Search in sources :

Example 1 with SqlParameter

use of org.apache.druid.sql.http.SqlParameter in project druid by druid-io.

the class SqlQueryTest method testSerde.

@Test
public void testSerde() throws Exception {
    final ObjectMapper jsonMapper = TestHelper.makeJsonMapper();
    final SqlQuery query = new SqlQuery("SELECT ?", ResultFormat.ARRAY, true, true, true, ImmutableMap.of("useCache", false), ImmutableList.of(new SqlParameter(SqlType.INTEGER, 1)));
    Assert.assertEquals(query, jsonMapper.readValue(jsonMapper.writeValueAsString(query), SqlQuery.class));
}
Also used : SqlQuery(org.apache.druid.sql.http.SqlQuery) SqlParameter(org.apache.druid.sql.http.SqlParameter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with SqlParameter

use of org.apache.druid.sql.http.SqlParameter in project druid by druid-io.

the class CalciteParameterQueryTest method testNullParameter.

@Test
public void testNullParameter() throws Exception {
    cannotVectorize();
    // contrived example of using null as an sql parameter to at least test the codepath because lots of things dont
    // actually work as null and things like 'IS NULL' fail to parse in calcite if expressed as 'IS ?'
    // this will optimize out the 3rd argument because 2nd argument will be constant and not null
    testQuery("SELECT COALESCE(dim2, ?, ?), COUNT(*) FROM druid.foo GROUP BY 1\n", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "case_searched(notnull(\"dim2\"),\"dim2\",'parameter')", ColumnType.STRING)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0", ColumnType.STRING))).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setContext(QUERY_CONTEXT_DEFAULT).build()), NullHandling.replaceWithDefault() ? ImmutableList.of(new Object[] { "a", 2L }, new Object[] { "abc", 1L }, new Object[] { "parameter", 3L }) : ImmutableList.of(new Object[] { "", 1L }, new Object[] { "a", 2L }, new Object[] { "abc", 1L }, new Object[] { "parameter", 2L }), ImmutableList.of(new SqlParameter(SqlType.VARCHAR, "parameter"), new SqlParameter(SqlType.VARCHAR, null)));
    // when converting to rel expression, this will optimize out 2nd argument to coalesce which is null
    testQuery("SELECT COALESCE(dim2, ?, ?), COUNT(*) FROM druid.foo GROUP BY 1\n", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "case_searched(notnull(\"dim2\"),\"dim2\",'parameter')", ColumnType.STRING)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0", ColumnType.STRING))).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setContext(QUERY_CONTEXT_DEFAULT).build()), NullHandling.replaceWithDefault() ? ImmutableList.of(new Object[] { "a", 2L }, new Object[] { "abc", 1L }, new Object[] { "parameter", 3L }) : ImmutableList.of(new Object[] { "", 1L }, new Object[] { "a", 2L }, new Object[] { "abc", 1L }, new Object[] { "parameter", 2L }), ImmutableList.of(new SqlParameter(SqlType.VARCHAR, null), new SqlParameter(SqlType.VARCHAR, "parameter")));
}
Also used : SqlParameter(org.apache.druid.sql.http.SqlParameter) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 3 with SqlParameter

use of org.apache.druid.sql.http.SqlParameter in project druid by druid-io.

the class BloomDimFilterSqlTest method testBloomFilterNullParameter.

@Test
public void testBloomFilterNullParameter() throws Exception {
    BloomKFilter filter = new BloomKFilter(1500);
    filter.addBytes(null, 0, 0);
    byte[] bytes = BloomFilterSerializersModule.bloomKFilterToBytes(filter);
    String base64 = StringUtils.encodeBase64String(bytes);
    // bloom filter expression is evaluated and optimized out at planning time since parameter is null and null matches
    // the supplied filter of the other parameter
    testQuery("SELECT COUNT(*) FROM druid.foo WHERE bloom_filter_test(?, ?)", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).aggregators(aggregators(new CountAggregatorFactory("a0"))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 6L }), // there are no empty strings in the druid expression language since empty is coerced into a null when parsed
    ImmutableList.of(new SqlParameter(SqlType.VARCHAR, NullHandling.defaultStringValue()), new SqlParameter(SqlType.VARCHAR, base64)));
}
Also used : SqlParameter(org.apache.druid.sql.http.SqlParameter) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) BloomKFilter(org.apache.druid.query.filter.BloomKFilter) BaseCalciteQueryTest(org.apache.druid.sql.calcite.BaseCalciteQueryTest) Test(org.junit.Test)

Example 4 with SqlParameter

use of org.apache.druid.sql.http.SqlParameter in project druid by druid-io.

the class BloomDimFilterSqlTest method testBloomFilterBigParameter.

@Ignore("this test is for comparison with testBloomFilterBigNoParam")
@Test
public void testBloomFilterBigParameter() throws Exception {
    BloomKFilter filter = new BloomKFilter(5_000_000);
    filter.addString("def");
    byte[] bytes = BloomFilterSerializersModule.bloomKFilterToBytes(filter);
    String base64 = StringUtils.encodeBase64String(bytes);
    testQuery("SELECT COUNT(*) FROM druid.foo WHERE bloom_filter_test(dim1, ?)", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).filters(new BloomDimFilter("dim1", BloomKFilterHolder.fromBloomKFilter(filter), null)).aggregators(aggregators(new CountAggregatorFactory("a0"))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 1L }), ImmutableList.of(new SqlParameter(SqlType.VARCHAR, base64)));
}
Also used : SqlParameter(org.apache.druid.sql.http.SqlParameter) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) BloomDimFilter(org.apache.druid.query.filter.BloomDimFilter) BloomKFilter(org.apache.druid.query.filter.BloomKFilter) Ignore(org.junit.Ignore) BaseCalciteQueryTest(org.apache.druid.sql.calcite.BaseCalciteQueryTest) Test(org.junit.Test)

Example 5 with SqlParameter

use of org.apache.druid.sql.http.SqlParameter in project druid by druid-io.

the class CalciteParameterQueryTest method testPartiallyMissingParameterInTheMiddle.

@Test
public void testPartiallyMissingParameterInTheMiddle() throws Exception {
    List<SqlParameter> params = new ArrayList<>();
    params.add(null);
    params.add(new SqlParameter(SqlType.INTEGER, 1));
    expectedException.expect(SqlPlanningException.class);
    expectedException.expectMessage("Parameter at position[0] is not bound");
    testQuery("SELECT 1 + ?, dim1 FROM foo LIMIT ?", ImmutableList.of(), ImmutableList.of(), params);
}
Also used : SqlParameter(org.apache.druid.sql.http.SqlParameter) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

SqlParameter (org.apache.druid.sql.http.SqlParameter)7 Test (org.junit.Test)7 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 TypedValue (org.apache.calcite.avatica.remote.TypedValue)2 ServiceEventBuilder (org.apache.druid.java.util.emitter.service.ServiceEventBuilder)2 BloomKFilter (org.apache.druid.query.filter.BloomKFilter)2 BaseCalciteQueryTest (org.apache.druid.sql.calcite.BaseCalciteQueryTest)2 DruidPlanner (org.apache.druid.sql.calcite.planner.DruidPlanner)2 PlannerContext (org.apache.druid.sql.calcite.planner.PlannerContext)2 PlannerResult (org.apache.druid.sql.calcite.planner.PlannerResult)2 PrepareResult (org.apache.druid.sql.calcite.planner.PrepareResult)2 ValidationResult (org.apache.druid.sql.calcite.planner.ValidationResult)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)1 BloomDimFilter (org.apache.druid.query.filter.BloomDimFilter)1 SqlQuery (org.apache.druid.sql.http.SqlQuery)1 Ignore (org.junit.Ignore)1