apache / apache/datafusion-sqlparser-rs
Support for `IN $placeholder` syntax
- 主要言語
- Rust
- スター
- 3.5k
- フォーク
- 772
- 平均マージ
- 4日 9時間
- マージ済み PR(30日)
- 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") のケースを実行して失敗を再現します。既存の consumer への影響を抑えながら、IN $placeholder と IN ($placeholder) を区別できる表現を決定し、両方の形式が正しく parse されることを示すテストカバレッジを追加します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- rust, sql
- 領域
- compilers, databases
- issue の種類
- 機能追加
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100