Skip to content

fix(substrait): fix regressed edge case in renaming inner struct fields #15634

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

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
18 changes: 12 additions & 6 deletions datafusion/common/src/dfschema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ impl DFSchema {
|| (!DFSchema::datatype_is_semantically_equal(
f1.data_type(),
f2.data_type(),
) && !can_cast_types(f2.data_type(), f1.data_type()))
))
{
_plan_err!(
"Schema mismatch: Expected field '{}' with type {:?}, \
Expand All @@ -659,9 +659,12 @@ impl DFSchema {
}

/// Checks if two [`DataType`]s are logically equal. This is a notably weaker constraint
/// than datatype_is_semantically_equal in that a Dictionary<K,V> type is logically
/// equal to a plain V type, but not semantically equal. Dictionary<K1, V1> is also
/// logically equal to Dictionary<K2, V1>.
/// than datatype_is_semantically_equal in that different representations of same data can be
/// logically but not semantically equivalent. Semantically equivalent types are always also
/// logically equivalent. For example:
/// - a Dictionary<K,V> type is logically equal to a plain V type
/// - a Dictionary<K1, V1> is also logically equal to Dictionary<K2, V1>
/// - Utf8 and Utf8View are logically equal
pub fn datatype_is_logically_equal(dt1: &DataType, dt2: &DataType) -> bool {
// check nested fields
match (dt1, dt2) {
Expand Down Expand Up @@ -711,12 +714,15 @@ impl DFSchema {
.zip(iter2)
.all(|((t1, f1), (t2, f2))| t1 == t2 && Self::field_is_logically_equal(f1, f2))
}
_ => dt1 == dt2,
// Utf8 and Utf8View are logically equivalent
(DataType::Utf8, DataType::Utf8View) => true,
Copy link
Contributor

@zhuqi-lucas zhuqi-lucas Apr 8, 2025

Choose a reason for hiding this comment

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

Thank you @Blizzara for fix, this new relax looks good to me, and if we want more relax type, we can add later.

(DataType::Utf8View, DataType::Utf8) => true,
_ => Self::datatype_is_semantically_equal(dt1, dt2),
}
}

/// Returns true of two [`DataType`]s are semantically equal (same
/// name and type), ignoring both metadata and nullability.
/// name and type), ignoring both metadata and nullability, and decimal precision/scale.
///
/// request to upstream: <https://github.com/apache/arrow-rs/issues/3199>
pub fn datatype_is_semantically_equal(dt1: &DataType, dt2: &DataType) -> bool {
Expand Down
6 changes: 3 additions & 3 deletions datafusion/expr/src/logical_plan/invariants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ fn assert_valid_semantic_plan(plan: &LogicalPlan) -> Result<()> {
/// Returns an error if the plan does not have the expected schema.
/// Ignores metadata and nullability.
pub fn assert_expected_schema(schema: &DFSchemaRef, plan: &LogicalPlan) -> Result<()> {
let compatible = plan.schema().has_equivalent_names_and_types(schema);
let compatible = plan.schema().logically_equivalent_names_and_types(schema);

if let Err(e) = compatible {
if !compatible {
internal_err!(
"Failed due to a difference in schemas: {e}, original schema: {:?}, new schema: {:?}",
"Failed due to a difference in schemas: original schema: {:?}, new schema: {:?}",
schema,
plan.schema()
)
Expand Down
7 changes: 5 additions & 2 deletions datafusion/optimizer/src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,11 @@ mod tests {
});
let err = opt.optimize(plan, &config, &observe).unwrap_err();

// Simplify assert to check the error message contains the expected message, which is only the schema length mismatch
assert_contains!(err.strip_backtrace(), "Schema mismatch: the schema length are not same Expected schema length: 3, got: 0");
// Simplify assert to check the error message contains the expected message
assert_contains!(
err.strip_backtrace(),
"Failed due to a difference in schemas: original schema: DFSchema"
);
}

#[test]
Expand Down
42 changes: 41 additions & 1 deletion datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ async fn roundtrip_literal_list() -> Result<()> {
async fn roundtrip_literal_struct() -> Result<()> {
let plan = generate_plan_from_sql(
"SELECT STRUCT(1, true, CAST(NULL AS STRING)) FROM data",
false,
true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

unrelated, but it was passing with true so why not have that

true,
)
.await?;
Expand All @@ -1076,6 +1076,46 @@ async fn roundtrip_literal_struct() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn roundtrip_literal_named_struct() -> Result<()> {
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 uses a named_struct() func so it wasn't hitting the bug, but I kept it as an addition still

let plan = generate_plan_from_sql(
"SELECT STRUCT(1 as int_field, true as boolean_field, CAST(NULL AS STRING) as string_field) FROM data",
true,
true,
)
.await?;

assert_snapshot!(
plan,
@r#"
Projection: Struct({int_field:1,boolean_field:true,string_field:}) AS named_struct(Utf8("int_field"),Int64(1),Utf8("boolean_field"),Boolean(true),Utf8("string_field"),NULL)
TableScan: data projection=[]
"#
);
Ok(())
}

#[tokio::test]
async fn roundtrip_literal_renamed_struct() -> Result<()> {
// This test aims to hit a case where the struct column itself has the expected name, but its
// inner field needs to be renamed.
let plan = generate_plan_from_sql(
"SELECT CAST((STRUCT(1)) AS Struct<\"int_field\"Int>) AS 'Struct({c0:1})' FROM data",
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand how this query is converting String --> StringView

(this seems like a good test in my opinion)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, there's no String -> StringView in this test. This is just the test the regression that happened in Substrait consumer, where we were no longer renaming the inner fields, as the "is the schema the same" check passed too easily.

The String -> StringView is tested by the slt tests added in #15239

true,
true,
)
.await?;

assert_snapshot!(
plan,
@r#"
Projection: Struct({int_field:1}) AS Struct({c0:1})
TableScan: data projection=[]
"#
);
Ok(())
}

#[tokio::test]
async fn roundtrip_values() -> Result<()> {
// TODO: would be nice to have a struct inside the LargeList, but arrow_cast doesn't support that currently
Expand Down