Merge pull request #7990 from influxdata/js-modulo-operator-fix

Fixed String() output for MOD operator and added MOD op precedence
pull/7926/head^2
Jonathan A. Sternberg 2017-02-13 14:46:12 -06:00 committed by GitHub
commit 9636960ce6
2 changed files with 68 additions and 1 deletions

View File

@ -2817,6 +2817,62 @@ func TestParser_ParseExpr(t *testing.T) {
},
},
// Binary expression with LHS precedence
{
s: `1 / 2 + 3`,
expr: &influxql.BinaryExpr{
Op: influxql.ADD,
LHS: &influxql.BinaryExpr{
Op: influxql.DIV,
LHS: &influxql.IntegerLiteral{Val: 1},
RHS: &influxql.IntegerLiteral{Val: 2},
},
RHS: &influxql.IntegerLiteral{Val: 3},
},
},
// Binary expression with RHS precedence
{
s: `1 + 2 / 3`,
expr: &influxql.BinaryExpr{
Op: influxql.ADD,
LHS: &influxql.IntegerLiteral{Val: 1},
RHS: &influxql.BinaryExpr{
Op: influxql.DIV,
LHS: &influxql.IntegerLiteral{Val: 2},
RHS: &influxql.IntegerLiteral{Val: 3},
},
},
},
// Binary expression with LHS precedence
{
s: `1 % 2 + 3`,
expr: &influxql.BinaryExpr{
Op: influxql.ADD,
LHS: &influxql.BinaryExpr{
Op: influxql.MOD,
LHS: &influxql.IntegerLiteral{Val: 1},
RHS: &influxql.IntegerLiteral{Val: 2},
},
RHS: &influxql.IntegerLiteral{Val: 3},
},
},
// Binary expression with RHS precedence
{
s: `1 + 2 % 3`,
expr: &influxql.BinaryExpr{
Op: influxql.ADD,
LHS: &influxql.IntegerLiteral{Val: 1},
RHS: &influxql.BinaryExpr{
Op: influxql.MOD,
LHS: &influxql.IntegerLiteral{Val: 2},
RHS: &influxql.IntegerLiteral{Val: 3},
},
},
},
// Binary expression with LHS paren group.
{
s: `(1 + 2) * 3`,
@ -2950,6 +3006,16 @@ func TestParser_ParseExpr(t *testing.T) {
t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
} else if tt.err == "" && !reflect.DeepEqual(tt.expr, expr) {
t.Errorf("%d. %q\n\nexpr mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.expr, expr)
} else if err == nil {
// Attempt to reparse the expr as a string and confirm it parses the same.
expr2, err := influxql.ParseExpr(expr.String())
if err != nil {
t.Errorf("%d. %q: unable to parse expr string: %s", i, expr.String(), err)
} else if !reflect.DeepEqual(tt.expr, expr2) {
t.Logf("\n# %s\nexp=%s\ngot=%s\n", tt.s, mustMarshalJSON(tt.expr), mustMarshalJSON(expr2))
t.Logf("\nSQL exp=%s\nSQL got=%s\n", tt.expr.String(), expr2.String())
t.Errorf("%d. %q\n\nexpr reparse mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.expr, expr2)
}
}
}
}

View File

@ -156,6 +156,7 @@ var tokens = [...]string{
SUB: "-",
MUL: "*",
DIV: "/",
MOD: "%",
AND: "AND",
OR: "OR",
@ -285,7 +286,7 @@ func (tok Token) Precedence() int {
return 3
case ADD, SUB:
return 4
case MUL, DIV:
case MUL, DIV, MOD:
return 5
}
return 0