Skip to content

Commit

Permalink
Merge #81145
Browse files Browse the repository at this point in the history
81145: sql: minor sqlsmith and tlp cleanup r=mgartner a=mgartner

#### sqlsmith: remove duplicate disableImpureFuncs

In #79973, `Smither.disableImpureFuncs` and `Smither.DisableImpureFuncs`
was added. The `Smither` already had `disableImpureFns` and
`DisableImpureFns` for disabling the generation of impure function
calls, so the newly added field and method are redundant. This commit
consolidates things.

Release note: None

#### tlp: use Smither.DisableImpureFns to disable impure functions

TLP now uses `Smither.DisableImpureFns` to disable the generation of
impure function calls, rather than writing to the internal
`Smither.disableImpureFns` field. Writing to the internal field directly
was preferred prior to #81017 when a single Smither was shared between
TLP query generation and mutation statement generation so that mutation
statements could use impure functions.

Release note: None


Co-authored-by: Marcus Gartner <[email protected]>
  • Loading branch information
craig[bot] and mgartner committed May 9, 2022
2 parents ce7b197 + 0fc60e8 commit 1a91d7c
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/roachtest/tests/costfuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func runOneRoundCostFuzz(

// Initialize a smither that generates only deterministic SELECT statements.
smither, err := sqlsmith.NewSmither(conn, rnd,
sqlsmith.DisableMutations(), sqlsmith.DisableImpureFuncs(), sqlsmith.DisableLimits(),
sqlsmith.DisableMutations(), sqlsmith.DisableImpureFns(), sqlsmith.DisableLimits(),
sqlsmith.SetComplexity(.3),
)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/roachtest/tests/tlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func runOneTLP(
defer mutSmither.Close()

// Initialize a smither that will never generate mutations.
tlpSmither, err := sqlsmith.NewSmither(conn, rnd, sqlsmith.DisableMutations())
tlpSmither, err := sqlsmith.NewSmither(conn, rnd,
sqlsmith.DisableMutations(), sqlsmith.DisableImpureFns())
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/sqlsmith/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func makeFunc(s *Smither, ctx Context, typ *types.T, refs colRefs) (tree.TypedEx
if class == tree.WindowClass && s.d6() != 1 {
class = tree.NormalClass
}
fns := s.functions[class][typ.Oid()]
fns := functions[class][typ.Oid()]
if len(fns) == 0 {
return nil, false
}
Expand Down
18 changes: 2 additions & 16 deletions pkg/internal/sqlsmith/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treebin"
"github.com/cockroachdb/cockroach/pkg/sql/sem/volatility"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/errors"
"github.com/lib/pq/oid"
Expand Down Expand Up @@ -478,7 +477,7 @@ type function struct {
overload *tree.Overload
}

func functions(s *Smither) map[tree.FunctionClass]map[oid.Oid][]function {
var functions = func() map[tree.FunctionClass]map[oid.Oid][]function {
m := map[tree.FunctionClass]map[oid.Oid][]function{}
for _, def := range tree.FunDefs {
switch def.Name {
Expand Down Expand Up @@ -511,19 +510,6 @@ func functions(s *Smither) map[tree.FunctionClass]map[oid.Oid][]function {
if skip {
continue
}
if s.disableImpureFuncs {
var impure bool
for _, def := range def.Definition {
overload := def.(*tree.Overload)
switch overload.Volatility {
case volatility.Stable, volatility.Volatile:
impure = true
}
}
if impure {
continue
}
}
if _, ok := m[def.Class]; !ok {
m[def.Class] = map[oid.Oid][]function{}
}
Expand Down Expand Up @@ -557,4 +543,4 @@ func functions(s *Smither) map[tree.FunctionClass]map[oid.Oid][]function {
}
}
return m
}
}()
10 changes: 0 additions & 10 deletions pkg/internal/sqlsmith/sqlsmith.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/lib/pq/oid"
)

// sqlsmith-go
Expand Down Expand Up @@ -69,7 +68,6 @@ type Smither struct {
nameCounts map[string]int
activeSavepoints []string
types *typeInfo
functions map[tree.FunctionClass]map[oid.Oid][]function

stmtWeights, alterWeights []statementWeight
stmtSampler, alterSampler *statementSampler
Expand All @@ -84,7 +82,6 @@ type Smither struct {
disableImpureFns bool
disableLimits bool
disableWindowFuncs bool
disableImpureFuncs bool
simpleDatums bool
avoidConsts bool
outputSort bool
Expand Down Expand Up @@ -125,7 +122,6 @@ func NewSmither(db *gosql.DB, rnd *rand.Rand, opts ...SmitherOption) (*Smither,
for _, opt := range opts {
opt.Apply(s)
}
s.functions = functions(s)
s.stmtSampler = newWeightedStatementSampler(s.stmtWeights, rnd.Int63())
s.alterSampler = newWeightedStatementSampler(s.alterWeights, rnd.Int63())
s.tableExprSampler = newWeightedTableExprSampler(s.tableExprWeights, rnd.Int63())
Expand Down Expand Up @@ -243,12 +239,6 @@ var DisableMutations = simpleOption("disable mutations", func(s *Smither) {
s.tableExprWeights = nonMutatingTableExprs
})

// DisableImpureFuncs causes the Smither to not emit statements that contain
// impure builtins (aka builtins that don't always return the same result).
var DisableImpureFuncs = simpleOption("disable impure funcs", func(s *Smither) {
s.disableImpureFuncs = true
})

// SetComplexity configures the Smither's complexity, in other words the
// likelihood that at any given node the Smither will recurse and create a
// deeper query true. The default is .2.
Expand Down
7 changes: 0 additions & 7 deletions pkg/internal/sqlsmith/tlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ SELECT (SELECT count(*) FROM undiff), (SELECT count(*) FROM diff)`,
// have all been implemented in SQLancer. See:
// https://github.com/sqlancer/sqlancer/tree/1.1.0/src/sqlancer/cockroachdb/oracle/tlp.
func (s *Smither) GenerateTLP() (unpartitioned, partitioned string, args []interface{}) {
// Set disableImpureFns to true so that generated predicates are immutable.
originalDisableImpureFns := s.disableImpureFns
s.disableImpureFns = true
defer func() {
s.disableImpureFns = originalDisableImpureFns
}()

switch tlpType := s.rnd.Intn(5); tlpType {
case 0:
partitioned, unpartitioned, args = s.generateWhereTLP()
Expand Down

0 comments on commit 1a91d7c

Please sign in to comment.