Skip to content

Commit

Permalink
opt: improve composite check for Project FDs
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
RaduBerinde committed Oct 2, 2020
1 parent c5c7bf5 commit 185474c
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 19 deletions.
16 changes: 1 addition & 15 deletions pkg/sql/opt/memo/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"sort"
"strings"

"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props"
Expand Down Expand Up @@ -755,20 +754,7 @@ func (prj *ProjectExpr) initUnexportedFields(mem *Memo) {
// This does not necessarily hold for "composite" types like decimals or
// collated strings. For example if d is a decimal, d::TEXT can have
// different values for equal values of d, like 1 and 1.0.
//
// We only add the FD if composite types are not involved.
//
// TODO(radu): add an allowlist of expressions/operators that are ok, like
// arithmetic.
composite := false
for i, ok := from.Next(0); ok; i, ok = from.Next(i + 1) {
typ := mem.Metadata().ColumnMeta(i).Type
if colinfo.HasCompositeKeyEncoding(typ) {
composite = true
break
}
}
if !composite {
if !CanBeCompositeSensitive(mem.Metadata(), item.Element) {
prj.internalFuncDeps.AddSynthesizedCol(from, item.Col)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/memo/testdata/logprops/groupby
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ project
│ ├── project
│ │ ├── columns: column7:7(bool!null) column9:9(string!null) x:1(int!null) y:2(int) z:3(float!null) s:4(string!null)
│ │ ├── key: (1)
│ │ ├── fd: ()-->(9), (1)-->(2-4,7), (3,4)-->(1,2)
│ │ ├── fd: ()-->(9), (1)-->(2-4,7), (3,4)-->(1,2,7), (3)-->(7)
│ │ ├── prune: (1-4,7,9)
│ │ ├── interesting orderings: (+1) (-4,+3,+1)
│ │ ├── select
Expand Down
20 changes: 20 additions & 0 deletions pkg/sql/opt/memo/testdata/logprops/project
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,26 @@ project
└── cast: STRING [as=d:6, type=string, outer=(4), immutable]
└── variable: xysd.d:4 [type=decimal]

# We have the FD d --> d+1.
build
SELECT d, d+1 FROM xysd
----
project
├── columns: d:4(decimal!null) "?column?":6(decimal!null)
├── immutable
├── fd: (4)-->(6)
├── prune: (4,6)
├── scan xysd
│ ├── columns: x:1(int!null) y:2(int) s:3(string) d:4(decimal!null) crdb_internal_mvcc_timestamp:5(decimal)
│ ├── key: (1)
│ ├── fd: (1)-->(2-5), (3,4)~~>(1,2,5)
│ ├── prune: (1-5)
│ └── interesting orderings: (+1) (-3,+4,+1)
└── projections
└── plus [as="?column?":6, type=decimal, outer=(4), immutable]
├── variable: d:4 [type=decimal]
└── const: 1 [type=decimal]

# We have the equality relation between the synthesized column and the column
# it refers to.
norm
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/memo/testdata/stats_quality/tpch/q01
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ sort
│ ├── columns: column20:20(float!null) column22:22(float!null) l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_returnflag:9(char!null) l_linestatus:10(char!null)
│ ├── immutable
│ ├── stats: [rows=5909394.86, distinct(5)=50, null(5)=0, distinct(6)=925955, null(6)=0, distinct(7)=11, null(7)=0, distinct(9)=3, null(9)=0, distinct(10)=2, null(10)=0, distinct(20)=5909394.86, null(20)=0, distinct(22)=5909394.86, null(22)=0, distinct(9,10)=6, null(9,10)=0]
│ ├── fd: (6,7)-->(20)
│ ├── select
│ │ ├── save-table-name: q1_select_4
│ │ ├── columns: l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_tax:8(float!null) l_returnflag:9(char!null) l_linestatus:10(char!null) l_shipdate:11(date!null)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/norm/testdata/rules/groupby
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ project
├── project
│ ├── columns: column7:7 column9:9!null k:1!null i:2!null f:3
│ ├── key: (1)
│ ├── fd: (1)-->(2,3,7), (2,3)~~>(1), (2)-->(9)
│ ├── fd: (1)-->(2,3), (2,3)~~>(1), (3)-->(7), (2)-->(9)
│ ├── scan a
│ │ ├── columns: k:1!null i:2!null f:3
│ │ ├── key: (1)
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/opt/norm/testdata/rules/limit
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ project
├── columns: f:3 r:7
├── cardinality: [0 - 5]
├── immutable
├── fd: (3)-->(7)
├── ordering: +3
├── limit
│ ├── columns: i:2 f:3
Expand Down Expand Up @@ -375,6 +376,7 @@ SELECT f, f+1.1 AS r FROM (SELECT f, i FROM a GROUP BY f, i) a ORDER BY f OFFSET
project
├── columns: f:3 r:7
├── immutable
├── fd: (3)-->(7)
├── ordering: +3
├── offset
│ ├── columns: i:2 f:3
Expand Down Expand Up @@ -434,6 +436,7 @@ project
├── columns: f:3 r:7
├── cardinality: [0 - 10]
├── immutable
├── fd: (3)-->(7)
├── ordering: +3
├── offset
│ ├── columns: i:2 f:3
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/opt/norm/testdata/rules/prune_cols
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ SELECT f, f+1.1 AS r FROM (SELECT f, k FROM a GROUP BY f, k HAVING sum(k)=100) a
project
├── columns: f:3 r:7
├── immutable
├── fd: (3)-->(7)
├── select
│ ├── columns: k:1!null f:3 sum:6!null
│ ├── immutable
Expand Down Expand Up @@ -436,6 +437,7 @@ project
├── columns: f:3 r:6
├── cardinality: [0 - 5]
├── immutable
├── fd: (3)-->(6)
├── limit
│ ├── columns: f:3 s:4
│ ├── cardinality: [0 - 5]
Expand Down Expand Up @@ -678,6 +680,7 @@ project
├── columns: f:3 r:6
├── cardinality: [0 - 5]
├── immutable
├── fd: (3)-->(6)
├── offset
│ ├── columns: f:3 s:4
│ ├── cardinality: [0 - 5]
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/norm/testdata/rules/select
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ SELECT f, f+1.1 AS r FROM (SELECT f, i FROM a GROUP BY f, i HAVING sum(f)=10.0)
project
├── columns: f:3 r:8
├── immutable
├── fd: (3)-->(8)
├── select
│ ├── columns: i:2 f:3 sum:7!null
│ ├── key: (2,3)
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/xform/testdata/external/tpch
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ sort
├── project
│ ├── columns: column20:20!null column22:22!null l_quantity:5!null l_extendedprice:6!null l_discount:7!null l_returnflag:9!null l_linestatus:10!null
│ ├── immutable
│ ├── fd: (6,7)-->(20)
│ ├── select
│ │ ├── columns: l_quantity:5!null l_extendedprice:6!null l_discount:7!null l_tax:8!null l_returnflag:9!null l_linestatus:10!null l_shipdate:11!null
│ │ ├── scan lineitem
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/opt/xform/testdata/external/tpch-no-stats
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ group-by
├── sort
│ ├── columns: l_quantity:5!null l_extendedprice:6!null l_discount:7!null l_returnflag:9!null l_linestatus:10!null column20:20!null column22:22!null
│ ├── immutable
│ ├── fd: (6,7)-->(20)
│ ├── ordering: +9,+10
│ └── project
│ ├── columns: column20:20!null column22:22!null l_quantity:5!null l_extendedprice:6!null l_discount:7!null l_returnflag:9!null l_linestatus:10!null
│ ├── immutable
│ ├── fd: (6,7)-->(20)
│ ├── select
│ │ ├── columns: l_quantity:5!null l_extendedprice:6!null l_discount:7!null l_tax:8!null l_returnflag:9!null l_linestatus:10!null l_shipdate:11!null
│ │ ├── scan lineitem
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/xform/testdata/physprops/ordering
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ sort (segmented)
├── columns: x:1!null computed:6!null y:2!null
├── immutable
├── key: (1,2)
├── fd: (1,2)-->(6)
├── fd: (2)-->(6)
├── ordering: +1,+6
└── project
├── columns: computed:6!null x:1!null y:2!null
├── immutable
├── key: (1,2)
├── fd: (1,2)-->(6)
├── fd: (2)-->(6)
├── ordering: +1
├── scan a
│ ├── columns: x:1!null y:2!null
Expand Down

0 comments on commit 185474c

Please sign in to comment.