google / zetasql

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

[BigQuery] Extracting table names from resolved columns #130

Open jrbangit opened 1 year ago

jrbangit commented 1 year ago

I am trying to extract the source table name from a resolved column, for example

resolvedFunctionCall.accept(new ResolvedNodes.Visitor() {
                            @Override
                            public void visit(ResolvedNodes.ResolvedColumnRef resolvedColumnRef) {
                                sourceCol = String.format("%s.%s", resolvedColumnRef.getColumn().getTableName(), resolvedColumnRef.getColumn().getName());
                            }
                        });

However, if the tables used in a query are aliased then the resolvedColumnRef.getColumn().getTableName() results to the alias name instead of the actual table name.

You can re-create this scenario by analyzing a simple query that utilizes subqueries and make sure the tables inside subqueries are aliased, or you can also try it using with statements, then run the usual steps below. From the resolvedStatements which was returned by Analyzer.analyzeStatement, you can visit any node like the example above.

SimpleCatalog simpleCatalog = RegisterCatalog.register(simpleTableList); Analyzer.analyzeStatement(query, analyzerOptions, simpleCatalog);

does someone know how can we extract the origin and actual table names of a particular resolved column despite being aliased in queries?

Thank you in advance.