Skip to content
Closed
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
79 changes: 55 additions & 24 deletions datafusion/src/execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ mod tests {

let expected = vec![
"+--------------+---------------+---------------+-------------+",
"| COUNT(nanos) | COUNT(micros) | COUNT(millis) | COUNT(secs) |",
"| count(nanos) | count(micros) | count(millis) | count(secs) |",
"+--------------+---------------+---------------+-------------+",
"| 3 | 3 | 3 | 3 |",
"+--------------+---------------+---------------+-------------+",
Expand All @@ -1477,7 +1477,7 @@ mod tests {

let expected = vec![
"+----------------------------+----------------------------+-------------------------+---------------------+",
"| MIN(nanos) | MIN(micros) | MIN(millis) | MIN(secs) |",
"| min(nanos) | min(micros) | min(millis) | min(secs) |",
"+----------------------------+----------------------------+-------------------------+---------------------+",
"| 2011-12-13 11:13:10.123450 | 2011-12-13 11:13:10.123450 | 2011-12-13 11:13:10.123 | 2011-12-13 11:13:10 |",
"+----------------------------+----------------------------+-------------------------+---------------------+",
Expand All @@ -1503,7 +1503,7 @@ mod tests {

let expected = vec![
"+-------------------------+-------------------------+-------------------------+---------------------+",
"| MAX(nanos) | MAX(micros) | MAX(millis) | MAX(secs) |",
"| max(nanos) | max(micros) | max(millis) | max(secs) |",
"+-------------------------+-------------------------+-------------------------+---------------------+",
"| 2021-01-01 05:11:10.432 | 2021-01-01 05:11:10.432 | 2021-01-01 05:11:10.432 | 2021-01-01 05:11:10 |",
"+-------------------------+-------------------------+-------------------------+---------------------+",
Expand Down Expand Up @@ -1690,7 +1690,7 @@ mod tests {

let expected = vec![
"+-----+------------+",
"| str | COUNT(val) |",
"| str | count(val) |",
"+-----+------------+",
"| A | 4 |",
"| B | 1 |",
Expand Down Expand Up @@ -1741,7 +1741,7 @@ mod tests {

let expected = vec![
"+------+------------+",
"| dict | COUNT(val) |",
"| dict | count(val) |",
"+------+------------+",
"| A | 4 |",
"| B | 1 |",
Expand All @@ -1758,7 +1758,7 @@ mod tests {

let expected = vec![
"+-----+-------------+",
"| val | COUNT(dict) |",
"| val | count(dict) |",
"+-----+-------------+",
"| 1 | 3 |",
"| 2 | 2 |",
Expand All @@ -1777,7 +1777,7 @@ mod tests {

let expected = vec![
"+-----+----------------------+",
"| val | COUNT(DISTINCT dict) |",
"| val | count(DISTINCT dict) |",
"+-----+----------------------+",
"| 1 | 2 |",
"| 2 | 2 |",
Expand Down Expand Up @@ -2044,6 +2044,14 @@ mod tests {

assert_batches_sorted_eq!(expected, &results);

let expected = vec![
"+---------+",
"| SQRT(i) |",
"+---------+",
"| 1 |",
"+---------+",
];

let results = plan_and_collect(&mut ctx, "SELECT SQRT(i) FROM t")
.await
.unwrap();
Expand All @@ -2058,6 +2066,14 @@ mod tests {
"Error during planning: Invalid function 'SQRT'"
);

let expected = vec![
"+-----------+",
"| \"sqrt\"(i) |",
"+-----------+",
"| 1 |",
"+-----------+",
];

let results = plan_and_collect(&mut ctx, "SELECT \"sqrt\"(i) FROM t")
.await
.unwrap();
Expand Down Expand Up @@ -2093,11 +2109,11 @@ mod tests {
let result = plan_and_collect(&mut ctx, "SELECT \"MY_FUNC\"(i) FROM t").await?;

let expected = vec![
"+------------+",
"| MY_FUNC(i) |",
"+------------+",
"| 1 |",
"+------------+",
"+--------------+",
"| \"MY_FUNC\"(i) |",
"+--------------+",
"| 1 |",
"+--------------+",
];
assert_batches_eq!(expected, &result);

Expand All @@ -2112,7 +2128,7 @@ mod tests {

let expected = vec![
"+--------+",
"| MAX(i) |",
"| max(i) |",
"+--------+",
"| 1 |",
"+--------+",
Expand All @@ -2124,6 +2140,14 @@ mod tests {

assert_batches_sorted_eq!(expected, &results);

let expected = vec![
"+--------+",
"| MAX(i) |",
"+--------+",
"| 1 |",
"+--------+",
];

let results = plan_and_collect(&mut ctx, "SELECT MAX(i) FROM t")
.await
.unwrap();
Expand All @@ -2138,6 +2162,13 @@ mod tests {
"Error during planning: Invalid function 'MAX'"
);

let expected = vec![
"+----------+",
"| \"max\"(i) |",
"+----------+",
"| 1 |",
"+----------+",
];
let results = plan_and_collect(&mut ctx, "SELECT \"max\"(i) FROM t")
.await
.unwrap();
Expand Down Expand Up @@ -2174,11 +2205,11 @@ mod tests {
let result = plan_and_collect(&mut ctx, "SELECT \"MY_AVG\"(i) FROM t").await?;

let expected = vec![
"+-----------+",
"| MY_AVG(i) |",
"+-----------+",
"| 1 |",
"+-----------+",
"+-------------+",
"| \"MY_AVG\"(i) |",
"+-------------+",
"| 1 |",
"+-------------+",
];
assert_batches_eq!(expected, &result);

Expand Down Expand Up @@ -2274,11 +2305,11 @@ mod tests {

assert_eq!(results.len(), 1);
let expected = vec![
"+---------+---------+-----------------+",
"| SUM(c1) | SUM(c2) | COUNT(UInt8(1)) |",
"+---------+---------+-----------------+",
"| 10 | 110 | 20 |",
"+---------+---------+-----------------+",
"+---------+---------+----------+",
"| SUM(c1) | SUM(c2) | COUNT(*) |",
"+---------+---------+----------+",
"| 10 | 110 | 20 |",
"+---------+---------+----------+",
];
assert_batches_eq!(expected, &results);

Expand Down Expand Up @@ -2503,7 +2534,7 @@ mod tests {

let expected = vec![
"+-----------+",
"| my_avg(a) |",
"| MY_AVG(a) |",
"+-----------+",
"| 3 |",
"+-----------+",
Expand Down
5 changes: 4 additions & 1 deletion datafusion/src/execution/dataframe_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,10 @@ mod tests {

let f = df.registry();

let df = df.select(vec![f.udf("my_fn")?.call(vec![col("c12")])])?;
let df = df.select(vec![f
.udf("my_fn")?
.call(vec![col("c12")])
.alias("my_fn(c12)")])?;
let plan = df.to_logical_plan();

// build query using SQL
Expand Down
7 changes: 6 additions & 1 deletion datafusion/src/logical_plan/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,12 @@ fn fmt_function(
impl fmt::Debug for Expr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expr::Alias(expr, alias) => write!(f, "{:?} AS {}", expr, alias),
Expr::Alias(expr, alias) => match expr.as_ref() {
Expr::Column(name) if alias == name => {
write!(f, "{:?}", expr)
}
_ => write!(f, "{:?} AS {}", expr, alias),
},
Expr::Column(name) => write!(f, "#{}", name),
Expr::ScalarVariable(var_names) => write!(f, "{}", var_names.join(".")),
Expr::Literal(v) => write!(f, "{:?}", v),
Expand Down
36 changes: 20 additions & 16 deletions datafusion/src/sql/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,10 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
/// Generate a relational expression from a select SQL expression
fn sql_select_to_rex(&self, sql: &SelectItem, schema: &DFSchema) -> Result<Expr> {
match sql {
SelectItem::UnnamedExpr(expr) => self.sql_to_rex(expr, schema),
SelectItem::UnnamedExpr(sql_expr) => {
let expr = self.sql_to_rex(sql_expr, schema)?;
Ok(expr.alias(&format!("{}", sql_expr)))
}
SelectItem::ExprWithAlias { expr, alias } => Ok(Alias(
Box::new(self.sql_to_rex(&expr, schema)?),
alias.value.clone(),
Expand Down Expand Up @@ -1517,7 +1520,7 @@ mod tests {
fn select_no_relation() {
quick_test(
"SELECT 1",
"Projection: Int64(1)\
"Projection: Int64(1) AS 1\
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an improvement to me.

\n EmptyRelation",
);
}
Expand Down Expand Up @@ -1568,7 +1571,7 @@ mod tests {
fn select_scalar_func_with_literal_no_relation() {
quick_test(
"SELECT sqrt(9)",
"Projection: sqrt(Int64(9))\
"Projection: sqrt(Int64(9)) AS sqrt(9)\
\n EmptyRelation",
);
}
Expand Down Expand Up @@ -2019,15 +2022,16 @@ mod tests {
#[test]
fn select_binary_expr() {
let sql = "SELECT age + salary from person";
let expected = "Projection: #age Plus #salary\
let expected = "Projection: #age Plus #salary AS age + salary\
\n TableScan: person projection=None";
quick_test(sql, expected);
}

#[test]
fn select_binary_expr_nested() {
let sql = "SELECT (age + salary)/2 from person";
let expected = "Projection: #age Plus #salary Divide Int64(2)\
let expected =
"Projection: #age Plus #salary Divide Int64(2) AS (age + salary) / 2\
\n TableScan: person projection=None";
quick_test(sql, expected);
}
Expand Down Expand Up @@ -2265,13 +2269,13 @@ mod tests {
) {
quick_test(
"SELECT age + 1, MIN(first_name) FROM person GROUP BY age + 1",
"Projection: #age Plus Int64(1), #MIN(first_name)\
"Projection: #age Plus Int64(1) AS age + 1, #MIN(first_name)\
\n Aggregate: groupBy=[[#age Plus Int64(1)]], aggr=[[MIN(#first_name)]]\
\n TableScan: person projection=None",
);
quick_test(
"SELECT MIN(first_name), age + 1 FROM person GROUP BY age + 1",
"Projection: #MIN(first_name), #age Plus Int64(1)\
"Projection: #MIN(first_name), #age Plus Int64(1) AS age + 1\
\n Aggregate: groupBy=[[#age Plus Int64(1)]], aggr=[[MIN(#first_name)]]\
\n TableScan: person projection=None",
);
Expand All @@ -2282,7 +2286,7 @@ mod tests {
{
quick_test(
"SELECT ((age + 1) / 2) * (age + 1), MIN(first_name) FROM person GROUP BY age + 1",
"Projection: #age Plus Int64(1) Divide Int64(2) Multiply #age Plus Int64(1), #MIN(first_name)\
"Projection: #age Plus Int64(1) Divide Int64(2) Multiply #age Plus Int64(1) AS ((age + 1) / 2) * (age + 1), #MIN(first_name)\
\n Aggregate: groupBy=[[#age Plus Int64(1)]], aggr=[[MIN(#first_name)]]\
\n TableScan: person projection=None",
);
Expand Down Expand Up @@ -2316,7 +2320,7 @@ mod tests {
fn select_simple_aggregate_nested_in_binary_expr_with_groupby() {
quick_test(
"SELECT state, MIN(age) < 10 FROM person GROUP BY state",
"Projection: #state, #MIN(age) Lt Int64(10)\
"Projection: #state, #MIN(age) Lt Int64(10) AS MIN(age) < 10\
\n Aggregate: groupBy=[[#state]], aggr=[[MIN(#age)]]\
\n TableScan: person projection=None",
);
Expand All @@ -2326,7 +2330,7 @@ mod tests {
fn select_simple_aggregate_and_nested_groupby_column() {
quick_test(
"SELECT age + 1, MAX(first_name) FROM person GROUP BY age",
"Projection: #age Plus Int64(1), #MAX(first_name)\
"Projection: #age Plus Int64(1) AS age + 1, #MAX(first_name)\
\n Aggregate: groupBy=[[#age]], aggr=[[MAX(#first_name)]]\
\n TableScan: person projection=None",
);
Expand All @@ -2336,7 +2340,7 @@ mod tests {
fn select_aggregate_compounded_with_groupby_column() {
quick_test(
"SELECT age + MIN(salary) FROM person GROUP BY age",
"Projection: #age Plus #MIN(salary)\
"Projection: #age Plus #MIN(salary) AS age + MIN(salary)\
\n Aggregate: groupBy=[[#age]], aggr=[[MIN(#salary)]]\
\n TableScan: person projection=None",
);
Expand All @@ -2346,7 +2350,7 @@ mod tests {
fn select_aggregate_with_non_column_inner_expression_with_groupby() {
quick_test(
"SELECT state, MIN(age + 1) FROM person GROUP BY state",
"Projection: #state, #MIN(age Plus Int64(1))\
"Projection: #state, #MIN(age Plus Int64(1)) AS MIN(age + 1)\
\n Aggregate: groupBy=[[#state]], aggr=[[MIN(#age Plus Int64(1))]]\
\n TableScan: person projection=None",
);
Expand All @@ -2364,7 +2368,7 @@ mod tests {
#[test]
fn select_count_one() {
let sql = "SELECT COUNT(1) FROM person";
let expected = "Projection: #COUNT(UInt8(1))\
let expected = "Projection: #COUNT(UInt8(1)) AS COUNT(1)\
\n Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]\
\n TableScan: person projection=None";
quick_test(sql, expected);
Expand All @@ -2382,7 +2386,7 @@ mod tests {
#[test]
fn select_scalar_func() {
let sql = "SELECT sqrt(age) FROM person";
let expected = "Projection: sqrt(#age)\
let expected = "Projection: sqrt(#age) AS sqrt(age)\
\n TableScan: person projection=None";
quick_test(sql, expected);
}
Expand All @@ -2399,7 +2403,7 @@ mod tests {
fn select_where_nullif_division() {
let sql = "SELECT c3/(c4+c5) \
FROM aggregate_test_100 WHERE c3/nullif(c4+c5, 0) > 0.1";
let expected = "Projection: #c3 Divide #c4 Plus #c5\
let expected = "Projection: #c3 Divide #c4 Plus #c5 AS c3 / (c4 + c5)\
\n Filter: #c3 Divide nullif(#c4 Plus #c5, Int64(0)) Gt Float64(0.1)\
\n TableScan: aggregate_test_100 projection=None";
quick_test(sql, expected);
Expand Down Expand Up @@ -2481,7 +2485,7 @@ mod tests {
#[test]
fn select_group_by_count_star() {
let sql = "SELECT state, COUNT(*) FROM person GROUP BY state";
let expected = "Projection: #state, #COUNT(UInt8(1))\
let expected = "Projection: #state, #COUNT(UInt8(1)) AS COUNT(*)\
\n Aggregate: groupBy=[[#state]], aggr=[[COUNT(UInt8(1))]]\
\n TableScan: person projection=None";

Expand Down