Apache DataFusion SQL Query Engine
ARROW-10584: [Rust] [DataFusion] Add SQL support for JOIN ON syntax
Andy Grovefdb9776cc408
diff --git a/rust/benchmarks/src/bin/tpch.rs b/rust/benchmarks/src/bin/tpch.rsindex 36c4e6e2e7..82edd9874c 100644--- a/rust/benchmarks/src/bin/tpch.rs+++ b/rust/benchmarks/src/bin/tpch.rs@@ -26,7 +26,7 @@ use arrow::util::pretty;use datafusion::datasource::parquet::ParquetTable;use datafusion::datasource::{CsvFile, MemTable, TableProvider};use datafusion::error::{DataFusionError, Result};-use datafusion::logical_plan::{and, or, when, LogicalPlan};+use datafusion::logical_plan::LogicalPlan;use datafusion::physical_plan::csv::CsvExec;use datafusion::prelude::*;@@ -169,81 +169,33 @@ fn create_logical_plan(ctx: &mut ExecutionContext, query: usize) -> Result<Logicl_linestatus",),- 12 => {- // We do not have sufficient SQL support for this query yet-- // "SELECT- // l_shipmode,- // sum(case- // when o_orderpriority = '1-URGENT'- // OR o_orderpriority = '2-HIGH'- // then 1- // else 0- // end) as high_line_count,- // sum(case- // when o_orderpriority <> '1-URGENT'- // AND o_orderpriority <> '2-HIGH'- // then 1- // else 0- // end) AS low_line_count- // FROM- // orders,- // lineitem- // WHERE- // o_orderkey = l_orderkey- // AND l_shipmode in ('MAIL', 'SHIP')- // AND l_commitdate < l_receiptdate- // AND l_shipdate < l_commitdate- // AND l_receiptdate >= date '1994-01-01'- // AND l_receiptdate < date '1994-01-01' + interval '1' year- // GROUP BY- // l_shipmode- // ORDER BY- // l_shipmode"-- Ok(ctx- .table("lineitem")?- .filter(- col("l_shipmode")- .eq(lit("MAIL"))- .or(col("l_shipmode").eq(lit("SHIP"))),- )?- .filter(col("l_commitdate").lt(col("l_receiptdate")))?- .filter(col("l_shipdate").lt(col("l_commitdate")))?- .filter(col("l_receiptdate").gt_eq(lit("1994-01-01")))?- // we do not support date functions yet, so faking the "+ interval '1' year" part- .filter(col("l_receiptdate").lt(lit("1995-01-01")))?- .join(- ctx.table("orders")?,- JoinType::Inner,- &["l_orderkey"],- &["o_orderkey"],- )?- .aggregate(- vec![col("l_shipmode")],- vec![- sum(when(- or(- col("o_orderpriority").eq(lit("1-URGENT")),- col("o_orderpriority").eq(lit("2-HIGH")),- ),- lit(1),- )- .otherwise(lit(0))?)- .alias("high_line_count"),- sum(when(- and(- col("o_orderpriority").not_eq(lit("1-URGENT")),- col("o_orderpriority").not_eq(lit("2-HIGH")),- ),- lit(1),- )- .otherwise(lit(0))?)- .alias("low_line_count"),- ],- )?- .to_logical_plan())- }+ 12 => ctx.create_logical_plan(+ "SELECT+ l_shipmode,+ sum(case+ when o_orderpriority = '1-URGENT'+ OR o_orderpriority = '2-HIGH'+ then 1+ else 0+ end) as high_line_count,+ sum(case+ when o_orderpriority <> '1-URGENT'+ AND o_orderpriority <> '2-HIGH'+ then 1+ else 0+ end) AS low_line_count+ FROM+ lineitem JOIN orders ON l_orderkey = o_orderkey+ WHERE (l_shipmode = 'MAIL' OR l_shipmode = 'SHIP')+ AND l_commitdate < l_receiptdate+ AND l_shipdate < l_commitdate+ AND l_receiptdate >= '1994-01-01'+ AND l_receiptdate < '1995-01-01'+ GROUP BY+ l_shipmode+ ORDER BY+ l_shipmode",+ ),_ => unimplemented!("unsupported query"),}diff --git a/rust/datafusion/src/sql/planner.rs b/rust/datafusion/src/sql/planner.rsindex f5d2f88114..c5007d2230 100644--- a/rust/datafusion/src/sql/planner.rs+++ b/rust/datafusion/src/sql/planner.rs@@ -38,12 +38,15 @@ use crate::{use arrow::datatypes::*;use super::parser::ExplainPlan;+use crate::prelude::JoinType;use sqlparser::ast::{- BinaryOperator, DataType as SQLDataType, Expr as SQLExpr, Query, Select, SelectItem,- SetExpr, TableFactor, TableWithJoins, UnaryOperator, Value,+ BinaryOperator, DataType as SQLDataType, Expr as SQLExpr, Join, JoinConstraint,+ JoinOperator, Query, Select, SelectItem, SetExpr, TableFactor, TableWithJoins,+ UnaryOperator, Value,};use sqlparser::ast::{ColumnDef as SQLColumnDef, ColumnOption};use sqlparser::ast::{OrderByExpr, Statement};+use sqlparser::parser::ParserError::ParserError;/// The SchemaProvider trait allows the query planner to obtain meta-data about tables and/// functions referenced in SQL statements@@ -207,27 +210,97 @@ impl<'a, S: SchemaProvider> SqlToRel<'a, S> {}}- fn from_join_to_plan(&self, from: &Vec<TableWithJoins>) -> Result<LogicalPlan> {- if from.len() == 0 {- return Ok(LogicalPlanBuilder::empty(true).build()?);+ fn plan_tables_with_joins(&self, from: &Vec<TableWithJoins>) -> Result<LogicalPlan> {+ match from.len() {+ 0 => Ok(LogicalPlanBuilder::empty(true).build()?),+ 1 => self.plan_table_with_joins(&from[0]),+ _ => {+ // https://issues.apache.org/jira/browse/ARROW-10729+ Err(DataFusionError::NotImplemented(+ "JOINS are only supported when using the explicit JOIN ON syntax \+ (https://issues.apache.org/jira/browse/ARROW-10729)"+ .to_string(),+ ))+ }}- if from.len() != 1 {- return Err(DataFusionError::NotImplemented(- "FROM with multiple tables is still not implemented".to_string(),- ));- };- let relation = &from[0].relation;+ }++ fn plan_table_with_joins(&self, t: &TableWithJoins) -> Result<LogicalPlan> {+ let left = self.create_relation(&t.relation)?;+ match t.joins.len() {+ 0 => Ok(left),+ n => {+ let mut left = self.parse_relation_join(&left, &t.joins[0])?;+ for i in 1..n {+ left = self.parse_relation_join(&left, &t.joins[i])?;+ }+ Ok(left)+ }+ }+ }++ fn parse_relation_join(+ &self,+ left: &LogicalPlan,+ join: &Join,+ ) -> Result<LogicalPlan> {+ let right = self.create_relation(&join.relation)?;+ match &join.join_operator {+ JoinOperator::Inner(constraint) => {+ match constraint {+ JoinConstraint::On(sql_expr) => {+ let mut keys: Vec<(String, String)> = vec![];+ let join_schema =+ create_join_schema(left.schema(), &right.schema())?;++ // parse ON expression+ let expr = self.sql_to_rex(sql_expr, &join_schema)?;++ // extract join keys+ extract_join_keys(&expr, &mut keys)?;+ let left_keys: Vec<&str> =+ keys.iter().map(|pair| pair.0.as_str()).collect();+ let right_keys: Vec<&str> =+ keys.iter().map(|pair| pair.1.as_str()).collect();++ // return the logical plan representing the join+ LogicalPlanBuilder::from(&left)+ .join(&right, JoinType::Inner, &left_keys, &right_keys)?+ .build()+ }+ JoinConstraint::Using(_) => {+ // https://issues.apache.org/jira/browse/ARROW-10728+ Err(DataFusionError::NotImplemented(+ "JOIN with USING is not supported (https://issues.apache.org/jira/browse/ARROW-10728)".to_string(),+ ))+ }+ JoinConstraint::Natural => {+ // https://issues.apache.org/jira/browse/ARROW-10727+ Err(DataFusionError::NotImplemented(+ "NATURAL JOIN is not supported (https://issues.apache.org/jira/browse/ARROW-10727)".to_string(),+ ))+ }+ }+ }+ other => Err(DataFusionError::NotImplemented(format!(+ "Unsupported JOIN operator {:?}",+ other+ ))),+ }+ }++ fn create_relation(&self, relation: &TableFactor) -> Result<LogicalPlan> {match relation {TableFactor::Table { name, .. } => {- let name = name.to_string();- match self.schema_provider.get_table_meta(&name) {- Some(schema) => Ok(LogicalPlanBuilder::scan(+ let table_name = name.to_string();+ match self.schema_provider.get_table_meta(&table_name) {+ Some(schema) => LogicalPlanBuilder::scan("default",- &name,+ &table_name,schema.as_ref(),None,)?- .build()?),+ .build(),None => Err(DataFusionError::Plan(format!("no schema found for table {}",name@@ -235,9 +308,9 @@ impl<'a, S: SchemaProvider> SqlToRel<'a, S> {}}TableFactor::Derived { subquery, .. } => self.query_to_plan(subquery),- TableFactor::NestedJoin(_) => Err(DataFusionError::NotImplemented(- "Nested joins are not supported".to_string(),- )),+ TableFactor::NestedJoin(table_with_joins) => {+ self.plan_table_with_joins(table_with_joins)+ }}}@@ -249,7 +322,7 @@ impl<'a, S: SchemaProvider> SqlToRel<'a, S> {));}- let plan = self.from_join_to_plan(&select.from)?;+ let plan = self.plan_tables_with_joins(&select.from)?;// filter (also known as selection) firstlet plan = self.filter(&plan, &select.selection)?;@@ -628,6 +701,53 @@ impl<'a, S: SchemaProvider> SqlToRel<'a, S> {}}+fn create_join_schema(left: &SchemaRef, right: &SchemaRef) -> Result<Schema> {+ let mut fields = vec![];+ for field in left.fields() {+ fields.push(field.clone());+ }+ for field in right.fields() {+ fields.push(field.clone());+ }+ Ok(Schema::new(fields))+}++/// Parse equijoin ON condition which could be a single Eq or multiple conjunctive Eqs+///+/// Examples+///+/// foo = bar+/// foo = bar AND bar = baz AND ...+///+fn extract_join_keys(expr: &Expr, accum: &mut Vec<(String, String)>) -> Result<()> {+ match expr {+ Expr::BinaryExpr { left, op, right } => match op {+ Operator::Eq => match (left.as_ref(), right.as_ref()) {+ (Expr::Column(l), Expr::Column(r)) => {+ accum.push((l.to_owned(), r.to_owned()));+ Ok(())+ }+ other => Err(DataFusionError::SQL(ParserError(format!(+ "Unsupported expression '{:?}' in JOIN condition",+ other+ )))),+ },+ Operator::And => {+ extract_join_keys(left, accum)?;+ extract_join_keys(right, accum)+ }+ other => Err(DataFusionError::SQL(ParserError(format!(+ "Unsupported expression '{:?}' in JOIN condition",+ other+ )))),+ },+ other => Err(DataFusionError::SQL(ParserError(format!(+ "Unsupported expression '{:?}' in JOIN condition",+ other+ )))),+ }+}+/// Determine if an expression is an aggregate expression or notfn is_aggregate_expr(e: &Expr) -> bool {match e {@@ -969,9 +1089,38 @@ mod tests {quick_test(sql, expected);}+ #[test]+ fn equijoin_explicit_syntax() {+ let sql = "SELECT id, order_id \+ FROM person \+ JOIN orders \+ ON id = customer_id";+ let expected = "Projection: #id, #order_id\+ \n Join: id = customer_id\+ \n TableScan: person projection=None\+ \n TableScan: orders projection=None";+ quick_test(sql, expected);+ }++ #[test]+ fn equijoin_explicit_syntax_3_tables() {+ let sql = "SELECT id, order_id, l_description \+ FROM person \+ JOIN orders ON id = customer_id \+ JOIN lineitem ON o_item_id = l_item_id";+ let expected = "Projection: #id, #order_id, #l_description\+ \n Join: o_item_id = l_item_id\+ \n Join: id = customer_id\+ \n TableScan: person projection=None\+ \n TableScan: orders projection=None\+ \n TableScan: lineitem projection=None";+ quick_test(sql, expected);+ }+fn logical_plan(sql: &str) -> Result<LogicalPlan> {let planner = SqlToRel::new(&MockSchemaProvider {});- let ast = DFParser::parse_sql(&sql).unwrap();+ let result = DFParser::parse_sql(&sql);+ let ast = result.unwrap();planner.statement_to_plan(&ast[0])}@@ -999,6 +1148,22 @@ mod tests {false,),]))),+ "orders" => Some(Arc::new(Schema::new(vec![+ Field::new("order_id", DataType::UInt32, false),+ Field::new("customer_id", DataType::UInt32, false),+ Field::new("o_item_id", DataType::Utf8, false),+ Field::new("qty", DataType::Int32, false),+ Field::new("price", DataType::Float64, false),+ Field::new(+ "birth_date",+ DataType::Timestamp(TimeUnit::Nanosecond, None),+ false,+ ),+ ]))),+ "lineitem" => Some(Arc::new(Schema::new(vec![+ Field::new("l_item_id", DataType::UInt32, false),+ Field::new("l_description", DataType::Utf8, false),+ ]))),"aggregate_test_100" => Some(Arc::new(Schema::new(vec![Field::new("c1", DataType::Utf8, false),Field::new("c2", DataType::UInt32, false),diff --git a/rust/datafusion/tests/sql.rs b/rust/datafusion/tests/sql.rsindex 13459f9412..e236f692df 100644--- a/rust/datafusion/tests/sql.rs+++ b/rust/datafusion/tests/sql.rs@@ -1011,6 +1011,65 @@ fn create_case_context() -> Result<ExecutionContext> {Ok(ctx)}+#[tokio::test]+async fn equijoin() -> Result<()> {+ let mut ctx = create_join_context()?;+ let sql =+ "SELECT t1_id, t1_name, t2_name FROM t1 JOIN t2 ON t1_id = t2_id ORDER BY t1_id";+ let actual = execute(&mut ctx, sql).await;+ let expected = vec![+ vec!["11", "a", "z"],+ vec!["22", "b", "y"],+ vec!["44", "d", "x"],+ ];+ assert_eq!(expected, actual);+ Ok(())+}++fn create_join_context() -> Result<ExecutionContext> {+ let mut ctx = ExecutionContext::new();++ let t1_schema = Arc::new(Schema::new(vec![+ Field::new("t1_id", DataType::UInt32, true),+ Field::new("t1_name", DataType::Utf8, true),+ ]));+ let t1_data = RecordBatch::try_new(+ t1_schema.clone(),+ vec![+ Arc::new(UInt32Array::from(vec![11, 22, 33, 44])),+ Arc::new(StringArray::from(vec![+ Some("a"),+ Some("b"),+ Some("c"),+ Some("d"),+ ])),+ ],+ )?;+ let t1_table = MemTable::new(t1_schema, vec![vec![t1_data]])?;+ ctx.register_table("t1", Box::new(t1_table));++ let t2_schema = Arc::new(Schema::new(vec![+ Field::new("t2_id", DataType::UInt32, true),+ Field::new("t2_name", DataType::Utf8, true),+ ]));+ let t2_data = RecordBatch::try_new(+ t2_schema.clone(),+ vec![+ Arc::new(UInt32Array::from(vec![11, 22, 44, 55])),+ Arc::new(StringArray::from(vec![+ Some("z"),+ Some("y"),+ Some("x"),+ Some("w"),+ ])),+ ],+ )?;+ let t2_table = MemTable::new(t2_schema, vec![vec![t2_data]])?;+ ctx.register_table("t2", Box::new(t2_table));++ Ok(ctx)+}+#[tokio::test]async fn csv_explain() {let mut ctx = ExecutionContext::new();