Skip to content

Commit e30ae21

Browse files
committed
handle single wildcard as well
1 parent 9887464 commit e30ae21

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

datafusion/sql/src/set_expr.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ fn alias_set_expr(set_expr: &mut SetExpr, schema: &DFSchemaRef) {
203203
// This helps with set expression queries where the right side has duplicate expressions,
204204
// but the left side has unique column names, which control the output schema anyway.
205205
fn alias_select_items(items: &mut [SelectItem], schema: &DFSchemaRef) {
206+
// Figure out how many (qualified) wildcards we got. We only handle
207+
// the case of a single unqualified wildcard; for multiple or qualified
208+
// wildcards we can't reliably determine column counts, so bail out.
209+
let (wildcard_count, qualified_wildcard_count) =
210+
items.iter().fold((0, 0), |(wc, qwc), item| match item {
211+
SelectItem::Wildcard(_) => (wc + 1, qwc),
212+
SelectItem::QualifiedWildcard(_, _) => (wc, qwc + 1),
213+
_ => (wc, qwc),
214+
});
215+
if qualified_wildcard_count > 0 || wildcard_count > 1 {
216+
return;
217+
}
218+
219+
let wildcard_expansion = schema.fields().len().saturating_sub(items.len() - 1);
220+
206221
let mut col_idx = 0;
207222
for item in items.iter_mut() {
208223
match item {
@@ -222,8 +237,11 @@ fn alias_select_items(items: &mut [SelectItem], schema: &DFSchemaRef) {
222237
SelectItem::ExprWithAlias { .. } => {
223238
col_idx += 1;
224239
}
225-
SelectItem::Wildcard(_) | SelectItem::QualifiedWildcard(_, _) => {
226-
// Wildcards expand to multiple columns - skip position tracking
240+
SelectItem::Wildcard(_) => {
241+
col_idx += wildcard_expansion;
242+
}
243+
SelectItem::QualifiedWildcard(_, _) => {
244+
unreachable!("qualified wildcards are handled above")
227245
}
228246
}
229247
}

datafusion/sql/tests/sql_integration.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,25 @@ fn union_all_with_duplicate_expressions() {
26782678
);
26792679
}
26802680

2681+
#[test]
2682+
fn union_with_qualified_and_duplicate_expressions() {
2683+
let sql = "\
2684+
SELECT 0 a, id b, price c, 0 d FROM test_decimal \
2685+
UNION SELECT 1, *, 1 FROM test_decimal";
2686+
let plan = logical_plan(sql).unwrap();
2687+
assert_snapshot!(
2688+
plan,
2689+
@"
2690+
Distinct:
2691+
Union
2692+
Projection: Int64(0) AS a, test_decimal.id AS b, test_decimal.price AS c, Int64(0) AS d
2693+
TableScan: test_decimal
2694+
Projection: Int64(1) AS a, test_decimal.id, test_decimal.price, Int64(1) AS d
2695+
TableScan: test_decimal
2696+
"
2697+
);
2698+
}
2699+
26812700
#[test]
26822701
fn intersect_with_duplicate_expressions() {
26832702
let sql = "\

0 commit comments

Comments
 (0)