Skip to content

struct literal syntax does not work with custom dialect #1227

Closed

Description

@universalmind303

The following should work, but instead fails with Expected an expression:, found: { at Line: 1, Column 8

use sqlparser::dialect::Dialect;

#[derive(Debug)]
pub struct MyDialect;
impl Dialect for MyDialect {
    fn is_identifier_start(&self, ch: char) -> bool {
        ch.is_alphabetic() || ch == '_'
    }

    fn is_identifier_part(&self, ch: char) -> bool {
        ch.is_alphabetic() || ch.is_ascii_digit() || ch == '$' || ch == '_'
    }

    fn supports_filter_during_aggregation(&self) -> bool {
        true
    }

    fn supports_group_by_expr(&self) -> bool {
        true
    }

    fn supports_named_fn_args_with_eq_operator(&self) -> bool {
        true
    }
}

fn main() {
    let sql = "select {'a': 1}";
    let dialect = MyDialect;
    let parser = sqlparser::parser::Parser::new(&dialect);
    assert!(parser.try_with_sql(sql).unwrap().parse_statement().is_ok());
}

Activity

  1. universalmind303 commented on Apr 18, 2024

    @universalmind303
    ContributorAuthor

    FWIW, it seems like this is due to the usage of dialect_of! throughout the parser code.

    It seems like either the dialect itself needs to be in control of supported syntaxes, or a custom dialect should just be treated as an extension of GenericDialect.

    I think the latter would be a bit easier as dialect_of is used ~100 times, so that would be a lot of functionality to expose to the Dialect trait.

  2. universalmind303 commented on Apr 18, 2024

    @universalmind303
    ContributorAuthor

    Looking more closely at the code, it looks like you can "extend" a dialect by implementing the Dialect::dialect method

    impl Dialect for MyDialect {
        fn dialect(&self) -> std::any::TypeId {
            std::any::TypeId::of::<sqlparser::dialect::GenericDialect>()
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions