Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl RelationPlanner for MatchRecognizePlanner {
.iter()
.map(|m| {
let alias = ctx.normalize_ident(m.alias.clone());
let expr = ctx.sql_to_expr(m.expr.clone(), schema.as_ref())?;
let expr = ctx.sql_to_expr(m.expr.clone(), &schema)?;
Ok((alias, expr))
})
.collect::<Result<_>>()?;
Expand All @@ -384,7 +384,7 @@ impl RelationPlanner for MatchRecognizePlanner {
.iter()
.map(|s| {
let name = ctx.normalize_ident(s.symbol.clone());
let expr = ctx.sql_to_expr(s.definition.clone(), schema.as_ref())?;
let expr = ctx.sql_to_expr(s.definition.clone(), &schema)?;
Ok((name, expr))
})
.collect::<Result<_>>()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ fn plan_pivot(
// Parse aggregate functions
let aggregates: Vec<Expr> = aggregate_functions
.iter()
.map(|agg| ctx.sql_to_expr(agg.expr.clone(), schema.as_ref()))
.map(|agg| ctx.sql_to_expr(agg.expr.clone(), schema))
.collect::<Result<_>>()?;

// Get the pivot column (only single-column pivot supported)
Expand All @@ -373,7 +373,7 @@ fn plan_pivot(
"Only single-column PIVOT is supported"
));
}
let pivot_col = ctx.sql_to_expr(value_column[0].clone(), schema.as_ref())?;
let pivot_col = ctx.sql_to_expr(value_column[0].clone(), schema)?;
let pivot_col_name = extract_column_name(&pivot_col)?;

// Parse pivot values
Expand All @@ -385,7 +385,7 @@ fn plan_pivot(
.alias
.as_ref()
.map(|id| ctx.normalize_ident(id.clone()));
let expr = ctx.sql_to_expr(item.expr.clone(), schema.as_ref())?;
let expr = ctx.sql_to_expr(item.expr.clone(), schema)?;
Ok((alias, expr))
})
.collect::<Result<Vec<_>>>()?,
Expand Down Expand Up @@ -495,7 +495,7 @@ fn plan_unpivot(
.as_ref()
.map(|id| ctx.normalize_ident(id.clone()))
.unwrap_or_else(|| c.expr.to_string());
let expr = ctx.sql_to_expr(c.expr.clone(), schema.as_ref())?;
let expr = ctx.sql_to_expr(c.expr.clone(), schema)?;
let col_name = extract_column_name(&expr)?;
Ok((col_name.to_string(), label))
})
Expand Down
13 changes: 7 additions & 6 deletions datafusion/core/src/execution/session_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ use datafusion_common::config::{ConfigExtension, ConfigOptions, TableOptions};
use datafusion_common::display::{PlanType, StringifiedPlan, ToStringifiedPlan};
use datafusion_common::tree_node::TreeNode;
use datafusion_common::{
DFSchema, DataFusionError, ResolvedTableReference, TableReference, config_err,
exec_err, plan_datafusion_err,
DFSchema, DFSchemaRef, DataFusionError, ResolvedTableReference, TableReference,
config_err, exec_err, plan_datafusion_err,
};
use datafusion_execution::TaskContext;
use datafusion_execution::config::SessionConfig;
Expand Down Expand Up @@ -568,16 +568,17 @@ impl SessionState {
let dialect = self.config.options().sql_parser.dialect;

let sql_expr = self.sql_to_expr_with_alias(sql, &dialect)?;
let df_schema = Arc::new(df_schema.clone());

self.create_logical_expr_from_sql_expr(sql_expr, df_schema)
self.create_logical_expr_from_sql_expr(sql_expr, &df_schema)
}

/// Creates a datafusion style AST [`Expr`] from a SQL expression.
#[cfg(feature = "sql")]
pub fn create_logical_expr_from_sql_expr(
&self,
sql_expr: SQLExprWithAlias,
df_schema: &DFSchema,
df_schema: &DFSchemaRef,
) -> datafusion_common::Result<Expr> {
let provider = SessionContextProvider {
state: self,
Expand Down Expand Up @@ -2187,7 +2188,7 @@ mod tests {

let sql = "[1,2,3]";
let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]);
let df_schema = DFSchema::try_from(schema)?;
let df_schema = Arc::new(DFSchema::try_from(schema)?);
let dialect = state.config.options().sql_parser.dialect;
let sql_expr = state.sql_to_expr(sql, &dialect)?;

Expand Down Expand Up @@ -2216,7 +2217,7 @@ mod tests {
};

let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]);
let df_schema = DFSchema::try_from(schema).unwrap();
let df_schema = Arc::new(DFSchema::try_from(schema).unwrap());
let dialect = state.config.options().sql_parser.dialect;
let query = SqlToRel::new_with_options(&provider, state.get_parser_options());

Expand Down
6 changes: 3 additions & 3 deletions datafusion/expr/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
use arrow::datatypes::{DataType, Field, FieldRef, SchemaRef};
use datafusion_common::datatype::DataTypeExt;
use datafusion_common::{
DFSchema, Result, TableReference, config::ConfigOptions,
DFSchema, DFSchemaRef, Result, TableReference, config::ConfigOptions,
file_options::file_type::FileType, not_impl_err,
};
#[cfg(feature = "sql")]
Expand Down Expand Up @@ -408,14 +408,14 @@ pub trait RelationPlannerContext {

/// Converts a SQL expression into a logical expression using the current
/// planner context.
fn sql_to_expr(&mut self, expr: SQLExpr, schema: &DFSchema) -> Result<Expr>;
fn sql_to_expr(&mut self, expr: SQLExpr, schema: &DFSchemaRef) -> Result<Expr>;

/// Converts a SQL expression into a logical expression without DataFusion
/// rewrites.
fn sql_expr_to_logical_expr(
&mut self,
expr: SQLExpr,
schema: &DFSchema,
schema: &DFSchemaRef,
) -> Result<Expr>;

/// Normalizes an identifier according to session settings.
Expand Down
16 changes: 8 additions & 8 deletions datafusion/sql/src/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::planner::{ContextProvider, PlannerContext, SqlToRel};

use arrow::datatypes::DataType;
use datafusion_common::{
DFSchema, Dependency, Diagnostic, Result, Span, internal_datafusion_err,
DFSchema, DFSchemaRef, Dependency, Diagnostic, Result, Span, internal_datafusion_err,
internal_err, not_impl_err, plan_datafusion_err, plan_err,
};
use datafusion_expr::{
Expand Down Expand Up @@ -225,7 +225,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
pub(super) fn sql_function_to_expr(
&self,
function: SQLFunction,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
let function_args = FunctionArgs::try_new(function)?;
Expand Down Expand Up @@ -695,7 +695,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
&self,
expr: SQLExpr,
fn_name: &str,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
let fun = self
Expand Down Expand Up @@ -734,7 +734,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
fn sql_fn_arg_to_logical_expr(
&self,
sql: FunctionArg,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
let (expr, _) =
Expand All @@ -745,7 +745,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
fn sql_fn_arg_to_logical_expr_with_name(
&self,
sql: FunctionArg,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<(Expr, Option<ArgumentName>)> {
match sql {
Expand Down Expand Up @@ -840,7 +840,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
pub(super) fn function_args_to_expr(
&self,
args: Vec<FunctionArg>,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<Vec<Expr>> {
args.into_iter()
Expand All @@ -851,7 +851,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
pub(super) fn function_args_to_expr_with_names(
&self,
args: Vec<FunctionArg>,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<(Vec<Expr>, Vec<Option<ArgumentName>>)> {
let results: Result<Vec<(Expr, Option<ArgumentName>)>> = args
Expand All @@ -872,7 +872,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
within_group: Vec<OrderByExpr>,
mut args: Vec<Expr>,
mut arg_names: Vec<Option<ArgumentName>>,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<WithinGroupExtraction> {
let within_group = self.order_by_to_sort_expr(
Expand Down
10 changes: 5 additions & 5 deletions datafusion/sql/src/expr/grouping_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
// under the License.

use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
use datafusion_common::plan_err;
use datafusion_common::{DFSchema, Result};
use datafusion_common::Result;
use datafusion_common::{DFSchemaRef, plan_err};
use datafusion_expr::{Expr, GroupingSet};
use sqlparser::ast::Expr as SQLExpr;

impl<S: ContextProvider> SqlToRel<'_, S> {
pub(super) fn sql_grouping_sets_to_expr(
&self,
exprs: Vec<Vec<SQLExpr>>,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
let args: Result<Vec<Vec<_>>> = exprs
Expand All @@ -42,7 +42,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
pub(super) fn sql_rollup_to_expr(
&self,
exprs: Vec<Vec<SQLExpr>>,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
let args: Result<Vec<_>> = exprs
Expand All @@ -63,7 +63,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
pub(super) fn sql_cube_to_expr(
&self,
exprs: Vec<Vec<SQLExpr>>,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
let args: Result<Vec<_>> = exprs
Expand Down
8 changes: 4 additions & 4 deletions datafusion/sql/src/expr/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use arrow::datatypes::FieldRef;
use datafusion_common::datatype::DataTypeExt;
use datafusion_common::{
Column, DFSchema, Result, Span, TableReference, assert_or_internal_err,
Column, DFSchema, DFSchemaRef, Result, Span, TableReference, assert_or_internal_err,
exec_datafusion_err, internal_err, not_impl_err, plan_datafusion_err, plan_err,
};
use datafusion_expr::planner::PlannerResult;
Expand All @@ -33,7 +33,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
pub(super) fn sql_identifier_to_expr(
&self,
id: Ident,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
let id_span = id.span;
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
pub(crate) fn sql_compound_identifier_to_expr(
&self,
ids: Vec<Ident>,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
assert_or_internal_err!(ids.len() >= 2, "Not a compound identifier: {ids:?}");
Expand Down Expand Up @@ -226,7 +226,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
operand: Option<Box<SQLExpr>>,
conditions: Vec<CaseWhen>,
else_result: Option<Box<SQLExpr>>,
schema: &DFSchema,
schema: &DFSchemaRef,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
let expr = if let Some(e) = operand {
Expand Down
Loading