Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add important timestamp/date builtins. #2420

Merged
merged 2 commits into from
Sep 9, 2015
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
109 changes: 107 additions & 2 deletions sql/parser/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"math/rand"
"reflect"
"strings"
"time"
)

var errEmptyInputString = errors.New("the input string should not be empty")
Expand All @@ -36,8 +37,10 @@ type typeList []reflect.Type
type builtin struct {
types typeList
returnType Datum
impure bool
fn func(DTuple) (Datum, error)
// Set to true when a function returns a different value when called with
// the same parameters. e.g.: random(), now().
impure bool
fn func(DTuple) (Datum, error)
}

func (b builtin) match(types typeList) bool {
Expand Down Expand Up @@ -319,6 +322,99 @@ var builtins = map[string][]builtin{
},
},

// Timestamp/Date functions.

"age": {
builtin{
types: typeList{timestampType},
returnType: DummyInterval,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should have impure: true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

impure: true,
fn: func(args DTuple) (Datum, error) {
return DInterval{Duration: time.Now().Sub(args[0].(DTimestamp).Time)}, nil
},
},
builtin{
types: typeList{timestampType, timestampType},
returnType: DummyInterval,
fn: func(args DTuple) (Datum, error) {
return DInterval{Duration: args[0].(DTimestamp).Sub(args[1].(DTimestamp).Time)}, nil
},
},
},

"current_date": {
builtin{
types: typeList{},
returnType: DummyDate,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should have impure: true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no clue as to why I'm doing this. I'd love to add in some documentation for this field for future development. What does this field mean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏚ [tamird:~/src/go/src/github.com/cockroachdb/cockroach] master ± git grep -A10 -B15 -F .impure
sql/parser/normalize.go-
sql/parser/normalize.go-var _ Visitor = &isConstVisitor{}
sql/parser/normalize.go-
sql/parser/normalize.go-func (v *isConstVisitor) Visit(expr Expr, pre bool) (Visitor, Expr) {
sql/parser/normalize.go-        if pre && v.isConst {
sql/parser/normalize.go-                if isVar(expr) {
sql/parser/normalize.go-                        v.isConst = false
sql/parser/normalize.go-                        return nil, expr
sql/parser/normalize.go-                }
sql/parser/normalize.go-
sql/parser/normalize.go-                switch t := expr.(type) {
sql/parser/normalize.go-                case *Subquery:
sql/parser/normalize.go-                        v.isConst = false
sql/parser/normalize.go-                        return nil, expr
sql/parser/normalize.go-                case *FuncExpr:
sql/parser/normalize.go:                        // typeCheckFuncExpr populates t.fn.impure.
sql/parser/normalize.go:                        if _, err := typeCheckFuncExpr(t); err != nil || t.fn.impure {
sql/parser/normalize.go-                                v.isConst = false
sql/parser/normalize.go-                                return nil, expr
sql/parser/normalize.go-                        }
sql/parser/normalize.go-                }
sql/parser/normalize.go-        }
sql/parser/normalize.go-        return v, expr
sql/parser/normalize.go-}
sql/parser/normalize.go-
sql/parser/normalize.go-// isConst returns true if the expression contains only constant values
sql/parser/normalize.go-// (i.e. it does not contain a DReference).

The interesting bit is:

case *FuncExpr:
        // typeCheckFuncExpr populates t.fn.impure.
        if _, err := typeCheckFuncExpr(t); err != nil || t.fn.impure {
                v.isConst = false
                return nil, expr
        }
}

fn.impure is true when a function is not pure, that is, it produces different results with the same inputs. This is used by isConstVisitor to determine that a function call with constant arguments is not a constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment and also classified certain functions as impure.

impure: true,
fn: func(args DTuple) (Datum, error) {
return DDate{Time: time.Now().Truncate(24 * time.Hour)}, nil
},
},
},

"current_timestamp": {nowImpl},
"now": {nowImpl},

"extract": {
builtin{
types: typeList{stringType, timestampType},
returnType: DummyInt,
fn: func(args DTuple) (Datum, error) {
// extract timeSpan fromTime.
fromTime := args[1].(DTimestamp)
timeSpan := strings.ToLower(string(args[0].(DString)))
switch timeSpan {
case "year":
return DInt(fromTime.Year()), nil

case "quarter":
return DInt(fromTime.Month()/4 + 1), nil

case "month":
return DInt(fromTime.Month()), nil

case "week":
_, week := fromTime.ISOWeek()
return DInt(week), nil

case "day":
return DInt(fromTime.Day()), nil

case "dayofweek", "dow":
return DInt(fromTime.Weekday()), nil

case "dayofyear", "doy":
return DInt(fromTime.YearDay()), nil

case "hour":
return DInt(fromTime.Hour()), nil

case "minute":
return DInt(fromTime.Minute()), nil

case "second":
return DInt(fromTime.Second()), nil

case "millisecond":
return DInt(fromTime.Nanosecond() / int(time.Millisecond)), nil

case "microsecond":
return DInt(fromTime.Nanosecond() / int(time.Microsecond)), nil

case "nanosecond":
return DInt(fromTime.Nanosecond()), nil

case "epoch":
return DInt(fromTime.Unix()), nil

default:
return DNull, fmt.Errorf("unsupported timespan: %s", timeSpan)
}
},
},
},

// Aggregate functions.

"avg": {
Expand Down Expand Up @@ -433,6 +529,15 @@ var substringImpls = []builtin{
},
}

var nowImpl = builtin{
types: typeList{},
returnType: DummyTimestamp,
impure: true,
fn: func(args DTuple) (Datum, error) {
return DTimestamp{Time: time.Now()}, nil
},
}

func stringBuiltin1(f func(string) (Datum, error), returnType Datum) builtin {
return builtin{
types: typeList{stringType},
Expand Down
6 changes: 5 additions & 1 deletion sql/parser/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,11 @@ func ParseDate(s DString) (DDate, error) {
// ParseTimestamp parses the timestamp.
func ParseTimestamp(s DString) (DTimestamp, error) {
str := string(s)
t, err := time.Parse(timestampFormat, str)
t, err := time.Parse(dateFormat, str)
if err == nil {
return DTimestamp{Time: t}, nil
}
t, err = time.Parse(timestampFormat, str)
if err == nil {
t = t.UTC()
return DTimestamp{Time: t}, nil
Expand Down
1 change: 1 addition & 0 deletions sql/parser/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func TestEvalExpr(t *testing.T) {
{`'hello'::char(2)`, `'he'`},
{`123::text`, `'123'`},
{`'2010-09-28'::date`, `2010-09-28`},
{`'2010-09-28'::timestamp`, `2010-09-28 00:00:00+00:00`},
{`('2010-09-28 12:00:00.1'::timestamp)::date`, `2010-09-28`},
{`'2010-09-28 12:00:00.1'::timestamp`, `2010-09-28 12:00:00.1+00:00`},
{`'2010-09-28 12:00:00.1+02:00'::timestamp`, `2010-09-28 10:00:00.1+00:00`},
Expand Down
Loading