[Enhancement] add information_schema.table_properties #38745 (#38746) (#39886)

bp #38746

---------

Co-authored-by: Vallish Pai <vallishpai@gmail.com>
This commit is contained in:
Mingyu Chen
2024-08-27 17:22:19 +08:00
committed by GitHub
parent bbf7701046
commit 173aafc86f
9 changed files with 671 additions and 6 deletions

View File

@ -82,9 +82,10 @@ public enum SchemaTableType {
TSchemaTableType.SCH_FILE_CACHE_STATISTICS),
SCH_WORKLOAD_GROUP_PRIVILEGES("WORKLOAD_GROUP_PRIVILEGES",
"WORKLOAD_GROUP_PRIVILEGES", TSchemaTableType.SCH_WORKLOAD_GROUP_PRIVILEGES),
SCH_WORKLOAD_GROUP_RESOURCE_USAGE("WORKLOAD_GROUP_RESOURCE_USAGE",
"WORKLOAD_GROUP_RESOURCE_USAGE", TSchemaTableType.SCH_WORKLOAD_GROUP_RESOURCE_USAGE);
"WORKLOAD_GROUP_RESOURCE_USAGE", TSchemaTableType.SCH_WORKLOAD_GROUP_RESOURCE_USAGE),
SCH_TABLE_PROPERTIES("TABLE_PROPERTIES", "TABLE_PROPERTIES",
TSchemaTableType.SCH_TABLE_PROPERTIES);
private static final String dbName = "INFORMATION_SCHEMA";
private static SelectList fullSelectLists;

View File

@ -532,8 +532,7 @@ public class SchemaTable extends Table {
.column("WORKLOAD_GROUP_NAME", ScalarType.createVarchar(256))
.column("PRIVILEGE_TYPE", ScalarType.createVarchar(PRIVILEGE_TYPE_LEN))
.column("IS_GRANTABLE", ScalarType.createVarchar(IS_GRANTABLE_LEN))
.build())
)
.build()))
.put("workload_group_resource_usage",
new SchemaTable(SystemIdGenerator.getNextId(), "workload_group_resource_usage", TableType.SCHEMA,
builder().column("BE_ID", ScalarType.createType(PrimitiveType.BIGINT))
@ -542,8 +541,15 @@ public class SchemaTable extends Table {
.column("CPU_USAGE_PERCENT", ScalarType.createType(PrimitiveType.DOUBLE))
.column("LOCAL_SCAN_BYTES_PER_SECOND", ScalarType.createType(PrimitiveType.BIGINT))
.column("REMOTE_SCAN_BYTES_PER_SECOND", ScalarType.createType(PrimitiveType.BIGINT))
.build())
)
.build()))
.put("table_properties",
new SchemaTable(SystemIdGenerator.getNextId(), "table_properties", TableType.SCHEMA,
builder().column("TABLE_CATALOG", ScalarType.createVarchar(NAME_CHAR_LEN))
.column("TABLE_SCHEMA", ScalarType.createVarchar(NAME_CHAR_LEN))
.column("TABLE_NAME", ScalarType.createVarchar(NAME_CHAR_LEN))
.column("PROPERTY_NAME", ScalarType.createStringType())
.column("PROPERTY_VALUE", ScalarType.createStringType())
.build()))
.build();
private boolean fetchAllFe = false;

View File

@ -27,6 +27,7 @@ import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.SchemaTable;
import org.apache.doris.catalog.Table;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.TableProperty;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ClientPool;
import org.apache.doris.common.Pair;
@ -36,6 +37,7 @@ import org.apache.doris.common.proc.PartitionsProcDir;
import org.apache.doris.common.util.NetUtils;
import org.apache.doris.common.util.TimeUtils;
import org.apache.doris.datasource.CatalogIf;
import org.apache.doris.datasource.ExternalCatalog;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.datasource.hive.HMSExternalCatalog;
import org.apache.doris.datasource.iceberg.IcebergMetadataCache;
@ -108,6 +110,8 @@ public class MetadataGenerator {
private static final ImmutableMap<String, Integer> WORKLOAD_GROUP_PRIVILEGES_COLUMN_TO_INDEX;
private static final ImmutableMap<String, Integer> TABLE_PROPERTIES_COLUMN_TO_INDEX;
static {
ImmutableMap.Builder<String, Integer> activeQueriesbuilder = new ImmutableMap.Builder();
List<Column> activeQueriesColList = SchemaTable.TABLE_MAP.get("active_queries").getFullSchema();
@ -141,6 +145,13 @@ public class MetadataGenerator {
wgPrivsBuilder.put(wgPrivsColList.get(i).getName().toLowerCase(), i);
}
WORKLOAD_GROUP_PRIVILEGES_COLUMN_TO_INDEX = wgPrivsBuilder.build();
ImmutableMap.Builder<String, Integer> propertiesBuilder = new ImmutableMap.Builder();
List<Column> propertiesColList = SchemaTable.TABLE_MAP.get("table_properties").getFullSchema();
for (int i = 0; i < propertiesColList.size(); i++) {
propertiesBuilder.put(propertiesColList.get(i).getName().toLowerCase(), i);
}
TABLE_PROPERTIES_COLUMN_TO_INDEX = propertiesBuilder.build();
}
public static TFetchSchemaTableDataResult getMetadataTable(TFetchSchemaTableDataRequest request) throws TException {
@ -224,6 +235,10 @@ public class MetadataGenerator {
result = workloadGroupPrivsMetadataResult(schemaTableParams);
columnIndex = WORKLOAD_GROUP_PRIVILEGES_COLUMN_TO_INDEX;
break;
case TABLE_PROPERTIES:
result = tablePropertiesMetadataResult(schemaTableParams);
columnIndex = TABLE_PROPERTIES_COLUMN_TO_INDEX;
break;
default:
return errorResult("invalid schema table name.");
}
@ -1013,4 +1028,91 @@ public class MetadataGenerator {
result.setStatus(new TStatus(TStatusCode.OK));
return result;
}
private static void tablePropertiesForInternalCatalog(UserIdentity currentUserIdentity,
CatalogIf catalog, DatabaseIf database, List<TableIf> tables, List<TRow> dataBatch) {
for (TableIf table : tables) {
if (!(table instanceof OlapTable)) {
continue;
}
if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(currentUserIdentity, catalog.getName(),
database.getFullName(), table.getName(), PrivPredicate.SHOW)) {
continue;
}
OlapTable olapTable = (OlapTable) table;
TableProperty property = olapTable.getTableProperty();
if (property == null) {
// if there is no properties, then write empty properties and check next table.
TRow trow = new TRow();
trow.addToColumnValue(new TCell().setStringVal(catalog.getName())); // TABLE_CATALOG
trow.addToColumnValue(new TCell().setStringVal(database.getFullName())); // TABLE_SCHEMA
trow.addToColumnValue(new TCell().setStringVal(table.getName())); // TABLE_NAME
trow.addToColumnValue(new TCell().setStringVal("")); // PROPERTIES_NAME
trow.addToColumnValue(new TCell().setStringVal("")); // PROPERTIES_VALUE
dataBatch.add(trow);
continue;
}
Map<String, String> propertiesMap = property.getProperties();
propertiesMap.forEach((key, value) -> {
TRow trow = new TRow();
trow.addToColumnValue(new TCell().setStringVal(catalog.getName())); // TABLE_CATALOG
trow.addToColumnValue(new TCell().setStringVal(database.getFullName())); // TABLE_SCHEMA
trow.addToColumnValue(new TCell().setStringVal(table.getName())); // TABLE_NAME
trow.addToColumnValue(new TCell().setStringVal(key)); // PROPERTIES_NAME
trow.addToColumnValue(new TCell().setStringVal(value)); // PROPERTIES_VALUE
dataBatch.add(trow);
});
} // for table
}
private static void tablePropertiesForExternalCatalog(UserIdentity currentUserIdentity,
CatalogIf catalog, DatabaseIf database, List<TableIf> tables, List<TRow> dataBatch) {
for (TableIf table : tables) {
if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(currentUserIdentity, catalog.getName(),
database.getFullName(), table.getName(), PrivPredicate.SHOW)) {
continue;
}
// Currently for external catalog, we put properties as empty, can extend in future
TRow trow = new TRow();
trow.addToColumnValue(new TCell().setStringVal(catalog.getName())); // TABLE_CATALOG
trow.addToColumnValue(new TCell().setStringVal(database.getFullName())); // TABLE_SCHEMA
trow.addToColumnValue(new TCell().setStringVal(table.getName())); // TABLE_NAME
trow.addToColumnValue(new TCell().setStringVal("")); // PROPERTIES_NAME
trow.addToColumnValue(new TCell().setStringVal("")); // PROPERTIES_VALUE
dataBatch.add(trow);
} // for table
}
private static TFetchSchemaTableDataResult tablePropertiesMetadataResult(TSchemaTableRequestParams params) {
if (!params.isSetCurrentUserIdent()) {
return errorResult("current user ident is not set.");
}
if (!params.isSetDbId()) {
return errorResult("current db id is not set.");
}
if (!params.isSetCatalog()) {
return errorResult("current catalog is not set.");
}
TUserIdentity tcurrentUserIdentity = params.getCurrentUserIdent();
UserIdentity currentUserIdentity = UserIdentity.fromThrift(tcurrentUserIdentity);
TFetchSchemaTableDataResult result = new TFetchSchemaTableDataResult();
Long dbId = params.getDbId();
String clg = params.getCatalog();
CatalogIf catalog = Env.getCurrentEnv().getCatalogMgr().getCatalog(clg);
List<TRow> dataBatch = Lists.newArrayList();
DatabaseIf database = catalog.getDbNullable(dbId);
List<TableIf> tables = database.getTables();
if (catalog instanceof InternalCatalog) {
tablePropertiesForInternalCatalog(currentUserIdentity, catalog, database, tables, dataBatch);
} else if (catalog instanceof ExternalCatalog) {
tablePropertiesForExternalCatalog(currentUserIdentity, catalog, database, tables, dataBatch);
}
result.setDataBatch(dataBatch);
result.setStatus(new TStatus(TStatusCode.OK));
return result;
}
}