Skip to content
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

[BugFix] don't fill catalog and database name to cte relation in the hive catalog (backport #49253) #49292

Merged
merged 1 commit into from
Aug 8, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.starrocks.qe.ConnectContext;
import com.starrocks.qe.SessionVariable;
import com.starrocks.sql.analyzer.AnalyzerUtils;
import com.starrocks.sql.ast.CTERelation;
import com.starrocks.sql.ast.QueryStatement;
import com.starrocks.sql.ast.TableRelation;
import com.starrocks.sql.common.ErrorType;
Expand All @@ -26,6 +27,7 @@
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.Objects.requireNonNull;

Expand All @@ -51,9 +53,11 @@ public QueryStatement getQueryStatement() throws StarRocksPlannerException {
String sqlDialect = sessionVariable.getSqlDialect();
QueryStatement queryStatement = doGetQueryStatement(sessionVariable);
sessionVariable.setSqlDialect(sqlDialect);

List<String> cteRelationNames = queryStatement.getQueryRelation().getCteRelations()
.stream().map(CTERelation::getName)
.collect(Collectors.toList());
List<TableRelation> tableRelations = AnalyzerUtils.collectTableRelations(queryStatement);
formatRelations(tableRelations);
formatRelations(tableRelations, cteRelationNames);
return queryStatement;
}

Expand Down Expand Up @@ -84,7 +88,7 @@ protected ParseNode rollback(SessionVariable sessionVariable) {
return null;
}

protected abstract void formatRelations(List<TableRelation> tableRelations);
protected abstract void formatRelations(List<TableRelation> tableRelations, List<String> cteRelationNames);

public String getInlineViewDef() {
return inlineViewDef;
Expand Down
12 changes: 11 additions & 1 deletion fe/fe-core/src/main/java/com/starrocks/catalog/HiveView.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.common.base.Strings;
import com.starrocks.analysis.ParseNode;
import com.starrocks.analysis.TableName;
import com.starrocks.qe.SessionVariable;
import com.starrocks.sql.ast.QueryStatement;
import com.starrocks.sql.ast.TableRelation;
Expand Down Expand Up @@ -59,8 +60,17 @@ public ParseNode rollback(SessionVariable sessionVariable) {
}
}

public void formatRelations(List<TableRelation> tableRelations) {
public void formatRelations(List<TableRelation> tableRelations, List<String> cteRelationNames) {
for (TableRelation tableRelation : tableRelations) {
TableName name = tableRelation.getName();

// do not fill catalog and database name to cte relation
if (Strings.isNullOrEmpty(name.getCatalog()) &&
Strings.isNullOrEmpty(name.getDb()) &&
cteRelationNames.contains(name.getTbl())) {
return;
}

tableRelation.getName().setCatalog(catalogName);
if (Strings.isNullOrEmpty(tableRelation.getName().getDb())) {
tableRelation.getName().setDb(dbName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public IcebergView(long id, String catalogName, String dbName, String name, List
}

@Override
protected void formatRelations(List<TableRelation> tableRelations) {
protected void formatRelations(List<TableRelation> tableRelations, List<String> cteRelationNames) {
for (TableRelation tableRelation : tableRelations) {
// iceberg view query statement with external catalog which created by starrocks must have catalog name
if (Strings.isNullOrEmpty(tableRelation.getName().getCatalog())) {
Expand Down
6 changes: 5 additions & 1 deletion test/sql/test_hive/R/test_hive_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ select * from hive_sql_test_${uuid0}.hive_oss_db.string_par_with_null_orc a inne
2 Bob bj 2
2 Bob bj 2
-- !result
select * from hive_sql_test_${uuid0}.test_oss.test_hive_view;
-- result:
1
-- !result
drop catalog hive_sql_test_${uuid0}
-- result:
-- !result
-- !result
6 changes: 5 additions & 1 deletion test/sql/test_hive/T/test_hive_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ select * from hive_sql_test_${uuid0}.hive_oss_db.string_par_with_null_parquet a
-- partition value is null and with runtime filter for orc
select * from hive_sql_test_${uuid0}.hive_oss_db.string_par_with_null_orc a inner join[shuffle] (select b.id as id from hive_sql_test_${uuid0}.hive_oss_db.string_par_with_null_orc b inner join[shuffle] hive_sql_test_${uuid0}.hive_oss_db.string_par_with_null_orc c on b.city=c.city) d on a.id = d.id order by 1, 2, 3, 4;

drop catalog hive_sql_test_${uuid0}
-- test hive view with cte
-- view definition: CREATE VIEW `test_hive_view` AS with test_cte as (select `base_hive_table`.`k1` from `test_oss`.`base_hive_table`) select `test_cte`.`k1` from test_cte
select * from hive_sql_test_${uuid0}.test_oss.test_hive_view;

drop catalog hive_sql_test_${uuid0}
Loading