Refactor SHOW PARTITIONS implementation - extract methods
Extract following methods:
- ShowQueriesRewrite#Visitor#getPartitioningColumns
- ShowQueriesRewrite#Visitor#getPartitionsAsValuesRows
@@ -372,15 +372,50 @@ final class ShowQueriesRewrite
TableLayout layout = getLayout(showPartitions, showPartitions.getTable(), showPartitions.getWhere())
.orElseThrow(() -> new SemanticException(NOT_SUPPORTED, showPartitions, "Could not find layout for table: %s", table));
+ List<Expression> rows = getPartitionsAsValuesRows(showPartitions, table, layout);
+ List<String> partitioningColumns = getPartitioningColumns(showPartitions, table, tableHandle, layout);
- Map<ColumnHandle, String> columnHandleNames = ImmutableBiMap.copyOf(metadata.getColumnHandles(session, tableHandle)).inverse();
+ Optional<Expression> where;
+ if (rows.isEmpty()) {
+ // VALUES does not allow no rows
+ List<Expression> row = partitioningColumns.stream()
+ .map(partitioningColumn -> new NullLiteral())
+ .collect(toImmutableList());
+ rows = ImmutableList.of(new Row(row));
+ where = Optional.of(FALSE_LITERAL);
+ }
+ else {
+ where = showPartitions.getWhere();
+ }
+ return simpleQuery(
+ selectList(partitioningColumns.stream().map(QueryUtil::identifier).collect(toImmutableList())),
+ aliased(new Values(rows), "x", partitioningColumns),
+ where,
+ Optional.empty(),
+ Optional.empty(),
+ orderBy(showPartitions.getOrderBy()),
+ showPartitions.getLimit());
+ }
+
+ private List<String> getPartitioningColumns(
+ ShowPartitions showPartitions,
+ QualifiedObjectName table,
+ TableHandle tableHandle,
+ TableLayout layout)
+ {
+ Map<ColumnHandle, String> columnHandleNames = ImmutableBiMap.copyOf(metadata.getColumnHandles(session, tableHandle)).inverse();
DiscretePredicates discretePredicates = layout.getDiscretePredicates()
.orElseThrow(() -> new SemanticException(NOT_SUPPORTED, showPartitions, "Table does not have partition columns: %s", table));
-
- List<String> partitioningColumns = discretePredicates.getColumns().stream()
+ return discretePredicates.getColumns().stream()
.map(columnHandle -> requireNonNull(columnHandleNames.get(columnHandle), "no column name for handle"))
.collect(toImmutableList());
+ }
+
+ private List<Expression> getPartitionsAsValuesRows(ShowPartitions showPartitions, QualifiedObjectName table, TableLayout layout)
+ {
+ DiscretePredicates discretePredicates = layout.getDiscretePredicates()
+ .orElseThrow(() -> new SemanticException(NOT_SUPPORTED, showPartitions, "Table does not have partition columns: %s", table));
Map<ColumnHandle, Integer> partitioningColumnIndex = IntStream.range(0, discretePredicates.getColumns().size())
.boxed()
@@ -398,29 +433,7 @@ final class ShowQueriesRewrite
rowBuilder.add(new Row(row));
}
-
- List<Expression> rows = rowBuilder.build();
- Optional<Expression> where;
- if (rows.isEmpty()) {
- // VALUES does not allow no rows
- List<Expression> row = partitioningColumns.stream()
- .map(partitioningColumn -> new NullLiteral())
- .collect(toImmutableList());
- rows = ImmutableList.of(new Row(row));
- where = Optional.of(FALSE_LITERAL);
- }
- else {
- where = showPartitions.getWhere();
- }
-
- return simpleQuery(
- selectList(partitioningColumns.stream().map(QueryUtil::identifier).collect(toImmutableList())),
- aliased(new Values(rows), "x", partitioningColumns),
- where,
- Optional.empty(),
- Optional.empty(),
- orderBy(showPartitions.getOrderBy()),
- showPartitions.getLimit());
+ return rowBuilder.build();
}
private Optional<TableLayout> getLayout(Node node, QualifiedName tableName, Optional<Expression> where)
