Skip to content

feat: Quote column names if required in error messages#5778

Merged
alamb merged 4 commits intoapache:mainfrom
alamb:alamb/quite_identifier
Apr 5, 2023
Merged

feat: Quote column names if required in error messages#5778
alamb merged 4 commits intoapache:mainfrom
alamb:alamb/quite_identifier

Conversation

@alamb
Copy link
Contributor

@alamb alamb commented Mar 29, 2023

Which issue does this PR close?

Closes #5523
Closes #5625

Rationale for this change

Error messages are hard to read with so many unecessary "

What changes are included in this PR?

This work is based off of @Weijun-H 's great start in #5625 (🙏 )

  1. Improve error messages by only quoting column names if necessary
  2. Add test for quote_identifier

Are these changes tested?

Yes -- both existing tests and new tets

Are there any user-facing changes?

"Schema error: Schema contains duplicate \
qualified field name \"t1\".\"c0\"",
&format!("{}", join.err().unwrap())
join.unwrap_err().to_string(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I cleaned these tests up to use a more standard unwrap_err() and to_string() pattern

field name \"t1\".\"c0\" and unqualified field name \"c0\" which would be ambiguous",
&format!("{}", join.err().unwrap())
);
assert_contains!(join.unwrap_err().to_string(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I switched to assert_contains as that includes the expected and actual in the message where assert!(..contains()) just says "false" when it fails

}

#[test]
fn test_quote_identifier() -> Result<()> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test verifies that identifiers are properly quoted (and that when parsed, quoted identifiers are the same)

pub fn quote_identifier(s: &str) -> String {
format!("\"{}\"", s.replace('"', "\"\""))
pub fn quote_identifier(s: &str) -> Cow<str> {
if needs_quotes(s) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the code change

@github-actions github-actions bot added core Core DataFusion crate logical-expr Logical plan and expressions sql SQL Planner sqllogictest SQL Logic Tests (.slt) labels Mar 29, 2023
@alamb alamb marked this pull request as ready for review March 29, 2023 18:25
@alamb alamb closed this Mar 29, 2023
@alamb alamb reopened this Mar 29, 2023
@alamb
Copy link
Contributor Author

alamb commented Apr 3, 2023

@Jefffrey and @Weijun-H I wonder if you have time to review this PR and if so what do you think about the approach (especially compared to #5625)?

Comment on lines +171 to +176
if needs_quotes(s) {
Cow::Owned(format!("\"{}\"", s.replace('"', "\"\"")))
} else {
Cow::Borrowed(s)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Nice refactoring!

@Weijun-H
Copy link
Member

Weijun-H commented Apr 3, 2023

LGTM

Copy link
Contributor

@Jefffrey Jefffrey left a comment

Choose a reason for hiding this comment

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

Thanks, looks good

@alamb alamb force-pushed the alamb/quite_identifier branch from 2c188a7 to e8f6a4a Compare April 4, 2023 20:29
@alamb alamb merged commit a1c60a1 into apache:main Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate logical-expr Logical plan and expressions sql SQL Planner sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quote column only if has special characters

3 participants