Add ability to fold without now().

pull/1210/head
Ben Johnson 2014-12-11 09:58:33 -07:00
parent e854b913e5
commit f469097e8b
3 changed files with 23 additions and 12 deletions

View File

@ -577,12 +577,12 @@ func (e *Wildcard) String() string { return "*" }
// Fold performs constant folding on an expression.
// The function, "now()", is expanded into the current time during folding.
func Fold(expr Expr, now time.Time) Expr {
func Fold(expr Expr, now *time.Time) Expr {
switch expr := expr.(type) {
case *Call:
// Replace "now()" with current time.
if strings.ToLower(expr.Name) == "now" {
return &TimeLiteral{Val: now}
if strings.ToLower(expr.Name) == "now" && now != nil {
return &TimeLiteral{Val: *now}
}
// Fold call arguments.
@ -610,7 +610,7 @@ func Fold(expr Expr, now time.Time) Expr {
}
// foldBinaryExpr performs constant folding if the binary expression has two literals.
func foldBinaryExpr(expr *BinaryExpr, now time.Time) Expr {
func foldBinaryExpr(expr *BinaryExpr, now *time.Time) Expr {
// Fold both sides of binary expression first.
expr.LHS = Fold(expr.LHS, now)
expr.RHS = Fold(expr.RHS, now)

View File

@ -141,15 +141,9 @@ func TestFold(t *testing.T) {
// String literals.
{`"foo" + 'bar'`, `"foobar"`},
} {
// Parse incoming expression.
expr, err := influxql.NewParser(strings.NewReader(tt.in)).ParseExpr()
if err != nil {
t.Errorf("%d. %s: parse error: %s", i, tt.in, err)
continue
}
// Fold expression.
expr = influxql.Fold(expr, mustParseTime("2000-01-01T00:00:00Z"))
now := mustParseTime("2000-01-01T00:00:00Z")
expr := influxql.Fold(MustParseExpr(tt.in), &now)
// Compare with expected output.
if out := expr.String(); tt.out != out {
@ -158,3 +152,11 @@ func TestFold(t *testing.T) {
}
}
}
// Ensure an a "now()" call is not folded when now is not passed in.
func TestFold_WithoutNow(t *testing.T) {
expr := influxql.Fold(MustParseExpr(`now()`), nil)
if s := expr.String(); s != `now()` {
t.Fatalf("unexpected expr: %s", s)
}
}

View File

@ -451,6 +451,15 @@ func MustParseSelectStatement(s string) *influxql.SelectStatement {
return stmt.(*influxql.SelectStatement)
}
// MustParseExpr parses an expression. Panic on error.
func MustParseExpr(s string) influxql.Expr {
expr, err := influxql.NewParser(strings.NewReader(s)).ParseExpr()
if err != nil {
panic(err.Error())
}
return expr
}
// errstring converts an error to its string representation.
func errstring(err error) string {
if err != nil {