Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions datafusion/physical-expr/src/equivalence/properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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<Constraints> {
// Source column index of each output column, `usize::MAX` if the
// output is not a plain column
Expand Down