-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ts_date_trunc() is similar to date_trunc() in PostgreSQL, but supports intervals like '2 months' or '1 year 6 months'.
- Loading branch information
Aleksander Alekseev
committed
May 10, 2021
1 parent
bc740a3
commit 6196c8d
Showing
8 changed files
with
260 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- This file and its contents are licensed under the Apache License 2.0. | ||
-- Please see the included NOTICE for copyright information and | ||
-- LICENSE-APACHE for a copy of the license. | ||
|
||
-- ts_date_trunc() is similar to date_trunc() in PostgreSQL but supports intervals like 'N months' | ||
CREATE OR REPLACE FUNCTION ts_date_trunc(bucket_width INTERVAL, ts DATE) RETURNS DATE | ||
AS '@MODULE_PATHNAME@', 'ts_date_trunc' LANGUAGE C IMMUTABLE PARALLEL SAFE STRICT; | ||
|
||
CREATE OR REPLACE FUNCTION ts_date_trunc(bucket_width INTERVAL, ts DATE, origin DATE) RETURNS DATE | ||
AS '@MODULE_PATHNAME@', 'ts_date_trunc' LANGUAGE C IMMUTABLE PARALLEL SAFE STRICT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* This file and its contents are licensed under the Apache License 2.0. | ||
* Please see the included NOTICE for copyright information and | ||
* LICENSE-APACHE for a copy of the license. | ||
*/ | ||
#include <postgres.h> | ||
#include <utils/date.h> | ||
#include <utils/datetime.h> | ||
|
||
#include "ts_date_trunc.h" | ||
|
||
TS_FUNCTION_INFO_V1(ts_date_trunc); | ||
|
||
TSDLLEXPORT Datum | ||
ts_date_trunc(PG_FUNCTION_ARGS) | ||
{ | ||
Interval *interval = PG_GETARG_INTERVAL_P(0); | ||
DateADT date = PG_GETARG_DATEADT(1); | ||
int origin_year = 2000, origin_month = 1, origin_day = 1; | ||
int year, month, day; | ||
int delta, bucket_number; | ||
|
||
if (PG_NARGS() > 2) | ||
{ | ||
DateADT origin_date = PG_GETARG_DATUM(2); | ||
j2date(origin_date + POSTGRES_EPOCH_JDATE, &origin_year, &origin_month, &origin_day); | ||
|
||
if (origin_day != 1) | ||
{ | ||
ereport(ERROR, | ||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), | ||
errmsg("Invalid `origin` day, YYYY-MM-01 expected"))); | ||
} | ||
} | ||
|
||
if ((interval->time != 0) || (interval->day != 0)) | ||
{ | ||
ereport(ERROR, | ||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), | ||
errmsg("only months and years are supported"))); | ||
} | ||
|
||
if (DATE_NOT_FINITE(date)) | ||
PG_RETURN_DATEADT(date); | ||
|
||
j2date(date + POSTGRES_EPOCH_JDATE, &year, &month, &day); | ||
|
||
if ((year < origin_year) || ((year == origin_year) && (month < origin_month))) | ||
{ | ||
ereport(ERROR, | ||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), | ||
errmsg("`date` < `origin` not supported, choose another `origin`"))); | ||
} | ||
|
||
delta = (year * 12 + month) - (origin_year * 12 + origin_month); | ||
bucket_number = delta / interval->month; | ||
year = origin_year + (bucket_number * interval->month) / 12; | ||
month = | ||
(((origin_year * 12 + (origin_month - 1)) + (bucket_number * interval->month)) % 12) + 1; | ||
day = 1; | ||
|
||
date = date2j(year, month, day) - POSTGRES_EPOCH_JDATE; | ||
PG_RETURN_DATEADT(date); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* This file and its contents are licensed under the Apache License 2.0. | ||
* Please see the included NOTICE for copyright information and | ||
* LICENSE-APACHE for a copy of the license. | ||
*/ | ||
#ifndef TIMESCALEDB_DATE_TRUNC_H | ||
#define TIMESCALEDB_DATE_TRUNC_H | ||
|
||
#include <postgres.h> | ||
#include <fmgr.h> | ||
|
||
#include "export.h" | ||
|
||
extern TSDLLEXPORT Datum ts_date_trunc(PG_FUNCTION_ARGS); | ||
|
||
#endif /* TIMESCALEDB_DATE_TRUNC_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters