apache / apache/datafusion-sqlparser-rs

Unexpected failure parsing SQLite CREATE TABLE statement

Open
#322 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
3.5k
Forks
772
Avg merge
4d 9h
Merged PRs (30d)
17

Description

When parsing the following statement with the SQLite dialect:

```sql
CREATE TABLE '""' (
'id' INT UNSIGNED NOT NULL,
'name' TEXT NOT NULL,
'surname' TEXT NULL,
'zip' INT UNSIGNED NULL
);
```

the result is:

`ParserError("Expected column name or constraint definition, found: 'id'")`

but I expect that this would be correctly parsed as a CREATE TABLE statement with `""` as the table name. From the SQL grammar, the table name in a CREATE TABLE statement appears to be a [local or schema qualified name](https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#local-or-schema-qualified-name). It's not actually clear to me if using single quotes to delimit a table name is valid according to that description of the grammar, but SQLite does accept it. For reference, [here](https://www.sqlite.org/lang_createtable.html) is the SQLite documentation for CREATE TABLE.

This is definitely an uncommon edge case. I found it in the [Standardized corpus for SQLite Database Forensics](https://www.sciencedirect.com/science/article/pii/S1742287618300471) (accessible [here](https://faui1-files.cs.fau.de/public/sqlite-forensic-corpus/)) in sample 01-01. (For context, I'm using that collection as a set of test cases for a personal project I'm working on which uses this library.)

Full repro using sqlparser 0.9.0:

```rust
fn main() {
let statement = "CREATE TABLE '\"\"' (\n\t'id' INT UNSIGNED NOT NULL,\n\t'name' TEXT NOT NULL,\n\t'surname' TEXT NULL,\n\t'zip' INT UNSIGNED NULL\n)";
let dialect = sqlparser::dialect::SQLiteDialect {};
let parsed = sqlparser::parser::Parser::parse_sql(&dialect, statement).unwrap();
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the SQLiteDialect and Parser::parse_sql entry points, using the supplied CREATE TABLE statement as the reproduction. Trace how quoted table and column names are parsed, then add coverage showing that this statement is accepted as a CREATE TABLE statement with the expected table name.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sqlite
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.