Skip to content

Commit

Permalink
time/timetz pg conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
dentiny committed Feb 26, 2025
1 parent f7997b4 commit b8f4403
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/pgduckdb_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ ConvertIntervalDatum(const duckdb::Value &value) {
return IntervalPGetDatum(pg_interval);
}

static Datum
ConvertTimeDatum(const duckdb::Value &value) {
std::string value_str = value.ToString();
Datum pg_time =
DirectFunctionCall3(time_in, CStringGetDatum(value_str.c_str()), ObjectIdGetDatum(TIMEOID), Int32GetDatum(-1));
return pg_time;
}

static Datum
ConvertTimeTzDatum(const duckdb::Value &value) {
std::string value_str = value.ToString();
Datum pg_timetz = DirectFunctionCall3(timetz_in, CStringGetDatum(value_str.c_str()), ObjectIdGetDatum(TIMETZOID),
Int32GetDatum(-1));
return pg_timetz;
}

inline Datum
ConvertTimestampDatum(const duckdb::Value &value) {
// Extract raw int64_t value of timestamp
Expand Down Expand Up @@ -794,6 +810,14 @@ ConvertDuckToPostgresValue(TupleTableSlot *slot, duckdb::Value &value, idx_t col
slot->tts_values[col] = ConvertDateDatum(value);
break;
}
case TIMEOID: {
slot->tts_values[col] = ConvertTimeDatum(value);
break;
}
case TIMETZOID: {
slot->tts_values[col] = ConvertTimeTzDatum(value);
break;
}
case TIMESTAMPOID: {
slot->tts_values[col] = ConvertTimestampDatum(value);
break;
Expand Down Expand Up @@ -1095,6 +1119,10 @@ GetPostgresDuckDBType(const duckdb::LogicalType &type) {
return type.IsJSONType() ? JSONOID : VARCHAROID;
case duckdb::LogicalTypeId::DATE:
return DATEOID;
case duckdb::LogicalTypeId::TIME:
return TIMEOID;
case duckdb::LogicalTypeId::TIME_TZ:
return TIMETZOID;
case duckdb::LogicalTypeId::TIMESTAMP:
case duckdb::LogicalTypeId::TIMESTAMP_SEC:
case duckdb::LogicalTypeId::TIMESTAMP_MS:
Expand Down
8 changes: 6 additions & 2 deletions test/regression/expected/test_all_types.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ FROM test_all_types()
SELECT * exclude(
tinyint, -- PG14 outputs this differently currently
varint,
TIME,
time_tz,
bit,
small_enum,
medium_enum,
Expand Down Expand Up @@ -40,10 +38,12 @@ usmallint | 0
uint | 0
ubigint | 0
date | 07-14-5881580
time | 00:00:00
timestamp | Sun Jan 10 08:01:49.551616 294247
timestamp_s | Sun Jan 10 08:01:49.551616 294247
timestamp_ms | Sun Jan 10 08:01:49.551616 294247
timestamp_ns | Wed Sep 22 00:00:00 1677
time_tz | 00:00:00+15:59:59
timestamp_tz | Sun Jan 10 00:01:49.551616 294247 PST
float | -3.4028235e+38
double | -1.7976931348623157e+308
Expand Down Expand Up @@ -72,10 +72,12 @@ usmallint | 65535
uint | 4294967295
ubigint | 18446744073709551615
date | 07-10-5881580
time | 24:00:00
timestamp | Sun Jan 10 04:00:54.775806 294247
timestamp_s | Sun Jan 10 04:00:54 294247
timestamp_ms | Sun Jan 10 04:00:54.775 294247
timestamp_ns | Fri Apr 11 23:47:16.854775 2262
time_tz | 24:00:00-15:59:59
timestamp_tz | Sat Jan 09 20:00:54.775806 294247 PST
float | 3.4028235e+38
double | 1.7976931348623157e+308
Expand Down Expand Up @@ -104,10 +106,12 @@ usmallint |
uint |
ubigint |
date |
time |
timestamp |
timestamp_s |
timestamp_ms |
timestamp_ns |
time_tz |
timestamp_tz |
float |
double |
Expand Down
2 changes: 0 additions & 2 deletions test/regression/sql/test_all_types.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ FROM test_all_types()
SELECT * exclude(
tinyint, -- PG14 outputs this differently currently
varint,
TIME,
time_tz,
bit,
small_enum,
medium_enum,
Expand Down

0 comments on commit b8f4403

Please sign in to comment.