We rebuilt our SQL parser in Rust: 3.3x faster with a zero-copy AST and better diagnostics

408 points heisenberg_zzh 50 comments

Hey r/rust

We encountered a massive bottleneck where our SQL parser was taking 13s on a 20s query. We rewrote it from scratch in Rust and wanted to share the architectural lessons.

The key wins came from letting Rust's principles guide the design:

  • Zero-Copy: A fully borrowed AST using &'a str to eliminate allocations.
  • Better Errors: "Furthest-error-tracking" for contextual errors with suggestions.
  • Clean Architecture: Strictly separating parsing (syntax) from analysis (semantics).

We wrote a deep-dive on the process, from our Pratt parser implementation to how the borrow checker forced us into a better design.

Blog Post: https://www.databend.com/blog/category-engineering/2025-09-10-query-parser/

Demo Repo: https://github.com/ZhiHanZ/sql-parser-demo

Happy to answer any questions!