4747import io .trino .sql .tree .Node ;
4848import io .trino .sql .tree .NodeLocation ;
4949import io .trino .sql .tree .QualifiedName ;
50+ import io .trino .sql .tree .Query ;
5051import io .trino .sql .tree .RenameMaterializedView ;
5152import io .trino .sql .tree .RenameSchema ;
5253import io .trino .sql .tree .RenameTable ;
6364import io .trino .sql .tree .StringLiteral ;
6465import io .trino .sql .tree .Table ;
6566import io .trino .sql .tree .TableFunctionInvocation ;
67+ import io .trino .sql .tree .WithQuery ;
6668import jakarta .servlet .http .HttpServletRequest ;
6769import jakarta .ws .rs .HttpMethod ;
6870
7173import java .net .URLDecoder ;
7274import java .util .ArrayList ;
7375import java .util .Enumeration ;
76+ import java .util .HashSet ;
7477import java .util .List ;
7578import java .util .Map ;
7679import java .util .Optional ;
@@ -208,8 +211,9 @@ else if (statement instanceof ExecuteImmediate executeImmediate) {
208211 ImmutableSet .Builder <String > catalogBuilder = ImmutableSet .builder ();
209212 ImmutableSet .Builder <String > schemaBuilder = ImmutableSet .builder ();
210213 ImmutableSet .Builder <String > catalogSchemaBuilder = ImmutableSet .builder ();
214+ Set <QualifiedName > temporaryTables = new HashSet <>();
211215
212- visitNode (statement , tableBuilder , catalogBuilder , schemaBuilder , catalogSchemaBuilder );
216+ visitNode (statement , tableBuilder , catalogBuilder , schemaBuilder , catalogSchemaBuilder , temporaryTables );
213217 tables = tableBuilder .build ();
214218 catalogBuilder .addAll (tables .stream ().map (q -> q .getParts ().getFirst ()).iterator ());
215219 catalogs = catalogBuilder .build ();
@@ -273,7 +277,8 @@ private String decodePreparedStatementFromHeader(String headerValue)
273277 private void visitNode (Node node , ImmutableSet .Builder <QualifiedName > tableBuilder ,
274278 ImmutableSet .Builder <String > catalogBuilder ,
275279 ImmutableSet .Builder <String > schemaBuilder ,
276- ImmutableSet .Builder <String > catalogSchemaBuilder )
280+ ImmutableSet .Builder <String > catalogSchemaBuilder ,
281+ Set <QualifiedName > temporaryTables )
277282 throws RequestParsingException
278283 {
279284 switch (node ) {
@@ -289,6 +294,7 @@ private void visitNode(Node node, ImmutableSet.Builder<QualifiedName> tableBuild
289294 case DropCatalog s -> catalogBuilder .add (s .getCatalogName ().getValue ());
290295 case DropSchema s -> setCatalogAndSchemaNameFromSchemaQualifiedName (Optional .of (s .getSchemaName ()), catalogBuilder , schemaBuilder , catalogSchemaBuilder );
291296 case DropTable s -> tableBuilder .add (qualifyName (s .getTableName ()));
297+ case Query q -> q .getWith ().ifPresent (with -> temporaryTables .addAll (with .getQueries ().stream ().map (WithQuery ::getName ).map (Identifier ::getValue ).map (QualifiedName ::of ).toList ()));
292298 case RenameMaterializedView s -> {
293299 tableBuilder .add (qualifyName (s .getSource ()));
294300 tableBuilder .add (qualifyName (s .getTarget ()));
@@ -347,13 +353,18 @@ private void visitNode(Node node, ImmutableSet.Builder<QualifiedName> tableBuild
347353 case SetSchemaAuthorization s -> setCatalogAndSchemaNameFromSchemaQualifiedName (Optional .of (s .getSource ()), catalogBuilder , schemaBuilder , catalogSchemaBuilder );
348354 case SetTableAuthorization s -> tableBuilder .add (qualifyName (s .getSource ()));
349355 case SetViewAuthorization s -> tableBuilder .add (qualifyName (s .getSource ()));
350- case Table s -> tableBuilder .add (qualifyName (s .getName ()));
356+ case Table s -> {
357+ // ignore temporary tables as they can have various table parts
358+ if (!temporaryTables .contains (s .getName ())) {
359+ tableBuilder .add (qualifyName (s .getName ()));
360+ }
361+ }
351362 case TableFunctionInvocation s -> tableBuilder .add (qualifyName (s .getName ()));
352363 default -> {}
353364 }
354365
355366 for (Node child : node .getChildren ()) {
356- visitNode (child , tableBuilder , catalogBuilder , schemaBuilder , catalogSchemaBuilder );
367+ visitNode (child , tableBuilder , catalogBuilder , schemaBuilder , catalogSchemaBuilder , temporaryTables );
357368 }
358369 }
359370
0 commit comments