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] fix wrong plan when duplicate alias is same as the column name #50566

Merged
merged 2 commits into from
Sep 2, 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 @@ -1478,16 +1478,13 @@ public Expr visitSelect(SelectRelation selectRelation, Void context) {
if (item.getAlias() == null) {
continue;
}
// Ignore c1 as c1
if (item.getExpr() instanceof SlotRef &&
alias.equalsIgnoreCase(((SlotRef) item.getExpr()).getColumnName())) {
continue;
}

// Alias is case-insensitive
Expr lastAssociatedExpr = aliases.putIfAbsent(alias.toLowerCase(), item.getExpr());
if (lastAssociatedExpr != null) {
// Duplicate alias is allowed, eg: select a.v1 as v, a.v2 as v from t0 a,
// But it should not be ambiguous, eg: select a.v1 as v, a.v2 as v from t0 a order by v
// But it should be ambiguous when the alias is used in the order by expr,
// eg: select a.v1 as v, a.v2 as v from t0 a order by v
aliasesMaybeAmbiguous.add(alias.toLowerCase());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,37 @@ public void testAmbiguous() {
"FROM test.t0");
}

@Test
public void testAliasSameAsColumnName() throws Exception {
// test duplicate alias is same as column name
String sql = "select v1 v1, v2 + 1 v1, abs(v1) from t0";
String plan = getFragmentPlan(sql);
assertContains(plan, "1:Project\n" +
" | <slot 1> : 1: v1\n" +
" | <slot 4> : 2: v2 + 1\n" +
" | <slot 5> : abs(1: v1)");

sql = "select v1 v1, v1 v1, abs(v1) from t0";
plan = getFragmentPlan(sql);
assertContains(plan, "1:Project\n" +
" | <slot 1> : 1: v1\n" +
" | <slot 4> : abs(1: v1)");

Exception exception = Assert.assertThrows(SemanticException.class, () -> {
// test duplicate alias is different from column name
String duplicateAlias = "select v1 v1, v2 v1, abs(v1) from t0 order by v1";
getFragmentPlan(duplicateAlias);
});
Assert.assertTrue(exception.getMessage(), exception.getMessage().contains("Column 'v1' is ambiguous."));

exception = Assert.assertThrows(SemanticException.class, () -> {
// test duplicate alias is different from column name
String duplicateAlias = "select v1 v4, v2 + 1 v4, abs(v4) from t0";
getFragmentPlan(duplicateAlias);
});
Assert.assertTrue(exception.getMessage(), exception.getMessage().contains("Column v4 is ambiguous"));
}

private void testSqlRewrite(String sql, String expected) {
testSqlRewrite(sql, expected, true);
}
Expand Down
Loading