google / google/googlesql

Analyzer extractTableNamesFromStatement - returning incorrect table names

Open
#123 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
2.6k
Forks
260
PR merge metrics
No merged PRs in 30d

Description

I'm using ZetaSQL in our streaming application that basically consumes from GCP audit log. As you can imagine, there are lots possibilities that a weird query will occur and flow in the audit log and eventually into our application.

I've encountered an issue specifically on the `extractTableNamesFromStatement` method of Analyzer which only occurs on specific type of query. Particularly for queries having the same table name for both target and source, like the query below.

`CREATE OR REPLACE TABLE project_test.dataset_test.test_table AS SELECT DISTINCT * FROM project_test.dataset_test.test_table`

the expected result based on my tests with various types of statements should return `[project_test.dataset_test.test_table]`

However, in the query above, the analyzer returns `[project_test, dataset_test, test_table]` instead.

here's how I used it:
```
LanguageOptions languageOptions = new LanguageOptions();
languageOptions.enableMaximumLanguageFeatures();
languageOptions.setSupportsAllStatementKinds();

AnalyzerOptions analyzerOptions = new AnalyzerOptions();
analyzerOptions.setLanguageOptions(languageOptions);
analyzerOptions.setPruneUnusedColumns(true);


Analyzer.extractTableNamesFromStatement(query, analyzerOptions)
.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
```

Contributor guide

Open the contributing guide

Research direction

Start at Analyzer.extractTableNamesFromStatement and reproduce the reported CREATE OR REPLACE TABLE query with the analyzer options shown. Trace why the target and source table name are split into project_test, dataset_test, and test_table; done means the result contains the single fully qualified table name project_test.dataset_test.test_table.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.