Skip to content

Commit d4820d1

Browse files
fix: log metadata differences when comparing physical and logical schema (#19070)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #19069 ## Rationale for this change Differences in physical/logical schema metadata can cause aggregate physical planning to fail, but these differences are not shown in the error output. ### Previous message ``` Error while planning query: Internal error: Physical input schema should be the same as the one converted from logical input schema. Differences: .` ``` ### Example of updated message ``` Physical input schema should be the same as the one converted from logical input schema. Differences: - field metadata at index 0 [usage_idle]: (physical) {"iox::column::type": "iox::column_type::field::float"} vs (logical) {} - field metadata at index 1 [usage_system]: (physical) {"iox::column::type": "iox::column_type::field::float"} vs (logical) {}. ``` <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> Minor improvements to error messages. <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 91e623b commit d4820d1

File tree

1 file changed

+244
-0
lines changed

1 file changed

+244
-0
lines changed

datafusion/core/src/physical_planner.rs

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,17 @@ impl DefaultPhysicalPlanner {
687687
)
688688
{
689689
let mut differences = Vec::new();
690+
691+
if physical_input_schema.metadata()
692+
!= physical_input_schema_from_logical.metadata()
693+
{
694+
differences.push(format!(
695+
"schema metadata differs: (physical) {:?} vs (logical) {:?}",
696+
physical_input_schema.metadata(),
697+
physical_input_schema_from_logical.metadata()
698+
));
699+
}
700+
690701
if physical_input_schema.fields().len()
691702
!= physical_input_schema_from_logical.fields().len()
692703
{
@@ -716,6 +727,15 @@ impl DefaultPhysicalPlanner {
716727
if physical_field.is_nullable() && !logical_field.is_nullable() {
717728
differences.push(format!("field nullability at index {} [{}]: (physical) {} vs (logical) {}", i, physical_field.name(), physical_field.is_nullable(), logical_field.is_nullable()));
718729
}
730+
if physical_field.metadata() != logical_field.metadata() {
731+
differences.push(format!(
732+
"field metadata at index {} [{}]: (physical) {:?} vs (logical) {:?}",
733+
i,
734+
physical_field.name(),
735+
physical_field.metadata(),
736+
logical_field.metadata()
737+
));
738+
}
719739
}
720740
return internal_err!("Physical input schema should be the same as the one converted from logical input schema. Differences: {}", differences
721741
.iter()
@@ -3921,4 +3941,228 @@ digraph {
39213941

39223942
Ok(())
39233943
}
3944+
3945+
// --- Tests for aggregate schema mismatch error messages ---
3946+
3947+
use crate::catalog::TableProvider;
3948+
use datafusion_catalog::Session;
3949+
use datafusion_expr::TableType;
3950+
3951+
/// A TableProvider that returns schemas for logical planning vs physical planning.
3952+
/// Used to test schema mismatch error messages.
3953+
#[derive(Debug)]
3954+
struct MockSchemaTableProvider {
3955+
logical_schema: SchemaRef,
3956+
physical_schema: SchemaRef,
3957+
}
3958+
3959+
#[async_trait]
3960+
impl TableProvider for MockSchemaTableProvider {
3961+
fn as_any(&self) -> &dyn Any {
3962+
self
3963+
}
3964+
3965+
fn schema(&self) -> SchemaRef {
3966+
Arc::clone(&self.logical_schema)
3967+
}
3968+
3969+
fn table_type(&self) -> TableType {
3970+
TableType::Base
3971+
}
3972+
3973+
async fn scan(
3974+
&self,
3975+
_state: &dyn Session,
3976+
_projection: Option<&Vec<usize>>,
3977+
_filters: &[Expr],
3978+
_limit: Option<usize>,
3979+
) -> Result<Arc<dyn ExecutionPlan>> {
3980+
Ok(Arc::new(NoOpExecutionPlan::new(Arc::clone(
3981+
&self.physical_schema,
3982+
))))
3983+
}
3984+
}
3985+
3986+
/// Attempts to plan a query with potentially mismatched schemas.
3987+
async fn plan_with_schemas(
3988+
logical_schema: SchemaRef,
3989+
physical_schema: SchemaRef,
3990+
query: &str,
3991+
) -> Result<Arc<dyn ExecutionPlan>> {
3992+
let provider = MockSchemaTableProvider {
3993+
logical_schema,
3994+
physical_schema,
3995+
};
3996+
let ctx = SessionContext::new();
3997+
ctx.register_table("test", Arc::new(provider)).unwrap();
3998+
3999+
ctx.sql(query).await.unwrap().create_physical_plan().await
4000+
}
4001+
4002+
#[tokio::test]
4003+
// When schemas match, planning proceeds past the schema_satisfied_by check.
4004+
// It then panics on unimplemented error in NoOpExecutionPlan.
4005+
#[should_panic(expected = "NoOpExecutionPlan")]
4006+
async fn test_aggregate_schema_check_passes() {
4007+
let schema =
4008+
Arc::new(Schema::new(vec![Field::new("c1", DataType::Int32, false)]));
4009+
4010+
plan_with_schemas(
4011+
Arc::clone(&schema),
4012+
schema,
4013+
"SELECT count(*) FROM test GROUP BY c1",
4014+
)
4015+
.await
4016+
.unwrap();
4017+
}
4018+
4019+
#[tokio::test]
4020+
async fn test_aggregate_schema_mismatch_metadata() {
4021+
let logical_schema =
4022+
Arc::new(Schema::new(vec![Field::new("c1", DataType::Int32, false)]));
4023+
let physical_schema = Arc::new(
4024+
Schema::new(vec![Field::new("c1", DataType::Int32, false)])
4025+
.with_metadata(HashMap::from([("key".into(), "value".into())])),
4026+
);
4027+
4028+
let err = plan_with_schemas(
4029+
logical_schema,
4030+
physical_schema,
4031+
"SELECT count(*) FROM test GROUP BY c1",
4032+
)
4033+
.await
4034+
.unwrap_err();
4035+
4036+
assert_contains!(err.to_string(), "schema metadata differs");
4037+
}
4038+
4039+
#[tokio::test]
4040+
async fn test_aggregate_schema_mismatch_field_count() {
4041+
let logical_schema =
4042+
Arc::new(Schema::new(vec![Field::new("c1", DataType::Int32, false)]));
4043+
let physical_schema = Arc::new(Schema::new(vec![
4044+
Field::new("c1", DataType::Int32, false),
4045+
Field::new("c2", DataType::Int32, false),
4046+
]));
4047+
4048+
let err = plan_with_schemas(
4049+
logical_schema,
4050+
physical_schema,
4051+
"SELECT count(*) FROM test GROUP BY c1",
4052+
)
4053+
.await
4054+
.unwrap_err();
4055+
4056+
assert_contains!(err.to_string(), "Different number of fields");
4057+
}
4058+
4059+
#[tokio::test]
4060+
async fn test_aggregate_schema_mismatch_field_name() {
4061+
let logical_schema =
4062+
Arc::new(Schema::new(vec![Field::new("c1", DataType::Int32, false)]));
4063+
let physical_schema = Arc::new(Schema::new(vec![Field::new(
4064+
"different_name",
4065+
DataType::Int32,
4066+
false,
4067+
)]));
4068+
4069+
let err = plan_with_schemas(
4070+
logical_schema,
4071+
physical_schema,
4072+
"SELECT count(*) FROM test GROUP BY c1",
4073+
)
4074+
.await
4075+
.unwrap_err();
4076+
4077+
assert_contains!(err.to_string(), "field name at index");
4078+
}
4079+
4080+
#[tokio::test]
4081+
async fn test_aggregate_schema_mismatch_field_type() {
4082+
let logical_schema =
4083+
Arc::new(Schema::new(vec![Field::new("c1", DataType::Int32, false)]));
4084+
let physical_schema =
4085+
Arc::new(Schema::new(vec![Field::new("c1", DataType::Int64, false)]));
4086+
4087+
let err = plan_with_schemas(
4088+
logical_schema,
4089+
physical_schema,
4090+
"SELECT count(*) FROM test GROUP BY c1",
4091+
)
4092+
.await
4093+
.unwrap_err();
4094+
4095+
assert_contains!(err.to_string(), "field data type at index");
4096+
}
4097+
4098+
#[tokio::test]
4099+
async fn test_aggregate_schema_mismatch_field_nullability() {
4100+
let logical_schema =
4101+
Arc::new(Schema::new(vec![Field::new("c1", DataType::Int32, false)]));
4102+
let physical_schema =
4103+
Arc::new(Schema::new(vec![Field::new("c1", DataType::Int32, true)]));
4104+
4105+
let err = plan_with_schemas(
4106+
logical_schema,
4107+
physical_schema,
4108+
"SELECT count(*) FROM test GROUP BY c1",
4109+
)
4110+
.await
4111+
.unwrap_err();
4112+
4113+
assert_contains!(err.to_string(), "field nullability at index");
4114+
}
4115+
4116+
#[tokio::test]
4117+
async fn test_aggregate_schema_mismatch_field_metadata() {
4118+
let logical_schema =
4119+
Arc::new(Schema::new(vec![Field::new("c1", DataType::Int32, false)]));
4120+
let physical_schema =
4121+
Arc::new(Schema::new(vec![Field::new("c1", DataType::Int32, false)
4122+
.with_metadata(HashMap::from([("key".into(), "value".into())]))]));
4123+
4124+
let err = plan_with_schemas(
4125+
logical_schema,
4126+
physical_schema,
4127+
"SELECT count(*) FROM test GROUP BY c1",
4128+
)
4129+
.await
4130+
.unwrap_err();
4131+
4132+
assert_contains!(err.to_string(), "field metadata at index");
4133+
}
4134+
4135+
#[tokio::test]
4136+
async fn test_aggregate_schema_mismatch_multiple() {
4137+
let logical_schema = Arc::new(Schema::new(vec![
4138+
Field::new("c1", DataType::Int32, false),
4139+
Field::new("c2", DataType::Utf8, false),
4140+
]));
4141+
let physical_schema = Arc::new(
4142+
Schema::new(vec![
4143+
Field::new("c1", DataType::Int64, true)
4144+
.with_metadata(HashMap::from([("key".into(), "value".into())])),
4145+
Field::new("c2", DataType::Utf8, false),
4146+
])
4147+
.with_metadata(HashMap::from([(
4148+
"schema_key".into(),
4149+
"schema_value".into(),
4150+
)])),
4151+
);
4152+
4153+
let err = plan_with_schemas(
4154+
logical_schema,
4155+
physical_schema,
4156+
"SELECT count(*) FROM test GROUP BY c1",
4157+
)
4158+
.await
4159+
.unwrap_err();
4160+
4161+
// Verify all applicable error fragments are present
4162+
let err_str = err.to_string();
4163+
assert_contains!(&err_str, "schema metadata differs");
4164+
assert_contains!(&err_str, "field data type at index");
4165+
assert_contains!(&err_str, "field nullability at index");
4166+
assert_contains!(&err_str, "field metadata at index");
4167+
}
39244168
}

0 commit comments

Comments
 (0)