@@ -581,6 +581,60 @@ def test_non_simple_projection_skips_metadata_inference(session):
581581 _ = df3 ._plan .attributes
582582
583583
584+ def test_mixed_simple_column_and_literal_alias_still_requires_describe (session ):
585+ """Alias(Literal) is not a simple rename; inference aborts even when the first column is plain."""
586+ df = session .create_dataframe ([[1 , 2 ]], schema = ["a" , "b" ])
587+ _ = df .schema
588+
589+ df2 = df .select ("a" , lit (1 ).alias ("c" ))
590+ assert df2 ._plan ._metadata .attributes is None
591+
592+ with SqlCounter (query_count = 0 , describe_count = 1 ):
593+ _ = df2 ._plan .attributes
594+
595+
596+ def test_simple_column_then_complex_expression_no_partial_metadata (session ):
597+ """First column is inferable but second is not; all-or-nothing — no partial cached attributes."""
598+ df = session .create_dataframe ([[1 , 2 ]], schema = ["a" , "b" ])
599+ _ = df .schema
600+
601+ df2 = df .select ("a" , (col ("b" ) + lit (1 )).alias ("b2" ))
602+ assert df2 ._plan ._metadata .attributes is None
603+
604+ with SqlCounter (query_count = 0 , describe_count = 1 ):
605+ _ = df2 ._plan .attributes
606+
607+
608+ def test_cast_on_column_alias_still_requires_describe (session ):
609+ """Alias(Cast(...)) is not Alias(Attribute); types cannot be copied from the subquery without DESCRIBE."""
610+ df = session .create_dataframe ([[1 , 2 ]], schema = ["a" , "b" ])
611+ _ = df .schema
612+
613+ df2 = df .select (col ("a" ).cast (LongType ()).alias ("a" ))
614+ assert df2 ._plan ._metadata .attributes is None
615+
616+ with SqlCounter (query_count = 0 , describe_count = 1 ):
617+ _ = df2 ._plan .attributes
618+
619+
620+ def test_select_star_after_cached_parent (session ):
621+ """SELECT * after parent schema is cached: infer_metadata can copy child attributes when reduce_describe is on."""
622+ df = session .create_dataframe ([[1 , 2 ]], schema = ["a" , "b" ])
623+ _ = df .schema
624+ parent_attrs = df ._plan ._metadata .attributes
625+ assert parent_attrs is not None
626+
627+ df2 = df .select ("*" )
628+ if session .reduce_describe_query_enabled :
629+ assert df2 ._plan ._metadata .attributes is not None
630+ check_attributes_equality (df2 ._plan ._metadata .attributes , parent_attrs )
631+ else :
632+ assert df2 ._plan ._metadata .attributes is None
633+
634+ # Resolving attributes must match the logical schema (DESCRIBE may run when reduce is off).
635+ check_attributes_equality (df2 ._plan .attributes , parent_attrs )
636+
637+
584638@pytest .mark .skipif (IS_IN_STORED_PROC , reason = "Can't create a session in SP" )
585639def test_reduce_describe_query_enabled_on_session (db_parameters ):
586640 with Session .builder .configs (db_parameters ).create () as new_session :
0 commit comments