From 69b2ea3ddc0fd6831f5ca4cfa2a5fcde4ebfc3cc Mon Sep 17 00:00:00 2001 From: Gian Merlino Date: Tue, 21 May 2019 11:32:36 -0700 Subject: [PATCH] SQL: TIME_EXTRACT should have 2 required operands. (#7710) * SQL: TIME_EXTRACT should have 2 required operands. Timestamp and time unit are both required. * Add regression test. --- .../builtin/TimeExtractOperatorConversion.java | 2 +- .../druid/sql/calcite/CalciteQueryTest.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/TimeExtractOperatorConversion.java b/sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/TimeExtractOperatorConversion.java index 28464fa56f5..5f1c77db0b1 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/TimeExtractOperatorConversion.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/TimeExtractOperatorConversion.java @@ -43,7 +43,7 @@ public class TimeExtractOperatorConversion implements SqlOperatorConversion private static final SqlFunction SQL_FUNCTION = OperatorConversions .operatorBuilder("TIME_EXTRACT") .operandTypes(SqlTypeFamily.TIMESTAMP, SqlTypeFamily.CHARACTER, SqlTypeFamily.CHARACTER) - .requiredOperands(1) + .requiredOperands(2) .returnType(SqlTypeName.BIGINT) .functionCategory(SqlFunctionCategory.TIMEDATE) .build(); 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 ecef1b5a303..e67cde8b843 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 @@ -21,6 +21,8 @@ package org.apache.druid.sql.calcite; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; +import org.apache.calcite.runtime.CalciteContextException; +import org.apache.calcite.tools.ValidationException; import org.apache.druid.common.config.NullHandling; import org.apache.druid.java.util.common.DateTimes; import org.apache.druid.java.util.common.Intervals; @@ -6695,6 +6697,22 @@ public class CalciteQueryTest extends BaseCalciteQueryTest assertQueryIsUnplannable(theQuery); } + @Test + public void testTimeExtractWithTooFewArguments() throws Exception + { + // Regression test for https://github.com/apache/incubator-druid/pull/7710. + expectedException.expect(ValidationException.class); + expectedException.expectCause(CoreMatchers.instanceOf(CalciteContextException.class)); + expectedException.expectCause( + ThrowableMessageMatcher.hasMessage( + CoreMatchers.containsString( + "Invalid number of arguments to function 'TIME_EXTRACT'. Was expecting 2 arguments" + ) + ) + ); + testQuery("SELECT TIME_EXTRACT(__time) FROM druid.foo", ImmutableList.of(), ImmutableList.of()); + } + @Test public void testUsingSubqueryAsFilterForbiddenByConfig() {