suneet-s commented on code in PR #12472: URL: https://github.com/apache/druid/pull/12472#discussion_r856371135
########## sql/src/test/java/org/apache/druid/sql/calcite/CalciteTimeBoundaryQueryTest.java: ########## @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.sql.calcite; + +import com.google.common.collect.ImmutableList; +import org.apache.druid.java.util.common.DateTimes; +import org.apache.druid.java.util.common.Intervals; +import org.apache.druid.query.Druids; +import org.apache.druid.query.aggregation.LongMaxAggregatorFactory; +import org.apache.druid.query.aggregation.LongMinAggregatorFactory; +import org.apache.druid.query.spec.MultipleIntervalSegmentSpec; +import org.apache.druid.query.timeboundary.TimeBoundaryQuery; +import org.apache.druid.sql.calcite.filtration.Filtration; +import org.junit.Test; + +import java.util.HashMap; + +public class CalciteTimeBoundaryQueryTest extends BaseCalciteQueryTest +{ + // __time for foo is [2000-01-01, 2000-01-02, 2000-01-03, 2001-01-01, 2001-01-02, 2001-01-03] + @Test + public void testMaxTimeQuery() throws Exception + { + HashMap<String, Object> context = new HashMap<>(QUERY_CONTEXT_DEFAULT); + context.put(TimeBoundaryQuery.MAX_TIME_ARRAY_OUTPUT_NAME, "a0"); + testQuery( + "SELECT MAX(__time) AS maxTime FROM foo", + ImmutableList.of( + Druids.newTimeBoundaryQueryBuilder() + .dataSource("foo") + .bound(TimeBoundaryQuery.MAX_TIME) + .context(context) + .build() + ), + ImmutableList.of(new Object[]{DateTimes.of("2001-01-03").getMillis()}) + ); + } + + @Test + public void testMinTimeQuery() throws Exception + { + HashMap<String, Object> context = new HashMap<>(QUERY_CONTEXT_DEFAULT); + context.put(TimeBoundaryQuery.MIN_TIME_ARRAY_OUTPUT_NAME, "a0"); + testQuery( + "SELECT MIN(__time) AS minTime FROM foo", + ImmutableList.of( + Druids.newTimeBoundaryQueryBuilder() + .dataSource("foo") + .bound(TimeBoundaryQuery.MIN_TIME) + .context(context) + .build() + ), + ImmutableList.of(new Object[]{DateTimes.of("2000-01-01").getMillis()}) + ); + } + + @Test + public void testMinTimeQueryWithFilters() throws Exception + { + HashMap<String, Object> context = new HashMap<>(QUERY_CONTEXT_DEFAULT); + context.put(TimeBoundaryQuery.MIN_TIME_ARRAY_OUTPUT_NAME, "a0"); + testQuery( + "SELECT MIN(__time) AS minTime FROM foo where __time >= '2001-01-01' and __time < '2003-01-01'", + ImmutableList.of( + Druids.newTimeBoundaryQueryBuilder() + .dataSource("foo") + .intervals( + new MultipleIntervalSegmentSpec( + ImmutableList.of(Intervals.of("2001-01-01T00:00:00.000Z/2003-01-01T00:00:00.000Z")) + ) + ) + .bound(TimeBoundaryQuery.MIN_TIME) + .context(context) + .build() + ), + ImmutableList.of(new Object[]{DateTimes.of("2001-01-01").getMillis()}) + ); + } + + // this test is to maintain that if both min(__time) and max(__time) are present, + // we currently don't convert that query to timeBoundary Review Comment: ```suggestion // Currently, if both min(__time) and max(__time) are present, we don't convert it // to a timeBoundary query. This should be supported in a future change ``` (bonus points if you can create a github issue and link it here) :) ########## processing/src/test/java/org/apache/druid/query/timeboundary/TimeBoundaryQueryRunnerTest.java: ########## @@ -216,6 +247,22 @@ public void testTimeBoundary() Assert.assertEquals(DateTimes.of("2011-04-15T00:00:00.000Z"), maxTime); } + @Test(expected = UOE.class) + @SuppressWarnings("unchecked") + public void testTimeBoundaryArrayResults() + { + TimeBoundaryQuery timeBoundaryQuery = Druids.newTimeBoundaryQueryBuilder() + .dataSource("testing") + .bound(null) + .build(); + ResponseContext context = ConcurrentResponseContext.createEmpty(); + context.initializeMissingSegments(); + new TimeBoundaryQueryQueryToolChest().resultsAsArrays( + timeBoundaryQuery, + runner.run(QueryPlus.wrap(timeBoundaryQuery), context) + ).toList(); + } + Review Comment: Seems like these tests belong in `TimeBoundaryQueryQueryToolChestTest` instead of this class? * testTimeBoundaryArrayResults * testTimeBoundaryMaxArraysResults * testTimeBoundaryMinArraysResults ########## processing/src/main/java/org/apache/druid/query/timeboundary/TimeBoundaryQueryQueryToolChest.java: ########## @@ -224,4 +226,40 @@ public Result<TimeBoundaryResultValue> apply(Object input) } }; } + + @Override + public RowSignature resultArraySignature(TimeBoundaryQuery query) + { + if (query.isMinTime() || query.isMaxTime()) { + RowSignature.Builder builder = RowSignature.builder(); + String outputName = query.isMinTime() ? + query.getContextValue(TimeBoundaryQuery.MIN_TIME_ARRAY_OUTPUT_NAME, TimeBoundaryQuery.MIN_TIME) : + query.getContextValue(TimeBoundaryQuery.MAX_TIME_ARRAY_OUTPUT_NAME, TimeBoundaryQuery.MAX_TIME); + return builder.add(outputName, ColumnType.LONG).build(); + } + return super.resultArraySignature(query); Review Comment: I see, this is still a substantial improvement for use cases that just need either min or max time. Let's call this out as a temporary limitation in the PR description, in case someone stumbles across it and decides to try and fix it. ########## processing/src/test/java/org/apache/druid/query/timeboundary/TimeBoundaryQueryQueryToolChestTest.java: ########## @@ -289,6 +292,43 @@ public void testFilteredFilterSegments() Assert.assertEquals(7, segments.size()); } + @Test(expected = UOE.class) + public void testResultArraySignature() + { + TimeBoundaryQuery timeBoundaryQuery = Druids.newTimeBoundaryQueryBuilder() + .dataSource("testing") + .build(); + new TimeBoundaryQueryQueryToolChest().resultArraySignature(timeBoundaryQuery); + } + + @Test + public void testResultArraySignatureWithMinTime() + { + TimeBoundaryQuery timeBoundaryQuery = Druids.newTimeBoundaryQueryBuilder() + .dataSource("testing") + .bound(TimeBoundaryQuery.MIN_TIME) + .context(ImmutableMap.of(TimeBoundaryQuery.MIN_TIME_ARRAY_OUTPUT_NAME, "foo")) + .build(); + RowSignature rowSignature = new TimeBoundaryQueryQueryToolChest().resultArraySignature(timeBoundaryQuery); + RowSignature.Builder expectedRowSignatureBuilder = RowSignature.builder(); + expectedRowSignatureBuilder.add("foo", ColumnType.LONG); + Assert.assertEquals(expectedRowSignatureBuilder.build(), rowSignature); + } + + @Test + public void testResultArraySignatureWithMaxTime() + { + TimeBoundaryQuery timeBoundaryQuery = Druids.newTimeBoundaryQueryBuilder() + .dataSource("testing") + .bound(TimeBoundaryQuery.MAX_TIME) + .context(ImmutableMap.of(TimeBoundaryQuery.MAX_TIME_ARRAY_OUTPUT_NAME, "foo")) + .build(); + RowSignature rowSignature = new TimeBoundaryQueryQueryToolChest().resultArraySignature(timeBoundaryQuery); + RowSignature.Builder expectedRowSignatureBuilder = RowSignature.builder(); + expectedRowSignatureBuilder.add("foo", ColumnType.LONG); + Assert.assertEquals(expectedRowSignatureBuilder.build(), rowSignature); + } + Review Comment: looks like we're missing some unit tests for `resultsAsArrays` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
