@@ -353,10 +353,7 @@ impl LogicalPlan {
353353 LogicalPlan :: Copy ( CopyTo { output_schema, .. } ) => output_schema,
354354 LogicalPlan :: Ddl ( ddl) => ddl. schema ( ) ,
355355 LogicalPlan :: Unnest ( Unnest { schema, .. } ) => schema,
356- LogicalPlan :: RecursiveQuery ( RecursiveQuery { static_term, .. } ) => {
357- // we take the schema of the static term as the schema of the entire recursive query
358- static_term. schema ( )
359- }
356+ LogicalPlan :: RecursiveQuery ( RecursiveQuery { schema, .. } ) => schema,
360357 }
361358 }
362359
@@ -740,7 +737,14 @@ impl LogicalPlan {
740737 } ;
741738 Ok ( LogicalPlan :: Distinct ( distinct) )
742739 }
743- LogicalPlan :: RecursiveQuery ( _) => Ok ( self ) ,
740+ LogicalPlan :: RecursiveQuery ( RecursiveQuery {
741+ name,
742+ static_term,
743+ recursive_term,
744+ is_distinct,
745+ schema : _,
746+ } ) => RecursiveQuery :: try_new ( name, static_term, recursive_term, is_distinct)
747+ . map ( LogicalPlan :: RecursiveQuery ) ,
744748 LogicalPlan :: Analyze ( _) => Ok ( self ) ,
745749 LogicalPlan :: Explain ( _) => Ok ( self ) ,
746750 LogicalPlan :: TableScan ( _) => Ok ( self ) ,
@@ -1080,12 +1084,13 @@ impl LogicalPlan {
10801084 } ) => {
10811085 self . assert_no_expressions ( expr) ?;
10821086 let ( static_term, recursive_term) = self . only_two_inputs ( inputs) ?;
1083- Ok ( LogicalPlan :: RecursiveQuery ( RecursiveQuery {
1084- name : name. clone ( ) ,
1085- static_term : Arc :: new ( static_term) ,
1086- recursive_term : Arc :: new ( recursive_term) ,
1087- is_distinct : * is_distinct,
1088- } ) )
1087+ RecursiveQuery :: try_new (
1088+ name. clone ( ) ,
1089+ Arc :: new ( static_term) ,
1090+ Arc :: new ( recursive_term) ,
1091+ * is_distinct,
1092+ )
1093+ . map ( LogicalPlan :: RecursiveQuery )
10891094 }
10901095 LogicalPlan :: Analyze ( a) => {
10911096 self . assert_no_expressions ( expr) ?;
@@ -2257,7 +2262,7 @@ impl PartialOrd for EmptyRelation {
22572262/// intermediate table, then empty the intermediate table.
22582263///
22592264/// [Postgres Docs]: https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-RECURSIVE
2260- #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Hash ) ]
2265+ #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
22612266pub struct RecursiveQuery {
22622267 /// Name of the query
22632268 pub name : String ,
@@ -2269,6 +2274,90 @@ pub struct RecursiveQuery {
22692274 /// Should the output of the recursive term be deduplicated (`UNION`) or
22702275 /// not (`UNION ALL`).
22712276 pub is_distinct : bool ,
2277+ /// Schema exposed to parent plans after reconciling the static and recursive terms.
2278+ pub schema : DFSchemaRef ,
2279+ }
2280+
2281+ impl PartialOrd for RecursiveQuery {
2282+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
2283+ match self . name . partial_cmp ( & other. name ) {
2284+ Some ( Ordering :: Equal ) => {
2285+ match self . static_term . partial_cmp ( & other. static_term ) {
2286+ Some ( Ordering :: Equal ) => {
2287+ match self . recursive_term . partial_cmp ( & other. recursive_term ) {
2288+ Some ( Ordering :: Equal ) => {
2289+ self . is_distinct . partial_cmp ( & other. is_distinct )
2290+ }
2291+ cmp => cmp,
2292+ }
2293+ }
2294+ cmp => cmp,
2295+ }
2296+ }
2297+ cmp => cmp,
2298+ }
2299+ // If the query definition compares equal but the derived schema differs,
2300+ // return `None` instead of contradicting `PartialEq` with `Some(Equal)`.
2301+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields
2302+ . filter ( |cmp| * cmp != Ordering :: Equal || self == other)
2303+ }
2304+ }
2305+
2306+ impl RecursiveQuery {
2307+ pub fn try_new (
2308+ name : String ,
2309+ static_term : Arc < LogicalPlan > ,
2310+ recursive_term : Arc < LogicalPlan > ,
2311+ is_distinct : bool ,
2312+ ) -> Result < Self > {
2313+ let schema =
2314+ recursive_query_output_schema ( static_term. schema ( ) , recursive_term. schema ( ) ) ?;
2315+ Ok ( Self {
2316+ name,
2317+ static_term,
2318+ recursive_term,
2319+ is_distinct,
2320+ schema,
2321+ } )
2322+ }
2323+ }
2324+
2325+ /// Compute a recursive query's output schema by considering both its static and
2326+ /// recursive terms.
2327+ ///
2328+ /// Field names, types, and metadata come from the static term. A field is
2329+ /// nullable if either the static or the recursive term produces a nullable
2330+ /// value in that position, matching how `UNION` reconciles branch nullability.
2331+ ///
2332+ /// Functional dependencies are intentionally dropped: the recursive term
2333+ /// appends rows that can duplicate values the static term guarantees unique, so
2334+ /// any FDs carried by the static term may not hold over the combined output.
2335+ fn recursive_query_output_schema (
2336+ static_schema : & DFSchemaRef ,
2337+ recursive_schema : & DFSchemaRef ,
2338+ ) -> Result < DFSchemaRef > {
2339+ if static_schema. fields ( ) . len ( ) != recursive_schema. fields ( ) . len ( ) {
2340+ return Err ( DataFusionError :: Plan ( format ! (
2341+ "Non-recursive term and recursive term must have the same number of columns ({} != {})" ,
2342+ static_schema. fields( ) . len( ) ,
2343+ recursive_schema. fields( ) . len( )
2344+ ) ) ) ;
2345+ }
2346+
2347+ let fields = static_schema
2348+ . iter ( )
2349+ . zip ( recursive_schema. fields ( ) )
2350+ . map ( |( ( qualifier, static_field) , recursive_field) | {
2351+ let nullable = static_field. is_nullable ( ) || recursive_field. is_nullable ( ) ;
2352+ (
2353+ qualifier. cloned ( ) ,
2354+ static_field. as_ref ( ) . clone ( ) . with_nullable ( nullable) . into ( ) ,
2355+ )
2356+ } )
2357+ . collect :: < Vec < _ > > ( ) ;
2358+
2359+ DFSchema :: new_with_metadata ( fields, static_schema. metadata ( ) . clone ( ) )
2360+ . map ( DFSchemaRef :: new)
22722361}
22732362
22742363/// Values expression. See
@@ -4500,6 +4589,74 @@ mod tests {
45004589 . build ( )
45014590 }
45024591
4592+ fn recursive_term_scan ( name : & str , fields : Vec < Field > ) -> Result < Arc < LogicalPlan > > {
4593+ Ok ( Arc :: new (
4594+ table_scan ( Some ( name) , & Schema :: new ( fields) , None ) ?. build ( ) ?,
4595+ ) )
4596+ }
4597+
4598+ #[ test]
4599+ fn recursive_query_widens_nullability_per_column ( ) -> Result < ( ) > {
4600+ // Column `a` is non-nullable in both terms and must stay non-nullable;
4601+ // column `b` is non-nullable in the static term but nullable in the
4602+ // recursive term, so the output must widen it to nullable.
4603+ let static_term = recursive_term_scan (
4604+ "static" ,
4605+ vec ! [
4606+ Field :: new( "a" , DataType :: Int32 , false ) ,
4607+ Field :: new( "b" , DataType :: Int32 , false ) ,
4608+ ] ,
4609+ ) ?;
4610+ let recursive_term = recursive_term_scan (
4611+ "rec" ,
4612+ vec ! [
4613+ Field :: new( "a" , DataType :: Int32 , false ) ,
4614+ Field :: new( "b" , DataType :: Int32 , true ) ,
4615+ ] ,
4616+ ) ?;
4617+
4618+ let query =
4619+ RecursiveQuery :: try_new ( "t" . to_string ( ) , static_term, recursive_term, false ) ?;
4620+
4621+ // Names and types are taken from the static term.
4622+ assert_eq ! ( query. schema. field( 0 ) . name( ) , "a" ) ;
4623+ assert_eq ! ( query. schema. field( 1 ) . name( ) , "b" ) ;
4624+ assert_eq ! ( query. schema. field( 0 ) . data_type( ) , & DataType :: Int32 ) ;
4625+ assert_eq ! ( query. schema. field( 1 ) . data_type( ) , & DataType :: Int32 ) ;
4626+ // Nullability is widened independently per column.
4627+ assert ! ( !query. schema. field( 0 ) . is_nullable( ) ) ;
4628+ assert ! ( query. schema. field( 1 ) . is_nullable( ) ) ;
4629+ // `schema()` returns the widened recursive-query schema.
4630+ assert_eq ! (
4631+ LogicalPlan :: RecursiveQuery ( query. clone( ) ) . schema( ) ,
4632+ & query. schema
4633+ ) ;
4634+ Ok ( ( ) )
4635+ }
4636+
4637+ #[ test]
4638+ fn recursive_query_rejects_column_count_mismatch ( ) -> Result < ( ) > {
4639+ let static_term =
4640+ recursive_term_scan ( "static" , vec ! [ Field :: new( "a" , DataType :: Int32 , false ) ] ) ?;
4641+ let recursive_term = recursive_term_scan (
4642+ "rec" ,
4643+ vec ! [
4644+ Field :: new( "a" , DataType :: Int32 , false ) ,
4645+ Field :: new( "b" , DataType :: Int32 , false ) ,
4646+ ] ,
4647+ ) ?;
4648+
4649+ let err =
4650+ RecursiveQuery :: try_new ( "t" . to_string ( ) , static_term, recursive_term, false )
4651+ . unwrap_err ( ) ;
4652+ assert ! (
4653+ err. strip_backtrace( )
4654+ . contains( "must have the same number of columns" ) ,
4655+ "unexpected error: {err}"
4656+ ) ;
4657+ Ok ( ( ) )
4658+ }
4659+
45034660 #[ test]
45044661 fn test_display_indent ( ) -> Result < ( ) > {
45054662 let plan = display_plan ( ) ?;
0 commit comments