Skip to content

ORCA: support column-level COLLATE "C" instead of falling back - #2247

Open
yjhjstz wants to merge 3 commits into
apache:mainfrom
yjhjstz:orca_collation
Open

yjhjstz wants to merge 3 commits into
apache:mainfrom
yjhjstz:orca_collation

Conversation

@yjhjstz

@yjhjstz yjhjstz commented Nov 4, 2026

Copy link
Copy Markdown
Member

Fixes #717

What does this PR do?

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Breaking Changes

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:

User-facing changes:

Dependencies:

Checklist

Additional Context

CI Skip Instructions


Propagate column-level C collation through the full ORCA pipeline so
that sort, comparison, and aggregate operations produce correct results
matching the PostgreSQL planner.

Key changes:

1. Query->DXL (CTranslatorUtils::GetTableDescr): pass md_col->Collation()
   when creating CDXLColDescr for table columns - this was the root cause
   of collation being lost at the very start of the pipeline.

2. Expr->DXL (CTranslatorExprToDXL::MakeDXLTableDescr, PdxlnCTAS): pass
   CColumnDescriptor collation to CDXLColDescr.

3. DXL->PlStmt (CMappingColIdVarPlStmt::VarFromDXLNodeScId): when the
   CDXLScalarIdent has no explicit collation (e.g., partial aggregate
   output columns), fall back to the child TargetEntry expression's
   collation. This fixes Finalize Aggregate inheriting the correct
   collation from Partial Aggregate.

4. Aggregate collation (CTranslatorDXLToScalar): set aggcollid from
   inputcollid instead of TypeCollation, so min/max/string_agg use the
   correct column collation.

5. Expression-level COLLATE "C" fallback (walkers.c): detect when
   RelabelType overrides collation (from fold_constants converting
   CollateExpr to RelabelType) and trigger fallback to PostgreSQL
   planner, since ORCA does not yet handle expression-level COLLATE.

The existing collation infrastructure (CMDColumn, CColumnDescriptor,
CColRef, CDXLColRef, CDXLScalarIdent) was already in place but the
entry point in CTranslatorUtils was not passing collation, causing
all downstream stages to see collation=0.

fix  regress
With column-level COLLATE "C" supported, ORCA could return wrong
results in three ways:

1. Contradiction detection and range constraints.  CDefaultComparator
   compares datums by evaluating "const op const", and the constants
   carry the type's default collation.  For a column whose collation
   differs from it, e.g. text COLLATE "C", the derived ranges are
   ordered wrongly: "name > 'Z' AND name < 'a'" was turned into
   One-Time Filter: false, although 'Zz' satisfies it under C.

2. Static partition pruning uses the same comparator, so partitions of a
   COLLATE "C" range partition key could be pruned wrongly.

3. ORCA does not track index collations, so an index whose key collation
   differs from the column's (e.g. CREATE INDEX ON t (c COLLATE "C") on
   a default collation column, or the other way round) was used for
   range predicates and orderings it cannot answer.  This one also
   happens without this branch's changes.

For 1 and 2, add the type's default collation to the type metadata
(IMDType::TypeCollation, serialized as an optional DXL attribute), and
don't derive intervals from range comparisons on a column whose
collation differs from its type's.  Equality based comparisons only
produce points, whose intersections and unions don't depend on the
sort order, so they are still used.  Columns of type name, whose type
collation is C, are not affected.  Range partitioned tables with a
COLLATE "C" text key lose static partition pruning for now; that needs
a collation aware comparator.

For 3, hide indexes whose key collation differs from the indexed
column's from ORCA.
@yjhjstz
yjhjstz marked this pull request as ready for review November 4, 2026 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] ORCA fallbacks for collate "C"

1 participant