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

function: implement NOW function #568

Merged
merged 1 commit into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ We support and actively test against certain third-party clients to ensure compa
- `SUBSTRING(str, pos)`, `SUBSTRING(str, pos, len)` : Return a substring from the provided string.
- `SUBSTR(str, pos)`, `SUBSTR(str, pos, len)` : Return a substring from the provided string.
- `MID(str, pos)`, `MID(str, pos, len)` : Return a substring from the provided string.
- Date and Timestamp functions: `YEAR(date)`, `MONTH(date)`, `DAY(date)`, `WEEKDAY(date)`, `HOUR(date)`, `MINUTE(date)`, `SECOND(date)`, `DAYOFWEEK(date)`, `DAYOFYEAR(date)`.
- Date and Timestamp functions: `YEAR(date)`, `MONTH(date)`, `DAY(date)`, `WEEKDAY(date)`, `HOUR(date)`, `MINUTE(date)`, `SECOND(date)`, `DAYOFWEEK(date)`, `DAYOFYEAR(date)`, `NOW()`.
- `ARRAY_LENGTH(json)`: If the json representation is an array, this function returns its size.
- `SPLIT(str,sep)`: Receives a string and a separator and returns the parts of the string split by the separator as a JSON array of strings.
- `CONCAT(...)`: Concatenate any group of fields into a single string.
Expand Down
1 change: 1 addition & 0 deletions SUPPORTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@
- MONTH
- SECOND
- YEAR
- NOW
1 change: 1 addition & 0 deletions sql/expression/function/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ var Defaults = sql.Functions{
"replace": sql.Function3(NewReplace),
"ifnull": sql.Function2(NewIfNull),
"nullif": sql.Function2(NewNullIf),
"now": sql.Function0(NewNow),
}
38 changes: 38 additions & 0 deletions sql/expression/function/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,41 @@ var (
dayOfWeek = datePartFunc(func(t time.Time) int { return int(t.Weekday()) + 1 })
dayOfYear = datePartFunc((time.Time).YearDay)
)

type clock func() time.Time

var defaultClock = time.Now

// Now is a function that returns the current time.
type Now struct {
clock
}

// NewNow returns a new Now node.
func NewNow() sql.Expression {
return &Now{defaultClock}
}

// Type implements the sql.Expression interface.
func (*Now) Type() sql.Type { return sql.Timestamp }

func (*Now) String() string { return "NOW()" }

// IsNullable implements the sql.Expression interface.
func (*Now) IsNullable() bool { return false }

// Resolved implements the sql.Expression interface.
func (*Now) Resolved() bool { return true }

// Children implements the sql.Expression interface.
func (*Now) Children() []sql.Expression { return nil }

// Eval implements the sql.Expression interface.
func (n *Now) Eval(*sql.Context, sql.Row) (interface{}, error) {
return n.clock(), nil
}

// TransformUp implements the sql.Expression interface.
func (n *Now) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) {
return f(n)
}
13 changes: 13 additions & 0 deletions sql/expression/function/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,16 @@ func TestTime_DayOfYear(t *testing.T) {
})
}
}

func TestNow(t *testing.T) {
require := require.New(t)
date := time.Date(2018, time.December, 2, 16, 25, 0, 0, time.Local)
clock := clock(func() time.Time {
return date
})
f := &Now{clock}

result, err := f.Eval(nil, nil)
require.NoError(err)
require.Equal(date, result)
}