Skip to content

Commit 26dddd1

Browse files
committed
numeric
1 parent e5699a6 commit 26dddd1

File tree

10 files changed

+143
-31
lines changed

10 files changed

+143
-31
lines changed

ydb/library/yql/core/type_ann/type_ann_pg.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ const TTypeAnnotationNode* ToPgImpl(TPositionHandle pos, const TTypeAnnotationNo
449449
pgType = "int8";
450450
break;
451451
case NUdf::EDataSlot::Uint64:
452+
case NUdf::EDataSlot::Decimal:
453+
case NUdf::EDataSlot::DyNumber:
452454
pgType = "numeric";
453455
break;
454456
case NUdf::EDataSlot::Float:

ydb/library/yql/parser/pg_wrapper/arrow.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <ydb/library/yql/parser/pg_wrapper/interface/utils.h>
66
#include <ydb/library/yql/minikql/mkql_node_cast.h>
77
#include <ydb/library/yql/minikql/arrow/arrow_util.h>
8+
#include <ydb/library/dynumber/dynumber.h>
9+
#include <ydb/library/yql/public/decimal/yql_decimal.h>
810
#include <util/generic/singleton.h>
911

1012
#include <arrow/compute/cast.h>
@@ -15,6 +17,7 @@
1517
extern "C" {
1618
#include "utils/date.h"
1719
#include "utils/timestamp.h"
20+
#include "utils/fmgrprotos.h"
1821
}
1922

2023
namespace NYql {
@@ -171,6 +174,20 @@ Numeric Uint64ToPgNumeric(ui64 value) {
171174
return ret2;
172175
}
173176

177+
Numeric DecimalToPgNumeric(const NUdf::TUnboxedValuePod& value, ui8 precision, ui8 scale) {
178+
const auto str = NYql::NDecimal::ToString(value.GetInt128(), precision, scale);
179+
Y_ENSURE(str);
180+
return (Numeric)DirectFunctionCall3Coll(numeric_in, DEFAULT_COLLATION_OID,
181+
PointerGetDatum(str), Int32GetDatum(0), Int32GetDatum(-1));
182+
}
183+
184+
Numeric DyNumberToPgNumeric(const NUdf::TUnboxedValuePod& value) {
185+
auto str = NKikimr::NDyNumber::DyNumberToString(value.AsStringRef());
186+
Y_ENSURE(str);
187+
return (Numeric)DirectFunctionCall3Coll(numeric_in, DEFAULT_COLLATION_OID,
188+
PointerGetDatum(str->c_str()), Int32GetDatum(0), Int32GetDatum(-1));
189+
}
190+
174191
Numeric PgFloatToNumeric(double item, ui64 scale, int digits) {
175192
double intPart, fracPart;
176193
bool error;

ydb/library/yql/parser/pg_wrapper/arrow_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ extern "C" {
1111
namespace NYql {
1212

1313
Numeric Uint64ToPgNumeric(ui64 value);
14+
Numeric DecimalToPgNumeric(const NUdf::TUnboxedValuePod& value, ui8 precision, ui8 scale);
15+
Numeric DyNumberToPgNumeric(const NUdf::TUnboxedValuePod& value);
1416
Numeric PgFloatToNumeric(double item, ui64 scale, int digits);
1517
Numeric PgDecimal128ToNumeric(arrow::Decimal128 val, int32_t precision, int32_t scale, Numeric high_bits_mul);
1618
TColumnConverter BuildPgColumnConverter(const std::shared_ptr<arrow::DataType>& originalType, NKikimr::NMiniKQL::TPgType* targetType);

ydb/library/yql/parser/pg_wrapper/comp_factory.cpp

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,8 @@ NUdf::TUnboxedValuePod ConvertToPgValue(NUdf::TUnboxedValuePod value, TMaybe<NUd
19571957
return ScalarDatumToPod(Int64GetDatum(value.Get<i64>()));
19581958
case NUdf::EDataSlot::Uint64:
19591959
return PointerDatumToPod(NumericGetDatum(Uint64ToPgNumeric(value.Get<ui64>())));
1960+
case NUdf::EDataSlot::DyNumber:
1961+
return PointerDatumToPod(NumericGetDatum(DyNumberToPgNumeric(value)));
19601962
case NUdf::EDataSlot::Float:
19611963
return ScalarDatumToPod(Float4GetDatum(value.Get<float>()));
19621964
case NUdf::EDataSlot::Double:
@@ -2182,9 +2184,10 @@ template <NUdf::EDataSlot Slot>
21822184
class TToPg : public TMutableComputationNode<TToPg<Slot>> {
21832185
typedef TMutableComputationNode<TToPg<Slot>> TBaseComputation;
21842186
public:
2185-
TToPg(TComputationMutables& mutables, IComputationNode* arg)
2187+
TToPg(TComputationMutables& mutables, IComputationNode* arg, TDataType* argType)
21862188
: TBaseComputation(mutables)
21872189
, Arg(arg)
2190+
, ArgType(argType)
21882191
{
21892192
}
21902193

@@ -2194,7 +2197,13 @@ class TToPg : public TMutableComputationNode<TToPg<Slot>> {
21942197
return value.Release();
21952198
}
21962199

2197-
return ConvertToPgValue<Slot>(value);
2200+
if constexpr (Slot == NUdf::EDataSlot::Decimal) {
2201+
auto decimalType = static_cast<TDataDecimalType*>(ArgType);
2202+
return PointerDatumToPod(NumericGetDatum(DecimalToPgNumeric(value,
2203+
decimalType->GetParams().first, decimalType->GetParams().second)));
2204+
} else {
2205+
return ConvertToPgValue<Slot>(value);
2206+
}
21982207
}
21992208

22002209
private:
@@ -2203,6 +2212,7 @@ class TToPg : public TMutableComputationNode<TToPg<Slot>> {
22032212
}
22042213

22052214
IComputationNode* const Arg;
2215+
TDataType* ArgType;
22062216
};
22072217

22082218
class TPgArray : public TMutableComputationNode<TPgArray> {
@@ -3159,64 +3169,69 @@ TComputationNodeFactory GetPgFactory() {
31593169
argType = AS_TYPE(TOptionalType, argType)->GetItemType();
31603170
}
31613171

3162-
auto sourceDataSlot = AS_TYPE(TDataType, argType)->GetDataSlot();
3172+
auto dataType = AS_TYPE(TDataType, argType);
3173+
auto sourceDataSlot = dataType->GetDataSlot();
31633174
switch (*sourceDataSlot) {
31643175
case NUdf::EDataSlot::Bool:
3165-
return new TToPg<NUdf::EDataSlot::Bool>(ctx.Mutables, arg);
3176+
return new TToPg<NUdf::EDataSlot::Bool>(ctx.Mutables, arg, dataType);
31663177
case NUdf::EDataSlot::Int8:
3167-
return new TToPg<NUdf::EDataSlot::Int8>(ctx.Mutables, arg);
3178+
return new TToPg<NUdf::EDataSlot::Int8>(ctx.Mutables, arg, dataType);
31683179
case NUdf::EDataSlot::Uint8:
3169-
return new TToPg<NUdf::EDataSlot::Uint8>(ctx.Mutables, arg);
3180+
return new TToPg<NUdf::EDataSlot::Uint8>(ctx.Mutables, arg, dataType);
31703181
case NUdf::EDataSlot::Int16:
3171-
return new TToPg<NUdf::EDataSlot::Int16>(ctx.Mutables, arg);
3182+
return new TToPg<NUdf::EDataSlot::Int16>(ctx.Mutables, arg, dataType);
31723183
case NUdf::EDataSlot::Uint16:
3173-
return new TToPg<NUdf::EDataSlot::Uint16>(ctx.Mutables, arg);
3184+
return new TToPg<NUdf::EDataSlot::Uint16>(ctx.Mutables, arg, dataType);
31743185
case NUdf::EDataSlot::Int32:
3175-
return new TToPg<NUdf::EDataSlot::Int32>(ctx.Mutables, arg);
3186+
return new TToPg<NUdf::EDataSlot::Int32>(ctx.Mutables, arg, dataType);
31763187
case NUdf::EDataSlot::Uint32:
3177-
return new TToPg<NUdf::EDataSlot::Uint32>(ctx.Mutables, arg);
3188+
return new TToPg<NUdf::EDataSlot::Uint32>(ctx.Mutables, arg, dataType);
31783189
case NUdf::EDataSlot::Int64:
3179-
return new TToPg<NUdf::EDataSlot::Int64>(ctx.Mutables, arg);
3190+
return new TToPg<NUdf::EDataSlot::Int64>(ctx.Mutables, arg, dataType);
31803191
case NUdf::EDataSlot::Uint64:
3181-
return new TToPg<NUdf::EDataSlot::Uint64>(ctx.Mutables, arg);
3192+
return new TToPg<NUdf::EDataSlot::Uint64>(ctx.Mutables, arg, dataType);
31823193
case NUdf::EDataSlot::Float:
3183-
return new TToPg<NUdf::EDataSlot::Float>(ctx.Mutables, arg);
3194+
return new TToPg<NUdf::EDataSlot::Float>(ctx.Mutables, arg, dataType);
31843195
case NUdf::EDataSlot::Double:
3185-
return new TToPg<NUdf::EDataSlot::Double>(ctx.Mutables, arg);
3196+
return new TToPg<NUdf::EDataSlot::Double>(ctx.Mutables, arg, dataType);
31863197
case NUdf::EDataSlot::Utf8:
3187-
return new TToPg<NUdf::EDataSlot::Utf8>(ctx.Mutables, arg);
3198+
return new TToPg<NUdf::EDataSlot::Utf8>(ctx.Mutables, arg, dataType);
31883199
case NUdf::EDataSlot::String:
3189-
return new TToPg<NUdf::EDataSlot::String>(ctx.Mutables, arg);
3200+
return new TToPg<NUdf::EDataSlot::String>(ctx.Mutables, arg, dataType);
31903201
case NUdf::EDataSlot::Date:
3191-
return new TToPg<NUdf::EDataSlot::Date>(ctx.Mutables, arg);
3202+
return new TToPg<NUdf::EDataSlot::Date>(ctx.Mutables, arg, dataType);
31923203
case NUdf::EDataSlot::Datetime:
3193-
return new TToPg<NUdf::EDataSlot::Datetime>(ctx.Mutables, arg);
3204+
return new TToPg<NUdf::EDataSlot::Datetime>(ctx.Mutables, arg, dataType);
31943205
case NUdf::EDataSlot::Timestamp:
3195-
return new TToPg<NUdf::EDataSlot::Timestamp>(ctx.Mutables, arg);
3206+
return new TToPg<NUdf::EDataSlot::Timestamp>(ctx.Mutables, arg, dataType);
31963207
case NUdf::EDataSlot::Interval:
3197-
return new TToPg<NUdf::EDataSlot::Interval>(ctx.Mutables, arg);
3208+
return new TToPg<NUdf::EDataSlot::Interval>(ctx.Mutables, arg, dataType);
31983209
case NUdf::EDataSlot::TzDate:
3199-
return new TToPg<NUdf::EDataSlot::TzDate>(ctx.Mutables, arg);
3210+
return new TToPg<NUdf::EDataSlot::TzDate>(ctx.Mutables, arg, dataType);
32003211
case NUdf::EDataSlot::TzDatetime:
3201-
return new TToPg<NUdf::EDataSlot::TzDatetime>(ctx.Mutables, arg);
3212+
return new TToPg<NUdf::EDataSlot::TzDatetime>(ctx.Mutables, arg, dataType);
32023213
case NUdf::EDataSlot::TzTimestamp:
3203-
return new TToPg<NUdf::EDataSlot::TzTimestamp>(ctx.Mutables, arg);
3214+
return new TToPg<NUdf::EDataSlot::TzTimestamp>(ctx.Mutables, arg, dataType);
32043215
case NUdf::EDataSlot::Date32:
3205-
return new TToPg<NUdf::EDataSlot::Date32>(ctx.Mutables, arg);
3216+
return new TToPg<NUdf::EDataSlot::Date32>(ctx.Mutables, arg, dataType);
32063217
case NUdf::EDataSlot::Datetime64:
3207-
return new TToPg<NUdf::EDataSlot::Datetime64>(ctx.Mutables, arg);
3218+
return new TToPg<NUdf::EDataSlot::Datetime64>(ctx.Mutables, arg, dataType);
32083219
case NUdf::EDataSlot::Timestamp64:
3209-
return new TToPg<NUdf::EDataSlot::Timestamp64>(ctx.Mutables, arg);
3220+
return new TToPg<NUdf::EDataSlot::Timestamp64>(ctx.Mutables, arg, dataType);
32103221
case NUdf::EDataSlot::Interval64:
3211-
return new TToPg<NUdf::EDataSlot::Interval64>(ctx.Mutables, arg);
3222+
return new TToPg<NUdf::EDataSlot::Interval64>(ctx.Mutables, arg, dataType);
32123223
case NUdf::EDataSlot::Uuid:
3213-
return new TToPg<NUdf::EDataSlot::Uuid>(ctx.Mutables, arg);
3224+
return new TToPg<NUdf::EDataSlot::Uuid>(ctx.Mutables, arg, dataType);
32143225
case NUdf::EDataSlot::Yson:
3215-
return new TToPg<NUdf::EDataSlot::Yson>(ctx.Mutables, arg);
3226+
return new TToPg<NUdf::EDataSlot::Yson>(ctx.Mutables, arg, dataType);
32163227
case NUdf::EDataSlot::Json:
3217-
return new TToPg<NUdf::EDataSlot::Json>(ctx.Mutables, arg);
3228+
return new TToPg<NUdf::EDataSlot::Json>(ctx.Mutables, arg, dataType);
32183229
case NUdf::EDataSlot::JsonDocument:
3219-
return new TToPg<NUdf::EDataSlot::JsonDocument>(ctx.Mutables, arg);
3230+
return new TToPg<NUdf::EDataSlot::JsonDocument>(ctx.Mutables, arg, dataType);
3231+
case NUdf::EDataSlot::Decimal:
3232+
return new TToPg<NUdf::EDataSlot::Decimal>(ctx.Mutables, arg, dataType);
3233+
case NUdf::EDataSlot::DyNumber:
3234+
return new TToPg<NUdf::EDataSlot::DyNumber>(ctx.Mutables, arg, dataType);
32203235
default:
32213236
ythrow yexception() << "Unsupported type: " << NUdf::GetDataTypeInfo(*sourceDataSlot).Name;
32223237
}

ydb/library/yql/parser/pg_wrapper/ya.make

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ PEERDIR(
7979
ydb/library/yql/public/issue
8080
ydb/library/yql/public/udf
8181
ydb/library/yql/utils
82+
ydb/library/yql/public/decimal
8283
ydb/library/binary_json
84+
ydb/library/dynumber
8385
ydb/library/uuid
8486

8587
contrib/libs/icu

ydb/library/yql/tests/sql/dq_file/part14/canondata/result.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,28 @@
21932193
}
21942194
],
21952195
"test.test[pg-nullif-default.txt-Results]": [],
2196+
"test.test[pg-numeric_to_pg-default.txt-Analyze]": [
2197+
{
2198+
"checksum": "b4dd508a329723c74293d80f0278c705",
2199+
"size": 505,
2200+
"uri": "https://{canondata_backend}/1847551/c04b6845f7d6b8061d0f3bb18348cc2396fe3c4b/resource.tar.gz#test.test_pg-numeric_to_pg-default.txt-Analyze_/plan.txt"
2201+
}
2202+
],
2203+
"test.test[pg-numeric_to_pg-default.txt-Debug]": [
2204+
{
2205+
"checksum": "9b4c62254b0fdbd9ad13924e5f3f4ea7",
2206+
"size": 544,
2207+
"uri": "https://{canondata_backend}/1847551/c04b6845f7d6b8061d0f3bb18348cc2396fe3c4b/resource.tar.gz#test.test_pg-numeric_to_pg-default.txt-Debug_/opt.yql_patched"
2208+
}
2209+
],
2210+
"test.test[pg-numeric_to_pg-default.txt-Plan]": [
2211+
{
2212+
"checksum": "b4dd508a329723c74293d80f0278c705",
2213+
"size": 505,
2214+
"uri": "https://{canondata_backend}/1847551/c04b6845f7d6b8061d0f3bb18348cc2396fe3c4b/resource.tar.gz#test.test_pg-numeric_to_pg-default.txt-Plan_/plan.txt"
2215+
}
2216+
],
2217+
"test.test[pg-numeric_to_pg-default.txt-Results]": [],
21962218
"test.test[pg-range_function_multi-default.txt-Analyze]": [
21972219
{
21982220
"checksum": "b2a2eb5d6b0a138ee924c128fc7738ef",

ydb/library/yql/tests/sql/hybrid_file/part4/canondata/result.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,20 @@
19171917
"uri": "https://{canondata_backend}/1889210/431569691fa60b20bf9ef4cc94610d8f1b1518e2/resource.tar.gz#test.test_pg-nothing-default.txt-Plan_/plan.txt"
19181918
}
19191919
],
1920+
"test.test[pg-numeric_to_pg-default.txt-Debug]": [
1921+
{
1922+
"checksum": "7041589a99b95f150c08439ec13e21d2",
1923+
"size": 543,
1924+
"uri": "https://{canondata_backend}/1847551/682cc73a2d58def116940ca081e758391e0f27cb/resource.tar.gz#test.test_pg-numeric_to_pg-default.txt-Debug_/opt.yql_patched"
1925+
}
1926+
],
1927+
"test.test[pg-numeric_to_pg-default.txt-Plan]": [
1928+
{
1929+
"checksum": "b4dd508a329723c74293d80f0278c705",
1930+
"size": 505,
1931+
"uri": "https://{canondata_backend}/1847551/682cc73a2d58def116940ca081e758391e0f27cb/resource.tar.gz#test.test_pg-numeric_to_pg-default.txt-Plan_/plan.txt"
1932+
}
1933+
],
19201934
"test.test[pg-order_by_agg_input_columns_keys-default.txt-Debug]": [
19211935
{
19221936
"checksum": "9059183606f5b34e7e58570133ef9d3b",

ydb/library/yql/tests/sql/sql2yql/canondata/result.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11416,6 +11416,13 @@
1141611416
"uri": "https://{canondata_backend}/1900335/ff80d8557940adda4b5c22a224a0d0bc8e975889/resource.tar.gz#test_sql2yql.test_pg-numbers_to_pg_/sql.yql"
1141711417
}
1141811418
],
11419+
"test_sql2yql.test[pg-numeric_to_pg]": [
11420+
{
11421+
"checksum": "14e17e7194d9c1bb1eb7b9119dd78f39",
11422+
"size": 1704,
11423+
"uri": "https://{canondata_backend}/1847551/ca138a141bfa621116f6d751dcc66c2d966547d9/resource.tar.gz#test_sql2yql.test_pg-numeric_to_pg_/sql.yql"
11424+
}
11425+
],
1141911426
"test_sql2yql.test[pg-order_by_agg_extra_for_keys]": [
1142011427
{
1142111428
"checksum": "e5d2f7b4f76fafc7565d1b2c620f3f31",
@@ -29210,6 +29217,13 @@
2921029217
"uri": "https://{canondata_backend}/1900335/ff80d8557940adda4b5c22a224a0d0bc8e975889/resource.tar.gz#test_sql_format.test_pg-numbers_to_pg_/formatted.sql"
2921129218
}
2921229219
],
29220+
"test_sql_format.test[pg-numeric_to_pg]": [
29221+
{
29222+
"checksum": "5a745f5a514580aeb085aab95c567862",
29223+
"size": 171,
29224+
"uri": "https://{canondata_backend}/1847551/ca138a141bfa621116f6d751dcc66c2d966547d9/resource.tar.gz#test_sql_format.test_pg-numeric_to_pg_/formatted.sql"
29225+
}
29226+
],
2921329227
"test_sql_format.test[pg-pg_in_dict_key_with_stable_pickle]": [
2921429228
{
2921529229
"checksum": "f758865b5d78a28d3f519575ea234704",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
select ToPg(Decimal('nan',25,1)), ToPg(Decimal('inf',25,1)), ToPg(Decimal('-inf',25,1)),
2+
ToPg(Decimal('1.23',25,1)), ToPg(DyNumber("-10.23"));
3+

ydb/library/yql/tests/sql/yt_native_file/part14/canondata/result.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,27 @@
21392139
"uri": "https://{canondata_backend}/1925842/1ad87feba078148d2bd94b42ca0bec2e4edbaee6/resource.tar.gz#test.test_pg-nullif-default.txt-Results_/results.txt"
21402140
}
21412141
],
2142+
"test.test[pg-numeric_to_pg-default.txt-Debug]": [
2143+
{
2144+
"checksum": "8c69fe68581dea7dd5094faf43b834fb",
2145+
"size": 476,
2146+
"uri": "https://{canondata_backend}/1937492/061b4d6cd950f54394b5b967f2fda3605781cbb4/resource.tar.gz#test.test_pg-numeric_to_pg-default.txt-Debug_/opt.yql"
2147+
}
2148+
],
2149+
"test.test[pg-numeric_to_pg-default.txt-Plan]": [
2150+
{
2151+
"checksum": "b4dd508a329723c74293d80f0278c705",
2152+
"size": 505,
2153+
"uri": "https://{canondata_backend}/1937492/061b4d6cd950f54394b5b967f2fda3605781cbb4/resource.tar.gz#test.test_pg-numeric_to_pg-default.txt-Plan_/plan.txt"
2154+
}
2155+
],
2156+
"test.test[pg-numeric_to_pg-default.txt-Results]": [
2157+
{
2158+
"checksum": "fb905d5c3db8957c8e1ab971e8a94ce3",
2159+
"size": 1861,
2160+
"uri": "https://{canondata_backend}/1937492/061b4d6cd950f54394b5b967f2fda3605781cbb4/resource.tar.gz#test.test_pg-numeric_to_pg-default.txt-Results_/results.txt"
2161+
}
2162+
],
21422163
"test.test[pg-range_function_multi-default.txt-Debug]": [
21432164
{
21442165
"checksum": "f02b3edd21dc6bacd29b70cae9855b0c",

0 commit comments

Comments
 (0)