GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — PostgreSQL: Add support for DO statement by devtechedge · Pull Request #2618 · apache/datafusion-sqlparser-rs Conversation
|
|
Please add examples of this syntax to the fuzzer corpus |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@ ## main #2618 +/- ## ========================================== + Coverage 83.09% 83.11% +0.02% ========================================== Files 42 42 Lines 36261 36328 +67 Branches 36261 36328 +67 ========================================== + Hits 30130 30194 +64 Misses 2636 2636 - Partials 3495 3498 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
bad1741 to
0198d07
Compare
|
@LucaCappelletti94 Good call, the fuzzer should know about this syntax too. I added five seeds under Together they cover a plain fmt, clippy and the full test suite pass with them added. Happy to add or reshape any of them if you had other cases in mind 🙂 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Do { | ||
| /// Optional procedural language name. | ||
| language: Option<Ident>, | ||
| /// The anonymous code block, a string literal. | ||
| code: ValueWithSpan, | ||
| }, |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Refactor this to a named struct, as every new statement variant does. I will do another review round after this refactoring as I expect most of the code will change.
| Do { | |
| /// Optional procedural language name. | |
| language: Option<Ident>, | |
| /// The anonymous code block, a string literal. | |
| code: ValueWithSpan, | |
| }, | |
| Do(DoStatement), |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks @LucaCappelletti94!
I refactored it into a named DoStatement struct (Statement::Do(DoStatement)), with its own Display and From<DoStatement> for Statement, following the pattern of the other statement structs.
I also gave it a Spanned impl covering the language and code block, with a small span test.
Parsing behavior and existing tests are unchanged, and fmt, clippy and the full test suite pass.
Ready for another look whenever you have time.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
f58b626 to
adc68d1
Compare
| match &self.peek_token_ref().token { | ||
| Token::SingleQuotedString(_) | ||
| | Token::DollarQuotedString(_) | ||
| | Token::EscapedStringLiteral(_) => {} |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You should accept the Unicode escape string as a code block too. PostgreSQL 18 runs DO U&'BEGIN NULL; END', and the PostgreSQL dialect tokenizes it as Token::UnicodeStringLiteral, so this match rejects it with Expected: string literal, found: U&'BEGIN NULL; END'.
| | Token::EscapedStringLiteral(_) => {} | |
| | Token::EscapedStringLiteral(_) | |
| | Token::UnicodeStringLiteral(_) => {} |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| _ => unreachable!(), | ||
| } | ||
|
|
||
| dialects.verified_stmt("DO 'BEGIN NULL; END'"); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
And its red test.
| dialects.verified_stmt("DO 'BEGIN NULL; END'"); | |
| dialects.verified_stmt("DO 'BEGIN NULL; END'"); | |
| dialects.verified_stmt("DO U&'BEGIN NULL; END'"); |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks @LucaCappelletti94. I've applied both suggestions: parse_do now accepts Unicode escape strings (U&'...') as the code block, and I added DO U&'BEGIN NULL; END' to parse_do_statement. fmt and the full test suite pass locally. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| pub fn parse_do(&mut self) -> Result<Statement, ParserError> { | ||
| let mut language = self.parse_do_language()?; | ||
| match &self.peek_token_ref().token { | ||
| Token::SingleQuotedString(_) | ||
| | Token::DollarQuotedString(_) | ||
| | Token::EscapedStringLiteral(_) | ||
| | Token::UnicodeStringLiteral(_) => {} | ||
| _ => return self.expected_ref("string literal", self.peek_token_ref()), | ||
| } | ||
| let code = self.parse_value()?; | ||
| if language.is_none() { | ||
| language = self.parse_do_language()?; | ||
| } | ||
| Ok(Statement::Do(DoStatement { language, code })) | ||
| } | ||
|
|
||
| fn parse_do_language(&mut self) -> Result<Option<Ident>, ParserError> { | ||
| if self.parse_keyword(Keyword::LANGUAGE) { | ||
| Ok(Some(self.parse_identifier()?)) | ||
| } else { | ||
| Ok(None) | ||
| } | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You should parse DO the way PostgreSQL's dostmt_opt_list does. That means any run of code blocks and LANGUAGE items, with the name either an identifier or any string literal, and the missing-code and duplicate checks left to the server. The repo's fuzz_postgres_accepts target panics on DO LANGUAGE plpgsql, DO $$a$$ $$b$$, a repeated LANGUAGE and DO LANGUAGE $$plpgsql$$ $$a$$, all of which PostgreSQL's grammar accepts, and fuzzing from this PR's seeds reaches one within seconds.
Please use the fuzzer locally before the next review round to catch these things.
| pub fn parse_do(&mut self) -> Result<Statement, ParserError> { | |
| let mut language = self.parse_do_language()?; | |
| match &self.peek_token_ref().token { | |
| Token::SingleQuotedString(_) | |
| | Token::DollarQuotedString(_) | |
| | Token::EscapedStringLiteral(_) | |
| | Token::UnicodeStringLiteral(_) => {} | |
| _ => return self.expected_ref("string literal", self.peek_token_ref()), | |
| } | |
| let code = self.parse_value()?; | |
| if language.is_none() { | |
| language = self.parse_do_language()?; | |
| } | |
| Ok(Statement::Do(DoStatement { language, code })) | |
| } | |
| fn parse_do_language(&mut self) -> Result<Option<Ident>, ParserError> { | |
| if self.parse_keyword(Keyword::LANGUAGE) { | |
| Ok(Some(self.parse_identifier()?)) | |
| } else { | |
| Ok(None) | |
| } | |
| } | |
| pub fn parse_do(&mut self) -> Result<Statement, ParserError> { | |
| let mut options = Vec::new(); | |
| loop { | |
| let language = self.parse_keyword(Keyword::LANGUAGE); | |
| let string_literal = matches!( | |
| self.peek_token_ref().token, | |
| Token::SingleQuotedString(_) | |
| | Token::DollarQuotedString(_) | |
| | Token::EscapedStringLiteral(_) | |
| | Token::UnicodeStringLiteral(_) | |
| ); | |
| let option = match (language, string_literal) { | |
| (true, true) => DoOption::LanguageLiteral(self.parse_value()?), | |
| (true, false) => DoOption::Language(self.parse_identifier()?), | |
| (false, true) => DoOption::Code(self.parse_value()?), | |
| (false, false) if options.is_empty() => { | |
| return self.expected_ref("string literal", self.peek_token_ref()); | |
| } | |
| (false, false) => break, | |
| }; | |
| options.push(option); | |
| } | |
| Ok(Statement::Do(DoStatement { options })) | |
| } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| pub struct DoStatement { | ||
| /// Optional procedural language name. | ||
| pub language: Option<Ident>, | ||
| /// The anonymous code block, a string literal. | ||
| pub code: ValueWithSpan, | ||
| } | ||
|
|
||
| impl fmt::Display for DoStatement { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "DO ")?; | ||
| if let Some(language) = &self.language { | ||
| write!(f, "LANGUAGE {language} ")?; | ||
| } | ||
| write!(f, "{}", self.code) | ||
| } | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You should store the items in source order, so every accepted form prints back as written.
| pub struct DoStatement { | |
| /// Optional procedural language name. | |
| pub language: Option<Ident>, | |
| /// The anonymous code block, a string literal. | |
| pub code: ValueWithSpan, | |
| } | |
| impl fmt::Display for DoStatement { | |
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
| write!(f, "DO ")?; | |
| if let Some(language) = &self.language { | |
| write!(f, "LANGUAGE {language} ")?; | |
| } | |
| write!(f, "{}", self.code) | |
| } | |
| } | |
| pub struct DoStatement { | |
| /// The code blocks and `LANGUAGE` options, in source order. | |
| pub options: Vec<DoOption>, | |
| } | |
| impl fmt::Display for DoStatement { | |
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
| write!(f, "DO {}", display_separated(&self.options, " ")) | |
| } | |
| } | |
| /// An item of a [`DoStatement`]. | |
| #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | |
| #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | |
| #[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] | |
| pub enum DoOption { | |
| /// `LANGUAGE lang_name` | |
| Language(Ident), | |
| /// `LANGUAGE` followed by the name as a string literal. | |
| LanguageLiteral(ValueWithSpan), | |
| /// The anonymous code block, a string literal. | |
| Code(ValueWithSpan), | |
| } | |
| impl fmt::Display for DoOption { | |
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
| match self { | |
| DoOption::Language(language) => write!(f, "LANGUAGE {language}"), | |
| DoOption::LanguageLiteral(language) => write!(f, "LANGUAGE {language}"), | |
| DoOption::Code(code) => write!(f, "{code}"), | |
| } | |
| } | |
| } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Join, JoinConstraint, JoinOperator, JsonPath, JsonPathElem, LateralView, LimitClause, | ||
| MatchRecognizePattern, Measure, Merge, MergeAction, MergeClause, MergeInsertExpr, | ||
| MergeInsertKind, MergeUpdateExpr, MergeUpdateKind, NamedParenthesizedList, | ||
| CreateTableOptions, Cte, Delete, DoStatement, DoUpdate, ExceptSelectItem, |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You should import the new DoOption.
| CreateTableOptions, Cte, Delete, DoStatement, DoUpdate, ExceptSelectItem, | |
| CreateTableOptions, Cte, Delete, DoOption, DoStatement, DoUpdate, ExceptSelectItem, |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| impl Spanned for DoStatement { | ||
| fn span(&self) -> Span { | ||
| let DoStatement { language, code } = self; | ||
| union_spans( | ||
| language | ||
| .iter() | ||
| .map(|l| l.span) | ||
| .chain(core::iter::once(code.span)), | ||
| ) | ||
| } | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You should span the option list, each item by its name or literal.
| impl Spanned for DoStatement { | |
| fn span(&self) -> Span { | |
| let DoStatement { language, code } = self; | |
| union_spans( | |
| language | |
| .iter() | |
| .map(|l| l.span) | |
| .chain(core::iter::once(code.span)), | |
| ) | |
| } | |
| } | |
| impl Spanned for DoStatement { | |
| fn span(&self) -> Span { | |
| union_spans(self.options.iter().map(|option| option.span())) | |
| } | |
| } | |
| impl Spanned for DoOption { | |
| fn span(&self) -> Span { | |
| match self { | |
| DoOption::Language(language) => language.span, | |
| DoOption::LanguageLiteral(value) | DoOption::Code(value) => value.span, | |
| } | |
| } | |
| } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| match dialects.verified_stmt("DO $$BEGIN RAISE NOTICE 'hi'; END$$") { | ||
| Statement::Do(DoStatement { language, code }) => { | ||
| assert_eq!(language, None); | ||
| assert_eq!( | ||
| code.value, | ||
| Value::DollarQuotedString(DollarQuotedString { | ||
| value: "BEGIN RAISE NOTICE 'hi'; END".to_string(), | ||
| tag: None, | ||
| }) | ||
| ); | ||
| } | ||
| _ => unreachable!(), | ||
| } | ||
|
|
||
| match dialects.verified_stmt("DO LANGUAGE plpgsql $body$BEGIN NULL; END$body$") { | ||
| Statement::Do(DoStatement { language, .. }) => { | ||
| assert_eq!(language, Some(Ident::new("plpgsql"))); | ||
| } | ||
| _ => unreachable!(), | ||
| } | ||
|
|
||
| dialects.verified_stmt("DO 'BEGIN NULL; END'"); | ||
| dialects.verified_stmt("DO U&'BEGIN NULL; END'"); | ||
| dialects.one_statement_parses_to( | ||
| "DO $$BEGIN NULL; END$$ LANGUAGE plpgsql", | ||
| "DO LANGUAGE plpgsql $$BEGIN NULL; END$$", | ||
| ); | ||
| assert_eq!( | ||
| dialects | ||
| .parse_sql_statements("DO LANGUAGE plpgsql $$BEGIN NULL; END$$ LANGUAGE sql") | ||
| .unwrap_err(), | ||
| ParserError::ParserError("Expected: end of statement, found: LANGUAGE".to_string()) | ||
| ); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
More red tests.
| match dialects.verified_stmt("DO $$BEGIN RAISE NOTICE 'hi'; END$$") { | |
| Statement::Do(DoStatement { language, code }) => { | |
| assert_eq!(language, None); | |
| assert_eq!( | |
| code.value, | |
| Value::DollarQuotedString(DollarQuotedString { | |
| value: "BEGIN RAISE NOTICE 'hi'; END".to_string(), | |
| tag: None, | |
| }) | |
| ); | |
| } | |
| _ => unreachable!(), | |
| } | |
| match dialects.verified_stmt("DO LANGUAGE plpgsql $body$BEGIN NULL; END$body$") { | |
| Statement::Do(DoStatement { language, .. }) => { | |
| assert_eq!(language, Some(Ident::new("plpgsql"))); | |
| } | |
| _ => unreachable!(), | |
| } | |
| dialects.verified_stmt("DO 'BEGIN NULL; END'"); | |
| dialects.verified_stmt("DO U&'BEGIN NULL; END'"); | |
| dialects.one_statement_parses_to( | |
| "DO $$BEGIN NULL; END$$ LANGUAGE plpgsql", | |
| "DO LANGUAGE plpgsql $$BEGIN NULL; END$$", | |
| ); | |
| assert_eq!( | |
| dialects | |
| .parse_sql_statements("DO LANGUAGE plpgsql $$BEGIN NULL; END$$ LANGUAGE sql") | |
| .unwrap_err(), | |
| ParserError::ParserError("Expected: end of statement, found: LANGUAGE".to_string()) | |
| ); | |
| match dialects.verified_stmt("DO $$BEGIN RAISE NOTICE 'hi'; END$$") { | |
| Statement::Do(DoStatement { options }) => assert_eq!( | |
| options, | |
| vec![DoOption::Code( | |
| Value::DollarQuotedString(DollarQuotedString { | |
| value: "BEGIN RAISE NOTICE 'hi'; END".to_string(), | |
| tag: None, | |
| }) | |
| .with_empty_span() | |
| )] | |
| ), | |
| _ => unreachable!(), | |
| } | |
| match dialects.verified_stmt("DO LANGUAGE plpgsql $body$BEGIN NULL; END$body$") { | |
| Statement::Do(DoStatement { options }) => { | |
| assert_eq!(options[0], DoOption::Language(Ident::new("plpgsql"))); | |
| } | |
| _ => unreachable!(), | |
| } | |
| dialects.verified_stmt("DO 'BEGIN NULL; END'"); | |
| dialects.verified_stmt("DO U&'BEGIN NULL; END'"); | |
| dialects.verified_stmt("DO $$BEGIN NULL; END$$ LANGUAGE plpgsql"); | |
| dialects.verified_stmt("DO LANGUAGE 'plpgsql' $$BEGIN NULL; END$$"); | |
| dialects.verified_stmt("DO LANGUAGE $$plpgsql$$ $$BEGIN NULL; END$$"); | |
| dialects.verified_stmt("DO $$BEGIN NULL; END$$ LANGUAGE E'plpgsql'"); | |
| dialects.verified_stmt("DO LANGUAGE plpgsql"); | |
| dialects.verified_stmt("DO $$BEGIN NULL; END$$ $$BEGIN NULL; END$$"); | |
| dialects.verified_stmt("DO LANGUAGE plpgsql LANGUAGE sql $$BEGIN NULL; END$$"); |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks, both reviews are addressed in 4e846e6.
I added all your test cases. I ran What it did turn up are older issues elsewhere:
Happy to look at those in separate PRs. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
4e846e6 to
3ce05be
Compare
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Also add this red test in the postgres test suite. Please do check whether I missed any other DO related syntaxes, I am not omniscient.
dialects.verified_stmt("DO LANGUAGE plpgsql LANGUAGE sql $$BEGIN NULL; END$$"); dialects.one_statement_parses_to( "DO LANGUAGE N'BEGIN NULL; END'", "DO LANGUAGE nchar 'BEGIN NULL; END'", );
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ); | ||
| let option = match (language, string_literal) { | ||
| (true, true) => DoOption::LanguageLiteral(self.parse_value()?), | ||
| (true, false) => DoOption::Language(self.parse_identifier()?), |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You should read LANGUAGE N'...' the way PostgreSQL does, as the language nchar followed by a code block. PostgreSQL's scanner splits N'...' into the keyword NCHAR and a string, and NCHAR is a non-reserved word, so DO LANGUAGE N'BEGIN NULL; END' passes pg_query. fuzz_postgres_accepts panics on it with Expected: identifier, found: N'BEGIN NULL; END', reached from this PR's seeds in about two minutes.
| (true, false) => DoOption::Language(self.parse_identifier()?), | |
| (true, false) => match &self.peek_token_ref().token { | |
| // PostgreSQL lexes `N'...'` as the keyword `NCHAR` followed by a string | |
| Token::NationalStringLiteral(code) => { | |
| let code = Value::SingleQuotedString(code.clone()); | |
| let span = self.next_token().span; | |
| options.push(DoOption::Language(Ident::with_span(span, "nchar"))); | |
| DoOption::Code(code.with_span(span)) | |
| } | |
| _ => DoOption::Language(self.parse_identifier()?), | |
| }, |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thank you for the detailed review and for the suggested fix, I really appreciate it. I will add the test in the Postgres suite and implement the I will also check for any other So I can plan my time well, could you please give me a rough idea of how many more rounds of changes you expect for this PR? I am very happy to keep iterating until you are satisfied with it, and a rough sense of the remaining scope will simply help me plan and make sure I cover everything carefully. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Absolutely no idea, until we can't find any fault in it anymore I guess. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@LucaCappelletti94, I've pushed the 5th round of changes for this PR. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
b205a41 to
84477aa
Compare
|
Hi @LucaCappelletti94, I've rebased this onto the latest main and resolved the conflicts. The round-5 changes from your last review are in, and fmt, clippy and tests all pass. Whenever you have a moment, could you take another look? Thanks! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
36210f5 to
bbf088f
Compare
| (true, false) | ||
| if matches!(self.peek_token_ref().token, Token::NationalStringLiteral(_)) => | ||
| { | ||
| // PostgreSQL lexes `N'...'` as the `NCHAR` keyword followed | ||
| // by a string literal, so `LANGUAGE N'...'` is the language | ||
| // name `nchar` followed by the code block. | ||
| let next_token = self.next_token(); | ||
| let span = next_token.span; | ||
| let Token::NationalStringLiteral(code) = next_token.token else { | ||
| return self.expected("national string literal", next_token); | ||
| }; | ||
| options.push(DoOption::Language(Ident::with_span(span, "nchar"))); | ||
| DoOption::Code(Value::SingleQuotedString(code).with_span(span)) | ||
| } | ||
| (true, false) => DoOption::Language(self.parse_identifier()?), |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You could take the token once and step back when it is not N'...'. The guard already proved the token is a NationalStringLiteral, so the else arm returning expected("national string literal", ...) can never run.
| (true, false) | |
| if matches!(self.peek_token_ref().token, Token::NationalStringLiteral(_)) => | |
| { | |
| // PostgreSQL lexes `N'...'` as the `NCHAR` keyword followed | |
| // by a string literal, so `LANGUAGE N'...'` is the language | |
| // name `nchar` followed by the code block. | |
| let next_token = self.next_token(); | |
| let span = next_token.span; | |
| let Token::NationalStringLiteral(code) = next_token.token else { | |
| return self.expected("national string literal", next_token); | |
| }; | |
| options.push(DoOption::Language(Ident::with_span(span, "nchar"))); | |
| DoOption::Code(Value::SingleQuotedString(code).with_span(span)) | |
| } | |
| (true, false) => DoOption::Language(self.parse_identifier()?), | |
| (true, false) => match self.next_token() { | |
| // PostgreSQL lexes `N'...'` as the `NCHAR` keyword followed | |
| // by a string literal, so `LANGUAGE N'...'` is the language | |
| // name `nchar` followed by the code block. | |
| TokenWithSpan { | |
| token: Token::NationalStringLiteral(code), | |
| span, | |
| } => { | |
| options.push(DoOption::Language(Ident::with_span(span, "nchar"))); | |
| DoOption::Code(Value::SingleQuotedString(code).with_span(span)) | |
| } | |
| _ => { | |
| self.prev_token(); | |
| DoOption::Language(self.parse_identifier()?) | |
| } | |
| }, |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| match self { | ||
| CreateTableOptions::None => Span::empty(), | ||
| CreateTableOptions::With(vec) => union_spans(vec.iter().map(|i| i.span())), | ||
| CreateTableOptions::Options(vec) => { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is this a clippy code smell or what is its reason?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ); | ||
|
|
||
| for i in 0..EXPECTED_PROJ_QTY { | ||
| for proj in select.projection.iter().take(EXPECTED_PROJ_QTY) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is this another clippy code smell?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Accept any run of code blocks and LANGUAGE items, with the language name as an identifier or any string literal, and keep them in source order so every accepted form round-trips. Missing-code and duplicate checks are left to the server, matching PostgreSQL's grammar.
bbf088f to
bb6e1aa
Compare
Add the forms that PostgreSQL also accepts for a Unicode escape string around a LANGUAGE clause and for a LANGUAGE name given as a string literal.
There was an error while loading. Please reload this page.
|
Thanks for the careful pass, and for catching the dead arm before another round. The parser point was a real defect, so I took the token once and stepped back when it is not The other two comments were my own scope creep, not code: a local clippy run had rewritten a redundant I rebased onto main as well, since the branch had conflicts. I also compared thirty
If you would prefer the two clippy lines handled in a separate PR, tell me and I will move them out. 🙂 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| options.push(DoOption::Language(Ident::with_span(span, "nchar"))); | ||
| DoOption::Code(Value::SingleQuotedString(code).with_span(span)) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You should split the N'...' token's span between the two items it produces. Both currently get the whole token's span, so get_source on the nchar language returns N'BEGIN NULL; END' instead of N. N is always a single character on the token's first line, so the quote starts one column later.
| options.push(DoOption::Language(Ident::with_span(span, "nchar"))); | |
| DoOption::Code(Value::SingleQuotedString(code).with_span(span)) | |
| let quote = Location::new(span.start.line, span.start.column + 1); | |
| let language = Ident::with_span(Span::new(span.start, quote), "nchar"); | |
| options.push(DoOption::Language(language)); | |
| let code = Value::SingleQuotedString(code); | |
| DoOption::Code(code.with_span(Span::new(quote, span.end))) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| assert_eq!(test.get_source(r[0].span()), "plpgsql $$BEGIN NULL; END$$"); | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You should cover the split, since this test fails on the current head because the language's span covers the whole literal.
| assert_eq!(test.get_source(r[0].span()), "plpgsql $$BEGIN NULL; END$$"); | |
| } | |
| assert_eq!(test.get_source(r[0].span()), "plpgsql $$BEGIN NULL; END$$"); | |
| let sql = "DO LANGUAGE N'BEGIN NULL; END'"; | |
| let test = SpanTest::new(dialect, sql); | |
| let options = match Parser::parse_sql(dialect, sql).unwrap().remove(0) { | |
| Statement::Do(DoStatement { options }) => options, | |
| stmt => panic!("expected DO, got {stmt:?}"), | |
| }; | |
| assert_eq!(test.get_source(options[0].span()), "N"); | |
| assert_eq!(test.get_source(options[1].span()), "'BEGIN NULL; END'"); | |
| } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Share the token span between the nchar language name and the code block it produces, so get_source returns N and the literal separately. Cover the split in the DO span test.
|
You were right about the span, and I appreciate you taking the time to point at the exact columns. 👍 Both points are addressed in commit 006a559. The span point is a real bug, and the test point is the coverage that should have come with it. The language name now runs from the start of the token to the opening quote, so The span test covers both halves, with one assertion for I checked the new assertion against the previous commit first, and it fails there, so the test pins the split rather than only passing with the fix. The formatting check, clippy with I am happy to reshape the offset or the test if you had something else in mind. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thank you for refining the PR @devtechedge, I will be merging this now. cc @alamb |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Hi Luca, thanks for guiding this PR through so many detailed review rounds. Matching PostgreSQL's dostmt_opt_list more closely made the parser truer to what people actually write inside DO blocks, and splitting the N string token span was a sharp catch 🔧. I also appreciated the habit of running the fuzz harness before each round, which caught issues well before they mattered. Thanks again for the merge. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
PostgreSQL DO statement
There was an error while loading. Please reload this page.