Skip to content

Commit

Permalink
[BugFix] Fix an issue that create analyze job default behavior is inc…
Browse files Browse the repository at this point in the history
…onsistent with documentation. (#47068)

Why I'm doing:
Fix an issue that create analyze job default behavior is inconsistent with documentation.

What I'm doing:
Fixes #47067

Signed-off-by: edwinhzhang <[email protected]>
(cherry picked from commit fb99da1)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/sql/parser/AstBuilder.java
#	fe/fe-core/src/test/java/com/starrocks/analysis/CreateAnalyzeJobTest.java
#	fe/fe-core/src/test/java/com/starrocks/sql/analyzer/AnalyzeCreateAnalyzeJobTest.java
  • Loading branch information
zhangheihei authored and mergify[bot] committed Jun 20, 2024
1 parent 5dffd65 commit 1a75668
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PROPERTIES (property [,property])
- Collection type
- FULL: indicates full collection.
- SAMPLE: indicates sampled collection.
- If no collection type is specified, full collection is used by default.
- If no collection type is specified, sampled collection is used by default.

- `col_name`: columns from which to collect statistics. Separate multiple columns with commas (`,`). If this parameter is not specified, the entire table is collected.

Expand Down
2 changes: 1 addition & 1 deletion docs/en/using_starrocks/Cost_based_optimizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ Parameter description:
- Collection type
- FULL: indicates full collection.
- SAMPLE: indicates sampled collection.
- If no collection type is specified, full collection is used by default.
- If no collection type is specified, sample collection is used by default.

- `col_name`: columns from which to collect statistics. Separate multiple columns with commas (`,`). If this parameter is not specified, the entire table is collected.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CREATE ANALYZE [FULL|SAMPLE] TABLE tbl_name (col_name [,col_name]) PROPERTIES (p
- 采集类型
- FULL:全量采集。
- SAMPLE:抽样采集。
- 如果不指定采集类型,默认为全量采集
- 如果不指定采集类型,默认为抽样采集

- `col_name`: 要采集统计信息的列,多列使用逗号 (,)分隔。如果不指定,表示采集整张表的信息。

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/using_starrocks/Cost_based_optimizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ CREATE ANALYZE [FULL|SAMPLE] TABLE tbl_name (col_name [,col_name]) PROPERTIES (p
- 采集类型
- FULL:全量采集。
- SAMPLE:抽样采集。
- 如果不指定采集类型,默认为全量采集
- 如果不指定采集类型,默认为抽样采集

- `col_name`: 要采集统计信息的列,多列使用逗号 (,)分隔。如果不指定,表示采集整张表的信息。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,7 @@ public ParseNode visitCreateAnalyzeStatement(StarRocksParser.CreateAnalyzeStatem
return new CreateAnalyzeJobStmt(((Identifier) visit(context.db)).getValue(), context.FULL() == null,
properties);
} else if (context.TABLE() != null) {
<<<<<<< HEAD
QualifiedName qualifiedName = getQualifiedName(context.qualifiedName());
TableName tableName = qualifiedNameToTableName(qualifiedName);

Expand All @@ -1884,6 +1885,13 @@ public ParseNode visitCreateAnalyzeStatement(StarRocksParser.CreateAnalyzeStatem
}

return new CreateAnalyzeJobStmt(tableName, columnNames, context.SAMPLE() != null, properties);
=======
List<QualifiedName> qualifiedNames = context.qualifiedName().stream().map(this::getQualifiedName).
collect(toList());
TableName tableName = qualifiedNameToTableName(qualifiedNames.get(0));
List<Expr> columns = getAnalyzeColumns(qualifiedNames.subList(1, qualifiedNames.size()));
return new CreateAnalyzeJobStmt(tableName, columns, context.FULL() == null, properties, pos);
>>>>>>> fb99da1c2a ([BugFix] Fix an issue that create analyze job default behavior is inconsistent with documentation. (#47068))
} else {
return new CreateAnalyzeJobStmt(context.FULL() == null, properties);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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.analysis;

import com.starrocks.common.AnalysisException;
import com.starrocks.qe.ConnectContext;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.CreateAnalyzeJobStmt;
import com.starrocks.sql.ast.StatementBase;
import com.starrocks.sql.plan.ConnectorPlanTestBase;
import com.starrocks.utframe.StarRocksAssert;
import com.starrocks.utframe.UtFrameUtils;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class CreateAnalyzeJobTest {
public static ConnectContext connectContext;
public static StarRocksAssert starRocksAssert;

@BeforeClass
public static void beforeClass() throws Exception {
UtFrameUtils.createMinStarRocksCluster();
// create connect context
connectContext = UtFrameUtils.createDefaultCtx();
starRocksAssert = new StarRocksAssert(connectContext);
ConnectorPlanTestBase.mockHiveCatalog(connectContext);
}

@Test
public void createExternalAnalyzeJobTest() throws Exception {
String sql = "create analyze table hive0.partitioned_db.t1";
try {
StatementBase statementBase = UtFrameUtils.parseStmtWithNewParser(sql, connectContext);
} catch (AnalysisException e) {
Assert.assertTrue(e.getMessage().contains("External table hive0.partitioned_db.t1 don't support SAMPLE analyze."));
}

connectContext.setCurrentCatalog("hive0");
String sql1 = "create analyze all";
Assert.assertThrows(AnalysisException.class, () -> UtFrameUtils.parseStmtWithNewParser(sql1, connectContext));

String sql2 = "create analyze sample table hive0.partitioned_db.t1";
Assert.assertThrows(AnalysisException.class, () -> UtFrameUtils.parseStmtWithNewParser(sql2, connectContext));

String sql3 = "create analyze database tpch";
Assert.assertThrows(AnalysisException.class, () -> UtFrameUtils.parseStmtWithNewParser(sql3, connectContext));

String sql4 = "create analyze full table hive0.partitioned_db.t1";
StatementBase statementBase = UtFrameUtils.parseStmtWithNewParser(sql4, connectContext);
Assert.assertTrue(statementBase instanceof CreateAnalyzeJobStmt);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.BeforeClass;
import org.junit.Test;

import static com.starrocks.sql.analyzer.AnalyzeTestUtil.analyzeFail;
import static com.starrocks.sql.analyzer.AnalyzeTestUtil.analyzeSuccess;
import static com.starrocks.sql.analyzer.AnalyzeTestUtil.getStarRocksAssert;

Expand Down Expand Up @@ -64,4 +65,21 @@ public void testColumn() throws Exception {
Assert.assertEquals(table.getId(), analyzeStmt.getTableId());
Assert.assertEquals(2, analyzeStmt.getColumnNames().size());
}
<<<<<<< HEAD
=======

@Test
public void testCreateAnalyzeJob() throws Exception {
String sql = "create analyze table db.tbl";
CreateAnalyzeJobStmt analyzeStmt = (CreateAnalyzeJobStmt) analyzeSuccess(sql);

DDLStmtExecutor.execute(analyzeStmt, starRocksAssert.getCtx());
Assert.assertEquals(1,
starRocksAssert.getCtx().getGlobalStateMgr().getAnalyzeMgr().getAllAnalyzeJobList().size());

sql = "create analyze table hive0.tpch.customer(C_NAME, C_PHONE)";
analyzeFail(sql, "External table hive0.tpch.customer don't support SAMPLE analyze.");

}
>>>>>>> fb99da1c2a ([BugFix] Fix an issue that create analyze job default behavior is inconsistent with documentation. (#47068))
}

0 comments on commit 1a75668

Please sign in to comment.