Skip to content

Commit

Permalink
Fixed incorrectly compiled LIMIT clause.
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Oct 8, 2018
1 parent d3f1a2d commit da44689
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions pkg/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,11 @@ func TestFor(t *testing.T) {
Convey("Should compile query with LIMIT 2, 2", t, func() {
c := compiler.New()

// 4 is offset
// 2 is count
prog, err := c.Compile(`
FOR i IN [ 1, 2, 3, 4, 1, 3 ]
LIMIT 2, 2
FOR i IN [ 1,2,3,4,5,6,7,8 ]
LIMIT 4, 2
RETURN i
`)

Expand All @@ -509,7 +511,7 @@ func TestFor(t *testing.T) {

So(err, ShouldBeNil)

So(string(out), ShouldEqual, `[3,4]`)
So(string(out), ShouldEqual, `[5,6]`)
})

Convey("Should compile query with FILTER i > 2", t, func() {
Expand Down
28 changes: 18 additions & 10 deletions pkg/compiler/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,29 +292,37 @@ func (v *visitor) doVisitForExpression(ctx *fql.ForExpressionContext, scope *sco
}

func (v *visitor) createLimit(ctx *fql.LimitClauseContext) (int, int, error) {
var limit int
var err error
var count int
var offset int

intLiterals := ctx.AllIntegerLiteral()

limitLiteral := intLiterals[0]
limit, err := strconv.Atoi(limitLiteral.GetText())
if len(intLiterals) > 1 {
offset, err = v.parseInt(intLiterals[0])

if err != nil {
return 0, 0, err
}
if err != nil {
return 0, 0, err
}

if len(intLiterals) > 1 {
offsetLiteral := intLiterals[1]
count, err = v.parseInt(intLiterals[1])

offset, err = strconv.Atoi(offsetLiteral.GetText())
if err != nil {
return 0, 0, err
}
} else {
count, err = strconv.Atoi(intLiterals[0].GetText())

if err != nil {
return 0, 0, err
}
}

return limit, offset, nil
return count, offset, nil
}

func (v *visitor) parseInt(node antlr.TerminalNode) (int, error) {
return strconv.Atoi(node.GetText())
}

func (v *visitor) createFilter(ctx *fql.FilterClauseContext, scope *scope) (core.Expression, error) {
Expand Down

0 comments on commit da44689

Please sign in to comment.