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));
}
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")));
}
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)));
}
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)));
}
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);
}
Aggregations