Skip to content

Commit

Permalink
memo: optbuild unit tests for fast path unique check exprs
Browse files Browse the repository at this point in the history
This commit adds the `ExprFmtHideFastPathChecks` flag, which hides fast
path check expressions from the output of expression formatting. It is
used in all places except TestBuilder. Also, unit tests for
`buildFiltersForFastPathCheck` are added.

Epic: CRDB-26290
Informs: #58047

Release note: None
  • Loading branch information
Mark Sirek committed Sep 28, 2023
1 parent d0bb911 commit fe3ee3a
Show file tree
Hide file tree
Showing 13 changed files with 1,600 additions and 764 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/explain_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (b *stmtBundleBuilder) addOptPlans(ctx context.Context) {

b.z.AddFile("opt.txt", formatOptPlan(memo.ExprFmtHideAll))
b.z.AddFile("opt-v.txt", formatOptPlan(
memo.ExprFmtHideQualifications|memo.ExprFmtHideScalars|memo.ExprFmtHideTypes|memo.ExprFmtHideNotVisibleIndexInfo,
memo.ExprFmtHideQualifications|memo.ExprFmtHideScalars|memo.ExprFmtHideTypes|memo.ExprFmtHideNotVisibleIndexInfo|memo.ExprFmtHideFastPathChecks,
))
b.z.AddFile("opt-vv.txt", formatOptPlan(memo.ExprFmtHideQualifications|memo.ExprFmtHideNotVisibleIndexInfo))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/relational.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ func (b *Builder) buildApplyJoin(join memo.RelExpr) (execPlan, error) {
// Enhance the error with the EXPLAIN (OPT, VERBOSE) of the inner
// expression.
fmtFlags := memo.ExprFmtHideQualifications | memo.ExprFmtHideScalars | memo.ExprFmtHideTypes |
memo.ExprFmtHideNotVisibleIndexInfo
memo.ExprFmtHideNotVisibleIndexInfo | memo.ExprFmtHideFastPathChecks
explainOpt := o.FormatExpr(newRightSide, fmtFlags, false /* redactableValues */)
err = errors.WithDetailf(err, "newRightSide:\n%s", explainOpt)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ func (b *Builder) buildRoutinePlanGenerator(
// Enhance the error with the EXPLAIN (OPT, VERBOSE) of the
// inner expression.
fmtFlags := memo.ExprFmtHideQualifications | memo.ExprFmtHideScalars |
memo.ExprFmtHideTypes
memo.ExprFmtHideTypes | memo.ExprFmtHideFastPathChecks
explainOpt := o.FormatExpr(optimizedExpr, fmtFlags, false /* redactableValues */)
err = errors.WithDetailf(err, "routineExpr:\n%s", explainOpt)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/opt/exec/execbuilder/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func (b *Builder) buildExplainOpt(explain *memo.ExplainExpr) (execPlan, error) {
switch {
case explain.Options.Flags[tree.ExplainFlagVerbose]:
fmtFlags = memo.ExprFmtHideQualifications | memo.ExprFmtHideScalars |
memo.ExprFmtHideTypes | memo.ExprFmtHideNotNull | memo.ExprFmtHideNotVisibleIndexInfo
memo.ExprFmtHideTypes | memo.ExprFmtHideNotNull | memo.ExprFmtHideNotVisibleIndexInfo |
memo.ExprFmtHideFastPathChecks

case explain.Options.Flags[tree.ExplainFlagTypes]:
fmtFlags = memo.ExprFmtHideQualifications | memo.ExprFmtHideNotVisibleIndexInfo
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/indexrec/indexrec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestIndexRec(t *testing.T) {

const fmtFlags = memo.ExprFmtHideStats | memo.ExprFmtHideRuleProps |
memo.ExprFmtHideQualifications | memo.ExprFmtHideScalars | memo.ExprFmtHideTypes |
memo.ExprFmtHideNotVisibleIndexInfo
memo.ExprFmtHideNotVisibleIndexInfo | memo.ExprFmtHideFastPathChecks
datadriven.Walk(t, datapathutils.TestDataPath(t), func(t *testing.T, path string) {
catalog := testcat.New()
datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string {
Expand Down
9 changes: 8 additions & 1 deletion pkg/sql/opt/memo/expr_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ const (
// info regardless of whether we are actually scanning an invisible index.
ExprFmtHideNotVisibleIndexInfo

// ExprFmtHideFastPathChecks hides information about insert fast path unique
// check expressions. These expressions are not executed, but used to find
// constrained index scans which may be used to perform the check as a
// fast path check. Most of the time these expressions should not be displayed
// due to the fact that they are not actually executed.
ExprFmtHideFastPathChecks

// ExprFmtHideAll shows only the basic structure of the expression.
// Note: this flag should be used judiciously, as its meaning changes whenever
// we add more flags.
Expand Down Expand Up @@ -985,7 +992,7 @@ func (f *ExprFmtCtx) formatScalarWithLabel(
switch scalar.Op() {
case opt.ProjectionsOp, opt.AggregationsOp, opt.UniqueChecksOp, opt.FKChecksOp, opt.KVOptionsOp, opt.FastPathUniqueChecksOp:
// Omit empty lists (except filters) and special-purpose fast path check expressions.
if scalar.ChildCount() == 0 || scalar.Op() == opt.FastPathUniqueChecksOp {
if scalar.ChildCount() == 0 || (scalar.Op() == opt.FastPathUniqueChecksOp && f.HasFlags(ExprFmtHideFastPathChecks)) {
return
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/sql/opt/memo/memo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,29 @@ import (

func TestMemo(t *testing.T) {
flags := memo.ExprFmtHideCost | memo.ExprFmtHideRuleProps | memo.ExprFmtHideQualifications |
memo.ExprFmtHideStats | memo.ExprFmtHideNotVisibleIndexInfo
memo.ExprFmtHideStats | memo.ExprFmtHideNotVisibleIndexInfo | memo.ExprFmtHideFastPathChecks
runDataDrivenTest(t, datapathutils.TestDataPath(t, "memo"), flags)
}

func TestFormat(t *testing.T) {
runDataDrivenTest(t, datapathutils.TestDataPath(t, "format"), memo.ExprFmtShowAll)
runDataDrivenTest(t, datapathutils.TestDataPath(t, "format"), memo.ExprFmtShowAll|memo.ExprFmtHideFastPathChecks)
}

func TestLogicalProps(t *testing.T) {
flags := memo.ExprFmtHideCost | memo.ExprFmtHideQualifications | memo.ExprFmtHideStats |
memo.ExprFmtHideNotVisibleIndexInfo
memo.ExprFmtHideNotVisibleIndexInfo | memo.ExprFmtHideFastPathChecks
runDataDrivenTest(t, datapathutils.TestDataPath(t, "logprops"), flags)
}

func TestStats(t *testing.T) {
flags := memo.ExprFmtHideCost | memo.ExprFmtHideRuleProps | memo.ExprFmtHideQualifications |
memo.ExprFmtHideScalars | memo.ExprFmtHideNotVisibleIndexInfo
memo.ExprFmtHideScalars | memo.ExprFmtHideNotVisibleIndexInfo | memo.ExprFmtHideFastPathChecks
runDataDrivenTest(t, datapathutils.TestDataPath(t, "stats"), flags)
}

func TestStatsQuality(t *testing.T) {
flags := memo.ExprFmtHideCost | memo.ExprFmtHideRuleProps | memo.ExprFmtHideQualifications |
memo.ExprFmtHideScalars | memo.ExprFmtHideNotVisibleIndexInfo
memo.ExprFmtHideScalars | memo.ExprFmtHideNotVisibleIndexInfo | memo.ExprFmtHideFastPathChecks
runDataDrivenTest(t, datapathutils.TestDataPath(t, "stats_quality"), flags)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/opt/memo/typing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func TestTyping(t *testing.T) {
memo.ExprFmtHideCost|
memo.ExprFmtHideQualifications|
memo.ExprFmtHideScalars|
memo.ExprFmtHideNotVisibleIndexInfo,
memo.ExprFmtHideNotVisibleIndexInfo|
memo.ExprFmtHideFastPathChecks,
)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/norm/norm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestNormRules(t *testing.T) {

const fmtFlags = memo.ExprFmtHideStats | memo.ExprFmtHideCost | memo.ExprFmtHideRuleProps |
memo.ExprFmtHideQualifications | memo.ExprFmtHideScalars | memo.ExprFmtHideTypes |
memo.ExprFmtHideNotVisibleIndexInfo
memo.ExprFmtHideNotVisibleIndexInfo | memo.ExprFmtHideFastPathChecks
datadriven.Walk(t, datapathutils.TestDataPath(t, "rules"), func(t *testing.T, path string) {
catalog := testcat.New()
datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string {
Expand All @@ -66,7 +66,7 @@ func TestNormRuleProps(t *testing.T) {

const fmtFlags = memo.ExprFmtHideStats | memo.ExprFmtHideCost |
memo.ExprFmtHideQualifications | memo.ExprFmtHideScalars | memo.ExprFmtHideTypes |
memo.ExprFmtHideNotVisibleIndexInfo
memo.ExprFmtHideNotVisibleIndexInfo | memo.ExprFmtHideFastPathChecks
datadriven.Walk(t, datapathutils.TestDataPath(t, "ruleprops"), func(t *testing.T, path string) {
catalog := testcat.New()
datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string {
Expand Down
Loading

0 comments on commit fe3ee3a

Please sign in to comment.