feat(core): add TopNRel physical operator#1007
Open
nielspardon wants to merge 1 commit into
Open
Conversation
nielspardon
force-pushed
the
feat/topn-rel-core
branch
from
July 17, 2026 12:45
a1ad89a to
954f199
Compare
Add a TopN POJO (a SingleInputRel) modeling the TopNRel physical relation introduced in the Substrait spec v0.88.0: expression-based offset/count, a tie-handling mode (ROWS_ONLY/WITH_TIES), and one or more sort fields. Map the mode to/from the proto FetchMode enum, wire the relation visitor (RelVisitor, AbstractRelVisitor, RelCopyOnWriteVisitor), add both proto converters (RelProtoConverter and ProtoRelConverter), a SubstraitBuilder DSL helper, and a proto round-trip test. Engine mappings for isthmus (Calcite) and Spark are intentionally out of scope: those integrations map only logical operators today, so how to map this physical operator is deferred. SqlKindFromRel and the example SubstraitStringify get the minimal visit(TopN) needed to keep the direct RelVisitor implementors compiling. Closes substrait-io#806
nielspardon
force-pushed
the
feat/topn-rel-core
branch
from
July 17, 2026 12:57
954f199 to
c328b55
Compare
nielspardon
marked this pull request as ready for review
July 17, 2026 13:02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds core support for the
TopNRelphysical relational operator, introduced to theSubstrait spec in substrait#1009
and released in v0.88.0 (the spec version this repo's submodule pins).
TopNRelis a combined sort + fetch operator (
top_n = 23in theReloneof) — a genuinely newrelation rather than sugar over the existing
Sort+Fetch, since it usesExpression-based offset/count and addsWITH TIESsemantics.Changes
io.substrait.relation.physical.TopN(aSingleInputRel):>= 1sortfields,
Optional<Expression>offset (null → 0) and count (absent → ALL), and a nestedModeenum (ROWS_ONLY/WITH_TIES, defaultROWS_ONLY) that maps to/from the protoFetchModeviatoProto()/fromProto().RelVisitor.visit(TopN), anAbstractRelVisitorfallbackdefault, and
RelCopyOnWriteVisitor.visit(TopN).RelProtoConverter(POJO → proto) andProtoRelConverter(proto → POJO, including the
TOP_Ndispatch case).SubstraitBuilder.topN(...)for ergonomics.TopNRelRoundtripTestcovering rows-only, offset+count,count-absent (ALL),
WITH_TIES, default mode, multiple sort fields, and nesting.Mode normalization
The POJO
Modeenum models only the two modes a producer may set (ROWS_ONLY,WITH_TIES). Onproto → POJO conversion, an unset/
FETCH_MODE_UNSPECIFIEDmode is normalized toROWS_ONLY,which the spec defines as its default — so downstream code never encounters an unspecified mode.
The consequence is that a proto → POJO → proto pass over a plan with an unset mode is not
byte-identical (mode
0→1) but is semantically equivalent — the kind of spec-irrelevantdifference tracked by #994 (semantics-aware plan comparison).
Scope
Engine mappings for isthmus (Calcite) and Spark are intentionally out of scope:
those integrations map only logical operators today, and neither engine has a native
single top-N node or
WITH TIESsemantics, so how to map this physical operator there is aseparate decision left for a follow-up. To keep the two direct
RelVisitorimplementorsoutside
:corecompiling,SqlKindFromRelreturnsSELECTforTopNand the exampleSubstraitStringifygains a stringify branch — no behavioral engine mapping is added.Testing
./gradlew clean buildpasses::core(PMD, javadoc, spotless, tests incl. the newround-trip test),
:isthmus,:examples:substrait-spark, and all three Spark variants(
spark-3.4_2.12,spark-3.5_2.12,spark-4.0_2.13).Closes #806
🤖 Generated with AI