Search in sources :

Example 1 with RowSignature

use of org.apache.druid.segment.column.RowSignature in project druid by druid-io.

the class CalciteInsertDmlTest method testInsertWithPartitionedByAndClusteredBy.

@Test
public void testInsertWithPartitionedByAndClusteredBy() {
    // Test correctness of the query when both PARTITIONED BY and CLUSTERED BY clause is present
    RowSignature targetRowSignature = RowSignature.builder().add("__time", ColumnType.LONG).add("floor_m1", ColumnType.FLOAT).add("dim1", ColumnType.STRING).build();
    testInsertQuery().sql("INSERT INTO druid.dst SELECT __time, FLOOR(m1) as floor_m1, dim1 FROM foo PARTITIONED BY DAY CLUSTERED BY 2, dim1").expectTarget("dst", targetRowSignature).expectResources(dataSourceRead("foo"), dataSourceWrite("dst")).expectQuery(newScanQueryBuilder().dataSource("foo").intervals(querySegmentSpec(Filtration.eternity())).columns("__time", "dim1", "v0").virtualColumns(expressionVirtualColumn("v0", "floor(\"m1\")", ColumnType.FLOAT)).orderBy(ImmutableList.of(new ScanQuery.OrderBy("v0", ScanQuery.Order.ASCENDING), new ScanQuery.OrderBy("dim1", ScanQuery.Order.ASCENDING))).context(queryContextWithGranularity(Granularities.DAY)).build()).verify();
}
Also used : RowSignature(org.apache.druid.segment.column.RowSignature) Test(org.junit.Test)

Example 2 with RowSignature

use of org.apache.druid.segment.column.RowSignature in project druid by druid-io.

the class CalciteInsertDmlTest method testPartitionedBySupportedClauses.

@Test
public void testPartitionedBySupportedClauses() {
    RowSignature targetRowSignature = RowSignature.builder().add("__time", ColumnType.LONG).add("dim1", ColumnType.STRING).build();
    Map<String, Granularity> partitionedByArgumentToGranularityMap = ImmutableMap.<String, Granularity>builder().put("HOUR", Granularities.HOUR).put("DAY", Granularities.DAY).put("MONTH", Granularities.MONTH).put("YEAR", Granularities.YEAR).put("ALL", Granularities.ALL).put("ALL TIME", Granularities.ALL).put("FLOOR(__time TO QUARTER)", Granularities.QUARTER).put("TIME_FLOOR(__time, 'PT1H')", Granularities.HOUR).build();
    partitionedByArgumentToGranularityMap.forEach((partitionedByArgument, expectedGranularity) -> {
        Map<String, Object> queryContext = null;
        try {
            queryContext = ImmutableMap.of(DruidSqlInsert.SQL_INSERT_SEGMENT_GRANULARITY, queryJsonMapper.writeValueAsString(expectedGranularity));
        } catch (JsonProcessingException e) {
            // Won't reach here
            Assert.fail(e.getMessage());
        }
        testInsertQuery().sql(StringUtils.format("INSERT INTO druid.dst SELECT __time, dim1 FROM foo PARTITIONED BY %s", partitionedByArgument)).expectTarget("dst", targetRowSignature).expectResources(dataSourceRead("foo"), dataSourceWrite("dst")).expectQuery(newScanQueryBuilder().dataSource("foo").intervals(querySegmentSpec(Filtration.eternity())).columns("__time", "dim1").context(queryContext).build()).verify();
        didTest = false;
    });
    didTest = true;
}
Also used : Granularity(org.apache.druid.java.util.common.granularity.Granularity) RowSignature(org.apache.druid.segment.column.RowSignature) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test)

Example 3 with RowSignature

use of org.apache.druid.segment.column.RowSignature in project druid by druid-io.

the class ObjectWriter method writeHeader.

static void writeHeader(final JsonGenerator jsonGenerator, final RelDataType rowType, final boolean includeTypes, final boolean includeSqlTypes) throws IOException {
    final RowSignature signature = RowSignatures.fromRelDataType(rowType.getFieldNames(), rowType);
    jsonGenerator.writeStartObject();
    for (int i = 0; i < signature.size(); i++) {
        jsonGenerator.writeFieldName(signature.getColumnName(i));
        if (!includeTypes && !includeSqlTypes) {
            jsonGenerator.writeNull();
        } else {
            jsonGenerator.writeStartObject();
            if (includeTypes) {
                jsonGenerator.writeStringField(ObjectWriter.TYPE_HEADER_NAME, signature.getColumnType(i).map(TypeSignature::asTypeString).orElse(null));
            }
            if (includeSqlTypes) {
                jsonGenerator.writeStringField(ObjectWriter.SQL_TYPE_HEADER_NAME, rowType.getFieldList().get(i).getType().getSqlTypeName().getName());
            }
            jsonGenerator.writeEndObject();
        }
    }
    jsonGenerator.writeEndObject();
}
Also used : TypeSignature(org.apache.druid.segment.column.TypeSignature) RowSignature(org.apache.druid.segment.column.RowSignature)

Example 4 with RowSignature

use of org.apache.druid.segment.column.RowSignature in project druid by druid-io.

the class TimeseriesQueryRunnerTest method testTimeseriesWithTimestampResultFieldContextForArrayResponse.

@Test
public void testTimeseriesWithTimestampResultFieldContextForArrayResponse() {
    Granularity gran = Granularities.DAY;
    TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(gran).intervals(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).aggregators(QueryRunnerTestHelper.ROWS_COUNT, QueryRunnerTestHelper.INDEX_DOUBLE_SUM, QueryRunnerTestHelper.QUALITY_UNIQUES).postAggregators(QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT).descending(descending).context(makeContext(ImmutableMap.of(TimeseriesQuery.CTX_TIMESTAMP_RESULT_FIELD, TIMESTAMP_RESULT_FIELD_NAME, TimeseriesQuery.SKIP_EMPTY_BUCKETS, true))).build();
    Assert.assertEquals(TIMESTAMP_RESULT_FIELD_NAME, query.getTimestampResultField());
    QueryToolChest<Result<TimeseriesResultValue>, TimeseriesQuery> toolChest = new TimeseriesQueryQueryToolChest();
    RowSignature rowSignature = toolChest.resultArraySignature(query);
    Assert.assertNotNull(rowSignature);
    List<String> columnNames = rowSignature.getColumnNames();
    Assert.assertNotNull(columnNames);
    Assert.assertEquals(6, columnNames.size());
    Assert.assertEquals("__time", columnNames.get(0));
    Assert.assertEquals(TIMESTAMP_RESULT_FIELD_NAME, columnNames.get(1));
    Assert.assertEquals("rows", columnNames.get(2));
    Assert.assertEquals("index", columnNames.get(3));
    Assert.assertEquals("uniques", columnNames.get(4));
    Assert.assertEquals("addRowsIndexConstant", columnNames.get(5));
    Sequence<Result<TimeseriesResultValue>> results = runner.run(QueryPlus.wrap(query));
    Sequence<Object[]> resultsAsArrays = toolChest.resultsAsArrays(query, results);
    Assert.assertNotNull(resultsAsArrays);
    final String[] expectedIndex = descending ? QueryRunnerTestHelper.EXPECTED_FULL_ON_INDEX_VALUES_DESC : QueryRunnerTestHelper.EXPECTED_FULL_ON_INDEX_VALUES;
    final String[] expectedIndexToUse = Arrays.stream(expectedIndex).filter(eachIndex -> !"0.0".equals(eachIndex)).toArray(String[]::new);
    final Long expectedLast = descending ? QueryRunnerTestHelper.EARLIEST.getMillis() : QueryRunnerTestHelper.LAST.getMillis();
    int count = 0;
    Object[] lastResult = null;
    for (Object[] result : resultsAsArrays.toList()) {
        Long current = (Long) result[0];
        Assert.assertFalse(StringUtils.format("Timestamp[%s] > expectedLast[%s]", current, expectedLast), descending ? current < expectedLast : current > expectedLast);
        Assert.assertEquals((Long) result[1], current, 0);
        Assert.assertEquals(QueryRunnerTestHelper.SKIPPED_DAY.getMillis() == current ? (Long) 0L : (Long) 13L, result[2]);
        if (QueryRunnerTestHelper.SKIPPED_DAY.getMillis() != current) {
            Assert.assertEquals(Doubles.tryParse(expectedIndexToUse[count]).doubleValue(), (Double) result[3], (Double) result[3] * 1e-6);
            Assert.assertEquals((Double) result[4], 9.0d, 0.02);
            Assert.assertEquals(new Double(expectedIndexToUse[count]) + 13L + 1L, (Double) result[5], (Double) result[5] * 1e-6);
        } else {
            if (NullHandling.replaceWithDefault()) {
                Assert.assertEquals(0.0D, (Double) result[3], (Double) result[3] * 1e-6);
                Assert.assertEquals(0.0D, (Double) result[4], 0.02);
                Assert.assertEquals(new Double(expectedIndexToUse[count]) + 1L, (Double) result[5], (Double) result[5] * 1e-6);
            } else {
                Assert.assertNull(result[3]);
                Assert.assertEquals((Double) result[4], 0.0, 0.02);
                Assert.assertNull(result[5]);
            }
        }
        lastResult = result;
        ++count;
    }
    Assert.assertEquals(expectedLast, lastResult[0]);
}
Also used : QueryPlus(org.apache.druid.query.QueryPlus) DateTimeZone(org.joda.time.DateTimeZone) Arrays(java.util.Arrays) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Druids(org.apache.druid.query.Druids) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) Map(java.util.Map) QueryRunner(org.apache.druid.query.QueryRunner) Parameterized(org.junit.runners.Parameterized) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) DateTimes(org.apache.druid.java.util.common.DateTimes) Sequence(org.apache.druid.java.util.common.guava.Sequence) FinalizeResultsQueryRunner(org.apache.druid.query.FinalizeResultsQueryRunner) RegexDimFilter(org.apache.druid.query.filter.RegexDimFilter) HyperUniquesAggregatorFactory(org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory) ImmutableSet(com.google.common.collect.ImmutableSet) NotDimFilter(org.apache.druid.query.filter.NotDimFilter) ImmutableMap(com.google.common.collect.ImmutableMap) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) StringUtils(org.apache.druid.java.util.common.StringUtils) TestExprMacroTable(org.apache.druid.query.expression.TestExprMacroTable) Collectors(java.util.stream.Collectors) QueryContexts(org.apache.druid.query.QueryContexts) List(java.util.List) QueryRunnerTestHelper(org.apache.druid.query.QueryRunnerTestHelper) Doubles(com.google.common.primitives.Doubles) DoubleFirstAggregatorFactory(org.apache.druid.query.aggregation.first.DoubleFirstAggregatorFactory) DoubleLastAggregatorFactory(org.apache.druid.query.aggregation.last.DoubleLastAggregatorFactory) Iterables(com.google.common.collect.Iterables) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) Granularity(org.apache.druid.java.util.common.granularity.Granularity) Intervals(org.apache.druid.java.util.common.Intervals) FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MapLookupExtractor(org.apache.druid.query.extraction.MapLookupExtractor) HumanReadableBytes(org.apache.druid.java.util.common.HumanReadableBytes) Interval(org.joda.time.Interval) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) DoubleMinAggregatorFactory(org.apache.druid.query.aggregation.DoubleMinAggregatorFactory) StringComparators(org.apache.druid.query.ordering.StringComparators) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) StreamSupport(java.util.stream.StreamSupport) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) ExpectedException(org.junit.rules.ExpectedException) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) DoubleMaxAggregatorFactory(org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) Period(org.joda.time.Period) ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) DateTime(org.joda.time.DateTime) QueryToolChest(org.apache.druid.query.QueryToolChest) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test) Granularities(org.apache.druid.java.util.common.granularity.Granularities) Result(org.apache.druid.query.Result) TestHelper(org.apache.druid.segment.TestHelper) Rule(org.junit.Rule) NullHandling(org.apache.druid.common.config.NullHandling) RowSignature(org.apache.druid.segment.column.RowSignature) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) ColumnType(org.apache.druid.segment.column.ColumnType) FloatSumAggregatorFactory(org.apache.druid.query.aggregation.FloatSumAggregatorFactory) InDimFilter(org.apache.druid.query.filter.InDimFilter) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) Assert(org.junit.Assert) Collections(java.util.Collections) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) Granularity(org.apache.druid.java.util.common.granularity.Granularity) Result(org.apache.druid.query.Result) RowSignature(org.apache.druid.segment.column.RowSignature) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 5 with RowSignature

use of org.apache.druid.segment.column.RowSignature in project druid by druid-io.

the class DruidQuery method computeGrouping.

@Nonnull
private static Grouping computeGrouping(final PartialDruidQuery partialQuery, final PlannerContext plannerContext, final RowSignature rowSignature, final VirtualColumnRegistry virtualColumnRegistry, final RexBuilder rexBuilder, final boolean finalizeAggregations) {
    final Aggregate aggregate = Preconditions.checkNotNull(partialQuery.getAggregate(), "aggregate");
    final Project aggregateProject = partialQuery.getAggregateProject();
    final List<DimensionExpression> dimensions = computeDimensions(partialQuery, plannerContext, rowSignature, virtualColumnRegistry);
    final Subtotals subtotals = computeSubtotals(partialQuery, rowSignature);
    final List<Aggregation> aggregations = computeAggregations(partialQuery, plannerContext, rowSignature, virtualColumnRegistry, rexBuilder, finalizeAggregations);
    final RowSignature aggregateRowSignature = RowSignatures.fromRelDataType(ImmutableList.copyOf(Iterators.concat(dimensions.stream().map(DimensionExpression::getOutputName).iterator(), aggregations.stream().map(Aggregation::getOutputName).iterator())), aggregate.getRowType());
    final DimFilter havingFilter = computeHavingFilter(partialQuery, plannerContext, aggregateRowSignature);
    final Grouping grouping = Grouping.create(dimensions, subtotals, aggregations, havingFilter, aggregateRowSignature);
    if (aggregateProject == null) {
        return grouping;
    } else {
        return grouping.applyProject(plannerContext, aggregateProject);
    }
}
Also used : Aggregation(org.apache.druid.sql.calcite.aggregation.Aggregation) Project(org.apache.calcite.rel.core.Project) DimensionExpression(org.apache.druid.sql.calcite.aggregation.DimensionExpression) Aggregate(org.apache.calcite.rel.core.Aggregate) RowSignature(org.apache.druid.segment.column.RowSignature) DimFilter(org.apache.druid.query.filter.DimFilter) Nonnull(javax.annotation.Nonnull)

Aggregations

RowSignature (org.apache.druid.segment.column.RowSignature)46 ColumnType (org.apache.druid.segment.column.ColumnType)17 List (java.util.List)14 Test (org.junit.Test)13 Collectors (java.util.stream.Collectors)12 Nullable (javax.annotation.Nullable)11 PlannerContext (org.apache.druid.sql.calcite.planner.PlannerContext)11 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)10 Expressions (org.apache.druid.sql.calcite.expression.Expressions)10 Project (org.apache.calcite.rel.core.Project)9 RexLiteral (org.apache.calcite.rex.RexLiteral)9 RexNode (org.apache.calcite.rex.RexNode)9 SqlKind (org.apache.calcite.sql.SqlKind)9 ISE (org.apache.druid.java.util.common.ISE)9 Aggregation (org.apache.druid.sql.calcite.aggregation.Aggregation)9 ArrayList (java.util.ArrayList)8 StringUtils (org.apache.druid.java.util.common.StringUtils)8 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)8 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableSet (com.google.common.collect.ImmutableSet)6