From 0e900238c6dec2c33cc8c7f76ae934618c82f277 Mon Sep 17 00:00:00 2001 From: Marcus Gartner Date: Mon, 21 Feb 2022 15:54:23 -0500 Subject: [PATCH] opt: add use-multi-col-stats test flag A new optimizer test flag, `use-multi-col-stats`, has been added to allow disabling multi-column stats for a given test. This can be helpful when debugging issues with multi-column stats. Release note: None --- pkg/sql/opt/testutils/opttester/opt_tester.go | 18 +++++++- .../opttester/testdata/use-multi-col-stats | 44 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 pkg/sql/opt/testutils/opttester/testdata/use-multi-col-stats diff --git a/pkg/sql/opt/testutils/opttester/opt_tester.go b/pkg/sql/opt/testutils/opttester/opt_tester.go index 9d14881af598..4175e9a966c5 100644 --- a/pkg/sql/opt/testutils/opttester/opt_tester.go +++ b/pkg/sql/opt/testutils/opttester/opt_tester.go @@ -245,6 +245,10 @@ type Flags struct { // QueryArgs are values for placeholders, used for assign-placeholders-*. QueryArgs []string + + // UseMultiColStats is the value for SessionData.OptimizerUseMultiColStats. + // It defaults to true in New. + UseMultiColStats bool } // New constructs a new instance of the OptTester for the given SQL statement. @@ -252,7 +256,7 @@ type Flags struct { func New(catalog cat.Catalog, sql string) *OptTester { ctx := context.Background() ot := &OptTester{ - Flags: Flags{JoinLimit: opt.DefaultJoinOrderLimit}, + Flags: Flags{JoinLimit: opt.DefaultJoinOrderLimit, UseMultiColStats: true}, catalog: catalog, sql: sql, ctx: ctx, @@ -269,7 +273,6 @@ func New(catalog cat.Catalog, sql string) *OptTester { ot.evalCtx.SessionData().Database = "defaultdb" ot.evalCtx.SessionData().ZigzagJoinEnabled = true ot.evalCtx.SessionData().OptimizerUseHistograms = true - ot.evalCtx.SessionData().OptimizerUseMultiColStats = true ot.evalCtx.SessionData().LocalityOptimizedSearch = true ot.evalCtx.SessionData().ReorderJoinsLimit = opt.DefaultJoinOrderLimit ot.evalCtx.SessionData().InsertFastPath = true @@ -517,6 +520,7 @@ func (ot *OptTester) RunCommand(tb testing.TB, d *datadriven.TestData) string { ot.evalCtx.SessionData().PreferLookupJoinsForFKs = ot.Flags.PreferLookupJoinsForFKs ot.evalCtx.SessionData().PropagateInputOrdering = ot.Flags.PropagateInputOrdering ot.evalCtx.SessionData().NullOrderedLast = ot.Flags.NullOrderedLast + ot.evalCtx.SessionData().OptimizerUseMultiColStats = ot.Flags.UseMultiColStats ot.evalCtx.TestingKnobs.OptimizerCostPerturbation = ot.Flags.PerturbCost ot.evalCtx.Locality = ot.Flags.Locality @@ -1064,6 +1068,16 @@ func (f *Flags) Set(arg datadriven.CmdArg) error { case "propagate-input-ordering": f.PropagateInputOrdering = true + case "use-multi-col-stats": + if len(arg.Vals) != 1 { + return fmt.Errorf("use-multi-col-stats requires a single argument") + } + b, err := strconv.ParseBool(arg.Vals[0]) + if err != nil { + return errors.Wrap(err, "use-multi-col-stats") + } + f.UseMultiColStats = b + default: return fmt.Errorf("unknown argument: %s", arg.Key) } diff --git a/pkg/sql/opt/testutils/opttester/testdata/use-multi-col-stats b/pkg/sql/opt/testutils/opttester/testdata/use-multi-col-stats new file mode 100644 index 000000000000..314958d7fdff --- /dev/null +++ b/pkg/sql/opt/testutils/opttester/testdata/use-multi-col-stats @@ -0,0 +1,44 @@ +exec-ddl +CREATE TABLE rides ( + id UUID NOT NULL, + city VARCHAR NOT NULL, + vehicle_city VARCHAR NULL, + rider_id UUID NULL, + vehicle_id UUID NULL, + start_address VARCHAR NULL, + end_address VARCHAR NULL, + start_time TIMESTAMP NULL, + end_time TIMESTAMP NULL, + revenue DECIMAL(10,2) NULL, + CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), + INDEX rides_auto_index_fk_city_ref_users (city ASC, rider_id ASC), + INDEX rides_auto_index_fk_vehicle_city_ref_vehicles (vehicle_city ASC, vehicle_id ASC), + FAMILY "primary" (id, city, vehicle_city, rider_id, vehicle_id, start_address, end_address, start_time, end_time, revenue), + CONSTRAINT check_vehicle_city_city CHECK (vehicle_city = city) +) +---- + +inject-stats file=inject-stats.json table=rides +---- + +opt format=show-stats use-multi-col-stats=true +SELECT * FROM rides WHERE city = 'rome' and id = '17198184-b24f-4aa8-9933-64a72ff6665f' +---- +scan rides + ├── columns: id:1(uuid!null) city:2(varchar!null) vehicle_city:3(varchar) rider_id:4(uuid) vehicle_id:5(uuid) start_address:6(varchar) end_address:7(varchar) start_time:8(timestamp) end_time:9(timestamp) revenue:10(decimal) + ├── constraint: /2/1: [/'rome'/'17198184-b24f-4aa8-9933-64a72ff6665f' - /'rome'/'17198184-b24f-4aa8-9933-64a72ff6665f'] + ├── cardinality: [0 - 1] + ├── stats: [rows=0.9111111, distinct(1)=0.911111, null(1)=0, avgsize(1)=1, distinct(2)=0.911111, null(2)=0, avgsize(2)=6, distinct(1,2)=0.911111, null(1,2)=0, avgsize(1,2)=8] + ├── key: () + └── fd: ()-->(1-10) + +opt format=show-stats use-multi-col-stats=false +SELECT * FROM rides WHERE city = 'rome' and id = '17198184-b24f-4aa8-9933-64a72ff6665f' +---- +scan rides + ├── columns: id:1(uuid!null) city:2(varchar!null) vehicle_city:3(varchar) rider_id:4(uuid) vehicle_id:5(uuid) start_address:6(varchar) end_address:7(varchar) start_time:8(timestamp) end_time:9(timestamp) revenue:10(decimal) + ├── constraint: /2/1: [/'rome'/'17198184-b24f-4aa8-9933-64a72ff6665f' - /'rome'/'17198184-b24f-4aa8-9933-64a72ff6665f'] + ├── cardinality: [0 - 1] + ├── stats: [rows=0.1111111, distinct(1)=0.111111, null(1)=0, avgsize(1)=1, distinct(2)=0.111111, null(2)=0, avgsize(2)=6] + ├── key: () + └── fd: ()-->(1-10)