'Generating AST for BigQuery using ZetaSQL

I am new to ZetaSQL and trying to generate an AST for BigQuery (sample query eg: select cols from dataset.table where condition;) using ZetaSQL.

I have added below dependancy in my pom.xml

    <dependencies>
        <dependency>
            <groupId>com.google.zetasql</groupId>
            <artifactId>zetasql-client</artifactId>
            <version>2022.02.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.zetasql</groupId>
            <artifactId>zetasql-jni-channel</artifactId>
            <version>2022.02.1</version>
        </dependency>
    </dependencies>

This is how I am using Analyser to extract tables from query which works fine.

   String Querystr = "SELECT * FROM `projct_name`.dataset.table WHERE col='val';";
   List<List<String>> tables = Analyzer.extractTableNamesFromStatement(Querystr);

When I try to use analyzeExpression for same query, it is not able to parse the query.

   ResolvedNodes.ResolvedExpr resolvedExpr = Analyzer.analyzeExpression(Querystr, new AnalyzerOptions(), new SimpleCatalog("sample_catalog"));

Error :

Exception in thread "main" com.google.zetasql.SqlException: Syntax error: Unexpected keyword SELECT [at 1:1]
    at com.google.zetasql.Analyzer.analyzeExpression(Analyzer.java:81)
    at SQL_Parser.main(SQL_Parser.java:43)
Caused by: com.google.zetasql.io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Syntax error: Unexpected keyword SELECT [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.analyzeExpression(Analyzer.java:79)

Similarly, If I try using analyzeStatement for same query, I am getting Table not found error.

    ResolvedNodes.ResolvedStatement resolvedStatement = Analyzer.analyzeStatement(Querystr, new AnalyzerOptions(), new SimpleCatalog("new_catalog"));

Error :

Exception in thread "main" com.google.zetasql.SqlException: Table not found: projct_name.dataset.table [at 1:15]
    at com.google.zetasql.Analyzer.analyzeStatement(Analyzer.java:63)
    at SQL_Parser.main(SQL_Parser.java:49)
Caused by: com.google.zetasql.io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Table not found: projct_name.dataset.table [at 1:15]
    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.analyzeStatement(Analyzer.java:61)

It is not able to parse same query which worked fine when I tried extracting table names from the query. Query is pretty straightforward. Am I using above functions provided by ZetaSQL Analyser correctly? Is there any example explaining usage of it to generate AST?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source