Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: add use-multi-col-stats test flag #76866

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pkg/sql/opt/testutils/opttester/opt_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,18 @@ 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.
// Metadata used by the SQL query is accessed via the catalog.
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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
44 changes: 44 additions & 0 deletions pkg/sql/opt/testutils/opttester/testdata/use-multi-col-stats
Original file line number Diff line number Diff line change
@@ -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)