Skip to content

Commit b16672a

Browse files
committed
[fix](meta) show partitions with Limit for external HMS tables (27835)
Issue:#27834
1 parent 45a49ac commit b16672a

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

fe/fe-core/src/main/java/org/apache/doris/analysis/ShowPartitionsStmt.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class ShowPartitionsStmt extends ShowStmt {
5858
private static final Logger LOG = LogManager.getLogger(ShowPartitionsStmt.class);
5959

6060
private static final String FILTER_PARTITION_ID = "PartitionId";
61-
private static final String FILTER_PARTITION_NAME = "PartitionName";
61+
public static final String FILTER_PARTITION_NAME = "PartitionName";
6262
private static final String FILTER_STATE = "State";
6363
private static final String FILTER_BUCKETS = "Buckets";
6464
private static final String FILTER_REPLICATION_NUM = "ReplicationNum";
@@ -198,6 +198,11 @@ public void analyzeImpl(Analyzer analyzer) throws UserException {
198198
throw new AnalysisException("Should order by column");
199199
}
200200
SlotRef slotRef = (SlotRef) orderByElement.getExpr();
201+
if (catalog instanceof HMSExternalCatalog
202+
&& !slotRef.getColumnName().equalsIgnoreCase(FILTER_PARTITION_NAME)) {
203+
throw new AnalysisException("External table only support Order By on PartitionName");
204+
}
205+
201206
int index = PartitionsProcDir.analyzeColumn(slotRef.getColumnName());
202207
OrderByPair orderByPair = new OrderByPair(index, !orderByElement.getIsAsc());
203208
orderByPairs.add(orderByPair);
@@ -228,6 +233,10 @@ private void analyzeSubPredicate(Expr subExpr) throws AnalysisException {
228233
}
229234

230235
String leftKey = ((SlotRef) subExpr.getChild(0)).getColumnName();
236+
if (catalog instanceof HMSExternalCatalog && !leftKey.equalsIgnoreCase(FILTER_PARTITION_NAME)) {
237+
throw new AnalysisException(String.format("Only %s column supported in where clause for this catalog",
238+
FILTER_PARTITION_NAME));
239+
}
231240
if (subExpr instanceof BinaryPredicate) {
232241
BinaryPredicate binaryPredicate = (BinaryPredicate) subExpr;
233242
if (leftKey.equalsIgnoreCase(FILTER_PARTITION_NAME) || leftKey.equalsIgnoreCase(FILTER_STATE)) {

fe/fe-core/src/main/java/org/apache/doris/common/proc/PartitionsProcDir.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public PartitionsProcDir(Database db, OlapTable olapTable, boolean isTempPartiti
8181
this.isTempPartition = isTempPartition;
8282
}
8383

84-
public boolean filter(String columnName, Comparable element, Map<String, Expr> filterMap) throws AnalysisException {
84+
public static boolean filter(String columnName, Comparable element, Map<String, Expr> filterMap)
85+
throws AnalysisException {
8586
if (filterMap == null) {
8687
return true;
8788
}
@@ -142,7 +143,7 @@ public boolean filter(String columnName, Comparable element, Map<String, Expr> f
142143
return true;
143144
}
144145

145-
public boolean like(String str, String expr) {
146+
public static boolean like(String str, String expr) {
146147
expr = expr.toLowerCase();
147148
expr = expr.replace(".", "\\.");
148149
expr = expr.replace("?", ".");

fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java

+32-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.doris.analysis.AdminShowReplicaStatusStmt;
2525
import org.apache.doris.analysis.AdminShowTabletStorageFormatStmt;
2626
import org.apache.doris.analysis.DescribeStmt;
27+
import org.apache.doris.analysis.Expr;
2728
import org.apache.doris.analysis.HelpStmt;
2829
import org.apache.doris.analysis.LimitElement;
2930
import org.apache.doris.analysis.PartitionNames;
@@ -1691,28 +1692,54 @@ private void handleShowMaxComputeTablePartitions(ShowPartitionsStmt showStmt) {
16911692
resultSet = new ShowResultSet(showStmt.getMetaData(), rows);
16921693
}
16931694

1694-
private void handleShowHMSTablePartitions(ShowPartitionsStmt showStmt) {
1695+
private void handleShowHMSTablePartitions(ShowPartitionsStmt showStmt) throws AnalysisException {
16951696
HMSExternalCatalog catalog = (HMSExternalCatalog) (showStmt.getCatalog());
16961697
List<List<String>> rows = new ArrayList<>();
16971698
String dbName = ClusterNamespace.getNameFromFullName(showStmt.getTableName().getDb());
16981699

16991700
List<String> partitionNames;
17001701
LimitElement limit = showStmt.getLimitElement();
1701-
if (limit != null && limit.hasLimit()) {
1702-
// only limit is valid on Hive
1702+
if (limit != null && limit.hasLimit()
1703+
&& (!limit.hasOffset() || limit.getOffset() == 0)) {
1704+
// hmsClient returns unordered partition list, hence if offset > 0 cannot pass limit
17031705
partitionNames = catalog.getClient()
1704-
.listPartitionNames(dbName, showStmt.getTableName().getTbl(), limit.getLimit());
1706+
.listPartitionNames(dbName, showStmt.getTableName().getTbl(), limit.getLimit());
17051707
} else {
17061708
partitionNames = catalog.getClient().listPartitionNames(dbName, showStmt.getTableName().getTbl());
17071709
}
1710+
1711+
Map<String, Expr> filterMap = showStmt.getFilterMap();
1712+
List<OrderByPair> orderByPairs = showStmt.getOrderByPairs();
1713+
LimitElement limitElement = showStmt.getLimitElement();
1714+
1715+
/* Filter add rows */
17081716
for (String partition : partitionNames) {
17091717
List<String> list = new ArrayList<>();
1718+
1719+
if (filterMap != null && !filterMap.isEmpty()) {
1720+
if (!PartitionsProcDir.filter(ShowPartitionsStmt.FILTER_PARTITION_NAME, partition, filterMap)) {
1721+
continue;
1722+
}
1723+
}
17101724
list.add(partition);
17111725
rows.add(list);
17121726
}
17131727

17141728
// sort by partition name
1715-
rows.sort(Comparator.comparing(x -> x.get(0)));
1729+
if (orderByPairs != null && orderByPairs.get(0).isDesc()) {
1730+
rows.sort(Comparator.comparing(x -> x.get(0), Comparator.reverseOrder()));
1731+
} else {
1732+
rows.sort(Comparator.comparing(x -> x.get(0)));
1733+
}
1734+
1735+
if (limitElement != null && limitElement.hasLimit()) {
1736+
int beginIndex = (int) limitElement.getOffset();
1737+
int endIndex = (int) (beginIndex + limitElement.getLimit());
1738+
if (endIndex > rows.size()) {
1739+
endIndex = rows.size();
1740+
}
1741+
rows = rows.subList(beginIndex, endIndex);
1742+
}
17161743

17171744
resultSet = new ShowResultSet(showStmt.getMetaData(), rows);
17181745
}

0 commit comments

Comments
 (0)