Skip to content

fix(sqlparser): typed string must be single quoted #17482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 28, 2024

Conversation

xiangjinwu
Copy link
Contributor

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

See #17461. Somehow a user wrote the invalid SQL Date t::date but it was interpreted by our sqlparser as (Date 't')::date and resulting in evaluation error.

The root cause is upstream apache/datafusion-sqlparser-rs#235 support of HiveSQL where an unquoted identifier can be treated as a literal string value. It is never the case for PostgreSQL.

After the fix, we report the following error message:

dev=> select date t::date from generate_series('2024-01-01 00:00:00+00'::timestamptz, now(), interval '1 month') as s(t);
ERROR:  Failed to run the query

Caused by:
  sql parser error: expected end of statement, found: ::
LINE 1: select date t::date from generate_series('2024-01-01 00:00:00+00'::timestamptz, now(), interval '1 month') as s(t);
                     ^

which is similar to PostgreSQL:

test=# select date t::date from generate_series('2024-01-01 00:00:00+00'::timestamptz, now(), interval '1 month') as s(t);
ERROR:  syntax error at or near "::"
LINE 1: select date t::date from generate_series('2024-01-01 00:00:0...
                     ^

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added test labels as necessary. See details.
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • My PR contains breaking changes. (If it deprecates some features, please create a tracking issue to remove them in the future).
  • All checks passed in ./risedev check (or alias, ./risedev c)
  • My PR changes performance-critical code. (Please run macro/micro-benchmarks and show the results.)
  • My PR contains critical fixes that are necessary to be merged into the latest release. (Please check out the details)

Documentation

  • My PR needs documentation updates. (Please use the Release note section below to summarize the impact on users)

Release note

If this PR includes changes that directly affect users or other significant modifications relevant to the community, kindly draft a release note to provide a concise summary of these changes. Please prioritize highlighting the impact these changes will have on users.

@xiangjinwu xiangjinwu added the need-cherry-pick-release-1.10 [⚠️DEPRECATED] Open a cherry-pick PR to branch release-1.10 after the current PR is merged label Jun 27, 2024
@github-actions github-actions bot added the type/fix Type: Bug fix. Only for pull requests. label Jun 27, 2024
@@ -11,7 +11,7 @@
- input: SHOW TABLES FROM t
formatted_sql: SHOW TABLES FROM t
formatted_ast: 'ShowObjects { object: Table { schema: Some(Ident { value: "t", quote_style: None }) }, filter: None }'
- input: SHOW TABLES FROM t LIKE "t%"
- input: SHOW TABLES FROM t LIKE 't%'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Strictly speaking this fix is a breaking change. But it was wrong syntax and our documentation has been using single quotes correctly. The impact shall be negligible.

@xiangjinwu xiangjinwu requested review from stdrc and lmatz June 27, 2024 08:42
@xiangjinwu
Copy link
Contributor Author

create function int_42() returns int as int_42 using link 'http://localhost:8815';

This as int_42 has been mis-parsed as SingleQuotedDef

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum FunctionDefinition {
SingleQuotedDef(String),
DoubleDollarDef(String),
}

cc @wangrunji0408 Shall we introduce a new variant for (unquoted or double quoted) identifier?

Copy link
Member

@stdrc stdrc left a comment

Choose a reason for hiding this comment

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

LGTM!

@wangrunji0408
Copy link
Contributor

wangrunji0408 commented Jun 28, 2024

cc @wangrunji0408 Shall we introduce a new variant for (unquoted or double quoted) identifier?

added

Actually I think the string after as should be quoted. But for compatibility, it's better not to deprecate the support for unquoted identifier.

@xiangjinwu xiangjinwu added this pull request to the merge queue Jun 28, 2024
alt((
single_quoted_string.map(FunctionDefinition::SingleQuotedDef),
dollar_quoted_string.map(FunctionDefinition::DoubleDollarDef),
Self::parse_identifier.map(|i| FunctionDefinition::Identifier(i.value)),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I said (unquoted or double quoted) identifier I was thinking of .real_value() but now I realize .value has better backward compatibility:

When the SQL to parse is as camelCase, the old behavior gets camelCase but the identifier's real_value is camelcase. Although as "camelCase" is a case-sensitive identifier, I agree it is better to encourage as 'camelCase' if breaking change is inevitable.

Merged via the queue into main with commit de8903f Jun 28, 2024
29 of 30 checks passed
@xiangjinwu xiangjinwu deleted the fix-sqlparser-typed-literal branch June 28, 2024 08:17
github-actions bot pushed a commit that referenced this pull request Jun 28, 2024
github-merge-queue bot pushed a commit that referenced this pull request Jun 28, 2024
Signed-off-by: Runji Wang <[email protected]>
Co-authored-by: xiangjinwu <[email protected]>
Co-authored-by: Runji Wang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-cherry-pick-release-1.10 [⚠️DEPRECATED] Open a cherry-pick PR to branch release-1.10 after the current PR is merged type/fix Type: Bug fix. Only for pull requests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants