}
@Override
protected TupleDescriptor visitShowPartitions(ShowPartitions showPartitions, AnalysisContext context)
{
QualifiedTableName table = MetadataUtil.createQualifiedTableName(session, showPartitions.getTable());
Optional<TableHandle> tableHandle = metadata.getTableHandle(session, table);
if (!tableHandle.isPresent()) {
throw new SemanticException(MISSING_TABLE, showPartitions, "Table '%s' does not exist", table);
}
/*
Generate a dynamic pivot to output one column per partition key.
For example, a table with two partition keys (ds, cluster_name)
would generate the following query:
SELECT
partition_number
, max(CASE WHEN partition_key = 'ds' THEN partition_value END) ds
, max(CASE WHEN partition_key = 'cluster_name' THEN partition_value END) cluster_name
FROM ...
GROUP BY partition_number
The values are also cast to the type of the partition column.
The query is then wrapped to allow custom filtering and ordering.
*/
ImmutableList.Builder<SelectItem> selectList = ImmutableList.builder();
ImmutableList.Builder<SelectItem> wrappedList = ImmutableList.builder();
selectList.add(unaliasedName("partition_number"));
for (ColumnMetadata column : metadata.getTableMetadata(tableHandle.get()).getColumns()) {
if (!column.isPartitionKey()) {
continue;
}
Expression key = equal(nameReference("partition_key"), new StringLiteral(column.getName()));
Expression value = caseWhen(key, nameReference("partition_value"));
value = new Cast(value, column.getType().getName());
Expression function = functionCall("max", value);
selectList.add(new SingleColumn(function, column.getName()));
wrappedList.add(unaliasedName(column.getName()));
}
Query query = new Query(
Optional.<With>absent(),
new QuerySpecification(
selectAll(selectList.build()),
table(QualifiedName.of(table.getCatalogName(), TABLE_INTERNAL_PARTITIONS.getSchemaName(), TABLE_INTERNAL_PARTITIONS.getTableName())),
Optional.of(logicalAnd(
equal(nameReference("table_schema"), new StringLiteral(table.getSchemaName())),
equal(nameReference("table_name"), new StringLiteral(table.getTableName())))),
ImmutableList.of(nameReference("partition_number")),
Optional.<Expression>absent(),
ImmutableList.<SortItem>of(),
Optional.<String>absent()),
ImmutableList.<SortItem>of(),