Skip to content

Commit

Permalink
Add IListBuilder to UDF ABI (#8914)
Browse files Browse the repository at this point in the history
  • Loading branch information
avevad authored Sep 12, 2024
1 parent 10c0003 commit 52e48f7
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2909,6 +2909,40 @@ class TDictValueBuilder: public NUdf::IDictValueBuilder
TKeyPayloadPairVector Items_;
};

///////////////////////////////////////////////////////////////////////////////
// TListValueBuilder
///////////////////////////////////////////////////////////////////////////////
class TListValueBuilder: public NUdf::IListValueBuilder {
public:
explicit TListValueBuilder(const THolderFactory& HolderFactory)
: HolderFactory_(HolderFactory)
{}

// Destroys (moves out from) the element
IListValueBuilder& Add(NUdf::TUnboxedValue&& element) final {
List_.emplace_back(element);
return *this;
}

// Destroys (moves out from) the elements
IListValueBuilder& AddMany(const NUdf::TUnboxedValue* elements, size_t count) final {
std::copy_n(std::make_move_iterator(elements), count, std::back_inserter(List_));
return *this;
}

NUdf::TUnboxedValue Build() final {
if (List_.empty()) {
return HolderFactory_.GetEmptyContainerLazy();
}

return HolderFactory_.VectorAsVectorHolder(std::move(List_));
}

private:
const NMiniKQL::THolderFactory& HolderFactory_;
TUnboxedValueVector List_;
};

//////////////////////////////////////////////////////////////////////////////
// THolderFactory
//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -3450,6 +3484,10 @@ NUdf::IDictValueBuilder::TPtr THolderFactory::NewDict(
GetCompare(*keyType, useIHash));
}

NUdf::IListValueBuilder::TPtr THolderFactory::NewList() const {
return new TListValueBuilder(*this);
}

#define DEFINE_HASHED_SINGLE_FIXED_MAP_OPT(xType) \
template NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedMapHolder<xType, true> \
(TValuesDictHashSingleFixedMap<xType>&& map, std::optional<NUdf::TUnboxedValue>&& nullPayload) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,8 @@ class THolderFactory: private TNonCopyable
const NUdf::TType* dictType,
ui32 flags) const;

NUdf::IListValueBuilder::TPtr NewList() const;

NUdf::TUnboxedValuePod Cloned(const NUdf::TUnboxedValuePod& it) const;
NUdf::TUnboxedValuePod Reversed(const NUdf::TUnboxedValuePod& it) const;

Expand Down
22 changes: 9 additions & 13 deletions ydb/library/yql/minikql/computation/mkql_value_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,15 @@ NUdf::TUnboxedValue TDefaultValueBuilder::SubString(NUdf::TUnboxedValuePod value
}

NUdf::TUnboxedValue TDefaultValueBuilder::NewList(NUdf::TUnboxedValue* items, ui64 count) const {
if (!items || !count)
if (items == nullptr || count == 0) {
return HolderFactory_.GetEmptyContainerLazy();

if (count < Max<ui32>()) {
NUdf::TUnboxedValue* inplace = nullptr;
auto array = HolderFactory_.CreateDirectArrayHolder(count, inplace);
for (ui64 i = 0; i < count; ++i)
*inplace++ = std::move(*items++);
return std::move(array);
}

TDefaultListRepresentation list;
for (ui64 i = 0; i < count; ++i) {
list = list.Append(std::move(*items++));
}
NUdf::TUnboxedValue* inplace = nullptr;
auto array = HolderFactory_.CreateDirectArrayHolder(count, inplace);
std::copy_n(std::make_move_iterator(items), count, inplace);

return HolderFactory_.CreateDirectListHolder(std::move(list));
return array;
}

NUdf::TUnboxedValue TDefaultValueBuilder::ReverseList(const NUdf::TUnboxedValuePod& list) const
Expand Down Expand Up @@ -331,5 +323,9 @@ bool TDefaultValueBuilder::GetSecureParam(NUdf::TStringRef key, NUdf::TStringRef
return false;
}

NUdf::IListValueBuilder::TPtr TDefaultValueBuilder::NewListBuilder() const {
return HolderFactory_.NewList();
}

} // namespace NMiniKQL
} // namespace Nkikimr
4 changes: 3 additions & 1 deletion ydb/library/yql/minikql/computation/mkql_value_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TDefaultValueBuilder final: public NUdf::IValueBuilder, private TNonCopyab
NUdf::TUnboxedValue PrependString(const NUdf::TStringRef& ref,NUdf::TUnboxedValuePod value) const final;

NUdf::TUnboxedValue SubString(NUdf::TUnboxedValuePod value, ui32 offset, ui32 size) const final;
NUdf::TUnboxedValue NewList(NUdf::TUnboxedValue* items, ui64 count) const final;
NUdf::TUnboxedValue NewList(NUdf::TUnboxedValue* items, ui64 count) const final; // Destroys (moves out from) items

NUdf::TUnboxedValue NewString(const NUdf::TStringRef& ref) const final;

Expand Down Expand Up @@ -91,6 +91,8 @@ class TDefaultValueBuilder final: public NUdf::IValueBuilder, private TNonCopyab

NUdf::TUnboxedValue NewArray64(ui64 count, NUdf::TUnboxedValue*& itemsPtr) const final;

NUdf::IListValueBuilder::TPtr NewListBuilder() const final;

private:
const THolderFactory& HolderFactory_;
NUdf::EValidatePolicy Policy_;
Expand Down
33 changes: 32 additions & 1 deletion ydb/library/yql/public/udf/udf_value_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ class IDictValueBuilder

UDF_ASSERT_TYPE_SIZE(IDictValueBuilder, 8);

class IListValueBuilder {
public:
using TPtr = TUniquePtr<IListValueBuilder>;

public:
virtual ~IListValueBuilder() = default;

// Destroys (moves out from) the element
virtual IListValueBuilder& Add(TUnboxedValue&& element) = 0;

// Destroys (moves out from) the elements
virtual IListValueBuilder& AddMany(const NUdf::TUnboxedValue* elements, size_t count) = 0;

virtual TUnboxedValue Build() = 0;
};

UDF_ASSERT_TYPE_SIZE(IListValueBuilder, 8);

///////////////////////////////////////////////////////////////////////////////
// IDateBuilder
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -208,6 +226,7 @@ class IValueBuilder1

virtual IDictValueBuilder::TPtr NewDict(const TType* dictType, ui32 flags) const = 0;

// Destroys (moves out from) items
virtual TUnboxedValue NewList(TUnboxedValue* items, ui64 count) const = 0;

virtual TUnboxedValue ReverseList(const TUnboxedValuePod& list) const = 0;
Expand Down Expand Up @@ -288,7 +307,19 @@ class IValueBuilder7: public IValueBuilder6 {
};
#endif

#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 27)
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 39)
class IValueBuilder8: public IValueBuilder7 {
public:
virtual IListValueBuilder::TPtr NewListBuilder() const = 0;
};
#endif

#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 39)
class IValueBuilder: public IValueBuilder8 {
protected:
IValueBuilder();
};
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 27)
class IValueBuilder: public IValueBuilder7 {
protected:
IValueBuilder();
Expand Down
2 changes: 1 addition & 1 deletion ydb/library/yql/public/udf/udf_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NYql {
namespace NUdf {

#define CURRENT_UDF_ABI_VERSION_MAJOR 2
#define CURRENT_UDF_ABI_VERSION_MINOR 38
#define CURRENT_UDF_ABI_VERSION_MINOR 39
#define CURRENT_UDF_ABI_VERSION_PATCH 0

#ifdef USE_CURRENT_UDF_ABI_VERSION
Expand Down

0 comments on commit 52e48f7

Please sign in to comment.