From 51e970731dbc96dbdb3f55069ee273210f1007e9 Mon Sep 17 00:00:00 2001 From: Murphy Date: Mon, 2 Dec 2024 11:42:20 +0800 Subject: [PATCH] fix test Signed-off-by: Murphy --- .../com/starrocks/sql/optimizer/Utils.java | 4 ++- .../com/starrocks/sql/parser/StarRocks.g4 | 1 - .../scheduler/history/TaskRunHistoryTest.java | 2 -- .../sql/analyzer/AnalyzeStmtTest.java | 13 +++++++-- .../com/starrocks/sql/parser/ParserTest.java | 2 +- .../statistic/columns/ColumnUsageTest.java | 28 +++++++++++++++---- 6 files changed, 37 insertions(+), 13 deletions(-) diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/Utils.java b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/Utils.java index 1fcd333c853db5..c80314d4369429 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/Utils.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/Utils.java @@ -937,7 +937,9 @@ public static List> resolveColumnRefRecursive(ColumnRefOpera ScalarOperator impl = expr.getOp().getProjection().resolveColumnRef(ref); if (impl != null) { List subRefs = Utils.extractColumnRef(impl); - subRefs.remove(impl); + if (impl instanceof ColumnRefOperator) { + subRefs.remove(impl); + } List> subColumns = Lists.newArrayList(); for (ColumnRefOperator subRef : subRefs) { subColumns.addAll(ListUtils.emptyIfNull(resolveColumnRefRecursive(subRef, factory, expr))); diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/parser/StarRocks.g4 b/fe/fe-core/src/main/java/com/starrocks/sql/parser/StarRocks.g4 index adaba8bde0ff89..f5444108638b9c 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/parser/StarRocks.g4 +++ b/fe/fe-core/src/main/java/com/starrocks/sql/parser/StarRocks.g4 @@ -1271,7 +1271,6 @@ analyzeStatement analyzeColumnClause : '(' qualifiedName (',' qualifiedName)* ')' #regularColumns - | qualifiedName (',' qualifiedName)* #regularColumns | ALL COLUMNS #allColumns | PREDICATE COLUMNS #predicateColumns ; diff --git a/fe/fe-core/src/test/java/com/starrocks/scheduler/history/TaskRunHistoryTest.java b/fe/fe-core/src/test/java/com/starrocks/scheduler/history/TaskRunHistoryTest.java index c639717e1d8a50..b6431a2466d25c 100644 --- a/fe/fe-core/src/test/java/com/starrocks/scheduler/history/TaskRunHistoryTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/scheduler/history/TaskRunHistoryTest.java @@ -42,7 +42,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; public class TaskRunHistoryTest { @@ -179,7 +178,6 @@ public void testKeeper(@Mocked RepoExecutor repo) { } }; keeper.run(); - assertTrue(keeper.isTableExisted()); assertFalse(keeper.isTableCorrected()); new MockUp() { diff --git a/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/AnalyzeStmtTest.java b/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/AnalyzeStmtTest.java index e68b61fd758736..884752165a6c85 100644 --- a/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/AnalyzeStmtTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/AnalyzeStmtTest.java @@ -26,6 +26,7 @@ import com.starrocks.common.MetaNotFoundException; import com.starrocks.qe.ConnectContext; import com.starrocks.qe.DDLStmtExecutor; +import com.starrocks.scheduler.history.TableKeeper; import com.starrocks.server.CatalogMgr; import com.starrocks.server.GlobalStateMgr; import com.starrocks.sql.ast.AnalyzeHistogramDesc; @@ -54,7 +55,9 @@ import com.starrocks.statistic.NativeAnalyzeStatus; import com.starrocks.statistic.StatisticSQLBuilder; import com.starrocks.statistic.StatisticUtils; +import com.starrocks.statistic.StatisticsMetaManager; import com.starrocks.statistic.StatsConstants; +import com.starrocks.statistic.columns.PredicateColumnsStorage; import com.starrocks.utframe.StarRocksAssert; import com.starrocks.utframe.UtFrameUtils; import mockit.Mock; @@ -103,13 +106,14 @@ public static void beforeClass() throws Exception { " \"replication_num\" = \"1\"\n" + ");"; starRocksAssert.withTable(createStructTableSql); + } @Test public void testAllColumns() { String sql = "analyze table db.tbl"; AnalyzeStmt analyzeStmt = (AnalyzeStmt) analyzeSuccess(sql); - Assert.assertEquals(analyzeStmt.getColumnNames().size(), 0); + Assert.assertEquals(4, analyzeStmt.getColumnNames().size()); } @Test @@ -136,7 +140,7 @@ public void testSelectedColumns() { sql = "analyze table test.t0"; analyzeStmt = (AnalyzeStmt) analyzeSuccess(sql); - Assert.assertEquals(analyzeStmt.getColumnNames().size(), 0); + Assert.assertEquals(3, analyzeStmt.getColumnNames().size()); } @Test @@ -471,6 +475,11 @@ public void testTypeKeys() throws Exception { @Test public void testAnalyzePredicateColumns() { + StatisticsMetaManager statistic = new StatisticsMetaManager(); + statistic.createStatisticsTablesForTest(); + TableKeeper keeper = PredicateColumnsStorage.createKeeper(); + keeper.run(); + AnalyzeStmt stmt = (AnalyzeStmt) analyzeSuccess("analyze table db.tbl all columns"); Assert.assertTrue(stmt.isAllColumns()); stmt = (AnalyzeStmt) analyzeSuccess("analyze table db.tbl predicate columns"); diff --git a/fe/fe-core/src/test/java/com/starrocks/sql/parser/ParserTest.java b/fe/fe-core/src/test/java/com/starrocks/sql/parser/ParserTest.java index 78f6d94bc8c21e..d5c88342f37a41 100644 --- a/fe/fe-core/src/test/java/com/starrocks/sql/parser/ParserTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/sql/parser/ParserTest.java @@ -387,7 +387,7 @@ void testUnexpectedTokenSqls(String sql, String expecting) { SessionVariable sessionVariable = new SessionVariable(); try { SqlParser.parse(sql, sessionVariable).get(0); - fail("sql should fail."); + fail("sql should fail: " + sql); } catch (Exception e) { System.out.println(e.getMessage()); assertContains(e.getMessage(), expecting); diff --git a/fe/fe-core/src/test/java/com/starrocks/statistic/columns/ColumnUsageTest.java b/fe/fe-core/src/test/java/com/starrocks/statistic/columns/ColumnUsageTest.java index 95b35153a4373f..96f5e2d070f6a5 100644 --- a/fe/fe-core/src/test/java/com/starrocks/statistic/columns/ColumnUsageTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/statistic/columns/ColumnUsageTest.java @@ -15,20 +15,34 @@ package com.starrocks.statistic.columns; import com.google.common.base.Splitter; +import com.starrocks.common.FeConstants; +import com.starrocks.scheduler.history.TableKeeper; import com.starrocks.sql.analyzer.AnalyzeTestUtil; import com.starrocks.sql.ast.AnalyzeStmt; import com.starrocks.sql.plan.PlanTestBase; +import com.starrocks.statistic.StatisticsMetaManager; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import java.util.List; +import java.util.stream.Collectors; class ColumnUsageTest extends PlanTestBase { + @BeforeAll + public static void beforeClass() { + StatisticsMetaManager statistic = new StatisticsMetaManager(); + statistic.createStatisticsTablesForTest(); + TableKeeper keeper = PredicateColumnsStorage.createKeeper(); + keeper.run(); + FeConstants.runningUnitTest = true; + } + @BeforeEach public void before() { PredicateColumnsMgr.getInstance().reset(); @@ -39,23 +53,24 @@ public void testColumnUsage() throws Exception { // normal predicate starRocksAssert.query("select * from t0 where v1 > 1").explainQuery(); starRocksAssert.query("select * from information_schema.column_stats_usage where table_name = 't0'") - .explainContains("constant exprs", "'v1' | 'predicate'"); + .explainContains("constant exprs", "'v1' | 'normal,predicate'"); starRocksAssert.query("select * from test_all_type where lower(t1a) = '123' and t1e < 1.1").explainQuery(); starRocksAssert.query("select * from information_schema.column_stats_usage where table_name = 'test_all_type'") - .explainContains("constant exprs", "'t1a' | 'predicate'"); + .explainContains("constant exprs", "'t1a' | 'normal,predicate'"); // group by starRocksAssert.query("select v2, v3, count(*) from t0 group by v2, v3").explainQuery(); starRocksAssert.query("select * from information_schema.column_stats_usage where table_name = 't0'") - .explainContains("constant exprs", "'v1' | 'predicate'"); + .explainContains("constant exprs", "'v1' | 'normal,predicate'"); // join starRocksAssert.query("select * from t0 join t1 on t0.v2 = t1.v4").explainQuery(); starRocksAssert.query("select * from information_schema.column_stats_usage where table_name = 't0'") - .explainContains(" 'v3' | 'group_by'", "'v2' | 'predicate,join,group_by'", "'v1' | 'predicate'"); + .explainContains(" 'v3' | 'normal,group_by'", "'v2' | 'normal,predicate,join,group_by'", + "'v1' | 'normal,predicate'"); starRocksAssert.query("select * from information_schema.column_stats_usage where table_name = 't1'") - .explainContains("constant exprs", "'v4' | 'predicate,join'"); + .explainContains("constant exprs", "'v4' | 'normal,predicate,join'"); } @ParameterizedTest @@ -132,6 +147,7 @@ public void testAnalyzePredicateColumns(String query, String analyzeStmt, String AnalyzeStmt stmt = (AnalyzeStmt) AnalyzeTestUtil.analyzeSuccess(analyzeStmt); List expect = StringUtils.isNotEmpty(expectedColumns) ? Splitter.on(",").splitToList(expectedColumns) : List.of(); - Assertions.assertEquals(expect, stmt.getColumnNames()); + Assertions.assertEquals(expect.stream().sorted().collect(Collectors.toList()), + stmt.getColumnNames().stream().sorted().collect(Collectors.toList())); } } \ No newline at end of file