Skip to content
Open
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
17 changes: 13 additions & 4 deletions datafusion-examples/examples/query_planning/expr_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ fn boundary_analysis_in_conjunctions_demo() -> Result<()> {
Ok(())
}

/// This function shows how to use `Expr::get_type` to retrieve the DataType
/// This function shows how to use `Expr::to_field` to retrieve the DataType
/// of an expression
fn expression_type_demo() -> Result<()> {
let expr = col("c");
Expand All @@ -481,14 +481,20 @@ fn expression_type_demo() -> Result<()> {
vec![Field::new("c", DataType::Utf8, true)].into(),
HashMap::new(),
)?;
assert_eq!("Utf8", format!("{}", expr.get_type(&schema).unwrap()));
assert_eq!(
"Utf8",
format!("{}", expr.to_field(&schema).unwrap().1.data_type())
);

// Using a schema where the column `foo` is of type Int32
let schema = DFSchema::from_unqualified_fields(
vec![Field::new("c", DataType::Int32, true)].into(),
HashMap::new(),
)?;
assert_eq!("Int32", format!("{}", expr.get_type(&schema).unwrap()));
assert_eq!(
"Int32",
format!("{}", expr.to_field(&schema).unwrap().1.data_type())
);

// Get the type of an expression that adds 2 columns. Adding an Int32
// and Float32 results in Float32 type
Expand All @@ -501,7 +507,10 @@ fn expression_type_demo() -> Result<()> {
.into(),
HashMap::new(),
)?;
assert_eq!("Float32", format!("{}", expr.get_type(&schema).unwrap()));
assert_eq!(
"Float32",
format!("{}", expr.to_field(&schema).unwrap().1.data_type())
);

Ok(())
}
Expand Down
20 changes: 4 additions & 16 deletions datafusion/common/src/dfschema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,21 +1213,25 @@ impl Display for DFSchema {
/// widely used in the DataFusion codebase.
pub trait ExprSchema: std::fmt::Debug {
/// Is this column reference nullable?
#[deprecated(since = "53.0.0", note = "use field_from_column")]
fn nullable(&self, col: &Column) -> Result<bool> {
Ok(self.field_from_column(col)?.is_nullable())
}

/// What is the datatype of this column?
#[deprecated(since = "53.0.0", note = "use field_from_column")]
fn data_type(&self, col: &Column) -> Result<&DataType> {
Ok(self.field_from_column(col)?.data_type())
}

/// Returns the column's optional metadata.
#[deprecated(since = "53.0.0", note = "use field_from_column")]
fn metadata(&self, col: &Column) -> Result<&HashMap<String, String>> {
Ok(self.field_from_column(col)?.metadata())
}

/// Return the column's datatype and nullability
#[deprecated(since = "53.0.0", note = "use field_from_column")]
fn data_type_and_nullable(&self, col: &Column) -> Result<(&DataType, bool)> {
let field = self.field_from_column(col)?;
Ok((field.data_type(), field.is_nullable()))
Expand All @@ -1239,22 +1243,6 @@ pub trait ExprSchema: std::fmt::Debug {

// Implement `ExprSchema` for `Arc<DFSchema>`
impl<P: AsRef<DFSchema> + std::fmt::Debug> ExprSchema for P {
fn nullable(&self, col: &Column) -> Result<bool> {
self.as_ref().nullable(col)
}

fn data_type(&self, col: &Column) -> Result<&DataType> {
self.as_ref().data_type(col)
}

fn metadata(&self, col: &Column) -> Result<&HashMap<String, String>> {
ExprSchema::metadata(self.as_ref(), col)
}

fn data_type_and_nullable(&self, col: &Column) -> Result<(&DataType, bool)> {
self.as_ref().data_type_and_nullable(col)
}

fn field_from_column(&self, col: &Column) -> Result<&FieldRef> {
self.as_ref().field_from_column(col)
}
Expand Down
7 changes: 5 additions & 2 deletions datafusion/common/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5172,8 +5172,11 @@ impl fmt::Debug for ScalarValue {
ScalarValue::List(_) => write!(f, "List({self})"),
ScalarValue::LargeList(_) => write!(f, "LargeList({self})"),
ScalarValue::Struct(struct_arr) => {
// ScalarValue Struct should always have a single element
assert_eq!(struct_arr.len(), 1);
// ScalarValue Struct may have 0 rows (e.g. empty array not foldable) or 1 row
if struct_arr.is_empty() {
return write!(f, "Struct({{}})");
}
Comment on lines +5175 to +5178
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem correct; a scalar should always have length 1? Also this fix seems unrelated to the issue at hand

assert_eq!(struct_arr.len(), 1, "Struct ScalarValue with >1 row");

let columns = struct_arr.columns();
let fields = struct_arr.fields();
Expand Down
4 changes: 2 additions & 2 deletions datafusion/core/tests/expr_api/simplification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,14 +748,14 @@ fn test_simplify_concat() -> Result<()> {
null,
col("c5"),
]);
let expr_datatype = expr.get_type(schema.as_ref())?;
let expr_datatype = expr.to_field(schema.as_ref())?.1.data_type().clone();
let expected = concat(vec![
col("c1"),
lit(ScalarValue::Utf8View(Some("hello rust!".to_string()))),
col("c2"),
col("c5"),
]);
let expected_datatype = expected.get_type(schema.as_ref())?;
let expected_datatype = expected.to_field(schema.as_ref())?.1.data_type().clone();
assert_eq!(expr_datatype, expected_datatype);
test_simplify(expr, expected);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion datafusion/core/tests/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ fn test_nested_schema_nullability() {
.unwrap();

let expr = col("parent").field("child");
assert!(expr.nullable(&dfschema).unwrap());
assert!(expr.to_field(&dfschema).unwrap().1.is_nullable());
}

#[test]
Expand Down
4 changes: 3 additions & 1 deletion datafusion/expr/src/conditional_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ impl CaseBuilder {
let then_types: Vec<DataType> = then_expr
.iter()
.map(|e| match e {
Expr::Literal(_, _) => e.get_type(&DFSchema::empty()),
Expr::Literal(_, _) => {
Ok(e.to_field(&DFSchema::empty())?.1.data_type().clone())
}
_ => Ok(DataType::Null),
})
.collect::<Result<Vec<_>>>()?;
Expand Down
2 changes: 1 addition & 1 deletion datafusion/expr/src/expr_rewriter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fn coerce_exprs_for_schema(
.enumerate()
.map(|(idx, expr)| {
let new_type = dst_schema.field(idx).data_type();
if new_type != &expr.get_type(src_schema)? {
if new_type != expr.to_field(src_schema)?.1.data_type() {
match expr {
Expr::Alias(Alias { expr, name, .. }) => {
Ok(expr.cast_to(new_type, src_schema)?.alias(name))
Expand Down
10 changes: 7 additions & 3 deletions datafusion/expr/src/expr_rewriter/order_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,22 @@ mod test {
TestCase {
desc: r#"min(c2) --> "min(c2)" -- (column *named* "min(t.c2)"!)"#,
input: sort(min(col("c2"))),
expected: sort(col("min(t.c2)")),
expected: sort(Expr::Column(Column::new_unqualified("min(t.c2)"))),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this being changed?

},
TestCase {
desc: r#"c1 + min(c2) --> "c1 + min(c2)" -- (column *named* "min(t.c2)"!)"#,
input: sort(col("c1") + min(col("c2"))),
// should be "c1" not t.c1
expected: sort(col("c1") + col("min(t.c2)")),
expected: sort(
col("c1") + Expr::Column(Column::new_unqualified("min(t.c2)")),
),
},
TestCase {
desc: r#"avg(c3) --> "avg(t.c3)" as average (column *named* "avg(t.c3)", aliased)"#,
input: sort(avg(col("c3"))),
expected: sort(col("avg(t.c3)").alias("average")),
expected: sort(
Expr::Column(Column::new_unqualified("avg(t.c3)")).alias("average"),
),
},
];

Expand Down
Loading
Loading