Skip to content

[CALCITE-5987] SqlImplementor loses type information for literals - #5108

Open
mihaibudiu wants to merge 1 commit into
apache:mainfrom
mihaibudiu:issue5987
Open

[CALCITE-5987] SqlImplementor loses type information for literals#5108
mihaibudiu wants to merge 1 commit into
apache:mainfrom
mihaibudiu:issue5987

Conversation

@mihaibudiu

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-5987

Changes Proposed

Added an option to SqlImplementor that enables it to preserve types for literals. For example, today a literal emitted as 5.0 could be a DOUBLE, REAL, FLOAT, or DECIMAL. With this flag the output would be CAST(5.0 AS REAL), making the type explicit.

There is a new subclass of RelToSelConverter called RelToSqlConverterRoundTrip which executes all the tests for RelToSql for the Calcite dialect by checking that with this change the produced SQL has enough information to reconstruct the "original" program.

@Dwrite

Dwrite commented Jul 21, 2026

Copy link
Copy Markdown

@mihaibudiu need fix conflict first

Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
@mihaibudiu

Copy link
Copy Markdown
Contributor Author

I have fixed the conflicts. This is a "safe" change, since it adds a new configuration flag to SqlImplementer. If the flag is not set the behavior is unchanged. As discussed briefly in Jira (a long time ago) I think this is actually a very useful feature: it will allow you to use RelToSql and then SqlToRel and get back the same program (almost always, sometimes RelToSql does some weird rewrites). This is not happening today for many programs.

@sonarqubecloud

Copy link
Copy Markdown

case NULL:
case ANY:
case UNKNOWN:
// No valid SQL syntax for casts to these types

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for NULL/ANY/UNKNOWN we just return the literal as-is since there's no valid CAST syntax. Are these types actually reachable for a RexLiteral here, or is this purely defensive? Also, does the round-trip test suite cover a bare top-level NULL literal (not nested in a ROW) to confirm we're not silently losing type info on that path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect the NULL case is reachable, e.g., in a query like SELECT NULL, which is legal.
There are actually about 500+ round-trip tests added: the subclass RelToSqlConverterRoundTripTest runs almost every test from RelToSqlConverterTest. There are several tests that contain a SELECT NULL pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants