Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(sql-editor): explain an Elasticsearch search by profiling it (#21682)

Danny Xua542bcbf4df5
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index 7c7803c375..5f9628cae4 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -902,7 +902,10 @@ func queryRetry(
if queryContext.Explain {
if format, ok := db.ExplainResultFormat(instance.Metadata.GetEngine(), queryContext.Option.GetExplainFormat()); ok {
for _, result := range results {
- if result.Error == "" {
+ // A driver that stated the plan itself keeps it, with whether it
+ // ran and anything it carried: Elasticsearch answers an explain
+ // with the profile of a search it executed.
+ if result.Error == "" && result.QueryPlan == nil {
result.QueryPlan = &v1pb.QueryResult_QueryPlan{Format: format}
}
}
@@ -1702,12 +1705,17 @@ func (s *SQLService) accessCheckWithGrantedTargets(
// A plan names the tables it reads, so reading one needs bb.sql.explain on
// every database the statement references, not only the connected one.
// A plain EXPLAIN, typed or requested, executes nothing and is done here;
- // EXPLAIN ANALYZE executes its statement and is checked for that below.
+ // an explain that executes is checked for what it executes, below.
if isExplain || perm == permission.SQLExplain || (span.ExplainAnalyze && common.EngineSupportQueryNewACL(instance.Metadata.GetEngine())) {
if err := s.checkExplainAccess(ctx, user, instance, database, span, workspacePolicy.Policy, projectPolicy.Policy); err != nil {
return err
}
- if !span.ExplainAnalyze {
+ // Elasticsearch states a plan only for a search it ran: an explain
+ // request there profiles the search, so it reads what the search
+ // reads and needs that permission too, as EXPLAIN ANALYZE does.
+ explainRuns := span.ExplainAnalyze ||
+ (isExplain && instance.Metadata.GetEngine() == storepb.Engine_ELASTICSEARCH)
+ if !explainRuns {
continue
}
}
diff --git a/backend/api/v1/sql_service_test.go b/backend/api/v1/sql_service_test.go
index 50d74188e7..ccea1a3ae0 100644
--- a/backend/api/v1/sql_service_test.go
+++ b/backend/api/v1/sql_service_test.go
@@ -390,7 +390,11 @@ func TestValidateExplainFormat(t *testing.T) {
// plan is text.
{name: "databricks text", engine: storepb.Engine_DATABRICKS, format: v1pb.QueryOption_TEXT},
{name: "databricks json has no reader", engine: storepb.Engine_DATABRICKS, format: v1pb.QueryOption_JSON, wantErr: true},
- {name: "elasticsearch refuses even an unspecified format", engine: storepb.Engine_ELASTICSEARCH, format: v1pb.QueryOption_EXPLAIN_FORMAT_UNSPECIFIED, wantErr: true},
+ // Elasticsearch states one plan, its profile, which it answers with only
+ // for a search it ran; the driver refuses to profile anything else.
+ {name: "elasticsearch default", engine: storepb.Engine_ELASTICSEARCH, format: v1pb.QueryOption_EXPLAIN_FORMAT_UNSPECIFIED},
+ {name: "elasticsearch json", engine: storepb.Engine_ELASTICSEARCH, format: v1pb.QueryOption_JSON},
+ {name: "elasticsearch has no text plan", engine: storepb.Engine_ELASTICSEARCH, format: v1pb.QueryOption_TEXT, wantErr: true},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
diff --git a/backend/plugin/db/elasticsearch/elasticsearch.go b/backend/plugin/db/elasticsearch/elasticsearch.go
index 7e73256dbd..83ac0a4798 100644
--- a/backend/plugin/db/elasticsearch/elasticsearch.go
+++ b/backend/plugin/db/elasticsearch/elasticsearch.go
@@ -393,7 +393,7 @@ func (d *Driver) Execute(ctx context.Context, statement string, _ db.ExecuteOpti
return 0, nil
}
-func (d *Driver) QueryConn(_ context.Context, _ *sql.Conn, statement string, _ db.QueryContext) ([]*v1pb.QueryResult, error) {
+func (d *Driver) QueryConn(_ context.Context, _ *sql.Conn, statement string, queryContext db.QueryContext) ([]*v1pb.QueryResult, error) {
parseResult, err := parser.ParseElasticsearchREST(statement)
if err != nil {
return nil, err
@@ -407,6 +407,16 @@ func (d *Driver) QueryConn(_ context.Context, _ *sql.Conn, statement string, _ d
var results []*v1pb.QueryResult
for _, request := range parseResult.Requests {
+ if queryContext.Explain {
+ // The plan is what the search did, so the search runs with
+ // profiling on; anything that is not a search is refused rather
+ // than run.
+ profiled, err := profiledSearch(request)
+ if err != nil {
+ return nil, err
+ }
+ request = profiled
+ }
if err := func() error {
startTime := time.Now()
// send HTTP request.
@@ -486,6 +496,20 @@ func (d *Driver) QueryConn(_ context.Context, _ *sql.Conn, statement string, _ d
// Handle based on the actual type
switch v := data.(type) {
case map[string]any:
+ if queryContext.Explain {
+ // The search ran so that it could be profiled; its hits
+ // are not what an explain asked for, and they leave
+ // here rather than reaching a caller that masking no
+ // longer covers.
+ profile, err := profileOnly(v)
+ if err != nil {
+ return err
+ }
+ result.ColumnNames = append(result.ColumnNames, profileField)
+ row.Values = append(row.Values, &v1pb.RowValue{Kind: &v1pb.RowValue_StringValue{StringValue: profile}})
+ result.QueryPlan = explainQueryPlan()
+ break
+ }
// Object response: create one column per key
for key, val := range v {
result.ColumnNames = append(result.ColumnNames, key)
@@ -496,6 +520,9 @@ func (d *Driver) QueryConn(_ context.Context, _ *sql.Conn, statement string, _ d
row.Values = append(row.Values, &v1pb.RowValue{Kind: &v1pb.RowValue_StringValue{StringValue: string(bytes)}})
}
case []any:
+ if queryContext.Explain {
+ return errors.Errorf("the profiled search answered with a list, which is not a search response")
+ }
// Array response (e.g., _cat APIs with format=json): create a single column with array data
result.ColumnNames = append(result.ColumnNames, "result")
bytes, err := json.Marshal(v)
diff --git a/backend/plugin/db/elasticsearch/query_plan.go b/backend/plugin/db/elasticsearch/query_plan.go
new file mode 100644
index 0000000000..a4d07f39dd
--- /dev/null
+++ b/backend/plugin/db/elasticsearch/query_plan.go
@@ -0,0 +1,113 @@
+package elasticsearch
+
+import (
+ "encoding/json"
+ "strings"
+
+ "github.com/pkg/errors"
+
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
+ parser "github.com/bytebase/bytebase/backend/plugin/parser/elasticsearch"
+)
+
+// Elasticsearch states no plan for a search it has not run. Its Profile API is
+// the only tree it publishes — what each Lucene query, collector and
+// aggregation did, and how long each took — and it comes back only with a
+// search that ran. `_validate/query?explain` runs nothing but answers with the
+// rewritten query as a line of text, which is not a plan to draw.
+//
+// So an explain request here runs the search with profiling on, which makes it
+// a read: the API gate requires the statement's own permission as well as
+// bb.sql.explain, as it does for a typed EXPLAIN ANALYZE. A request that is not
+// a search is refused rather than run, so an explain can never carry out a
+// write.
+
+const (
+ searchEndpoint = "_search"
+ profileField = "profile"
+ tookField = "took"
+)
+
+// profiledSearch returns the request with Elasticsearch's profiling turned on,
+// or an error where there is no search to profile.
+func profiledSearch(request *parser.Request) (*parser.Request, error) {
+ if err := searchable(request); err != nil {
+ return nil, err
+ }
+
+ // Every value is kept as the caller wrote it: decoding a number here would
+ // round a long past 2^53, and the search that ran would not be theirs.
+ body := map[string]json.RawMessage{}
+ if len(request.Data) > 0 {
+ // A search carries one JSON document; the newline-delimited endpoints
+ // that carry several are not searches this profiles.
+ if err := json.Unmarshal([]byte(request.Data[0]), &body); err != nil {
+ return nil, errors.Wrapf(err, "the search body is not a JSON object to profile")
+ }
+ }
+ body[profileField] = json.RawMessage("true")
+ profiled, err := json.Marshal(body)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to encode the profiled search")
+ }
+
+ // The request is the caller's; the one that runs is a copy of it.
+ next := *request
+ next.Data = []string{string(profiled)}
+ return &next, nil
+}
+
+// searchable reports whether the request is one Elasticsearch profiles: a
+// search, read with GET or POST. A URI search carrying no body is one too — the
+// profile goes in a body of its own.
+func searchable(request *parser.Request) error {
+ method := strings.ToUpper(strings.TrimSpace(request.Method))
+ if method != "GET" && method != "POST" {
+ return errors.Errorf("%s is not a search to explain: Elasticsearch profiles a search read with GET or POST", request.Method)
+ }
+ path, _, _ := strings.Cut(request.URL, "?")
+ if !strings.EqualFold(strings.TrimRight(path, "/"), searchEndpoint) &&
+ !strings.HasSuffix(strings.ToLower(strings.TrimRight(path, "/")), "/"+searchEndpoint) {
+ return errors.Errorf("%s is not a search to explain: Elasticsearch states a plan for _search alone", request.URL)
+ }
+ if len(request.Data) > 1 {
+ return errors.Errorf("%s is not a search to explain: a search carries one body, and these endpoints carry several", request.URL)
+ }
+ return nil
+}
+
+// profileOnly returns the plan a profiled search answered with: its profile
+// and the time the search took, and nothing else the response carried.
+//
+// The hits are dropped rather than returned. Masking is off for an explain
+// request — every other engine's explain states a plan and no data, so there
+// has never been anything there to mask — and a search profiled through this
+// path would otherwise hand back documents that an ordinary query would have
+// masked. The plan is what an explain asks for; the documents are not.
+//
+// The search's own `took` goes with the profile, since that is what the whole
+// search cost and no node within it states that.
+func profileOnly(response map[string]any) (string, error) {
+ profile, ok := response[profileField]
+ if !ok {
+ return "", errors.Errorf("the search answered with no profile to explain")
+ }
+ plan := map[string]any{profileField: profile}
+ if took, ok := response[tookField]; ok {
+ plan[tookField] = took
+ }
+ encoded, err := json.Marshal(plan)
+ if err != nil {
+ return "", errors.Wrapf(err, "failed to encode the profile")
+ }
+ return string(encoded), nil
+}
+
+// explainQueryPlan marks a result as the plan of a search that ran, which is
+// the only kind Elasticsearch states.
+func explainQueryPlan() *v1pb.QueryResult_QueryPlan {
+ return &v1pb.QueryResult_QueryPlan{
+ Format: v1pb.QueryOption_JSON,
+ Executed: true,
+ }
+}
diff --git a/backend/plugin/db/elasticsearch/query_plan_test.go b/backend/plugin/db/elasticsearch/query_plan_test.go
new file mode 100644
index 0000000000..eeb5207a32
--- /dev/null
+++ b/backend/plugin/db/elasticsearch/query_plan_test.go
@@ -0,0 +1,156 @@
+package elasticsearch
+
+import (
+ "encoding/json"
+ "slices"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
+ parser "github.com/bytebase/bytebase/backend/plugin/parser/elasticsearch"
+)
+
+// TestProfiledSearch pins what an explain request sends: the caller's search
+// with profiling on, and nothing else at all.
+func TestProfiledSearch(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ request parser.Request
+ want map[string]any
+ }{
+ {
+ name: "a search keeps its body",
+ request: parser.Request{Method: "POST", URL: "/customers/_search", Data: []string{`{"query":{"term":{"city":"berlin"}}}`}},
+ want: map[string]any{"query": map[string]any{"term": map[string]any{"city": "berlin"}}, "profile": true},
+ },
+ {
+ name: "a search with no body gets one",
+ request: parser.Request{Method: "GET", URL: "/customers/_search"},
+ want: map[string]any{"profile": true},
+ },
+ {
+ // A URI search asks in the query string; the profile goes in a body.
+ name: "a URI search is a search",
+ request: parser.Request{Method: "GET", URL: "/customers/_search?q=city:berlin&size=1"},
+ want: map[string]any{"profile": true},
+ },
+ {
+ name: "a search of every index",
+ request: parser.Request{Method: "POST", URL: "_search", Data: []string{`{"size":0}`}},
+ want: map[string]any{"size": float64(0), "profile": true},
+ },
+ {
+ // Already asked for: the request says so and means it.
+ name: "a search that asked for the profile already",
+ request: parser.Request{Method: "POST", URL: "/customers/_search", Data: []string{`{"profile":false}`}},
+ want: map[string]any{"profile": true},
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ profiled, err := profiledSearch(&tc.request)
+ require.NoError(t, err)
+ require.Len(t, profiled.Data, 1)
+
+ var body map[string]any
+ require.NoError(t, json.Unmarshal([]byte(profiled.Data[0]), &body))
+ require.Equal(t, tc.want, body)
+ // The method and the URL are the caller's.
+ require.Equal(t, tc.request.Method, profiled.Method)
+ require.Equal(t, tc.request.URL, profiled.URL)
+ })
+ }
+}
+
+// TestProfiledSearchRefusesWhatIsNotASearch locks the line between explaining
+// and doing. Profiling runs the request, so anything that is not a search —
+// which is everything that writes — is refused rather than sent.
+func TestProfiledSearchRefusesWhatIsNotASearch(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ request parser.Request
+ }{
+ {"an index of a document", parser.Request{Method: "PUT", URL: "/customers/_doc/1", Data: []string{`{"city":"berlin"}`}}},
+ {"a delete", parser.Request{Method: "DELETE", URL: "/customers"}},
+ {"a delete by query, which reads like a search", parser.Request{Method: "POST", URL: "/customers/_delete_by_query", Data: []string{`{"query":{"match_all":{}}}`}}},
+ {"an update by query", parser.Request{Method: "POST", URL: "/customers/_update_by_query"}},
+ {"a bulk write", parser.Request{Method: "POST", URL: "/_bulk", Data: []string{`{"index":{}}`, `{"city":"berlin"}`}}},
+ {"a multi search, which carries several bodies", parser.Request{Method: "POST", URL: "/customers/_msearch", Data: []string{`{}`, `{"query":{"match_all":{}}}`}}},
+ {"a search whose body is not an object", parser.Request{Method: "POST", URL: "/customers/_search", Data: []string{`"not a body"`}}},
+ {"a path that merely ends in the word", parser.Request{Method: "POST", URL: "/customers/not_search"}},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ _, err := profiledSearch(&tc.request)
+ require.Error(t, err)
+ })
+ }
+}
+
+// TestProfileOnly pins what an explain answers with: the profile and the time
+// the search took, and nothing else it returned. The hits are dropped here
+// rather than masked, because masking is off for an explain request.
+func TestProfileOnly(t *testing.T) {
+ response := map[string]any{
+ "took": float64(102),
+ "hits": map[string]any{
+ "total": map[string]any{"value": float64(3)},
+ "hits": []any{map[string]any{"_source": map[string]any{"ssn": "111-22-3333"}}},
+ },
+ "aggregations": map[string]any{"by_city": map[string]any{"buckets": []any{}}},
+ "profile": map[string]any{"shards": []any{map[string]any{"id": "[n][customers][0]"}}},
+ }
+ encoded, err := profileOnly(response)
+ require.NoError(t, err)
+
+ var plan map[string]any
+ require.NoError(t, json.Unmarshal([]byte(encoded), &plan))
+ require.Equal(t, []string{"profile", "took"}, sortedKeys(plan))
+ require.Equal(t, float64(102), plan["took"])
+ // Nothing a document could hide in survives.
+ require.NotContains(t, encoded, "ssn")
+ require.NotContains(t, encoded, "111-22-3333")
+ require.NotContains(t, encoded, "aggregations")
+}
+
+// TestExplainQueryPlan pins what the result says about its plan: JSON, and
+// that it ran, which is what the viewer badges measured.
+func TestExplainQueryPlan(t *testing.T) {
+ plan := explainQueryPlan()
+ require.Equal(t, v1pb.QueryOption_JSON, plan.Format)
+ require.True(t, plan.Executed)
+ // The plan is the result's rows here, not something carried beside them:
+ // an explain returns no data to carry it beside.
+ require.Empty(t, plan.Content)
+}
+
+func TestProfileOnlyWithoutAProfile(t *testing.T) {
+ // A response with no profile is not a plan, and saying so beats answering
+ // with the search's own hits.
+ _, err := profileOnly(map[string]any{"took": float64(1), "hits": map[string]any{}})
+ require.Error(t, err)
+}
+
+// TestProfiledSearchKeepsNumbersExact locks the caller's own body. Decoding a
+// number on the way through would round a long past 2^53, and the search that
+// ran would not be the one they wrote.
+func TestProfiledSearchKeepsNumbersExact(t *testing.T) {
+ const big = "9007199254740993"
+ request := parser.Request{
+ Method: "POST",
+ URL: "/events/_search",
+ Data: []string{`{"query":{"term":{"id":` + big + `}},"size":2}`},
+ }
+ profiled, err := profiledSearch(&request)
+ require.NoError(t, err)
+ require.Contains(t, profiled.Data[0], big)
+ require.Contains(t, profiled.Data[0], `"profile":true`)
+}
+
+func sortedKeys(m map[string]any) []string {
+ keys := make([]string, 0, len(m))
+ for key := range m {
+ keys = append(keys, key)
+ }
+ slices.Sort(keys)
+ return keys
+}
diff --git a/backend/plugin/db/explain_format.go b/backend/plugin/db/explain_format.go
index 06ea2e5d5a..9f05a4a8dc 100644
--- a/backend/plugin/db/explain_format.go
+++ b/backend/plugin/db/explain_format.go
@@ -34,7 +34,9 @@ func SupportedExplainFormats(engine storepb.Engine) []v1pb.QueryOption_ExplainFo
return []v1pb.QueryOption_ExplainFormat{v1pb.QueryOption_TEXT, v1pb.QueryOption_JSON, v1pb.QueryOption_XML, v1pb.QueryOption_YAML}
case storepb.Engine_MSSQL:
return []v1pb.QueryOption_ExplainFormat{v1pb.QueryOption_TEXT, v1pb.QueryOption_XML}
- case storepb.Engine_SPANNER, storepb.Engine_MONGODB:
+ // Elasticsearch joins these two in stating one plan format: its profile,
+ // which it answers with only for a search that ran.
+ case storepb.Engine_SPANNER, storepb.Engine_MONGODB, storepb.Engine_ELASTICSEARCH:
return []v1pb.QueryOption_ExplainFormat{v1pb.QueryOption_JSON}
case storepb.Engine_MYSQL, storepb.Engine_MARIADB, storepb.Engine_TIDB,
storepb.Engine_ORACLE, storepb.Engine_CLICKHOUSE, storepb.Engine_TRINO,
@@ -45,8 +47,7 @@ func SupportedExplainFormats(engine storepb.Engine) []v1pb.QueryOption_ExplainFo
// APIs state none, and an engine listed here is refused an explain request
// outright, since its driver would otherwise run the statement as written.
case storepb.Engine_REDIS, storepb.Engine_DYNAMODB,
- storepb.Engine_CASSANDRA, storepb.Engine_COSMOSDB,
- storepb.Engine_ELASTICSEARCH:
+ storepb.Engine_CASSANDRA, storepb.Engine_COSMOSDB:
return nil
default:
return []v1pb.QueryOption_ExplainFormat{v1pb.QueryOption_TEXT}
@@ -68,7 +69,8 @@ func ExplainResultFormat(engine storepb.Engine, format v1pb.QueryOption_ExplainF
if format != v1pb.QueryOption_EXPLAIN_FORMAT_UNSPECIFIED {
return format, true
}
- if engine == storepb.Engine_SPANNER || engine == storepb.Engine_MONGODB {
+ if engine == storepb.Engine_SPANNER || engine == storepb.Engine_MONGODB ||
+ engine == storepb.Engine_ELASTICSEARCH {
return v1pb.QueryOption_JSON, true
}
return v1pb.QueryOption_TEXT, true
diff --git a/backend/plugin/db/explain_format_test.go b/backend/plugin/db/explain_format_test.go
index da31bfde24..ff67626874 100644
--- a/backend/plugin/db/explain_format_test.go
+++ b/backend/plugin/db/explain_format_test.go
@@ -56,6 +56,8 @@ func TestExplainResultFormat(t *testing.T) {
{name: "cockroachdb json", engine: storepb.Engine_COCKROACHDB, format: v1pb.QueryOption_JSON, want: v1pb.QueryOption_JSON, isPlan: true},
{name: "cockroachdb default", engine: storepb.Engine_COCKROACHDB, want: v1pb.QueryOption_TEXT, isPlan: true},
{name: "databricks default", engine: storepb.Engine_DATABRICKS, want: v1pb.QueryOption_TEXT, isPlan: true},
+ {name: "elasticsearch default", engine: storepb.Engine_ELASTICSEARCH, want: v1pb.QueryOption_JSON, isPlan: true},
+ {name: "elasticsearch json", engine: storepb.Engine_ELASTICSEARCH, format: v1pb.QueryOption_JSON, want: v1pb.QueryOption_JSON, isPlan: true},
{name: "snowflake json", engine: storepb.Engine_SNOWFLAKE, format: v1pb.QueryOption_JSON, want: v1pb.QueryOption_JSON, isPlan: true},
{name: "snowflake default", engine: storepb.Engine_SNOWFLAKE, want: v1pb.QueryOption_TEXT, isPlan: true},
{name: "hive json", engine: storepb.Engine_HIVE, format: v1pb.QueryOption_JSON, want: v1pb.QueryOption_JSON, isPlan: true},
@@ -80,10 +82,15 @@ func TestSupportedExplainFormats(t *testing.T) {
storepb.Engine_DYNAMODB,
storepb.Engine_CASSANDRA,
storepb.Engine_COSMOSDB,
- storepb.Engine_ELASTICSEARCH,
} {
require.Emptyf(t, SupportedExplainFormats(engine), "%v", engine)
}
+ // Elasticsearch has one plan and it is its profile, which it states only
+ // for a search it ran: explaining there runs the search, and the driver
+ // refuses to profile anything that is not one.
+ require.Equal(t,
+ []v1pb.QueryOption_ExplainFormat{v1pb.QueryOption_JSON},
+ SupportedExplainFormats(storepb.Engine_ELASTICSEARCH))
// Databricks plans a statement through its own SQL, so it takes the text
// plan every engine without a reader of its own reports.
require.Equal(t,
diff --git a/docs/design/query-plan-visualizer.md b/docs/design/query-plan-visualizer.md
index 8f74b33c56..505c62591b 100644
--- a/docs/design/query-plan-visualizer.md
+++ b/docs/design/query-plan-visualizer.md
@@ -28,7 +28,7 @@ This ordering is a judgment call, not a measurement.
## 2. Background / problem
The editor explained on 12 engines and drew a picture for 3 (PostgreSQL, SQL
-Server, Spanner); it now explains on 18 and draws for 19 (§3.4): every engine
+Server, Spanner); it now explains on 19 and draws for 20 (§3.4): every engine
it explains, and BigQuery besides, whose plan arrives with an ordinary run
rather than with an explain. Reaching the
picture takes five steps: Ctrl/Cmd+E → plan rows in the grid → click a small
@@ -353,6 +353,7 @@ regression.
| **MongoDB** | one `Query()`, on demand, cached | graph, with no estimates at all | `explain("executionStats")`, drawn for its rows |
| **CockroachDB** | one `Query()`, on demand, cached | graph of the physical flow, with no estimates | none in the editor — `EXPLAIN ANALYZE` executes |
| **BigQuery** | carried by the run that returned the rows, no second call | graph of the stage DAG, measured, with no estimates beside it | the plan is the measured one; BigQuery has no `EXPLAIN`, and its explain action is a dry run |
+| **Elasticsearch** | carried by the profiled search the explain ran | graph per shard, measured, timed to the nanosecond | the plan is the measured one: explaining here runs the search |
| **Redshift** | one `Query()`, on demand, cached | graph, drawn from the text plan | none — `EXPLAIN` never analyzes |
| **StarRocks** | one `Query()`, on demand, cached | graph, drawn from the text plan | none — `EXPLAIN ANALYZE` executes |
| **Doris** | one `Query()`, on demand, cached | graph, drawn from the text plan by the StarRocks parser | none — Doris has no `EXPLAIN ANALYZE` |
@@ -416,11 +417,39 @@ detail — whether a filter reached the scan, which partitions it skipped,
whether a join broadcast or shuffled, and where the exchanges are. That is what
a Spark plan is read for.
+**Explain on Elasticsearch means the search, profiled.** It is the one engine
+here where an explain runs what it explains, and that follows from what
+Elasticsearch publishes. `_validate/query?explain` runs nothing, but answers
+with the rewritten Lucene query as a line of text: no tree, no timings, nothing
+to draw. The Profile API answers with the tree — every Lucene query, collector
+and aggregation, each timed — and only for a search that ran.
+
+So the request the driver sends is the caller's search with `"profile": true`
+in its body, and four things follow. It is a read, so it needs the permission
+that search needs as well as `bb.sql.explain`, which is the rule §3.5 already
+set for a typed `EXPLAIN ANALYZE`. Anything that is not a `_search` is refused
+rather than sent, so an explain can never carry out a write — a
+`_delete_by_query` reads like a search and is not one. The result is badged
+measured, because it is.
+
+And the hits do not come back. The search ran so that it could be profiled,
+which is not the same as being asked for; masking is off for an explain request
+— every other engine's explain states a plan and no data, so there has never
+been anything there to mask — and returning documents through this path would
+hand back what an ordinary query would have masked. The plan is the result, as
+it is for every other engine here.
+
+The profile is stated per shard with nothing above them, so the shards hang
+under a node for the search itself: the one node in any of these parsers that
+the engine did not state, carrying the `took` that no shard reports. Under each
+shard the phases keep Elasticsearch's own words and say which phase they are —
+the queries Lucene ran, the collectors that gathered their hits, the
+aggregations, the fetch. Every node's `time_in_nanos` covers its own subtree,
+which is what this model means by actual time, so this is the first engine
+added here whose graph ranks by time rather than shading by rows.
+
The engines that remain state no plan at all: CQL has no `EXPLAIN`, and the
-document and key-value APIs publish none. Elasticsearch is the one with
-something to draw — its Profile API returns a measured tree — but reaching it
-means rewriting a request body rather than planning a statement, which is a
-different shape of request from every explain here.
+document and key-value APIs publish none.
**Hive and Snowflake draw, and neither publishes what this section assumed.**
Hive's `EXPLAIN FORMATTED` answers with one JSON object whose `CBOPlan` is
diff --git a/frontend/src/apps/explain-visualizer/QueryPlanView.test.tsx b/frontend/src/apps/explain-visualizer/QueryPlanView.test.tsx
index 06050c11ed..ad43781273 100644
--- a/frontend/src/apps/explain-visualizer/QueryPlanView.test.tsx
+++ b/frontend/src/apps/explain-visualizer/QueryPlanView.test.tsx
@@ -38,6 +38,7 @@ describe("QueryPlanView", () => {
[Engine.ORACLE]: "oracle",
[Engine.CLICKHOUSE]: "clickhouse",
[Engine.BIGQUERY]: "bigquery",
+ [Engine.ELASTICSEARCH]: "elasticsearch",
[Engine.TRINO]: "trino",
[Engine.OCEANBASE]: "oceanbase",
[Engine.MONGODB]: "mongodb",
diff --git a/frontend/src/apps/explain-visualizer/QueryPlanView.tsx b/frontend/src/apps/explain-visualizer/QueryPlanView.tsx
index 949577f19d..82ff416412 100644
--- a/frontend/src/apps/explain-visualizer/QueryPlanView.tsx
+++ b/frontend/src/apps/explain-visualizer/QueryPlanView.tsx
@@ -21,6 +21,11 @@ import {
DATABRICKS_PLAN_EMPTY_MESSAGE,
DATABRICKS_PLAN_NO_PLAN_MESSAGE,
} from "./databricks-plan";
+import {
+ ELASTICSEARCH_PLAN_EMPTY_MESSAGE,
+ ELASTICSEARCH_PLAN_INVALID_JSON_MESSAGE,
+ ELASTICSEARCH_PLAN_NO_PLAN_MESSAGE,
+} from "./elasticsearch-plan";
import {
HIVE_PLAN_EMPTY_MESSAGE,
HIVE_PLAN_INVALID_JSON_MESSAGE,
@@ -142,6 +147,9 @@ const PARSE_ERROR_KEYS = new Map<string, QueryPlanTranslationKey>([
[SNOWFLAKE_PLAN_EMPTY_MESSAGE, "error.snowflake-empty"],
[SNOWFLAKE_PLAN_INVALID_JSON_MESSAGE, "error.snowflake-invalid"],
[SNOWFLAKE_PLAN_NO_PLAN_MESSAGE, "error.snowflake-no-plan"],
+ [ELASTICSEARCH_PLAN_EMPTY_MESSAGE, "error.elasticsearch-empty"],
+ [ELASTICSEARCH_PLAN_INVALID_JSON_MESSAGE, "error.elasticsearch-invalid"],
+ [ELASTICSEARCH_PLAN_NO_PLAN_MESSAGE, "error.elasticsearch-no-plan"],
[HIVE_PLAN_EMPTY_MESSAGE, "error.hive-empty"],
[HIVE_PLAN_INVALID_JSON_MESSAGE, "error.hive-invalid"],
[HIVE_PLAN_NO_PLAN_MESSAGE, "error.hive-no-plan"],
diff --git a/frontend/src/apps/explain-visualizer/elasticsearch-plan.test.ts b/frontend/src/apps/explain-visualizer/elasticsearch-plan.test.ts
new file mode 100644
index 0000000000..49f9711a0f
--- /dev/null
+++ b/frontend/src/apps/explain-visualizer/elasticsearch-plan.test.ts
@@ -0,0 +1,168 @@
+import { describe, expect, test } from "vitest";
+import {
+ ELASTICSEARCH_PLAN_EMPTY_MESSAGE,
+ ELASTICSEARCH_PLAN_INVALID_JSON_MESSAGE,
+ ELASTICSEARCH_PLAN_NO_PLAN_MESSAGE,
+ parseElasticsearchPlan,
+} from "./elasticsearch-plan";
+import {
+ PLAN_TOO_DEEP_MESSAGE,
+ type PlanNode,
+ type PlanTree,
+} from "./plan-model";
+import boolAggregation from "./test-data/elasticsearch/bool-aggregation.json";
+import multiShard from "./test-data/elasticsearch/multi-shard.json";
+
+/** A profile of one shard whose query nests `depth` deep. */
+const nestedPlan = (depth: number): string => {
+ let query: Record<string, unknown> = {
+ type: "TermQuery",
+ time_in_nanos: 1000,
+ };
+ for (let level = 1; level < depth; level += 1) {
+ query = {
+ type: "BooleanQuery",
+ time_in_nanos: 1000,
+ children: [query],
+ };
+ }
+ return JSON.stringify({
+ profile: { shards: [{ id: "[n][i][0]", searches: [{ query: [query] }] }] },
+ });
+};
+
+const parse = (source: string): PlanTree => {
+ const result = parseElasticsearchPlan(source);
+ if (!result.ok)
+ throw new Error(`expected a parsable plan: ${result.message}`);
+ return result.tree;
+};
+
+const parseFixture = (fixture: unknown): PlanTree =>
+ parse(JSON.stringify(fixture));
+
+const outline = (tree: PlanTree): string[] =>
+ tree.nodes.map(
+ (node) =>
+ `${node.id} ${node.nodeType}${node.relationship ? ` (${node.relationship})` : ""}`
+ );
+
+const propertyValue = (node: PlanNode, label: string): string | undefined =>
+ node.properties.find((property) => property.label === label)?.value;
+
+describe("parseElasticsearchPlan", () => {
+ test("puts each shard's phases under it, saying which phase they are", () => {
+ const tree = parseFixture(boolAggregation);
+ expect(outline(tree)).toEqual([
+ "0 Search",
+ "0.0 Shard",
+ "0.0.0 BooleanQuery (Query)",
+ "0.0.0.0 TermQuery",
+ "0.0.0.1 IndexOrDocValuesQuery",
+ "0.0.1 QueryPhaseCollector (Collector)",
+ "0.0.1.0 SimpleTopScoreDocCollector",
+ // A collector per aggregation, which is how the terms agg gathered.
+ "0.0.1.1 AggregatorCollector: [by_city]",
+ "0.0.2 GlobalOrdinalsStringTermsAggregator (Aggregation)",
+ // The avg nested inside the terms aggregation.
+ "0.0.2.0 AvgAggregator",
+ "0.0.3 fetch (Fetch)",
+ "0.0.3.0 FetchFieldsPhase",
+ "0.0.3.1 FetchSourcePhase",
+ "0.0.3.2 StoredFieldsPhase",
+ ]);
+ });
+
+ test("is measured in milliseconds, with each node's own time apart", () => {
+ const tree = parseFixture(boolAggregation);
+ // Elasticsearch times in nanoseconds and covers the subtree, which is what
+ // the model's actual time means.
+ const query = tree.nodes[2];
+ expect(query.actualTime).toBeCloseTo(16.279942, 5);
+ expect(tree.nodes[3].actualTime).toBeCloseTo(5.147482, 5);
+ // 16.28ms over children totalling 9.88ms leaves the query's own 6.4ms.
+ expect(query.selfTime).toBeCloseTo(6.397715, 5);
+ expect(tree.measured.time).toBe(true);
+ expect(tree.estimates.cost).toBe(false);
+ expect(tree.estimates.rows).toBe(false);
+ });
+
+ test("carries the search's own time on the node that holds the shards", () => {
+ const tree = parseFixture(boolAggregation);
+ expect(tree.root.nodeType).toBe("Search");
+ expect(tree.root.actualTime).toBe(102);
+ expect(propertyValue(tree.root, "Took ms")).toBe("102");
+ expect(propertyValue(tree.root, "Shards")).toBe("1");
+ // It claims no time of its own: the shards run at once, so what they did
+ // not account for cannot be attributed to the search as work it did.
+ expect(tree.root.selfTime).toBeUndefined();
+ });
+
+ test("resolves the index a shard read, which is a table here", () => {
+ const tree = parseFixture(boolAggregation);
+ expect(tree.nodes[1].relation).toEqual({ table: "customers" });
+ expect(tree.nodes[1].subject).toBe(
+ "[xBvstVRMRUOqYLT8Rvlu8w][customers][0]"
+ );
+ expect(propertyValue(tree.nodes[1], "Shard id")).toBe("0");
+ });
+
+ test("keeps the breakdown that says where a node spent its time", () => {
+ const tree = parseFixture(boolAggregation);
+ const query = tree.nodes[2];
+ expect(propertyValue(query, "Next doc")).toBe("66149");
+ expect(propertyValue(query, "Score count")).toBe("16");
+ // The description is the Lucene query, which is the subject of the card.
+ expect(query.subject).toContain("+city:berlin");
+ });
+
+ test("draws every shard of a search that touched several", () => {
+ const tree = parseFixture(multiShard);
+ const shards = tree.nodes.filter((node) => node.nodeType === "Shard");
+ expect(shards).toHaveLength(3);
+ expect(propertyValue(tree.root, "Shards")).toBe("3");
+ // Each names the same index, so the search names it once.
+ expect(tree.root.subject).toBe("events3");
+ expect(
+ new Set(shards.map((shard) => propertyValue(shard, "Shard id")))
+ ).toEqual(new Set(["0", "1", "2"]));
+ });
+
+ test("says so when there is no profile to read", () => {
+ expect(parseElasticsearchPlan("")).toEqual({
+ ok: false,
+ message: ELASTICSEARCH_PLAN_EMPTY_MESSAGE,
+ });
+ expect(parseElasticsearchPlan("not json")).toEqual({
+ ok: false,
+ message: ELASTICSEARCH_PLAN_INVALID_JSON_MESSAGE,
+ });
+ expect(parseElasticsearchPlan("[]")).toEqual({
+ ok: false,
+ message: ELASTICSEARCH_PLAN_INVALID_JSON_MESSAGE,
+ });
+ // A search that profiled nothing, and a response with no profile at all.
+ expect(
+ parseElasticsearchPlan(JSON.stringify({ profile: { shards: [] } }))
+ ).toEqual({ ok: false, message: ELASTICSEARCH_PLAN_NO_PLAN_MESSAGE });
+ expect(parseElasticsearchPlan(JSON.stringify({ took: 3 }))).toEqual({
+ ok: false,
+ message: ELASTICSEARCH_PLAN_NO_PLAN_MESSAGE,
+ });
+ });
+
+ test("reads a bare profile, without the response around it", () => {
+ const tree = parse(
+ JSON.stringify({ shards: [{ id: "[n][i][0]", index: "i" }] })
+ );
+ expect(tree.root.nodeType).toBe("Search");
+ expect(tree.nodes[1].relation).toEqual({ table: "i" });
+ });
+
+ test("refuses a plan deeper than the viewer draws", () => {
+ expect(parseElasticsearchPlan(nestedPlan(600))).toEqual({
+ ok: false,
+ message: PLAN_TOO_DEEP_MESSAGE,
+ });
+ });
+});
diff --git a/frontend/src/apps/explain-visualizer/elasticsearch-plan.ts b/frontend/src/apps/explain-visualizer/elasticsearch-plan.ts
new file mode 100644
index 0000000000..3d205c4b6d
--- /dev/null
+++ b/frontend/src/apps/explain-visualizer/elasticsearch-plan.ts
@@ -0,0 +1,241 @@
+import {
+ addPlanProperty,
+ checkPlanDepth,
+ isJsonRecord,
+ type JsonRecord,
+ type PlanNode,
+ type PlanParseResult,
+ type PlanProperty,
+ parseWithDepthLimit,
+ planLabel,
+ planSelfTime,
+} from "./plan-model";
+
+/**
+ * Maps an Elasticsearch profile onto the engine-neutral plan model.
+ *
+ * Elasticsearch states no plan for a search it has not run: the profile is a
+ * record of one. Every node carries `time_in_nanos` covering itself and its
+ * children, so this is a measured plan the viewer can rank by time — the first
+ * here that is not an estimate.
+ *
+ * A profile is stated per shard, with nothing above them, so the shards hang
+ * under a node for the search itself: the one node here the engine did not
+ * state, carrying the `took` that no shard reports. Under each shard the
+ * phases keep the engine's own words and say which phase they are — the
+ * queries Lucene ran, the collectors that gathered their hits, the
+ * aggregations, and the fetch.
+ */
+
+const NANOS_IN_MILLISECOND = 1e6;
+
+/** Keys the model promotes to typed fields, so the property list drops them. */
+const PROMOTED_KEYS = new Set([
+ "type",
+ "name",
+ "description",
+ "reason",
+ "time_in_nanos",
+ "children",
+ "breakdown",
+]);
+
+/** The phases a shard profiles, as the response names them. */
+const QUERY = "Query";
+const COLLECTOR = "Collector";
+const AGGREGATION = "Aggregation";
+const FETCH = "Fetch";
+
+export const ELASTICSEARCH_PLAN_EMPTY_MESSAGE =
+ "Elasticsearch returned no query plan for this statement.";
+export const ELASTICSEARCH_PLAN_INVALID_JSON_MESSAGE =
+ "The query plan is not valid JSON. An Elasticsearch profile is expected.";
+export const ELASTICSEARCH_PLAN_NO_PLAN_MESSAGE =
+ "The profile contains no shard to start from. Elasticsearch profiles a search it ran.";
+
+function optionalNumber(value: unknown): number | undefined {
+ if (typeof value === "number" && Number.isFinite(value)) return value;
+ if (typeof value === "string" && value.trim()) {
+ const parsed = Number(value);
+ if (Number.isFinite(parsed)) return parsed;
+ }
+ return undefined;
+}
+
+function stringValue(value: unknown): string | undefined {
+ return typeof value === "string" && value.trim() ? value : undefined;
+}
+
+/** Elasticsearch times every node in nanoseconds; the model reads milliseconds. */
+function milliseconds(value: unknown): number | undefined {
+ const nanos = optionalNumber(value);
+ return nanos === undefined ? undefined : nanos / NANOS_IN_MILLISECOND;
+}
+
+function recordsOf(value: unknown): JsonRecord[] {
+ return Array.isArray(value) ? value.filter(isJsonRecord) : [];
+}
+
+function toProperties(raw: JsonRecord): PlanProperty[] {
+ const properties: PlanProperty[] = [];
+ for (const [key, value] of Object.entries(raw)) {
+ if (PROMOTED_KEYS.has(key)) continue;
+ if (typeof value === "string" || typeof value === "number") {
+ addPlanProperty(properties, planLabel(key), String(value));
+ }
+ }
+ // What the node spent its time on, which is what a profile is read for.
+ if (isJsonRecord(raw.breakdown)) {
+ for (const [key, value] of Object.entries(raw.breakdown)) {
+ if (typeof value === "number") {
+ addPlanProperty(properties, planLabel(key), String(value));
+ }
+ }
+ }
+ return properties;
+}
+
+/**
+ * One node of a profile: a Lucene query, a collector, an aggregation or the
+ * fetch. A collector names itself and says why it ran, where the rest state a
+ * type and describe themselves.
+ *
+ * @param relationship - The phase this node belongs to, on the node that
+ * begins it.
+ */
+function toPlanNode(
+ raw: JsonRecord,
+ id: string,
+ depth: number,
+ relationship?: string
+): PlanNode {
+ checkPlanDepth(depth);
+ const children = recordsOf(raw.children).map((child, index) =>
+ toPlanNode(child, `${id}.${index}`, depth + 1)
+ );
+ const actualTime = milliseconds(raw.time_in_nanos);
+ return {
+ id,
+ nodeType: stringValue(raw.type) ?? stringValue(raw.name) ?? "Unknown",
+ subject: stringValue(raw.description) ?? stringValue(raw.reason),
+ relationship,
+ actualTime,
+ selfTime: planSelfTime(actualTime, children),
+ properties: toProperties(raw),
+ warnings: [],
+ children,
+ };
+}
+
+/** The phases of one shard, each headed by the node that begins it. */
+function shardPhases(shard: JsonRecord, id: string, depth: number): PlanNode[] {
+ const phases: PlanNode[] = [];
+ const next = (raw: JsonRecord, relationship: string) =>
+ phases.push(
+ toPlanNode(raw, `${id}.${phases.length}`, depth + 1, relationship)
+ );
+
+ for (const search of recordsOf(shard.searches)) {
+ for (const query of recordsOf(search.query)) next(query, QUERY);
+ for (const collector of recordsOf(search.collector)) {
+ next(collector, COLLECTOR);
+ }
+ }
+ for (const aggregation of recordsOf(shard.aggregations)) {
+ next(aggregation, AGGREGATION);
+ }
+ if (isJsonRecord(shard.fetch)) next(shard.fetch, FETCH);
+ return phases;
+}
+
+function toShardNode(shard: JsonRecord, id: string, depth: number): PlanNode {
+ checkPlanDepth(depth);
+ const children = shardPhases(shard, id, depth);
+ const index = stringValue(shard.index);
+ const properties: PlanProperty[] = [];
+ for (const key of ["node_id", "shard_id", "cluster"]) {
+ const value = shard[key];
+ if (typeof value === "string" || typeof value === "number") {
+ addPlanProperty(properties, planLabel(key), String(value));
+ }
+ }
+ // Rewriting a query precedes the phases and is timed apart from them.
+ for (const search of recordsOf(shard.searches)) {
+ const rewrite = milliseconds(search.rewrite_time);
+ if (rewrite !== undefined) {
+ addPlanProperty(properties, "Rewrite ms", String(rewrite));
+ }
+ }
+ return {
+ id,
+ nodeType: "Shard",
+ subject: stringValue(shard.id) ?? index,
+ // An Elasticsearch index is a table in Bytebase's own metadata.
+ relation: index ? { table: index } : undefined,
+ // A shard states no time of its own; its phases do.
+ selfTime: undefined,
+ actualTime: undefined,
+ properties,
+ warnings: [],
+ children,
+ };
+}
+
+export function parseElasticsearchPlan(source: string): PlanParseResult {
+ const trimmed = source.trim();
+ if (!trimmed) {
+ return { ok: false, message: ELASTICSEARCH_PLAN_EMPTY_MESSAGE };
+ }
+
+ let parsed: unknown;
+ try {
+ parsed = JSON.parse(trimmed);
+ } catch {
+ return { ok: false, message: ELASTICSEARCH_PLAN_INVALID_JSON_MESSAGE };
+ }
+ if (!isJsonRecord(parsed)) {
+ return { ok: false, message: ELASTICSEARCH_PLAN_INVALID_JSON_MESSAGE };
+ }
+
+ const profile = isJsonRecord(parsed.profile) ? parsed.profile : parsed;
+ const shards = recordsOf(profile.shards);
+ if (shards.length === 0) {
+ return { ok: false, message: ELASTICSEARCH_PLAN_NO_PLAN_MESSAGE };
+ }
+
+ const indices = [
+ ...new Set(
+ shards
+ .map((shard) => stringValue(shard.index))
+ .filter((index): index is string => index !== undefined)
+ ),
+ ];
+ const took = optionalNumber(parsed.took);
+
+ return parseWithDepthLimit(() => {
+ const children = shards.map((shard, index) =>
+ toShardNode(shard, `0.${index}`, 1)
+ );
+ const properties: PlanProperty[] = [];
+ addPlanProperty(properties, "Shards", String(shards.length));
+ if (took !== undefined)
+ addPlanProperty(properties, "Took ms", String(took));
+ return {
+ id: "0",
+ // The search itself, which the response states around the shards rather
+ // than among them.
+ nodeType: "Search",
+ subject: indices.length > 0 ? indices.join(", ") : undefined,
+ relation: indices.length === 1 ? { table: indices[0] } : undefined,
+ // What the search took, which is wall time while the shards ran at once.
+ // Subtracting their times from it would not divide it: three shards of
+ // ten milliseconds each inside a search of twelve leave a remainder that
+ // means nothing. So the search claims no time of its own, and what the
+ // shards did not account for is simply unaccounted.
+ actualTime: took,
+ properties,
+ warnings: [],
+ children,
+ };
+ });
+}
diff --git a/frontend/src/apps/explain-visualizer/plan-i18n.tsx b/frontend/src/apps/explain-visualizer/plan-i18n.tsx
index 828497715c..128107152f 100644
--- a/frontend/src/apps/explain-visualizer/plan-i18n.tsx
+++ b/frontend/src/apps/explain-visualizer/plan-i18n.tsx
@@ -44,6 +44,12 @@ const ENGLISH = {
"The query plan is not valid JSON. The diagram EXPLAIN (DISTSQL) encodes is expected.",
"error.cockroachdb-no-plan":
'The query plan JSON contains no "processors" to draw.',
+ "error.elasticsearch-empty":
+ "Elasticsearch returned no query plan for this statement.",
+ "error.elasticsearch-invalid":
+ "The query plan is not valid JSON. An Elasticsearch profile is expected.",
+ "error.elasticsearch-no-plan":
+ "The profile contains no shard to start from. Elasticsearch profiles a search it ran.",
"error.hive-empty": "Hive returned no query plan for this statement.",
"error.hive-invalid":
"The query plan is not valid JSON. Hive's EXPLAIN FORMATTED output is expected.",
diff --git a/frontend/src/apps/explain-visualizer/plan-parsers.ts b/frontend/src/apps/explain-visualizer/plan-parsers.ts
index feeca409b9..16f11531ef 100644
--- a/frontend/src/apps/explain-visualizer/plan-parsers.ts
+++ b/frontend/src/apps/explain-visualizer/plan-parsers.ts
@@ -4,6 +4,7 @@ import { parseBigqueryPlan } from "./bigquery-plan";
import { parseClickhousePlan } from "./clickhouse-plan";
import { parseCockroachdbPlan } from "./cockroachdb-plan";
import { parseDatabricksPlan } from "./databricks-plan";
+import { parseElasticsearchPlan } from "./elasticsearch-plan";
import { parseHivePlan } from "./hive-plan";
import { parseMongodbPlan } from "./mongodb-plan";
import { parseMssqlPlan } from "./mssql-plan";
@@ -55,6 +56,9 @@ const PARSERS: Record<VisualizerEngine, (source: string) => PlanParseResult> = {
// BigQuery's plan is the stages of the job that returned the rows, so it is
// measured rather than estimated.
[Engine.BIGQUERY]: parseBigqueryPlan,
+ // Elasticsearch profiles the search it ran, which is measured too, and timed
+ // to the nanosecond.
+ [Engine.ELASTICSEARCH]: parseElasticsearchPlan,
// The text plans drawn here, because none of these engines publishes
// anything else. StarRocks forked from Doris and their plans still agree on
// the fragments and the tree, so one parser reads both.
diff --git a/frontend/src/apps/explain-visualizer/test-data/README.md b/frontend/src/apps/explain-visualizer/test-data/README.md
index d412fd30f8..e74c462a23 100644
--- a/frontend/src/apps/explain-visualizer/test-data/README.md
+++ b/frontend/src/apps/explain-visualizer/test-data/README.md
@@ -401,3 +401,20 @@ suffixes each column with the expression id of the session that planned it, so
a re-capture renumbers them; the tests match the shape of those values rather
than the numbers.
+## `elasticsearch/`
+
+What a profiled search answers with, from Elasticsearch 8.15.0: the response's
+own `took` beside its `profile`, which is what the driver hands over once it has
+taken the profile out of the rows. Captured against an index of 60 documents and
+a three-shard index of 30, both created for the capture.
+
+| File | Shape it covers |
+| --- | --- |
+| `bool-aggregation.json` | One shard: a boolean query over a term and a range, the collectors that gathered it, a terms aggregation with an average nested inside, and the fetch's three phases |
+| `multi-shard.json` | The same search across three shards, each profiled on its own |
+
+To add one, send a search with `"profile": true` and keep the response's `took`
+and `profile`. Every node is timed in nanoseconds and covers its own
+subtree, so a re-capture renumbers every timing, and the tests that assert them
+against this capture would need updating with it.
+
diff --git a/frontend/src/apps/explain-visualizer/test-data/elasticsearch/bool-aggregation.json b/frontend/src/apps/explain-visualizer/test-data/elasticsearch/bool-aggregation.json
new file mode 100644
index 0000000000..9dd9a10ae0
--- /dev/null
+++ b/frontend/src/apps/explain-visualizer/test-data/elasticsearch/bool-aggregation.json
@@ -0,0 +1,231 @@
+{
+ "took": 102,
+ "profile": {
+ "shards": [
+ {
+ "id": "[xBvstVRMRUOqYLT8Rvlu8w][customers][0]",
+ "node_id": "xBvstVRMRUOqYLT8Rvlu8w",
+ "shard_id": 0,
+ "index": "customers",
+ "cluster": "(local)",
+ "searches": [
+ {
+ "query": [
+ {
+ "type": "BooleanQuery",
+ "description": "+city:berlin #IndexOrDocValuesQuery(indexQuery=spend:[100.0 TO Infinity], dvQuery=spend:[4636737291354636288 TO 9218868437227405312])",
+ "time_in_nanos": 16279942,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 66149,
+ "match": 0,
+ "next_doc_count": 17,
+ "score_count": 16,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 30222,
+ "count_weight_count": 0,
+ "build_scorer_count": 2,
+ "create_weight": 7141283,
+ "shallow_advance": 0,
+ "count_weight": 0,
+ "create_weight_count": 1,
+ "build_scorer": 9042288
+ },
+ "children": [
+ {
+ "type": "TermQuery",
+ "description": "city:berlin",
+ "time_in_nanos": 5147482,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 17081,
+ "match": 0,
+ "next_doc_count": 17,
+ "score_count": 16,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 1186,
+ "advance_count": 1,
+ "score": 19019,
+ "count_weight_count": 0,
+ "build_scorer_count": 3,
+ "create_weight": 3289675,
+ "shallow_advance": 0,
+ "count_weight": 0,
+ "create_weight_count": 1,
+ "build_scorer": 1820521
+ }
+ },
+ {
+ "type": "IndexOrDocValuesQuery",
+ "description": "IndexOrDocValuesQuery(indexQuery=spend:[100.0 TO Infinity], dvQuery=spend:[4636737291354636288 TO 9218868437227405312])",
+ "time_in_nanos": 4734745,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 14657,
+ "advance_count": 17,
+ "score": 0,
+ "count_weight_count": 0,
+ "build_scorer_count": 3,
+ "create_weight": 817003,
+ "shallow_advance": 0,
+ "count_weight": 0,
+ "create_weight_count": 1,
+ "build_scorer": 3903085
+ }
+ }
+ ]
+ }
+ ],
+ "rewrite_time": 661026,
+ "collector": [
+ {
+ "name": "QueryPhaseCollector",
+ "reason": "search_query_phase",
+ "time_in_nanos": 5390569,
+ "children": [
+ {
+ "name": "SimpleTopScoreDocCollector",
+ "reason": "search_top_hits",
+ "time_in_nanos": 515217
+ },
+ {
+ "name": "AggregatorCollector: [by_city]",
+ "reason": "aggregation",
+ "time_in_nanos": 4217933
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "aggregations": [
+ {
+ "type": "GlobalOrdinalsStringTermsAggregator",
+ "description": "by_city",
+ "time_in_nanos": 6681915,
+ "breakdown": {
+ "reduce": 0,
+ "build_aggregation_count": 1,
+ "post_collection": 3956,
+ "initialize_count": 1,
+ "reduce_count": 0,
+ "collect_count": 16,
+ "post_collection_count": 1,
+ "build_leaf_collector": 3332578,
+ "build_aggregation": 2589837,
+ "build_leaf_collector_count": 1,
+ "initialize": 48283,
+ "collect": 707261
+ },
+ "debug": {
+ "segments_with_multi_valued_ords": 0,
+ "collection_strategy": "remap using single bucket ords",
+ "segments_with_single_valued_ords": 1,
+ "total_buckets": 1,
+ "built_buckets": 1,
+ "result_strategy": "terms",
+ "has_filter": false
+ },
+ "children": [
+ {
+ "type": "AvgAggregator",
+ "description": "avg_spend",
+ "time_in_nanos": 3441966,
+ "breakdown": {
+ "reduce": 0,
+ "build_aggregation_count": 1,
+ "post_collection": 877,
+ "initialize_count": 1,
+ "reduce_count": 0,
+ "collect_count": 16,
+ "post_collection_count": 1,
+ "build_leaf_collector": 3072272,
+ "build_aggregation": 47327,
+ "build_leaf_collector_count": 1,
+ "initialize": 2854,
+ "collect": 318636
+ },
+ "debug": {
+ "built_buckets": 1
+ }
+ }
+ ]
+ }
+ ],
+ "fetch": {
+ "type": "fetch",
+ "description": "",
+ "time_in_nanos": 16203549,
+ "breakdown": {
+ "load_stored_fields": 1364975,
+ "load_source": 323452,
+ "load_stored_fields_count": 2,
+ "next_reader_count": 1,
+ "load_source_count": 2,
+ "next_reader": 3619659
+ },
+ "debug": {
+ "stored_fields": ["_id", "_routing", "_source"]
+ },
+ "children": [
+ {
+ "type": "FetchFieldsPhase",
+ "description": "",
+ "time_in_nanos": 1880086,
+ "breakdown": {
+ "process_count": 2,
+ "process": 109995,
+ "next_reader": 1770091,
+ "next_reader_count": 1
+ }
+ },
+ {
+ "type": "FetchSourcePhase",
+ "description": "",
+ "time_in_nanos": 35485,
+ "breakdown": {
+ "process_count": 2,
+ "process": 34721,
+ "next_reader": 764,
+ "next_reader_count": 1
+ },
+ "debug": {
+ "fast_path": 2
+ }
+ },
+ {
+ "type": "StoredFieldsPhase",
+ "description": "",
+ "time_in_nanos": 11442,
+ "breakdown": {
+ "process_count": 2,
+ "process": 9764,
+ "next_reader": 1678,
+ "next_reader_count": 1
+ }
+ }
+ ]
+ }
+ }
+ ]
+ }
+}
diff --git a/frontend/src/apps/explain-visualizer/test-data/elasticsearch/multi-shard.json b/frontend/src/apps/explain-visualizer/test-data/elasticsearch/multi-shard.json
new file mode 100644
index 0000000000..91209db522
--- /dev/null
+++ b/frontend/src/apps/explain-visualizer/test-data/elasticsearch/multi-shard.json
@@ -0,0 +1,745 @@
+{
+ "took": 11,
+ "profile": {
+ "shards": [
+ {
+ "id": "[xBvstVRMRUOqYLT8Rvlu8w][events3][0]",
+ "node_id": "xBvstVRMRUOqYLT8Rvlu8w",
+ "shard_id": 0,
+ "index": "events3",
+ "cluster": "(local)",
+ "searches": [
+ {
+ "query": [
+ {
+ "type": "ConstantScoreQuery",
+ "description": "ConstantScore(*:*)",
+ "time_in_nanos": 18417,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 11363,
+ "shallow_advance": 0,
+ "count_weight": 7054,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ },
+ "children": [
+ {
+ "type": "MatchAllDocsQuery",
+ "description": "*:*",
+ "time_in_nanos": 5800,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 2839,
+ "shallow_advance": 0,
+ "count_weight": 2961,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ }
+ ]
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k0",
+ "time_in_nanos": 217594,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 45308,
+ "shallow_advance": 0,
+ "count_weight": 172286,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k1",
+ "time_in_nanos": 56408,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 4804,
+ "shallow_advance": 0,
+ "count_weight": 51604,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k2",
+ "time_in_nanos": 138809,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 15796,
+ "shallow_advance": 0,
+ "count_weight": 123013,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k3",
+ "time_in_nanos": 102323,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 4437,
+ "shallow_advance": 0,
+ "count_weight": 97886,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ }
+ ],
+ "rewrite_time": 917531,
+ "collector": [
+ {
+ "name": "QueryPhaseCollector",
+ "reason": "search_query_phase",
+ "time_in_nanos": 959104,
+ "children": [
+ {
+ "name": "PartialHitCountCollector",
+ "reason": "search_count",
+ "time_in_nanos": 125220
+ },
+ {
+ "name": "AggregatorCollector: [kinds]",
+ "reason": "aggregation",
+ "time_in_nanos": 826408
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "aggregations": [
+ {
+ "type": "StringTermsAggregatorFromFilters",
+ "description": "kinds",
+ "time_in_nanos": 1900441,
+ "breakdown": {
+ "reduce": 0,
+ "build_aggregation_count": 1,
+ "post_collection": 1047,
+ "initialize_count": 1,
+ "reduce_count": 0,
+ "collect_count": 0,
+ "post_collection_count": 1,
+ "build_leaf_collector": 815005,
+ "build_aggregation": 1082002,
+ "build_leaf_collector_count": 1,
+ "initialize": 2387,
+ "collect": 0
+ },
+ "debug": {
+ "delegate": "FilterByFilterAggregator",
+ "built_buckets": 1,
+ "delegate_debug": {
+ "segments_with_doc_count_field": 0,
+ "segments_with_deleted_docs": 0,
+ "filters": [
+ {
+ "query": "kind:k0",
+ "segments_counted_in_constant_time": 1
+ },
+ {
+ "query": "kind:k1",
+ "segments_counted_in_constant_time": 1
+ },
+ {
+ "query": "kind:k2",
+ "segments_counted_in_constant_time": 1
+ },
+ {
+ "query": "kind:k3",
+ "segments_counted_in_constant_time": 1
+ }
+ ],
+ "segments_counted": 1,
+ "segments_collected": 0
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": "[xBvstVRMRUOqYLT8Rvlu8w][events3][1]",
+ "node_id": "xBvstVRMRUOqYLT8Rvlu8w",
+ "shard_id": 1,
+ "index": "events3",
+ "cluster": "(local)",
+ "searches": [
+ {
+ "query": [
+ {
+ "type": "ConstantScoreQuery",
+ "description": "ConstantScore(*:*)",
+ "time_in_nanos": 12064,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 8714,
+ "shallow_advance": 0,
+ "count_weight": 3350,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ },
+ "children": [
+ {
+ "type": "MatchAllDocsQuery",
+ "description": "*:*",
+ "time_in_nanos": 3763,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 1253,
+ "shallow_advance": 0,
+ "count_weight": 2510,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ }
+ ]
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k0",
+ "time_in_nanos": 270726,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 45422,
+ "shallow_advance": 0,
+ "count_weight": 225304,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k1",
+ "time_in_nanos": 96595,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 5912,
+ "shallow_advance": 0,
+ "count_weight": 90683,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k2",
+ "time_in_nanos": 55760,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 3745,
+ "shallow_advance": 0,
+ "count_weight": 52015,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k3",
+ "time_in_nanos": 50365,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 3403,
+ "shallow_advance": 0,
+ "count_weight": 46962,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ }
+ ],
+ "rewrite_time": 810668,
+ "collector": [
+ {
+ "name": "QueryPhaseCollector",
+ "reason": "search_query_phase",
+ "time_in_nanos": 776643,
+ "children": [
+ {
+ "name": "PartialHitCountCollector",
+ "reason": "search_count",
+ "time_in_nanos": 21490
+ },
+ {
+ "name": "AggregatorCollector: [kinds]",
+ "reason": "aggregation",
+ "time_in_nanos": 750119
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "aggregations": [
+ {
+ "type": "StringTermsAggregatorFromFilters",
+ "description": "kinds",
+ "time_in_nanos": 1859469,
+ "breakdown": {
+ "reduce": 0,
+ "build_aggregation_count": 1,
+ "post_collection": 1391,
+ "initialize_count": 1,
+ "reduce_count": 0,
+ "collect_count": 0,
+ "post_collection_count": 1,
+ "build_leaf_collector": 736676,
+ "build_aggregation": 1116913,
+ "build_leaf_collector_count": 1,
+ "initialize": 4489,
+ "collect": 0
+ },
+ "debug": {
+ "delegate": "FilterByFilterAggregator",
+ "built_buckets": 1,
+ "delegate_debug": {
+ "segments_with_doc_count_field": 0,
+ "segments_with_deleted_docs": 0,
+ "filters": [
+ {
+ "query": "kind:k0",
+ "segments_counted_in_constant_time": 1
+ },
+ {
+ "query": "kind:k1",
+ "segments_counted_in_constant_time": 1
+ },
+ {
+ "query": "kind:k2",
+ "segments_counted_in_constant_time": 1
+ },
+ {
+ "query": "kind:k3",
+ "segments_counted_in_constant_time": 1
+ }
+ ],
+ "segments_counted": 1,
+ "segments_collected": 0
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": "[xBvstVRMRUOqYLT8Rvlu8w][events3][2]",
+ "node_id": "xBvstVRMRUOqYLT8Rvlu8w",
+ "shard_id": 2,
+ "index": "events3",
+ "cluster": "(local)",
+ "searches": [
+ {
+ "query": [
+ {
+ "type": "ConstantScoreQuery",
+ "description": "ConstantScore(*:*)",
+ "time_in_nanos": 19313,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 11465,
+ "shallow_advance": 0,
+ "count_weight": 7848,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ },
+ "children": [
+ {
+ "type": "MatchAllDocsQuery",
+ "description": "*:*",
+ "time_in_nanos": 6204,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 1779,
+ "shallow_advance": 0,
+ "count_weight": 4425,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ }
+ ]
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k0",
+ "time_in_nanos": 185319,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 43532,
+ "shallow_advance": 0,
+ "count_weight": 141787,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k1",
+ "time_in_nanos": 58229,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 4806,
+ "shallow_advance": 0,
+ "count_weight": 53423,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k2",
+ "time_in_nanos": 73500,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 3611,
+ "shallow_advance": 0,
+ "count_weight": 69889,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ },
+ {
+ "type": "TermQuery",
+ "description": "kind:k3",
+ "time_in_nanos": 106085,
+ "breakdown": {
+ "set_min_competitive_score_count": 0,
+ "match_count": 0,
+ "shallow_advance_count": 0,
+ "set_min_competitive_score": 0,
+ "next_doc": 0,
+ "match": 0,
+ "next_doc_count": 0,
+ "score_count": 0,
+ "compute_max_score_count": 0,
+ "compute_max_score": 0,
+ "advance": 0,
+ "advance_count": 0,
+ "score": 0,
+ "count_weight_count": 1,
+ "build_scorer_count": 0,
+ "create_weight": 45405,
+ "shallow_advance": 0,
+ "count_weight": 60680,
+ "create_weight_count": 1,
+ "build_scorer": 0
+ }
+ }
+ ],
+ "rewrite_time": 514834,
+ "collector": [
+ {
+ "name": "QueryPhaseCollector",
+ "reason": "search_query_phase",
+ "time_in_nanos": 997483,
+ "children": [
+ {
+ "name": "PartialHitCountCollector",
+ "reason": "search_count",
+ "time_in_nanos": 235012
+ },
+ {
+ "name": "AggregatorCollector: [kinds]",
+ "reason": "aggregation",
+ "time_in_nanos": 752557
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "aggregations": [
+ {
+ "type": "StringTermsAggregatorFromFilters",
+ "description": "kinds",
+ "time_in_nanos": 1895704,
+ "breakdown": {
+ "reduce": 0,
+ "build_aggregation_count": 1,
+ "post_collection": 2664,
+ "initialize_count": 1,
+ "reduce_count": 0,
+ "collect_count": 0,
+ "post_collection_count": 1,
+ "build_leaf_collector": 731876,
+ "build_aggregation": 1156718,
+ "build_leaf_collector_count": 1,
+ "initialize": 4446,
+ "collect": 0
+ },
+ "debug": {
+ "delegate": "FilterByFilterAggregator",
+ "built_buckets": 1,
+ "delegate_debug": {
+ "segments_with_doc_count_field": 0,
+ "segments_with_deleted_docs": 0,
+ "filters": [
+ {
+ "query": "kind:k0",
+ "segments_counted_in_constant_time": 1
+ },
+ {
+ "query": "kind:k1",
+ "segments_counted_in_constant_time": 1
+ },
+ {
+ "query": "kind:k2",
+ "segments_counted_in_constant_time": 1
+ },
+ {
+ "query": "kind:k3",
+ "segments_counted_in_constant_time": 1
+ }
+ ],
+ "segments_counted": 1,
+ "segments_collected": 0
+ }
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 1e0f6da45c..4af1f9f788 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -3613,6 +3613,9 @@
"cockroachdb-no-plan": "The query plan JSON contains no \"processors\" to draw.",
"databricks-empty": "Databricks returned no query plan for this statement.",
"databricks-no-plan": "The query plan has no physical plan in it. EXPLAIN FORMATTED output is expected.",
+ "elasticsearch-empty": "Elasticsearch returned no query plan for this statement.",
+ "elasticsearch-invalid": "The query plan is not valid JSON. An Elasticsearch profile is expected.",
+ "elasticsearch-no-plan": "The profile contains no shard to start from. Elasticsearch profiles a search it ran.",
"hive-empty": "Hive returned no query plan for this statement.",
"hive-invalid": "The query plan is not valid JSON. Hive's EXPLAIN FORMATTED output is expected.",
"hive-no-plan": "The query plan has no CBO plan to draw. Hive states one only when its cost-based optimizer planned the statement.",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 99e7763a3c..d6595793fe 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -3613,6 +3613,9 @@
"cockroachdb-no-plan": "El JSON del plan de consulta no contiene \"processors\" que dibujar.",
"databricks-empty": "Databricks no devolvió ningún plan de consulta para esta sentencia.",
"databricks-no-plan": "El plan de consulta no contiene un plan físico. Se espera la salida de EXPLAIN FORMATTED.",
+ "elasticsearch-empty": "Elasticsearch no devolvió ningún plan de consulta para esta sentencia.",
+ "elasticsearch-invalid": "El plan de consulta no es JSON válido. Se espera un perfil de Elasticsearch.",
+ "elasticsearch-no-plan": "El perfil no contiene ningún fragmento por el que empezar. Elasticsearch perfila una búsqueda que ejecutó.",
"hive-empty": "Hive no devolvió ningún plan de consulta para esta sentencia.",
"hive-invalid": "El plan de consulta no es JSON válido. Se espera la salida de EXPLAIN FORMATTED de Hive.",
"hive-no-plan": "El plan de consulta no tiene un plan de CBO que dibujar. Hive solo lo indica cuando su optimizador basado en costes planificó la sentencia.",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 0cc9a98d44..55ba0f0af9 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -3613,6 +3613,9 @@
"cockroachdb-no-plan": "クエリプラン JSON に描画できる \"processors\" がありません。",
"databricks-empty": "Databricks はこのステートメントのクエリプランを返しませんでした。",
"databricks-no-plan": "クエリプランに物理プランがありません。EXPLAIN FORMATTED の出力が必要です。",
+ "elasticsearch-empty": "Elasticsearch はこのステートメントのクエリプランを返しませんでした。",
+ "elasticsearch-invalid": "クエリプランが有効な JSON ではありません。Elasticsearch のプロファイルが必要です。",
+ "elasticsearch-no-plan": "プロファイルに開始できるシャードがありません。Elasticsearch は実行した検索のみをプロファイルします。",
"hive-empty": "Hive はこのステートメントのクエリプランを返しませんでした。",
"hive-invalid": "クエリプランが有効な JSON ではありません。Hive の EXPLAIN FORMATTED の出力が必要です。",
"hive-no-plan": "クエリプランに描画できる CBO プランがありません。Hive はコストベースオプティマイザーがステートメントをプランした場合にのみ出力します。",
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index 277d4251d2..e9e47659bd 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -3613,6 +3613,9 @@
"cockroachdb-no-plan": "JSON của kế hoạch truy vấn không chứa \"processors\" nào để vẽ.",
"databricks-empty": "Databricks không trả về kế hoạch truy vấn cho câu lệnh này.",
"databricks-no-plan": "Kế hoạch truy vấn không có kế hoạch vật lý. Cần đầu ra EXPLAIN FORMATTED.",
+ "elasticsearch-empty": "Elasticsearch không trả về kế hoạch truy vấn cho câu lệnh này.",
+ "elasticsearch-invalid": "Kế hoạch truy vấn không phải JSON hợp lệ. Cần hồ sơ phân tích của Elasticsearch.",
+ "elasticsearch-no-plan": "Hồ sơ phân tích không có phân mảnh nào để bắt đầu. Elasticsearch chỉ phân tích tìm kiếm đã chạy.",
"hive-empty": "Hive không trả về kế hoạch truy vấn cho câu lệnh này.",
"hive-invalid": "Kế hoạch truy vấn không phải JSON hợp lệ. Cần đầu ra EXPLAIN FORMATTED của Hive.",
"hive-no-plan": "Kế hoạch truy vấn không có kế hoạch CBO nào để vẽ. Hive chỉ nêu kế hoạch này khi bộ tối ưu hóa dựa trên chi phí lập kế hoạch cho câu lệnh.",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 3738401a09..7d42c68419 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -3613,6 +3613,9 @@
"cockroachdb-no-plan": "查询计划 JSON 不包含可绘制的 \"processors\"。",
"databricks-empty": "Databricks 未返回此语句的查询计划。",
"databricks-no-plan": "查询计划中没有物理计划。需要 EXPLAIN FORMATTED 输出。",
+ "elasticsearch-empty": "Elasticsearch 未返回此语句的查询计划。",
+ "elasticsearch-invalid": "查询计划不是有效的 JSON。需要 Elasticsearch 的性能分析结果。",
+ "elasticsearch-no-plan": "性能分析结果中没有可作为起点的分片。Elasticsearch 只分析已执行的搜索。",
"hive-empty": "Hive 未返回此语句的查询计划。",
"hive-invalid": "查询计划不是有效的 JSON。需要 Hive EXPLAIN FORMATTED 输出。",
"hive-no-plan": "查询计划中没有可绘制的 CBO 计划。只有基于代价的优化器为该语句生成计划时,Hive 才会输出它。",
diff --git a/frontend/src/utils/queryPlanEngines.test.ts b/frontend/src/utils/queryPlanEngines.test.ts
index c7e7f8ff35..ca949563f0 100644
--- a/frontend/src/utils/queryPlanEngines.test.ts
+++ b/frontend/src/utils/queryPlanEngines.test.ts
@@ -27,6 +27,7 @@ describe("isVisualizerEngine", () => {
Engine.SNOWFLAKE,
Engine.HIVE,
Engine.BIGQUERY,
+ Engine.ELASTICSEARCH,
Engine.REDSHIFT,
Engine.STARROCKS,
Engine.DORIS,
@@ -91,6 +92,7 @@ describe("explainQueryOption", () => {
Engine.COCKROACHDB,
Engine.SNOWFLAKE,
Engine.HIVE,
+ Engine.ELASTICSEARCH,
]) {
expect(explainQueryOption(engine).explainFormat).toBe(
QueryOption_ExplainFormat.JSON
@@ -114,7 +116,6 @@ describe("explainQueryOption", () => {
Engine.REDIS,
Engine.DYNAMODB,
Engine.COSMOSDB,
- Engine.ELASTICSEARCH,
]) {
expect(explainQueryOption(engine).explainFormat, Engine[engine]).toBe(
QueryOption_ExplainFormat.EXPLAIN_FORMAT_UNSPECIFIED
diff --git a/frontend/src/utils/queryPlanEngines.ts b/frontend/src/utils/queryPlanEngines.ts
index 3cf889c1e3..83cb5373b9 100644
--- a/frontend/src/utils/queryPlanEngines.ts
+++ b/frontend/src/utils/queryPlanEngines.ts
@@ -29,6 +29,9 @@ export const VISUALIZER_EXPLAIN_FORMATS = {
// BigQuery states no plan for a statement it has not run. What it reports is
// the stages of a job that did, which the result carries beside its rows.
[Engine.BIGQUERY]: "JSON",
+ // Elasticsearch states none either: its profile records a search that ran,
+ // and the result carries it beside the hits.
+ [Engine.ELASTICSEARCH]: "JSON",
// These four publish no plan as a document; their text plan is what there
// is. Databricks prints Spark's FORMATTED plan: a tree of operators and then
// a block of detail for each.
diff --git a/frontend/src/utils/v1/instance.ts b/frontend/src/utils/v1/instance.ts
index cdd919f24d..63920a8cd6 100644
--- a/frontend/src/utils/v1/instance.ts
+++ b/frontend/src/utils/v1/instance.ts
@@ -241,6 +241,7 @@ export const instanceV1AllowsExplain = (
Engine.CLICKHOUSE,
Engine.COCKROACHDB,
Engine.DATABRICKS,
+ Engine.ELASTICSEARCH,
Engine.HIVE,
Engine.MSSQL,
Engine.ORACLE,