Add ability to fold without now().
parent
e854b913e5
commit
f469097e8b
|
@ -577,12 +577,12 @@ func (e *Wildcard) String() string { return "*" }
|
||||||
|
|
||||||
// Fold performs constant folding on an expression.
|
// Fold performs constant folding on an expression.
|
||||||
// The function, "now()", is expanded into the current time during folding.
|
// 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) {
|
switch expr := expr.(type) {
|
||||||
case *Call:
|
case *Call:
|
||||||
// Replace "now()" with current time.
|
// Replace "now()" with current time.
|
||||||
if strings.ToLower(expr.Name) == "now" {
|
if strings.ToLower(expr.Name) == "now" && now != nil {
|
||||||
return &TimeLiteral{Val: now}
|
return &TimeLiteral{Val: *now}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fold call arguments.
|
// 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.
|
// 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.
|
// Fold both sides of binary expression first.
|
||||||
expr.LHS = Fold(expr.LHS, now)
|
expr.LHS = Fold(expr.LHS, now)
|
||||||
expr.RHS = Fold(expr.RHS, now)
|
expr.RHS = Fold(expr.RHS, now)
|
||||||
|
|
|
@ -141,15 +141,9 @@ func TestFold(t *testing.T) {
|
||||||
// String literals.
|
// String literals.
|
||||||
{`"foo" + 'bar'`, `"foobar"`},
|
{`"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.
|
// 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.
|
// Compare with expected output.
|
||||||
if out := expr.String(); tt.out != out {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -451,6 +451,15 @@ func MustParseSelectStatement(s string) *influxql.SelectStatement {
|
||||||
return stmt.(*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.
|
// errstring converts an error to its string representation.
|
||||||
func errstring(err error) string {
|
func errstring(err error) string {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue