Skip to content

Commit 9945953

Browse files
committed
Update/add documentation
Signed-off-by: currantw <taylor.curran@improving.com>
1 parent d7ce254 commit 9945953

File tree

3 files changed

+98
-11
lines changed

3 files changed

+98
-11
lines changed

docs/ppl-lang/PPL-Example-Commands.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,23 @@ _- **Limitation: another command usage of (relation) subquery is in `appendcols`
499499
- `source = table | eval cdate = CAST('2012-08-07' as date), ctime = cast('2012-08-07T08:07:06' as timestamp) | fields cdate, ctime`
500500
- `source = table | eval chained_cast = cast(cast("true" as boolean) as integer) | fields chained_cast`
501501

502+
### **Relative Time Functions**
503+
502504
#### **relative_timestamp**
503-
[See additional function details](functions/ppl-datetime#RELATIVE_TIMESTAMP)
505+
[See additional function details](functions/ppl-datetime#relative_timestamp)
504506
- `source = table | eval one_hour_ago = relative_timestamp("-1h") | where timestamp < one_hour_ago`
505507
- `source = table | eval start_of_today = relative_timestamp("@d") | where timestamp > start_of_today`
506508
- `source = table | eval last_saturday = relative_timestamp("-1d@w6") | where timestamp >= last_saturday`
509+
510+
#### **earliest**
511+
[See additional function details](functions/ppl-datetime#earliest)
512+
- `source = table | where earliest("-1wk", timestamp)`
513+
- `source = table | where earliest("@qtr", timestamp)`
514+
- `source = table | where earliest("-2y@q", timestamp)`
515+
516+
#### **latest**
517+
[See additional function details](functions/ppl-datetime#latest)
518+
- `source = table | where latest("-60m", timestamp)`
519+
- `source = table | where latest("@year", timestamp)`
520+
- `source = table | where latest("-day@w1", timestamp)`
507521
---

docs/ppl-lang/functions/ppl-datetime.md

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,44 @@ Example:
397397
+-------------------------------+
398398

399399

400+
### `EARLIEST`
401+
402+
**Description:**
403+
404+
**Usage:** earliest(string, timestamp) returns whether the timestamp defined by the given relative string is earlier
405+
than or at the same time as the specified timestamp.
406+
407+
Argument type: STRING, TIMESTAMP
408+
409+
Return type: BOOLEAN
410+
411+
Example:
412+
413+
os> source=people | eval earliest = earliest("-1s", now()) | fields earliest | head 1
414+
fetched rows / total rows = 1/1
415+
+----------+
416+
| earliest |
417+
|----------|
418+
| True |
419+
+----------+
420+
421+
os> source=people | eval earliest = earliest("now", now()) | fields earliest | head 1
422+
fetched rows / total rows = 1/1
423+
+----------+
424+
| earliest |
425+
|----------|
426+
| True |
427+
+----------+
428+
429+
os> source=people | eval earliest = earliest("+1s", now()) | fields earliest | head 1
430+
fetched rows / total rows = 1/1
431+
+----------+
432+
| earliest |
433+
|----------|
434+
| False |
435+
+----------+
436+
437+
400438
### `FROM_UNIXTIME`
401439

402440
**Description:**
@@ -507,6 +545,44 @@ Example:
507545
+--------------------------+
508546

509547

548+
### `LATEST`
549+
550+
**Description:**
551+
552+
**Usage:** latest(string, timestamp) returns whether the timestamp defined by the given relative string is later
553+
than or at the same time as the specified timestamp. See [relative_timestamp](#relative_timestamp) for more details.
554+
555+
Argument type: STRING, TIMESTAMP
556+
557+
Return type: BOOLEAN
558+
559+
Example:
560+
561+
os> source=people | eval latest = latest("-1s", now()) | fields latest | head 1
562+
fetched rows / total rows = 1/1
563+
+--------+
564+
| latest |
565+
|--------|
566+
| False |
567+
+--------+
568+
569+
os> source=people | eval latest = latest("now", now()) | fields latest | head 1
570+
fetched rows / total rows = 1/1
571+
+--------+
572+
| latest |
573+
|--------|
574+
| True |
575+
+--------+
576+
577+
os> source=people | eval latest = latest("+1s", now()) | fields latest | head 1
578+
fetched rows / total rows = 1/1
579+
+--------+
580+
| latest |
581+
|--------|
582+
| True |
583+
+--------+
584+
585+
510586
### `LOCALTIMESTAMP`
511587

512588
**Description:**
@@ -738,7 +814,7 @@ Example:
738814
**Description:**
739815

740816

741-
**Usage:** relative_timestamp(str) returns a relative timestamp corresponding to the given relative string and the
817+
**Usage:** relative_timestamp(string) returns a relative timestamp corresponding to the given relative string and the
742818
current timestamp at the time of query execution.
743819

744820
The relative timestamp string has syntax `[+|-]<offset_time_integer><offset_time_unit>@<snap_time_unit>`, and is
@@ -750,9 +826,12 @@ made up of two optional components.
750826
specified), and rounds the time <i>down</i> to the start of the specified time unit. For example, `@wk` is the start
751827
of the current week (Sunday is considered to be the first day of the week).
752828

753-
The special relative timestamp string `now`, corresponding to the current timestamp, is also supported. The current
754-
timestamp is determined once at the start of query execution, and is used for all relative timestamp calculations for
755-
that query.
829+
The special relative timestamp string `now`, corresponding to the current timestamp, is also supported.
830+
831+
The current timestamp is determined once at the start of query execution, and is used for all relative timestamp
832+
calculations for that query. The Spark session time zone (`spark.sql.session.timeZone`) is used for determining
833+
relative timestamps, and accounts for changes in the time zone offset (e.g. daylight savings time); as a result, adding
834+
one day (`+1d`) is not the same as adding twenty-four hours (`+24h`).
756835

757836
The relative timestamp string is case-insensitive.
758837

ppl-spark-integration/src/main/java/org/opensearch/sql/expression/function/SerializableUdf.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,7 @@ public Instant apply(String relativeString, Object currentTimestamp, String zone
227227
? ((Timestamp) currentTimestamp).toInstant()
228228
: (Instant) currentTimestamp;
229229

230-
/// The Spark session time zone (`spark.sql.session.timeZone`)
231-
/// is used, which may be different from the system time zone.
232230
ZoneId zoneId = ZoneId.of(zoneIdString);
233-
234-
/// Relative time calculations are performed using [ZonedDateTime] because offsets (e.g. one hour ago)
235-
/// need to account for changes in the time zone offset (e.g. daylight savings time), while snaps (e.g.
236-
/// start of previous Wednesday) need to account for the local date time.
237231
ZonedDateTime currentDateTime = ZonedDateTime.ofInstant(currentInstant, zoneId);
238232
ZonedDateTime relativeDateTime = TimeUtils.getRelativeZonedDateTime(relativeString, currentDateTime);
239233

0 commit comments

Comments
 (0)