cockroachdb / cockroach

CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
https://www.cockroachlabs.com
Other
29.85k stars 3.77k forks source link

opt: EXPLAIN and EXPLAIN ANALYZE can show different required orderings in plan #84577

Open mgartner opened 2 years ago

mgartner commented 2 years ago

To reproduce:

  1. Disable SimplifyRootOrdering by commenting it out here: https://github.com/cockroachdb/cockroach/blob/37ed37650402d538af475dcfb4f44c5b7b14196a/pkg/sql/opt/xform/optimizer.go#L815-L829
  2. Run the following queries:
    
    statement ok
    CREATE TABLE t (
    a INT,
    b INT,
    c INT
    )

query T EXPLAIN SELECT a, b, c, count(*) FROM t GROUP BY b, a, c ORDER BY c, b DESC, b DESC

distribution: local vectorized: true · • group (partial streaming) │ group by: a, b, c │ ordered: +c,-b │ └── • sort │ order: +c,-b │ └── • scan missing stats table: t@t_pkey spans: FULL SCAN

query T EXPLAIN ANALYZE SELECT a, b, c, count(*) FROM t GROUP BY b, a, c ORDER BY c, b DESC, b DESC

planning time: 10µs execution time: 100µs distribution: vectorized: maximum memory usage: network usage: regions: · • group (streaming) │ nodes: │ regions: │ actual row count: 0 │ estimated max memory allocated: 0 B │ estimated max sql temp disk usage: 0 B │ group by: a, b, c │ ordered: +c,-b,-b │ └── • sort │ nodes: │ regions: │ actual row count: 0 │ estimated max memory allocated: 0 B │ estimated max sql temp disk usage: 0 B │ order: +c,-b │ └── • scan nodes: regions: actual row count: 0 KV time: 0µs KV contention time: 0µs KV rows read: 0 KV bytes read: 0 B KV gRPC calls: 0 estimated max memory allocated: 0 B missing stats table: t@t_pkey spans: FULL SCAN



Notice how the group by has an ordering of `+c,-b` in the `EXPLAIN` and `+c,-b,-b` in the `EXPLAIN ANALYZE`. The ordering should be the same in both the `EXPLAIN` and `EXPLAIN ANALYZE`, and they should match the ordering used when running the query without any `EXPLAIN` prefix.

Jira issue: CRDB-17744
github-actions[bot] commented 8 months ago

We have marked this issue as stale because it has been inactive for 18 months. If this issue is still relevant, removing the stale label or adding a comment will keep it active. Otherwise, we'll close it in 10 days to keep the issue queue tidy. Thank you for your contribution to CockroachDB!

mgartner commented 8 months ago

Moving to cold storage since this requires disabling a rule.