-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix duplicated schema name error from count wildcard #14824
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
Changes from all commits
0af4ab9
7f18e05
3ef7ddd
40385aa
a456792
3497965
d956307
e24cf29
e54d4b8
6ee5a35
2a2d0d3
98d42a1
9efb1ee
76aadc8
7edbf02
39e9d72
670f06e
6931c24
e95018f
fadf6e3
d24f151
610c9a3
cb6c975
90a7b0a
6550841
5f55161
2a8f4f4
70280b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2455,7 +2455,7 @@ async fn test_count_wildcard_on_sort() -> Result<()> { | |
let ctx = create_join_context()?; | ||
|
||
let sql_results = ctx | ||
.sql("select b,count(*) from t1 group by b order by count(*)") | ||
.sql("select b,count(1) from t1 group by b order by count(1)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. count_all() is count(1) now, so we need to change to count(1) to have a consistent name (count() is count(a) AS count() now). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to double check -- the reason this needs to change is that the test is comparing again a dataframe built with Though maybe we could change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is the err after changing it back to count(*)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. double alias in sort 😕
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found I could avoid the double alias by adding a check in diff --git a/datafusion/expr/src/expr.rs b/datafusion/expr/src/expr.rs
index f8baf9c94..2f3c2c575 100644
--- a/datafusion/expr/src/expr.rs
+++ b/datafusion/expr/src/expr.rs
@@ -1276,7 +1276,14 @@ impl Expr {
/// Return `self AS name` alias expression
pub fn alias(self, name: impl Into<String>) -> Expr {
- Expr::Alias(Alias::new(self, None::<&str>, name.into()))
+ let name = name.into();
+ // don't realias the same thing
+ if matches!(&self, Expr::Alias(Alias {name: existing_name, ..} ) if existing_name == &name)
+ {
+ self
+ } else {
+ Expr::Alias(Alias::new(self, None::<&str>, name))
+ }
}
/// Return `self AS name` alias expression with a specific qualifier
@@ -1285,7 +1292,15 @@ impl Expr {
relation: Option<impl Into<TableReference>>,
name: impl Into<String>,
) -> Expr {
- Expr::Alias(Alias::new(self, relation, name.into()))
+ let relation = relation.map(|r| r.into());
+ let name = name.into();
+ // don't realias the same thing
+ if matches!(&self, Expr::Alias(Alias {name: existing_name, relation: existing_relation, ..} ) if existing_name == &name && relation.as_ref()==existing_relation.as_ref() )
+ {
+ self
+ } else {
+ Expr::Alias(Alias::new(self, relation, name))
+ }
}
/// Remove an alias from an expression if one exists.
diff --git a/datafusion/functions-aggregate/src/count.rs b/datafusion/functions-aggregate/src/count.rs
index a3339f0fc..1faf1968b 100644
--- a/datafusion/functions-aggregate/src/count.rs
+++ b/datafusion/functions-aggregate/src/count.rs
@@ -81,7 +81,7 @@ pub fn count_distinct(expr: Expr) -> Expr {
/// Creates aggregation to count all rows, equivalent to `COUNT(*)`, `COUNT()`, `COUNT(1)`
pub fn count_all() -> Expr {
- count(Expr::Literal(COUNT_STAR_EXPANSION))
+ count(Expr::Literal(COUNT_STAR_EXPANSION)).alias("count(*)")
}
#[user_doc( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
.await? | ||
.explain(false, false)? | ||
.collect() | ||
|
@@ -2481,7 +2481,7 @@ async fn test_count_wildcard_on_sort() -> Result<()> { | |
async fn test_count_wildcard_on_where_in() -> Result<()> { | ||
let ctx = create_join_context()?; | ||
let sql_results = ctx | ||
.sql("SELECT a,b FROM t1 WHERE a in (SELECT count(*) FROM t2)") | ||
.sql("SELECT a,b FROM t1 WHERE a in (SELECT count(1) FROM t2)") | ||
.await? | ||
.explain(false, false)? | ||
.collect() | ||
|
@@ -2522,7 +2522,7 @@ async fn test_count_wildcard_on_where_in() -> Result<()> { | |
async fn test_count_wildcard_on_where_exist() -> Result<()> { | ||
let ctx = create_join_context()?; | ||
let sql_results = ctx | ||
.sql("SELECT a, b FROM t1 WHERE EXISTS (SELECT count(*) FROM t2)") | ||
.sql("SELECT a, b FROM t1 WHERE EXISTS (SELECT count(1) FROM t2)") | ||
.await? | ||
.explain(false, false)? | ||
.collect() | ||
|
@@ -2559,7 +2559,7 @@ async fn test_count_wildcard_on_window() -> Result<()> { | |
let ctx = create_join_context()?; | ||
|
||
let sql_results = ctx | ||
.sql("select count(*) OVER(ORDER BY a DESC RANGE BETWEEN 6 PRECEDING AND 2 FOLLOWING) from t1") | ||
.sql("select count(1) OVER(ORDER BY a DESC RANGE BETWEEN 6 PRECEDING AND 2 FOLLOWING) from t1") | ||
.await? | ||
.explain(false, false)? | ||
.collect() | ||
|
@@ -2598,7 +2598,7 @@ async fn test_count_wildcard_on_aggregate() -> Result<()> { | |
register_alltypes_tiny_pages_parquet(&ctx).await?; | ||
|
||
let sql_results = ctx | ||
.sql("select count(*) from t1") | ||
.sql("select count(1) from t1") | ||
.await? | ||
.explain(false, false)? | ||
.collect() | ||
|
@@ -2628,7 +2628,7 @@ async fn test_count_wildcard_on_where_scalar_subquery() -> Result<()> { | |
let ctx = create_join_context()?; | ||
|
||
let sql_results = ctx | ||
.sql("select a,b from t1 where (select count(*) from t2 where t1.a = t2.a)>0;") | ||
.sql("select a,b from t1 where (select count(1) from t2 where t1.a = t2.a)>0;") | ||
.await? | ||
.explain(false, false)? | ||
.collect() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,6 +286,7 @@ pub struct NamePreserver { | |
|
||
/// If the qualified name of an expression is remembered, it will be preserved | ||
/// when rewriting the expression | ||
#[derive(Debug)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
pub enum SavedName { | ||
/// Saved qualified name to be preserved | ||
Saved { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
count_all() is count(1)