Skip to content

Commit

Permalink
sqlsmith: added support for randomly using NULLS FIRST or NULLS LAST
Browse files Browse the repository at this point in the history
Prior to this commit, SQLSmith did not produce queries that used the
non-default nulls ordering. This commit adds support for sometimes using
NULLS FIRST or NULLS LAST with ordering clauses.

Release note: None
  • Loading branch information
rytaft committed Dec 13, 2022
1 parent ad1e76f commit b10363d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
18 changes: 15 additions & 3 deletions pkg/internal/sqlsmith/relational.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,16 @@ func (s *Smither) randDirection() tree.Direction {
return orderDirections[s.rnd.Intn(len(orderDirections))]
}

var nullsOrders = []tree.NullsOrder{
tree.DefaultNullsOrder,
tree.NullsFirst,
tree.NullsLast,
}

func (s *Smither) randNullsOrder() tree.NullsOrder {
return nullsOrders[s.rnd.Intn(len(nullsOrders))]
}

var nullabilities = []tree.Nullability{
tree.NotNull,
tree.Null,
Expand Down Expand Up @@ -772,7 +782,8 @@ func makeSelect(s *Smither) (tree.Statement, bool) {
}
order[i] = &tree.Order{
Expr: expr,
NullsOrder: tree.NullsFirst,
Direction: s.randDirection(),
NullsOrder: s.randNullsOrder(),
}
}
stmt = &tree.Select{
Expand Down Expand Up @@ -1277,8 +1288,9 @@ func (s *Smither) makeOrderBy(refs colRefs) tree.OrderBy {
continue
}
ob = append(ob, &tree.Order{
Expr: ref.item,
Direction: s.randDirection(),
Expr: ref.item,
Direction: s.randDirection(),
NullsOrder: s.randNullsOrder(),
})
}
return ob
Expand Down
8 changes: 6 additions & 2 deletions pkg/internal/sqlsmith/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,9 @@ func makeFunc(s *Smither, ctx Context, typ *types.T, refs colRefs) (tree.TypedEx
orderTypes []*types.T
)
addOrdCol := func(expr tree.Expr, typ *types.T) {
order = append(order, &tree.Order{Expr: expr, Direction: s.randDirection()})
order = append(order, &tree.Order{
Expr: expr, Direction: s.randDirection(), NullsOrder: s.randNullsOrder(),
})
orderTypes = append(orderTypes, typ)
}
s.sample(len(refs)-len(parts), 2, func(i int) {
Expand Down Expand Up @@ -520,7 +522,9 @@ func makeFunc(s *Smither, ctx Context, typ *types.T, refs colRefs) (tree.TypedEx
funcExpr.AggType = tree.GeneralAgg
funcExpr.OrderBy = make(tree.OrderBy, len(args))
for i := range funcExpr.OrderBy {
funcExpr.OrderBy[i] = &tree.Order{Expr: args[i], Direction: s.randDirection()}
funcExpr.OrderBy[i] = &tree.Order{
Expr: args[i], Direction: s.randDirection(), NullsOrder: s.randNullsOrder(),
}
}
}
}
Expand Down

0 comments on commit b10363d

Please sign in to comment.