Let a Dialect override tokenization
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 35/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- rust
调研方向
Start by reading Tokenizer::next_token, the private State character-stream implementation, and the parser dialect hooks. The issue proposes a new tokenizer override and raises unresolved questions about lookahead, custom token variants, and performance; first assess those design choices, then validate the hook against existing dialect-specific lexing and measure with sqlparser_bench. Done means the design is settled and dialects can override tokenization without consuming input when they return None.
由索引模型根据 Issue 内容生成。
描述
Follow-up to the discussion in #2610.
Problem
The parser lets a dialect take over at specific points (parse_prefix, parse_infix, parse_statement): it receives &mut Parser, and returning None falls back to the default. The tokenizer has no equivalent. A dialect can only answer yes/no questions the tokenizer asks (is_identifier_part, supports_triple_quoted_string, and about 20 others), so it can toggle existing lexemes but not add new ones.
Dialect-specific lexing therefore lives in Tokenizer::next_token behind dialect_of! checks:
b'...'byte strings (BigQuery, MySQL) and bit strings (Postgres)r'...'raw strings (BigQuery)//as a line comment (Snowflake) or integer division (DuckDB)#as a line comment (Snowflake, BigQuery, MySQL, Hive)\rending a line comment (Postgres)
A downstream dialect cannot add a branch like these. Its only option is tokenize_with_location_into_buf_with_mapper, which rewrites one finished token at a time and cannot see characters or merge tokens.
Example: in dbt models, a Jinja comment {# ... #} breaks under every dialect that treats # as a line comment, because the tokenizer consumes the rest of the line, including #}.
Proposal
- Expose the character stream that
next_tokenreads from (today the privateState:peek,next,location), or a narrow public wrapper around it. - Add a hook with the same contract as the parser hooks:
/// Dialect-specific tokenizer override, called before the default rules. /// /// If `None` is returned, falls back to the default behavior. fn next_token( &self, _chars: &mut State, _prev_token: Option<&Token>, ) -> Option<Result<Token, TokenizerError>> { None } - As a separate step, move the existing
dialect_of!branches into the hook implementations of their dialects. That would show the hook covers syntax already in the tree, and would removedialect_of!from the tokenizer.
Open questions
- Lookahead: a hook that returns
Nonemust not consume input.StatewrapsPeekable<Chars>and allows one character of lookahead. ShouldStatebeClone, or offerpeek_nth? Tokenis a closed enum, so a dialect-specific lexeme must map to an existing variant (Word,CustomBinaryOperator, ...). Is that enough, or should there be aToken::Customvariant?- Cost: one extra dynamic call per token. The tokenizer already calls several dialect methods per token, and
sqlparser_benchcan measure the difference.
- 主要语言
- Rust
- 星标
- 3.5k
- 派生
- 805
- 平均合并
- 2 天 3 小时
- 30 天内合并 PR
- 116
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apache/datafusion-sqlparser-rs 的其他 Issue
-
Redshift
难度 2/5 1-3 小时 新手友好度 82/100
apache/datafusion-sqlparser-rs#2959 ·
维护者通常 1 天内回复
-
Redshift
难度 2/5 1-3 小时 新手友好度 68/100
apache/datafusion-sqlparser-rs#2958 ·
维护者通常 1 天内回复
-
PostgreSQL
难度 2/5 1-3 小时 新手友好度 76/100
apache/datafusion-sqlparser-rs#2862 ·
维护者通常 1 天内回复
-
PostgreSQL
难度 2/5 1-3 小时 新手友好度 75/100
apache/datafusion-sqlparser-rs#2861 ·
维护者通常 1 天内回复
-
PostgreSQL
难度 2/5 1-3 小时 新手友好度 77/100
apache/datafusion-sqlparser-rs#2858 ·
维护者通常 1 天内回复
查看 apache/datafusion-sqlparser-rs 的全部 Issue
相似的 Issue
-
area:schema documentation
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
ElectronicWave/Elemental#252 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
dekobon/big-code-analysis#1887 ·
维护者通常 1 天内回复
-
Shell中文符号输入异常未关闭bug
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 82/100
维护者通常 1 天内回复