Skip to content

Commit d69f701

Browse files
committed
Fix failure when SHOW BRANCHES result is empty
1 parent 0dcb1a0 commit d69f701

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

core/trino-main/src/main/java/io/trino/sql/rewrite/ShowQueriesRewrite.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
import io.trino.sql.tree.TableElement;
101101
import io.trino.sql.tree.Values;
102102

103+
import java.util.ArrayList;
103104
import java.util.Collection;
104105
import java.util.List;
105106
import java.util.Map;
@@ -839,16 +840,21 @@ protected Node visitShowBranches(ShowBranches showBranches, Void context)
839840
throw semanticException(TABLE_NOT_FOUND, showBranches, "Table '%s' does not exist", tableName);
840841
}
841842

842-
ImmutableList.Builder<Expression> rows = ImmutableList.builder();
843+
String columnName = "Branch";
844+
List<Expression> rows = new ArrayList<>();
843845
for (String branch : metadata.listBranches(session, tableName)) {
844846
rows.add(row(new StringLiteral(branch)));
845847
}
846848

849+
if (rows.isEmpty()) {
850+
return emptyQuery(ImmutableList.of(columnName), ImmutableList.of(VARCHAR));
851+
}
852+
847853
return simpleQuery(
848854
selectList(
849855
aliasedName("branch_name", "Branch")),
850856
aliased(
851-
new Values(rows.build()),
857+
new Values(ImmutableList.copyOf(rows)),
852858
"branches",
853859
ImmutableList.of("branch_name")));
854860
}

core/trino-main/src/test/java/io/trino/sql/query/TestShowQueries.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,12 @@ public void testShowCreateViewWithProperties()
317317
mock_table""");
318318
}
319319

320+
@Test
321+
void testShowBranchesEmptyResult()
322+
{
323+
assertions.assertQueryReturnsEmptyResult("SHOW BRANCHES FROM TABLE mock.mockSchema.mockTable");
324+
}
325+
320326
private static ColumnMetadata columnMetadata(String name, Type type, String value)
321327
{
322328
return ColumnMetadata.builder().setName(name).setType(type).setDefaultValue(Optional.of(value)).build();

0 commit comments

Comments
 (0)