apache / apache/datafusion-sqlparser-rs
Why custom Dialect doesn't work properly
- Dominant language
- Rust
- Stars
- 3.5k
- Forks
- 772
- Avg merge
- 4d 9h
- Merged PRs (30d)
- 17
Description
I have extended a Dialect named MyDialect, and implemented the is_identifier_start method. is_identifier_start allows the identifier to start with a number, but why does my test code report an error? The prompt says that it cannot start with a number, and if I run it with the native HiveDialect, the code No problem again
**My Code:**
```
#[derive(Debug)]
pub struct MyDialect;
impl Dialect for MyDialect {
fn is_delimited_identifier_start(&self, ch: char) -> bool {
(ch == '"') || (ch == '`')
}
fn is_identifier_start(&self, ch: char) -> bool {
ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch.is_ascii_digit() || ch == '$'
}
fn is_identifier_part(&self, ch: char) -> bool {
ch.is_ascii_lowercase()
|| ch.is_ascii_uppercase()
|| ch.is_ascii_digit()
|| ch == '_'
|| ch == '$'
|| ch == '{'
|| ch == '}'
}
fn supports_filter_during_aggregation(&self) -> bool {
true
}
}
#[cfg(test)]
mod tests {
use sqlparser::parser::Parser;
#[test]
pub fn test_ast() {
let sql = "SELECT * from 1_a";
let expected_sql = "SELECT * from 1_a"
.replace("\n", "");
let dialect = crate::core::ast::MyDialect {};
let ast = Parser::parse_sql(&dialect, sql).unwrap();
assert_eq!(ast[0].to_string(), expected_sql);
}
}
```
**HIveDialect Code(from sqlparser-rs)**
use crate::dialect::Dialect;
/// A [`Dialect`] for [Hive](https://hive.apache.org/).
```
#[derive(Debug)]
pub struct HiveDialect {}
impl Dialect for HiveDialect {
fn is_delimited_identifier_start(&self, ch: char) -> bool {
(ch == '"') || (ch == '`')
}
fn is_identifier_start(&self, ch: char) -> bool {
ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch.is_ascii_digit() || ch == '$'
}
fn is_identifier_part(&self, ch: char) -> bool {
ch.is_ascii_lowercase()
|| ch.is_ascii_uppercase()
|| ch.is_ascii_digit()
|| ch == '_'
|| ch == '$'
|| ch == '{'
|| ch == '}'
}
fn supports_filter_during_aggregation(&self) -> bool {
true
}
}
```
**Error:**
Contributor guide
No contributing guide indexed for this repository
Research direction
Start at Parser::parse_sql and the Dialect trait, then compare the custom implementation with HiveDialect using the provided test_ast reproduction for SELECT * from 1_a. Trace how identifier-start handling is applied and confirm the expected parsing behavior; done means the reproduction is explained or the parser behavior is corrected with a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust, sql
- Domain
- compilers, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100