diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/aggregation/builtin/EarliestLatestAnySqlAggregator.java b/sql/src/main/java/org/apache/druid/sql/calcite/aggregation/builtin/EarliestLatestAnySqlAggregator.java index 58fd083dcab..ba061483639 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/aggregation/builtin/EarliestLatestAnySqlAggregator.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/aggregation/builtin/EarliestLatestAnySqlAggregator.java @@ -202,12 +202,20 @@ public class EarliestLatestAnySqlAggregator implements SqlAggregator theAggFactory = aggregatorType.createAggregatorFactory(aggregatorName, fieldName, null, outputType, -1); break; case 2: + int maxStringBytes; + try { + maxStringBytes = RexLiteral.intValue(rexNodes.get(1)); + } + catch (AssertionError ae) { + plannerContext.setPlanningError("The second argument '%s' to function '%s' is not a number", rexNodes.get(1), aggregateCall.getName()); + return null; + } theAggFactory = aggregatorType.createAggregatorFactory( aggregatorName, fieldName, null, outputType, - RexLiteral.intValue(rexNodes.get(1)) + maxStringBytes ); break; default: diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/aggregation/builtin/EarliestLatestBySqlAggregator.java b/sql/src/main/java/org/apache/druid/sql/calcite/aggregation/builtin/EarliestLatestBySqlAggregator.java index f13a918e6fa..46762306a45 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/aggregation/builtin/EarliestLatestBySqlAggregator.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/aggregation/builtin/EarliestLatestBySqlAggregator.java @@ -122,12 +122,20 @@ public class EarliestLatestBySqlAggregator implements SqlAggregator ); break; case 3: + int maxStringBytes; + try { + maxStringBytes = RexLiteral.intValue(rexNodes.get(2)); + } + catch (AssertionError ae) { + plannerContext.setPlanningError("The third argument '%s' to function '%s' is not a number", rexNodes.get(2), aggregateCall.getName()); + return null; + } theAggFactory = aggregatorType.createAggregatorFactory( aggregatorName, fieldName, EarliestLatestAnySqlAggregator.getColumnName(plannerContext, virtualColumnRegistry, args.get(1), rexNodes.get(1)), outputType, - RexLiteral.intValue(rexNodes.get(2)) + maxStringBytes ); break; default: diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java index 15be7d30b23..9d6fcd0c336 100644 --- a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java +++ b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java @@ -997,6 +997,56 @@ public class CalciteQueryTest extends BaseCalciteQueryTest ); } + @Test + public void testStringLatestGroupByWithAlwaysFalseCondition() + { + testQuery( + "SELECT LATEST(dim4, 10),dim2 FROM numfoo WHERE (dim1 = 'something' AND dim1 IN( 'something else') ) GROUP BY dim2", + ImmutableList.of( + Druids.newScanQueryBuilder() + .dataSource(InlineDataSource.fromIterable( + ImmutableList.of(), + RowSignature.builder() + .add("EXPR$0", ColumnType.STRING) + .add("dim2", ColumnType.STRING) + .build() + )) + .intervals(querySegmentSpec(Filtration.eternity())) + .columns("EXPR$0", "dim2") + .context(QUERY_CONTEXT_DEFAULT) + .resultFormat(ResultFormat.RESULT_FORMAT_COMPACTED_LIST) + .legacy(false) + .build() + ), + ImmutableList.of() + ); + } + + @Test + public void testStringLatestByGroupByWithAlwaysFalseCondition() + { + testQuery( + "SELECT LATEST_BY(dim4, __time, 10),dim2 FROM numfoo WHERE (dim1 = 'something' AND dim1 IN( 'something else') ) GROUP BY dim2", + ImmutableList.of( + Druids.newScanQueryBuilder() + .dataSource(InlineDataSource.fromIterable( + ImmutableList.of(), + RowSignature.builder() + .add("EXPR$0", ColumnType.STRING) + .add("dim2", ColumnType.STRING) + .build() + )) + .intervals(querySegmentSpec(Filtration.eternity())) + .columns("EXPR$0", "dim2") + .context(QUERY_CONTEXT_DEFAULT) + .resultFormat(ResultFormat.RESULT_FORMAT_COMPACTED_LIST) + .legacy(false) + .build() + ), + ImmutableList.of() + ); + } + // This test the off-heap (buffer) version of the EarliestAggregator (Double/Float/Long) @Test public void testPrimitiveEarliestInSubquery()