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

Đang mở Phù hợp với người mới
#5,990 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java
Lĩnh vực
backend, performance

Hướng nghiên cứu

Kiểm tra bậc hai nằm trong pushDownAggregate ở CalciteLogicalIndexScan.java, khoảng dòng 388, nơi schema.contains chạy bên trong một bộ lọc stream trên osIndex.getAllFieldTypes(). Hãy bắt đầu từ đó và so sánh với bản sửa pushDownFilter được nhắc tới trong #4825. Thay List bằng một HashSet được xây dựng từ buildSchema(), sau đó xác nhận pushdown aggregate vẫn trả về cùng các kiểu trường. Hoàn thành khi kế hoạch cho mẫu rộng stats count() không còn tăng theo số trường và các test pushdown aggregate hiện có đều pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.
Ngôn ngữ chính
Java
Star
176
Fork
237
Merge trung bình
2 ngày 12 giờ
Pull request đã merge (30 ngày)
34

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của opensearch-project/sql

Tất cả issue của opensearch-project/sql

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.