Skip to content

Commit

Permalink
Figure out FIRST_VALUE & LAST_VALUE
Browse files Browse the repository at this point in the history
  • Loading branch information
emk committed Nov 9, 2023
1 parent 065dd46 commit cc34ab3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,10 @@ pub enum WindowFrameStart {
unbounded_token: Keyword,
preceding_token: Keyword,
},
CurrentRow {
current_token: Keyword,
row_token: PseudoKeyword,
},
}

/// A window frame end. Keep this simple for now.
Expand All @@ -1425,6 +1429,10 @@ pub enum WindowFrameEnd {
current_token: Keyword,
row_token: PseudoKeyword,
},
UnboundedFollowing {
unbounded_token: Keyword,
following_token: Keyword,
},
}

/// Data types.
Expand Down Expand Up @@ -2544,6 +2552,12 @@ peg::parser! {
preceding_token,
}
}
/ current_token:k("CURRENT") row_token:pk("ROW") {
WindowFrameStart::CurrentRow {
current_token,
row_token,
}
}

rule window_frame_end() -> WindowFrameEnd
= current_token:k("CURRENT") row_token:pk("ROW") {
Expand All @@ -2552,6 +2566,12 @@ peg::parser! {
row_token,
}
}
/ unbounded_token:k("UNBOUNDED") following_token:k("FOLLOWING") {
WindowFrameEnd::UnboundedFollowing {
unbounded_token,
following_token,
}
}

rule cast() -> Cast
= cast_type:cast_type() paren1:p("(") expression:expression() as_token:k("AS") data_type:data_type() paren2:p(")") {
Expand Down
6 changes: 2 additions & 4 deletions tests/sql/functions/windows/first_last_value.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- pending: sqlite3 FIRST_VALUE, LAST_VALUE seem to require frames to work?
-- pending: trino FIRST_VALUE, LAST_VALUE seem to require frames to work?

-- FIRST_VALUE, LAST_VALUE

-- A fixture table for testing window functions.
Expand All @@ -22,7 +19,8 @@ CREATE OR REPLACE TABLE __result1 AS
SELECT
item,
FIRST_VALUE(item) OVER (PARTITION BY category ORDER BY price) AS cheapest_alternative,
LAST_VALUE(item) OVER (PARTITION BY category ORDER BY price) AS most_expensive_alternative,
-- We need the RANGE frame here.
LAST_VALUE(item) OVER (PARTITION BY category ORDER BY price RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS most_expensive_alternative,
FROM groceries;

CREATE OR REPLACE TABLE __expected1 (
Expand Down

0 comments on commit cc34ab3

Please sign in to comment.