Skip to content
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

SQLite: Allow dollar signs in placeholder names #1620

Merged
merged 3 commits into from
Dec 28, 2024

Conversation

hansott
Copy link
Contributor

@hansott hansott commented Dec 26, 2024

Relevant: #1402

Screenshot 2024-12-26 at 14 46 58

Not sure if this is the best way to fix, open for feedback.

Relevant: apache#1402

SQLite version 3.43.2 2023-10-10 13:08:14
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .mode box
sqlite> .nullvalue NULL
sqlite> SELECT $$, $$ABC$$, $ABC$, $ABC;
┌──────┬─────────┬───────┬──────┐
│  $$  │ $$ABC$$ │ $ABC$ │ $ABC │
├──────┼─────────┼───────┼──────┤
│ NULL │ NULL    │ NULL  │ NULL │
└──────┴─────────┴───────┴──────┘
@hansott
Copy link
Contributor Author

hansott commented Dec 26, 2024

cc @iffyio @alamb

@hansott hansott changed the title SQLite: Allow dollar signs in placeholder names Fix SQLite Unterminated dollar-quoted string (Allow dollar signs in placeholder names) Dec 26, 2024
@hansott hansott changed the title Fix SQLite Unterminated dollar-quoted string (Allow dollar signs in placeholder names) Fix SQLite Error Unterminated dollar-quoted string (Allow dollar signs in placeholder names) Dec 26, 2024
@@ -636,6 +636,10 @@ pub trait Dialect: Debug + Any {
false
}

fn supports_dollar_quoted_string(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's better to add some documents for this method. (Just like others).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added!

Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

Thanks @hansott! The changes look reasonable to me overall, left some comments

src/tokenizer.rs Outdated
Comment on lines 1526 to 1528
// Check if the second character is a dollar sign
let next_is_dollar = matches!(chars.peek(), Some('$'));
if next_is_dollar && self.dialect.supports_dollar_quoted_string() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Check if the second character is a dollar sign
let next_is_dollar = matches!(chars.peek(), Some('$'));
if next_is_dollar && self.dialect.supports_dollar_quoted_string() {
// If the dialect does not support dollar-quoted string, then `$$` is rather a placeholder.
if matches!(chars.peek(), Some('$')) && self.dialect.supports_dollar_quoted_string() {

if that makes sense, figured we could mention in the comment what the condition implies?

src/tokenizer.rs Outdated
Comment on lines 1567 to 1568
let next_is_dollar = matches!(chars.peek(), Some('$'));
if next_is_dollar && self.dialect.supports_dollar_quoted_string() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let next_is_dollar = matches!(chars.peek(), Some('$'));
if next_is_dollar && self.dialect.supports_dollar_quoted_string() {
if matches!(chars.peek(), Some('$')) && self.dialect.supports_dollar_quoted_string() {

src/tokenizer.rs Outdated
@@ -2604,6 +2609,30 @@ mod tests {
);
}

#[test]
fn tokenize_dollar_placeholder_sqlite() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn tokenize_dollar_placeholder_sqlite() {
fn tokenize_dollar_placeholder() {

@@ -636,6 +636,12 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if this dialect allows dollar quoted strings
/// e.g. `SELECT $$Hello, world!$$` or `SELECT $tag$Hello, world!$tag$`
fn supports_dollar_quoted_string(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wondering would it make more sense to flip the condition to e.g. supports_dollar_placeholder(), setting that to true for sqlite and default false for the others? Thinking that might be the more conservative change of the two given that its the placeholder behavior that is more subtle/specific, (most of the other dialects technically don't support dollar quoted string so its less misleading if they don't have to explicitly flag otherwise)

@iffyio iffyio changed the title Fix SQLite Error Unterminated dollar-quoted string (Allow dollar signs in placeholder names) SQLite: Allow dollar signs in placeholder names Dec 27, 2024
@hansott
Copy link
Contributor Author

hansott commented Dec 27, 2024

@iffyio Addressed all your comments, great feedback 🥇

Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks @hansott!

@iffyio iffyio merged commit 48f025f into apache:main Dec 28, 2024
8 checks passed
@hansott hansott deleted the patch-dollar-sign-sqlite branch December 28, 2024 13:29
hansott added a commit to AikidoSec/datafusion-sqlparser-rs that referenced this pull request Dec 28, 2024
@alamb
Copy link
Contributor

alamb commented Dec 28, 2024

@iffyio Addressed all your comments, great feedback 🥇

@iffyio ++ for sure

Thanks again for all your work reviewing PRs

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.

4 participants