Skip to content

Fix quadratic result materialization for wide schemas (#5939) - #25

Open
penghuo wants to merge 7 commits into
mainfrom
fix-5939-wide-schema-materialization
Open

penghuo wants to merge 7 commits into
mainfrom
fix-5939-wide-schema-materialization

Conversation

@penghuo

@penghuo penghuo commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Description

OpenSearchExecutionEngine.buildResultSet read every cell of the Calcite JDBC ResultSet by column name. Avatica's getObject(String) resolves each label with a case-insensitive linear scan of the column metadata, so a result with ~14k columns cost O(rows × columns²) to materialize. On the fixture in the issue (14,002 mapped fields, 100 docs), source=idx | head 100 took ~128 s on local 3.7-main — well past the typical 30 s client deadline.

The fix reads each cell by its JDBC ordinal (getObject(int)) and resolves column names once. Only the Calcite PPL path is touched; legacy engine is unchanged.

Related Issues

Resolves opensearch-project#5939

Verification

Local end-to-end run with ./gradlew opensearch-sql:run, Calcite + pushdown enabled, issue's fixture (14,002 fields / 100 docs / 22 populated per doc):

Query Before After
source=idx | head 100 128.46 s 2.26 s
source=idx | head 10 14.67 s 2.02 s
source=idx | fields @timestamp, message | head 100 2.24 s 2.23 s

Pre- and post-fix response schemas and sorted datarows are byte-equal.

Hot-threads sample during the slow pre-fix run (confirmed root cause):

100.3% cpu usage by thread '...[sql-worker]...'
  org.apache.calcite.avatica.AvaticaResultSet.getAccessor(AvaticaResultSet.java:146)
  org.apache.calcite.avatica.AvaticaResultSet.getObject(AvaticaResultSet.java:399)
  org.opensearch.sql.opensearch.executor.OpenSearchExecutionEngine.buildResultSet(OpenSearchExecutionEngine.java:447)

Regression test

OpenSearchExecutionEngineTest#build_result_set_reads_cells_by_ordinal_not_name mocks a 2,000-column ResultSet and asserts the engine never calls getObject(String). Any future revert to name-based lookup fails this test.

Run with:

./gradlew :opensearch:test --tests "*OpenSearchExecutionEngineTest"

JMH microbenchmark

Adds BuildResultSetBenchmark under benchmarks/ comparing the two materialization paths over a real Calcite JDBC ResultSet:

columns materializeByName materializeByOrdinal
100 9.68 ms 4.66 ms
1000 751.9 ms 221.1 ms
5000 19,433 ms 5,371 ms

The by-name path grows ~26× between 1k and 5k columns; the delta between the two paths grows ~26× per 5× column increase, matching the issue's O(columns²) analysis.

Run with:

./gradlew :benchmarks:jmh -Pjmh.includes=BuildResultSetBenchmark

Check List

  • New functionality includes testing.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added. (not user-visible — internal performance fix)
  • Commits are signed per the DCO using --signoff or -s.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…ject#5939)

OpenSearchExecutionEngine.buildResultSet read every cell from the
Calcite JDBC ResultSet by column name. Avatica's getObject(String)
resolves each label with a case-insensitive linear scan of the column
metadata, so a 14k-column result cost O(rows x columns^2) and a 100-row
`head 100` over a wide index exceeded the 30s client deadline.

Read each cell by its JDBC ordinal instead and resolve column names
once. Only the Calcite PPL path changes; legacy engine untouched.

Local verification against `./gradlew opensearch-sql:run` with the
issue's 14,002-column / 100-document fixture, Calcite + pushdown
enabled:

  Query                 Before      After
  head 100 (full)       128.46 s    2.26 s
  head 10  (full)        14.67 s    2.02 s
  fields+head 100         2.24 s    2.23 s

Pre- and post-fix schemas and sorted datarows are byte-equal.

Regression test (`OpenSearchExecutionEngineTest`) mocks a 2k-column
ResultSet and asserts the engine never calls getObject(String).

Adds a JMH microbenchmark under benchmarks/ that compares the two
materialization paths on a Calcite JDBC ResultSet. The by-name path
grows ~26x between 1k and 5k columns while the by-ordinal path grows
~24x; the delta between them grows ~26x per 5x column increase.

Signed-off-by: Peng Huo <penghuo@gmail.com>
@penghuo
penghuo requested a review from dai-chen as a code owner November 9, 2026 18:06
Avatica's getObject(int) wraps array cells as java.sql.Array (ArrayImpl)
via AvaticaSite.get, while the old getObject(String) path returned the
underlying List already unwrapped. processValue had no case for
java.sql.Array, so array-typed columns threw
"unsupported object class org.apache.calcite.avatica.util.ArrayImpl"
after the opensearch-project#5939 fix.

Unwrap java.sql.Array to its backing array/list and recurse through the
existing List branch so both read paths produce the same ExprValue.
Doctest (86 PPL/SQL doc examples) passes locally.

Signed-off-by: Peng Huo <penghuo@gmail.com>
The previous attempt used ResultSet.getObject(int). That path goes through
AvaticaSite.get, which applies SQL-type-driven coercion that getObject(String)
does not: Types.ARRAY -> ArrayImpl (vs raw List), Types.OTHER geometry ->
WKT string (vs Point object), float -> double with precision loss, etc.
CalciteDataTypeIT, CalciteGeoPointFormatsIT, CalciteStatsCommandIT,
CalcitePPLGraphLookupIT and the issues/4173 yaml rest test all regressed.

Reach into AvaticaResultSet.accessorList via reflection (one-time, cached)
and call accessor.getObject() directly. This is semantically identical to
the pre-opensearch-project#5939 getObject(String) path -- same Cursor.Accessor, same getObject()
method -- but skips Avatica's per-cell case-insensitive label scan. If the
field is ever unavailable (non-Avatica ResultSet, Avatica upgrade), fall
back to getObject(name); correctness is preserved, only performance drops.

Also drops the processValue java.sql.Array branch (no longer needed) and
updates the regression test to cover the fallback path.

Local verification:
- :opensearch:test                     green
- :doctest:doctest (86 PPL/SQL docs)   green
- CalciteDataTypeIT, CalciteGeoPointFormatsIT, CalciteStatsCommandIT,
  CalcitePPLGraphLookupIT                 green
- :integ-test:yamlRestTest             green
- opensearch-sql:run with 14,002-col fixture:
    head 100 warm:  128.46 s -> 2.46 s (~52x)
    head 10  warm:   14.67 s -> 1.80 s (~8x)
    projected head: unaffected

Signed-off-by: Peng Huo <penghuo@gmail.com>
- Drop obvious comments ("package-private for testing", "Get the ResultSet
  metadata to know about columns").
- Collapse the pre-array-cache preamble into one line pointing at the hot
  path and the issue number.
- Add javadoc on avaticaAccessorListField and resolveAvaticaAccessors
  explaining why reflection is needed: getObject(int) applies
  AvaticaSite.get coercion (WKT, ArrayImpl, double narrowing) and
  getObject(String) runs the quadratic name scan; reaching accessorList
  directly is the only way to get both the raw value and the ordinal
  lookup speed.

Signed-off-by: Peng Huo <penghuo@gmail.com>
Previously resolveAvaticaAccessors returned null on reflection failure and
buildResultSet silently fell back to getObject(String) — a slow but
correct path. That would let a future Avatica upgrade reintroduce the
opensearch-project#5939 O(cols^2) hot path with no observable signal.

Move the Class.forName / getDeclaredField / setAccessible calls into a
static initializer and raise ExceptionInInitializerError if any step
fails. Non-Avatica ResultSets (e.g. unit-test mocks) still fall back to
getObject(String); only a genuine Avatica layout change breaks the
plugin, forcing the upgrader to re-track the internals before shipping.

Signed-off-by: Peng Huo <penghuo@gmail.com>
Signed-off-by: Peng Huo <penghuo@gmail.com>
Signed-off-by: Peng Huo <penghuo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Calcite PPL result materialization becomes quadratic with wide schemas

1 participant