8000 [Enhancement] Add more meta functions for better debug (backport #60027) by LiShuMing · Pull Request #60115 · StarRocks/starrocks · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Enhancement] Add more meta functions for better debug (backport #60027) #60115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: branch-3.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

package com.starrocks.sql.optimizer.function;

import com.google.common.collect.Maps;
import com.google.gson.JsonArray;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.SerializedName;
import com.starrocks.analysis.TableName;
import com.starrocks.catalog.BaseTableInfo;
import com.starrocks.catalog.Column;
import com.starrocks.catalog.Database;
import com.starrocks.catalog.InternalCatalog;
Expand All @@ -33,6 +35,7 @@
import com.starrocks.common.ErrorReport;
import com.starrocks.common.util.concurrent.lock.LockType;
import com.starrocks.common.util.concurrent.lock.Locker;
import com.starrocks.connector.ConnectorPartitionTraits;
import com.starrocks.connector.PartitionInfo;
import com.starrocks.connector.PartitionUtil;
import com.starrocks.connector.hive.Partition;
Expand All @@ -55,6 +58,7 @@
import com.starrocks.sql.optimizer.dump.QueryDumper;
import com.starrocks.sql.optimizer.operator.scalar.ConstantOperator;
import com.starrocks.sql.optimizer.rewrite.ConstantFunction;
import com.starrocks.sql.optimizer.rule.transformation.materialization.MvUtils;
import com.starrocks.thrift.TResultBatch;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
Expand All @@ -74,6 +78,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static com.starrocks.catalog.PrimitiveType.BOOLEAN;
import static com.starrocks.catalog.PrimitiveType.VARCHAR;
Expand Down Expand Up @@ -158,6 +163,130 @@ public static ConstantOperator inspectMvMeta(ConstantOperator mvName) {
}
}

static class MVRefreshInfoMeta {
// base table to refresh info
@SerializedName(value = "tableToUpdatePartitions")
private final Map<String, Set<String>> tableToUpdatePartitions;
// olap table info
@SerializedName("baseOlapTableVisibleVersionMap")
private final Map<String, Map<String, MaterializedView.BasePartitionInfo>> baseOlapTableVisibleVersionMap;
// external table info
@SerializedName("baseExternalTableInfoVisibleVersionMap")
private final Map<String, Map<String, MaterializedView.BasePartitionInfo>> baseExternalTableInfoVisibleVersionMap;

public MVRefreshInfoMeta(
Map<String, Set<String>> tableToUpdatePartitions,
Map<String, Map<String, MaterializedView.BasePartitionInfo>> baseTableVisibleVersionMap,
Map<String, Map<String, MaterializedView.BasePartitionInfo>> baseTableInfoVisibleVersionMap) {
this.tableToUpdatePartitions = tableToUpdatePartitions;
this.baseOlapTableVisibleVersionMap = baseTableVisibleVersionMap;
this.baseExternalTableInfoVisibleVersionMap = baseTableInfoVisibleVersionMap;
}
public String inspect() {
return GsonUtils.GSON.toJson(this);
}
}
@ConstantFunction(name = "inspect_mv_refresh_info", argTypes = {VARCHAR}, returnType = VARCHAR, isMetaFunction = true)
public static ConstantOperator inspectMVRefreshInfo(ConstantOperator mvName) {
if (mvName == null) {
ErrorReport.reportSemanticException(ErrorCode.ERR_INVALID_PARAMETER, mvName);
}
TableName tableName = TableName.fromString(mvName.getVarchar());
Pair<Database, Table> dbTable = inspectTable(tableName);
Table table = dbTable.getRight();
if (!table.isMaterializedView()) {
ErrorReport.reportSemanticException(ErrorCode.ERR_INVALID_PARAMETER,
tableName + " is not materialized view");
}
Locker locker = new Locker();
MaterializedView mv = (MaterializedView) table;
try {
locker.lockDatabase(dbTable.getLeft(), LockType.READ);
Map<String, Set<String>> tableToUpdatePartitions = Maps.newHashMap();
Map<Long, String> tableIdToTableNameMap = Maps.newHashMap();
for (BaseTableInfo baseTableInfo : mv.getBaseTableInfos()) {
Table baseTable = MvUtils.getTableChecked(baseTableInfo);
Set<String> toUpdatePartitions = null;
if (baseTable instanceof OlapTable) {
toUpdatePartitions = mv.getUpdatedPartitionNamesOfOlapTable((OlapTable) baseTable, false);
} else {
toUpdatePartitions = mv.getUpdatedPartitionNamesOfExternalTable(baseTable, false);
}
if (CollectionUtils.isNotEmpty(toUpdatePartitions)) {
tableToUpdatePartitions.put(baseTable.getName(), toUpdatePartitions);
}
tableIdToTableNameMap.put(baseTable.getId(), baseTable.getName());
}
Map<Long, Map<String, MaterializedView.BasePartitionInfo>> olapVisibleVersionMap =
mv.getRefreshScheme().getAsyncRefreshContext().getBaseTableVisibleVersionMap();
Map<String, Map<String, MaterializedView.BasePartitionInfo>> baseOlapTableVisibleVersionMap =
olapVisibleVersionMap.entrySet().stream()
.map(entry -> Pair.of(tableIdToTableNameMap.get(entry.getKey()), entry.getValue()))
.collect(Collectors.toMap(x -> x.getLeft(), x -> x.getRight()));

Map<BaseTableInfo, Map<String, MaterializedView.BasePartitionInfo>> externalVisibleVersionMap =
mv.getRefreshScheme().getAsyncRefreshContext().getBaseTableInfoVisibleVersionMap();
Map<String, Map<String, MaterializedView.BasePartitionInfo>> baseExternalTableVisibleVersionMap =
externalVisibleVersionMap.entrySet().stream()
.map(entry -> Pair.of(entry.getKey().getReadableString(), entry.getValue()))
.collect(Collectors.toMap(x -> x.getLeft(), x -> x.getRight()));
MVRefreshInfoMeta meta = new MVRefreshInfoMeta(tableToUpdatePartitions,
baseOlapTableVisibleVersionMap,
baseExternalTableVisibleVersionMap);
String json = meta.inspect();
return ConstantOperator.createVarchar(json);
} finally {
locker.unLockDatabase(dbTable.getLeft(), LockType.READ);
}
}

@ConstantFunction(name = "inspect_table_partition_info", argTypes = {VARCHAR}, returnType = VARCHAR, isMetaFunction = true)
public static ConstantOperator inspectTablePartitionInfo(ConstantOperator input) {
if (input == null) {
ErrorReport.reportSemanticException(ErrorCode.ERR_INVALID_PARAMETER, input);
}
TableName tableName = TableName.fromString(input.getVarchar());
Pair<Database, Table> dbTable = inspectTable(tableName);
Table table = dbTable.getRight();
if (table == null) {
ErrorReport.reportSemanticException(ErrorCode.ERR_INVALID_PARAMETER,
tableName + " is not a table");
}
Locker locker = new Locker();
try {
locker.lockDatabase(dbTable.getLeft(), LockType.READ);

JsonObject obj = new JsonObject();
if (table instanceof OlapTable) {
OlapTable olapTable = (OlapTable) table;
olapTable.getPartitions()
.stream()
.forEach(partition -> {
MaterializedView.BasePartitionInfo basePartitionInfo =
new MaterializedView.BasePartitionInfo(partition.getId(),
partition.getVisibleVersion(),
partition.getVisibleVersionTime());
obj.add(partition.getName(), GsonUtils.GSON.toJsonTree(basePartitionInfo));
});
} else {
Map<String, PartitionInfo> partitionNameWithPartitionInfo =
ConnectorPartitionTraits.build(table).getPartitionNameWithPartitionInfo();
partitionNameWithPartitionInfo.entrySet()
.stream()
.map(entry -> Pair.of(entry.getKey(),
MaterializedView.BasePartitionInfo.fromExternalTable(entry.getValue())))
.forEach(pair -> {
obj.add(pair.getLeft(),
pair.getRight() == null ? JsonNull.INSTANCE : GsonUtils.GSON.toJsonTree(pair.getRight()));
});
}
String json = obj.toString();
return ConstantOperator.createVarchar(json);
} finally {
locker.unLockDatabase(dbTable.getLeft(), LockType.READ);
}
}

/**
* Return related materialized-views of a table, in JSON array format
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,38 @@
import com.google.common.collect.Lists;
import com.starrocks.analysis.TableName;
import com.starrocks.catalog.Type;
import com.starrocks.common.Config;
import com.starrocks.common.ErrorReportException;
import com.starrocks.common.FeConstants;
import com.starrocks.leader.ReportHandler;
import com.starrocks.load.pipe.filelist.RepoExecutor;
import com.starrocks.memory.MemoryUsageTracker;
import com.starrocks.persist.gson.GsonUtils;
import com.starrocks.qe.ConnectContext;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.UserIdentity;
import com.starrocks.sql.optimizer.function.MetaFunctions;
import com.starrocks.sql.optimizer.operator.scalar.ConstantOperator;
import com.starrocks.sql.optimizer.rule.transformation.materialization.MVTestBase;
import com.starrocks.thrift.TResultBatch;
import com.starrocks.utframe.StarRocksAssert;
import com.starrocks.utframe.UtFrameUtils;
import mockit.Mock;
import mockit.MockUp;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import java.nio.ByteBuffer;
import java.util.List;

public class MetaFunctionsTest {
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MetaFunctionsTest extends MVTestBase {

static {
MemoryUsageTracker.registerMemoryTracker("Report", new ReportHandler());
}

private static ConnectContext connectContext;
private static StarRocksAssert starRocksAssert;

@BeforeClass
public static void beforeClass() throws Exception {
FeConstants.runningUnitTest = true;
Config.alter_scheduler_interval_millisecond = 100;
Config.dynamic_partition_enable = true;
Config.dynamic_partition_check_interval_seconds = 1;
Config.enable_strict_storage_medium_check = false;
UtFrameUtils.createMinStarRocksCluster();
UtFrameUtils.addMockBackend(10002);
UtFrameUtils.addMockBackend(10003);
// create connect context
connectContext = UtFrameUtils.createDefaultCtx();
starRocksAssert = new StarRocksAssert(connectContext);

MVTestBase.beforeClass();
starRocksAssert.withDatabase("test").useDatabase("test")
.withTable("CREATE TABLE test.tbl1\n" +
"(\n" +
Expand Down Expand Up @@ -146,13 +131,15 @@ public void testInspectMemoryDetail() {
public void testInspectTableAccessDeniedException() {
connectContext.setCurrentUserIdentity(testUser);
connectContext.setCurrentRoleIds(testUser);
connectContext.setThreadLocalInfo();
MetaFunctions.inspectTable(new TableName("test", "tbl1"));
}

@Test(expected = ErrorReportException.class)
public void testInspectExternalTableAccessDeniedException() {
connectContext.setCurrentUserIdentity(testUser);
connectContext.setCurrentRoleIds(testUser);
connectContext.setThreadLocalInfo();
MetaFunctions.inspectTable(new TableName("test", "mysql_external_table"));
}

Expand Down Expand Up @@ -221,6 +208,74 @@ public List<TResultBatch> executeDQL(String sql) {
};
Assert.assertNull(lookupString("t1", "v1", "c1"));
}
}

@Test
public void inspectMVRefreshInfoReturnsValidJsonForMaterializedView() throws Exception {
starRocksAssert.withRefreshedMaterializedView("create materialized view mv1 distributed by random " +
"as select k1, sum(v1) from test.tbl1 group by k1");
ConstantOperator result = MetaFunctions.inspectMVRefreshInfo(ConstantOperator.createVarchar("test.mv1"));
Assert.assertNotNull(result);
Assert.assertTrue(result.getVarchar().contains("tableToUpdatePartitions"));
starRocksAssert.dropMaterializedView("mv1");
}

@Test(expected = SemanticException.class)
public void inspectMVRefreshInfoThrowsExceptionForNonMaterializedView() throws Exception {
starRocksAssert.withTable("create table tbl2(k1 int, v1 int) properties('replication_num'='1')");
MetaFunctions.inspectMVRefreshInfo(ConstantOperator.createVarchar("test.tbl2"));
starRocksAssert.dropTable("tbl2");
}

@Test
public void inspectTablePartitionInfoReturnsValidJsonForOlapTable() throws Exception {
starRocksAssert.withTable("create table tbl3(k1 int, v1 int) partition by range(k1) " +
"(partition p1 values less than('10'), partition p2 values less than('20')) " +
"properties('replication_num'='1')");
ConstantOperator result = MetaFunctions.inspectTablePartitionInfo(ConstantOperator.createVarchar("test.tbl3"));
Assert.assertNotNull(result);
Assert.assertTrue(result.getVarchar().contains("p1"));
Assert.assertTrue(result.getVarchar().contains("p2"));
starRocksAssert.dropTable("tbl3");
}

@Test(expected = SemanticException.class)
public void inspectTablePartitionInfoThrowsExceptionForInvalidTable() {
MetaFunctions.inspectTablePartitionInfo(ConstantOperator.createVarchar("test.invalid_table"));
}

@Test
public void inspectMVRefreshInfoHandlesEmptyBaseTables() throws Exception {
starRocksAssert.withMaterializedView("create materialized view mv_empty distributed by random " +
" as select k1 from test.tbl1 group by k1");
ConstantOperator result = MetaFunctions.inspectMVRefreshInfo(ConstantOperator.createVarchar("test.mv_empty"));
Assert.assertNotNull(result);
Assert.assertTrue(result.getVarchar().contains("{}")); // Ensure empty base tables are handled
starRocksAssert.dropMaterializedView("mv_empty");
}

@Test(expected = SemanticException.class)
public void inspectMVRefreshInfoThrowsExceptionForNullInput() {
MetaFunctions.inspectMVRefreshInfo(null);
}

@Test
public void inspectTablePartitionInfoHandlesEmptyPartitions() throws Exception {
starRocksAssert.withTable("create table empty_partition_table(k1 int, v1 int) properties('replication_num'='1')");
ConstantOperator result = MetaFunctions.inspectTablePartitionInfo(
ConstantOperator.createVarchar("test.empty_partition_table"));
Assert.assertNotNull(result);
Assert.assertTrue(result.getVarchar().contains("empty_partition_table"));
starRocksAssert.dropTable("empty_partition_table");
}

@Test(expected = SemanticException.class)
public void inspectTablePartitionInfoThrowsExceptionForNullInput() {
MetaFunctions.inspectTablePartitionInfo(null);
}

@Test(expected = SemanticException.class)
public void inspectTablePartitionInfoThrowsExceptionForNonExistentTable() {
MetaFunctions.inspectTablePartitionInfo(ConstantOperator.createVarchar("test.non_existent_table"));
}
}
37 changes: 31 additions & 6 deletions test/sql/test_materialized_view/R/test_mv_meta_functions
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,44 @@ insert into user_tags values('2023-04-13', 3, 'e', 6);
create materialized view user_tags_mv1 distributed by hash(user_id) as select user_id, bitmap_union(to_bitmap(tag_id)) from user_tags group by user_id;
-- result:
-- !result
[UC]select inspect_mv_refresh_info('user_tags_mv1');
-- result:
{"tableToUpdatePartitions":{"user_tags":["p1"]},"baseTableVisibleVersionMap":{"user_tags":{}},"baseTableInfoVisibleVersionMap":{}}
-- !result
[UC]select inspect_table_partition_info('user_tags');
-- result:
{"p1":{"id":12210,"version":8,"lastRefreshTime":1750235292626,"lastFileModifiedTime":-1,"fileNumber":-1}}
-- !result
refresh materialized view user_tags_mv1 with sync mode;
select inspect_mv_plan('user_tags_mv1');
[UC]select inspect_mv_plan('user_tags_mv1');
-- result:
[REGEX]plan 0.*
plan 0:
LogicalAggregation {type=GLOBAL ,aggregations={5: bitmap_agg=bitmap_agg(4: tag_id)} ,groupKeys=[2: user_id] ,projection=null ,predicate=null}
-> LogicalOlapScanOperator {table=12211, selectedPartitionId=null, selectedIndexId=12212, outputColumns=[2: user_id, 4: tag_id], predicate=null, prunedPartitionPredicates=[], limit=-1}

-- !result
select inspect_mv_plan('user_tags_mv1', true);
[UC]select inspect_mv_plan('user_tags_mv1', true);
-- result:
[REGEX]plan 0.*
plan 0:
LogicalAggregation {type=GLOBAL ,aggregations={5: bitmap_agg=bitmap_agg(4: tag_id)} ,groupKeys=[2: user_id] ,projection=null ,predicate=null}
-> LogicalOlapScanOperator {table=12211, selectedPartitionId=null, selectedIndexId=12212, outputColumns=[2: user_id, 4: tag_id], predicate=null, prunedPartitionPredicates=[], limit=-1}

-- !result
select inspect_mv_plan('user_tags_mv1', false);
[UC]select inspect_mv_plan('user_tags_mv1', false);
-- result:
[REGEX]plan 0:.*
plan 0:
LogicalAggregation {type=GLOBAL ,aggregations={5: bitmap_agg=bitmap_agg(4: tag_id)} ,groupKeys=[2: user_id] ,projection=null ,predicate=null}
-> LogicalOlapScanOperator {table=12211, selectedPartitionId=null, selectedIndexId=12212, outputColumns=[2: user_id, 4: tag_id], predicate=null, prunedPartitionPredicates=[], limit=-1}

-- !result
insert into user_tags values('2023-04-13', 3, 'e', 6);
-- result:
-- !result
[UC]select inspect_mv_refresh_info('user_tags_mv1');
-- result:
{"tableToUpdatePartitions":{"user_tags":["p1"]},"baseTableVisibleVersionMap":{"user_tags":{"p1":{"id":12210,"version":8,"lastRefreshTime":1750235292626,"lastFileModifiedTime":-1,"fileNumber":-1}}},"baseTableInfoVisibleVersionMap":{}}
-- !result
[UC]select inspect_table_partition_info('user_tags');
-- result:
{"p1":{"id":12210,"version":9,"lastRefreshTime":1750235294261,"lastFileModifiedTime":-1,"fileNumber":-1}}
-- !result
12 changes: 9 additions & 3 deletions test/sql/test_materialized_view/T/test_mv_meta_functions
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ insert into user_tags values('2023-04-13', 2, 'e', 5);
insert into user_tags values('2023-04-13', 3, 'e', 6);

create materialized view user_tags_mv1 distributed by hash(user_id) as select user_id, bitmap_union(to_bitmap(tag_id)) from user_tags group by user_id;
[UC]select inspect_mv_refresh_info('user_tags_mv1');
[UC]select inspect_table_partition_info('user_tags');

refresh materialized view user_tags_mv1 with sync mode;

select inspect_mv_plan('user_tags_mv1');
[UC]select inspect_mv_plan('user_tags_mv1');
[UC]select inspect_mv_plan('user_tags_mv1', true);
[UC]select inspect_mv_plan('user_tags_mv1', false);

select inspect_mv_plan('user_tags_mv1', true);
insert into user_tags values('2023-04-13', 3, 'e', 6);

select inspect_mv_plan('user_tags_mv1', false);
[UC]select inspect_mv_refresh_info('user_tags_mv1');
[UC]select inspect_table_partition_info('user_tags');
Loading
0