Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Implement sql_select_limit #506

Merged
merged 1 commit into from
Oct 26, 2018
Merged
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
37 changes: 37 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,42 @@ func TestQueries(t *testing.T) {
})
}

func TestSessionSelectLimit(t *testing.T) {
ctx := newCtx()
ctx.Session.Set("sql_select_limit", sql.Int64, int64(1))

q := []struct {
query string
expected []sql.Row
}{
{
"SELECT * FROM mytable ORDER BY i",
[]sql.Row{{int64(1), "first row"}},
},
{
"SELECT * FROM mytable ORDER BY i LIMIT 2",
[]sql.Row{
{int64(1), "first row"},
{int64(2), "second row"},
},
},
{
"SELECT i FROM (SELECT i FROM mytable LIMIT 2) t ORDER BY i",
[]sql.Row{{int64(1)}},
},
{
"SELECT i FROM (SELECT i FROM mytable) t ORDER BY i LIMIT 2",
[]sql.Row{{int64(1)}},
},
}
e := newEngine(t)
t.Run("sql_select_limit", func(t *testing.T) {
for _, tt := range q {
testQueryWithContext(ctx, t, e, tt.query, tt.expected)
}
})
}

func TestSessionDefaults(t *testing.T) {
ctx := newCtx()
ctx.Session.Set("auto_increment_increment", sql.Int64, 0)
Expand Down Expand Up @@ -602,6 +638,7 @@ func TestSessionDefaults(t *testing.T) {
require.Equal(defaults["ndbinfo_version"].Value, val)
})
}

func TestWarnings(t *testing.T) {
ctx := newCtx()
ctx.Session.Warn(&sql.Warning{Code: 1})
Expand Down
3 changes: 3 additions & 0 deletions sql/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ func convertSelect(ctx *sql.Context, s *sqlparser.Select) (sql.Node, error) {
if err != nil {
return nil, err
}
} else if ok, val := sql.HasDefaultValue(ctx.Session, "sql_select_limit"); !ok {
limit := val.(int64)
node = plan.NewLimit(int64(limit), node)
}

if s.Limit != nil && s.Limit.Offset != nil {
Expand Down
9 changes: 9 additions & 0 deletions sql/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ func DefaultSessionConfig() map[string]TypedValue {
}
}

// HasDefaultValue checks if session variable value is the default one.
func HasDefaultValue(s Session, key string) (bool, interface{}) {
typ, val := s.Get(key)
if cfg, ok := DefaultSessionConfig()[key]; ok {
return (cfg.Typ == typ && cfg.Value == val), val
}
return false, val
}

// NewSession creates a new session with data.
func NewSession(address string, user string, id uint32) Session {
return &BaseSession{
Expand Down
14 changes: 13 additions & 1 deletion sql/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func TestSessionConfig(t *testing.T) {
require := require.New(t)

sess := NewSession("foo", "bar", 1)

typ, v := sess.Get("foo")
require.Equal(Null, typ)
require.Equal(nil, v)
Expand All @@ -34,7 +33,20 @@ func TestSessionConfig(t *testing.T) {
require.Equal(3, sess.Warnings()[0].Code)
require.Equal(2, sess.Warnings()[1].Code)
require.Equal(1, sess.Warnings()[2].Code)
}

func TestHasDefaultValue(t *testing.T) {
require := require.New(t)
sess := NewSession("foo", "bar", 1)

for key := range DefaultSessionConfig() {
require.True(HasDefaultValue(sess, key))
}

sess.Set("auto_increment_increment", Int64, 123)
require.False(HasDefaultValue(sess, "auto_increment_increment"))

require.False(HasDefaultValue(sess, "non_existing_key"))
}

type testNode struct{}
Expand Down