Repository: flink Updated Branches: refs/heads/master 75d74d251 -> 1eeea74bf
[FLINK-9482] [table] EXTRACT function argument validation whether it is applicable for TIME type This closes #6121. Project: http://git-wip-us.apache.org/repos/asf/flink/repo Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/1eeea74b Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/1eeea74b Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/1eeea74b Branch: refs/heads/master Commit: 1eeea74bf925c768204130fdd75683a3ac4419a3 Parents: 75d74d2 Author: Viktor Vlasov <[email protected]> Authored: Tue Jun 5 13:04:15 2018 +0300 Committer: Shuyi Chen <[email protected]> Committed: Thu Jun 7 17:55:11 2018 -0700 ---------------------------------------------------------------------- .../table/codegen/calls/ExtractCallGen.scala | 4 +++ .../ScalarFunctionsValidationTest.scala | 35 ++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flink/blob/1eeea74b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/ExtractCallGen.scala ---------------------------------------------------------------------- diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/ExtractCallGen.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/ExtractCallGen.scala index f7e428b..18a26e5 100644 --- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/ExtractCallGen.scala +++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/ExtractCallGen.scala @@ -22,6 +22,7 @@ import java.lang.reflect.Method import org.apache.calcite.avatica.util.{TimeUnit, TimeUnitRange} import org.apache.flink.api.common.typeinfo.{SqlTimeTypeInfo, TypeInformation} +import org.apache.flink.table.api.ValidationException import org.apache.flink.table.codegen.CodeGenUtils._ import org.apache.flink.table.codegen.calls.CallGenerator.generateCallIfArgsNotNull import org.apache.flink.table.codegen.{CodeGenException, CodeGenerator, GeneratedExpression} @@ -56,6 +57,9 @@ class ExtractCallGen(returnType: TypeInformation[_], method: Method) case SqlTimeTypeInfo.DATE => return super.generate(codeGenerator, operands) + case SqlTimeTypeInfo.TIME => + throw new ValidationException("unit " + unit + " can not be applied to time variable") + case _ => // do nothing } http://git-wip-us.apache.org/repos/asf/flink/blob/1eeea74b/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/validation/ScalarFunctionsValidationTest.scala ---------------------------------------------------------------------- diff --git a/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/validation/ScalarFunctionsValidationTest.scala b/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/validation/ScalarFunctionsValidationTest.scala index 6673e47..05e4e9b 100644 --- a/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/validation/ScalarFunctionsValidationTest.scala +++ b/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/validation/ScalarFunctionsValidationTest.scala @@ -18,9 +18,9 @@ package org.apache.flink.table.expressions.validation +import org.apache.calcite.avatica.util.TimeUnit import org.apache.flink.table.api.{SqlParserException, ValidationException} import org.apache.flink.table.api.scala._ -import org.apache.flink.table.codegen.CodeGenException import org.apache.flink.table.expressions.utils.ScalarTypesTestBase import org.junit.Test @@ -98,16 +98,45 @@ class ScalarFunctionsValidationTest extends ScalarTypesTestBase { testSqlApi("TIMESTAMPADD(YEAR, 1.0, timestamp '2016-02-24 12:42:25')", "2016-06-16") } - @Test(expected = classOf[CodeGenException]) + @Test(expected = classOf[ValidationException]) def testDOWWithTimeWhichIsUnsupported(): Unit = { testSqlApi("EXTRACT(DOW FROM TIME '12:42:25')", "0") } - @Test(expected = classOf[CodeGenException]) + @Test(expected = classOf[ValidationException]) def testDOYWithTimeWhichIsUnsupported(): Unit = { testSqlApi("EXTRACT(DOY FROM TIME '12:42:25')", "0") } + private def testExtractFromTimeZeroResult(unit: TimeUnit): Unit = { + testSqlApi("EXTRACT(" + unit + " FROM TIME '00:00:00')", "0") + } + + @Test(expected = classOf[ValidationException]) + def testMillenniumWithTime(): Unit = { + testExtractFromTimeZeroResult(TimeUnit.MILLENNIUM) + } + + @Test(expected = classOf[ValidationException]) + def testCenturyWithTime(): Unit = { + testExtractFromTimeZeroResult(TimeUnit.CENTURY) + } + + @Test(expected = classOf[ValidationException]) + def testYearWithTime(): Unit = { + testExtractFromTimeZeroResult(TimeUnit.YEAR) + } + + @Test(expected = classOf[ValidationException]) + def testMonthWithTime(): Unit = { + testExtractFromTimeZeroResult(TimeUnit.MONTH) + } + + @Test(expected = classOf[ValidationException]) + def testDayWithTime(): Unit = { + testExtractFromTimeZeroResult(TimeUnit.DAY) + } + // ---------------------------------------------------------------------------------------------- // Sub-query functions // ----------------------------------------------------------------------------------------------
