apache / apache/datafusion-sqlparser-rs

A bracket-quoted identifier containing `]]` round-trips to SQL that fails to reparse

未关闭
#2,409 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Rust
星标
3.5k
派生
772
平均合并
4 天 9 小时
30 天内合并 PR
17

描述

```rust
use sqlparser::dialect::MsSqlDialect;
use sqlparser::parser::Parser;

fn main() {
let sql = "SELECT [a]]b]";
let ast = Parser::parse_sql(&MsSqlDialect {}, sql).unwrap();
let printed = ast[0].to_string();
println!("input: {}", sql);
println!("printed: {}", printed);
let reparsed = Parser::parse_sql(&MsSqlDialect {}, &printed);
println!("reparsed: {:?}", reparsed);
}
```

```
input: SELECT [a]]b]
printed: SELECT [a]b]
reparsed: Err(ParserError("Expected: end of statement, found: ] at Line: 1, Column: 12"))
```

`[a]]b]` is a bracket-quoted identifier whose value is `a]b` (the tokenizer folds the doubled `]]` into a literal `]`). Displaying the parsed AST back to SQL writes `[a]b]` instead, and that string does not parse as the same identifier: it parses as `a` followed by leftover tokens `]b]`.

Tested on sqlparser 0.62.0.

BTW, this bug was found using [hegel](https://crates.io/crates/hegeltest). Happy to contribute the tests if you're interested.

贡献指南

这个仓库没有索引到贡献指南

调研方向

使用 MsSqlDialect、Parser::parse_sql 和 AST to_string,通过 Rust 示例复现该问题。检查 tokenizer 如何处理成对的方括号,以及解析后的标识符如何显示,然后添加一个往返回归测试,证明 [a]]b] 会重新解析为标识符 a]b,并运行相关测试套件。

由索引模型根据 Issue 内容生成。

评估

技术栈
rust
领域
databases
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
描述清楚
新手友好度
68/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。