-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] sys.object_dependencies (#35060)
Signed-off-by: Murphy <[email protected]> (cherry picked from commit 9756c20) Signed-off-by: Murphy <[email protected]>
- Loading branch information
1 parent
9992179
commit 4b1ade9
Showing
15 changed files
with
455 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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. | ||
|
||
#include "exec/schema_scanner/sys_object_dependencies.h" | ||
|
||
#include "exec/schema_scanner/schema_helper.h" | ||
#include "exec/schema_scanner/starrocks_grants_to_scanner.h" | ||
#include "gen_cpp/FrontendService_types.h" | ||
#include "runtime/runtime_state.h" | ||
#include "runtime/string_value.h" | ||
#include "types/logical_type.h" | ||
|
||
namespace starrocks { | ||
|
||
SchemaScanner::ColumnDesc SysObjectDependencies::_s_columns[] = { | ||
{"OBJECT_ID", TYPE_BIGINT, sizeof(int64_t), false}, | ||
{"OBJECT_NAME", TYPE_VARCHAR, sizeof(StringValue), false}, | ||
{"OBJECT_DATABASE", TYPE_VARCHAR, sizeof(StringValue), false}, | ||
{"OBJECT_CATALOG", TYPE_VARCHAR, sizeof(StringValue), false}, | ||
{"OBJECT_TYPE", TYPE_VARCHAR, sizeof(StringValue), false}, | ||
|
||
{"REF_OBJECT_ID", TYPE_BIGINT, sizeof(int64_t), false}, | ||
{"REF_OBJECT_NAME", TYPE_VARCHAR, sizeof(StringValue), false}, | ||
{"REF_OBJECT_DATABASE", TYPE_VARCHAR, sizeof(StringValue), false}, | ||
{"REF_OBJECT_CATALOG", TYPE_VARCHAR, sizeof(StringValue), false}, | ||
{"REF_OBJECT_TYPE", TYPE_VARCHAR, sizeof(StringValue), false}, | ||
}; | ||
|
||
SysObjectDependencies::SysObjectDependencies() | ||
: SchemaScanner(_s_columns, sizeof(_s_columns) / sizeof(SchemaScanner::ColumnDesc)) {} | ||
|
||
SysObjectDependencies::~SysObjectDependencies() = default; | ||
|
||
Status SysObjectDependencies::start(RuntimeState* state) { | ||
RETURN_IF(!_is_init, Status::InternalError("used before initialized.")); | ||
RETURN_IF(!_param->ip || !_param->port, Status::InternalError("IP or port not exists")); | ||
|
||
TAuthInfo auth = build_auth_info(); | ||
TObjectDependencyReq request; | ||
request.__set_auth_info(auth); | ||
|
||
return (SchemaHelper::list_object_dependencies(*(_param->ip), _param->port, request, &_result)); | ||
} | ||
|
||
Status SysObjectDependencies::_fill_chunk(ChunkPtr* chunk) { | ||
auto& slot_id_map = (*chunk)->get_slot_id_to_index_map(); | ||
const TObjectDependencyItem& info = _result.items[_index]; | ||
DatumArray datum_array{ | ||
(info.object_id), Slice(info.object_name), Slice(info.database), | ||
Slice(info.catalog), Slice(info.object_type), | ||
|
||
(info.ref_object_id), Slice(info.ref_object_name), Slice(info.ref_database), | ||
Slice(info.ref_catalog), Slice(info.ref_object_type), | ||
}; | ||
for (const auto& [slot_id, index] : slot_id_map) { | ||
Column* column = (*chunk)->get_column_by_slot_id(slot_id).get(); | ||
column->append_datum(datum_array[slot_id - 1]); | ||
} | ||
_index++; | ||
return {}; | ||
} | ||
|
||
Status SysObjectDependencies::get_next(ChunkPtr* chunk, bool* eos) { | ||
RETURN_IF(!_is_init, Status::InternalError("Used before initialized.")); | ||
RETURN_IF((nullptr == chunk || nullptr == eos), Status::InternalError("input pointer is nullptr.")); | ||
|
||
if (_index >= _result.items.size()) { | ||
*eos = true; | ||
return Status::OK(); | ||
} | ||
*eos = false; | ||
return _fill_chunk(chunk); | ||
} | ||
|
||
} // namespace starrocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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. | ||
|
||
#pragma once | ||
|
||
#include "exec/schema_scanner.h" | ||
#include "gen_cpp/FrontendService_types.h" | ||
|
||
namespace starrocks { | ||
|
||
class SysObjectDependencies : public SchemaScanner { | ||
public: | ||
SysObjectDependencies(); | ||
~SysObjectDependencies() override; | ||
Status start(RuntimeState* state) override; | ||
Status get_next(ChunkPtr* chunk, bool* eos) override; | ||
|
||
private: | ||
Status _fill_chunk(ChunkPtr* chunk); | ||
|
||
size_t _index = 0; | ||
TObjectDependencyRes _result; | ||
static SchemaScanner::ColumnDesc _s_columns[]; | ||
}; | ||
|
||
} // namespace starrocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
fe/fe-core/src/main/java/com/starrocks/catalog/system/sys/SysObjectDependencies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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.catalog.system.sys; | ||
|
||
import com.starrocks.catalog.BaseTableInfo; | ||
import com.starrocks.catalog.Database; | ||
import com.starrocks.catalog.InternalCatalog; | ||
import com.starrocks.catalog.MaterializedView; | ||
import com.starrocks.catalog.ScalarType; | ||
import com.starrocks.catalog.Table; | ||
import com.starrocks.catalog.system.SystemId; | ||
import com.starrocks.catalog.system.SystemTable; | ||
import com.starrocks.privilege.AccessDeniedException; | ||
import com.starrocks.server.GlobalStateMgr; | ||
import com.starrocks.sql.analyzer.Authorizer; | ||
import com.starrocks.sql.ast.UserIdentity; | ||
import com.starrocks.thrift.TAuthInfo; | ||
import com.starrocks.thrift.TObjectDependencyItem; | ||
import com.starrocks.thrift.TObjectDependencyReq; | ||
import com.starrocks.thrift.TObjectDependencyRes; | ||
import com.starrocks.thrift.TSchemaTableType; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
public class SysObjectDependencies { | ||
|
||
public static final String NAME = "object_dependencies"; | ||
|
||
public static SystemTable create() { | ||
return new SystemTable(SystemId.OBJECT_DEPENDENCIES, NAME, Table.TableType.SCHEMA, | ||
SystemTable.builder() | ||
.column("object_id", ScalarType.BIGINT) | ||
.column("object_name", ScalarType.createVarcharType(SystemTable.NAME_CHAR_LEN)) | ||
.column("object_database", ScalarType.createVarcharType(SystemTable.NAME_CHAR_LEN)) | ||
.column("object_catalog", ScalarType.createVarcharType(SystemTable.NAME_CHAR_LEN)) | ||
.column("object_type", ScalarType.createVarcharType(64)) | ||
|
||
.column("ref_object_id", ScalarType.BIGINT) | ||
.column("ref_object_name", ScalarType.createVarcharType(SystemTable.NAME_CHAR_LEN)) | ||
.column("ref_object_database", ScalarType.createVarcharType(SystemTable.NAME_CHAR_LEN)) | ||
.column("ref_object_catalog", ScalarType.createVarcharType(SystemTable.NAME_CHAR_LEN)) | ||
.column("ref_object_type", ScalarType.createVarcharType(64)) | ||
.build(), | ||
TSchemaTableType.STARROCKS_OBJECT_DEPENDENCIES); | ||
} | ||
|
||
public static TObjectDependencyRes listObjectDependencies(TObjectDependencyReq req) { | ||
TAuthInfo auth = req.getAuth_info(); | ||
TObjectDependencyRes response = new TObjectDependencyRes(); | ||
|
||
UserIdentity currentUser; | ||
if (auth.isSetCurrent_user_ident()) { | ||
currentUser = UserIdentity.fromThrift(auth.getCurrent_user_ident()); | ||
} else { | ||
currentUser = UserIdentity.createAnalyzedUserIdentWithIp(auth.getUser(), auth.getUser_ip()); | ||
} | ||
|
||
// list dependencies of mv | ||
Collection<Database> dbs = GlobalStateMgr.getCurrentState().getFullNameToDb().values(); | ||
for (Database db : CollectionUtils.emptyIfNull(dbs)) { | ||
String catalog = Optional.ofNullable(db.getCatalogName()) | ||
.orElse(InternalCatalog.DEFAULT_INTERNAL_CATALOG_NAME); | ||
for (Table table : db.getTables()) { | ||
// Only show tables with privilege | ||
try { | ||
Authorizer.checkAnyActionOnTableLikeObject(currentUser, null, db.getFullName(), table); | ||
} catch (AccessDeniedException e) { | ||
continue; | ||
} | ||
|
||
if (table.isMaterializedView()) { | ||
MaterializedView mv = (MaterializedView) table; | ||
for (BaseTableInfo refObj : CollectionUtils.emptyIfNull(mv.getBaseTableInfos())) { | ||
TObjectDependencyItem item = new TObjectDependencyItem(); | ||
item.setObject_id(mv.getId()); | ||
item.setObject_name(mv.getName()); | ||
item.setDatabase(db.getFullName()); | ||
item.setCatalog(catalog); | ||
item.setObject_type(mv.getType().toString()); | ||
|
||
item.setRef_object_id(refObj.getTableId()); | ||
item.setRef_object_name(refObj.getTableName()); | ||
item.setRef_database(refObj.getDbName()); | ||
item.setRef_catalog(refObj.getCatalogName()); | ||
item.setRef_object_type(Optional.ofNullable(refObj.getTable()) | ||
.map(x -> x.getType().toString()) | ||
.orElse("UNKNOWN")); | ||
|
||
response.addToItems(item); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return response; | ||
} | ||
|
||
} |
Oops, something went wrong.