BrokersTableValuedFunction.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 org.apache.doris.tablefunction;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.thrift.TBrokersMetadataParams;
import org.apache.doris.thrift.TMetaScanRange;
import org.apache.doris.thrift.TMetadataType;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Map;
/**
* The Implement of table valued function
* brokers().
*/
public class BrokersTableValuedFunction extends MetadataTableValuedFunction {
public static final String NAME = "brokers";
private static final String CLUSTER_NAME = "cluster_name";
private static final ImmutableList<Column> SCHEMA = ImmutableList.of(
new Column("Name", ScalarType.createStringType()),
new Column("Host", ScalarType.createStringType()),
new Column("Port", ScalarType.createStringType()),
new Column("Alive", ScalarType.createStringType()),
new Column("LastStartTime", ScalarType.createStringType()),
new Column("LastUpdateTime", ScalarType.createStringType()),
new Column("ErrMsg", ScalarType.createStringType())
);
private static final ImmutableMap<String, Integer> COLUMN_TO_INDEX;
static {
ImmutableMap.Builder<String, Integer> builder = new ImmutableMap.Builder();
for (int i = 0; i < SCHEMA.size(); i++) {
builder.put(SCHEMA.get(i).getName().toLowerCase(), i);
}
COLUMN_TO_INDEX = builder.build();
}
private final String clusterName;
/**
* constructor for BrokersTableValuedFunction
*/
public BrokersTableValuedFunction(Map<String, String> params) throws AnalysisException {
if (params == null) {
throw new AnalysisException("params cannot be null");
}
// 1. Normalize the property map: convert all keys to lowercase
Map<String, String> normalizedParams = Maps.newHashMap();
for (Map.Entry<String, String> entry : params.entrySet()) {
if (entry.getKey() == null) {
throw new AnalysisException("Property key cannot be null");
}
normalizedParams.put(entry.getKey().toLowerCase(), entry.getValue());
}
// 2. Lookup value using the normalized map
String originalClusterName = normalizedParams.get(CLUSTER_NAME);
if (originalClusterName != null) {
if (StringUtils.isBlank(originalClusterName)) {
throw new AnalysisException("Invalid brokers param value: " + originalClusterName);
}
clusterName = originalClusterName.trim();
} else {
clusterName = null;
}
// 3. Validate keys against the normalized map
for (String key : normalizedParams.keySet()) {
if (!CLUSTER_NAME.equals(key)) {
throw new AnalysisException("'" + key + "' is invalid property,"
+ " only support property [" + CLUSTER_NAME + "]");
}
}
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)
&& !Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(),
PrivPredicate.OPERATOR)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN/OPERATOR");
}
}
public static Integer getColumnIndexFromColumnName(String columnName) {
return COLUMN_TO_INDEX.get(columnName.toLowerCase());
}
@Override
public TMetadataType getMetadataType() {
return TMetadataType.BROKERS;
}
@Override
public TMetaScanRange getMetaScanRange(List<String> requiredFields) {
TMetaScanRange metaScanRange = new TMetaScanRange();
metaScanRange.setMetadataType(TMetadataType.BROKERS);
TBrokersMetadataParams brokersMetadataParams = new TBrokersMetadataParams();
brokersMetadataParams.setClusterName(clusterName);
metaScanRange.setBrokersParams(brokersMetadataParams);
return metaScanRange;
}
@Override
public String getTableName() {
return "BrokersTableValuedFunction";
}
@Override
public List<Column> getTableColumns() throws AnalysisException {
return SCHEMA;
}
}