Skip to content

Commit 2457ce4

Browse files
authored
Minor: Avoid cloning as many Ident during SQL planning (#4534)
* Minor: Avoid cloning some Identifiers during planning * Remove another copy
1 parent 4ecf3e7 commit 2457ce4

3 files changed

Lines changed: 25 additions & 31 deletions

File tree

‎datafusion/expr/src/logical_plan/plan.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,13 +1300,17 @@ pub struct SubqueryAlias {
13001300
}
13011301

13021302
impl SubqueryAlias {
1303-
pub fn try_new(plan: LogicalPlan, alias: &str) -> datafusion_common::Result<Self> {
1303+
pub fn try_new(
1304+
plan: LogicalPlan,
1305+
alias: impl Into<String>,
1306+
) -> datafusion_common::Result<Self> {
1307+
let alias = alias.into();
13041308
let schema: Schema = plan.schema().as_ref().clone().into();
13051309
let schema =
1306-
DFSchemaRef::new(DFSchema::try_from_qualified_schema(alias, &schema)?);
1310+
DFSchemaRef::new(DFSchema::try_from_qualified_schema(&alias, &schema)?);
13071311
Ok(SubqueryAlias {
13081312
input: Arc::new(plan),
1309-
alias: alias.to_string(),
1313+
alias,
13101314
schema,
13111315
})
13121316
}

‎datafusion/sql/src/planner.rs‎

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ use datafusion_expr::{
7272
};
7373

7474
use crate::parser::{CreateExternalTable, DescribeTable, Statement as DFStatement};
75-
use crate::utils::{
76-
make_decimal_type, normalize_ident, normalize_ident_owned, resolve_columns,
77-
};
75+
use crate::utils::{make_decimal_type, normalize_ident, resolve_columns};
7876

7977
use super::{
8078
parser::DFParser,
@@ -296,7 +294,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
296294
..
297295
} if with_options.is_empty() => {
298296
let mut plan = self.query_to_plan(*query, &mut PlannerContext::new())?;
299-
plan = self.apply_expr_alias(plan, &columns)?;
297+
plan = self.apply_expr_alias(plan, columns)?;
300298

301299
Ok(LogicalPlan::CreateView(CreateView {
302300
name: object_name_to_table_reference(name)?,
@@ -484,7 +482,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
484482

485483
for cte in with.cte_tables {
486484
// A `WITH` block can't use the same name more than once
487-
let cte_name = normalize_ident(&cte.alias.name);
485+
let cte_name = normalize_ident(cte.alias.name.clone());
488486
if planner_context.ctes.contains_key(&cte_name) {
489487
return Err(DataFusionError::SQL(ParserError(format!(
490488
"WITH query name {:?} specified more than once",
@@ -689,7 +687,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
689687
.iter()
690688
.any(|x| x.option == ColumnOption::Null);
691689
fields.push(Field::new(
692-
&normalize_ident(&column.name),
690+
&normalize_ident(column.name),
693691
data_type,
694692
allow_null,
695693
));
@@ -894,7 +892,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
894892
JoinConstraint::Using(idents) => {
895893
let keys: Vec<Column> = idents
896894
.into_iter()
897-
.map(|x| Column::from_name(normalize_ident(&x)))
895+
.map(|x| Column::from_name(normalize_ident(x)))
898896
.collect();
899897
LogicalPlanBuilder::from(left)
900898
.join_using(&right, join_type, keys)?
@@ -977,16 +975,16 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
977975
) -> Result<LogicalPlan> {
978976
let apply_name_plan = LogicalPlan::SubqueryAlias(SubqueryAlias::try_new(
979977
plan,
980-
&normalize_ident(&alias.name),
978+
normalize_ident(alias.name),
981979
)?);
982980

983-
self.apply_expr_alias(apply_name_plan, &alias.columns)
981+
self.apply_expr_alias(apply_name_plan, alias.columns)
984982
}
985983

986984
fn apply_expr_alias(
987985
&self,
988986
plan: LogicalPlan,
989-
idents: &Vec<Ident>,
987+
idents: Vec<Ident>,
990988
) -> Result<LogicalPlan> {
991989
if idents.is_empty() {
992990
Ok(plan)
@@ -999,7 +997,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
999997
} else {
1000998
let fields = plan.schema().fields().clone();
1001999
LogicalPlanBuilder::from(plan)
1002-
.project(fields.iter().zip(idents.iter()).map(|(field, ident)| {
1000+
.project(fields.iter().zip(idents.into_iter()).map(|(field, ident)| {
10031001
col(field.name()).alias(normalize_ident(ident))
10041002
}))?
10051003
.build()
@@ -1609,7 +1607,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
16091607
from_schema,
16101608
&[select_expr.clone()],
16111609
)?;
1612-
let expr = Alias(Box::new(select_expr), normalize_ident(&alias));
1610+
let expr = Alias(Box::new(select_expr), normalize_ident(alias));
16131611
Ok(vec![normalize_col(expr, plan)?])
16141612
}
16151613
SelectItem::Wildcard(options) => {
@@ -1960,13 +1958,13 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
19601958

19611959
Ok(Expr::Column(Column {
19621960
relation: None,
1963-
name: normalize_ident(&id),
1961+
name: normalize_ident(id),
19641962
}))
19651963
}
19661964
}
19671965

1968-
SQLExpr::MapAccess { ref column, keys } => {
1969-
if let SQLExpr::Identifier(ref id) = column.as_ref() {
1966+
SQLExpr::MapAccess { column, keys } => {
1967+
if let SQLExpr::Identifier(id) = *column {
19701968
plan_indexed(col(&normalize_ident(id)), keys)
19711969
} else {
19721970
Err(DataFusionError::NotImplemented(format!(
@@ -1983,7 +1981,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
19831981

19841982
SQLExpr::CompoundIdentifier(ids) => {
19851983
if ids[0].value.starts_with('@') {
1986-
let var_names: Vec<_> = ids.into_iter().map(|s| normalize_ident(&s)).collect();
1984+
let var_names: Vec<_> = ids.into_iter().map(normalize_ident).collect();
19871985
let ty = self
19881986
.schema_provider
19891987
.get_variable_type(&var_names)
@@ -2308,7 +2306,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
23082306
// (e.g. "foo.bar") for function names yet
23092307
function.name.to_string()
23102308
} else {
2311-
normalize_ident(&function.name.0[0])
2309+
normalize_ident(function.name.0[0].clone())
23122310
};
23132311

23142312
// first, check SQL reserved words
@@ -3074,7 +3072,7 @@ fn idents_to_table_reference(idents: Vec<Ident>) -> Result<OwnedTableReference>
30743072
impl IdentTaker {
30753073
fn take(&mut self) -> String {
30763074
let ident = self.0.pop().expect("no more identifiers");
3077-
normalize_ident_owned(ident)
3075+
normalize_ident(ident)
30783076
}
30793077
}
30803078

@@ -3117,7 +3115,7 @@ pub fn object_name_to_qualifier(sql_table_name: &ObjectName) -> String {
31173115
.rev()
31183116
.zip(columns)
31193117
.map(|(ident, column_name)| {
3120-
format!(r#"{} = '{}'"#, column_name, normalize_ident(ident))
3118+
format!(r#"{} = '{}'"#, column_name, normalize_ident(ident.clone()))
31213119
})
31223120
.collect::<Vec<_>>()
31233121
.join(" AND ")

‎datafusion/sql/src/utils.rs‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -539,16 +539,8 @@ pub(crate) fn make_decimal_type(
539539
}
540540
}
541541

542-
// Normalize an identifier to a lowercase string unless the identifier is quoted.
543-
pub(crate) fn normalize_ident(id: &Ident) -> String {
544-
match id.quote_style {
545-
Some(_) => id.value.clone(),
546-
None => id.value.to_ascii_lowercase(),
547-
}
548-
}
549-
550542
// Normalize an owned identifier to a lowercase string unless the identifier is quoted.
551-
pub(crate) fn normalize_ident_owned(id: Ident) -> String {
543+
pub(crate) fn normalize_ident(id: Ident) -> String {
552544
match id.quote_style {
553545
Some(_) => id.value,
554546
None => id.value.to_ascii_lowercase(),

0 commit comments

Comments
 (0)