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

#1020: earliest/latest not supported with where command #1021

Merged
merged 7 commits into from
Jan 27, 2025
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
99 changes: 29 additions & 70 deletions docs/ppl-lang/functions/ppl-datetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,30 +411,15 @@ Return type: BOOLEAN

Example:

os> source=people | eval earliest = earliest("-1s", now()) | fields earliest | head 1
fetched rows / total rows = 1/1
+----------+
| earliest |
|----------|
| True |
+----------+

os> source=people | eval earliest = earliest("now", now()) | fields earliest | head 1
fetched rows / total rows = 1/1
+----------+
| earliest |
|----------|
| True |
+----------+

os> source=people | eval earliest = earliest("+1s", now()) | fields earliest | head 1
fetched rows / total rows = 1/1
+----------+
| earliest |
|----------|
| False |
+----------+

os> source=relative_datetime | eval timestamp = relative_timestamp(relative_string) | where earliest("now",timestamp) | sort timestamp | fields description, relative_string
fetched rows / total rows = 3/3
+--------------+-----------------+
| description | relative_string |
+--------------+-----------------+
| Now | NOW |
| Tomorrow | +D@D |
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this +D@D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

+D will given a timestamp corresponding to the current time tomorrow, while +D@D will give a timestamp corresponding to the start of tomorrow (i.e. 00:00 AM).

| In one month | +month |
+--------------+-----------------+

### `FROM_UNIXTIME`

Expand Down Expand Up @@ -560,29 +545,15 @@ Return type: BOOLEAN

Example:

os> source=people | eval latest = latest("-1s", now()) | fields latest | head 1
fetched rows / total rows = 1/1
+--------+
| latest |
|--------|
| False |
+--------+

os> source=people | eval latest = latest("now", now()) | fields latest | head 1
fetched rows / total rows = 1/1
+--------+
| latest |
|--------|
| True |
+--------+

os> source=people | eval latest = latest("+1s", now()) | fields latest | head 1
fetched rows / total rows = 1/1
+--------+
| latest |
|--------|
| True |
+--------+
os> source=relative_datetime | eval timestamp = relative_timestamp(relative_string) | where latest("now",timestamp) | sort timestamp | fields description, relative_string
fetched rows / total rows = 3/3
+---------------+-----------------+
| description | relative_string |
+---------------+-----------------+
| Two weeks ago | -2wk |
| Yesterday | -1d@d |
| Now | NOW |
+---------------+-----------------+


### `LOCALTIMESTAMP`
Expand Down Expand Up @@ -881,29 +852,17 @@ Return type: TIMESTAMP

Example:

os> source=people | eval seconds_diff = timestampdiff(SECOND, now(), relative_timestamp("now")) | fields seconds_diff | head 1
fetched rows / total rows = 1/1
+--------------+
| seconds_diff |
|--------------+
| 0 |
+--------------+

os> source=people | eval hours_diff = timestampdiff(HOUR, now(), relative_timestamp("+1h")) | fields hours_diff | head 1
fetched rows / total rows = 1/1
+------------+
| hours_diff |
|------------+
| 1 |
+------------+

os> source=people | eval day = day_of_week(relative_timestamp("@w0")) | fields day | head 1
os> source=relative_datetime | eval relative = relative_timestamp(relative_string) | sort relative | fields description, relative_string
fetched rows / total rows = 1/1
+-----+
| day |
|-----|
| 1 |
+-----+
+---------------+-----------------+
| description | relative_string |
+---------------+-----------------+
| Two weeks ago | -2wk |
| Yesterday | -1d@d |
| Now | NOW |
| Tomorrow | +D@D |
| In one month | +month |
+---------------+-----------------+

### `SECOND`

Expand Down Expand Up @@ -1227,7 +1186,7 @@ Examples::
**Description:**

Usage: TIMESTAMPDIFF(interval, start, end) returns the difference between the start and end date/times in interval units.
Arguments will be automatically converted to a ]TIMESTAMP when appropriate.
Arguments will be automatically converted to a TIMESTAMP when appropriate.
Any argument that is a STRING must be formatted as a valid TIMESTAMP.

Argument type: INTERVAL, DATE/TIMESTAMP/STRING, DATE/TIMESTAMP/STRING
Expand Down
10 changes: 10 additions & 0 deletions integ-test/script/data/relative_datetime.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{"index": {"_index": "relative_datetime"}}
{"description": "Now", "relative_string": "NOW"}
{"index": {"_index": "relative_datetime"}}
{"description": "Tomorrow", "relative_string": "+D@D"}
{"index": {"_index": "relative_datetime"}}
{"description": "In one month", "relative_string": "+month"}
{"index": {"_index": "relative_datetime"}}
{"description": "Two weeks ago", "relative_string": "-2wk"}
{"index": {"_index": "relative_datetime"}}
{"description": "Yesterday", "relative_string": "-1d@d"}
12 changes: 12 additions & 0 deletions integ-test/script/data/relative_datetime.mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"mappings": {
"properties": {
"description": {
"type": "text"
},
"relative_string": {
"type": "text"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,26 @@ trait FlintSparkSuite extends QueryTest with FlintSuite with OpenSearchSuite wit
| """.stripMargin)
}

protected def createRelativeDateTimeTable(testTable: String): Unit = {
sql(s"""
| CREATE TABLE $testTable
| (
| description STRING,
| relative_string STRING
| )
| USING $tableType $tableOptions
|""".stripMargin)

sql(s"""
| INSERT INTO $testTable
| VALUES ('Now', 'NOW'),
| ('Tomorrow', '+D@D'),
| ('In one month', '+month'),
| ('Two weeks ago', '-2wk'),
| ('Yesterday', '-1d@d')
| """.stripMargin)
}

protected def createNullableStateCountryTable(testTable: String): Unit = {
sql(s"""
| CREATE TABLE $testTable
Expand Down
Loading
Loading