google / zetasql

ZetaSQL - Analyzer Framework for SQL
Apache License 2.0
2.28k stars 214 forks source link

Analyze queries with DECLARE #139

Open emersonlesage opened 1 year ago

emersonlesage commented 1 year ago

Does the analyzer support queries that contain DECLARE statements? Im getting "unexpected token: DECLARE" errors when running it.

matthewcbrown commented 1 year ago

It's often helpful to provide some context when requesting help.
For instance, what query are you running? What API are you calling?

But, to answer your question as best I can. Yes, the analyzer supports queries that contain DECLARE statements.

emersonlesage commented 1 year ago

Thanks for the reply, and sorry I should have included more details.

I am following the AnalyzeBigQuery.java file here: https://github.com/GoogleCloudPlatform/professional-services/blob/main/tools/zetasql-helper/zetasql-toolkit-examples/src/main/java/com/google/zetasql/toolkit/examples/AnalyzeBigQuery.java

and trying to make it work with a modified query that contains a DECLARE statement:

DECLARE my_title STRING DEFAULT 'Unrestricted submarine warfare'; SELECT title, language FROM bigquery-public-data.samples.wikipedia WHERE title = my_title;

Everything builds fine and the original files example files run without issues, but once I include the DECLARE keyword I get the following error:

Exception in thread "main" com.google.zetasql.SqlException: Syntax error: Unexpected keyword DECLARE [at 1:1] at com.google.zetasql.Analyzer.analyzeNextStatement(Analyzer.java:220) at com.google.zetasql.toolkit.ZetaSQLToolkitAnalyzer$1.next(ZetaSQLToolkitAnalyzer.java:144) at com.google.zetasql.toolkit.ZetaSQLToolkitAnalyzer$1.next(ZetaSQLToolkitAnalyzer.java:1) at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) at com.google.zetasql.toolkit.examples.AnalyzeBigQuery.main(AnalyzeBigQuery.java:72) Caused by: com.google.zetasql.io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Syntax error: Unexpected keyword DECLARE [at 1:1] at com.google.zetasql.io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:262) at com.google.zetasql.io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:243) at com.google.zetasql.io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:156) at com.google.zetasql.ZetaSqlLocalServiceGrpc$ZetaSqlLocalServiceBlockingStub.analyze(ZetaSqlLocalServiceGrpc.java:1506) at com.google.zetasql.Analyzer.analyzeNextStatement(Analyzer.java:218) ... 4 more

Furthermore I am running this within the Google Cloud Shell. I suspect that I need to add the declared variable to the bigquery catalog but am unsure how to do so, any help would be much appreciated!

masterlittle commented 1 year ago

DECLARE statements are considered a part of script in Bigquery. That's not supported in Zetasql currently.

Though I also tried finding how to add a constant in a simple catalog but could not find how to do it.