apache / apache/datafusion-sqlparser-rs

[PG Dialect] `SELECT ARRAY(SELECT 1)` fails to parse

未關閉
#400 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Rust
星號
3.5k
分支
772
平均合併
4 天 9 小時
30 天內合併 PR
17

描述

Whilst working on a project that uses `sqlparser` to parse some real SQL queries from a common PG client (PGAdmin 6.1) I found that the following query emitted by PGAdmin at startup fails to parse correctly:

```sql
SELECT
roles.oid as id, roles.rolname as name,
roles.rolsuper as is_superuser,
CASE WHEN roles.rolsuper THEN true ELSE roles.rolcreaterole END as
can_create_role,
CASE WHEN roles.rolsuper THEN true
ELSE roles.rolcreatedb END as can_create_db,
CASE WHEN 'pg_signal_backend'=ANY(ARRAY(WITH RECURSIVE cte AS (
SELECT pg_roles.oid,pg_roles.rolname FROM pg_roles
WHERE pg_roles.oid = roles.oid
UNION ALL
SELECT m.roleid,pgr.rolname FROM cte cte_1
JOIN pg_auth_members m ON m.member = cte_1.oid
JOIN pg_roles pgr ON pgr.oid = m.roleid)
SELECT rolname FROM cte)) THEN True
ELSE False END as can_signal_backend
FROM
pg_catalog.pg_roles as roles
WHERE
rolname = current_user
```

I poked around a bit and managed to reduce the failing query to:

```sql
SELECT ARRAY(SELECT 1))
```

which against PG 13 produces the following results:

```
edd=# SELECT ARRAY(SELECT 1);
array
-------
{1}
(1 row)
```

However, the following program panics:

```rust
use sqlparser;

let q = "SELECT ARRAY(SELECT 1)";
let dialect = sqlparser::dialect::PostgreSqlDialect {};
let ast = sqlparser::parser::Parser::parse_sql(&dialect, &q).unwrap();
```

with the following panic (due to an error being returned)

```
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParserError("Expected ), found: 1")
```

Finally, if I change the query to something that is _invalid_ in PG:

```sql
SELECT ARRAY(1);
```

then whilst this query fails against PG 13 it actually gets parsed by `sqlparser`:

```
[2022-01-06T18:38:38Z DEBUG sqlparser::parser] Parsing sql 'SELECT ARRAY(1)'...
[2022-01-06T18:38:38Z DEBUG sqlparser::parser] parsing expr
[2022-01-06T18:38:38Z DEBUG sqlparser::parser] parsing expr
[2022-01-06T18:38:38Z DEBUG sqlparser::parser] prefix: Value(Number("1", false))
[2022-01-06T18:38:38Z DEBUG sqlparser::parser] get_next_precedence() RParen
[2022-01-06T18:38:38Z DEBUG sqlparser::parser] next precedence: 0
[2022-01-06T18:38:38Z DEBUG sqlparser::parser] prefix: Function(Function { name: ObjectName([Ident { value: "ARRAY", quote_style: None }]), args: [Unnamed(Value(Number("1", false)))], over: None, distinct: false })
[2022-01-06T18:38:38Z DEBUG sqlparser::parser] get_next_precedence() EOF
[2022-01-06T18:38:38Z DEBUG sqlparser::parser] next precedence: 0
```

貢獻指南

這個儲存庫沒有索引到貢獻指南

研究方向

首先,使用 PostgreSqlDialect 對 SELECT ARRAY(SELECT 1) 執行 Parser::parse_sql,並追蹤處理 ARRAY 的 PostgreSQL 運算式解析路徑。為這個有效查詢新增回歸測試覆蓋,並確認解析成功,同時保留對無效範例 ARRAY(1) 的現有行為。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
postgresql, rust, sql
領域
databases
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。