Skip to content

[CALCITE-6823] Cannot convert CHAR to Integer when applying SubstitutionVisitor#4974

Open
xuzifu666 wants to merge 7 commits into
apache:mainfrom
xuzifu666:calcite-6823
Open

[CALCITE-6823] Cannot convert CHAR to Integer when applying SubstitutionVisitor#4974
xuzifu666 wants to merge 7 commits into
apache:mainfrom
xuzifu666:calcite-6823

Conversation

@xuzifu666
Copy link
Copy Markdown
Member

/** Test case of
* <a href="https://issues.apache.org/jira/browse/CALCITE-6823">[CALCITE-6823]
* Cannot convert CHAR to Integer when applying SubstitutionVisitor</a>. */
@Test void testDateFilter() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What happens if the string is not a valid date literal?
You should get a decent error, not a crash.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Make sense! I had fixed the issue and added a test about it.

new DateString(requireNonNull(rexLiteral.getValueAs(String.class)))
.getDaysSinceEpoch());
} catch (IllegalArgumentException e) {
LOGGER.warn("Invalid date literal: {}", rexLiteral.getValueAs(String.class), e);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is NULL the correct result in these cases?
Is there a spec for what is supposed to happen?
Maybe you can add some documentation to the function about when it returns null.

Normally the program you are showing below in your test should use type coercion to insert a CAST of the string literal to date, and thus this case should probably be unreachable.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, in my view returning NULL is the correct behavior. According to the specification for getValue() method:

  1. When a value cannot be extracted or is invalid, it returns NULL;
  2. When NULL is returned, any containing optimization (such as materialized view substitution) is skipped;
  3. This is an explicit signal this optimization cannot be applied.

The spec for what is supposed to happen: the SQL validator should have already coerced string literals to DATE type via CAST operations, making this case unreachable. However, in practice, due to type inference rules or edge cases in the validator, such cases can still slip through.
I added comments for cases where NULL is returned.

@sonarqubecloud
Copy link
Copy Markdown

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