Skip to content

YQL-17439: block implementation of From/IntervalFrom/To Datetime UDF functions #1755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions ydb/library/yql/public/udf/arrow/udf_arrow_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <arrow/chunked_array.h>
#include <arrow/compute/kernel.h>
#include <arrow/compute/exec_internal.h>
#include <arrow/util/bitmap_ops.h>

namespace NYql {
namespace NUdf {
Expand Down Expand Up @@ -224,7 +225,7 @@ class TSimpleArrowUdfImpl : public TBoxedValue {
};

inline void PrepareSimpleArrowUdf(IFunctionTypeInfoBuilder& builder, TType* signature, TType* userType, TExec exec, bool typesOnly,
const TString& name) {
const TString& name, arrow::compute::NullHandling::type nullHandling = arrow::compute::NullHandling::type::COMPUTED_NO_PREALLOCATE) {
auto typeInfoHelper = builder.TypeInfoHelper();
TCallableTypeInspector callableInspector(*typeInfoHelper, signature);
Y_ENSURE(callableInspector);
Expand Down Expand Up @@ -284,7 +285,7 @@ inline void PrepareSimpleArrowUdf(IFunctionTypeInfoBuilder& builder, TType* sign

if (!typesOnly) {
builder.Implementation(new TSimpleArrowUdfImpl(argBlockTypes, callableInspector.GetReturnType(),
onlyScalars, exec, builder, name, arrow::compute::NullHandling::COMPUTED_NO_PREALLOCATE));
onlyScalars, exec, builder, name, nullHandling));
}
}

Expand Down Expand Up @@ -422,6 +423,49 @@ arrow::Status UnaryPreallocatedExecImpl(arrow::compute::KernelContext* ctx, cons
return arrow::Status::OK();
}

template<typename TInput, typename TOutput, std::pair<TOutput, bool> Core(TInput)>
struct TUnaryUnsafeFixedSizeFilterKernel {
static arrow::Status Do(arrow::compute::KernelContext* ctx, const arrow::compute::ExecBatch& batch, arrow::Datum* res) {
static_assert(std::is_arithmetic<TInput>::value);

Y_UNUSED(ctx);
const auto& inArray = batch.values.front().array();
const auto* inValues = inArray->GetValues<TInput>(1);

const auto length = inArray->length;

auto& outArray = res->array();
auto* outValues = outArray->GetMutableValues<TOutput>(1);

TTypedBufferBuilder<uint8_t> nullBuilder(arrow::default_memory_pool());
nullBuilder.Reserve(length);

bool isAllNull = inArray->GetNullCount() == length;
if (!isAllNull) {
for (i64 i = 0; i < length; ++i) {
auto [output, isValid] = Core(inValues[i]);
outValues[i] = output;
nullBuilder.UnsafeAppend(isValid);
}
} else {
nullBuilder.UnsafeAppend(length, 0);
}
auto validMask = nullBuilder.Finish();
validMask = MakeDenseBitmap(validMask->data(), length, arrow::default_memory_pool());

auto inMask = inArray->buffers[0];
if (inMask) {
outArray->buffers[0] = AllocateBitmapWithReserve(length, arrow::default_memory_pool());
arrow::internal::BitmapAnd(validMask->data(), 0, inArray->buffers[0]->data(), inArray->offset, outArray->length, outArray->offset, outArray->buffers[0]->mutable_data());
} else {
outArray->buffers[0] = std::move(validMask);
}

return arrow::Status::OK();
}
};


template <typename TInput, typename TOutput, TOutput(*Core)(TInput)>
class TUnaryOverOptionalImpl : public TBoxedValue {
public:
Expand Down Expand Up @@ -477,5 +521,21 @@ class TUnaryOverOptionalImpl : public TBoxedValue {
return false; \
}

#define END_ARROW_UDF_WITH_NULL_HANDLING(udfNameBlocks, exec, nullHandling) \
inline bool udfNameBlocks::DeclareSignature(\
const ::NYql::NUdf::TStringRef& name, \
::NYql::NUdf::TType* userType, \
::NYql::NUdf::IFunctionTypeInfoBuilder& builder, \
bool typesOnly) { \
if (Name() == name) { \
PrepareSimpleArrowUdf(builder, GetSignatureType(builder), userType, exec, typesOnly, TString(name), nullHandling); \
return true; \
} \
return false; \
}

#define END_SIMPLE_ARROW_UDF(udfName, exec) \
END_ARROW_UDF(udfName##_BlocksImpl, exec)

#define END_SIMPLE_ARROW_UDF_WITH_NULL_HANDLING(udfName, exec, nullHandling) \
END_ARROW_UDF_WITH_NULL_HANDLING(udfName##_BlocksImpl, exec, nullHandling)
79 changes: 58 additions & 21 deletions ydb/library/yql/udfs/common/datetime2/datetime_udf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ extern const char GetMicrosecondOfSecondName[] = "GetMicrosecondOfSecond";

extern const char TMResourceName[] = "DateTime2.TM";

const ui64 UsecondsInDay = 86400000000ll;
const ui64 UsecondsInHour = 3600000000ll;
const ui64 UsecondsInMinute = 60000000ll;
const ui64 UsecondsInSecond = 1000000ll;
const ui64 UsecondsInMilliseconds = 1000ll;

template <const char* TFuncName, typename TResult, ui32 ScaleAfterSeconds>
class TToUnits {
public:
Expand Down Expand Up @@ -862,8 +868,8 @@ NUdf::TUnboxedValuePod DoAddYears(const NUdf::TUnboxedValuePod& date, i64 years,
};

// From*

SIMPLE_STRICT_UDF(TFromSeconds, TOptional<TTimestamp>(TAutoMap<ui32>)) {
BEGIN_SIMPLE_STRICT_ARROW_UDF(TFromSeconds, TOptional<TTimestamp>(TAutoMap<ui32>)) {
Y_UNUSED(valueBuilder);
auto res = args[0].Get<ui32>();
if (!ValidateDatetime(res)) {
Expand All @@ -872,7 +878,11 @@ NUdf::TUnboxedValuePod DoAddYears(const NUdf::TUnboxedValuePod& date, i64 years,
return TUnboxedValuePod((ui64)(res * 1000000ull));
}

SIMPLE_STRICT_UDF(TFromMilliseconds, TOptional<TTimestamp>(TAutoMap<ui64>)) {
using TFromSecondsKernel = TUnaryUnsafeFixedSizeFilterKernel<ui32, ui64,
[] (ui32 seconds) { return std::make_pair(ui64(seconds * 1000000ull), ValidateDatetime(seconds)); }>;
END_SIMPLE_ARROW_UDF(TFromSeconds, TFromSecondsKernel::Do);

BEGIN_SIMPLE_STRICT_ARROW_UDF(TFromMilliseconds, TOptional<TTimestamp>(TAutoMap<ui64>)) {
Y_UNUSED(valueBuilder);
auto res = args[0].Get<ui64>();
if (res >= MAX_TIMESTAMP / 1000u) {
Expand All @@ -881,7 +891,11 @@ NUdf::TUnboxedValuePod DoAddYears(const NUdf::TUnboxedValuePod& date, i64 years,
return TUnboxedValuePod(res * 1000u);
}

SIMPLE_STRICT_UDF(TFromMicroseconds, TOptional<TTimestamp>(TAutoMap<ui64>)) {
using TFromMillisecondsKernel = TUnaryUnsafeFixedSizeFilterKernel<ui64, ui64,
[] (ui64 milliseconds) { return std::make_pair(ui64(milliseconds * 1000u), milliseconds < MAX_TIMESTAMP / 1000u); }>;
END_SIMPLE_ARROW_UDF(TFromMilliseconds, TFromMillisecondsKernel::Do);

BEGIN_SIMPLE_STRICT_ARROW_UDF(TFromMicroseconds, TOptional<TTimestamp>(TAutoMap<ui64>)) {
Y_UNUSED(valueBuilder);
auto res = args[0].Get<ui64>();
if (!ValidateTimestamp(res)) {
Expand All @@ -890,58 +904,81 @@ NUdf::TUnboxedValuePod DoAddYears(const NUdf::TUnboxedValuePod& date, i64 years,
return TUnboxedValuePod(res);
}

SIMPLE_STRICT_UDF(TIntervalFromDays, TOptional<TInterval>(TAutoMap<i32>)) {
using TFromMicrosecondsKernel = TUnaryUnsafeFixedSizeFilterKernel<ui64, ui64,
[] (ui64 timestamp) { return std::make_pair(timestamp, ValidateTimestamp(timestamp)); }>;
END_SIMPLE_ARROW_UDF(TFromMicroseconds, TFromMicrosecondsKernel::Do);

template <typename TInput, i64 Multiplier>
using TIntervalFromKernel = TUnaryUnsafeFixedSizeFilterKernel<TInput, i64,
[] (TInput interval) { return std::make_pair(i64(interval * Multiplier), ValidateInterval(interval)); }>;

BEGIN_SIMPLE_STRICT_ARROW_UDF(TIntervalFromDays, TOptional<TInterval>(TAutoMap<i32>)) {
Y_UNUSED(valueBuilder);
const i64 res = i64(args[0].Get<i32>()) * 86400000000ll;
const i64 res = i64(args[0].Get<i32>()) * UsecondsInDay;
return ValidateInterval(res) ? TUnboxedValuePod(res) : TUnboxedValuePod();
}
END_SIMPLE_ARROW_UDF(TIntervalFromDays, (TIntervalFromKernel<i32, UsecondsInDay>::Do));

SIMPLE_STRICT_UDF(TIntervalFromHours, TOptional<TInterval>(TAutoMap<i32>)) {
BEGIN_SIMPLE_STRICT_ARROW_UDF(TIntervalFromHours, TOptional<TInterval>(TAutoMap<i32>)) {
Y_UNUSED(valueBuilder);
const i64 res = i64(args[0].Get<i32>()) * 3600000000ll;
const i64 res = i64(args[0].Get<i32>()) * UsecondsInHour;
return ValidateInterval(res) ? TUnboxedValuePod(res) : TUnboxedValuePod();
}
END_SIMPLE_ARROW_UDF(TIntervalFromHours, (TIntervalFromKernel<i32, UsecondsInHour>::Do));

SIMPLE_STRICT_UDF(TIntervalFromMinutes, TOptional<TInterval>(TAutoMap<i32>)) {
BEGIN_SIMPLE_STRICT_ARROW_UDF(TIntervalFromMinutes, TOptional<TInterval>(TAutoMap<i32>)) {
Y_UNUSED(valueBuilder);
const i64 res = i64(args[0].Get<i32>()) * 60000000ll;
const i64 res = i64(args[0].Get<i32>()) * UsecondsInMinute;
return ValidateInterval(res) ? TUnboxedValuePod(res) : TUnboxedValuePod();
}
END_SIMPLE_ARROW_UDF(TIntervalFromMinutes, (TIntervalFromKernel<i32, UsecondsInMinute>::Do));

SIMPLE_STRICT_UDF(TIntervalFromSeconds, TOptional<TInterval>(TAutoMap<i32>)) {
BEGIN_SIMPLE_STRICT_ARROW_UDF(TIntervalFromSeconds, TOptional<TInterval>(TAutoMap<i32>)) {
Y_UNUSED(valueBuilder);
const i64 res = i64(args[0].Get<i32>()) * 1000000ll;
const i64 res = i64(args[0].Get<i32>()) * UsecondsInSecond;
return ValidateInterval(res) ? TUnboxedValuePod(res) : TUnboxedValuePod();
}
END_SIMPLE_ARROW_UDF(TIntervalFromSeconds, (TIntervalFromKernel<i32, UsecondsInSecond>::Do));

SIMPLE_STRICT_UDF(TIntervalFromMilliseconds, TOptional<TInterval>(TAutoMap<i64>)) {
BEGIN_SIMPLE_STRICT_ARROW_UDF(TIntervalFromMilliseconds, TOptional<TInterval>(TAutoMap<i64>)) {
Y_UNUSED(valueBuilder);
const i64 res = i64(args[0].Get<i64>()) * 1000ll;
const i64 res = i64(args[0].Get<i64>()) * UsecondsInMilliseconds;
return ValidateInterval(res) ? TUnboxedValuePod(res) : TUnboxedValuePod();
}
END_SIMPLE_ARROW_UDF(TIntervalFromMilliseconds, (TIntervalFromKernel<i64, UsecondsInMilliseconds>::Do));

SIMPLE_STRICT_UDF(TIntervalFromMicroseconds, TOptional<TInterval>(TAutoMap<i64>)) {
BEGIN_SIMPLE_STRICT_ARROW_UDF(TIntervalFromMicroseconds, TOptional<TInterval>(TAutoMap<i64>)) {
Y_UNUSED(valueBuilder);
const i64 res = args[0].Get<i64>();
return ValidateInterval(res) ? TUnboxedValuePod(res) : TUnboxedValuePod();
}
END_SIMPLE_ARROW_UDF(TIntervalFromMicroseconds, (TIntervalFromKernel<i64, 1>::Do));

// To*

SIMPLE_STRICT_UDF(TToDays, i32(TAutoMap<TInterval>)) {
BEGIN_SIMPLE_STRICT_ARROW_UDF(TToDays, i32(TAutoMap<TInterval>)) {
Y_UNUSED(valueBuilder);
return TUnboxedValuePod(i32(args[0].Get<i64>() / 86400000000ll));
return TUnboxedValuePod(i32(args[0].Get<i64>() / UsecondsInDay));
}
END_SIMPLE_ARROW_UDF_WITH_NULL_HANDLING(TToDays,
(UnaryPreallocatedExecImpl<i32, i32, [] (i32 arg) { return i32(arg / UsecondsInDay); }>),
arrow::compute::NullHandling::INTERSECTION);

SIMPLE_STRICT_UDF(TToHours, i32(TAutoMap<TInterval>)) {
BEGIN_SIMPLE_STRICT_ARROW_UDF(TToHours, i32(TAutoMap<TInterval>)) {
Y_UNUSED(valueBuilder);
return TUnboxedValuePod(i32(args[0].Get<i64>() / 3600000000ll));
return TUnboxedValuePod(i32(args[0].Get<i64>() / UsecondsInHour));
}
END_SIMPLE_ARROW_UDF_WITH_NULL_HANDLING(TToHours,
(UnaryPreallocatedExecImpl<i32, i32, [] (i32 arg) { return i32(arg / UsecondsInHour); }>),
arrow::compute::NullHandling::INTERSECTION);

SIMPLE_STRICT_UDF(TToMinutes, i32(TAutoMap<TInterval>)) {
BEGIN_SIMPLE_STRICT_ARROW_UDF(TToMinutes, i32(TAutoMap<TInterval>)) {
Y_UNUSED(valueBuilder);
return TUnboxedValuePod(i32(args[0].Get<i64>() / 60000000ll));
return TUnboxedValuePod(i32(args[0].Get<i64>() / UsecondsInMinute));
}
END_SIMPLE_ARROW_UDF_WITH_NULL_HANDLING(TToMinutes,
(UnaryPreallocatedExecImpl<i32, i32, [] (i32 arg) { return i32(arg / UsecondsInMinute); }>),
arrow::compute::NullHandling::INTERSECTION);

// StartOf*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"test.test[BlockFrom]": [
{
"uri": "file://test.test_BlockFrom_/results.txt"
}
],
"test.test[BlockGet]": [
{
"uri": "file://test.test_BlockGet_/results.txt"
Expand Down Expand Up @@ -34,6 +39,11 @@
"uri": "file://test.test_ImplicitSplit_/results.txt"
}
],
"test.test[MultirowBlockTo]": [
{
"uri": "file://test.test_MultirowBlockTo_/results.txt"
}
],
"test.test[ParseIso8601]": [
{
"uri": "file://test.test_ParseIso8601_/results.txt"
Expand Down
Loading