Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Nickolay Ponomarev
    @nickolay
    I'm still not sure why trait Dialect { fn parse_select(&self, tokens) -> AST { /* default impl */ }; impl Dialect for HiveDialect { fn parse_select(...) { /* override */ } } won't work. Granted I'm not very experienced in Rust, but a tiny test worked fine.
    Andy Grove
    @andygrove
    yeah I can explain the problem
    lets use a fictional AcmeSQL where SELECT is 99% same as ANSI but has a different ALIAS syntax e.g. SELECT col1 ALIASED AS 'abc', .. FROM ... WHERE ...
    we don't want to implement parse_select for AcmeSQL, we just want to implement a custom parse_expression most likely
    you know, I should just create a little project to demonstrate
    maybe in rust playground
    Andy Grove
    @andygrove
    well I'd already started on some code , let's see if this makes it clearer
    pub struct Parser {
        parse_select: Box<ParserFunc>,
        parse_identifier: Box<ParserFunc>,
        parse_literal: Box<ParserFunc>,
    }
    
    pub trait Dialect {
        fn create_parser(&self) -> Box<Parser>;
    }
    impl Dialect for CustomDialect {
    
        fn create_parser(&self) -> Box<Parser> {
            // start with base ANSI parser
            let mut base: Box<DialectNew> = Box::new(BaseDialect {});
            let mut parser = base.create_parser();
            // override how literals are parse in my dialect
            parser.parse_literal = Box::new({|tokens| Box::new(ASTNode::SQLValue(Value::Long(123)))});
            parser
        }
    }
    so if we just want to change how literals are parsed we can do this
    no need to duplicate other logic
    in the parser, the parse_select method from the base parser will end up delegating to self.parse_literal() which we have overridden
    I think I've finally figured out how to do inheritance with Rust
    does that make sense?
    I think I can create a small PoC tomorrow for this idea
    Nickolay Ponomarev
    @nickolay
    I understand the problem, I think. What I don't get is why you don't just put the base behavior in a trait
    I get the solution as well, it's more complex than a trait and I'm struggling to understand why
    this is the "tiny test" I mentioned above: nickolay/sqlparser-rs@c8484f2
    I defined the base behavior in Dialect, the methods can be overriden, like virtual functions in C-like
    Andy Grove
    @andygrove
    Ah, I see. I think you are right.. that is a better approach.
    I haven't used traits with default implementations before. I don't think that existed when I started learning Rust
    Nickolay Ponomarev
    @nickolay
    May well be. It's an interesting experience -- seeing the language evolve while you're still learning it
    Nickolay Ponomarev
    @nickolay
    hey @andygrove - I don't know if you've seen the PRs added recently. I started looking into them and some (#72 / #73) look uncontroversial - do you want to take a look before I merge or merge them yourself?
    Nickolay Ponomarev
    @nickolay
    #75 / #76 have been updated and seem ready to merge as well
    Nickolay Ponomarev
    @nickolay
    @andygrove since you seem to be away, I went ahead and merged a few PRs. Would appreciate your thoughts on andygrove/sqlparser-rs#84 when you have time!