use of org.apache.druid.query.dimension.DefaultDimensionSpec in project druid by druid-io.
the class BaseFilterTest method selectColumnValuesMatchingFilter.
/**
* Selects elements from "selectColumn" from rows matching a filter. selectColumn must be a single valued dimension.
*/
private List<String> selectColumnValuesMatchingFilter(final DimFilter filter, final String selectColumn) {
final Sequence<Cursor> cursors = makeCursorSequence(makeFilter(filter));
Sequence<List<String>> seq = Sequences.map(cursors, cursor -> {
final DimensionSelector selector = cursor.getColumnSelectorFactory().makeDimensionSelector(new DefaultDimensionSpec(selectColumn, selectColumn));
final List<String> values = new ArrayList<>();
while (!cursor.isDone()) {
IndexedInts row = selector.getRow();
Preconditions.checkState(row.size() == 1);
values.add(selector.lookupName(row.get(0)));
cursor.advance();
}
return values;
});
return seq.toList().get(0);
}
use of org.apache.druid.query.dimension.DefaultDimensionSpec in project druid by druid-io.
the class CalciteQueryTest method testOrderByAnyDouble.
@Test
public void testOrderByAnyDouble() throws Exception {
List<Object[]> expected;
if (NullHandling.replaceWithDefault()) {
expected = ImmutableList.of(new Object[] { "1", 0.0 }, new Object[] { "2", 0.0 }, new Object[] { "abc", 0.0 }, new Object[] { "def", 0.0 }, new Object[] { "", 1.0 }, new Object[] { "10.1", 1.7 });
} else {
expected = ImmutableList.of(new Object[] { "2", 0.0 }, new Object[] { "", 1.0 }, new Object[] { "10.1", 1.7 }, // reversed by TopNNumericResultBuilder.build()
new Object[] { "1", null }, new Object[] { "abc", null }, new Object[] { "def", null });
}
testQuery("SELECT dim1, ANY_VALUE(d1) FROM druid.numfoo GROUP BY 1 ORDER BY 2 LIMIT 10", ImmutableList.of(new TopNQueryBuilder().dataSource(CalciteTests.DATASOURCE3).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).dimension(new DefaultDimensionSpec("dim1", "_d0")).aggregators(aggregators(new DoubleAnyAggregatorFactory("a0", "d1"))).metric(new InvertedTopNMetricSpec(new NumericTopNMetricSpec("a0"))).threshold(10).context(QUERY_CONTEXT_DEFAULT).build()), expected);
}
use of org.apache.druid.query.dimension.DefaultDimensionSpec in project druid by druid-io.
the class CalciteQueryTest method testGroupByCaseWhenOfTripleAnd.
@Test
public void testGroupByCaseWhenOfTripleAnd() throws Exception {
// Cannot vectorize due to virtual columns.
cannotVectorize();
testQuery("SELECT\n" + " CASE WHEN m1 > 1 AND m1 < 5 AND cnt = 1 THEN 'x' ELSE NULL END," + " COUNT(*)\n" + "FROM druid.foo\n" + "GROUP BY 1", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "case_searched(((\"m1\" > 1) && (\"m1\" < 5) && (\"cnt\" == 1)),'x',null)", ColumnType.STRING)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0"))).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { NullHandling.defaultStringValue(), 3L }, new Object[] { "x", 3L }));
}
use of org.apache.druid.query.dimension.DefaultDimensionSpec in project druid by druid-io.
the class CalciteQueryTest method testGroupingSetsWithOrderByAggregator.
@Test
public void testGroupingSetsWithOrderByAggregator() throws Exception {
// Cannot vectorize due to virtual columns.
cannotVectorize();
testQuery("SELECT dim2, gran, SUM(cnt)\n" + "FROM (SELECT FLOOR(__time TO MONTH) AS gran, COALESCE(dim2, '') dim2, cnt FROM druid.foo) AS x\n" + "GROUP BY GROUPING SETS ( (), (dim2), (gran) )\n" + "ORDER BY SUM(cnt)\n", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "case_searched(notnull(\"dim2\"),\"dim2\",'')", ColumnType.STRING), expressionVirtualColumn("v1", "timestamp_floor(\"__time\",'P1M',null,'UTC')", ColumnType.LONG)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0"), new DefaultDimensionSpec("v1", "d1", ColumnType.LONG))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"))).setSubtotalsSpec(ImmutableList.of(ImmutableList.of("d0"), ImmutableList.of("d1"), ImmutableList.of())).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(new OrderByColumnSpec("a0", Direction.ASCENDING, StringComparators.NUMERIC)), Integer.MAX_VALUE)).setContext(withTimestampResultContext(QUERY_CONTEXT_DEFAULT, "d1", 1, Granularities.MONTH)).build()), ImmutableList.of(new Object[] { "abc", null, 1L }, new Object[] { "a", null, 2L }, new Object[] { "", null, 3L }, new Object[] { NULL_STRING, timestamp("2000-01-01"), 3L }, new Object[] { NULL_STRING, timestamp("2001-01-01"), 3L }, new Object[] { NULL_STRING, null, 6L }));
}
use of org.apache.druid.query.dimension.DefaultDimensionSpec in project druid by druid-io.
the class CalciteQueryTest method testConcatGroup.
@Test
public void testConcatGroup() throws Exception {
testQuery("SELECT CONCAT(dim1, '-', dim1, '_', dim1) as dimX FROM foo GROUP BY 1", ImmutableList.of(new GroupByQuery.Builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setVirtualColumns(expressionVirtualColumn("v0", "concat(\"dim1\",'-',\"dim1\",'_',\"dim1\")", ColumnType.STRING)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0"))).setGranularity(Granularities.ALL).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { "-_" }, new Object[] { "1-1_1" }, new Object[] { "10.1-10.1_10.1" }, new Object[] { "2-2_2" }, new Object[] { "abc-abc_abc" }, new Object[] { "def-def_def" }));
final List<Object[]> secondResults;
if (useDefault) {
secondResults = ImmutableList.of(new Object[] { "10.1x2.0999910.1" }, new Object[] { "1ax4.099991" }, new Object[] { "2x3.099992" }, new Object[] { "abcx6.09999abc" }, new Object[] { "ax1.09999" }, new Object[] { "defabcx5.09999def" });
} else {
secondResults = ImmutableList.of(new Object[] { null }, new Object[] { "1ax4.099991" }, new Object[] { "2x3.099992" }, new Object[] { "ax1.09999" }, new Object[] { "defabcx5.09999def" });
}
testQuery("SELECT CONCAT(dim1, CONCAT(dim2,'x'), m2, 9999, dim1) as dimX FROM foo GROUP BY 1", ImmutableList.of(new GroupByQuery.Builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setVirtualColumns(expressionVirtualColumn("v0", "concat(\"dim1\",concat(\"dim2\",'x'),\"m2\",9999,\"dim1\")", ColumnType.STRING)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0"))).setGranularity(Granularities.ALL).setContext(QUERY_CONTEXT_DEFAULT).build()), secondResults);
}
Aggregations