Skip to content

Commit

Permalink
ARROW-8204: [Rust] [DataFusion] Add support for aliased expressions i…
Browse files Browse the repository at this point in the history
…n SQL

Note that this follows on from #6703 so I'll rebase once that is merged.

Also note that this depends on sqlparser 0.2.5 which I just published.

Closes #6713 from andygrove/ARROW-8204 and squashes the following commits:

48a6a01 <Andy Grove> Add support for aliased expressions in SQL query planner

Authored-by: Andy Grove <andygrove73@gmail.com>
Signed-off-by: Paddy Horan <paddyhoran@hotmail.com>
  • Loading branch information
andygrove authored and paddyhoran committed Mar 25, 2020
1 parent 17b9980 commit 50c5daf
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rust/datafusion/src/sql/planner.rs
Expand Up @@ -26,6 +26,7 @@ use crate::logicalplan::{

use arrow::datatypes::*;

use crate::logicalplan::Expr::Alias;
use sqlparser::sqlast::*;

/// The SchemaProvider trait allows the query planner to obtain meta-data about tables and
Expand Down Expand Up @@ -271,6 +272,10 @@ impl<S: SchemaProvider> SqlToRel<S> {
Ok(Expr::Literal(ScalarValue::Utf8(s.clone())))
}

ASTNode::SQLAliasedExpr(ref expr, ref alias) => {
Ok(Alias(Arc::new(self.sql_to_rex(&expr, schema)?), alias.to_owned()))
}

ASTNode::SQLIdentifier(ref id) => {
match schema.fields().iter().position(|c| c.name().eq(id)) {
Some(index) => Ok(Expr::Column(index)),
Expand Down Expand Up @@ -594,6 +599,14 @@ mod tests {
quick_test(sql, expected);
}

#[test]
fn select_aliased_scalar_func() {
let sql = "SELECT sqrt(age) AS square_people FROM person";
let expected = "Projection: sqrt(CAST(#3 AS Float64)) AS square_people\
\n TableScan: person projection=None";
quick_test(sql, expected);
}

#[test]
fn select_order_by() {
let sql = "SELECT id FROM person ORDER BY id";
Expand Down

0 comments on commit 50c5daf

Please sign in to comment.