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: disable GenerateParameterizedJoin for custom query plans #128409

Closed
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
35 changes: 35 additions & 0 deletions pkg/sql/opt/optgen/cmd/optgen/rule_names_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (g *ruleNamesGen) generate(compiled *lang.CompiledExpr, w io.Writer) {
fmt.Fprintf(g.w, " // NumRuleNames tracks the total count of rule names.\n")
fmt.Fprintf(g.w, " NumRuleNames\n")
fmt.Fprintf(g.w, ")\n\n")

clear(g.unique)
g.genIsGeneric()
}

func (g *ruleNamesGen) genRuleNameEnumByTag(tag string) {
Expand All @@ -64,3 +67,35 @@ func (g *ruleNamesGen) genRuleNameEnumByTag(tag string) {
}
fmt.Fprintf(g.w, "\n")
}

func (g *ruleNamesGen) genIsGeneric() {
fmt.Fprintf(g.w, "// IsGeneric returns true if r is tagged as a Generic rule.\n")
fmt.Fprintf(g.w, "func (r RuleName) IsGeneric() bool {\n")
fmt.Fprintf(g.w, " switch r {\n")
for _, rule := range g.compiled.Rules {
if !rule.Tags.Contains("Generic") {
continue
}

// Only include each unique rule name once, not once per operator.
if _, ok := g.unique[rule.Name]; ok {
continue
}
g.unique[rule.Name] = struct{}{}

if len(g.unique) == 1 {
fmt.Fprintf(g.w, " case ")
} else {
fmt.Fprintf(g.w, ", ")
}
fmt.Fprintf(g.w, "%s", rule.Name)
}
if len(g.unique) > 0 {
fmt.Fprintf(g.w, ":\n")
fmt.Fprintf(g.w, " return true\n")
}
fmt.Fprintf(g.w, " default:\n")
fmt.Fprintf(g.w, " return false\n")
fmt.Fprintf(g.w, " }\n")
fmt.Fprintf(g.w, "}\n\n")
}
34 changes: 15 additions & 19 deletions pkg/sql/opt/testutils/opttester/opt_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ type Flags struct {
// memo/check_expr.go.
DisableCheckExpr bool

// Generic enables optimizations for generic query plans.
Generic bool

// ExploreTraceRule restricts the ExploreTrace output to only show the effects
// of a specific rule.
ExploreTraceRule opt.RuleName
Expand Down Expand Up @@ -477,6 +480,8 @@ func New(catalog cat.Catalog, sqlStr string) *OptTester {
//
// - disable-check-expr: skips the assertions in memo/check_expr.go.
//
// - generic: enables optimizations for generic query plans.
//
// - rule: used with exploretrace; the value is the name of a rule. When
// specified, the exploretrace output is filtered to only show expression
// changes due to that specific rule.
Expand All @@ -502,12 +507,6 @@ func New(catalog cat.Catalog, sqlStr string) *OptTester {
// - table: used to set the current table used by the command. This is used by
// the inject-stats command.
//
// - stats-quality-prefix: must be used with the stats-quality command. If
// rewriteActualFlag=true, indicates that a table should be created with the
// given prefix for the output of each subexpression in the query. Otherwise,
// outputs the name of the table that would be created for each
// subexpression.
//
// - ignore-tables: specifies the set of stats tables for which stats quality
// comparisons should not be outputted. Only used with the stats-quality
// command. Note that tables can always be added to the `ignore-tables` set
Expand All @@ -523,16 +522,6 @@ func New(catalog cat.Catalog, sqlStr string) *OptTester {
//
// - inject-stats: the file path is relative to the test file.
//
// - join-limit: sets the value for SessionData.ReorderJoinsLimit, which
// indicates the number of joins at which the optimizer should stop
// attempting to reorder.
//
// - prefer-lookup-joins-for-fks: sets SessionData.PreferLookupJoinsForFKs to
// true, causing foreign key operations to prefer lookup joins.
//
// - null-ordered-last: sets SessionData.NullOrderedLast to true, which orders
// NULL values last in ascending order.
//
// - cascade-levels: used to limit the depth of recursive cascades for
// build-cascades.
//
Expand Down Expand Up @@ -1003,6 +992,9 @@ func (f *Flags) Set(arg datadriven.CmdArg) error {
case "disable-check-expr":
f.DisableCheckExpr = true

case "generic":
f.Generic = true

case "rule":
if len(arg.Vals) != 1 {
return fmt.Errorf("rule requires one argument")
Expand Down Expand Up @@ -2339,15 +2331,19 @@ func (ot *OptTester) makeOptimizer() *xform.Optimizer {
// not nil, allows the caller to update the table metadata before optimizing.
func (ot *OptTester) optimizeExpr(
o *xform.Optimizer, tables map[cat.StableID]cat.Table,
) (opt.Expr, error) {
err := ot.buildExpr(o.Factory())
) (root opt.Expr, err error) {
err = ot.buildExpr(o.Factory())
if err != nil {
return nil, err
}
if tables != nil {
o.Memo().Metadata().UpdateTableMeta(ot.ctx, &ot.evalCtx, tables)
}
root, err := o.Optimize()
if ot.Flags.Generic {
root, err = o.OptimizeGeneric()
} else {
root, err = o.Optimize()
}
if err != nil {
return nil, err
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/sql/opt/xform/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@ func (o *Optimizer) Memo() *memo.Memo {
// equivalent to the given expression. If there is a cost "tie", then any one
// of the qualifying lowest cost expressions may be selected by the optimizer.
func (o *Optimizer) Optimize() (_ opt.Expr, err error) {
originalMatchedRule := o.matchedRule
defer func() { o.matchedRule = originalMatchedRule }()
o.matchedRule = func(r opt.RuleName) bool {
if r.IsGeneric() {
return false
}
if originalMatchedRule != nil {
// Respect the original callback, if one was set.
return originalMatchedRule(r)
}
return true
}
return o.optimize()
}

// OptimizeGeneric is similar to Optimize. It enables all exploration rules
// designed specifically for optimization of generic query plans.
func (o *Optimizer) OptimizeGeneric() (_ opt.Expr, err error) {
return o.optimize()
}

func (o *Optimizer) optimize() (_ opt.Expr, err error) {
log.VEventf(o.ctx, 1, "optimize start")
defer log.VEventf(o.ctx, 1, "optimize finish")
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/xform/rules/generic.opt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# | / \ |
# Scan t Values ($1) Scan t Values ($1)
#
[GenerateParameterizedJoin, Explore]
[GenerateParameterizedJoin, Explore, Generic]
(Select
$scan:(Scan $scanPrivate:*) & (IsCanonicalScan $scanPrivate)
$filters:* &
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/opt/xform/testdata/external/hibernate
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ project
└── filters
└── min:14 = $1 [outer=(14), constraints=(/14: (/NULL - ]), fd=()-->(14)]

opt
opt generic
select
person0_.id as id1_2_,
person0_.address as address2_2_,
Expand Down Expand Up @@ -951,7 +951,7 @@ project
└── filters
└── max:16 = 0 [outer=(16), constraints=(/16: [/0 - /0]; tight), fd=()-->(16)]

opt
opt generic
select
person0_.id as id1_2_,
person0_.address as address2_2_,
Expand Down Expand Up @@ -1016,7 +1016,7 @@ project
│ └── filters (true)
└── filters (true)

opt
opt generic
select
person0_.id as id1_2_,
person0_.address as address2_2_,
Expand Down
16 changes: 8 additions & 8 deletions pkg/sql/opt/xform/testdata/external/nova
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ create table instance_type_extra_specs
)
----

opt
opt generic
select anon_1.flavors_created_at as anon_1_flavors_created_at,
anon_1.flavors_updated_at as anon_1_flavors_updated_at,
anon_1.flavors_id as anon_1_flavors_id,
Expand Down Expand Up @@ -710,7 +710,7 @@ sort
└── filters
└── instance_type_extra_specs_1.instance_type_id:22 = instance_types.id:1 [outer=(1,22), constraints=(/1: (/NULL - ]; /22: (/NULL - ]), fd=(1)==(22), (22)==(1)]

opt
opt generic
select anon_1.instance_types_created_at as anon_1_instance_types_created_at,
anon_1.instance_types_updated_at as anon_1_instance_types_updated_at,
anon_1.instance_types_deleted_at as anon_1_instance_types_deleted_at,
Expand Down Expand Up @@ -861,7 +861,7 @@ project
│ └── instance_type_extra_specs_1.deleted:37 = $7 [outer=(37), constraints=(/37: (/NULL - ]), fd=()-->(37)]
└── filters (true)

opt
opt generic
select anon_1.instance_types_created_at as anon_1_instance_types_created_at,
anon_1.instance_types_updated_at as anon_1_instance_types_updated_at,
anon_1.instance_types_deleted_at as anon_1_instance_types_deleted_at,
Expand Down Expand Up @@ -1026,7 +1026,7 @@ project
│ └── instance_type_extra_specs_1.deleted:37 = $7 [outer=(37), constraints=(/37: (/NULL - ]), fd=()-->(37)]
└── filters (true)

opt
opt generic
select anon_1.flavors_created_at as anon_1_flavors_created_at,
anon_1.flavors_updated_at as anon_1_flavors_updated_at,
anon_1.flavors_id as anon_1_flavors_id,
Expand Down Expand Up @@ -1166,7 +1166,7 @@ project
│ └── filters (true)
└── filters (true)

opt
opt generic
select anon_1.flavors_created_at as anon_1_flavors_created_at,
anon_1.flavors_updated_at as anon_1_flavors_updated_at,
anon_1.flavors_id as anon_1_flavors_id,
Expand Down Expand Up @@ -1609,7 +1609,7 @@ sort
└── filters
└── instance_type_extra_specs_1.instance_type_id:36 = instance_types.id:1 [outer=(1,36), constraints=(/1: (/NULL - ]; /36: (/NULL - ]), fd=(1)==(36), (36)==(1)]

opt
opt generic
select anon_1.instance_types_created_at as anon_1_instance_types_created_at,
anon_1.instance_types_updated_at as anon_1_instance_types_updated_at,
anon_1.instance_types_deleted_at as anon_1_instance_types_deleted_at,
Expand Down Expand Up @@ -2357,7 +2357,7 @@ sort
└── filters
└── instance_type_extra_specs_1.instance_type_id:50 = instance_types.id:1 [outer=(1,50), constraints=(/1: (/NULL - ]; /50: (/NULL - ]), fd=(1)==(50), (50)==(1)]

opt
opt generic
select anon_1.instance_types_created_at as anon_1_instance_types_created_at,
anon_1.instance_types_updated_at as anon_1_instance_types_updated_at,
anon_1.instance_types_deleted_at as anon_1_instance_types_deleted_at,
Expand Down Expand Up @@ -2511,7 +2511,7 @@ project
│ └── instance_type_extra_specs_1.deleted:37 = $7 [outer=(37), constraints=(/37: (/NULL - ]), fd=()-->(37)]
└── filters (true)

opt
opt generic
select anon_1.flavors_created_at as anon_1_flavors_created_at,
anon_1.flavors_updated_at as anon_1_flavors_updated_at,
anon_1.flavors_id as anon_1_flavors_id,
Expand Down
Loading
Loading