You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by as...@apache.org on 2022/05/25 08:05:14 UTC
[calcite] branch main updated: [CALCITE-5118] SqlDatePartFunction#rewriteCall should check operands length
This is an automated email from the ASF dual-hosted git repository.
asolimando pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new 2d92e2a895 [CALCITE-5118] SqlDatePartFunction#rewriteCall should check operands length
2d92e2a895 is described below
commit 2d92e2a895caba77d1451b723f9c8be8a710a9b2
Author: Benchao Li <li...@gmail.com>
AuthorDate: Wed Apr 27 22:24:04 2022 +0800
[CALCITE-5118] SqlDatePartFunction#rewriteCall should check operands length
---
.../calcite/sql/fun/SqlDatePartFunction.java | 7 ++++
.../org/apache/calcite/test/SqlValidatorTest.java | 37 ++++++++++++++++++++++
2 files changed, 44 insertions(+)
diff --git a/core/src/main/java/org/apache/calcite/sql/fun/SqlDatePartFunction.java b/core/src/main/java/org/apache/calcite/sql/fun/SqlDatePartFunction.java
index 31fbc284e0..5f6b2e0cf5 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlDatePartFunction.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlDatePartFunction.java
@@ -34,6 +34,8 @@ import org.apache.calcite.sql.validate.SqlValidator;
import java.util.List;
+import static org.apache.calcite.util.Static.RESOURCE;
+
/**
* SqlDatePartFunction represents the SQL:1999 standard {@code YEAR},
* {@code QUARTER}, {@code MONTH} and {@code DAY} functions.
@@ -56,6 +58,11 @@ public class SqlDatePartFunction extends SqlFunction {
@Override public SqlNode rewriteCall(SqlValidator validator, SqlCall call) {
final List<SqlNode> operands = call.getOperandList();
+
+ if (operands.size() != 1) {
+ throw validator.newValidationError(call, RESOURCE.invalidArgCount(getName(), 1));
+ }
+
final SqlParserPos pos = call.getParserPosition();
return SqlStdOperatorTable.EXTRACT.createCall(pos,
new SqlIntervalQualifier(timeUnit, null, SqlParserPos.ZERO),
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index a8692e61cf..d9d5a420ff 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -8977,6 +8977,43 @@ public class SqlValidatorTest extends SqlValidatorTestCase {
.rewritesTo(expected2);
}
+ @Test void testDatePartWithRewrite() {
+ final String sql = "select week(date '2022-04-27'), year(date '2022-04-27')";
+ final String expected = "SELECT EXTRACT(WEEK FROM DATE '2022-04-27'),"
+ + " EXTRACT(YEAR FROM DATE '2022-04-27')";
+ sql(sql)
+ .withValidatorCallRewrite(true)
+ .rewritesTo(expected);
+
+ final String noParamSql = "select ^week()^";
+ sql(noParamSql)
+ .withValidatorCallRewrite(true)
+ .fails("Invalid number of arguments to function 'WEEK'. Was expecting 1 arguments");
+
+ final String multiParamsSql = "select ^week(date '2022-04-27', 1)^";
+ sql(multiParamsSql)
+ .withValidatorCallRewrite(true)
+ .fails("Invalid number of arguments to function 'WEEK'. Was expecting 1 arguments");
+ }
+
+ @Test void testDatePartWithoutRewrite() {
+ final String sql = "select week(date '2022-04-27'), year(date '2022-04-27')";
+ final String expected = "SELECT WEEK(DATE '2022-04-27'), YEAR(DATE '2022-04-27')";
+ sql(sql)
+ .withValidatorCallRewrite(false)
+ .rewritesTo(expected);
+
+ final String noParamSql = "select ^week()^";
+ sql(noParamSql)
+ .withValidatorCallRewrite(false)
+ .fails("Invalid number of arguments to function 'WEEK'. Was expecting 1 arguments");
+
+ final String multiParamsSql = "select ^week(date '2022-04-27', 1)^";
+ sql(multiParamsSql)
+ .withValidatorCallRewrite(false)
+ .fails("Invalid number of arguments to function 'WEEK'. Was expecting 1 arguments");
+ }
+
@Disabled
@Test void testValuesWithAggFuncs() {
sql("values(^count(1)^)")