forked from StarRocks/starrocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShowPartitionsStmt.java
194 lines (162 loc) · 6.02 KB
/
ShowPartitionsStmt.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// This file is made available under Elastic License 2.0.
// This file is based on code available under the Apache license here:
// https://github.com/apache/incubator-doris/blob/master/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowPartitionsStmt.java
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.starrocks.analysis;
import com.google.common.base.Strings;
import com.starrocks.catalog.Column;
import com.starrocks.catalog.ScalarType;
import com.starrocks.common.AnalysisException;
import com.starrocks.common.proc.ProcNodeInterface;
import com.starrocks.common.proc.ProcResult;
import com.starrocks.common.util.OrderByPair;
import com.starrocks.qe.ShowResultSetMetaData;
import com.starrocks.sql.ast.AstVisitor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ShowPartitionsStmt extends ShowStmt {
public static final String FILTER_PARTITION_ID = "PartitionId";
public static final String FILTER_PARTITION_NAME = "PartitionName";
public static final String FILTER_STATE = "State";
public static final String FILTER_BUCKETS = "Buckets";
public static final String FILTER_REPLICATION_NUM = "ReplicationNum";
public static final String FILTER_LAST_CONSISTENCY_CHECK_TIME = "LastConsistencyCheckTime";
public static final Set<String> FILTER_COLUMNS;
static {
FILTER_COLUMNS = new HashSet<>();
FILTER_COLUMNS.add(FILTER_PARTITION_ID);
FILTER_COLUMNS.add(FILTER_PARTITION_NAME);
FILTER_COLUMNS.add(FILTER_STATE);
FILTER_COLUMNS.add(FILTER_BUCKETS);
FILTER_COLUMNS.add(FILTER_REPLICATION_NUM);
FILTER_COLUMNS.add(FILTER_LAST_CONSISTENCY_CHECK_TIME);
}
private String dbName;
private String tableName;
private Expr whereClause;
private List<OrderByElement> orderByElements;
private LimitElement limitElement;
private boolean isTempPartition = false;
private List<OrderByPair> orderByPairs;
private Map<String, Expr> filterMap;
private ProcNodeInterface node;
public ShowPartitionsStmt(TableName tableName, Expr whereClause, List<OrderByElement> orderByElements,
LimitElement limitElement, boolean isTempPartition) {
this.dbName = tableName.getDb();
this.tableName = tableName.getTbl();
this.whereClause = whereClause;
this.orderByElements = orderByElements;
this.limitElement = limitElement;
if (whereClause != null) {
this.filterMap = new HashMap<>();
}
this.isTempPartition = isTempPartition;
}
public List<OrderByPair> getOrderByPairs() {
return orderByPairs;
}
public LimitElement getLimitElement() {
return limitElement;
}
public Map<String, Expr> getFilterMap() {
return filterMap;
}
public ProcNodeInterface getNode() {
return node;
}
@Override
public ShowResultSetMetaData getMetaData() {
ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder();
ProcResult result = null;
try {
result = node.fetchResult();
} catch (AnalysisException e) {
return builder.build();
}
for (String col : result.getColumnNames()) {
builder.addColumn(new Column(col, ScalarType.createVarchar(30)));
}
return builder.build();
}
@Override
public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append("SHOW PARTITIONS FROM ");
if (!Strings.isNullOrEmpty(dbName)) {
sb.append("`").append(dbName).append("`");
}
if (!Strings.isNullOrEmpty(tableName)) {
sb.append(".`").append(tableName).append("`");
}
if (whereClause != null) {
sb.append(" WHERE ").append(whereClause.toSql());
}
// Order By clause
if (orderByElements != null && !orderByElements.isEmpty()) {
sb.append(" ORDER BY ");
for (int i = 0; i < orderByElements.size(); ++i) {
sb.append(orderByElements.get(i).toSql());
sb.append((i + 1 != orderByElements.size()) ? ", " : "");
}
}
if (limitElement != null) {
sb.append(limitElement.toSql());
}
return sb.toString();
}
@Override
public String toString() {
return toSql();
}
public String getDbName() {
return dbName;
}
public String getTableName() {
return tableName;
}
public Expr getWhereClause() {
return whereClause;
}
public List<OrderByElement> getOrderByElements() {
return orderByElements;
}
public boolean isTempPartition() {
return isTempPartition;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public void setOrderByPairs(List<OrderByPair> orderByPairs) {
this.orderByPairs = orderByPairs;
}
public void setNode(ProcNodeInterface node) {
this.node = node;
}
@Override
public boolean isSupportNewPlanner() {
return true;
}
@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitShowPartitionsStmt(this, context);
}
}