diff --git a/datafusion/physical-expr/src/equivalence/properties/mod.rs b/datafusion/physical-expr/src/equivalence/properties/mod.rs index 820743413b326..a47e8e7af24bd 100644 --- a/datafusion/physical-expr/src/equivalence/properties/mod.rs +++ b/datafusion/physical-expr/src/equivalence/properties/mod.rs @@ -1563,9 +1563,26 @@ impl EquivalenceProperties { /// Projects constraints according to the given projection mapping. /// - /// This function takes a projection mapping and extracts column indices of - /// target columns. It then projects the constraints to only include - /// relationships between columns that exist in the projected output. + /// A primary key or unique constraint is propagated only when the projection + /// includes every column that forms the key as a plain column reference. + /// Their indices are remapped to their positions in the output schema. + /// + /// # Example + /// + /// The original scan has columns `[a, b]`, with `a` as the primary key. + /// + /// The required projection is `[b, a]`, so the constraints must be updated. + /// Column `a` remains the primary key, but the constraint's physical + /// representation is updated to match the projected schema: + /// + /// ```text + /// Original: [a, b] constraints=[PrimaryKey([0])] + /// Projected: [b, a] constraints=[PrimaryKey([1])] + /// ``` + /// + /// Projecting `[b, a + 1]` instead drops the constraint because `a` is not + /// projected directly. This function does not infer constraints on computed + /// expressions. /// /// # Parameters /// @@ -1574,8 +1591,8 @@ impl EquivalenceProperties { /// /// # Returns /// - /// Returns an optional `Constraints` object containing only the constraints - /// that are valid for the projected columns (if any exists). + /// Returns `Some` containing the propagated constraints if at least one + /// constraint survives the projection, or `None` if none do. fn projected_constraints(&self, mapping: &ProjectionMapping) -> Option { // Source column index of each output column, `usize::MAX` if the // output is not a plain column