Skip to content

[CALCITE-7953] RelBuilder.fields(Iterable<String>) searches the input once per name, so resolving many fields by name is quadratic - #5434

Merged
mihaibudiu merged 1 commit into
apache:mainfrom
RyanL1997:CALCITE-7953
Nov 23, 2026
Merged

mihaibudiu merged 1 commit into
apache:mainfrom
RyanL1997:CALCITE-7953

Conversation

@RyanL1997

@RyanL1997 RyanL1997 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7953

Changes Proposed

RelBuilder.fields(Iterable<String>) called field(String) once per name, and each call searches the input's fields, so resolving n names on an n-field input took O(n²) time.

As discussed in the Jira case, when the input has its row type's field names, fields now looks the names up in the row type; a RelRecordType with many fields keeps a map from name to field. This is the case after a scan, filter, sort, project or aggregate, and after a join whose inputs have no field names in common. Otherwise, for example after a join with repeated names, and for 2 or fewer names, it calls field(String) as before. Results and errors are unchanged.

Tests in RelBuilderTest:

  • testFieldsByNameOnWideInput looks up all fields of a join of two 50,000-field inputs and fails if that takes more than 2 seconds. It takes about 20 seconds before the fix and 50 ms after.
  • testFieldsByNameAfterJoin, testFieldsByNameWithDynamicStar and testFieldsByNameWithExtraField check that fields returns the same fields and errors as field(String) when the input's names repeat, and when RelDataType.getField returns a field for a name the row type does not have.

@RyanL1997
RyanL1997 marked this pull request as ready for review November 20, 2026 23:52
@RyanL1997

Copy link
Copy Markdown
Contributor Author

Pushed a small follow-up: the check now compares field names rather than whole fields, so a join of inputs with distinct names also uses the row type (20,000 fields: 1.3 s -> 10 ms). The wide-input test now uses such a join. Comparing names costs a bit more, so skipping the check for 1 or 2 names now saves about 13 µs rather than 6.5 µs on a 1,000-field input.

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Nov 23, 2026
… once per name, so resolving many fields by name is quadratic
@sonarqubecloud

Copy link
Copy Markdown

@mihaibudiu
mihaibudiu merged commit 602e9dd into apache:main Nov 23, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants