You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[FEATURE] Calcite Engine Framework: pushdown sort with non-reference expression #3912
Sort script pushdown needs to match the plan pattern like: Sort-Project-Scan, where non-trivial sort expression is put to a projected column in Project operator and Sort just needs to reference projected expression.
Calcite Planner doesn't propagate monotonicity of RexCalls very well. It also has limitation of dropping NullDirection if the RexCall can inherit monotonicity based on input reference. There are a bunch of rules recalculating non-trivial expression's monotonicity based on input. So it's hard to maintain monotonicity on non-trivial expression level. It requires more workarounds to achieve it.
Workarounds needed:
Wrap sorted expression with additional monotonic aware UDF to cheat optimizer
Extend RelMdCollation to support RexCall level collation propagation (to avoid dropping NullDirection)
Maintain virtual metadata column in Scan to avoid some rules failing to map to column
Instead of pushing down pure sort script to scan, a proper solution is to optimize it as pushing down project expression script to scan. Then the problem would be similar to pushing down sort for reference expression. It may have duplicated script execution for both projected column and sort script but it depends on whether upper project wants to pruning column.
Activity
songkant-aws commentedon Jul 23, 2025
Looking this issue
songkant-aws commentedon Aug 6, 2025
Sort script pushdown needs to match the plan pattern like: Sort-Project-Scan, where non-trivial sort expression is put to a projected column in Project operator and Sort just needs to reference projected expression.
Calcite Planner doesn't propagate monotonicity of RexCalls very well. It also has limitation of dropping NullDirection if the RexCall can inherit monotonicity based on input reference. There are a bunch of rules recalculating non-trivial expression's monotonicity based on input. So it's hard to maintain monotonicity on non-trivial expression level. It requires more workarounds to achieve it.
Workarounds needed:
Instead of pushing down pure sort script to scan, a proper solution is to optimize it as pushing down project expression script to scan. Then the problem would be similar to pushing down sort for reference expression. It may have duplicated script execution for both projected column and sort script but it depends on whether upper project wants to pruning column.