Improved vector text representation - fixes #46

This commit is contained in:
Andrew Kane
2022-12-01 14:57:47 -08:00
parent cc35af160e
commit c74bb5495b
2 changed files with 32 additions and 7 deletions

View File

@@ -1,3 +1,7 @@
## 0.3.3 (unreleased)
- Improved vector text representation
## 0.3.2 (2022-11-22)
- Fixed `invalid memory alloc request size` error

View File

@@ -5,6 +5,7 @@
#include "vector.h"
#include "fmgr.h"
#include "catalog/pg_type.h"
#include "common/shortest_dec.h"
#include "lib/stringinfo.h"
#include "libpq/pqformat.h"
#include "utils/array.h"
@@ -188,24 +189,44 @@ Datum
vector_out(PG_FUNCTION_ARGS)
{
Vector *vector = PG_GETARG_VECTOR_P(0);
StringInfoData buf;
int dim = vector->dim;
char *buf;
char *ptr;
int i;
int n;
initStringInfo(&buf);
/*
* Need:
*
* dim * (FLOAT_SHORTEST_DECIMAL_LEN - 1) bytes for
* float_to_shortest_decimal_bufn
*
* dim - 1 bytes for separator
*
* 3 bytes for [, ], and \0
*/
buf = (char *) palloc(FLOAT_SHORTEST_DECIMAL_LEN * dim + 2);
ptr = buf;
appendStringInfoChar(&buf, '[');
*ptr = '[';
ptr++;
for (i = 0; i < dim; i++)
{
if (i > 0)
appendStringInfoString(&buf, ",");
{
*ptr = ',';
ptr++;
}
appendStringInfoString(&buf, float8out_internal(vector->x[i]));
n = float_to_shortest_decimal_bufn(vector->x[i], ptr);
ptr += n;
}
appendStringInfoChar(&buf, ']');
*ptr = ']';
ptr++;
*ptr = '\0';
PG_FREE_IF_COPY(vector, 0);
PG_RETURN_CSTRING(buf.data);
PG_RETURN_CSTRING(buf);
}
/*