From 185474c10ff373fb48035fb23eda9a6321bb6e26 Mon Sep 17 00:00:00 2001 From: Radu Berinde Date: Fri, 2 Oct 2020 18:34:39 -0400 Subject: [PATCH] opt: improve composite check for Project FDs Release note: None --- pkg/sql/opt/memo/expr.go | 16 +-------------- pkg/sql/opt/memo/testdata/logprops/groupby | 2 +- pkg/sql/opt/memo/testdata/logprops/project | 20 +++++++++++++++++++ .../opt/memo/testdata/stats_quality/tpch/q01 | 1 + pkg/sql/opt/norm/testdata/rules/groupby | 2 +- pkg/sql/opt/norm/testdata/rules/limit | 3 +++ pkg/sql/opt/norm/testdata/rules/prune_cols | 3 +++ pkg/sql/opt/norm/testdata/rules/select | 1 + pkg/sql/opt/xform/testdata/external/tpch | 1 + .../opt/xform/testdata/external/tpch-no-stats | 2 ++ pkg/sql/opt/xform/testdata/physprops/ordering | 4 ++-- 11 files changed, 36 insertions(+), 19 deletions(-) diff --git a/pkg/sql/opt/memo/expr.go b/pkg/sql/opt/memo/expr.go index 5187e9b89fc3..21e50a0bf7a2 100644 --- a/pkg/sql/opt/memo/expr.go +++ b/pkg/sql/opt/memo/expr.go @@ -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" @@ -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) } } diff --git a/pkg/sql/opt/memo/testdata/logprops/groupby b/pkg/sql/opt/memo/testdata/logprops/groupby index b38f13ddf433..f54b46018e02 100644 --- a/pkg/sql/opt/memo/testdata/logprops/groupby +++ b/pkg/sql/opt/memo/testdata/logprops/groupby @@ -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 diff --git a/pkg/sql/opt/memo/testdata/logprops/project b/pkg/sql/opt/memo/testdata/logprops/project index faa5be5e027e..93313faafdb4 100644 --- a/pkg/sql/opt/memo/testdata/logprops/project +++ b/pkg/sql/opt/memo/testdata/logprops/project @@ -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 diff --git a/pkg/sql/opt/memo/testdata/stats_quality/tpch/q01 b/pkg/sql/opt/memo/testdata/stats_quality/tpch/q01 index 5fa5e299b548..e5b963e3caa1 100644 --- a/pkg/sql/opt/memo/testdata/stats_quality/tpch/q01 +++ b/pkg/sql/opt/memo/testdata/stats_quality/tpch/q01 @@ -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) diff --git a/pkg/sql/opt/norm/testdata/rules/groupby b/pkg/sql/opt/norm/testdata/rules/groupby index f56251059f65..e41496005f98 100644 --- a/pkg/sql/opt/norm/testdata/rules/groupby +++ b/pkg/sql/opt/norm/testdata/rules/groupby @@ -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) diff --git a/pkg/sql/opt/norm/testdata/rules/limit b/pkg/sql/opt/norm/testdata/rules/limit index ce208fb4df89..c5f9f50013ef 100644 --- a/pkg/sql/opt/norm/testdata/rules/limit +++ b/pkg/sql/opt/norm/testdata/rules/limit @@ -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 @@ -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 @@ -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 diff --git a/pkg/sql/opt/norm/testdata/rules/prune_cols b/pkg/sql/opt/norm/testdata/rules/prune_cols index 997e7d7e82d2..98c191375141 100644 --- a/pkg/sql/opt/norm/testdata/rules/prune_cols +++ b/pkg/sql/opt/norm/testdata/rules/prune_cols @@ -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 @@ -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] @@ -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] diff --git a/pkg/sql/opt/norm/testdata/rules/select b/pkg/sql/opt/norm/testdata/rules/select index a54d6bd68b2a..b395cdff36ce 100644 --- a/pkg/sql/opt/norm/testdata/rules/select +++ b/pkg/sql/opt/norm/testdata/rules/select @@ -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) diff --git a/pkg/sql/opt/xform/testdata/external/tpch b/pkg/sql/opt/xform/testdata/external/tpch index ddd39b0f4a18..56186c2f4111 100644 --- a/pkg/sql/opt/xform/testdata/external/tpch +++ b/pkg/sql/opt/xform/testdata/external/tpch @@ -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 diff --git a/pkg/sql/opt/xform/testdata/external/tpch-no-stats b/pkg/sql/opt/xform/testdata/external/tpch-no-stats index e24e1c771c79..878c7a17e54c 100644 --- a/pkg/sql/opt/xform/testdata/external/tpch-no-stats +++ b/pkg/sql/opt/xform/testdata/external/tpch-no-stats @@ -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 diff --git a/pkg/sql/opt/xform/testdata/physprops/ordering b/pkg/sql/opt/xform/testdata/physprops/ordering index 5114e540a527..6dd0149609e7 100644 --- a/pkg/sql/opt/xform/testdata/physprops/ordering +++ b/pkg/sql/opt/xform/testdata/physprops/ordering @@ -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