4444import io .trino .sql .tree .Node ;
4545import io .trino .sql .tree .NodeLocation ;
4646import io .trino .sql .tree .QualifiedName ;
47+ import io .trino .sql .tree .Query ;
4748import io .trino .sql .tree .RenameMaterializedView ;
4849import io .trino .sql .tree .RenameSchema ;
4950import io .trino .sql .tree .RenameTable ;
5960import io .trino .sql .tree .Statement ;
6061import io .trino .sql .tree .Table ;
6162import io .trino .sql .tree .TableFunctionInvocation ;
63+ import io .trino .sql .tree .WithQuery ;
6264import jakarta .servlet .http .HttpServletRequest ;
6365import jakarta .ws .rs .HttpMethod ;
6466
6870import java .util .ArrayList ;
6971import java .util .Arrays ;
7072import java .util .Enumeration ;
73+ import java .util .HashSet ;
7174import java .util .List ;
7275import java .util .Map ;
7376import java .util .Optional ;
7477import java .util .Set ;
7578import java .util .stream .Collectors ;
7679
80+ import static com .google .common .collect .ImmutableSet .toImmutableSet ;
7781import static com .google .common .io .BaseEncoding .base64Url ;
7882import static io .airlift .json .JsonCodec .jsonCodec ;
7983import static java .lang .Math .toIntExact ;
@@ -90,6 +94,7 @@ public class TrinoQueryProperties
9094 private String queryType = "" ;
9195 private String resourceGroupQueryType = "" ;
9296 private Set <QualifiedName > tables = ImmutableSet .of ();
97+ private final Set <QualifiedName > temporaryTables = new HashSet <>();
9398 private final Optional <String > defaultCatalog ;
9499 private final Optional <String > defaultSchema ;
95100 private Set <String > catalogs = ImmutableSet .of ();
@@ -201,12 +206,17 @@ else if (statement instanceof ExecuteImmediate executeImmediate) {
201206
202207 getNames (statement , tableBuilder , catalogBuilder , schemaBuilder , catalogSchemaBuilder );
203208 tables = tableBuilder .build ();
204- catalogBuilder .addAll (tables .stream ().map (q -> q .getParts ().getFirst ()).iterator ());
209+
210+ Set <QualifiedName > filteredTables = tables .stream ()
211+ .filter (table -> !temporaryTables .contains (table ))
212+ .collect (toImmutableSet ());
213+
214+ catalogBuilder .addAll (filteredTables .stream ().map (q -> q .getParts ().getFirst ()).iterator ());
205215 catalogs = catalogBuilder .build ();
206- schemaBuilder .addAll (tables .stream ().map (q -> q .getParts ().get (1 )).iterator ());
216+ schemaBuilder .addAll (filteredTables .stream ().map (q -> q .getParts ().get (1 )).iterator ());
207217 schemas = schemaBuilder .build ();
208218 catalogSchemaBuilder .addAll (
209- tables .stream ().map (qualifiedName -> format ("%s.%s" , qualifiedName .getParts ().getFirst (), qualifiedName .getParts ().get (1 ))).iterator ());
219+ filteredTables .stream ().map (qualifiedName -> format ("%s.%s" , qualifiedName .getParts ().getFirst (), qualifiedName .getParts ().get (1 ))).iterator ());
210220 catalogSchemas = catalogSchemaBuilder .build ();
211221 }
212222 catch (IOException e ) {
@@ -278,6 +288,11 @@ private void getNames(Node node, ImmutableSet.Builder<QualifiedName> tableBuilde
278288 case DropCatalog s -> catalogBuilder .add (s .getCatalogName ().getValue ());
279289 case DropSchema s -> setCatalogAndSchemaNameFromSchemaQualifiedName (Optional .of (s .getSchemaName ()), catalogBuilder , schemaBuilder , catalogSchemaBuilder );
280290 case DropTable s -> tableBuilder .add (qualifyName (s .getTableName ()));
291+ case Query s -> s .getWith ().ifPresent (with -> {
292+ for (WithQuery withQuery : with .getQueries ()) {
293+ temporaryTables .add (QualifiedName .of (withQuery .getName ().getValue ()));
294+ }
295+ });
281296 case RenameMaterializedView s -> {
282297 tableBuilder .add (qualifyName (s .getSource ()));
283298 tableBuilder .add (qualifyName (s .getTarget ()));
@@ -385,6 +400,12 @@ private QualifiedName qualifyName(QualifiedName table)
385400 throws RequestParsingException
386401 {
387402 List <String > tableParts = table .getParts ();
403+
404+ // ignore temporary tables created by WITH clause as they can have various table parts
405+ if (temporaryTables .contains (table )) {
406+ return table ;
407+ }
408+
388409 return switch (tableParts .size ()) {
389410 case 1 -> QualifiedName .of (defaultCatalog .orElseThrow (this ::unsetDefaultExceptionSupplier ), defaultSchema .orElseThrow (this ::unsetDefaultExceptionSupplier ), tableParts .getFirst ());
390411 case 2 -> QualifiedName .of (defaultCatalog .orElseThrow (this ::unsetDefaultExceptionSupplier ), tableParts .getFirst (), tableParts .get (1 ));
0 commit comments