Skip to content

Commit 6ed7b46

Browse files
committed
Add dedicated NUMBER type
1 parent 9af0b31 commit 6ed7b46

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

pyhost.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static PyObject* ipaddressV6Class;
2727

2828
static PyObject* trinoErrorResultFunction;
2929
static PyObject* decimalToStringFunction;
30+
static PyObject* numberToStringFunction;
3031
static PyObject* guestFunction;
3132

3233
static const u8* trinoArgType;
@@ -137,6 +138,7 @@ static void skipType(const u8** const type)
137138
case DOUBLE:
138139
case REAL:
139140
case DECIMAL:
141+
case NUMBER:
140142
case VARCHAR:
141143
case VARBINARY:
142144
case DATE:
@@ -241,7 +243,8 @@ static PyObject* doBuildArgs(const u8** const type, const u8** const data)
241243
*data += sizeof(f32);
242244
return checked(PyFloat_FromDouble(value));
243245
}
244-
case DECIMAL: {
246+
case DECIMAL:
247+
case NUMBER: {
245248
PyObject* number = readString(data);
246249
PyObject* value = checked(PyObject_CallOneArg(decimalClass, number));
247250
Py_DECREF(number);
@@ -657,6 +660,20 @@ static bool buildResult(const u8** const type, PyObject* input, Buffer* buffer)
657660
Py_DECREF(string);
658661
return true;
659662
}
663+
case NUMBER: {
664+
PyObject* string = PyObject_CallOneArg(numberToStringFunction, input);
665+
if (string == NULL) {
666+
resultError(input, "NUMBER");
667+
return false;
668+
}
669+
if (!bufferAppendString(buffer, string)) {
670+
Py_DECREF(string);
671+
resultError(input, "NUMBER");
672+
return false;
673+
}
674+
Py_DECREF(string);
675+
return true;
676+
}
660677
case VARCHAR:
661678
case JSON: {
662679
const char* typeName = trinoType == VARCHAR ? "VARCHAR" : "JSON";
@@ -1011,6 +1028,7 @@ int main(const int argc, char* argv[])
10111028
PyObject* trinoModule = loadModule("trino");
10121029
trinoErrorResultFunction = findFunction(trinoModule, "_trino_error_result");
10131030
decimalToStringFunction = findFunction(trinoModule, "_decimal_to_string");
1031+
numberToStringFunction = findFunction(trinoModule, "_number_to_string");
10141032

10151033
DEBUG("Python host initialized");
10161034
return 0;

pyhost.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef enum
3232
JSON = 20,
3333
UUID = 21,
3434
IPADDRESS = 22,
35+
NUMBER = 23,
3536
} TrinoType;
3637

3738
// WebAssembly types

trino.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,12 @@ def _decimal_to_string(value: Decimal):
4747
if not value.is_finite():
4848
raise ValueError('Decimal is not finite: ' + str(value))
4949
return "{:f}".format(value)
50+
51+
def _number_to_string(value: Decimal):
52+
if not isinstance(value, Decimal):
53+
raise ValueError('Not a Decimal: ' + type(value).__name__)
54+
if value.is_nan():
55+
return "NaN"
56+
if value.is_infinite():
57+
return "-Infinity" if value.is_signed() else "+Infinity"
58+
return "{:f}".format(value)

0 commit comments

Comments
 (0)