Choosing a SQL parser for Snowflake
Every query an AI agent runs through Contextflo goes through access control first. We work out which tables the query reads, check them against the team's rules, and only then send it to the warehouse. If we can't tell which tables it reads, the query doesn't run. On Snowflake, the parser we had couldn't tell on about 1 in 16 queries.
Why Snowflake needs a parser at all
On BigQuery we don't parse SQL for access control. A dry run asks BigQuery itself which tables and views a query touches, and it returns the list without running anything. That answer comes from the engine, so it can't disagree with what actually executes.
Snowflake has no equivalent. The closest thing is EXPLAIN, and we tested it as a table source. It compiles the query without running it, but two things rule it out:
- It expands views into their base tables and never names the view. Access rules are usually written against views, so we would be checking the wrong names.
- It drops tables the optimizer folds away. A
COUNT(*)answered from metadata, or aWHERE 1=0, shows no table scan at all, so the table vanishes from the list.
So on Snowflake the table list has to come from parsing the SQL ourselves.
What we had
We were using node-sql-parser, a JavaScript parser covering many dialects. Its Snowflake support is labelled alpha, and it shows. Our rule is fail closed: a query we can't read doesn't run. With node-sql-parser, that left a lot of perfectly ordinary queries unrunnable.
The failures weren't exotic. They were the SQL agents write every day:
| Construct | Queries using it | node-sql-parser failed on |
|---|---|---|
:: casts | 1,147 | 170 |
col:path JSON access | 1,213 | 130 |
QUALIFY | 297 | 68 |
ILIKE | 566 | 52 |
LATERAL | 107 | 31 |
FLATTEN | 121 | 30 |
TABLE(...) | 52 | 18 |
GENERATOR | 27 | 15 |
* EXCLUDE / RENAME | 55 | 15 |
INFORMATION_SCHEMA | 144 | 12 |
Snowflake people write :: casts and QUALIFY without thinking. A parser that trips on them can't be the gate.
The bake-off
We pulled the 10,000 most recently run distinct Snowflake queries from our own logs. One templated query with a 2,500-id IN list made up over 4,000 of them, so we counted it once for correctness. That left 5,705 to compare, and we scored every parser on the 5,303 that had actually run successfully in Snowflake. A parse failure on one of those is a real, valid query a user would have been blocked on.
The candidates:
- node-sql-parser, what we had.
- Polyglot, a Rust parser compiled to WebAssembly.
- sqlparser-rs, the Apache DataFusion SQL parser, compiled to WebAssembly.
- sqlglot, the Python parser, as the baseline. It's the most widely used open-source SQL transpiler, but it's Python and our services are Node, so it was there to check the others against.
There's no ground truth for "which tables does this query read", so we used agreement. A table counted as missed when at least two other parsers found it and this one didn't. That's a potential leak. A table counted as extra when no other parser found it. That's a potential false block.
| Parser | Failed to parse | Missed | Extra | Same as sqlglot | p50 / p99 / max |
|---|---|---|---|---|---|
| node-sql-parser | 323 (6.1%) | 0 | 0 | 100% when it parses | 1.2 / 18.7 / 68 ms |
| Polyglot | 46 (0.9%) | 3 | 0 | 99.9% | 0.9 / 74 / 3,448 ms |
| sqlparser-rs | 1 (0.02%) | 0 | 0 | 99.9% | 0.2 / 3.3 / 22 ms |
| sqlglot | 1 (0.02%) | 0 | 1 | baseline | 0.9 / 12 / 27 ms |
node-sql-parser was never wrong when it did parse. Its problem was purely coverage.
Why Polyglot lost
Polyglot failed far less often than node-sql-parser, and on raw failure rate it looked like a fine upgrade. Then we looked at the "missed" column. On three queries it parsed without complaint and returned a partial table list. One dropped 7 of 8 tables.
For access control that's the worst possible outcome. A parse error is safe: we fail closed and the query doesn't run. A confident, incomplete answer is a leak, because the tables it forgot never get checked. One 3.4 second outlier didn't help either. It was out.
sqlparser-rs
sqlparser-rs failed on exactly one query, DESCRIBE USER, which we reject anyway. It agreed with sqlglot on 99.9% of table lists, and it was the fastest of the four by a wide margin, 3.3 ms at p99.
The disagreements with sqlglot were small and explainable. On EXPLAIN SELECT ..., sqlparser-rs lists the inner tables while sqlglot treats the whole statement as an opaque command. On Snowflake's SEMANTIC_VIEW(...), sqlparser-rs produces its own kind of FROM item, which we now check by name like a table.
It's also a serious project. It lives under the Apache DataFusion umbrella (apache/datafusion-sqlparser-rs), and a long list of Rust databases and query engines depend on it. It has dialects for Postgres, Redshift, MySQL, BigQuery, ClickHouse and DuckDB too, so the same parser could eventually replace node-sql-parser for our other engines.
A note on how it parses
The sqlparser-rs README points to Eli Bendersky's write-up on top-down operator precedence parsing, the technique often called Pratt parsing. Statements are parsed by hand-written recursive descent, and expressions use Pratt's approach: every operator gets a binding power, and the parser decides how tightly a + b * c or x::int = 1 groups by comparing those powers instead of encoding precedence as a deep tower of grammar rules.
That's a big part of why it handles Snowflake's odd corners well. Adding an operator like :: or : for JSON paths means giving it a binding power, not reshaping the grammar. Generated parsers built from a fixed grammar, which is closer to how node-sql-parser works, tend to need the grammar rewritten for every dialect quirk. That's where the alpha label comes from.
Fail closed, on purpose
A parser gives you a syntax tree. Access control needs a decision. Between the two sits a walker, and how you write it matters as much as which parser you pick.
Every FROM item sqlparser-rs produced across the whole corpus was one of five kinds:
| Kind | Queries | What we do |
|---|---|---|
| Table | 5,675 | Check the name |
| Subquery | 413 | Walk into it |
LATERAL FLATTEN(...) | 116 | Allow FLATTEN, walk its arguments |
TABLE(...) | 58 | Allow FLATTEN, GENERATOR, SPLIT_TO_TABLE |
| Semantic view | 2 | Check the name |
The walker is an allowlist. Anything outside those kinds, any table function not on the list, any node type a future parser version adds, gets rejected. If the parser grows a new feature before we've decided how to check it, the query is blocked rather than quietly waved through.
Getting Apache code into Node
The WebAssembly build we benchmarked is a community npm package maintained by one person. The parser inside it is Apache's, but the packaging, release cadence and JavaScript glue are not, and that's not something we want sitting in front of every Snowflake query.
So we're building the binding ourselves. sqlparser-rs is an ordinary Rust crate, and compiling it to WebAssembly takes a few dozen lines of Rust plus wasm-pack. That puts us one dependency away from the Apache source instead of two, and we upgrade when we decide to.
What we'd tell anyone doing this
Measure on your own traffic. Every parser's README claims Snowflake support. Only running 5,000 real queries through them showed which claims held, and the one that looked second best was the only one that could leak.
See it on your own data.
Connect your warehouse and your team can ask Claude about it the same day, without waiting on a data team. Want a walkthrough first? Grab 20 minutes.
Book a 20-minute callSee how teams use Contextflo
Related posts
Keep reading

Conversational analytics: 5 ways to set it up, compared
7 min read

How to connect Claude to BigQuery, and fix the errors you'll hit
8 min read

How to connect Claude to Postgres without giving it write access
12 min read

How to build a BI dashboard with Claude that your team can actually trust
8 min read

How to give Claude or ChatGPT read-only access to production data
9 min read

You connected your warehouse to Claude, now what?
5 min read
Conversations with data people: Eiva Orce on fixing the mess before adding AI
7 min read
How much does Amazon Redshift cost? A worked monthly bill
9 min read
Databricks MCP servers: which one answers which question
9 min read


