apache / apache/datafusion-sqlparser-rs
SQL Server: Increase support for multi statement object definitions (without semicolons)
- Ngôn ngữ chính
- Rust
- Star
- 3.5k
- Fork
- 772
- Merge trung bình
- 4 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 17
Mô tả
It would be extremely useful if this library was able to properly parse multiple statements, particularly in a object definition body, without requiring semi-colons which are optional for SQL Server.
---
The following otherwise valid SQL fails parsing (MSSQL dialect):
```mssql
create or alter procedure test()
as
begin
declare @x bit = 1
if @x = 1
begin
select 1
end
end
```
The error is at the `if @x = 1` line:
```
end of statement, found: if
```
The problem appears to be related to parsing multiple statements in the object definition body. The parsing concludes successfully if the code is changed to `declare @x bit = 1;` with a semi-colon.
There's also the similar case of two top level statements:
```mssql
declare @x bit = 1
if @x = 1
begin
select 1
end
```
The API I was using was parse_sql:
```rust
static DIALECT: sqlparser::dialect::MsSqlDialect = sqlparser::dialect::MsSqlDialect {};
let sql_text = ...
let mut statements = Parser::parse_sql(&DIALECT, sql_text.as_str())?; // error: end of statement, found: if
```
There's a workaround for the multiple top level statements scenario, but that won't work for the stored procedure example:
```rust
fn parse_all_statements(sql_text: &str) -> Result, Box> {
let mut parser = Parser::new(&DIALECT)
.try_with_sql(&sql_text)?;
let mut statements = Vec::new();
loop {
if let Token::EOF = parser.peek_token_ref().token {
break;
}
statements.push(parser.parse_statement()?);
while let Token::SemiColon = parser.peek_token_ref().token {
parser.advance_token();
}
}
return Ok(statements);
}
```
---
follow up from: https://github.com/apache/datafusion-sqlparser-rs/pull/1791#issuecomment-2784202219
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Hướng nghiên cứu
Start with Parser::parse_sql and the MsSqlDialect entry points shown in the report, then reproduce both SQL Server examples without semicolons. Trace how parse_statement handles statements inside object definition bodies and at the top level. Done means both examples parse successfully while semicolon-terminated input continues to work.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- rust, sql
- Lĩnh vực
- compilers
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100