Skip to content

Commit 388ffd3

Browse files
authored
chore(query): last_day support default interval type (#18022)
refactor: last_day support default interval type LAST_DAY( <date_or_timetamp_expr> [ , <date_part> ] ) The interval type for which to find the last day. Accepted values are week, month, quarter, and year. default with: month
1 parent 255b6dd commit 388ffd3

File tree

4 files changed

+104
-2
lines changed

4 files changed

+104
-2
lines changed

src/query/ast/src/parser/expr.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,9 +1216,18 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
12161216

12171217
let last_day = map(
12181218
rule! {
1219-
LAST_DAY ~ "(" ~ #subexpr(0) ~ "," ~ #interval_kind ~ ")"
1219+
LAST_DAY ~ "(" ~ #subexpr(0) ~ ("," ~ #interval_kind)? ~ ")"
1220+
},
1221+
|(_, _, date, opt_unit, _)| {
1222+
if let Some((_, unit)) = opt_unit {
1223+
ExprElement::LastDay { unit, date }
1224+
} else {
1225+
ExprElement::LastDay {
1226+
unit: IntervalKind::Month,
1227+
date,
1228+
}
1229+
}
12201230
},
1221-
|(_, _, date, _, unit, _)| ExprElement::LastDay { unit, date },
12221231
);
12231232

12241233
let previous_day = map(

src/query/ast/tests/it/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,8 @@ fn test_expr() {
12651265
r#"date_part(year, d)"#,
12661266
r#"datepart(year, d)"#,
12671267
r#"DATEDIFF(SECOND, to_timestamp('2024-01-01 21:01:35.423179'), to_timestamp('2023-12-31 09:38:18.165575'))"#,
1268+
r#"last_day(to_date('2024-10-22'), week)"#,
1269+
r#"last_day(to_date('2024-10-22'))"#,
12681270
r#"date_sub(QUARTER, 1, to_date('2018-01-02'))"#,
12691271
r#"datebetween(QUARTER, to_date('2018-01-02'), to_date('2018-04-02'))"#,
12701272
r#"position('a' in str)"#,

src/query/ast/tests/it/testdata/expr.txt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,92 @@ DateDiff {
19151915
}
19161916

19171917

1918+
---------- Input ----------
1919+
last_day(to_date('2024-10-22'), week)
1920+
---------- Output ---------
1921+
LAST_DAY(to_date('2024-10-22'), WEEK)
1922+
---------- AST ------------
1923+
LastDay {
1924+
span: Some(
1925+
0..37,
1926+
),
1927+
unit: Week,
1928+
date: FunctionCall {
1929+
span: Some(
1930+
9..30,
1931+
),
1932+
func: FunctionCall {
1933+
distinct: false,
1934+
name: Identifier {
1935+
span: Some(
1936+
9..16,
1937+
),
1938+
name: "to_date",
1939+
quote: None,
1940+
ident_type: None,
1941+
},
1942+
args: [
1943+
Literal {
1944+
span: Some(
1945+
17..29,
1946+
),
1947+
value: String(
1948+
"2024-10-22",
1949+
),
1950+
},
1951+
],
1952+
params: [],
1953+
order_by: [],
1954+
window: None,
1955+
lambda: None,
1956+
},
1957+
},
1958+
}
1959+
1960+
1961+
---------- Input ----------
1962+
last_day(to_date('2024-10-22'))
1963+
---------- Output ---------
1964+
LAST_DAY(to_date('2024-10-22'), MONTH)
1965+
---------- AST ------------
1966+
LastDay {
1967+
span: Some(
1968+
0..31,
1969+
),
1970+
unit: Month,
1971+
date: FunctionCall {
1972+
span: Some(
1973+
9..30,
1974+
),
1975+
func: FunctionCall {
1976+
distinct: false,
1977+
name: Identifier {
1978+
span: Some(
1979+
9..16,
1980+
),
1981+
name: "to_date",
1982+
quote: None,
1983+
ident_type: None,
1984+
},
1985+
args: [
1986+
Literal {
1987+
span: Some(
1988+
17..29,
1989+
),
1990+
value: String(
1991+
"2024-10-22",
1992+
),
1993+
},
1994+
],
1995+
params: [],
1996+
order_by: [],
1997+
window: None,
1998+
lambda: None,
1999+
},
2000+
},
2001+
}
2002+
2003+
19182004
---------- Input ----------
19192005
date_sub(QUARTER, 1, to_date('2018-01-02'))
19202006
---------- Output ---------

tests/sqllogictests/suites/query/functions/02_0012_function_datetimes_tz.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ select last_day(to_date('2024-02-01'), month);
3636
----
3737
2024-02-29
3838

39+
query B
40+
select last_day(to_date('2024-02-01'), month)=last_day(to_date('2024-02-01'));
41+
----
42+
1
43+
3944
query T
4045
select last_day(to_date('2024-02-01'), quarter);
4146
----

0 commit comments

Comments
 (0)