google / zetasql

ZetaSQL - Analyzer Framework for SQL
Apache License 2.0
2.32k stars 219 forks source link

Get AST for query with error #75

Open pgrivachev opened 3 years ago

pgrivachev commented 3 years ago

I sucessfully use ZetaSQL to obtain AST for different queries. But if query contain any issue, I receive only error. For example for the query

select   from my_table; 

I will get only the following error and no any AST:

ERROR:
  Code: InvalidArgument
  Message: Syntax error: SELECT list must not be empty

Is it possible to get some kind of AST for such SQL queries with issues or for incomplete queries?

jshute111 commented 3 years ago

If it failed to parse, there is no AST to return. The error object returned should include the error location in the input SQL string, and depending on the Options you pass and which API you're calling, that location can be included automatically in the error string to show a highlight of where the error occurred.

pgrivachev commented 3 years ago

Thank you @jshute111!

Yes, I successfully use the response from ZetaSQL with error position to highlight words in SQL: image

But my idea was that if I can get AST I can suggest customer to insert some table names like this (for valid sql): image

It is hard to understand cursor context without AST. Do you have any plans to return AST for error state if possible?