Skip to content
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 book/src/command/db.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ This data has the type signature:
```
{
author: string,
date: time,
date: timestamp,
message: string,
meta: <any>
}
Expand Down
2 changes: 1 addition & 1 deletion book/src/super-sql/expressions/comparisons.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Values of type [net](../types/network.md) are ordered by address family
(IPv4 before IPv6), then by network address, and then by prefix length.

Any other comparison is a type mismatch, e.g., a number and a string, a number
and a `time` or `duration`, or an `ip` and a `net`. The compiler reports a
and a `timestamp` or `duration`, or an `ip` and a `net`. The compiler reports a
type mismatch as an error when it knows the types of the operands. Otherwise,
the result of the comparison at runtime is an error value.

Expand Down
62 changes: 57 additions & 5 deletions book/src/super-sql/functions/time/bucket.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
# bucket

quantize a time or duration value into buckets of equal time spans
quantize a timestamp or duration value into buckets of equal time spans

## Synopsis

```
bucket(val: timestamp, span: duration) -> time
bucket(val: timestamp, span: duration) -> timestamp
bucket(val: duration, span: duration) -> duration
```

## Description

The `bucket` function quantizes a time or duration `val`
into buckets that are equally spaced as specified by `span`
where the bucket boundary aligns with 0.
The `bucket` function quantizes a timestamp or duration `val`
into buckets that are equally spaced as specified by `span`.
Bucket boundaries are multiples of `span` counted from zero, which for a
timestamp is the Unix epoch, `1970-01-01T00:00:00Z`. So a `1h` or `1d`
span starts each bucket on the hour or at midnight UTC, but a `7d` span
starts each bucket on a Thursday, the day of the week of the epoch.
If `span` is zero, `val` is returned unchanged.

## Examples

Expand All @@ -31,3 +35,51 @@ values bucket(this::timestamp, 1h)
2020-05-26T15:00:00Z
2020-05-26T15:00:00Z
```

---

_Count values in each hour_

```mdtest-spq
# spq
count() by hour:=bucket(ts, 1h) | sort hour
# input
{ts:2020-05-26T15:27:47Z}
{ts:2020-05-26T15:59:01Z}
{ts:2020-05-26T16:02:13Z}
# expected output
{hour:2020-05-26T15:00:00Z,count:2}
{hour:2020-05-26T16:00:00Z,count:1}
```

---

_Weekly buckets start on Thursdays_

```mdtest-spq {data-layout="stacked"}
# spq
values {ts, day:strftime("%A", ts), week:bucket(ts, 7d), week_day:strftime("%A", bucket(ts, 7d))}
# input
{ts:2024-07-28T23:00:00Z}
{ts:2024-07-30T10:00:00Z}
# expected output
{ts:2024-07-28T23:00:00Z,day:"Sunday",week:2024-07-25T00:00:00Z,week_day:"Thursday"}
{ts:2024-07-30T10:00:00Z,day:"Tuesday",week:2024-07-25T00:00:00Z,week_day:"Thursday"}
```

---

_Bucket durations into 15-minute spans_

```mdtest-spq
# spq
values bucket(this, 15m)
# input
1h7m
22m30s
14m59s
# expected output
1h
15m
0s
```
26 changes: 22 additions & 4 deletions book/src/super-sql/functions/time/date_part.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# date_part

return a specified part of a time value
return a specified part of a timestamp

## Synopsis

```
date_part(part: string, ts: time) -> int64
date_part(part: string, ts: timestamp) -> int64
EXTRACT(part FROM ts: timestamp) -> int64
```

## Description

The `date_part` function accepts a [`string`](../../types/string.md) argument `part` and a [`time`](../../types/time.md) value `ts` and
The `date_part` function accepts a [`string`](../../types/string.md) argument `part` and a [`timestamp`](../../types/time.md) value `ts` and
returns an [`int64`](../../types/numbers.md) representing the part of the date requested.

Valid values for `part` are:
Expand All @@ -27,11 +28,15 @@ Valid values for `part` are:
|`"second"` |The seconds field (0-59) |
|`"year"` |The year field |

The SQL syntax `EXTRACT(part FROM ts)` is also supported, where `part` may be
an identifier (e.g., `YEAR`) or a single-quoted string (e.g., `'year'`) and
isn't case-sensitive.

## Examples

---

_Extract all individual parts from a `time` value_
_Extract all individual parts from a `timestamp` value_

```mdtest-spq
# spq
Expand All @@ -57,3 +62,16 @@ values date_part("day", this),
40
2001
```

---

_Extract parts with the SQL `EXTRACT` syntax_

```mdtest-spq
# spq
SELECT EXTRACT(YEAR FROM ts) AS year, EXTRACT(MONTH FROM ts) AS month
# input
{ts:2001-10-09T01:46:40.123456Z}
# expected output
{year:2001,month:10}
```
13 changes: 6 additions & 7 deletions book/src/super-sql/functions/time/now.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ the current time
## Synopsis

```
now() -> time
now() -> timestamp
```

## Description

The `now` function takes no arguments and returns the current UTC time as a value of type `time`.
The `now` function takes no arguments and returns the current UTC time as a value of type `timestamp`.

This is useful to timestamp events in a data pipeline, e.g.,
when generating errors that are marked with their time of occurrence:
```
switch (
...
switch
case ... ( ... )
default ( values error({ts:now(), ...}) )
)
```

## Examples
Expand All @@ -27,7 +26,7 @@ _Running this command_
```
super -s -c 'values now()'
```
_produces this value (as of the last time this document was updated)_
_produces this value (as of the last time this document was updated)_
```
2025-08-27T02:18:53.568913Z
2026-11-23T22:20:16.539417Z
```
38 changes: 28 additions & 10 deletions book/src/super-sql/functions/time/strftime.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# strftime

format time values
format timestamp values

## Synopsis

```
strftime(format: string, t: time) -> string
strftime(format: string, t: timestamp) -> string
```

## Description

The `strftime` function returns a string representation of time `t`
The `strftime` function returns a string representation of timestamp `t`
as specified by the provided string `format`. `format` is a string
containing format directives that dictate how the time string is
formatted.
Expand All @@ -24,16 +24,17 @@ These directives are supported:
| %B | Month as full name | January, February, ..., December |
| %b | Month as abbreviated name | Jan, Feb, ..., Dec |
| %C | Century number (year / 100) as a 2-digit integer | 20 |
| %c | Locale's appropriate date and time representation | Tue Jul 30 14:30:15 2024 |
| %D | Equivalent to `%m/%d/%y` | 7/30/24 |
| %c | Date and time representation | Tue Jul 30 14:30:15 2024 |
| %D | Equivalent to `%m/%d/%y` | 07/30/24 |
| %d | Day of the month as a zero-padded decimal number | 01, 02, ..., 31 |
| %e | Day of the month as a decimal number (1-31); single digits are preceded by a blank | 1, 2, ..., 31 |
| %F | Equivalent to `%Y-%m-%d` | 2024-07-30 |
| %H | Hour (24-hour clock) as a zero-padded decimal number | 00, 01, ..., 23 |
| %I | Hour (12-hour clock) as a zero-padded decimal number | 00, 01, ..., 12 |
| %h | Equivalent to `%b` | Jan, Feb, ..., Dec |
| %I | Hour (12-hour clock) as a zero-padded decimal number | 01, 02, ..., 12 |
| %j | Day of the year as a zero-padded decimal number | 001, 002, ..., 366 |
| %k | Hour (24-hour clock) as a decimal number; single digits are preceded by a blank | 0, 1, ..., 23 |
| %l | Hour (12-hour clock) as a decimal number; single digits are preceded by a blank | 0, 1, ..., 12 |
| %l | Hour (12-hour clock) as a decimal number; single digits are preceded by a blank | 1, 2, ..., 12 |
| %M | Minute as a zero-padded decimal number | 00, 01, ..., 59 |
| %m | Month as a zero-padded decimal number | 01, 02, ..., 12 |
| %n | Newline character | \n |
Expand All @@ -45,18 +46,22 @@ These directives are supported:
| %t | Tab character | \t |
| %U | Week number of the year (Sunday as the first day of the week) | 00, 01, ..., 53 |
| %u | Weekday as a decimal number, range 1 to 7, with Monday being 1 | 1, 2, ..., 7 |
| %V | Week number of the year (Monday as the first day of the week) as a decimal number (01-53) | 01, 02, ..., 53 |
| %V | ISO 8601 week number of the year (Monday as the first day of the week) as a decimal number (01-53) | 01, 02, ..., 53 |
| %v | Equivalent to `%e-%b-%Y` | 31-Jul-2024 |
| %W | Week number of the year (Monday as the first day of the week) | 00, 01, ..., 53 |
| %w | Weekday as a decimal number, range 0 to 6, with Sunday being 0 | 0, 1, ..., 6 |
| %X | Locale's appropriate time representation | 14:30:15 |
| %x | Locale's appropriate date representation | 07/30/24 |
| %X | Time representation | 14:30:15 |
| %x | Date representation | 07/30/24 |
| %Y | Year with century as a decimal number | 2024 |
| %y | Year without century as a decimal number | 24, 23 |
| %Z | Timezone name | UTC |
| %z | +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from UTC) | +0000 |
| %% | A literal '%' character | % |

The timestamp is always formatted in UTC, and `%c`, `%x`, and `%X` always
use the formats shown, whatever the locale. There is no directive for
fractional seconds.

## Examples

---
Expand Down Expand Up @@ -84,3 +89,16 @@ strftime("%d/%m/%Y", this)
# expected output
"30/07/2024"
```

---

_Print a time of day with a 12-hour clock_

```mdtest-spq
# spq
strftime("%I:%M %p", this)
# input
2024-07-30T20:05:15.118252Z
# expected output
"08:05 PM"
```
2 changes: 1 addition & 1 deletion book/src/super-sql/operators/infer.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ are uniformly coercible to the candidate type, which may be one of:
* [float64](../types/numbers.md#floating-point),
* [ip](../types/network.md),
* [net](../types/network.md),
* [time](../types/time.md), or
* [timestamp](../types/time.md), or
* [bool](../types/bool.md).

`int64` inference takes precedence over `float64`. All of the other candidate types
Expand Down
2 changes: 1 addition & 1 deletion book/src/super-sql/operators/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ match for the given value as well as a string search for the term exactly
as it appears as typed. Such values include:
* integers,
* floating point numbers,
* time values,
* timestamps,
* durations,
* IP addresses,
* networks,
Expand Down
2 changes: 1 addition & 1 deletion book/src/super-sql/operators/sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ to the values present.
The heuristic examines the first input record and finds the first field in
left-to-right order that is an integer, or if no integer field is found,
the first field that is floating point. If no such numeric field is found, `sort` finds
the first field in left-to-right order that is _not_ of the `time` data type.
the first field in left-to-right order that is _not_ of the `timestamp` data type.
Note that there are some cases (such as the output of a grouped aggregation performed on heterogeneous data) where the first input record to `sort`
may vary even when the same query is executed repeatedly against the same data.
If you require a query to show deterministic output on repeated execution,
Expand Down
4 changes: 2 additions & 2 deletions book/src/super-sql/statements/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ any values the statement itself produces. A status record has the form
{
kind: string,
stmt: string,
start: time,
end: time,
start: timestamp,
end: timestamp,
info: <info>,
error: string
}
Expand Down
18 changes: 8 additions & 10 deletions book/src/super-sql/types/time.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# Date/Times

>[!WARNING]
> These data types are going to change in a forthcoming release of SuperSQL.

The `time` type represents an unsigned 64-bit number of nanoseconds since epoch.
The `timestamp` type represents a signed 64-bit number of nanoseconds since the
Unix epoch, i.e., an instant in UTC.

The `duration` type represents a signed 64-bit number of nanoseconds.

For backward compatibility with SQL, `INTERVAL` is a syntactic alias for type `duration`.

These data types are incompatible with SQL data and time types. A future version
of SuperSQL will change the `time` type to be SQL compatible and add support for other
SQL date/time and interval types. At that time, detailed syntax and semantics
for these types will be documented here.
>[!NOTE]
> Other SQL date and time types (e.g., `date`, `time` for a time of day, and
> `interval`) are reserved for a future version of SuperSQL. At that time,
> detailed syntax and semantics for these types will be documented here.
> Until then, a SQL `DATE` literal such as `DATE '2020-01-01'`, or a SQL cast
> like `CAST(x AS DATE)`, produces a `timestamp` at midnight UTC.
6 changes: 3 additions & 3 deletions book/src/tutorials/jq.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ which utilizes a [SUP type decorator](../formats/sup.md#22-type-decorators),
in this case,
to clarify its specific type of integer as unsigned 8 bits.

`v4` has type `time` and `v5` type `duration`.
`v4` has type `timestamp` and `v5` type `duration`.

`v6` is type `ip` and `v7` type `net`.

Expand Down Expand Up @@ -806,7 +806,7 @@ which produces this string:
```mdtest-output
"2019-11-11T19:50:46Z"
```
Since the super data model has a native `time` type and we might want to do native date comparisons
Since the super data model has a native `timestamp` type and we might want to do native date comparisons
on these time fields, we can easily translate the string to a time with a cast, e.g.,
```mdtest-command dir=book/src/tutorials
super -s -c 'unnest this | head 1 | values created_at::timestamp' prs.json
Expand Down Expand Up @@ -920,7 +920,7 @@ which now gives:
{type:<string>,fields:|["url","body","state","title","node_id","diff_url","html_url","issue_url","patch_url","commits_url","comments_url","statuses_url","merge_commit_sha","author_association","review_comment_url","review_comments_url"]|}
{type:<null>,fields:|["assignee","milestone","auto_merge","active_lock_reason"]|}
```
and we can see that the date fields are correctly typed as type `time`!
and we can see that the date fields are correctly typed as type `timestamp`!

>[!NOTE]
> We sorted the output values here using the
Expand Down
Loading