Skip to content

Commit 56cc652

Browse files
committed
sqlsmith: support DO block for plpgsql
A `DO` block can be either a top-level sql statement or be part of the plpgsql function body. In this commit we introduce the DO block for both of these 2 usages. We also introduced `DisableDoBlocks` which disable both usages of a DO block. Release note: None
1 parent 8f5ae52 commit 56cc652

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

pkg/cmd/smith/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ var (
6666
"DisableCrossJoins": sqlsmith.DisableCrossJoins(),
6767
"DisableDDLs": sqlsmith.DisableDDLs(),
6868
"DisableDecimals": sqlsmith.DisableDecimals(),
69+
"DisableDoBlocks": sqlsmith.DisableDoBlocks(),
6970
"DisableEverything": sqlsmith.DisableEverything(),
7071
"DisableIndexHints": sqlsmith.DisableIndexHints(),
7172
"DisableInsertSelect": sqlsmith.DisableInsertSelect(),

pkg/internal/sqlsmith/plpgsql.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ var (
141141
{2, makePLpgSQLReturn},
142142
{2, makePLpgSQLIf},
143143
{2, makePLpgSQLWhile},
144+
{2, makeDoBlockWithinScope},
144145
{2, makePLpgSQLForLoop},
145146
{5, makePLpgSQLNull},
146147
{10, makePLpgSQLAssign},
@@ -395,3 +396,20 @@ func (s *plpgsqlBlockScope) addVariable(name string, typ *types.T, constant bool
395396
s.constants[name] = struct{}{}
396397
}
397398
}
399+
400+
func (s *Smither) makeDoBlockTreeStmt() (*tree.DoBlock, bool) {
401+
scope := makeBlockScope(0, types.Unknown, tree.RoutineVolatile)
402+
doBlock, ok := makeDoBlockWithinScope(s, scope)
403+
if !ok {
404+
return nil, false
405+
}
406+
return &tree.DoBlock{Code: doBlock.(*ast.DoBlock)}, true
407+
}
408+
409+
func makeDoBlockWithinScope(s *Smither, scope plpgsqlBlockScope) (ast.Statement, bool) {
410+
if s.disableDoBlock {
411+
return nil, false
412+
}
413+
block, _ := makePLpgSQLBlock(s, scope)
414+
return &ast.DoBlock{Block: block.(*ast.Block)}, true
415+
}

pkg/internal/sqlsmith/relational.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ var (
110110
{1, makeImport},
111111
{1, makeCreateStats},
112112
{1, makeSetSessionCharacteristics},
113+
{5, makeDoBlock},
113114
}
114115
nonMutatingStatements = []statementWeight{
115116
{10, makeSelect},
117+
{2, makeDoBlock},
116118
}
117119
allStatements = append(mutatingStatements, nonMutatingStatements...)
118120

@@ -917,6 +919,10 @@ func makeCreateFunc(s *Smither) (tree.Statement, bool) {
917919
return s.makeCreateFunc()
918920
}
919921

922+
func makeDoBlock(s *Smither) (tree.Statement, bool) {
923+
return s.makeDoBlockTreeStmt()
924+
}
925+
920926
func makeDropFunc(s *Smither) (tree.Statement, bool) {
921927
if s.disableUDFCreation {
922928
return nil, false

pkg/internal/sqlsmith/sqlsmith.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ type Smither struct {
118118
// disableUDFCreation indicates whether we're not allowed to create UDFs.
119119
// It follows that if we haven't created any UDFs, we have no UDFs to invoke
120120
// too.
121-
disableUDFCreation bool
121+
disableUDFCreation bool
122+
// disableDoBlock indicates whether we're not allowed to create DO blocks,
123+
// both as top level statements and inside plpgsql function body
124+
// definition.
125+
disableDoBlock bool
122126
disableIsolationChange bool
123127

124128
bulkSrv *httptest.Server
@@ -612,6 +616,11 @@ var DisableUDFs = simpleOption("disable udfs", func(s *Smither) {
612616
s.disableUDFCreation = true
613617
})
614618

619+
// DisableDoBlocks causes the Smither to disable DO blocks.
620+
var DisableDoBlocks = simpleOption("disable do block", func(s *Smither) {
621+
s.disableDoBlock = true
622+
})
623+
615624
// DisableIsolationChange causes the Smither to disable stmts that modify the
616625
// txn isolation level.
617626
var DisableIsolationChange = simpleOption("disable isolation change", func(s *Smither) {

0 commit comments

Comments
 (0)