|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.nereids.trees.plans.commands; |
| 19 | + |
| 20 | +import org.apache.doris.catalog.Column; |
| 21 | +import org.apache.doris.catalog.DatabaseIf; |
| 22 | +import org.apache.doris.catalog.Env; |
| 23 | +import org.apache.doris.catalog.ScalarType; |
| 24 | +import org.apache.doris.catalog.TableIf; |
| 25 | +import org.apache.doris.catalog.Type; |
| 26 | +import org.apache.doris.common.ErrorCode; |
| 27 | +import org.apache.doris.common.ErrorReport; |
| 28 | +import org.apache.doris.common.Pair; |
| 29 | +import org.apache.doris.common.UserException; |
| 30 | +import org.apache.doris.common.util.Util; |
| 31 | +import org.apache.doris.datasource.CatalogIf; |
| 32 | +import org.apache.doris.mysql.privilege.PrivPredicate; |
| 33 | +import org.apache.doris.nereids.trees.plans.PlanType; |
| 34 | +import org.apache.doris.nereids.trees.plans.commands.info.TableNameInfo; |
| 35 | +import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; |
| 36 | +import org.apache.doris.qe.ConnectContext; |
| 37 | +import org.apache.doris.qe.ShowResultSet; |
| 38 | +import org.apache.doris.qe.ShowResultSetMetaData; |
| 39 | +import org.apache.doris.qe.StmtExecutor; |
| 40 | +import org.apache.doris.statistics.Histogram; |
| 41 | + |
| 42 | +import com.google.common.collect.ImmutableList; |
| 43 | +import com.google.common.collect.Lists; |
| 44 | +import com.google.common.collect.Sets; |
| 45 | + |
| 46 | +import java.util.List; |
| 47 | +import java.util.Objects; |
| 48 | +import java.util.Optional; |
| 49 | +import java.util.Set; |
| 50 | +import java.util.stream.Collectors; |
| 51 | + |
| 52 | +/** |
| 53 | + * Represents the command for SHOW COLUMN HISTOGRAM |
| 54 | + */ |
| 55 | +public class ShowColumnHistogramStatsCommand extends ShowCommand { |
| 56 | + /** |
| 57 | + * TITLE_NAMES |
| 58 | + */ |
| 59 | + private static final ImmutableList<String> TITLE_NAMES = |
| 60 | + new ImmutableList.Builder<String>() |
| 61 | + .add("column_name") |
| 62 | + .add("data_type") |
| 63 | + .add("sample_rate") |
| 64 | + .add("num_buckets") |
| 65 | + .add("buckets") |
| 66 | + .build(); |
| 67 | + |
| 68 | + private TableNameInfo tableNameInfo; |
| 69 | + |
| 70 | + private final List<String> columnNames; |
| 71 | + |
| 72 | + private TableIf table; |
| 73 | + |
| 74 | + public ShowColumnHistogramStatsCommand(TableNameInfo tableNameInfo, List<String> columnNames) { |
| 75 | + super(PlanType.SHOW_COLUMN_HISTOGRAM); |
| 76 | + this.tableNameInfo = tableNameInfo; |
| 77 | + this.columnNames = columnNames; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * validate |
| 82 | + */ |
| 83 | + public void validate(ConnectContext ctx) throws UserException { |
| 84 | + tableNameInfo.analyze(ctx); |
| 85 | + |
| 86 | + // disallow external catalog |
| 87 | + Util.prohibitExternalCatalog(tableNameInfo.getCtl(), this.getClass().getSimpleName()); |
| 88 | + CatalogIf<DatabaseIf> catalog = Env.getCurrentEnv().getCatalogMgr().getCatalog(tableNameInfo.getCtl()); |
| 89 | + if (catalog == null) { |
| 90 | + ErrorReport.reportAnalysisException("Catalog: {} not exists", tableNameInfo.getCtl()); |
| 91 | + } |
| 92 | + DatabaseIf<TableIf> db = catalog.getDb(tableNameInfo.getDb()).orElse(null); |
| 93 | + if (db == null) { |
| 94 | + ErrorReport.reportAnalysisException("DB: {} not exists", tableNameInfo.getDb()); |
| 95 | + } |
| 96 | + table = db.getTable(tableNameInfo.getTbl()).orElse(null); |
| 97 | + if (table == null) { |
| 98 | + ErrorReport.reportAnalysisException("Table: {} not exists", tableNameInfo.getTbl()); |
| 99 | + } |
| 100 | + |
| 101 | + if (!Env.getCurrentEnv().getAccessManager() |
| 102 | + .checkTblPriv(ConnectContext.get(), tableNameInfo.getCtl(), tableNameInfo.getDb(), |
| 103 | + tableNameInfo.getTbl(), PrivPredicate.SHOW)) { |
| 104 | + ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLEACCESS_DENIED_ERROR, "Permission denied", |
| 105 | + ConnectContext.get().getQualifiedUser(), ConnectContext.get().getRemoteIP(), |
| 106 | + tableNameInfo.getDb() + ": " + tableNameInfo.getTbl()); |
| 107 | + } |
| 108 | + |
| 109 | + if (columnNames != null) { |
| 110 | + Optional<Column> nullColumn = columnNames.stream() |
| 111 | + .map(table::getColumn) |
| 112 | + .filter(Objects::isNull) |
| 113 | + .findFirst(); |
| 114 | + if (nullColumn.isPresent()) { |
| 115 | + ErrorReport.reportAnalysisException("Column: {} not exists", nullColumn.get()); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * getMetaData |
| 122 | + */ |
| 123 | + public ShowResultSetMetaData getMetaData() { |
| 124 | + ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder(); |
| 125 | + |
| 126 | + for (String title : TITLE_NAMES) { |
| 127 | + builder.addColumn(new Column(title, ScalarType.createVarchar(30))); |
| 128 | + } |
| 129 | + return builder.build(); |
| 130 | + } |
| 131 | + |
| 132 | + public TableIf getTable() { |
| 133 | + return table; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * constructResultSet |
| 138 | + */ |
| 139 | + public ShowResultSet constructResultSet(List<Pair<String, Histogram>> columnStatistics) { |
| 140 | + List<List<String>> result = Lists.newArrayList(); |
| 141 | + columnStatistics.forEach(p -> { |
| 142 | + if (p.second == null || p.second.dataType == Type.NULL) { |
| 143 | + return; |
| 144 | + } |
| 145 | + List<String> row = Lists.newArrayList(); |
| 146 | + row.add(p.first); |
| 147 | + row.add(String.valueOf(p.second.dataType)); |
| 148 | + row.add(String.valueOf(p.second.sampleRate)); |
| 149 | + row.add(String.valueOf(p.second.numBuckets)); |
| 150 | + row.add(Histogram.getBucketsJson(p.second.buckets).toString()); |
| 151 | + result.add(row); |
| 152 | + }); |
| 153 | + |
| 154 | + return new ShowResultSet(getMetaData(), result); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * getColumnNames |
| 159 | + */ |
| 160 | + public Set<String> getColumnNames() { |
| 161 | + if (columnNames != null) { |
| 162 | + return Sets.newHashSet(columnNames); |
| 163 | + } |
| 164 | + return table.getColumns().stream() |
| 165 | + .map(Column::getName).collect(Collectors.toSet()); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception { |
| 170 | + validate(ctx); |
| 171 | + List<Pair<String, Histogram>> columnStatistics = Lists.newArrayList(); |
| 172 | + // TODO: support histogram in the future. |
| 173 | + return constructResultSet(columnStatistics); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public <R, C> R accept(PlanVisitor<R, C> visitor, C context) { |
| 178 | + return visitor.visitShowColumnHistogramStatsCommand(this, context); |
| 179 | + } |
| 180 | +} |
0 commit comments