@@ -1908,15 +1908,15 @@ class TPgCast : public TMutableComputationNode<TPgCast> {
1908
1908
bool ConvertLength = false ;
1909
1909
};
1910
1910
1911
- const i32 PgDateShift = 10957 ;
1912
- const i64 PgTimestampShift = 946684800000000ll ;
1911
+ const i32 PgDateShift = UNIX_EPOCH_JDATE - POSTGRES_EPOCH_JDATE ;
1912
+ const i64 PgTimestampShift = USECS_PER_DAY * (UNIX_EPOCH_JDATE - POSTGRES_EPOCH_JDATE) ;
1913
1913
1914
- inline i32 Date2Pg (ui16 value) {
1915
- return i32 ( value) - PgDateShift;
1914
+ inline i32 Date2Pg (i32 value) {
1915
+ return value + PgDateShift;
1916
1916
}
1917
1917
1918
- inline i64 Timestamp2Pg (ui64 value) {
1919
- return i64 ( value) - PgTimestampShift;
1918
+ inline i64 Timestamp2Pg (i64 value) {
1919
+ return value + PgTimestampShift;
1920
1920
}
1921
1921
1922
1922
inline Interval* Interval2Pg (i64 value) {
@@ -1957,6 +1957,8 @@ NUdf::TUnboxedValuePod ConvertToPgValue(NUdf::TUnboxedValuePod value, TMaybe<NUd
1957
1957
return ScalarDatumToPod (Int64GetDatum (value.Get <i64>()));
1958
1958
case NUdf::EDataSlot::Uint64:
1959
1959
return PointerDatumToPod (NumericGetDatum (Uint64ToPgNumeric (value.Get <ui64>())));
1960
+ case NUdf::EDataSlot::DyNumber:
1961
+ return PointerDatumToPod (NumericGetDatum (DyNumberToPgNumeric (value)));
1960
1962
case NUdf::EDataSlot::Float:
1961
1963
return ScalarDatumToPod (Float4GetDatum (value.Get <float >()));
1962
1964
case NUdf::EDataSlot::Double:
@@ -1979,10 +1981,23 @@ NUdf::TUnboxedValuePod ConvertToPgValue(NUdf::TUnboxedValuePod value, TMaybe<NUd
1979
1981
auto res = Timestamp2Pg (value.Get <ui64>());
1980
1982
return ScalarDatumToPod (res);
1981
1983
}
1982
- case NUdf::EDataSlot::Interval: {
1984
+ case NUdf::EDataSlot::Interval:
1985
+ case NUdf::EDataSlot::Interval64: {
1983
1986
auto res = Interval2Pg (value.Get <i64>());
1984
1987
return PointerDatumToPod (PointerGetDatum (res));
1985
1988
}
1989
+ case NUdf::EDataSlot::Date32: {
1990
+ auto res = Date2Pg (value.Get <i32>());
1991
+ return ScalarDatumToPod (res);
1992
+ }
1993
+ case NUdf::EDataSlot::Datetime64: {
1994
+ auto res = Timestamp2Pg (value.Get <i64>() * 1000000ull );
1995
+ return ScalarDatumToPod (res);
1996
+ }
1997
+ case NUdf::EDataSlot::Timestamp64: {
1998
+ auto res = Timestamp2Pg (value.Get <i64>());
1999
+ return ScalarDatumToPod (res);
2000
+ }
1986
2001
case NUdf::EDataSlot::Json: {
1987
2002
auto input = MakeCString (value.AsStringRef ());
1988
2003
auto res = DirectFunctionCall1Coll (json_in, DEFAULT_COLLATION_OID, PointerGetDatum (input));
@@ -2169,9 +2184,10 @@ template <NUdf::EDataSlot Slot>
2169
2184
class TToPg : public TMutableComputationNode <TToPg<Slot>> {
2170
2185
typedef TMutableComputationNode<TToPg<Slot>> TBaseComputation;
2171
2186
public:
2172
- TToPg (TComputationMutables& mutables, IComputationNode* arg)
2187
+ TToPg (TComputationMutables& mutables, IComputationNode* arg, TDataType* argType )
2173
2188
: TBaseComputation(mutables)
2174
2189
, Arg(arg)
2190
+ , ArgType(argType)
2175
2191
{
2176
2192
}
2177
2193
@@ -2181,7 +2197,13 @@ class TToPg : public TMutableComputationNode<TToPg<Slot>> {
2181
2197
return value.Release ();
2182
2198
}
2183
2199
2184
- 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
+ }
2185
2207
}
2186
2208
2187
2209
private:
@@ -2190,6 +2212,7 @@ class TToPg : public TMutableComputationNode<TToPg<Slot>> {
2190
2212
}
2191
2213
2192
2214
IComputationNode* const Arg;
2215
+ TDataType* ArgType;
2193
2216
};
2194
2217
2195
2218
class TPgArray : public TMutableComputationNode <TPgArray> {
@@ -2781,7 +2804,32 @@ struct TToPgExec {
2781
2804
}
2782
2805
break ;
2783
2806
}
2784
- case NUdf::EDataSlot::Interval: {
2807
+ case NUdf::EDataSlot::Date32: {
2808
+ auto inputPtr = array.GetValues <i32>(1 );
2809
+ auto outputPtr = res->array ()->GetMutableValues <ui64>(1 );
2810
+ for (size_t i = 0 ; i < length; ++i) {
2811
+ outputPtr[i] = Int32GetDatum (Date2Pg (inputPtr[i]));
2812
+ }
2813
+ break ;
2814
+ }
2815
+ case NUdf::EDataSlot::Datetime64: {
2816
+ auto inputPtr = array.GetValues <i64>(1 );
2817
+ auto outputPtr = res->array ()->GetMutableValues <ui64>(1 );
2818
+ for (size_t i = 0 ; i < length; ++i) {
2819
+ outputPtr[i] = Int64GetDatum (Timestamp2Pg (inputPtr[i] * 1000000ull ));
2820
+ }
2821
+ break ;
2822
+ }
2823
+ case NUdf::EDataSlot::Timestamp64: {
2824
+ auto inputPtr = array.GetValues <i64>(1 );
2825
+ auto outputPtr = res->array ()->GetMutableValues <ui64>(1 );
2826
+ for (size_t i = 0 ; i < length; ++i) {
2827
+ outputPtr[i] = Int64GetDatum (Timestamp2Pg (inputPtr[i]));
2828
+ }
2829
+ break ;
2830
+ }
2831
+ case NUdf::EDataSlot::Interval:
2832
+ case NUdf::EDataSlot::Interval64: {
2785
2833
NUdf::TFixedSizeBlockReader<i64, true > reader;
2786
2834
NUdf::TStringArrayBuilder<arrow::BinaryType, true > builder (NKikimr::NMiniKQL::TTypeInfoHelper (), arrow::binary (), *ctx->memory_pool (), length);
2787
2835
for (size_t i = 0 ; i < length; ++i) {
@@ -2881,10 +2929,14 @@ std::shared_ptr<arrow::compute::ScalarKernel> MakeToPgKernel(TType* inputType, T
2881
2929
case NUdf::EDataSlot::Date:
2882
2930
case NUdf::EDataSlot::Datetime:
2883
2931
case NUdf::EDataSlot::Timestamp:
2932
+ case NUdf::EDataSlot::Date32:
2933
+ case NUdf::EDataSlot::Datetime64:
2934
+ case NUdf::EDataSlot::Timestamp64:
2884
2935
break ;
2885
2936
case NUdf::EDataSlot::String:
2886
2937
case NUdf::EDataSlot::Utf8:
2887
2938
case NUdf::EDataSlot::Interval:
2939
+ case NUdf::EDataSlot::Interval64:
2888
2940
case NUdf::EDataSlot::Uint64:
2889
2941
case NUdf::EDataSlot::Yson:
2890
2942
case NUdf::EDataSlot::Json:
@@ -3117,56 +3169,69 @@ TComputationNodeFactory GetPgFactory() {
3117
3169
argType = AS_TYPE (TOptionalType, argType)->GetItemType ();
3118
3170
}
3119
3171
3120
- auto sourceDataSlot = AS_TYPE (TDataType, argType)->GetDataSlot ();
3172
+ auto dataType = AS_TYPE (TDataType, argType);
3173
+ auto sourceDataSlot = dataType->GetDataSlot ();
3121
3174
switch (*sourceDataSlot) {
3122
3175
case NUdf::EDataSlot::Bool:
3123
- return new TToPg<NUdf::EDataSlot::Bool>(ctx.Mutables , arg);
3176
+ return new TToPg<NUdf::EDataSlot::Bool>(ctx.Mutables , arg, dataType );
3124
3177
case NUdf::EDataSlot::Int8:
3125
- return new TToPg<NUdf::EDataSlot::Int8>(ctx.Mutables , arg);
3178
+ return new TToPg<NUdf::EDataSlot::Int8>(ctx.Mutables , arg, dataType );
3126
3179
case NUdf::EDataSlot::Uint8:
3127
- return new TToPg<NUdf::EDataSlot::Uint8>(ctx.Mutables , arg);
3180
+ return new TToPg<NUdf::EDataSlot::Uint8>(ctx.Mutables , arg, dataType);
3128
3181
case NUdf::EDataSlot::Int16:
3129
- return new TToPg<NUdf::EDataSlot::Int16>(ctx.Mutables , arg);
3182
+ return new TToPg<NUdf::EDataSlot::Int16>(ctx.Mutables , arg, dataType );
3130
3183
case NUdf::EDataSlot::Uint16:
3131
- return new TToPg<NUdf::EDataSlot::Uint16>(ctx.Mutables , arg);
3184
+ return new TToPg<NUdf::EDataSlot::Uint16>(ctx.Mutables , arg, dataType);
3132
3185
case NUdf::EDataSlot::Int32:
3133
- return new TToPg<NUdf::EDataSlot::Int32>(ctx.Mutables , arg);
3186
+ return new TToPg<NUdf::EDataSlot::Int32>(ctx.Mutables , arg, dataType );
3134
3187
case NUdf::EDataSlot::Uint32:
3135
- return new TToPg<NUdf::EDataSlot::Uint32>(ctx.Mutables , arg);
3188
+ return new TToPg<NUdf::EDataSlot::Uint32>(ctx.Mutables , arg, dataType );
3136
3189
case NUdf::EDataSlot::Int64:
3137
- return new TToPg<NUdf::EDataSlot::Int64>(ctx.Mutables , arg);
3190
+ return new TToPg<NUdf::EDataSlot::Int64>(ctx.Mutables , arg, dataType );
3138
3191
case NUdf::EDataSlot::Uint64:
3139
- return new TToPg<NUdf::EDataSlot::Uint64>(ctx.Mutables , arg);
3192
+ return new TToPg<NUdf::EDataSlot::Uint64>(ctx.Mutables , arg, dataType );
3140
3193
case NUdf::EDataSlot::Float:
3141
- return new TToPg<NUdf::EDataSlot::Float>(ctx.Mutables , arg);
3194
+ return new TToPg<NUdf::EDataSlot::Float>(ctx.Mutables , arg, dataType );
3142
3195
case NUdf::EDataSlot::Double:
3143
- return new TToPg<NUdf::EDataSlot::Double>(ctx.Mutables , arg);
3196
+ return new TToPg<NUdf::EDataSlot::Double>(ctx.Mutables , arg, dataType );
3144
3197
case NUdf::EDataSlot::Utf8:
3145
- return new TToPg<NUdf::EDataSlot::Utf8>(ctx.Mutables , arg);
3198
+ return new TToPg<NUdf::EDataSlot::Utf8>(ctx.Mutables , arg, dataType );
3146
3199
case NUdf::EDataSlot::String:
3147
- return new TToPg<NUdf::EDataSlot::String>(ctx.Mutables , arg);
3200
+ return new TToPg<NUdf::EDataSlot::String>(ctx.Mutables , arg, dataType );
3148
3201
case NUdf::EDataSlot::Date:
3149
- return new TToPg<NUdf::EDataSlot::Date>(ctx.Mutables , arg);
3202
+ return new TToPg<NUdf::EDataSlot::Date>(ctx.Mutables , arg, dataType );
3150
3203
case NUdf::EDataSlot::Datetime:
3151
- return new TToPg<NUdf::EDataSlot::Datetime>(ctx.Mutables , arg);
3204
+ return new TToPg<NUdf::EDataSlot::Datetime>(ctx.Mutables , arg, dataType );
3152
3205
case NUdf::EDataSlot::Timestamp:
3153
- return new TToPg<NUdf::EDataSlot::Timestamp>(ctx.Mutables , arg);
3206
+ return new TToPg<NUdf::EDataSlot::Timestamp>(ctx.Mutables , arg, dataType );
3154
3207
case NUdf::EDataSlot::Interval:
3155
- return new TToPg<NUdf::EDataSlot::Interval>(ctx.Mutables , arg);
3208
+ return new TToPg<NUdf::EDataSlot::Interval>(ctx.Mutables , arg, dataType );
3156
3209
case NUdf::EDataSlot::TzDate:
3157
- return new TToPg<NUdf::EDataSlot::TzDate>(ctx.Mutables , arg);
3210
+ return new TToPg<NUdf::EDataSlot::TzDate>(ctx.Mutables , arg, dataType );
3158
3211
case NUdf::EDataSlot::TzDatetime:
3159
- return new TToPg<NUdf::EDataSlot::TzDatetime>(ctx.Mutables , arg);
3212
+ return new TToPg<NUdf::EDataSlot::TzDatetime>(ctx.Mutables , arg, dataType );
3160
3213
case NUdf::EDataSlot::TzTimestamp:
3161
- return new TToPg<NUdf::EDataSlot::TzTimestamp>(ctx.Mutables , arg);
3214
+ return new TToPg<NUdf::EDataSlot::TzTimestamp>(ctx.Mutables , arg, dataType);
3215
+ case NUdf::EDataSlot::Date32:
3216
+ return new TToPg<NUdf::EDataSlot::Date32>(ctx.Mutables , arg, dataType);
3217
+ case NUdf::EDataSlot::Datetime64:
3218
+ return new TToPg<NUdf::EDataSlot::Datetime64>(ctx.Mutables , arg, dataType);
3219
+ case NUdf::EDataSlot::Timestamp64:
3220
+ return new TToPg<NUdf::EDataSlot::Timestamp64>(ctx.Mutables , arg, dataType);
3221
+ case NUdf::EDataSlot::Interval64:
3222
+ return new TToPg<NUdf::EDataSlot::Interval64>(ctx.Mutables , arg, dataType);
3162
3223
case NUdf::EDataSlot::Uuid:
3163
- return new TToPg<NUdf::EDataSlot::Uuid>(ctx.Mutables , arg);
3224
+ return new TToPg<NUdf::EDataSlot::Uuid>(ctx.Mutables , arg, dataType );
3164
3225
case NUdf::EDataSlot::Yson:
3165
- return new TToPg<NUdf::EDataSlot::Yson>(ctx.Mutables , arg);
3226
+ return new TToPg<NUdf::EDataSlot::Yson>(ctx.Mutables , arg, dataType );
3166
3227
case NUdf::EDataSlot::Json:
3167
- return new TToPg<NUdf::EDataSlot::Json>(ctx.Mutables , arg);
3228
+ return new TToPg<NUdf::EDataSlot::Json>(ctx.Mutables , arg, dataType );
3168
3229
case NUdf::EDataSlot::JsonDocument:
3169
- 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);
3170
3235
default :
3171
3236
ythrow yexception () << " Unsupported type: " << NUdf::GetDataTypeInfo (*sourceDataSlot).Name ;
3172
3237
}
0 commit comments