id: tabular_sort
name: Sort
description: >
  Sort table rows by one or more columns. Optionally limit the number
  of returned rows (useful for top-N queries).
version: 1.0.0
category: transform
type: table-to-table
default_implementation: duckdb

inputs:
  - name: table
    type: table
    format: parquet
    description: Input table to sort
    required: true

requires: {}  # structural — operates on any table

outputs:
  - name: result
    type: table
    format: parquet
    description: Sorted table

params:
  by:
    type: array
    description: >
      Columns to sort by. Each entry is a column name, optionally followed
      by ASC or DESC (e.g., ["age DESC", "name ASC"]). Default is ASC.
    required: true
  limit:
    type: integer
    description: Maximum number of rows to return (top-N)
    min: 1

# backends: audited 2026-08-14 (defect 50). A key means a runtime that DISPATCHES this op —
# folia-engine `dispatch_op` (products/sdk/folia-engine/src/lib.rs), a `registerOp`/OP_TABLE
# entry in packages/compute, `_BUILTIN_OP_MAP` in folia/compute.py, or a backend manifest
# (folia/backends/*/backend.yaml).
backends:
  js:
    function: tabular_sort
    dispatch: packages/compute/src/ops/tabular-sql.ts
  python:
    function: tabular.ops.sort
    dispatch: folia/compute.py _BUILTIN_OP_MAP

display_hints:
  table:
    renderer: tanstack-table
