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

feat(query): add last_day, previous_day, next_day function #16740

Merged
merged 5 commits into from
Nov 7, 2024
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
55 changes: 55 additions & 0 deletions src/query/ast/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,21 @@ pub enum Expr {
unit: IntervalKind,
date: Box<Expr>,
},
LastDay {
span: Span,
unit: IntervalKind,
date: Box<Expr>,
},
PreviousDay {
span: Span,
unit: Weekday,
date: Box<Expr>,
},
NextDay {
span: Span,
unit: Weekday,
date: Box<Expr>,
},
Hole {
span: Span,
name: String,
Expand Down Expand Up @@ -269,6 +284,9 @@ impl Expr {
| Expr::DateDiff { span, .. }
| Expr::DateSub { span, .. }
| Expr::DateTrunc { span, .. }
| Expr::LastDay { span, .. }
| Expr::PreviousDay { span, .. }
| Expr::NextDay { span, .. }
| Expr::Hole { span, .. } => *span,
}
}
Expand Down Expand Up @@ -411,6 +429,9 @@ impl Expr {
..
} => merge_span(merge_span(*span, interval.whole_span()), date.whole_span()),
Expr::DateTrunc { span, date, .. } => merge_span(*span, date.whole_span()),
Expr::LastDay { span, date, .. } => merge_span(*span, date.whole_span()),
Expr::PreviousDay { span, date, .. } => merge_span(*span, date.whole_span()),
Expr::NextDay { span, date, .. } => merge_span(*span, date.whole_span()),
Expr::Hole { span, .. } => *span,
}
}
Expand Down Expand Up @@ -734,6 +755,15 @@ impl Display for Expr {
Expr::DateTrunc { unit, date, .. } => {
write!(f, "DATE_TRUNC({unit}, {date})")?;
}
Expr::LastDay { unit, date, .. } => {
write!(f, "LAST_DAY({date}, {unit})")?;
}
Expr::PreviousDay { unit, date, .. } => {
write!(f, "PREVIOUS_DAY({date}, {unit})")?;
}
Expr::NextDay { unit, date, .. } => {
write!(f, "NEXT_DAY({date}, {unit})")?;
}
Expr::Hole { name, .. } => {
write!(f, ":{name}")?;
}
Expand All @@ -750,6 +780,31 @@ impl Display for Expr {
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Drive, DriveMut)]
pub enum Weekday {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
}

impl Display for Weekday {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(match self {
Weekday::Sunday => "SUNDAY",
Weekday::Monday => "MONDAY",
Weekday::Tuesday => "TUESDAY",
Weekday::Wednesday => "WEDNESDAY",
Weekday::Thursday => "THURSDAY",
Weekday::Friday => "FRIDAY",
Weekday::Saturday => "SATURDAY",
})
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Drive, DriveMut)]
pub enum IntervalKind {
Year,
Expand Down
18 changes: 18 additions & 0 deletions src/query/ast/src/ast/format/syntax/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,24 @@ pub(crate) fn pretty_expr(expr: Expr) -> RcDoc<'static> {
.append(RcDoc::space())
.append(pretty_expr(*date))
.append(RcDoc::text(")")),
Expr::LastDay { unit, date, .. } => RcDoc::text("LAST_DAY(")
.append(pretty_expr(*date))
.append(RcDoc::text(","))
.append(RcDoc::space())
.append(RcDoc::text(unit.to_string()))
.append(RcDoc::text(")")),
Expr::PreviousDay { unit, date, .. } => RcDoc::text("PREVIOUS_DAY(")
.append(pretty_expr(*date))
.append(RcDoc::text(","))
.append(RcDoc::space())
.append(RcDoc::text(unit.to_string()))
.append(RcDoc::text(")")),
Expr::NextDay { unit, date, .. } => RcDoc::text("NEXT_DAY(")
.append(pretty_expr(*date))
.append(RcDoc::text(","))
.append(RcDoc::space())
.append(RcDoc::text(unit.to_string()))
.append(RcDoc::text(")")),
Expr::Hole { name, .. } => RcDoc::text(":").append(RcDoc::text(name.to_string())),
}
}
118 changes: 109 additions & 9 deletions src/query/ast/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,18 @@ pub enum ExprElement {
unit: IntervalKind,
date: Expr,
},
LastDay {
unit: IntervalKind,
date: Expr,
},
PreviousDay {
unit: Weekday,
date: Expr,
},
NextDay {
unit: Weekday,
date: Expr,
},
Hole {
name: String,
},
Expand Down Expand Up @@ -416,6 +428,9 @@ impl ExprElement {
ExprElement::DateDiff { .. } => Affix::Nilfix,
ExprElement::DateSub { .. } => Affix::Nilfix,
ExprElement::DateTrunc { .. } => Affix::Nilfix,
ExprElement::LastDay { .. } => Affix::Nilfix,
ExprElement::PreviousDay { .. } => Affix::Nilfix,
ExprElement::NextDay { .. } => Affix::Nilfix,
ExprElement::Hole { .. } => Affix::Nilfix,
ExprElement::VariableAccess { .. } => Affix::Nilfix,
}
Expand Down Expand Up @@ -459,6 +474,9 @@ impl Expr {
Expr::DateDiff { .. } => Affix::Nilfix,
Expr::DateSub { .. } => Affix::Nilfix,
Expr::DateTrunc { .. } => Affix::Nilfix,
Expr::LastDay { .. } => Affix::Nilfix,
Expr::PreviousDay { .. } => Affix::Nilfix,
Expr::NextDay { .. } => Affix::Nilfix,
Expr::Hole { .. } => Affix::Nilfix,
}
}
Expand Down Expand Up @@ -657,6 +675,21 @@ impl<'a, I: Iterator<Item = WithSpan<'a, ExprElement>>> PrattParser<I> for ExprP
unit,
date: Box::new(date),
},
ExprElement::LastDay { unit, date } => Expr::LastDay {
span: transform_span(elem.span.tokens),
unit,
date: Box::new(date),
},
ExprElement::PreviousDay { unit, date } => Expr::PreviousDay {
span: transform_span(elem.span.tokens),
unit,
date: Box::new(date),
},
ExprElement::NextDay { unit, date } => Expr::NextDay {
span: transform_span(elem.span.tokens),
unit,
date: Box::new(date),
},
ExprElement::Hole { name } => Expr::Hole {
span: transform_span(elem.span.tokens),
name,
Expand Down Expand Up @@ -1222,6 +1255,27 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
|(_, _, unit, _, date, _)| ExprElement::DateTrunc { unit, date },
);

let last_day = map(
rule! {
LAST_DAY ~ "(" ~ #subexpr(0) ~ "," ~ #interval_kind ~ ")"
},
|(_, _, date, _, unit, _)| ExprElement::LastDay { unit, date },
);

let previous_day = map(
rule! {
PREVIOUS_DAY ~ "(" ~ #subexpr(0) ~ "," ~ #weekday ~ ")"
},
|(_, _, date, _, unit, _)| ExprElement::PreviousDay { unit, date },
);

let next_day = map(
rule! {
NEXT_DAY ~ "(" ~ #subexpr(0) ~ "," ~ #weekday ~ ")"
},
|(_, _, date, _, unit, _)| ExprElement::NextDay { unit, date },
);

let date_expr = map(
rule! {
DATE ~ #consumed(literal_string)
Expand Down Expand Up @@ -1281,19 +1335,25 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
| #json_op : "<operator>"
| #unary_op : "<operator>"
| #cast : "`CAST(... AS ...)`"
| #date_add: "`DATE_ADD(..., ..., (YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | DOY | DOW))`"
| #date_diff: "`DATE_DIFF(..., ..., (YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | DOY | DOW))`"
| #date_sub: "`DATE_SUB(..., ..., (YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | DOY | DOW))`"
| #date_trunc: "`DATE_TRUNC((YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND), ...)`"
| #date_expr: "`DATE <str_literal>`"
| #timestamp_expr: "`TIMESTAMP <str_literal>`"
| #interval: "`INTERVAL ... (YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | DOY | DOW)`"
| #pg_cast : "`::<type_name>`"
| #extract : "`EXTRACT((YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | WEEK) FROM ...)`"
| #date_part : "`DATE_PART((YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | WEEK), ...)`"
| #position : "`POSITION(... IN ...)`"
| #variable_access: "`$<ident>`"
),
rule! (
#date_add : "`DATE_ADD(..., ..., (YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | DOY | DOW))`"
| #date_diff : "`DATE_DIFF(..., ..., (YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | DOY | DOW))`"
| #date_sub : "`DATE_SUB(..., ..., (YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | DOY | DOW))`"
| #date_trunc : "`DATE_TRUNC((YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND), ...)`"
| #last_day : "`LAST_DAY(..., (YEAR | QUARTER | MONTH | WEEK)))`"
| #previous_day : "`PREVIOUS_DAY(..., (Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday))`"
| #next_day : "`NEXT_DAY(..., (Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday))`"
| #date_expr : "`DATE <str_literal>`"
| #timestamp_expr : "`TIMESTAMP <str_literal>`"
| #interval : "`INTERVAL ... (YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | DOY | DOW)`"
| #extract : "`EXTRACT((YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | WEEK) FROM ...)`"
| #date_part : "`DATE_PART((YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | SECOND | WEEK), ...)`"

),
rule!(
#substring : "`SUBSTRING(... [FROM ...] [FOR ...])`"
| #trim : "`TRIM(...)`"
Expand Down Expand Up @@ -1692,6 +1752,46 @@ pub fn type_name(i: Input) -> IResult<TypeName> {
)(i)
}

pub fn weekday(i: Input) -> IResult<Weekday> {
alt((
value(Weekday::Sunday, rule! { SUNDAY }),
value(Weekday::Monday, rule! { MONDAY }),
value(Weekday::Tuesday, rule! { TUESDAY }),
value(Weekday::Wednesday, rule! { WEDNESDAY }),
value(Weekday::Thursday, rule! { THURSDAY }),
value(Weekday::Friday, rule! { FRIDAY }),
value(Weekday::Saturday, rule! { SATURDAY }),
value(
Weekday::Sunday,
rule! { #literal_string_eq_ignore_case("SUNDAY") },
),
value(
Weekday::Monday,
rule! { #literal_string_eq_ignore_case("MONDAY") },
),
value(
Weekday::Tuesday,
rule! { #literal_string_eq_ignore_case("TUESDAY") },
),
value(
Weekday::Wednesday,
rule! { #literal_string_eq_ignore_case("WEDNESDAY") },
),
value(
Weekday::Thursday,
rule! { #literal_string_eq_ignore_case("THURSDAY") },
),
value(
Weekday::Friday,
rule! { #literal_string_eq_ignore_case("FRIDAY") },
),
value(
Weekday::Saturday,
rule! { #literal_string_eq_ignore_case("SATURDAY") },
),
))(i)
}

pub fn interval_kind(i: Input) -> IResult<IntervalKind> {
alt((
value(IntervalKind::Year, rule! { YEAR }),
Expand Down
23 changes: 23 additions & 0 deletions src/query/ast/src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ pub enum TokenKind {
FORMATS,
#[token("FRAGMENTS", ignore(ascii_case))]
FRAGMENTS,
#[token("FRIDAY", ignore(ascii_case))]
FRIDAY,
#[token("FROM", ignore(ascii_case))]
FROM,
#[token("FULL", ignore(ascii_case))]
Expand Down Expand Up @@ -725,6 +727,8 @@ pub enum TokenKind {
INTO,
#[token("INVERTED", ignore(ascii_case))]
INVERTED,
#[token("PREVIOUS_DAY", ignore(ascii_case))]
PREVIOUS_DAY,
#[token("PROCEDURE", ignore(ascii_case))]
PROCEDURE,
#[token("PROCEDURES", ignore(ascii_case))]
Expand All @@ -749,6 +753,8 @@ pub enum TokenKind {
KEY,
#[token("KILL", ignore(ascii_case))]
KILL,
#[token("LAST_DAY", ignore(ascii_case))]
LAST_DAY,
#[token("LATERAL", ignore(ascii_case))]
LATERAL,
#[token("LINEAR", ignore(ascii_case))]
Expand Down Expand Up @@ -816,6 +822,8 @@ pub enum TokenKind {
MATERIALIZED,
#[token("MUST_CHANGE_PASSWORD", ignore(ascii_case))]
MUST_CHANGE_PASSWORD,
#[token("NEXT_DAY", ignore(ascii_case))]
NEXT_DAY,
#[token("NON_DISPLAY", ignore(ascii_case))]
NON_DISPLAY,
#[token("NATURAL", ignore(ascii_case))]
Expand Down Expand Up @@ -1016,6 +1024,8 @@ pub enum TokenKind {
OPTIMIZED,
#[token("DECORRELATED", ignore(ascii_case))]
DECORRELATED,
#[token("SATURDAY", ignore(ascii_case))]
SATURDAY,
#[token("SCHEMA", ignore(ascii_case))]
SCHEMA,
#[token("SCHEMAS", ignore(ascii_case))]
Expand Down Expand Up @@ -1062,6 +1072,8 @@ pub enum TokenKind {
SIZE_LIMIT,
#[token("MAX_FILES", ignore(ascii_case))]
MAX_FILES,
#[token("MONDAY", ignore(ascii_case))]
MONDAY,
#[token("SKIP_HEADER", ignore(ascii_case))]
SKIP_HEADER,
#[token("SMALLINT", ignore(ascii_case))]
Expand Down Expand Up @@ -1138,6 +1150,8 @@ pub enum TokenKind {
TENANT,
#[token("THEN", ignore(ascii_case))]
THEN,
#[token("THURSDAY", ignore(ascii_case))]
THURSDAY,
#[token("TIMESTAMP", ignore(ascii_case))]
TIMESTAMP,
#[token("TIMEZONE_HOUR", ignore(ascii_case))]
Expand Down Expand Up @@ -1166,6 +1180,8 @@ pub enum TokenKind {
TRY_CAST,
#[token("TSV", ignore(ascii_case))]
TSV,
#[token("TUESDAY", ignore(ascii_case))]
TUESDAY,
#[token("TUPLE", ignore(ascii_case))]
TUPLE,
#[token("TYPE", ignore(ascii_case))]
Expand Down Expand Up @@ -1314,6 +1330,8 @@ pub enum TokenKind {
ENABLED,
#[token("WEBHOOK", ignore(ascii_case))]
WEBHOOK,
#[token("WEDNESDAY", ignore(ascii_case))]
WEDNESDAY,
#[token("ERROR_INTEGRATION", ignore(ascii_case))]
ERROR_INTEGRATION,
#[token("AUTO_INGEST", ignore(ascii_case))]
Expand Down Expand Up @@ -1354,6 +1372,8 @@ pub enum TokenKind {
SOURCE,
#[token("SQL", ignore(ascii_case))]
SQL,
#[token("SUNDAY", ignore(ascii_case))]
SUNDAY,
}

// Reference: https://www.postgresql.org/docs/current/sql-keywords-appendix.html
Expand Down Expand Up @@ -1593,6 +1613,9 @@ impl TokenKind {
| TokenKind::DATE_DIFF
| TokenKind::DATE_SUB
| TokenKind::DATE_TRUNC
| TokenKind::LAST_DAY
| TokenKind::PREVIOUS_DAY
| TokenKind::NEXT_DAY
| TokenKind::IGNORE_RESULT
)
}
Expand Down
Loading
Loading