fix int128 send and recv

This commit is contained in:
chenbd
2024-01-18 15:50:20 +08:00
parent 16cbdbdb43
commit 536f3f8c1b
4 changed files with 82 additions and 6 deletions

View File

@ -505,13 +505,33 @@ int64 pq_getmsgint64(StringInfo msg)
*/ */
int128 pq_getmsgint128(StringInfo msg) int128 pq_getmsgint128(StringInfo msg)
{ {
uint128 n128; uint128 result;
const int int128bytes = 4; const int int128bytes = 4;
pq_copymsgbytes(msg, (char*)&n128, int128bytes); /* [hh h l ll] -> int128 */
uint32 hh32;
uint32 h32;
uint32 l32;
uint32 ll32;
pq_copymsgbytes(msg, (char*)&hh32, int128bytes);
pq_copymsgbytes(msg, (char*)&h32, int128bytes);
pq_copymsgbytes(msg, (char*)&l32, int128bytes);
pq_copymsgbytes(msg, (char*)&ll32, int128bytes);
hh32 = ntohl(hh32);
h32 = ntohl(h32);
l32 = ntohl(l32);
ll32 = ntohl(ll32);
return (int128)pg_ntoh128(n128); result = hh32;
result <<= INT128_HALF_HIGH_NBYTES;
result |= h32;
result <<= INT128_HALF_HIGH_NBYTES;
result |= l32;
result <<= INT128_HALF_HIGH_NBYTES;
result |= ll32;
return (int128)result;
} }
/* -------------------------------- /* --------------------------------

View File

@ -94,6 +94,9 @@ static inline uint128 pg_bswap128(uint128 x)
#define pg_ntoh128(x) pg_bswap128(x) #define pg_ntoh128(x) pg_bswap128(x)
#endif /* WORDS_BIGENDIAN */ #endif /* WORDS_BIGENDIAN */
#define INT128_HIGH_NBYTES 64
#define INT128_HALF_HIGH_NBYTES 32
/* /*
* Append a [u]int8 to a StringInfo buffer, which already has enough space * Append a [u]int8 to a StringInfo buffer, which already has enough space
* preallocated. * preallocated.
@ -243,10 +246,13 @@ static inline void pq_sendint64(StringInfo buf, uint64 i)
} }
/* append a binary [u]int128 to a StringInfo buffer */ /* append a binary [u]int128 to a StringInfo buffer */
static inline void pq_sendint128(StringInfo buf, uint64 i) static inline void pq_sendint128(StringInfo buf, uint128 i)
{ {
enlargeStringInfo(buf, sizeof(uint128)); enlargeStringInfo(buf, sizeof(uint128));
pq_writeint128(buf, i); /* high 64 bit */
pq_writeint64(buf, (i>>INT128_HIGH_NBYTES));
/* low 64 bit */
pq_writeint64(buf, i);
} }
/* append a binary byte to a StringInfo buffer */ /* append a binary byte to a StringInfo buffer */

View File

@ -252,6 +252,40 @@ SELECT * FROM CAST_TBL;
1 | 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 1 | 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9
(1 row) (1 row)
CREATE TABLE INT16_TBL2(q1 int16, q2 int16,q3 int);
INSERT INTO INT16_TBL2 VALUES(' 123 ',' 456',1);
INSERT INTO INT16_TBL2 VALUES('456 ','12345678901234567890123456789',2);
INSERT INTO INT16_TBL2 VALUES('123456789012345678901234567890','123',3);
INSERT INTO INT16_TBL2 VALUES(+1234567890123456789012345678901,'12345678901234567890123456789012',4);
INSERT INTO INT16_TBL2 VALUES('+123456789012345678901234567890123','-1234567890123456789012345678901234',5);
select * from INT16_TBL2 order by 3;
q1 | q2 | q3
-----------------------------------+-------------------------------------+----
123 | 456 | 1
456 | 12345678901234567890123456789 | 2
123456789012345678901234567890 | 123 | 3
1234567890123456789012345678901 | 12345678901234567890123456789012 | 4
123456789012345678901234567890123 | -1234567890123456789012345678901234 | 5
(5 rows)
\copy INT16_TBL2 to 'test_int16.csv' BINARY;
\copy INT16_TBL2 from 'test_int16.csv' with BINARY;
select * from INT16_TBL2 order by 3;
q1 | q2 | q3
-----------------------------------+-------------------------------------+----
123 | 456 | 1
123 | 456 | 1
456 | 12345678901234567890123456789 | 2
456 | 12345678901234567890123456789 | 2
123456789012345678901234567890 | 123 | 3
123456789012345678901234567890 | 123 | 3
1234567890123456789012345678901 | 12345678901234567890123456789012 | 4
1234567890123456789012345678901 | 12345678901234567890123456789012 | 4
123456789012345678901234567890123 | -1234567890123456789012345678901234 | 5
123456789012345678901234567890123 | -1234567890123456789012345678901234 | 5
(10 rows)
drop table if exists INT16_TBL2;
DROP SCHEMA schema_int16 CASCADE; DROP SCHEMA schema_int16 CASCADE;
NOTICE: drop cascades to 3 other objects NOTICE: drop cascades to 3 other objects
DETAIL: drop cascades to table int16_tbl DETAIL: drop cascades to table int16_tbl

View File

@ -94,4 +94,20 @@ INSERT INTO CAST_TBL SELECT * FROM TEST_TBL;
SELECT * FROM CAST_TBL; SELECT * FROM CAST_TBL;
CREATE TABLE INT16_TBL2(q1 int16, q2 int16,q3 int);
INSERT INTO INT16_TBL2 VALUES(' 123 ',' 456',1);
INSERT INTO INT16_TBL2 VALUES('456 ','12345678901234567890123456789',2);
INSERT INTO INT16_TBL2 VALUES('123456789012345678901234567890','123',3);
INSERT INTO INT16_TBL2 VALUES(+1234567890123456789012345678901,'12345678901234567890123456789012',4);
INSERT INTO INT16_TBL2 VALUES('+123456789012345678901234567890123','-1234567890123456789012345678901234',5);
select * from INT16_TBL2 order by 3;
\copy INT16_TBL2 to 'test_int16.csv' BINARY;
\copy INT16_TBL2 from 'test_int16.csv' with BINARY;
select * from INT16_TBL2 order by 3;
drop table if exists INT16_TBL2;
DROP SCHEMA schema_int16 CASCADE; DROP SCHEMA schema_int16 CASCADE;