Skip to content

Commit

Permalink
ENG-4681 [YSQL] Support INTERVAL for date/time datatypes
Browse files Browse the repository at this point in the history
Summary: Support for INTERVAL type - stored as 128 bit len binary in DocDB

Test Plan: Adapted yb_interval pr_regress tests from PostgreSQL

Reviewers: mihnea, hector, alex, neil

Reviewed By: alex, neil

Subscribers: neha

Differential Revision: https://phabricator.dev.yugabyte.com/D6277
  • Loading branch information
d-uspenskiy committed Mar 12, 2019
1 parent 90064f9 commit bd64c96
Show file tree
Hide file tree
Showing 7 changed files with 1,262 additions and 55 deletions.
27 changes: 24 additions & 3 deletions src/postgres/src/backend/catalog/ybctype.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include "utils/syscache.h"
#include "utils/numeric.h"
#include "utils/uuid.h"
#include "utils/timestamp.h"

#include "yb/yql/pggate/ybc_pggate.h"

Expand Down Expand Up @@ -423,6 +424,26 @@ Datum YBCTimeToDatum(const int64 *data, int64 bytes, const YBCPgTypeAttrs *type_
return TimeADTGetDatum(*data);
}

/*
* INTERVAL conversions.
* PG represents INTERVAL as 128 bit structure, store it as binary
*/
void YBCDatumToInterval(Datum datum, void **data, int64 *bytes) {
*data = DatumGetIntervalP(datum);
*bytes = sizeof(Interval);
}

Datum YBCIntervalToDatum(const void *data, int64 bytes, const YBCPgTypeAttrs *type_attrs) {
const size_t sz = sizeof(Interval);
if (bytes != sz) {
ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED),
errmsg("Unexpected size for Interval (%ld)", bytes)));
}
Interval* result = palloc(sz);
memcpy(result, data, sz);
return IntervalPGetDatum(result);
}

/*
* Other conversions.
*/
Expand Down Expand Up @@ -656,9 +677,9 @@ static const YBCPgTypeEntity YBCTypeEntityTable[] = {
(YBCPgDatumToData)YBCDatumToInt64,
(YBCPgDatumFromData)YBCInt64ToDatum },

{ INTERVALOID, YB_YQL_DATA_TYPE_NOT_SUPPORTED, false,
(YBCPgDatumToData)NULL,
(YBCPgDatumFromData)NULL },
{ INTERVALOID, YB_YQL_DATA_TYPE_BINARY, true,
(YBCPgDatumToData)YBCDatumToInterval,
(YBCPgDatumFromData)YBCIntervalToDatum },

{ TIMETZOID, YB_YQL_DATA_TYPE_NOT_SUPPORTED, false,
(YBCPgDatumToData)NULL,
Expand Down
3 changes: 0 additions & 3 deletions src/postgres/src/backend/parser/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -13174,13 +13174,11 @@ SimpleTypename:
| ConstDatetime { $$ = $1; }
| ConstInterval opt_interval
{
parser_ybc_not_support(@1, "Interval");
$$ = $1;
$$->typmods = $2;
}
| ConstInterval '(' Iconst ')'
{
parser_ybc_not_support(@1, "Interval");
$$ = $1;
$$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
makeIntConst($3, @3));
Expand Down Expand Up @@ -13496,7 +13494,6 @@ ConstDatetime:
ConstInterval:
INTERVAL
{
parser_ybc_not_support(@1, "INTERVAL");
$$ = SystemTypeName("interval");
$$->location = @1;
}
Expand Down
48 changes: 24 additions & 24 deletions src/postgres/src/test/regress/expected/yb_date.out
Original file line number Diff line number Diff line change
Expand Up @@ -1153,30 +1153,30 @@ SELECT EXTRACT(CENTURY FROM TIMESTAMP '1970-03-20 04:30:00.00000'); -- 20
(1 row)

-- on an interval
-- TODO: Enable once we start supporting INTEREVAL - ENG-4681 (part of issue #711)
-- SELECT EXTRACT(CENTURY FROM INTERVAL '100 y'); -- 1
-- date_part
-- -----------
-- 1
-- (1 row)
--
-- SELECT EXTRACT(CENTURY FROM INTERVAL '99 y'); -- 0
-- date_part
-- -----------
-- 0
-- (1 row)
--
-- SELECT EXTRACT(CENTURY FROM INTERVAL '-99 y'); -- 0
-- date_part
-- -----------
-- 0
-- (1 row)
--
-- SELECT EXTRACT(CENTURY FROM INTERVAL '-100 y'); -- -1
-- date_part
-- -----------
-- -1
-- (1 row)
SELECT EXTRACT(CENTURY FROM INTERVAL '100 y'); -- 1
date_part
-----------
1
(1 row)

SELECT EXTRACT(CENTURY FROM INTERVAL '99 y'); -- 0
date_part
-----------
0
(1 row)

SELECT EXTRACT(CENTURY FROM INTERVAL '-99 y'); -- 0
date_part
-----------
0
(1 row)

SELECT EXTRACT(CENTURY FROM INTERVAL '-100 y'); -- -1
date_part
-----------
-1
(1 row)

--
-- test trunc function!
--
Expand Down
Loading

0 comments on commit bd64c96

Please sign in to comment.