Skip to content

Commit

Permalink
Add support for more types
Browse files Browse the repository at this point in the history
  • Loading branch information
JelteF committed Aug 23, 2024
1 parent 0ca85e1 commit 24c63d4
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 47 deletions.
70 changes: 23 additions & 47 deletions src/pgduckdb_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,56 +706,32 @@ duckdb::Value
ConvertPostgresToDuckValue(Datum value, Oid postgres_type) {
switch (postgres_type) {
case BOOLOID:
return duckdb::Value(DatumGetBool(value));
// case duckdb::LogicalType::TINYINT: {
// auto aux_info = type.GetAuxInfoShrPtr();
// if (aux_info && dynamic_cast<IsBpChar *>(aux_info.get())) {
// auto bpchar_length = VARSIZE_ANY_EXHDR(value);
// auto bpchar_data = VARDATA_ANY(value);
//
// if (bpchar_length != 1) {
// throw duckdb::InternalException(
// "Expected 1 length BPCHAR for TINYINT marked with IsBpChar at offset %llu", offset);
// }
// Append<int8_t>(result, bpchar_data[0], offset);
// } else {
// Append<int8_t>(result, DatumGetChar(value), offset);
// }
// break;
// }
return duckdb::Value::BOOLEAN(DatumGetBool(value));
case INT2OID:
return duckdb::Value(DatumGetInt16(value));
return duckdb::Value::SMALLINT(DatumGetInt16(value));
case INT4OID:
return duckdb::Value(DatumGetInt32(value));
// case duckdb::LogicalTypeId::UINTEGER:
// Append<uint32_t>(result, DatumGetUInt32(value), offset);
// break;
return duckdb::Value::INTEGER(DatumGetInt32(value));
case INT8OID:
return duckdb::Value(DatumGetInt64(value));
// case duckdb::LogicalTypeId::VARCHAR: {
// // NOTE: This also handles JSON
// AppendString(result, value, offset);
// break;
// }
// case duckdb::LogicalTypeId::DATE:
// Append<duckdb::date_t>(result, duckdb::date_t(static_cast<int32_t>(value + PGDUCKDB_DUCK_DATE_OFFSET)),
// offset); break; case duckdb::LogicalTypeId::TIMESTAMP: Append<duckdb::timestamp_t>( result,
// duckdb::timestamp_t(static_cast<int64_t>(value + PGDUCKDB_DUCK_TIMESTAMP_OFFSET)), offset); break; case
// duckdb::LogicalTypeId::FLOAT: { Append<float>(result, DatumGetFloat4(value), offset); break;
// }
// case duckdb::LogicalTypeId::DOUBLE: {
// auto aux_info = type.GetAuxInfoShrPtr();
// if (aux_info && dynamic_cast<NumericAsDouble *>(aux_info.get())) {
// // This NUMERIC could not be converted to a DECIMAL, convert it as DOUBLE instead
// auto numeric = DatumGetNumeric(value);
// auto numeric_var = FromNumeric(numeric);
// auto double_val = ConvertDecimal<double, DecimalConversionDouble>(numeric_var);
// Append<double>(result, double_val, offset);
// } else {
// Append<double>(result, DatumGetFloat8(value), offset);
// }
// break;
// }
return duckdb::Value::BIGINT(DatumGetInt64(value));
case BPCHAROID:
case TEXTOID:
// TODO: Is this the correct place for JSONOID? I'm unable to use it in a
// test so I don't know.
// case JSONOID:
case VARCHAROID: {
return duckdb::Value((const char *)value);
}
case DATEOID:
return duckdb::Value::DATE(duckdb::date_t(static_cast<int32_t>(value + PGDUCKDB_DUCK_DATE_OFFSET)));
case TIMESTAMPOID:
return duckdb::Value::TIMESTAMP(
duckdb::timestamp_t(static_cast<int64_t>(value + PGDUCKDB_DUCK_TIMESTAMP_OFFSET)));
case FLOAT4OID: {
return duckdb::Value::FLOAT(DatumGetFloat4(value));
}
case FLOAT8OID: {
return duckdb::Value::DOUBLE(DatumGetFloat8(value));
}
// case duckdb::LogicalTypeId::DECIMAL: {
// auto physical_type = type.InternalType();
// auto numeric = DatumGetNumeric(value);
Expand Down
43 changes: 43 additions & 0 deletions test/regression/expected/extended_protocol.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
CREATE TABLE t(
bool BOOLEAN,
i2 SMALLINT,
i4 INT,
i8 BIGINT,
fl4 REAL,
fl8 DOUBLE PRECISION,
t1 TEXT,
t2 VARCHAR,
d DATE,
ts TIMESTAMP);
INSERT INTO t VALUES (true, 2, 4, 8, 4.0, 8.0, 't1', 't2', '2024-05-04', '2020-01-01 01:02:03');
select fl4 from t;
fl4
-----
4
(1 row)

SELECT bool, i2, i4, i8, fl4, fl8, t1, t2, d, ts FROM t WHERE
bool = $1
and i2 = $2
and i4 = $3
and i8 = $4
-- FIXME: The following "larger than" comparisons all have a bugs
-- somewhere, the comparison reports that they are larger, but they clearly
-- are not. The floats are actually smaler (to rule out any floating point
-- funkyness) and the strings are equal.
and fl4 > $5
and fl8 > $6
and t1 > $7
and t2 > $8
and d = $9
and ts = $10
\bind true 2 4 8 5.0 9.0 t1 t2 '2024-05-04' '2020-01-01 01:02:03' \g
bool | i2 | i4 | i8 | fl4 | fl8 | t1 | t2 | d | ts
------+----+----+----+-----+-----+----+----+------------+--------------------------
t | 2 | 4 | 8 | 4 | 8 | t1 | t2 | 05-04-2024 | Wed Jan 01 01:02:03 2020
(1 row)

-- TODO: Fix this by supporting the UNKNOWN type somehow
SELECT $1 FROM t \bind something \g
ERROR: Could not convert DuckDB type: UNKNOWN to Postgres type
drop table t;
36 changes: 36 additions & 0 deletions test/regression/sql/extended_protocol.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
CREATE TABLE t(
bool BOOLEAN,
i2 SMALLINT,
i4 INT,
i8 BIGINT,
fl4 REAL,
fl8 DOUBLE PRECISION,
t1 TEXT,
t2 VARCHAR,
d DATE,
ts TIMESTAMP);
INSERT INTO t VALUES (true, 2, 4, 8, 4.0, 8.0, 't1', 't2', '2024-05-04', '2020-01-01 01:02:03');

select fl4 from t;

SELECT bool, i2, i4, i8, fl4, fl8, t1, t2, d, ts FROM t WHERE
bool = $1
and i2 = $2
and i4 = $3
and i8 = $4
-- FIXME: The following "larger than" comparisons all have a bugs
-- somewhere, the comparison reports that they are larger, but they clearly
-- are not. The floats are actually smaler (to rule out any floating point
-- funkyness) and the strings are equal.
and fl4 > $5
and fl8 > $6
and t1 > $7
and t2 > $8
and d = $9
and ts = $10
\bind true 2 4 8 5.0 9.0 t1 t2 '2024-05-04' '2020-01-01 01:02:03' \g

-- TODO: Fix this by supporting the UNKNOWN type somehow
SELECT $1 FROM t \bind something \g

drop table t;

0 comments on commit 24c63d4

Please sign in to comment.