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

Support many time functions #14

Merged
merged 4 commits into from
Aug 4, 2022
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
60 changes: 30 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,61 +376,61 @@ A list of ZetaSQL specifications and features supported by go-zetasqlite.
### Date functions

- [x] CURRENT_DATE
- [ ] EXTRACT
- [x] EXTRACT
- [x] DATE
- [x] DATE_ADD
- [x] DATE_SUB
- [x] DATE_DIFF
- [x] DATE_TRUNC
- [ ] DATE_FROM_UNIX_DATE
- [x] DATE_FROM_UNIX_DATE
- [ ] FORMAT_DATE
- [ ] LAST_DAY
- [x] LAST_DAY
- [x] PARSE_DATE
- [ ] UNIX_DATE
- [x] UNIX_DATE

### Datetime functions

- [x] CURRENT_DATETIME
- [ ] DATETIME
- [ ] EXTRACT
- [ ] DATETIME_ADD
- [ ] DATETIME_SUB
- [ ] DATETIME_DIFF
- [ ] DATETIME_TRUNC
- [x] DATETIME
- [x] EXTRACT
- [x] DATETIME_ADD
- [x] DATETIME_SUB
- [x] DATETIME_DIFF
- [x] DATETIME_TRUNC
- [ ] FORMAT_DATETIME
- [ ] LAST_DAY
- [x] LAST_DAY
- [x] PARSE_DATETIME

### Time functions

- [x] CURRENT_TIME
- [ ] TIME
- [ ] EXTRACT
- [ ] TIME_ADD
- [ ] TIME_SUB
- [ ] TIME_DIFF
- [ ] TIME_TRUNC
- [x] TIME
- [x] EXTRACT
- [x] TIME_ADD
- [x] TIME_SUB
- [x] TIME_DIFF
- [x] TIME_TRUNC
- [ ] FORMAT_TIME
- [x] PARSE_TIME

### Timestamp functions

- [x] CURRENT_TIMESTAMP
- [ ] EXTRACT
- [ ] STRING
- [ ] TIMESTAMP
- [ ] TIMESTAMP_ADD
- [ ] TIMESTAMP_SUB
- [ ] TIMESTAMP_DIFF
- [ ] TIMESTAMP_TRUNC
- [x] EXTRACT
- [x] STRING
- [x] TIMESTAMP
- [x] TIMESTAMP_ADD
- [x] TIMESTAMP_SUB
- [x] TIMESTAMP_DIFF
- [x] TIMESTAMP_TRUNC
- [ ] FORMAT_TIMESTAMP
- [x] PARSE_TIMESTAMP
- [ ] TIMESTAMP_SECONDS
- [ ] TIMESTAMP_MILLIS
- [ ] TIMEATAMP_MICROS
- [ ] UNIX_SECONDS
- [ ] UNIX_MILLIS
- [ ] UNIX_MICROS
- [x] TIMESTAMP_SECONDS
- [x] TIMESTAMP_MILLIS
- [x] TIMEATAMP_MICROS
- [x] UNIX_SECONDS
- [x] UNIX_MILLIS
- [x] UNIX_MICROS

### Interval functions

Expand Down
1 change: 1 addition & 0 deletions internal/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func newAnalyzerOptions() *zetasql.AnalyzerOptions {
zetasql.FeatureV13DateTimeConstructors,
zetasql.FeatureV13ExtendedDateTimeSignatures,
zetasql.FeatureV12CivilTime,
zetasql.FeatureV12WeekWithWeekday,
zetasql.FeatureIntervalType,
zetasql.FeatureGroupByRollup,
zetasql.FeatureV13NullsFirstLastInOrderBy,
Expand Down
54 changes: 54 additions & 0 deletions internal/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"regexp"
"strings"
"time"
)

func ADD(a, b Value) (Value, error) {
Expand Down Expand Up @@ -495,3 +496,56 @@ func MAKE_STRUCT(args ...Value) (Value, error) {
m: fieldMap,
}, nil
}

func EXTRACT(t time.Time, part string) (Value, error) {
switch part {
case "ISOYEAR":
year, _ := t.ISOWeek()
return IntValue(year), nil
case "YEAR":
return IntValue(t.Year()), nil
case "MONTH":
return IntValue(t.Month()), nil
case "ISOWEEK":
_, week := t.ISOWeek()
return IntValue(week), nil
case "WEEK":
_, week := t.AddDate(0, 0, -int(t.Weekday())).ISOWeek()
return IntValue(week), nil
case "DAY":
return IntValue(t.Day()), nil
case "DAYOFYEAR":
return IntValue(t.YearDay()), nil
case "DAYOFWEEK":
return IntValue(int(t.Weekday()) + 1), nil
case "QUARTER":
day := t.YearDay()
const quarterDays = 91
switch {
case day <= quarterDays:
return IntValue(1), nil
case day <= quarterDays*2:
return IntValue(2), nil
case day <= quarterDays*3:
return IntValue(3), nil
}
return IntValue(4), nil
case "HOUR":
return IntValue(t.Hour()), nil
case "MINUTE":
return IntValue(t.Minute()), nil
case "SECOND":
return IntValue(t.Second()), nil
case "MILLISECOND":
return IntValue(t.Nanosecond() / int(time.Millisecond)), nil
case "MICROSECOND":
return IntValue(t.Nanosecond() / int(time.Microsecond)), nil
case "DATE":
return DateValue(t), nil
case "DATETIME":
return DatetimeValue(t), nil
case "TIME":
return TimeValue(t), nil
}
return nil, fmt.Errorf("failed to extract: undefined part %s", part)
}
Loading