Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7756cf3

Browse files
committed
Add time_bucket and -date_bucket functions
1 parent37fa849 commit7756cf3

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

‎expected/test.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,9 @@ select first(bid_price,ts),last(ask_size,ts) from vquote group by symbol;
522522
10.1 | 105
523523
(1 row)
524524

525+
select symbol,time_bucket('2 minutes',ts) from vquote;
526+
symbol | time_bucket
527+
--------+----------------------------------------------------------------------------------------------------------------------------------------------
528+
AAA | {574164000000000,574164000000000,574164120000000,574164120000000,574164240000000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}
529+
(1 row)
530+

‎sql/test.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@ select create_projection('vquote','quote',array['ts','ask_price','ask_size','bid
142142
select vquote_refresh();
143143

144144
select first(bid_price,ts),last(ask_size,ts)from vquotegroup by symbol;
145-
145+
select symbol,time_bucket('2 minutes',ts)from vquote;

‎vops--1.0.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,8 @@ create function high(tile vops_int4) returns int4 as 'MODULE_PATHNAME','vops_int
12491249

12501250
-- date tile
12511251

1252+
createfunctiondate_bucket(interval, vops_date) returns vops_dateas'MODULE_PATHNAME','vops_date_bucket' language C parallel safe immutable strict;
1253+
12521254
createfunctionvops_date_const(opddate) returns vops_dateas'MODULE_PATHNAME','vops_int4_const' language C parallel safe immutable strict;
12531255
create cast (dateas vops_date) with function vops_date_const(date)AS IMPLICIT;
12541256

@@ -1584,6 +1586,8 @@ create function high(tile vops_date) returns date as 'MODULE_PATHNAME','vops_int
15841586

15851587
-- timestamp tile
15861588

1589+
createfunctiontime_bucket(interval, vops_timestamp) returns vops_timestampas'MODULE_PATHNAME','vops_time_bucket' language C parallel safe immutable strict;
1590+
15871591
createfunctionvops_timestamp_const(opdtimestamp) returns vops_timestampas'MODULE_PATHNAME','vops_int8_const' language C parallel safe immutable strict;
15881592
create cast (timestampas vops_timestamp) with function vops_timestamp_const(timestamp)AS IMPLICIT;
15891593

‎vops.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,92 @@ Datum vops_count_accumulate(PG_FUNCTION_ARGS)
10291029
PG_RETURN_INT64(count);
10301030
}
10311031

1032+
PG_FUNCTION_INFO_V1(vops_time_bucket);
1033+
Datumvops_time_bucket(PG_FUNCTION_ARGS)
1034+
{
1035+
Interval*interval=PG_GETARG_INTERVAL_P(0);
1036+
vops_int8*timestamp= (vops_int8*)PG_GETARG_POINTER(1);
1037+
vops_int8*result= (vops_int8*)palloc(sizeof(vops_int8));
1038+
inti;
1039+
result->hdr=timestamp->hdr;
1040+
if (interval->day==0&&interval->month==0) {
1041+
for (i=0;i<TILE_SIZE;i++) {
1042+
result->payload[i]=timestamp->payload[i] /interval->time*interval->time;
1043+
}
1044+
}elseif (interval->month==0) {
1045+
time_tdays=interval->day*USECS_PER_DAY+interval->time;
1046+
1047+
for (i=0;i<TILE_SIZE;i++) {
1048+
result->payload[i]=timestamp->payload[i] /days*days;
1049+
}
1050+
}else {
1051+
inttz;
1052+
structpg_tmtm;
1053+
fsec_tfsec;
1054+
Timestampdt;
1055+
intmonths;
1056+
1057+
if (interval->day!=0||interval->time!=0)
1058+
elog(ERROR,"Month interval may not include days and time");
1059+
1060+
for (i=0;i<TILE_SIZE;i++) {
1061+
timestamp2tm(timestamp->payload[i],&tz,&tm,&fsec,NULL,NULL);
1062+
tm.tm_sec=0;
1063+
tm.tm_min=0;
1064+
tm.tm_hour=0;
1065+
tm.tm_sec=0;
1066+
tm.tm_mday=1;
1067+
months= (tm.tm_year*12+tm.tm_mon-1) /interval->month;
1068+
tm.tm_year=months/12;
1069+
tm.tm_mon=months%12+1;
1070+
fsec=0;
1071+
tm2timestamp(&tm,fsec,&tz,&dt);
1072+
result->payload[i]=dt;
1073+
}
1074+
}
1075+
PG_RETURN_POINTER(result);
1076+
}
1077+
1078+
PG_FUNCTION_INFO_V1(vops_date_bucket);
1079+
Datumvops_date_bucket(PG_FUNCTION_ARGS)
1080+
{
1081+
Interval*interval=PG_GETARG_INTERVAL_P(0);
1082+
vops_int4*date= (vops_int4*)PG_GETARG_POINTER(1);
1083+
vops_int4*result= (vops_int4*)palloc(sizeof(vops_int4));
1084+
inti;
1085+
1086+
if (interval->time)
1087+
elog(ERROR,"Date interval may not include time");
1088+
1089+
result->hdr=date->hdr;
1090+
1091+
if (interval->month==0) {
1092+
time_tdays=interval->day;
1093+
1094+
for (i=0;i<TILE_SIZE;i++) {
1095+
result->payload[i]=date->payload[i] /days*days;
1096+
}
1097+
}else {
1098+
intyear,month,day,months;
1099+
1100+
if (interval->day!=0)
1101+
elog(ERROR,"Month interval may not include days");
1102+
1103+
for (i=0;i<TILE_SIZE;i++) {
1104+
j2date(date->payload[i],&year,&month,&day);
1105+
months= (year*12+month-1) /interval->month;
1106+
year=months /12;
1107+
month=months %12+1;
1108+
day=1;
1109+
result->payload[i]=date2j(year,month,day);
1110+
}
1111+
}
1112+
PG_RETURN_POINTER(result);
1113+
}
1114+
1115+
1116+
1117+
10321118
staticEState*estate;
10331119
staticTupleTableSlot*slot;
10341120
staticRelationrel;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp