apache / datafusion-sqlparser-rs

Extensible SQL Lexer and Parser for Rust
Apache License 2.0
2.81k stars 543 forks source link

[EPIC] Complete Span (source location) information / feature #1548

Open alamb opened 2 days ago

alamb commented 2 days ago

This ticket tracks the work remaining to complete adding source location information into sqlparser

Background

Let's use this ticket to organize needed / remaining work. If you find additional features are needed / issues, please leave a comment on this ticket

Source Span Contributing Guidelines

For contributing source spans improvement in addition to the general contribution guidelines, please make sure to pay attention to the following:

When adding support for source spans on a type, consider the impact to consumers of that type and whether your change would require a consumer to do non-trivial changes to their code.

Example of a trivial change

match node {  
   ast::Query { 
     field1,
     field2, 
     location: _,  // add a new line to ignored location
 }

If adding source spans to a type would require a significant change like wrapping the type, please open an issue to discuss. 

# AST Node Equality and Hashes

When adding tokens to AST nodes, make sure to store them using the [AttachedToken](https://docs.rs/sqlparser/latest/sqlparser/ast/helpers/struct.AttachedToken.html) (TODO UPDATE SOURCE REFERENCE)to ensure that semantically equivalent AST nodes compare as equal and hash to the same value. i.e. `select 5` and `SELECT 5` would compare as different `Select` nodes, if the select token was stored directly. f.e.

```rust
struct Select {
     select_token: AttachedToken, // only used for spans
     /// remaining fields
     field1,
     field2,
     ...
 }

Some high level work (list from https://github.com/apache/datafusion-sqlparser-rs/pull/1435)

Tasks

alamb commented 1 day ago

I think the initial thing we should do to kick this project is get a test pattern setup:

Then we should be good to start cranking out location information