Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor: Remove more clones from the planner #4546

Merged
merged 1 commit into from
Dec 13, 2022
Merged
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
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would always clone previously, and now it allows the caller to pass in a String if they have one and avoid the clone

}
}

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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is demonstrating you can still call col with a &str but now also with a String or &String to allow backwards compatibility as well as reuse

/// ```
/// # 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