apache / apache/datafusion-sqlparser-rs

Support for `IN $placeholder` syntax

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

描述

Some DBs support using placeholders for `IN` clauses in prepared statements. For example in DuckDB:

```
$ duckdb
DuckDB v1.3.0 (Ossivalis) 71c5c07cdd
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
D PREPARE qry AS SELECT 'a' in $list;
D EXECUTE qry(list := ['a', 'b']);
┌──────────────────────┐
│ contains($list, 'a') │
│ boolean │
├──────────────────────┤
│ true │
└──────────────────────┘
```

However, currently the parser doesn't handle this, for example:

```rust
#[test]
fn test_parse_in_placeholder() {
let stmt = all_dialects().verified_stmt("SELECT i IN $placeholder");
dbg!(&stmt);
}
```

Fails w/ `SELECT i IN $placeholder: ParserError("Expected: (, found: $placeholder")`.

To fix this, I think we would need to make two changes:

First off, the definition of `Expr::InList`:

```rust
/// `[ NOT ] IN (val1, val2, ...)`
InList {
expr: Box,
list: Vec,
negated: bool,
},
```

The issue is that `list` is always a Vec, where in this case we want list to be a `Expr::Value` w/ `value = Placeholder("$placeholder")`.

If `InList` supports that, then in `parse_in` we can do a check like this before the `expect_token(LParen)`:

```rust
if let Token::Placeholder(_) = &self.peek_token_ref().token {
let placeholder = self.parse_expr()?;
return Ok(Expr::InList {
expr: Box::new(expr),
list: placeholder,
negated,
})
};
self.expect_token(&Token::LParen)?;
```

But, how can we cleanly support this, without too much breakage for existing consumers? Note that I considered just putting the placeholder inside the list, but that doesn't work since that would represent `IN ($placeholder)` which has a very different meaning.

贡献指南

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

调研方向

从 Expr::InList 定义和 parser 的 parse_in 逻辑入手,然后运行提供的 all_dialects().verified_stmt("SELECT i IN $placeholder") 用例来复现故障。确定一种能够区分 IN $placeholder 和 IN ($placeholder) 的表示方式,同时限制对现有 consumer 的破坏,并添加覆盖以表明这两种形式都能正确解析。

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

评估

技术栈
rust, sql
领域
compilers, databases
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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