[BUG] PPL aggregate pushdown planning is O(fields²) on wide index patterns (pushDownAggregate uses List.contains)

Aperta Adatta ai principianti
#5,990 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java

Direzione di ricerca

Il controllo quadratico si trova in pushDownAggregate in CalciteLogicalIndexScan.java, intorno alla riga 388, dove schema.contains viene eseguito dentro un filtro di stream su osIndex.getAllFieldTypes(). Inizia da lì e confronta con la correzione di pushDownFilter citata in #4825. Sostituisci la List con un HashSet costruito da buildSchema(), quindi conferma che il pushdown dell'aggregazione restituisce ancora gli stessi tipi di campo. È completato quando il piano del pattern ampio stats count() non scala più con il numero di campi e i test esistenti del pushdown dell'aggregazione passano.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug untriaged

What is the bug?
PPL planning time grows quadratically with the number of fields in the index pattern for aggregations whose scan is not narrowed by a projection. On an index pattern with ~280K mapped fields (union across ~2,300 indices), source=logs-* | stats count() spends ~3–4 minutes in planning (_explain alone takes ~228 s), versus ~9 s when the same aggregation follows a where on one field.

The hot spot is CalciteLogicalIndexScan.pushDownAggregate:

List<String> schema = buildSchema();
Map<String, ExprType> fieldTypes =
    this.osIndex.getAllFieldTypes().entrySet().stream()
        .filter(entry -> schema.contains(entry.getKey()))   // List.contains -> O(N × M)
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

schema is a List<String> built from the scan's row type. When the scan still carries every field (e.g. stats count() with no referenced columns), both sides are ~N and the filter is O(N²). With a preceding where @timestamp >= ..., the scan is trimmed to one column before the aggregate rule fires ($102 in the logical plan becomes $0 in the pushed-down filter), so the loop is O(N) and the problem is hidden.

This is the same pattern that #4825 fixed in pushDownFilter; the copy in pushDownAggregate is still present on main.

Hot-threads sample on the coordinating node during a slow _explain:

java.util.ArrayList.indexOfRange(ArrayList.java:299)
java.util.ArrayList.indexOf(ArrayList.java:286)
java.util.ArrayList.contains(ArrayList.java:275)
org.opensearch.sql.opensearch.storage.scan.CalciteLogicalIndexScan.lambda$pushDownAggregate$11(CalciteLogicalIndexScan.java:388)
...
org.opensearch.sql.opensearch.storage.scan.CalciteLogicalIndexScan.pushDownAggregate(CalciteLogicalIndexScan.java:389)
org.opensearch.sql.opensearch.planner.rules.AggregateIndexScanRule.apply(AggregateIndexScanRule.java:100)
org.apache.calcite.plan.volcano.VolcanoPlanner.findBestExp(VolcanoPlanner.java:525)

How can one reproduce the bug?

  1. Create an index pattern whose merged mapping has a very large number of distinct fields (we used ~2,300 indices, ~280K distinct leaf fields; 35 indices × 5,000 disjoint keyword fields is enough to show it).
  2. Run POST /_plugins/_ppl/_explain {"query": "source=<pattern> | stats count()"}.
  3. Compare with source=<pattern> | where @timestamp >= '<recent>' | stats count().

Observed (Calcite enabled):

Query Planning / total time
source=logs-* | stats count() (~77K fields) 35 s total
source=logs-* | stats count() (~280K fields) 181 s total, _explain 228 s
source=logs-gdfieldtest-* | stats count() (35 indices, 175K fields, 7K docs) _explain 91–198 s
source=logs-* | where @timestamp >= ... | stats count() (~280K fields) _explain 9 s, 13–14 s total

What is the expected behavior?
Planning cost should be roughly linear in the number of fields. Using a Set<String> for the membership check (as in #4825) should be sufficient:

Set<String> schema = new HashSet<>(buildSchema());

What is your host/environment?

  • OpenSearch 3.5 (managed service), Calcite engine enabled
  • Also present on main (CalciteLogicalIndexScan.java line 388)

Do you have any additional context?

  • Related: #4825 (same O(N²) pattern in pushDownFilter), #5953 (wide-index planning for always-false filters — does not touch this code path).
  • Requests that hit this without a time filter commonly exceed the 300 s HTTP gateway timeout on managed domains.
Lingua principale
Java
Stelle
176
Fork
237
Merge medio
2g 12h
PR unite (30g)
34

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di opensearch-project/sql

Tutte le issue di opensearch-project/sql

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.