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

Commit 7cdd432

Browse files
authored
Merge pull request #506 from kuba--/sql-select-limit
Implement sql_select_limit
2 parents 0d92223 + fe2c821 commit 7cdd432

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

engine_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,42 @@ func TestQueries(t *testing.T) {
576576
})
577577
}
578578

579+
func TestSessionSelectLimit(t *testing.T) {
580+
ctx := newCtx()
581+
ctx.Session.Set("sql_select_limit", sql.Int64, int64(1))
582+
583+
q := []struct {
584+
query string
585+
expected []sql.Row
586+
}{
587+
{
588+
"SELECT * FROM mytable ORDER BY i",
589+
[]sql.Row{{int64(1), "first row"}},
590+
},
591+
{
592+
"SELECT * FROM mytable ORDER BY i LIMIT 2",
593+
[]sql.Row{
594+
{int64(1), "first row"},
595+
{int64(2), "second row"},
596+
},
597+
},
598+
{
599+
"SELECT i FROM (SELECT i FROM mytable LIMIT 2) t ORDER BY i",
600+
[]sql.Row{{int64(1)}},
601+
},
602+
{
603+
"SELECT i FROM (SELECT i FROM mytable) t ORDER BY i LIMIT 2",
604+
[]sql.Row{{int64(1)}},
605+
},
606+
}
607+
e := newEngine(t)
608+
t.Run("sql_select_limit", func(t *testing.T) {
609+
for _, tt := range q {
610+
testQueryWithContext(ctx, t, e, tt.query, tt.expected)
611+
}
612+
})
613+
}
614+
579615
func TestSessionDefaults(t *testing.T) {
580616
ctx := newCtx()
581617
ctx.Session.Set("auto_increment_increment", sql.Int64, 0)
@@ -613,6 +649,7 @@ func TestSessionDefaults(t *testing.T) {
613649
require.Equal(defaults["ndbinfo_version"].Value, val)
614650
})
615651
}
652+
616653
func TestWarnings(t *testing.T) {
617654
ctx := newCtx()
618655
ctx.Session.Warn(&sql.Warning{Code: 1})

sql/parse/parse.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ func convertSelect(ctx *sql.Context, s *sqlparser.Select) (sql.Node, error) {
287287
if err != nil {
288288
return nil, err
289289
}
290+
} else if ok, val := sql.HasDefaultValue(ctx.Session, "sql_select_limit"); !ok {
291+
limit := val.(int64)
292+
node = plan.NewLimit(int64(limit), node)
290293
}
291294

292295
if s.Limit != nil && s.Limit.Offset != nil {

sql/session.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ func DefaultSessionConfig() map[string]TypedValue {
152152
}
153153
}
154154

155+
// HasDefaultValue checks if session variable value is the default one.
156+
func HasDefaultValue(s Session, key string) (bool, interface{}) {
157+
typ, val := s.Get(key)
158+
if cfg, ok := DefaultSessionConfig()[key]; ok {
159+
return (cfg.Typ == typ && cfg.Value == val), val
160+
}
161+
return false, val
162+
}
163+
155164
// NewSession creates a new session with data.
156165
func NewSession(address string, user string, id uint32) Session {
157166
return &BaseSession{

sql/session_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ func TestSessionConfig(t *testing.T) {
1212
require := require.New(t)
1313

1414
sess := NewSession("foo", "bar", 1)
15-
1615
typ, v := sess.Get("foo")
1716
require.Equal(Null, typ)
1817
require.Equal(nil, v)
@@ -34,7 +33,20 @@ func TestSessionConfig(t *testing.T) {
3433
require.Equal(3, sess.Warnings()[0].Code)
3534
require.Equal(2, sess.Warnings()[1].Code)
3635
require.Equal(1, sess.Warnings()[2].Code)
36+
}
37+
38+
func TestHasDefaultValue(t *testing.T) {
39+
require := require.New(t)
40+
sess := NewSession("foo", "bar", 1)
41+
42+
for key := range DefaultSessionConfig() {
43+
require.True(HasDefaultValue(sess, key))
44+
}
45+
46+
sess.Set("auto_increment_increment", Int64, 123)
47+
require.False(HasDefaultValue(sess, "auto_increment_increment"))
3748

49+
require.False(HasDefaultValue(sess, "non_existing_key"))
3850
}
3951

4052
type testNode struct{}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy