[
https://issues.apache.org/jira/browse/CALCITE-7075?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17986816#comment-17986816
]
Maksim Zhuravkov commented on CALCITE-7075:
-------------------------------------------
Consider expressions (1) and (2), they are equivalent but they produce
different results:
1) CAST(char_length('1') AS INTERVAL DAY) // char_length('1') is 1
2) CAST(1 AS INTERVAL DAY)
> 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)