5959import io .trino .sql .tree .Statement ;
6060import io .trino .sql .tree .Table ;
6161import io .trino .sql .tree .TableFunctionInvocation ;
62+ import io .trino .sql .tree .WithQuery ;
6263import jakarta .servlet .http .HttpServletRequest ;
6364import jakarta .ws .rs .HttpMethod ;
6465
6869import java .util .ArrayList ;
6970import java .util .Arrays ;
7071import java .util .Enumeration ;
72+ import java .util .HashSet ;
7173import java .util .List ;
7274import java .util .Map ;
7375import java .util .Optional ;
7476import java .util .Set ;
7577import java .util .stream .Collectors ;
7678
79+ import static com .google .common .collect .ImmutableSet .toImmutableSet ;
7780import static com .google .common .io .BaseEncoding .base64Url ;
7881import static io .airlift .json .JsonCodec .jsonCodec ;
7982import static java .lang .Math .toIntExact ;
@@ -90,6 +93,7 @@ public class TrinoQueryProperties
9093 private String queryType = "" ;
9194 private String resourceGroupQueryType = "" ;
9295 private Set <QualifiedName > tables = ImmutableSet .of ();
96+ private final Set <QualifiedName > temporaryTables = new HashSet <>();
9397 private final Optional <String > defaultCatalog ;
9498 private final Optional <String > defaultSchema ;
9599 private Set <String > catalogs = ImmutableSet .of ();
@@ -201,12 +205,17 @@ else if (statement instanceof ExecuteImmediate executeImmediate) {
201205
202206 getNames (statement , tableBuilder , catalogBuilder , schemaBuilder , catalogSchemaBuilder );
203207 tables = tableBuilder .build ();
204- catalogBuilder .addAll (tables .stream ().map (q -> q .getParts ().getFirst ()).iterator ());
208+
209+ Set <QualifiedName > filteredTables = tables .stream ()
210+ .filter (table -> !temporaryTables .contains (table ))
211+ .collect (toImmutableSet ());
212+
213+ catalogBuilder .addAll (filteredTables .stream ().map (q -> q .getParts ().getFirst ()).iterator ());
205214 catalogs = catalogBuilder .build ();
206- schemaBuilder .addAll (tables .stream ().map (q -> q .getParts ().get (1 )).iterator ());
215+ schemaBuilder .addAll (filteredTables .stream ().map (q -> q .getParts ().get (1 )).iterator ());
207216 schemas = schemaBuilder .build ();
208217 catalogSchemaBuilder .addAll (
209- tables .stream ().map (qualifiedName -> format ("%s.%s" , qualifiedName .getParts ().getFirst (), qualifiedName .getParts ().get (1 ))).iterator ());
218+ filteredTables .stream ().map (qualifiedName -> format ("%s.%s" , qualifiedName .getParts ().getFirst (), qualifiedName .getParts ().get (1 ))).iterator ());
210219 catalogSchemas = catalogSchemaBuilder .build ();
211220 }
212221 catch (IOException e ) {
@@ -338,6 +347,7 @@ private void getNames(Node node, ImmutableSet.Builder<QualifiedName> tableBuilde
338347 case SetViewAuthorization s -> tableBuilder .add (qualifyName (s .getSource ()));
339348 case Table s -> tableBuilder .add (qualifyName (s .getName ()));
340349 case TableFunctionInvocation s -> tableBuilder .add (qualifyName (s .getName ()));
350+ case WithQuery withQuery -> temporaryTables .add (QualifiedName .of (withQuery .getName ().getValue ()));
341351 default -> {}
342352 }
343353
@@ -385,6 +395,12 @@ private QualifiedName qualifyName(QualifiedName table)
385395 throws RequestParsingException
386396 {
387397 List <String > tableParts = table .getParts ();
398+
399+ // ignore temporary tables created by WITH clause as they can have various table parts
400+ if (temporaryTables .contains (table )) {
401+ return table ;
402+ }
403+
388404 return switch (tableParts .size ()) {
389405 case 1 -> QualifiedName .of (defaultCatalog .orElseThrow (this ::unsetDefaultExceptionSupplier ), defaultSchema .orElseThrow (this ::unsetDefaultExceptionSupplier ), tableParts .getFirst ());
390406 case 2 -> QualifiedName .of (defaultCatalog .orElseThrow (this ::unsetDefaultExceptionSupplier ), tableParts .getFirst (), tableParts .get (1 ));
0 commit comments