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

AST doesn't include the original casing of the NULL and CURRENT_* literals #66

Open
merceyz opened this issue Aug 14, 2024 · 3 comments

Comments

@merceyz
Copy link

merceyz commented Aug 14, 2024

The AST doesn't provide the original casing used for the literals NULL, CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP making it impossible to detect what the column name would be from just the AST.

use fallible_iterator::FallibleIterator;
use sqlite3_parser::lexer::sql::Parser;

pub fn main() {
    let mut parser = Parser::new(b"SELECT nuLL, current_TIME");

    let ast = parser.next().unwrap().unwrap();

    println!("{}", ast);
    // SELECT NULL, CURRENT_TIME;

    println!("{:?}", ast);
    // Stmt(Select(Select { with: None, body: SelectBody { select: Select { distinctness: None, columns: [Expr(Literal(Null), None), Expr(Literal(CurrentTime), None)], from: None, where_clause: None, group_by: None, window_clause: None }, compounds: None }, order_by: None, limit: None }))
}

SQLite itself preserves the casing in the column name:

$ sqlite3 --table
SQLite version 3.46.0 2024-05-23 13:25:27
Enter ".help" for usage hints.
sqlite> SELECT nuLL, current_TIME;
+------+--------------+
| nuLL | current_TIME |
+------+--------------+
|      | 00:00:00     |
+------+--------------+
@gwenn
Copy link
Owner

gwenn commented Aug 14, 2024

This is expected because all keywords are normalized.

Self::CurrentTimestamp => s.append(TK_CTIME_KW, Some("CURRENT_TIMESTAMP")),

You might be able to preserve the original case if we implement #33
But for now I would prefer to keep the memory footprint of the AST as low as possible.

Also the SQLite syntax is case-insensitive.
So your sentence:

making it impossible to detect what the column name would be from just the AST.

seems wrong to me.

sqlite> .headers on
sqlite> CREATE TABLE x(A);
sqlite> INSERT INTO x VALUES ('...');
sqlite> SELECT a FROM x;
A
...```

@merceyz merceyz changed the title AST doesn't include the original casing of the CURRENT_* literals AST doesn't include the original casing of the NULL and CURRENT_* literals Aug 14, 2024
@merceyz
Copy link
Author

merceyz commented Aug 14, 2024

Literals are treated differently, their casing is preserved in the resulting column name.

@gwenn
Copy link
Owner

gwenn commented Aug 14, 2024

I agree but not keywords.
And lemon-rs cannot preserve original column name like SQLite3 because there is no schema access...

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

No branches or pull requests

2 participants