Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude with statement names from tables #532

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

willmostly
Copy link
Contributor

Description

Exclude with statement names from the list of tables extracted from SQL by TrinoQueryProperties

Additional context and related issues

The existing TrinoQueryProperties will include WITH statement names in the list of tables. For example, for the query

WITH arbitrary_name AS (
SELECT c1 from c.s.t)
SELECT * FROM arbutrary_name

the tables list will be {"c.s.t", "arbitrary_name"}, or, if default catalog and schema are not specified, processing of the SQL will fail when TrinoQueryPoperties attempts to qualify arbitrary_name with a catalog and schema.

Release notes

( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
(x) Release notes are required, with the following suggested text:

* Support `trinoQueryProperties` for SQL containing `WITH` statements

@cla-bot cla-bot bot added the cla-signed label Oct 21, 2024
@@ -194,13 +196,25 @@ else if (statement instanceof ExecuteImmediate executeImmediate) {

queryType = statement.getClass().getSimpleName();
resourceGroupQueryType = StatementUtils.getQueryType(statement).toString();
ImmutableSet.Builder<QualifiedName> tableBuilder = ImmutableSet.builder();
List<QualifiedName> allQUalifiedNames = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change to ArrayList? #Also, the new name has a typo. U should be a lowercase.

private void getNames(Node node, ImmutableSet.Builder<QualifiedName> tableBuilder,
private void getNames(
Node node,
List<QualifiedName> allQualifiedNames,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the difference between tableBuilder and allQualifiedNames. What's the motivation of this renaming?

Comment on lines +412 to +414
WITH dos (c1) as (select c1 from uno),
uno(c1) as (select c1 from cat.schem.tbl1)
SELECT c1 from uno, dos
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uppercase as, select, from.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think this is valid SQL. You cannot use uno as it is defined after dos.

Suggested change
WITH dos (c1) as (select c1 from uno),
uno(c1) as (select c1 from cat.schem.tbl1)
SELECT c1 from uno, dos
WITH dos AS (SELECT c1 from cat.schem.tbl1),
uno as (SELECT c1 FROM dos)
SELECT c1 FROM uno, dos

you can test this in trino via simple sql

// works fine
WITH dos AS (SELECT node_id FROM system.runtime.nodes),
uno AS (SELECT node_id as node_id2 FROM dos)
SELECT node_id, node_id2
FROM uno, dos

// doesn't work as uno is not defined before dos.
// Query 20241023_021715_04215_f8xaz failed: line 1:34: Table 'system.runtime.uno' does not exist
WITH dos AS (SELECT node_id FROM uno),
uno AS (SELECT node_id as node_id2 FROM system.runtime.nodes)
SELECT node_id, node_id2
FROM uno, dos

Comment on lines +424 to +425
assertThat(trinoQueryPropertiesWithDefaults.tablesContains(String.format("%s.%s.%s", DEFAULT_CATALOG, DEFAULT_SCHEMA, "uno"))).isFalse();
assertThat(trinoQueryPropertiesWithDefaults.tablesContains(String.format("%s.%s.%s", DEFAULT_CATALOG, DEFAULT_SCHEMA, "does"))).isFalse();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when(mockRequestNoDefaults.getReader()).thenReturn(bufferedReader);

TrinoQueryProperties trinoQueryPropertiesNoDefaults = new TrinoQueryProperties(mockRequestNoDefaults, requestAnalyzerConfig);
assertThat(trinoQueryPropertiesNoDefaults.getTables().size()).isEqualTo(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssertJ provides hasSize method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you consider temporary tables (with clause) as non-tables?

@mosabua
Copy link
Member

mosabua commented Oct 21, 2024

This overlaps with #531 ...

case SetViewAuthorization s -> allQualifiedNames.add(s.getSource());
case Table s -> allQualifiedNames.add(s.getName());
case TableFunctionInvocation s -> allQualifiedNames.add(s.getName());
case WithQuery withQuery -> withQueryNamesBuilder.add(withQuery.getName());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't know you could do this directly, i thought withQuery comes with Query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants