apache / apache/datafusion-sqlparser-rs
Expose a method for mutating Parser::index
- 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ả
We have designed an SQL parser using sqlparser's `Parser`, and we wish to implement two helper methods for consuming tokens—similar to `Parser::consume_token` and `Parser::consume_tokens`—but with the ability to accept string inputs and perform case-insensitive matching.
However, since the Parser does not provide a method to mutate the `Parser::index`, we are unable to correctly implement our own `consume_tokens` because we need to revert the parser state if consumption fails mid-way.
We propose adding and exposing a new method in sqlparser's `Parser`, such as `set_index` or `index_mut`, to allow users to mutate the index.
The expected implementation of our `consume_token` and `consume_tokens`:
``` rust
/// Consumes the next token if it matches the expected token, otherwise return false.
///
/// Note, the matching is not case sensitive.
fn consume_token(&mut self, expected: &str) -> bool {
if self.parser.peek_token().to_string().to_uppercase() == *expected.to_uppercase() {
self.parser.next_token();
true
} else {
false
}
}
/// If the current and subsequent tokens exactly match the `tokens` sequence, consume them and returns true.
/// Otherwise, no tokens are consumed and returns false
///
/// Note, the matching is not case sensitive.
fn consume_tokens(&mut self, tokens: &[&str]) -> bool {
let index = self.parser.index();
for token in tokens {
if !self.consume_token(*token) {
// Or: `*self.parser.index_mut() = index;`
self.parser.set_index(index);
return false;
}
}
true
}
```
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
Bắt đầu bằng cách tìm implementation của Rust Parser cùng các method index, consume_token và consume_tokens hiện có. Kiểm tra các test của parser liên quan đến việc consume token, sau đó cung cấp một cách để thay đổi hoặc thiết lập index ताकि việc consume nhiều token thất bại có thể khôi phục vị trí trước đó.
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
- Lĩnh vực
- databases
- Loại issue
- Tính năng
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- 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
- 45/100