Skip to content
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 @@ -714,7 +714,8 @@ public void registerTupleDescriptor(TupleDescriptor desc) {
public TupleDescriptor registerTableRef(TableRef ref) throws AnalysisException {
String uniqueAlias = ref.getUniqueAlias();
if (uniqueTableAliasSet.contains(uniqueAlias)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_NONUNIQ_TABLE, uniqueAlias);
ErrorReport.reportAnalysisException(ErrorCode.ERR_NONUNIQ_TABLE,
uniqueAlias.substring(uniqueAlias.lastIndexOf('.') + 1));
}
uniqueTableAliasSet.add(uniqueAlias);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.apache.doris.nereids.trees.plans.algebra.SetOperation;
import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier;
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
import org.apache.doris.nereids.trees.plans.logical.LogicalExcept;
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
import org.apache.doris.nereids.trees.plans.logical.LogicalGenerate;
Expand Down Expand Up @@ -104,6 +105,7 @@
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
Expand All @@ -115,6 +117,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -507,6 +510,8 @@ private LogicalJoin<Plan, Plan> bindJoin(MatchingContext<LogicalJoin<Plan, Plan>
LogicalJoin<Plan, Plan> join = ctx.root;
CascadesContext cascadesContext = ctx.cascadesContext;

checkConflictAlias(join);

SimpleExprAnalyzer analyzer = buildSimpleExprAnalyzer(
join, cascadesContext, join.children(), true, true);

Expand All @@ -531,6 +536,47 @@ private LogicalJoin<Plan, Plan> bindJoin(MatchingContext<LogicalJoin<Plan, Plan>
join.children(), null);
}

private void checkConflictAlias(Plan plan) {
Set<String> existsTableNames = Sets.newLinkedHashSet();
Consumer<String> checkAlias = tableAliasName -> {
if (!existsTableNames.add(tableAliasName)) {
String tableName = tableAliasName.substring(tableAliasName.lastIndexOf('.') + 1);
throw new AnalysisException("Not unique table/alias: '" + tableName + "'");
}
};

boolean stopCheckChildren = true;
plan.foreach(p -> {
if (p instanceof LogicalSubQueryAlias) {
String alias = ((LogicalSubQueryAlias<?>) p).getAlias();
String dbName = getDbName(p.children().get(0));
String result = dbName + "." + alias;
checkAlias.accept(result);
return stopCheckChildren;

} else if (p instanceof LogicalCatalogRelation) {
String table = ((LogicalCatalogRelation) p).qualifiedName();
checkAlias.accept(table);
return stopCheckChildren;
} else {
return !stopCheckChildren;
}
});
}

private String getDbName(Plan plan) {
if (plan instanceof LogicalCatalogRelation) {
return ((LogicalCatalogRelation) plan).qualifiedName().split("\\.")[0]
+ ((LogicalCatalogRelation) plan).qualifiedName().split("\\.")[1];
} else if (plan instanceof LogicalSubQueryAlias) {
return ((LogicalSubQueryAlias<?>) plan).getQualifier().get(0)
+ ((LogicalSubQueryAlias<?>) plan).getQualifier().get(1);

} else {
return "default-catalog" + "default-db";
}
}

private LogicalJoin<Plan, Plan> bindUsingJoin(MatchingContext<UsingJoin<Plan, Plan>> ctx) {
UsingJoin<Plan, Plan> using = ctx.root;
CascadesContext cascadesContext = ctx.cascadesContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.trees.plans.logical.LogicalSubQueryAlias;
import org.apache.doris.nereids.util.MemoTestUtils;
Expand All @@ -40,6 +41,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

class BindSlotReferenceTest {

@BeforeEach
Expand All @@ -58,11 +61,18 @@ public void testCannotFindSlot() {

@Test
public void testAmbiguousSlot() {
LogicalOlapScan scan1 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student);
LogicalOlapScan scan2 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student);
LogicalJoin<LogicalOlapScan, LogicalOlapScan> join = new LogicalJoin<>(
JoinType.CROSS_JOIN, scan1, scan2, null);
LogicalProject<LogicalJoin<LogicalOlapScan, LogicalOlapScan>> project = new LogicalProject<>(
String qualifiedName = "internal.db.student";
List<String> qualifier = ImmutableList.copyOf(qualifiedName.split("\\."));
LogicalOlapScan scan1 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student,
qualifier);
LogicalOlapScan scan2 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student,
qualifier);
LogicalSubQueryAlias<LogicalOlapScan> aliasedScan2 = new LogicalSubQueryAlias<>("scan2_alias", scan2);

LogicalJoin<LogicalPlan, LogicalPlan> join = new LogicalJoin<>(
JoinType.CROSS_JOIN, scan1, aliasedScan2, null);

LogicalProject<LogicalPlan> project = new LogicalProject<>(
ImmutableList.of(new UnboundSlot("id")), join);

AnalysisException exception = Assertions.assertThrows(AnalysisException.class,
Expand All @@ -73,14 +83,18 @@ public void testAmbiguousSlot() {
}

/*
select t1.id from student t1 join on student t2 on t1.di=t2.id group by id;
select t1.id from student t1 join student t2 on t1.id=t2.id group by id;
group_by_key bind on t1.id, not t2.id
*/
@Test
public void testGroupByOnJoin() {
LogicalOlapScan scan1 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student);
String qualifiedName = "internal.db.student";
List<String> qualifier = ImmutableList.copyOf(qualifiedName.split("\\."));
LogicalOlapScan scan1 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student,
qualifier);
LogicalSubQueryAlias<LogicalOlapScan> sub1 = new LogicalSubQueryAlias<>("t1", scan1);
LogicalOlapScan scan2 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student);
LogicalOlapScan scan2 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student,
qualifier);
LogicalSubQueryAlias<LogicalOlapScan> sub2 = new LogicalSubQueryAlias<>("t2", scan2);
LogicalJoin<LogicalSubQueryAlias<LogicalOlapScan>, LogicalSubQueryAlias<LogicalOlapScan>> join =
new LogicalJoin<>(JoinType.CROSS_JOIN, sub1, sub2, null);
Expand All @@ -100,14 +114,18 @@ public void testGroupByOnJoin() {
}

/*
select count(1) from student t1 join on student t2 on t1.di=t2.id group by id;
select count(1) from student t1 join student t2 on t1.di=t2.id group by id;
group by key is ambiguous
*/
@Test
public void testGroupByOnJoinAmbiguous() {
LogicalOlapScan scan1 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student);
String qualifiedName = "internal.db.student";
List<String> qualifier = ImmutableList.copyOf(qualifiedName.split("\\."));
LogicalOlapScan scan1 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student,
qualifier);
LogicalSubQueryAlias<LogicalOlapScan> sub1 = new LogicalSubQueryAlias<>("t1", scan1);
LogicalOlapScan scan2 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student);
LogicalOlapScan scan2 = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student,
qualifier);
LogicalSubQueryAlias<LogicalOlapScan> sub2 = new LogicalSubQueryAlias<>("t2", scan2);
LogicalJoin<LogicalSubQueryAlias<LogicalOlapScan>, LogicalSubQueryAlias<LogicalOlapScan>> join =
new LogicalJoin<>(JoinType.CROSS_JOIN, sub1, sub2, null);
Expand Down
29 changes: 29 additions & 0 deletions regression-test/data/nereids_syntax_p0/alias_conflict.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select_normal --
1

-- !catalog_normal --

-- !view_normal --

-- !select_no_conflict --
1 a 12 1 a 12

-- !select_no_conflict --
1 a 12 1 a 12

-- !select_diff_alias --
1 a 12 1 a 12

-- !select_nested_no_conflict --
1 a 12 1 a 12

-- !select_cross_db_no_conflict --
1 a 12 1 a 12

-- !child_query_no_conflict1 --
1 1 a 12

-- !child_query_no_conflict2 --
1 a 12 1 a 12

Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,5 @@ suite("limit_push_down") {
qt_limit_right_outer_join_full_outer_join_cross_join """explain shape plan SELECT t1.id FROM t1 RIGHT OUTER JOIN t2 ON t1.id = t2.id FULL OUTER JOIN t3 ON t1.id = t3.id CROSS JOIN t4 LIMIT 1;"""

// `limit 1, left outer join, right outer join, full outer join, cross join`:
qt_limit_left_outer_join_right_outer_join_full_outer_join_cross_join """explain shape plan SELECT * FROM t1 LEFT OUTER JOIN t2 ON t1.id = t2.id RIGHT OUTER JOIN t3 ON t1.id = t3.id FULL OUTER JOIN t4 ON t1.id = t4.id inner JOIN t4 on TRUE LIMIT 1;"""
qt_limit_left_outer_join_right_outer_join_full_outer_join_cross_join """explain shape plan SELECT * FROM t1 LEFT OUTER JOIN t2 ON t1.id = t2.id RIGHT OUTER JOIN t3 ON t1.id = t3.id FULL OUTER JOIN t4 ON t1.id = t4.id inner JOIN t4 as x on TRUE LIMIT 1;"""
}
Loading