Skip to content

Commit bbe2324

Browse files
committed
Revert "Fix/support duplicate column names apache#6543 (apache#21126)"
This reverts commit aadae6b.
1 parent 2aab559 commit bbe2324

6 files changed

Lines changed: 35 additions & 265 deletions

File tree

datafusion/sql/src/select.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
2323
use crate::query::to_order_by_exprs_with_select;
2424
use crate::utils::{
2525
CheckColumnsMustReferenceAggregatePurpose, CheckColumnsSatisfyExprsPurpose,
26-
check_columns_satisfy_exprs, deduplicate_select_expr_names, extract_aliases,
27-
rebase_expr, resolve_aliases_to_exprs, resolve_columns, resolve_positions_to_exprs,
28-
rewrite_recursive_unnests_bottom_up,
26+
check_columns_satisfy_exprs, extract_aliases, rebase_expr, resolve_aliases_to_exprs,
27+
resolve_columns, resolve_positions_to_exprs, rewrite_recursive_unnests_bottom_up,
2928
};
3029

3130
use datafusion_common::error::DataFusionErrorBuilder;
@@ -110,10 +109,6 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
110109
planner_context,
111110
)?;
112111

113-
// Auto-suffix duplicate expression names (e.g. cov, cov → cov, cov:1)
114-
// before projection so that the unique-name constraint is satisfied.
115-
let select_exprs = deduplicate_select_expr_names(select_exprs);
116-
117112
// Having and group by clause may reference aliases defined in select projection
118113
let projected_plan = self.project(base_plan.clone(), select_exprs)?;
119114
let select_exprs = projected_plan.expressions();

datafusion/sql/src/utils.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use datafusion_expr::builder::get_struct_unnested_columns;
3333
use datafusion_expr::expr::{
3434
Alias, GroupingSet, Unnest, WindowFunction, WindowFunctionParams,
3535
};
36-
use datafusion_expr::select_expr::SelectExpr;
3736
use datafusion_expr::utils::{expr_as_column_expr, find_column_exprs};
3837
use datafusion_expr::{
3938
ColumnUnnestList, Expr, ExprSchemable, LogicalPlan, col, expr_vec_fmt,
@@ -634,38 +633,6 @@ fn push_projection_dedupl(projection: &mut Vec<Expr>, expr: Expr) {
634633
projection.push(expr);
635634
}
636635
}
637-
638-
/// Auto-suffix duplicate SELECT expression names with `:{count}`.
639-
///
640-
/// The first occurrence keeps its original name so that ORDER BY / HAVING
641-
/// references resolve correctly. Wildcards are left untouched because they
642-
/// are expanded later in `project_with_validation`.
643-
///
644-
/// Duplicates are detected by the schema name of each expression, which
645-
/// identifies logically identical expressions before column normalization.
646-
pub(crate) fn deduplicate_select_expr_names(exprs: Vec<SelectExpr>) -> Vec<SelectExpr> {
647-
let mut seen: HashMap<String, usize> = HashMap::new();
648-
exprs
649-
.into_iter()
650-
.map(|select_expr| match select_expr {
651-
SelectExpr::Expression(expr) => {
652-
let name = expr.schema_name().to_string();
653-
let count = seen.entry(name.clone()).or_insert(0);
654-
let result = if *count > 0 {
655-
let (_qualifier, field_name) = expr.qualified_name();
656-
SelectExpr::Expression(expr.alias(format!("{field_name}:{count}")))
657-
} else {
658-
SelectExpr::Expression(expr)
659-
};
660-
*count += 1;
661-
result
662-
}
663-
// Leave wildcards alone — they are expanded later
664-
other => other,
665-
})
666-
.collect()
667-
}
668-
669636
/// The context is we want to rewrite unnest() into InnerProjection->Unnest->OuterProjection
670637
/// Given an expression which contains unnest expr as one of its children,
671638
/// Try transform depends on unnest type

datafusion/sql/tests/sql_integration.rs

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -789,13 +789,11 @@ fn select_column_does_not_exist() {
789789
#[test]
790790
fn select_repeated_column() {
791791
let sql = "SELECT age, age FROM person";
792-
let plan = logical_plan(sql).unwrap();
792+
let err = logical_plan(sql).expect_err("query should have failed");
793+
793794
assert_snapshot!(
794-
plan,
795-
@r"
796-
Projection: person.age, person.age AS age:1
797-
TableScan: person
798-
"
795+
err.strip_backtrace(),
796+
@r#"Error during planning: Projections require unique expression names but the expression "person.age" at position 0 and "person.age" at position 1 have the same name. Consider aliasing ("AS") one of them."#
799797
);
800798
}
801799

@@ -1533,14 +1531,11 @@ fn select_simple_aggregate_column_does_not_exist() {
15331531
#[test]
15341532
fn select_simple_aggregate_repeated_aggregate() {
15351533
let sql = "SELECT MIN(age), MIN(age) FROM person";
1536-
let plan = logical_plan(sql).unwrap();
1534+
let err = logical_plan(sql).expect_err("query should have failed");
1535+
15371536
assert_snapshot!(
1538-
plan,
1539-
@r"
1540-
Projection: min(person.age), min(person.age) AS min(person.age):1
1541-
Aggregate: groupBy=[[]], aggr=[[min(person.age)]]
1542-
TableScan: person
1543-
"
1537+
err.strip_backtrace(),
1538+
@r#"Error during planning: Projections require unique expression names but the expression "min(person.age)" at position 0 and "min(person.age)" at position 1 have the same name. Consider aliasing ("AS") one of them."#
15441539
);
15451540
}
15461541

@@ -1589,14 +1584,11 @@ fn select_from_typed_string_values() {
15891584
#[test]
15901585
fn select_simple_aggregate_repeated_aggregate_with_repeated_aliases() {
15911586
let sql = "SELECT MIN(age) AS a, MIN(age) AS a FROM person";
1592-
let plan = logical_plan(sql).unwrap();
1587+
let err = logical_plan(sql).expect_err("query should have failed");
1588+
15931589
assert_snapshot!(
1594-
plan,
1595-
@r"
1596-
Projection: min(person.age) AS a, min(person.age) AS a AS a:1
1597-
Aggregate: groupBy=[[]], aggr=[[min(person.age)]]
1598-
TableScan: person
1599-
"
1590+
err.strip_backtrace(),
1591+
@r#"Error during planning: Projections require unique expression names but the expression "min(person.age) AS a" at position 0 and "min(person.age) AS a" at position 1 have the same name. Consider aliasing ("AS") one of them."#
16001592
);
16011593
}
16021594

@@ -1633,14 +1625,11 @@ fn select_simple_aggregate_with_groupby_with_aliases() {
16331625
#[test]
16341626
fn select_simple_aggregate_with_groupby_with_aliases_repeated() {
16351627
let sql = "SELECT state AS a, MIN(age) AS a FROM person GROUP BY state";
1636-
let plan = logical_plan(sql).unwrap();
1628+
let err = logical_plan(sql).expect_err("query should have failed");
1629+
16371630
assert_snapshot!(
1638-
plan,
1639-
@r"
1640-
Projection: person.state AS a, min(person.age) AS a AS a:1
1641-
Aggregate: groupBy=[[person.state]], aggr=[[min(person.age)]]
1642-
TableScan: person
1643-
"
1631+
err.strip_backtrace(),
1632+
@r#"Error during planning: Projections require unique expression names but the expression "person.state AS a" at position 0 and "min(person.age) AS a" at position 1 have the same name. Consider aliasing ("AS") one of them."#
16441633
);
16451634
}
16461635

@@ -1761,14 +1750,11 @@ fn select_simple_aggregate_with_groupby_can_use_alias() {
17611750
#[test]
17621751
fn select_simple_aggregate_with_groupby_aggregate_repeated() {
17631752
let sql = "SELECT state, MIN(age), MIN(age) FROM person GROUP BY state";
1764-
let plan = logical_plan(sql).unwrap();
1753+
let err = logical_plan(sql).expect_err("query should have failed");
1754+
17651755
assert_snapshot!(
1766-
plan,
1767-
@r"
1768-
Projection: person.state, min(person.age), min(person.age) AS min(person.age):1
1769-
Aggregate: groupBy=[[person.state]], aggr=[[min(person.age)]]
1770-
TableScan: person
1771-
"
1756+
err.strip_backtrace(),
1757+
@r#"Error during planning: Projections require unique expression names but the expression "min(person.age)" at position 1 and "min(person.age)" at position 2 have the same name. Consider aliasing ("AS") one of them."#
17721758
);
17731759
}
17741760

datafusion/sqllogictest/test_files/aggregate.slt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7941,16 +7941,21 @@ select count(), count() * count() from t;
79417941
----
79427942
2 4
79437943

7944-
# Duplicate aggregate expressions are now auto-suffixed
7945-
query II
7944+
# DataFusion error: Error during planning: Projections require unique expression names but the expression "count\(Int64\(1\)\)" at position 0 and "count\(Int64\(1\)\)" at position 1 have the same name\. Consider aliasing \("AS"\) one of them\.
7945+
query error
79467946
select count(1), count(1) from t;
7947-
----
7948-
2 2
79497947

7950-
query II
7948+
# DataFusion error: Error during planning: Projections require unique expression names but the expression "count\(Int64\(1\)\)" at position 0 and "count\(Int64\(1\)\)" at position 1 have the same name\. Consider aliasing \("AS"\) one of them\.
7949+
query error
7950+
explain select count(1), count(1) from t;
7951+
7952+
# DataFusion error: Error during planning: Projections require unique expression names but the expression "count\(Int64\(1\) AS \)" at position 0 and "count\(Int64\(1\) AS \)" at position 1 have the same name\. Consider aliasing \("AS"\) one of them\.
7953+
query error
79517954
select count(), count() from t;
7952-
----
7953-
2 2
7955+
7956+
# DataFusion error: Error during planning: Projections require unique expression names but the expression "count\(Int64\(1\) AS \)" at position 0 and "count\(Int64\(1\) AS \)" at position 1 have the same name\. Consider aliasing \("AS"\) one of them\.
7957+
query error
7958+
explain select count(), count() from t;
79547959

79557960
query II
79567961
select count(1), count(2) from t;

datafusion/sqllogictest/test_files/duplicate_column_alias.slt

Lines changed: 0 additions & 175 deletions
This file was deleted.

datafusion/sqllogictest/test_files/unnest.slt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -547,16 +547,8 @@ select unnest(column1) from (select * from (values([1,2,3]), ([4,5,6])) limit 1
547547
5
548548
6
549549

550-
query II
550+
query error DataFusion error: Error during planning: Projections require unique expression names but the expression "UNNEST\(unnest_table.column1\)" at position 0 and "UNNEST\(unnest_table.column1\)" at position 1 have the same name. Consider aliasing \("AS"\) one of them.
551551
select unnest(column1), unnest(column1) from unnest_table;
552-
----
553-
1 1
554-
2 2
555-
3 3
556-
4 4
557-
5 5
558-
6 6
559-
12 12
560552

561553
query II
562554
select unnest(column1), unnest(column1) u1 from unnest_table;

0 commit comments

Comments
 (0)