Skip to content

Commit

Permalink
refactor: split relation of planner into one part.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwener committed Jan 6, 2023
1 parent 169b522 commit ede0ace
Show file tree
Hide file tree
Showing 10 changed files with 1,756 additions and 1,644 deletions.
43 changes: 42 additions & 1 deletion datafusion/sql/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod value;
use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
use crate::utils::normalize_ident;
use arrow_schema::DataType;
use datafusion_common::{DFSchema, DataFusionError, Result, ScalarValue};
use datafusion_common::{Column, DFSchema, DataFusionError, Result, ScalarValue};
use datafusion_expr::{
col, expr, lit, AggregateFunction, Between, BinaryExpr, BuiltinScalarFunction, Cast,
Expr, ExprSchemable, GetIndexedField, Like, Operator, TryCast,
Expand Down Expand Up @@ -66,6 +66,47 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}
}

/// Generate a relational expression from a SQL expression
pub(crate) fn sql_to_rex(
&self,
sql: SQLExpr,
schema: &DFSchema,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
let mut expr = self.sql_expr_to_logical_expr(sql, schema, planner_context)?;
expr = self.rewrite_partial_qualifier(expr, schema);
self.validate_schema_satisfies_exprs(schema, &[expr.clone()])?;
Ok(expr)
}

/// Rewrite aliases which are not-complete (e.g. ones that only include only table qualifier in a schema.table qualified relation)
fn rewrite_partial_qualifier(&self, expr: Expr, schema: &DFSchema) -> Expr {
match expr {
Expr::Column(col) => match &col.relation {
Some(q) => {
match schema
.fields()
.iter()
.find(|field| match field.qualifier() {
Some(field_q) => {
field.name() == &col.name
&& field_q.ends_with(&format!(".{q}"))
}
_ => false,
}) {
Some(df_field) => Expr::Column(Column {
relation: df_field.qualifier().cloned(),
name: df_field.name().clone(),
}),
None => Expr::Column(col),
}
}
None => Expr::Column(col),
},
_ => expr,
}
}

/// Internal implementation. Use
/// [`Self::sql_expr_to_logical_expr`] to plan exprs.
fn sql_expr_to_logical_expr_internal(
Expand Down
6 changes: 6 additions & 0 deletions datafusion/sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@
mod expr;
pub mod parser;
pub mod planner;
mod query;
mod relation;
mod select;
mod set_expr;
mod statement;
pub mod utils;
mod values;

pub use datafusion_common::{ResolvedTableReference, TableReference};
pub use sqlparser;
Loading

0 comments on commit ede0ace

Please sign in to comment.