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 ;
@@ -198,10 +201,13 @@ else if (statement instanceof ExecuteImmediate executeImmediate) {
198201 ImmutableSet .Builder <String > catalogBuilder = ImmutableSet .builder ();
199202 ImmutableSet .Builder <String > schemaBuilder = ImmutableSet .builder ();
200203 ImmutableSet .Builder <String > catalogSchemaBuilder = ImmutableSet .builder ();
204+ Set <QualifiedName > temporaryTables = new HashSet <>();
201205
202- getNames (statement , tableBuilder , catalogBuilder , schemaBuilder , catalogSchemaBuilder );
206+ getNames (statement , tableBuilder , catalogBuilder , schemaBuilder , catalogSchemaBuilder , temporaryTables );
203207 tables = tableBuilder .build ();
208+
204209 catalogBuilder .addAll (tables .stream ().map (q -> q .getParts ().getFirst ()).iterator ());
210+
205211 catalogs = catalogBuilder .build ();
206212 schemaBuilder .addAll (tables .stream ().map (q -> q .getParts ().get (1 )).iterator ());
207213 schemas = schemaBuilder .build ();
@@ -263,7 +269,8 @@ private String decodePreparedStatementFromHeader(String headerValue)
263269 private void getNames (Node node , ImmutableSet .Builder <QualifiedName > tableBuilder ,
264270 ImmutableSet .Builder <String > catalogBuilder ,
265271 ImmutableSet .Builder <String > schemaBuilder ,
266- ImmutableSet .Builder <String > catalogSchemaBuilder )
272+ ImmutableSet .Builder <String > catalogSchemaBuilder ,
273+ Set <QualifiedName > temporaryTables )
267274 throws RequestParsingException
268275 {
269276 switch (node ) {
@@ -278,6 +285,7 @@ private void getNames(Node node, ImmutableSet.Builder<QualifiedName> tableBuilde
278285 case DropCatalog s -> catalogBuilder .add (s .getCatalogName ().getValue ());
279286 case DropSchema s -> setCatalogAndSchemaNameFromSchemaQualifiedName (Optional .of (s .getSchemaName ()), catalogBuilder , schemaBuilder , catalogSchemaBuilder );
280287 case DropTable s -> tableBuilder .add (qualifyName (s .getTableName ()));
288+ case Query q -> q .getWith ().ifPresent (with -> temporaryTables .addAll (with .getQueries ().stream ().map (WithQuery ::getName ).map (Identifier ::getValue ).map (QualifiedName ::of ).toList ()));
281289 case RenameMaterializedView s -> {
282290 tableBuilder .add (qualifyName (s .getSource ()));
283291 tableBuilder .add (qualifyName (s .getTarget ()));
@@ -336,13 +344,18 @@ private void getNames(Node node, ImmutableSet.Builder<QualifiedName> tableBuilde
336344 case SetSchemaAuthorization s -> setCatalogAndSchemaNameFromSchemaQualifiedName (Optional .of (s .getSource ()), catalogBuilder , schemaBuilder , catalogSchemaBuilder );
337345 case SetTableAuthorization s -> tableBuilder .add (qualifyName (s .getSource ()));
338346 case SetViewAuthorization s -> tableBuilder .add (qualifyName (s .getSource ()));
339- case Table s -> tableBuilder .add (qualifyName (s .getName ()));
347+ case Table s -> {
348+ // ignore temporary tables as they can have various table parts
349+ if (!temporaryTables .contains (s .getName ())) {
350+ tableBuilder .add (qualifyName (s .getName ()));
351+ }
352+ }
340353 case TableFunctionInvocation s -> tableBuilder .add (qualifyName (s .getName ()));
341354 default -> {}
342355 }
343356
344357 for (Node child : node .getChildren ()) {
345- getNames (child , tableBuilder , catalogBuilder , schemaBuilder , catalogSchemaBuilder );
358+ getNames (child , tableBuilder , catalogBuilder , schemaBuilder , catalogSchemaBuilder , temporaryTables );
346359 }
347360 }
348361
0 commit comments