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

Invalid schema for unions in ViewTable #15134

Closed
Friede80 opened this issue Mar 10, 2025 · 1 comment · Fixed by #15135
Closed

Invalid schema for unions in ViewTable #15134

Friede80 opened this issue Mar 10, 2025 · 1 comment · Fixed by #15135
Labels
bug Something isn't working

Comments

@Friede80
Copy link
Contributor

Friede80 commented Mar 10, 2025

Describe the bug

When a ViewTable is created, the plan is run through the Analyzer with the ExpandWildcardRule and TypeCoercion rules. When this ViewTable is later inlined, it is run though the Analyzer again, but this second pass of TypeCoercion can drop the qualifier information in the DFSchema for fields of a union if a field had previously been cast.

To Reproduce

Below is a test case that demonstrates the error

    #[test]
    fn test_coerce_union() -> Result<()> {
        let left_plan = Arc::new(LogicalPlan::EmptyRelation(EmptyRelation {
            produce_one_row: false,
            schema: Arc::new(
                DFSchema::try_from_qualified_schema(
                    TableReference::full("datafusion", "test", "foo"),
                    &Schema::new(vec![Field::new("a", DataType::Int32, false)]),
                )
                .unwrap(),
            ),
        }));
        let right_plan = Arc::new(LogicalPlan::EmptyRelation(EmptyRelation {
            produce_one_row: false,
            schema: Arc::new(
                DFSchema::try_from_qualified_schema(
                    TableReference::full("datafusion", "test", "foo"),
                    &Schema::new(vec![Field::new("a", DataType::Int64, false)]),
                )
                .unwrap(),
            ),
        }));
        let union = LogicalPlan::Union(Union::try_new_with_loose_types(vec![
            left_plan, right_plan,
        ])?);
        let analyzed_union = Analyzer::with_rules(vec![Arc::new(TypeCoercion::new())])
            .execute_and_check(union, &ConfigOptions::default(), |_, _| {})?;
        let top_level_plan = LogicalPlan::Projection(Projection::try_new(
            vec![wildcard()],
            Arc::new(analyzed_union),
        )?);
        let expanded_plan =
            Analyzer::with_rules(vec![Arc::new(ExpandWildcardRule::new())])
                .execute_and_check(
                    top_level_plan,
                    &ConfigOptions::default(),
                    |_, _| {},
                )?;

        let expected = "Projection: datafusion.test.foo.a\n  Union\n    Projection: CAST(datafusion.test.foo.a AS Int64) AS a\n      EmptyRelation\n    EmptyRelation";
        assert_analyzed_plan_eq(Arc::new(TypeCoercion::new()), expanded_plan, expected)
    }

This will fail with:

Error: Context("type_coercion", SchemaError(FieldNotFound { field: Column { relation: Some(Full { catalog: "datafusion", schema: "test", table: "foo" }), name: "a" }, valid_fields: [Column { relation: None, name: "a" }] }, Some("")))

Expected behavior

The above test should succeed.

Additional context

No response

@Friede80 Friede80 added the bug Something isn't working label Mar 10, 2025
@Friede80
Copy link
Contributor Author

sqllogictest demonstrating the issue:

statement ok
CREATE TABLE t1 (x INT, y INT);

statement ok
INSERT INTO t1 VALUES (3, 3), (3, 3), (1, 1);

statement ok
CREATE TABLE t3 (y BIGINT, z BIGINT);

statement ok
INSERT INTO t3 VALUES (20, 20), (40, 40);

statement ok
CREATE VIEW v1 AS
SELECT y FROM t1 UNION ALL SELECT y FROM t3;


query I
SELECT * FROM v1;
----
3
3
1
20
40
External error: query failed: DataFusion error: type_coercion
caused by
Schema error: No field named t1.y. Did you mean 'y'?.
[SQL] SELECT * FROM v1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant