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

Correctly look for end delimiter dollar quoted string #1650

Merged
merged 7 commits into from
Jan 12, 2025

Conversation

hansott
Copy link
Contributor

@hansott hansott commented Jan 7, 2025

Currently the tokenizer throws an error for

SELECT $abc$x$ab$abc$

The logic is also quite difficult to read so I made it a bit simpler.

Before

'searching_for_end: loop {
    s.push_str(&peeking_take_while(chars, |ch| ch != '$'));
    match chars.peek() {
        Some('$') => {
            chars.next();
            let mut maybe_s = String::from("$");
            for c in value.chars() {
                if let Some(next_char) = chars.next() {
                    maybe_s.push(next_char);
                    if next_char != c {
                        // This doesn't match the dollar quote delimiter so this
                        // is not the end of the string.
                        s.push_str(&maybe_s);
                        continue 'searching_for_end;
                    }
                } else {
                    return self.tokenizer_error(
                        chars.location(),
                        "Unterminated dollar-quoted, expected $",
                    );
                }
            }
            if chars.peek() == Some(&'$') {
                chars.next();
                maybe_s.push('$');
                // maybe_s matches the end delimiter
                break 'searching_for_end;
            } else {
                // This also doesn't match the dollar quote delimiter as there are
                // more characters before the second dollar so this is not the end
                // of the string.
                s.push_str(&maybe_s);
                continue 'searching_for_end;
            }
        }
        _ => {
            return self.tokenizer_error(
                chars.location(),
                "Unterminated dollar-quoted, expected $",
            )
        }
    }
}

After

let mut temp = String::new();
let end_delimiter = format!("${}$", value);

loop {
    match chars.next() {
        Some(ch) => {
            temp.push(ch);

            if temp.ends_with(&end_delimiter) {
                s.push_str(&temp[..temp.len() - end_delimiter.len()]);
                break;
            }
        }
        None => {
            if temp.ends_with(&end_delimiter) {
                s.push_str(&temp[..temp.len() - end_delimiter.len()]);
                break;
            }

            return self.tokenizer_error(
                chars.location(),
                "Unterminated dollar-quoted, expected $",
            );
        }
    }
}

Currently the tokenizer throws an error for

```sql
SELECT $abc$x$ab$abc$
```

The logic is also quite difficult to read so I made it a bit simpler.
@hansott
Copy link
Contributor Author

hansott commented Jan 7, 2025

Adding some more tests first

@hansott
Copy link
Contributor Author

hansott commented Jan 9, 2025

@iffyio let me know if see some improvements

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.

Looks good to me overall thanks @hansott! Left one comment regarding the suffix matching

src/tokenizer.rs Outdated
temp.push(ch);

if temp.ends_with(&end_delimiter) {
s.push_str(&temp[..temp.len() - end_delimiter.len()]);
Copy link
Contributor

Choose a reason for hiding this comment

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

ah I think indexing based on String::len won't necessarily produce correct results on arbitrary UTF8 strings, we could something like strip_suffix that doesn't rely on the byte count? e.g

if let Some(temp) = temp.strip_suffix(&end_delimiter) {
    s.push_str(temp);
    break
}

String::len won't necessarily produce correct results on arbitrary UTF8 strings
@hansott
Copy link
Contributor Author

hansott commented Jan 12, 2025

@iffyio Updated, thanks for your 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!
cc @alamb

@iffyio iffyio merged commit c808c4e into apache:main Jan 12, 2025
9 checks passed
@hansott hansott deleted the dollar-quote-error branch January 12, 2025 20:34
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