Skip to content

Commit

Permalink
minor: Remove more clones from the planner
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Dec 12, 2022
1 parent 2457ce4 commit ee5c4c4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
21 changes: 18 additions & 3 deletions datafusion/common/src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ impl Column {
}

/// Deserialize a fully qualified name string into a column
pub fn from_qualified_name(flat_name: &str) -> Self {
pub fn from_qualified_name(flat_name: impl Into<String>) -> Self {
let flat_name = flat_name.into();
use sqlparser::tokenizer::Token;

let dialect = sqlparser::dialect::GenericDialect {};
let mut tokenizer = sqlparser::tokenizer::Tokenizer::new(&dialect, flat_name);
let mut tokenizer = sqlparser::tokenizer::Tokenizer::new(&dialect, &flat_name);
if let Ok(tokens) = tokenizer.tokenize() {
if let [Token::Word(relation), Token::Period, Token::Word(name)] =
tokens.as_slice()
Expand All @@ -70,7 +71,7 @@ impl Column {
// name
Column {
relation: None,
name: String::from(flat_name),
name: flat_name,
}
}

Expand Down Expand Up @@ -145,6 +146,20 @@ impl From<&str> for Column {
}
}

/// Create a column, cloning the string
impl From<&String> for Column {
fn from(c: &String) -> Self {
Self::from_qualified_name(c)
}
}

/// Create a column, reusing the existing string
impl From<String> for Column {
fn from(c: String) -> Self {
Self::from_qualified_name(c)
}
}

impl FromStr for Column {
type Err = Infallible;

Expand Down
10 changes: 8 additions & 2 deletions datafusion/expr/src/expr_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ use crate::{
ScalarFunctionImplementation, ScalarUDF, Signature, StateTypeFunction, Volatility,
};
use arrow::datatypes::DataType;
use datafusion_common::Result;
use datafusion_common::{Column, Result};
use std::sync::Arc;

/// Create a column expression based on a qualified or unqualified column name
pub fn col(ident: &str) -> Expr {
///
/// example:
/// ```
/// # use datafusion_expr::col;
/// let c = col("my_column");
/// ```
pub fn col(ident: impl Into<Column>) -> Expr {
Expr::Column(ident.into())
}

Expand Down
7 changes: 5 additions & 2 deletions datafusion/expr/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl LogicalPlanBuilder {
}

/// Apply an alias
pub fn alias(&self, alias: &str) -> Result<Self> {
pub fn alias(&self, alias: impl Into<String>) -> Result<Self> {
Ok(Self::from(subquery_alias(&self.plan, alias)?))
}

Expand Down Expand Up @@ -977,7 +977,10 @@ pub fn project(
}

/// Create a SubqueryAlias to wrap a LogicalPlan.
pub fn subquery_alias(plan: &LogicalPlan, alias: &str) -> Result<LogicalPlan> {
pub fn subquery_alias(
plan: &LogicalPlan,
alias: impl Into<String>,
) -> Result<LogicalPlan> {
Ok(LogicalPlan::SubqueryAlias(SubqueryAlias::try_new(
plan.clone(),
alias,
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1965,7 +1965,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {

SQLExpr::MapAccess { column, keys } => {
if let SQLExpr::Identifier(id) = *column {
plan_indexed(col(&normalize_ident(id)), keys)
plan_indexed(col(normalize_ident(id)), keys)
} else {
Err(DataFusionError::NotImplemented(format!(
"map access requires an identifier, found column {} instead",
Expand Down

0 comments on commit ee5c4c4

Please sign in to comment.