Skip to content
Closed
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
25 changes: 25 additions & 0 deletions datafusion/common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,31 @@ pub fn longest_consecutive_prefix<T: Borrow<usize>>(
count
}

/// formats the Iterator of Display'ble items as a comma separated
/// list
///
/// TODO an example:
/// ```
/// let data = vec!["foo", "bar"];
/// fmt_iterator(DDD);
/// ```
pub fn fmt_iterator<'a>(
iter: impl IntoIterator<Item = &'a dyn std::fmt::Display>,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
let iter = iter.into_iter();
let mut first = true;
for expr in iter {
if !first {
write!(f, ", ")?;
} else {
first = false;
}
write!(f, "{expr}")?;
}
Ok(())
}

/// An extension trait for smart pointers. Provides an interface to get a
/// raw pointer to the data (with metadata stripped away).
///
Expand Down
64 changes: 42 additions & 22 deletions datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::window_frame;
use crate::window_function;
use crate::Operator;
use arrow::datatypes::DataType;
use datafusion_common::utils::fmt_iterator;
use datafusion_common::{plan_err, Column, DataFusionError, Result, ScalarValue};
use std::collections::HashSet;
use std::fmt;
Expand Down Expand Up @@ -898,15 +899,34 @@ impl Expr {
}
}

#[macro_export]
macro_rules! expr_vec_fmt {
( $ARRAY:expr ) => {{
$ARRAY
.iter()
.map(|e| format!("{e}"))
.collect::<Vec<String>>()
.join(", ")
}};
/// Returns a struct that can display the `Vec` of [`Expr`]s as a
/// comma separated display list, without allocating
pub fn expr_vec_fmt<'a>(v: &'a [Expr]) -> CommaListExprFormatter<'a> {
CommaListExprFormatter(v)
}

/// See [`expr_vec_fmt`]
pub struct CommaListExprFormatter<'a>(&'a [Expr]);

impl<'a> Display for CommaListExprFormatter<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fmt_iterator(self.0.iter().map(|e| e as &dyn Display), f)
}
}

/// Returns a struct that can display the `Vec` of [`&Expr`]s as a
/// comma separated display list, without allocating
pub(crate) fn expr_vec_ref_fmt<'a>(v: &'a [&'a Expr]) -> CommaListExprRefFormatter<'a> {
CommaListExprRefFormatter(v)
}

/// See [`expr_vec_ref_fmt`]
pub(crate) struct CommaListExprRefFormatter<'a>(&'a [&'a Expr]);

impl<'a> Display for CommaListExprRefFormatter<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fmt_iterator(self.0.iter().map(|e| e as &dyn Display), f)
}
}

/// Format expressions for display as part of a logical plan. In many cases, this will produce
Expand Down Expand Up @@ -999,10 +1019,10 @@ impl fmt::Display for Expr {
}) => {
fmt_function(f, &fun.to_string(), false, args, true)?;
if !partition_by.is_empty() {
write!(f, " PARTITION BY [{}]", expr_vec_fmt!(partition_by))?;
write!(f, " PARTITION BY [{}]", expr_vec_fmt(partition_by))?;
}
if !order_by.is_empty() {
write!(f, " ORDER BY [{}]", expr_vec_fmt!(order_by))?;
write!(f, " ORDER BY [{}]", expr_vec_fmt(order_by))?;
}
write!(
f,
Expand All @@ -1024,7 +1044,7 @@ impl fmt::Display for Expr {
write!(f, " FILTER (WHERE {fe})")?;
}
if let Some(ob) = order_by {
write!(f, " ORDER BY [{}]", expr_vec_fmt!(ob))?;
write!(f, " ORDER BY [{}]", expr_vec_fmt(ob))?;
}
Ok(())
}
Expand All @@ -1040,7 +1060,7 @@ impl fmt::Display for Expr {
write!(f, " FILTER (WHERE {fe})")?;
}
if let Some(ob) = order_by {
write!(f, " ORDER BY [{}]", expr_vec_fmt!(ob))?;
write!(f, " ORDER BY [{}]", expr_vec_fmt(ob))?;
}
Ok(())
}
Expand Down Expand Up @@ -1110,9 +1130,9 @@ impl fmt::Display for Expr {
negated,
}) => {
if *negated {
write!(f, "{expr} NOT IN ([{}])", expr_vec_fmt!(list))
write!(f, "{expr} NOT IN ([{}])", expr_vec_fmt(list))
} else {
write!(f, "{expr} IN ([{}])", expr_vec_fmt!(list))
write!(f, "{expr} IN ([{}])", expr_vec_fmt(list))
}
}
Expr::Wildcard => write!(f, "*"),
Expand All @@ -1123,11 +1143,11 @@ impl fmt::Display for Expr {
Expr::GroupingSet(grouping_sets) => match grouping_sets {
GroupingSet::Rollup(exprs) => {
// ROLLUP (c0, c1, c2)
write!(f, "ROLLUP ({})", expr_vec_fmt!(exprs))
write!(f, "ROLLUP ({})", expr_vec_fmt(exprs))
}
GroupingSet::Cube(exprs) => {
// CUBE (c0, c1, c2)
write!(f, "CUBE ({})", expr_vec_fmt!(exprs))
write!(f, "CUBE ({})", expr_vec_fmt(exprs))
}
GroupingSet::GroupingSets(lists_of_exprs) => {
// GROUPING SETS ((c0), (c1, c2), (c3, c4))
Expand All @@ -1136,7 +1156,7 @@ impl fmt::Display for Expr {
"GROUPING SETS ({})",
lists_of_exprs
.iter()
.map(|exprs| format!("({})", expr_vec_fmt!(exprs)))
.map(|exprs| format!("({})", expr_vec_fmt(exprs)))
.collect::<Vec<String>>()
.join(", ")
)
Expand Down Expand Up @@ -1344,10 +1364,10 @@ fn create_name(e: &Expr) -> Result<String> {
let mut parts: Vec<String> =
vec![create_function_name(&fun.to_string(), false, args)?];
if !partition_by.is_empty() {
parts.push(format!("PARTITION BY [{}]", expr_vec_fmt!(partition_by)));
parts.push(format!("PARTITION BY [{}]", expr_vec_fmt(partition_by)));
}
if !order_by.is_empty() {
parts.push(format!("ORDER BY [{}]", expr_vec_fmt!(order_by)));
parts.push(format!("ORDER BY [{}]", expr_vec_fmt(order_by)));
}
parts.push(format!("{window_frame}"));
Ok(parts.join(" "))
Expand All @@ -1364,7 +1384,7 @@ fn create_name(e: &Expr) -> Result<String> {
name = format!("{name} FILTER (WHERE {fe})");
};
if let Some(order_by) = order_by {
name = format!("{name} ORDER BY [{}]", expr_vec_fmt!(order_by));
name = format!("{name} ORDER BY [{}]", expr_vec_fmt(order_by));
};
Ok(name)
}
Expand All @@ -1383,7 +1403,7 @@ fn create_name(e: &Expr) -> Result<String> {
info += &format!(" FILTER (WHERE {fe})");
}
if let Some(ob) = order_by {
info += &format!(" ORDER BY ([{}])", expr_vec_fmt!(ob));
info += &format!(" ORDER BY ([{}])", expr_vec_fmt(ob));
}
Ok(format!("{}({}){}", fun.name, names.join(","), info))
}
Expand Down
17 changes: 8 additions & 9 deletions datafusion/expr/src/logical_plan/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

//! Logical plan types

use crate::expr::{Exists, InSubquery, Placeholder};
use crate::expr::{expr_vec_fmt, expr_vec_ref_fmt, Exists, InSubquery, Placeholder};
use crate::expr_rewriter::create_col_from_scalar_expr;
use crate::expr_vec_fmt;
use crate::logical_plan::display::{GraphvizVisitor, IndentVisitor};
use crate::logical_plan::extension::UserDefinedLogicalNode;
use crate::logical_plan::{DmlStatement, Statement};
Expand Down Expand Up @@ -1003,7 +1002,7 @@ impl LogicalPlan {
source.supports_filters_pushdown(&filters)
{
filters.iter().zip(results.iter()).for_each(
|(x, res)| match res {
|(&x, res)| match res {
TableProviderFilterPushDown::Exact => {
full_filter.push(x)
}
Expand All @@ -1021,21 +1020,21 @@ impl LogicalPlan {
write!(
f,
", full_filters=[{}]",
expr_vec_fmt!(full_filter)
expr_vec_ref_fmt(&full_filter)
)?;
};
if !partial_filter.is_empty() {
write!(
f,
", partial_filters=[{}]",
expr_vec_fmt!(partial_filter)
expr_vec_ref_fmt(&partial_filter)
)?;
}
if !unsupported_filters.is_empty() {
write!(
f,
", unsupported_filters=[{}]",
expr_vec_fmt!(unsupported_filters)
expr_vec_ref_fmt(&unsupported_filters)
)?;
}
}
Expand Down Expand Up @@ -1072,7 +1071,7 @@ impl LogicalPlan {
write!(
f,
"WindowAggr: windowExpr=[[{}]]",
expr_vec_fmt!(window_expr)
expr_vec_fmt(window_expr)
)
}
LogicalPlan::Aggregate(Aggregate {
Expand All @@ -1082,8 +1081,8 @@ impl LogicalPlan {
}) => write!(
f,
"Aggregate: groupBy=[[{}]], aggr=[[{}]]",
expr_vec_fmt!(group_expr),
expr_vec_fmt!(aggr_expr)
expr_vec_fmt(group_expr),
expr_vec_fmt(aggr_expr)
),
LogicalPlan::Sort(Sort { expr, fetch, .. }) => {
write!(f, "Sort: ")?;
Expand Down
22 changes: 11 additions & 11 deletions datafusion/expr/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,10 +1291,10 @@ pub fn find_valid_equijoin_key_pair(
#[cfg(test)]
mod tests {
use super::*;
use crate::expr_vec_fmt;
use crate::{
col, cube, expr, grouping_set, rollup, AggregateFunction, WindowFrame,
WindowFunction,
col, cube,
expr::{self, expr_vec_fmt},
grouping_set, rollup, AggregateFunction, WindowFrame, WindowFunction,
};

#[test]
Expand Down Expand Up @@ -1497,22 +1497,22 @@ mod tests {

// 1. col
let sets = enumerate_grouping_sets(vec![simple_col.clone()])?;
let result = format!("[{}]", expr_vec_fmt!(sets));
let result = format!("[{}]", expr_vec_fmt(&sets));
assert_eq!("[simple_col]", &result);

// 2. cube
let sets = enumerate_grouping_sets(vec![cube.clone()])?;
let result = format!("[{}]", expr_vec_fmt!(sets));
let result = format!("[{}]", expr_vec_fmt(&sets));
assert_eq!("[CUBE (col1, col2, col3)]", &result);

// 3. rollup
let sets = enumerate_grouping_sets(vec![rollup.clone()])?;
let result = format!("[{}]", expr_vec_fmt!(sets));
let result = format!("[{}]", expr_vec_fmt(&sets));
assert_eq!("[ROLLUP (col1, col2, col3)]", &result);

// 4. col + cube
let sets = enumerate_grouping_sets(vec![simple_col.clone(), cube.clone()])?;
let result = format!("[{}]", expr_vec_fmt!(sets));
let result = format!("[{}]", expr_vec_fmt(&sets));
assert_eq!(
"[GROUPING SETS (\
(simple_col), \
Expand All @@ -1528,7 +1528,7 @@ mod tests {

// 5. col + rollup
let sets = enumerate_grouping_sets(vec![simple_col.clone(), rollup.clone()])?;
let result = format!("[{}]", expr_vec_fmt!(sets));
let result = format!("[{}]", expr_vec_fmt(&sets));
assert_eq!(
"[GROUPING SETS (\
(simple_col), \
Expand All @@ -1541,7 +1541,7 @@ mod tests {
// 6. col + grouping_set
let sets =
enumerate_grouping_sets(vec![simple_col.clone(), grouping_set.clone()])?;
let result = format!("[{}]", expr_vec_fmt!(sets));
let result = format!("[{}]", expr_vec_fmt(&sets));
assert_eq!(
"[GROUPING SETS (\
(simple_col, col1, col2, col3))]",
Expand All @@ -1554,7 +1554,7 @@ mod tests {
grouping_set,
rollup.clone(),
])?;
let result = format!("[{}]", expr_vec_fmt!(sets));
let result = format!("[{}]", expr_vec_fmt(&sets));
assert_eq!(
"[GROUPING SETS (\
(simple_col, col1, col2, col3), \
Expand All @@ -1566,7 +1566,7 @@ mod tests {

// 8. col + cube + rollup
let sets = enumerate_grouping_sets(vec![simple_col, cube, rollup])?;
let result = format!("[{}]", expr_vec_fmt!(sets));
let result = format!("[{}]", expr_vec_fmt(&sets));
assert_eq!(
"[GROUPING SETS (\
(simple_col), \
Expand Down
8 changes: 5 additions & 3 deletions datafusion/physical-expr/src/scalar_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ use crate::utils::expr_list_eq_strict_order;
use crate::PhysicalExpr;
use arrow::datatypes::{DataType, Schema};
use arrow::record_batch::RecordBatch;
use datafusion_common::utils::fmt_iterator;
use datafusion_common::Result;
use datafusion_expr::expr_vec_fmt;
use datafusion_expr::BuiltinScalarFunction;
use datafusion_expr::ColumnarValue;
use datafusion_expr::ScalarFunctionImplementation;
use std::any::Any;
use std::fmt::Debug;
use std::fmt::{self, Formatter};
use std::fmt::{Debug, Display};
use std::hash::{Hash, Hasher};
use std::sync::Arc;

Expand Down Expand Up @@ -103,7 +103,9 @@ impl ScalarFunctionExpr {

impl fmt::Display for ScalarFunctionExpr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}({})", self.name, expr_vec_fmt!(self.args))
write!(f, "{}(", self.name)?;
fmt_iterator(self.args.iter().map(|v| v as &dyn Display), f)?;
write!(f, ")")
}
}

Expand Down
9 changes: 4 additions & 5 deletions datafusion/sql/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ use sqlparser::ast::Ident;

use datafusion_common::{DataFusionError, Result, ScalarValue};
use datafusion_expr::expr::{
AggregateFunction, AggregateUDF, Between, BinaryExpr, Case, GetIndexedField,
GroupingSet, InList, InSubquery, Like, Placeholder, ScalarFunction, ScalarUDF,
WindowFunction,
expr_vec_fmt, AggregateFunction, AggregateUDF, Between, BinaryExpr, Case,
GetIndexedField, GroupingSet, InList, InSubquery, Like, Placeholder, ScalarFunction,
ScalarUDF, WindowFunction,
};
use datafusion_expr::expr::{Cast, Sort};
use datafusion_expr::expr_vec_fmt;
use datafusion_expr::utils::{expr_as_column_expr, find_column_exprs};
use datafusion_expr::{Expr, LogicalPlan, TryCast};
use std::collections::HashMap;
Expand Down Expand Up @@ -125,7 +124,7 @@ fn check_column_satisfies_expr(
"{}: Expression {} could not be resolved from available columns: {}",
message_prefix,
expr,
expr_vec_fmt!(columns)
expr_vec_fmt(columns)
)));
}
Ok(())
Expand Down