cashapp / sqldelight

SQLDelight - Generates typesafe Kotlin APIs from SQL
https://cashapp.github.io/sqldelight/
Apache License 2.0
6.01k stars 501 forks source link

Add EXTRACT function to PostgreSql Dialect #5273

Open griffio opened 1 month ago

griffio commented 1 month ago

Description

Extract temporal fields - returns Double or nullable Double

https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT

SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40'),
  EXTRACT(YEAR FROM TIMESTAMP '2023-05-15 10:30:45'),
  EXTRACT(EPOCH FROM INTERVAL '1 day 2 hours'),
  EXTRACT(CENTURY FROM TIMESTAMP '2000-12-16 12:21:13'),
  EXTRACT(MINUTE FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-08'),
  EXTRACT(DAY FROM created_date)
FROM Events;

Implementation Will be an expression not a function as the syntax contains the FROM operator 🥨 PR to follow

This is to support further types such as temporal ranges: https://www.postgresql.org/docs/16/rangetypes.html#RANGETYPES-BUILTIN