You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Friede80 (via GitHub)" <gi...@apache.org> on 2025/03/10 20:09:44 UTC

[I] Invalid schema for unions in ViewTable [datafusion]

Friede80 opened a new issue, #15134:
URL: https://github.com/apache/datafusion/issues/15134

   ### 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
   ```rust
   #[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_


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Invalid schema for unions in ViewTable [datafusion]

Posted by "Friede80 (via GitHub)" <gi...@apache.org>.
Friede80 commented on issue #15134:
URL: https://github.com/apache/datafusion/issues/15134#issuecomment-2715769216

   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;
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Invalid schema for unions in ViewTable [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb closed issue #15134: Invalid schema for unions in ViewTable
URL: https://github.com/apache/datafusion/issues/15134


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org