Class LimitAndSortHelper


  • public final class LimitAndSortHelper
    extends Object
    Utility class that extracts LIMIT and ORDER BY information from Substrait plans to enable pushdown of these operations to the underlying data source.

    This helper parses Plan objects to extract:

    • LIMIT values from FetchRel nodes, falling back to Constraints when no plan is available
    • ORDER BY sort fields from SortRel nodes, mapped to column names via table metadata

    Connectors can use the extracted LimitAndSortHelper.GenericSortField representations to build data-source-specific sort clauses (e.g., MongoDB sort documents, SQL ORDER BY clauses).

    • Method Detail

      • getLimit

        public static Optional<Integer> getLimit​(io.substrait.proto.Plan plan,
                                                 Constraints constraints)
        Determines if a LIMIT can be applied and extracts the limit value.

        When a Substrait Plan is provided, the limit is extracted from its FetchRel node. Otherwise, falls back to the limit defined in Constraints.

        Parameters:
        plan - the Substrait plan to extract the limit from, or null to use constraints
        constraints - the query constraints containing a fallback limit value
        Returns:
        an Optional containing the limit value if one can be applied, or empty otherwise
      • getSortFromPlan

        public static Optional<List<LimitAndSortHelper.GenericSortField>> getSortFromPlan​(io.substrait.proto.Plan plan)
        Extracts sort field information from a Substrait plan for ORDER BY pushdown.

        Parses the SortRel node in the plan to produce a list of LimitAndSortHelper.GenericSortField objects, each containing a column name and sort direction. If the plan is null, has no relations, or contains no sort relation, returns empty.

        Parameters:
        plan - the Substrait plan to extract sort information from, or null
        Returns:
        an Optional containing the list of sort fields, or empty if no sort is present