This is an automated email from the ASF dual-hosted git repository.
mbudiu 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 29e20ee16b [CALCITE-6913] Some casts inserted by type coercion do not
have source position information
29e20ee16b is described below
commit 29e20ee16bdcaaa29cad9d8d553c095bdb77fdf3
Author: Mihai Budiu <[email protected]>
AuthorDate: Tue Mar 25 17:04:42 2025 -0700
[CALCITE-6913] Some casts inserted by type coercion do not have source
position information
Signed-off-by: Mihai Budiu <[email protected]>
---
.../validate/implicit/AbstractTypeCoercion.java | 3 +--
.../org/apache/calcite/test/SqlValidatorTest.java | 24 ++++++++++++++++++++++
2 files changed, 25 insertions(+), 2 deletions(-)
diff --git
a/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
b/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
index f120a2fba7..396044eacf 100644
---
a/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
+++
b/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
@@ -34,7 +34,6 @@
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParseException;
-import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.parser.SqlParserUtil;
import org.apache.calcite.sql.type.SqlTypeCoercionRule;
import org.apache.calcite.sql.type.SqlTypeFamily;
@@ -313,7 +312,7 @@ protected boolean needToCast(SqlValidatorScope scope,
SqlNode node,
* <p>Ignore constant reduction which should happen in RexSimplify.
*/
private static SqlNode castTo(SqlNode node, RelDataType type) {
- return SqlStdOperatorTable.CAST.createCall(SqlParserPos.ZERO, node,
+ return SqlStdOperatorTable.CAST.createCall(node.getParserPosition(), node,
SqlTypeUtil.convertTypeToSpec(type).withNullable(type.isNullable()));
}
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 f4b632993a..31d191c667 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -26,14 +26,17 @@
import org.apache.calcite.rel.type.TimeFrameSet;
import org.apache.calcite.runtime.CalciteContextException;
import org.apache.calcite.sql.SqlBasicFunction;
+import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlCollation;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlOperatorTable;
+import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.fun.SqlLibrary;
import org.apache.calcite.sql.fun.SqlLibraryOperatorTableFactory;
@@ -12483,6 +12486,27 @@ private void checkCustomColumnResolving(String table) {
}
}
+ /** Test case for <a
href="https://issues.apache.org/jira/browse/CALCITE-6913">
+ * [CALCITE-6913] Some casts inserted by type coercion do not have
+ * source position information</a>. */
+ @Test void testImplicitCastPosition() throws SqlParseException {
+ final String sql = "select -'2'";
+ final SqlParser.Config config = SqlParser.config();
+ final SqlParser sqlParserReader = SqlParser.create(sql, config);
+ final SqlNode node = sqlParserReader.parseQuery();
+ final SqlValidator validator = fixture().factory.createValidator();
+ final SqlNode x = validator.validate(node);
+ assertThat(x instanceof SqlSelect, is(true));
+ // After coercion the statement is SELECT -(CAST '2' AS DECIMAL(...))
+ SqlSelect select = (SqlSelect) x;
+ SqlNodeList selectList = select.getSelectList();
+ assertThat(selectList.isEmpty(), is(false));
+ SqlNode neg = selectList.get(0);
+ assertThat(neg instanceof SqlCall, is(true));
+ SqlNode cast = ((SqlCall) neg).getOperandList().get(0);
+ assertThat(cast.getParserPosition().getLineNum(), is(1));
+ }
+
@Test void testValidateParameterizedExpression() throws SqlParseException {
final SqlParser.Config config = SqlParser.config();
final SqlValidator validator = fixture().factory.createValidator();