Skip to content

Commit 1d0c23f

Browse files
kanoshioufang-xing-esql
authored andcommitted
ES|QL: Support ::date in inline cast (elastic#123460)
* Inline cast to date * Update docs/changelog/123460.yaml * New capability for `::date` casting * More tests * Update tests --------- Co-authored-by: Fang Xing <[email protected]>
1 parent 40b2de6 commit 1d0c23f

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

docs/changelog/123460.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 123460
2+
summary: "ES|QL: Support `::date` in inline cast"
3+
area: ES|QL
4+
type: enhancement
5+
issues:
6+
- 116746

docs/reference/esql/functions/kibana/inline_cast.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/type/DataType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ public enum DataType {
407407
map.put("bool", BOOLEAN);
408408
map.put("int", INTEGER);
409409
map.put("string", KEYWORD);
410+
map.put("date", DataType.DATETIME);
410411
NAME_OR_ALIAS_TO_TYPE = Collections.unmodifiableMap(map);
411412
}
412413

x-pack/plugin/esql/qa/testFixtures/src/main/resources/convert.csv-spec

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ ROW date="1985-01-01T00:00:00Z"::datetime, zero=0::datetime
7474
1985-01-01T00:00:00.000Z|1970-01-01T00:00:00.000Z
7575
;
7676

77+
convertToDate
78+
required_capability: casting_operator_for_date
79+
ROW date="1985-01-01T00:00:00Z"::date, zero=0::date
80+
;
81+
82+
date:datetime | zero:datetime
83+
1985-01-01T00:00:00.000Z|1970-01-01T00:00:00.000Z
84+
;
85+
7786
convertToVersion
7887
required_capability: casting_operator
7988
ROW ver="1.2.3"::version
@@ -351,3 +360,94 @@ z = birth_date + 3 hours + 3 minutes::time_duration, w = birth_date + (3 hours +
351360
birth_date:datetime |x:datetime |y:datetime |z:datetime |w:datetime
352361
1953-09-02T00:00:00Z |1953-09-02T03:00:00Z |1953-09-01T21:00:00Z |1953-09-02T03:03:00Z |1953-09-02T03:03:00Z
353362
;
363+
364+
convertToDatePeriodWithDateCasting
365+
required_capability: cast_string_literal_to_temporal_amount
366+
required_capability: casting_operator_for_date
367+
row x = "2024-01-01"::date
368+
| eval y = x + "3 DAYS"::date_period
369+
;
370+
371+
x:datetime |y:datetime
372+
2024-01-01 |2024-01-04
373+
;
374+
375+
convertToTimeDurationWithDateCasting
376+
required_capability: cast_string_literal_to_temporal_amount
377+
required_capability: casting_operator_for_date
378+
row x = "2024-01-01"::date
379+
| eval y = x + "3 hours"::time_duration
380+
;
381+
382+
x:datetime |y:datetime
383+
2024-01-01 |2024-01-01T03:00:00.000Z
384+
;
385+
386+
convertToDatePeriodTimeDurationWithDateCasting
387+
required_capability: cast_string_literal_to_temporal_amount
388+
required_capability: casting_operator_for_date
389+
row x = "2024-01-01"::date + "3 hours"::time_duration, y = "2024-01-01"::date - to_timeduration("3 hours"),
390+
z = "2024-01-01"::date + "3 DAYS"::date_period, w = "2024-01-01"::date - to_dateperiod("3 days")
391+
| keep x, y, z, w;
392+
393+
x:datetime |y:datetime |z:datetime |w:datetime
394+
2024-01-01T03:00:00.000Z |2023-12-31T21:00:00.000Z |2024-01-04T00:00:00.000Z |2023-12-29T00:00:00.000Z
395+
;
396+
397+
convertToDatePeriodNestedWithDateCasting
398+
required_capability: cast_string_literal_to_temporal_amount
399+
required_capability: casting_operator_for_date
400+
row x = "2024-01-01"::date
401+
| eval y = x + to_dateperiod("3 days"::date_period)
402+
;
403+
404+
x:datetime |y:datetime
405+
2024-01-01 |2024-01-04
406+
;
407+
408+
convertToTimeDurationNestedWithDateCasting
409+
required_capability: cast_string_literal_to_temporal_amount
410+
required_capability: casting_operator_for_date
411+
row x = "2024-01-01"::date
412+
| eval y = x + to_timeduration("3 hours"::time_duration)
413+
;
414+
415+
x:datetime |y:datetime
416+
2024-01-01 |2024-01-01T03:00:00.000Z
417+
;
418+
419+
testEvalWithDateCasting
420+
required_capability: casting_operator_for_date
421+
row x = "1986-06-26T00:00:00.000Z"
422+
| eval y = x::date, z = y + 10 years
423+
;
424+
425+
x:keyword | y:datetime | z:datetime
426+
1986-06-26T00:00:00.000Z | 1986-06-26T00:00:00.000Z | 1996-06-26T00:00:00.000Z
427+
;
428+
429+
430+
filteringWithDateCasting
431+
required_capability: casting_operator_for_date
432+
from employees
433+
| where birth_date < "2023-08-25T11:25:41.052Z"::date - 70 years
434+
| sort emp_no
435+
| keep emp_no, birth_date;
436+
437+
emp_no:integer | birth_date:datetime
438+
10006 | 1953-04-20T00:00:00.000Z
439+
10009 | 1952-04-19T00:00:00.000Z
440+
10019 | 1953-01-23T00:00:00.000Z
441+
10020 | 1952-12-24T00:00:00.000Z
442+
10022 | 1952-07-08T00:00:00.000Z
443+
10026 | 1953-04-03T00:00:00.000Z
444+
10035 | 1953-02-08T00:00:00.000Z
445+
10051 | 1953-07-28T00:00:00.000Z
446+
10063 | 1952-08-06T00:00:00.000Z
447+
10066 | 1952-11-13T00:00:00.000Z
448+
10067 | 1953-01-07T00:00:00.000Z
449+
10072 | 1952-05-15T00:00:00.000Z
450+
10076 | 1952-06-13T00:00:00.000Z
451+
10097 | 1952-02-27T00:00:00.000Z
452+
10100 | 1953-04-21T00:00:00.000Z
453+
;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ public enum Cap {
112112
*/
113113
CASTING_OPERATOR,
114114

115+
/**
116+
* Support for the ::date casting operator
117+
*/
118+
CASTING_OPERATOR_FOR_DATE,
119+
115120
/**
116121
* Blocks can be labelled with {@link org.elasticsearch.compute.data.Block.MvOrdering#SORTED_ASCENDING} for optimizations.
117122
*/

0 commit comments

Comments
 (0)