BinaryMuse opened a new pull request, #2089:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2089
## Overview
This PR introduces a new API method `Parser::parse_sql_with_offsets()` that
returns parsed statements along with byte offsets into the original source
string.
## Motivation
I'm using `Parser::parse_sql` to parse an arbitrary number of statements.
Based on the type of statement, I need to handle execution differently.
However, the canonical representation of the `Statement` includes uppercase
type names (in most cases), which don't work as a query for ClickHouse since
ClickHouse uses case-sensitive type names: for example, `Nullable(Float64)` vs
`Nullable(FLOAT64)`.
`parse_sql_with_offsets` returns `Vec<(Statement, SourceOffset)>`, where
`SourceOffset::start()` and `::end()` return the byte offsets of the statement
from the original query, allowing me to recover the original source of the
statement in question:
```rust
let result = Parser::parse_sql_with_offsets(&dialect, &sql).unwrap();
for (statement, offset) in result {
let original_statement_sql = sql[offset.range()]
}
```
## Alternatives
This seems like it would only be useful while work on #1548 is not yet
complete, so it's totally reasonable if you'd prefer this PR not to be merged.
## Implementation details
- Add `SourceOffset` type to track byte positions in source text
- Add `Parser::parse_sql_with_offsets()` public API method
- Add `Parser::parse_statements_with_offsets()` internal method
- Add helper function to convert line/column to byte offsets
- Add comprehensive tests covering single/multiple statements and multiline
SQL
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]