Skip to content

Commit

Permalink
opt: Rework column formatting
Browse files Browse the repository at this point in the history
The current code builds column labels that are qualified with their owner
table name (e.g. a.x). This has resulted in several problems:

  1. Sometimes we want the unqualified column name, and extracting this
     has resulted in the addition of some awkward parsing code.
  2. Constructing the qualified column names adds measurable time to
     simple queries, even though the only time we use it is for debug
     and explain display. It's better to build qualified names only when
     we need them rather than up-front.

The trouble is that switching to store unqualified column labels has a
big effect on code all over the opt packages. This commit tries to minimize
the impact on tests, to make reviews easier. It contains a hack to keep
output as close as it can to the old output. In the next commit, the hack is
removed, which triggers a major update of tests across opt.

As part of the changes required for reworking column labeling, I refactored
the ExprView and Memo formatting code so that it's more consolidated and
consistent. Formatting state is now always passed via ExprFmtCtx, which has
been expanded to include a scratch buffer as well as the Memo (rather than
just Metadata).

Release note: None
  • Loading branch information
andy-kimball committed Aug 15, 2018
1 parent 3122b3d commit ecabf9f
Show file tree
Hide file tree
Showing 45 changed files with 937 additions and 1,038 deletions.
Empty file added pkg/sql/opt/bench/out
Empty file.
5 changes: 1 addition & 4 deletions pkg/sql/opt/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ import (
// This file contains interfaces that are used by the query optimizer to avoid
// including specifics of sqlbase structures in the opt code.

// ColumnName is the type of a column name.
type ColumnName string

// PrimaryIndex selects the primary index of a table when calling the
// Table.Index method. Every table is guaranteed to have a unique primary
// index, even if it meant adding a hidden unique rowid column.
Expand Down Expand Up @@ -128,7 +125,7 @@ type View interface {
// needed by the query optimizer.
type Column interface {
// ColName returns the name of the column.
ColName() ColumnName
ColName() tree.Name

// DatumType returns the data type of the column.
DatumType() types.T
Expand Down
112 changes: 56 additions & 56 deletions pkg/sql/opt/exec/execbuilder/testdata/aggregate
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ render · · (min int, max i
│ render 9 (agg8)[bool] · ·
│ render 10 (agg9)[bytes] · ·
└── group · · (agg0 int, agg1 int, agg2 int, agg3 int, agg4 decimal, agg5 decimal, agg6 decimal, agg7 decimal, agg8 bool, agg9 bytes) ·
│ aggregate 0 min(kv.v) · ·
│ aggregate 1 max(kv.v) · ·
│ aggregate 2 count(kv.v) · ·
│ aggregate 0 min(v) · ·
│ aggregate 1 max(v) · ·
│ aggregate 2 count(v) · ·
│ aggregate 3 sum_int(column8) · ·
│ aggregate 4 avg(kv.v) · ·
│ aggregate 5 sum(kv.v) · ·
│ aggregate 6 stddev(kv.v) · ·
│ aggregate 7 variance(kv.v) · ·
│ aggregate 4 avg(v) · ·
│ aggregate 5 sum(v) · ·
│ aggregate 6 stddev(v) · ·
│ aggregate 7 variance(v) · ·
│ aggregate 8 bool_and(column14) · ·
│ aggregate 9 xor_agg(column16) · ·
│ scalar · · ·
└── render · · (column8 int, column14 bool, column16 bytes, "kv.v" int) ·
└── render · · (column8 int, column14 bool, column16 bytes, v int) ·
│ render 0 (1)[int] · ·
│ render 1 ((v)[int] = (1)[int])[bool] · ·
│ render 2 ((s)[string]::BYTES)[bytes] · ·
Expand Down Expand Up @@ -541,18 +541,18 @@ sort · · (v int, count int) +count
query TTTTT
EXPLAIN (TYPES) SELECT v, count(1) FROM kv GROUP BY v ORDER BY count(1)
----
sort · · (v int, count int) +count
│ order +count · ·
└── group · · (v int, count int) ·
│ aggregate 0 kv.v · ·
│ aggregate 1 count(column5) · ·
│ group by @2 · ·
└── render · · (column5 int, "kv.v" int) ·
│ render 0 (1)[int] · ·
│ render 1 (v)[int] · ·
└── scan · · (v int) ·
· table kv@primary · ·
· spans ALL · ·
sort · · (v int, count int) +count
│ order +count · ·
└── group · · (v int, count int) ·
│ aggregate 0 v · ·
│ aggregate 1 count(column5) · ·
│ group by @2 · ·
└── render · · (column5 int, v int) ·
│ render 0 (1)[int] · ·
│ render 1 (v)[int] · ·
└── scan · · (v int) ·
· table kv@primary · ·
· spans ALL · ·

# TODO(radu): we don't propagate filters yet.
## Check that filters propagate through no-op aggregation.
Expand Down Expand Up @@ -848,18 +848,18 @@ render · ·
query TTTTT
EXPLAIN (TYPES) SELECT 1 a FROM kv GROUP BY v, w::DECIMAL HAVING w::DECIMAL > 1;
----
render · · (a int) ·
│ render 0 (1)[int] · ·
└── distinct · · (column5 decimal, "kv.v" int) weak-key(column5,"kv.v")
│ distinct on column5, kv.v · ·
└── filter · · (column5 decimal, "kv.v" int) ·
│ filter ((column5)[decimal] > (1)[decimal])[bool] · ·
└── render · · (column5 decimal, "kv.v" int) ·
│ render 0 ((w)[int]::DECIMAL)[decimal] · ·
│ render 1 (v)[int] · ·
└── scan · · (v int, w int) ·
· table kv@primary · ·
· spans ALL · ·
render · · (a int) ·
│ render 0 (1)[int] · ·
└── distinct · · (column5 decimal, v int) weak-key(column5,v)
│ distinct on column5, v · ·
└── filter · · (column5 decimal, v int) ·
│ filter ((column5)[decimal] > (1)[decimal])[bool] · ·
└── render · · (column5 decimal, v int) ·
│ render 0 ((w)[int]::DECIMAL)[decimal] · ·
│ render 1 (v)[int] · ·
└── scan · · (v int, w int) ·
· table kv@primary · ·
· spans ALL · ·

statement ok
CREATE TABLE foo(a INT, b CHAR)
Expand All @@ -868,34 +868,34 @@ CREATE TABLE foo(a INT, b CHAR)
query TTTTT
EXPLAIN (VERBOSE) SELECT min(a) AS m FROM foo GROUP BY @1
----
render · · (m) ·
│ render 0 agg0 · ·
└── group · · (column4, agg0) ·
│ aggregate 0 column4 · ·
│ aggregate 1 min(foo.a) · ·
│ group by @1 · ·
└── render · · (column4, "foo.a") ·
│ render 0 a · ·
│ render 1 a · ·
└── scan · · (a) ·
· table foo@primary · ·
· spans ALL · ·
render · · (m) ·
│ render 0 agg0 · ·
└── group · · (column4, agg0) ·
│ aggregate 0 column4 · ·
│ aggregate 1 min(a) · ·
│ group by @1 · ·
└── render · · (column4, a) ·
│ render 0 a · ·
│ render 1 a · ·
└── scan · · (a) ·
· table foo@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT min(a) AS m FROM foo GROUP BY @2
----
render · · (m) ·
│ render 0 agg0 · ·
└── group · · (column4, agg0) ·
│ aggregate 0 column4 · ·
│ aggregate 1 min(foo.a) · ·
│ group by @1 · ·
└── render · · (column4, "foo.a") ·
│ render 0 b · ·
│ render 1 a · ·
└── scan · · (a, b) ·
· table foo@primary · ·
· spans ALL · ·
render · · (m) ·
│ render 0 agg0 · ·
└── group · · (column4, agg0) ·
│ aggregate 0 column4 · ·
│ aggregate 1 min(a) · ·
│ group by @1 · ·
└── render · · (column4, a) ·
│ render 0 b · ·
│ render 1 a · ·
└── scan · · (a, b) ·
· table foo@primary · ·
· spans ALL · ·

query TTTTT
EXPLAIN (VERBOSE) SELECT array_agg(v) FROM (SELECT * FROM kv ORDER BY v)
Expand Down
Loading

0 comments on commit ecabf9f

Please sign in to comment.