[
https://issues.apache.org/jira/browse/CALCITE-7075?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17986570#comment-17986570
]
Maksim Zhuravkov commented on CALCITE-7075:
-------------------------------------------
It looks like checkExpressionPadTruncate first multiplies a number by
interval.multiplier, and then scaleValue does the same.
So adding _sourceFamily != SqlTypeFamily.NUMERIC_ should fix the issue
{noformat}
if (sourceFamily != SqlTypeFamily.NUMERIC
&& SqlTypeName.INTERVAL_TYPES.contains(targetType.getSqlTypeName())
&& !SqlTypeName.INTERVAL_TYPES.contains(sourceType.getSqlTypeName())) {
{noformat}
At least locally expressions seem to work:
{code:java}
String statement = "SELECT " +
"CAST(CHAR_LENGTH('abc') AS INTERVAL DAY)," +
"CAST(1 AS INTERVAL DAY), " +
"CAST(INTERVAL 4 YEAR AS INTERVAL MONTH)," +
"CAST(INTERVAL 3 DAY AS INTERVAL HOUR)," +
"CAST(INTERVAL 2 HOUR AS INTERVAL SECOND)" +
"";
try (ResultSet rs = conn.prepareStatement(statement).executeQuery()) {
rs.next();
System.err.println(rs.getString(1));
System.err.println(rs.getString(2));
System.err.println(rs.getString(3));
System.err.println(rs.getString(4));
System.err.println(rs.getString(5));
}
conn.close();
{code}
Results:
{noformat}
+3
+1
+48
+72
+7200.000000
{noformat}
> Incorrect code generation for CAST int_returning_func AS INTERVAL DAY
> ---------------------------------------------------------------------
>
> Key: CALCITE-7075
> URL: https://issues.apache.org/jira/browse/CALCITE-7075
> Project: Calcite
> Issue Type: Bug
> Affects Versions: 1.40.0
> Reporter: Maksim Zhuravkov
> Priority: Major
>
> Reproducer
> {noformat}
> // JdbcTest
> @Test
> void testCastIntToIntervalDays() throws Exception {
> String hsqldbMemUrl = "jdbc:hsqldb:mem:.";
> Properties info = new Properties();
> info.put("model",
> "inline:"
> + "{\n"
> + " version: '1.0',\n"
> + " defaultSchema: 'BASEJDBC',\n"
> + " schemas: [\n"
> + " {\n"
> + " type: 'jdbc',\n"
> + " name: 'BASEJDBC',\n"
> + " jdbcDriver: '" + jdbcDriver.class.getName() + "',\n"
> + " jdbcUrl: '" + hsqldbMemUrl + "',\n"
> + " jdbcCatalog: null,\n"
> + " jdbcSchema: null\n"
> + " }\n"
> + " ]\n"
> + "}");
> try (Connection conn = DriverManager.getConnection("jdbc:calcite:",
> info)) {
> String statement = "SELECT CAST(RAND_INTEGER(10) AS INTERVAL DAY),
> CAST(1 AS INTERVAL DAY)";
> try (ResultSet rs = conn.prepareStatement(statement).executeQuery()) {
> rs.next();
> System.err.println(rs.getString(1)); // + some multiple of 86400000
> System.err.println(rs.getString(2));// + 1
> }
> }
> }
> {noformat}
> The intermediate result of the INT-to-DAY conversion for the first expression
> is multiplied by the number of milliseconds twice:
> {noformat}
> {
> final org.apache.calcite.linq4j.Enumerable _inputEnumerable =
> org.apache.calcite.linq4j.Linq4j.asEnumerable(new Integer[] {
> 0});
> return new org.apache.calcite.linq4j.AbstractEnumerable(){
> public org.apache.calcite.linq4j.Enumerator<Object[]> enumerator() {
> return new org.apache.calcite.linq4j.Enumerator<Object[]>(){
> public final org.apache.calcite.linq4j.Enumerator<int>
> inputEnumerator = _inputEnumerable.enumerator();
> public void reset() {
> inputEnumerator.reset();
> }
> public boolean moveNext() {
> return inputEnumerator.moveNext();
> }
> public void close() {
> inputEnumerator.close();
> }
> public Object current() {
> return new Object[] {
>
> ((Number)org.apache.calcite.linq4j.tree.Primitive.of(long.class).numberValueRoundDown(($L4J$C$new_org_apache_calcite_runtime_RandomFunction_.randInteger(10)))).longValue()
> * 86400000 * 86400000,
> 86400000L};
> }
> static final org.apache.calcite.runtime.RandomFunction
> $L4J$C$new_org_apache_calcite_runtime_RandomFunction_ = new
> org.apache.calcite.runtime.RandomFunction();
> };
> }
> };
> }
> {noformat}
> The issue is reproducible on both the main and the latest release so far
> (1.40).
--
This message was sent by Atlassian Jira
(v8.20.10#820010)