Skip to content

Commit afdd0b8

Browse files
[ES|QL] Consider min/max from predicates when transform date_trunc/bucket to round_to option 2 (#132143)
* consider min/max from predicates when transform date_trunc/bucket into round_to
1 parent 238c1d1 commit afdd0b8

File tree

12 files changed

+852
-275
lines changed

12 files changed

+852
-275
lines changed

docs/changelog/131341.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 131341
2+
summary: Consider min/max from predicates when transform date_trunc/bucket to `round_to`
3+
area: ES|QL
4+
type: enhancement
5+
issues: []

docs/changelog/132143.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 132143
2+
summary: Consider min/max from predicates when transform date_trunc/bucket to `round_to`
3+
option 2
4+
area: ES|QL
5+
type: enhancement
6+
issues: []

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,3 +884,108 @@ c:long | b:datetime | yr:datetime
884884
9 | 1989-01-01T00:00:00.000Z | 1988-01-01T00:00:00.000Z
885885
13 | 1990-01-01T00:00:00.000Z | 1989-01-01T00:00:00.000Z
886886
;
887+
888+
bucketYearInAggWithGTOutOfRange#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
889+
FROM employees
890+
| WHERE hire_date >= "2000-01-01T00:00:00Z"
891+
| STATS COUNT(*) by bucket = BUCKET(hire_date, 1 month)
892+
| SORT bucket;
893+
894+
COUNT(*):long | bucket:date
895+
;
896+
897+
bucketYearInAggWithLTOutOfRange#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
898+
FROM employees
899+
| WHERE hire_date <= "1980-01-01T00:00:00Z"
900+
| STATS COUNT(*) by bucket = BUCKET(hire_date, 1 year)
901+
| SORT bucket;
902+
903+
COUNT(*):long | bucket:date
904+
;
905+
906+
bucketYearInAggWithGTLTOutOfRange#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
907+
FROM employees
908+
| WHERE hire_date <= "1980-01-01T00:00:00Z" and hire_date >= "1970-01-01"
909+
| STATS COUNT(*) by bucket = BUCKET(hire_date, 1 week)
910+
| SORT bucket;
911+
912+
COUNT(*):long | bucket:date
913+
;
914+
915+
bucketYearInAggWithEQOutOfRange#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
916+
FROM employees
917+
| WHERE hire_date == "1980-01-01T00:00:00Z"
918+
| STATS COUNT(*) by bucket = BUCKET(hire_date, 1 hour)
919+
| SORT bucket;
920+
921+
COUNT(*):long | bucket:date
922+
;
923+
924+
bucketWithRename#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
925+
FROM employees
926+
| RENAME hire_date as x, x as y
927+
| WHERE y >= "1980-01-01T00:00:00Z"
928+
| STATS COUNT(*) by bucket = BUCKET(y, 1 hour)
929+
| SORT bucket
930+
| LIMIT 5
931+
;
932+
933+
COUNT(*):long | bucket:datetime
934+
1 | 1985-02-18T00:00:00.000Z
935+
1 | 1985-02-24T00:00:00.000Z
936+
1 | 1985-05-13T00:00:00.000Z
937+
1 | 1985-07-09T00:00:00.000Z
938+
1 | 1985-09-17T00:00:00.000Z
939+
;
940+
941+
bucketWithEval#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
942+
FROM employees
943+
| EVAL x = hire_date
944+
| WHERE x >= "1980-01-01T00:00:00Z" and hire_date <= "1990-01-01T00:00:00Z"
945+
| STATS COUNT(*) by bucket = BUCKET(x, 1 hour)
946+
| SORT bucket
947+
| LIMIT 5
948+
;
949+
950+
COUNT(*):long | bucket:datetime
951+
1 | 1985-02-18T00:00:00.000Z
952+
1 | 1985-02-24T00:00:00.000Z
953+
1 | 1985-05-13T00:00:00.000Z
954+
1 | 1985-07-09T00:00:00.000Z
955+
1 | 1985-09-17T00:00:00.000Z
956+
;
957+
958+
bucketWithEvalExpression#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
959+
FROM employees
960+
| EVAL x = hire_date + 1 year
961+
| WHERE x >= "1980-01-01T00:00:00Z"
962+
| STATS COUNT(*) by bucket = BUCKET(x, 1 hour)
963+
| SORT bucket
964+
| LIMIT 5
965+
;
966+
967+
COUNT(*):long | bucket:datetime
968+
1 | 1986-02-18T00:00:00.000Z
969+
1 | 1986-02-24T00:00:00.000Z
970+
1 | 1986-05-13T00:00:00.000Z
971+
1 | 1986-07-09T00:00:00.000Z
972+
1 | 1986-09-17T00:00:00.000Z
973+
;
974+
975+
bucketWithRenameEvalExpression#[skip:-8.13.99, reason:BUCKET renamed in 8.14]
976+
FROM employees
977+
| EVAL x = hire_date + 1 year
978+
| RENAME x as y
979+
| WHERE y >= "1980-01-01T00:00:00Z"
980+
| STATS COUNT(*) by bucket = BUCKET(y, 1 hour)
981+
| SORT bucket
982+
| LIMIT 5
983+
;
984+
985+
COUNT(*):long | bucket:datetime
986+
1 | 1986-02-18T00:00:00.000Z
987+
1 | 1986-02-24T00:00:00.000Z
988+
1 | 1986-05-13T00:00:00.000Z
989+
1 | 1986-07-09T00:00:00.000Z
990+
1 | 1986-09-17T00:00:00.000Z
991+
;

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

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,4 +1581,258 @@ x:date | y:date
15811581

15821582
;
15831583

1584+
evalDateTruncMonthIntervalWithGTEInRange
1585+
FROM employees
1586+
| SORT hire_date
1587+
| WHERE hire_date >= "1990-01-01"
1588+
| EVAL x = date_trunc(1 month, hire_date)
1589+
| KEEP emp_no, hire_date, x
1590+
| LIMIT 5;
1591+
1592+
emp_no:integer | hire_date:date | x:date
1593+
10082 | 1990-01-03T00:00:00.000Z | 1990-01-01T00:00:00.000Z
1594+
10096 | 1990-01-14T00:00:00.000Z | 1990-01-01T00:00:00.000Z
1595+
10011 | 1990-01-22T00:00:00.000Z | 1990-01-01T00:00:00.000Z
1596+
10056 | 1990-02-01T00:00:00.000Z | 1990-02-01T00:00:00.000Z
1597+
10086 | 1990-02-16T00:00:00.000Z | 1990-02-01T00:00:00.000Z
1598+
;
1599+
1600+
evalDateTruncHoursIntervalWithLTEInRange
1601+
FROM employees
1602+
| SORT hire_date desc
1603+
| WHERE hire_date <= "1990-01-01"
1604+
| EVAL x = date_trunc(240 hours, hire_date)
1605+
| KEEP emp_no, hire_date, x
1606+
| LIMIT 5;
1607+
1608+
emp_no:integer | hire_date:date | x:date
1609+
10023 | 1989-12-17T00:00:00.000Z | 1989-12-17T00:00:00.000Z
1610+
10041 | 1989-11-12T00:00:00.000Z | 1989-11-07T00:00:00.000Z
1611+
10069 | 1989-11-05T00:00:00.000Z | 1989-10-28T00:00:00.000Z
1612+
10092 | 1989-09-22T00:00:00.000Z | 1989-09-18T00:00:00.000Z
1613+
10038 | 1989-09-20T00:00:00.000Z | 1989-09-18T00:00:00.000Z
1614+
;
1615+
1616+
evalDateTruncWeeklyIntervalWithLTGTInRange
1617+
from employees
1618+
| SORT hire_date
1619+
| WHERE hire_date > "1986-01-01" and hire_date < "1988-01-01"
1620+
| EVAL x = date_trunc(1 week, hire_date)
1621+
| KEEP emp_no, hire_date, x
1622+
| LIMIT 5;
1623+
1624+
emp_no:integer | hire_date:date | x:date
1625+
10053 | 1986-02-04T00:00:00.000Z | 1986-02-03T00:00:00.000Z
1626+
10066 | 1986-02-26T00:00:00.000Z | 1986-02-24T00:00:00.000Z
1627+
10090 | 1986-03-14T00:00:00.000Z | 1986-03-10T00:00:00.000Z
1628+
10079 | 1986-03-27T00:00:00.000Z | 1986-03-24T00:00:00.000Z
1629+
10001 | 1986-06-26T00:00:00.000Z | 1986-06-23T00:00:00.000Z
1630+
;
1631+
1632+
evalDateTruncQuarterlyIntervalWithGTInRange
1633+
from employees
1634+
| SORT hire_date
1635+
| WHERE hire_date > "1980-01-01"
1636+
| EVAL x = date_trunc(3 month, hire_date)
1637+
| KEEP emp_no, hire_date, x
1638+
| LIMIT 5;
1639+
1640+
emp_no:integer | hire_date:date | x:date
1641+
10009 | 1985-02-18T00:00:00.000Z | 1985-01-01T00:00:00.000Z
1642+
10048 | 1985-02-24T00:00:00.000Z | 1985-01-01T00:00:00.000Z
1643+
10098 | 1985-05-13T00:00:00.000Z | 1985-04-01T00:00:00.000Z
1644+
10076 | 1985-07-09T00:00:00.000Z | 1985-07-01T00:00:00.000Z
1645+
10061 | 1985-09-17T00:00:00.000Z | 1985-07-01T00:00:00.000Z
1646+
;
1647+
1648+
dateTruncGroupingYearIntervalWithLTInRange
1649+
from employees
1650+
| WHERE hire_date < "2025-01-01"
1651+
| EVAL y = date_trunc(1 year, hire_date)
1652+
| stats c = count(emp_no) by y
1653+
| SORT y
1654+
| KEEP y, c
1655+
| LIMIT 5;
1656+
1657+
y:date | c:long
1658+
1985-01-01T00:00:00.000Z | 11
1659+
1986-01-01T00:00:00.000Z | 11
1660+
1987-01-01T00:00:00.000Z | 15
1661+
1988-01-01T00:00:00.000Z | 9
1662+
1989-01-01T00:00:00.000Z | 13
1663+
;
1664+
1665+
dateTruncGroupingYearIntervalWithLTOutOfRange
1666+
from employees
1667+
| WHERE hire_date < "1980-01-01"
1668+
| EVAL y = date_trunc(1 year, hire_date)
1669+
| stats c = count(emp_no) by y
1670+
| SORT y
1671+
| KEEP y, c
1672+
| LIMIT 5;
1673+
1674+
y:date | c:long
1675+
;
1676+
1677+
dateTruncGroupingYearIntervalWithGTOutOfRange
1678+
from employees
1679+
| WHERE hire_date > "2000-01-01"
1680+
| EVAL y = date_trunc(1 year, hire_date)
1681+
| stats c = count(emp_no) by y
1682+
| SORT y
1683+
| KEEP y, c
1684+
| LIMIT 5;
1685+
1686+
y:date | c:long
1687+
;
15841688

1689+
dateTruncGroupingMonthIntervalWithLTGTInRange
1690+
from employees
1691+
| WHERE hire_date > "1987-01-01" and hire_date < "1988-01-01"
1692+
| EVAL y = date_trunc(1 month, hire_date)
1693+
| stats c = count(emp_no) by y
1694+
| SORT y
1695+
| KEEP y, c
1696+
| LIMIT 5;
1697+
1698+
y:date | c:long
1699+
1987-03-01T00:00:00.000Z | 5
1700+
1987-04-01T00:00:00.000Z | 3
1701+
1987-05-01T00:00:00.000Z | 1
1702+
1987-07-01T00:00:00.000Z | 1
1703+
1987-08-01T00:00:00.000Z | 2
1704+
;
1705+
1706+
dateTruncGroupingDayIntervalWithEQInRange
1707+
from employees
1708+
| WHERE hire_date == "1988-02-10"
1709+
| EVAL y = date_trunc(1 day, hire_date)
1710+
| stats c = count(emp_no) by y
1711+
| SORT y
1712+
| KEEP y, c
1713+
| LIMIT 5;
1714+
1715+
y:date | c:long
1716+
1988-02-10T00:00:00.000Z | 1
1717+
;
1718+
1719+
dateTruncGroupingDayIntervalWithEQOutOfRange
1720+
from employees
1721+
| WHERE hire_date == "2025-01-01"
1722+
| EVAL y = date_trunc(1 day, hire_date)
1723+
| stats c = count(emp_no) by y
1724+
| SORT y
1725+
| KEEP y, c
1726+
| LIMIT 5;
1727+
1728+
y:date | c:long
1729+
;
1730+
1731+
dateTruncWithEval
1732+
from employees
1733+
| EVAL x = hire_date
1734+
| WHERE x > "1987-01-01" and hire_date < "1988-01-01"
1735+
| EVAL y = date_trunc(1 month, x)
1736+
| STATS c = count(emp_no) by y
1737+
| SORT y
1738+
| KEEP y, c
1739+
| LIMIT 5;
1740+
1741+
y:date | c:long
1742+
1987-03-01T00:00:00.000Z | 5
1743+
1987-04-01T00:00:00.000Z | 3
1744+
1987-05-01T00:00:00.000Z | 1
1745+
1987-07-01T00:00:00.000Z | 1
1746+
1987-08-01T00:00:00.000Z | 2
1747+
;
1748+
1749+
dateTruncWithEvalExpression
1750+
from employees
1751+
| EVAL x = hire_date + 1 year
1752+
| WHERE x > "1987-01-01" and x < "1988-01-01"
1753+
| EVAL y = date_trunc(1 month, x)
1754+
| STATS c = count(emp_no) by y
1755+
| SORT y
1756+
| KEEP y, c
1757+
| LIMIT 5;
1758+
1759+
y:date | c:long
1760+
1987-02-01T00:00:00.000Z | 2
1761+
1987-03-01T00:00:00.000Z | 2
1762+
1987-06-01T00:00:00.000Z | 1
1763+
1987-07-01T00:00:00.000Z | 1
1764+
1987-08-01T00:00:00.000Z | 2
1765+
;
1766+
1767+
dateTruncWithRename
1768+
FROM employees
1769+
| RENAME hire_date as x
1770+
| WHERE x > "1987-01-01" and x < "1988-01-01"
1771+
| EVAL y = date_trunc(1 month, x)
1772+
| STATS c = count(emp_no) by y
1773+
| SORT y
1774+
| KEEP y, c
1775+
| LIMIT 5;
1776+
1777+
y:date | c:long
1778+
1987-03-01T00:00:00.000Z | 5
1779+
1987-04-01T00:00:00.000Z | 3
1780+
1987-05-01T00:00:00.000Z | 1
1781+
1987-07-01T00:00:00.000Z | 1
1782+
1987-08-01T00:00:00.000Z | 2
1783+
;
1784+
1785+
dateTruncWithRenameChain
1786+
FROM employees
1787+
| RENAME hire_date as a, a as x
1788+
| WHERE x > "1987-01-01" and x < "1988-01-01"
1789+
| EVAL y = date_trunc(1 month, x)
1790+
| STATS c = count(emp_no) by y
1791+
| SORT y
1792+
| KEEP y, c
1793+
| LIMIT 5;
1794+
1795+
y:date | c:long
1796+
1987-03-01T00:00:00.000Z | 5
1797+
1987-04-01T00:00:00.000Z | 3
1798+
1987-05-01T00:00:00.000Z | 1
1799+
1987-07-01T00:00:00.000Z | 1
1800+
1987-08-01T00:00:00.000Z | 2
1801+
;
1802+
1803+
dateTruncWithRenameBack
1804+
FROM employees
1805+
| RENAME hire_date as x, x as hire_date
1806+
| WHERE hire_date > "1987-01-01" and hire_date < "1988-01-01"
1807+
| EVAL y = date_trunc(1 month, hire_date)
1808+
| STATS c = count(emp_no) by y
1809+
| SORT y
1810+
| KEEP y, c
1811+
| LIMIT 5;
1812+
1813+
y:date | c:long
1814+
1987-03-01T00:00:00.000Z | 5
1815+
1987-04-01T00:00:00.000Z | 3
1816+
1987-05-01T00:00:00.000Z | 1
1817+
1987-07-01T00:00:00.000Z | 1
1818+
1987-08-01T00:00:00.000Z | 2
1819+
;
1820+
1821+
dateTruncWithEvalRename
1822+
FROM employees
1823+
| EVAL a = hire_date
1824+
| RENAME hire_date as b
1825+
| WHERE a > "1987-01-01" and a < "1988-01-01"
1826+
| EVAL y = date_trunc(1 month, b)
1827+
| STATS c = count(emp_no) by y
1828+
| SORT y
1829+
| KEEP y, c
1830+
| LIMIT 5;
1831+
1832+
y:date | c:long
1833+
1987-03-01T00:00:00.000Z | 5
1834+
1987-04-01T00:00:00.000Z | 3
1835+
1987-05-01T00:00:00.000Z | 1
1836+
1987-07-01T00:00:00.000Z | 1
1837+
1987-08-01T00:00:00.000Z | 2
1838+
;

0 commit comments

Comments
 (0)