Skip to content

Commit 6c8ea18

Browse files
petrmikheevcopybara-github
authored andcommitted
Add DataSliceImpl::CreateWithTypesBuffer (will be used in DenseSource, needed for removed values support)
PiperOrigin-RevId: 714900264 Change-Id: Ie9ce652512c4054fb75cd3902f9586fe1983405d
1 parent da2a428 commit 6c8ea18

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

koladata/internal/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ cc_test(
419419
":dtype",
420420
":object_id",
421421
":types",
422+
":types_buffer",
422423
"@com_google_absl//absl/status",
423424
"@com_google_absl//absl/status:status_matchers",
424425
"@com_google_absl//absl/strings",

koladata/internal/data_slice.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ class DataSliceImpl {
8686
arolla::DenseArray<T> main_values,
8787
arolla::DenseArray<Ts>... values);
8888

89+
// Creates a single-type DataSliceImpl with provided TypesBuffer. TypesBuffer
90+
// in single-type slice is optional, used in DenseSource to distinguish
91+
// missing and removed values.
92+
template <class T>
93+
static DataSliceImpl CreateWithTypesBuffer(TypesBuffer types_buffer,
94+
AllocationIdSet allocation_ids,
95+
arolla::DenseArray<T> values);
96+
8997
static DataSliceImpl Create(const arolla::DenseArray<DataItem>& items);
9098

9199
// Returns 0 dimension DataSliceImpl with specified values. In case of
@@ -398,6 +406,21 @@ DataSliceImpl DataSliceImpl::CreateWithAllocIds(
398406
return res;
399407
}
400408

409+
template <class T>
410+
DataSliceImpl DataSliceImpl::CreateWithTypesBuffer(TypesBuffer types_buffer,
411+
AllocationIdSet allocation_ids, arolla::DenseArray<T> values) {
412+
DataSliceImpl res;
413+
CreateImpl(res, std::move(values));
414+
res.internal_->types_buffer = std::move(types_buffer);
415+
if constexpr (std::is_same_v<T, ObjectId>) {
416+
res.internal_->allocation_ids = std::move(allocation_ids);
417+
} else {
418+
(void)allocation_ids;
419+
}
420+
return res;
421+
}
422+
423+
401424
using DataSlicePtr = std::unique_ptr<DataSliceImpl>;
402425

403426
} // namespace koladata::internal

koladata/internal/data_slice_test.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "koladata/internal/object_id.h"
3232
#include "koladata/internal/slice_builder.h"
3333
#include "koladata/internal/types.h"
34+
#include "koladata/internal/types_buffer.h"
3435
#include "arolla/dense_array/dense_array.h"
3536
#include "arolla/dense_array/qtype/types.h"
3637
#include "arolla/memory/optional_value.h"
@@ -150,6 +151,27 @@ TEST(DataSliceImpl, Create) {
150151
EXPECT_THAT(ds.values<int64_t>(), ElementsAre(57, 75, 19));
151152
}
152153

154+
TEST(DataSliceImpl, CreateWithTypesBuffer) {
155+
constexpr int64_t kSize = 3;
156+
auto array =
157+
arolla::CreateDenseArray<int64_t>({std::nullopt, 75, std::nullopt});
158+
TypesBuffer types_buffer;
159+
types_buffer.InitAllUnset(3);
160+
types_buffer.id_to_typeidx[1] =
161+
types_buffer.get_or_add_typeidx(ScalarTypeId<int64_t>());
162+
types_buffer.id_to_typeidx[2] = TypesBuffer::kRemoved;
163+
DataSliceImpl ds = DataSliceImpl::CreateWithTypesBuffer(
164+
std::move(types_buffer), AllocationIdSet(), array);
165+
EXPECT_EQ(ds.size(), kSize);
166+
EXPECT_EQ(ds.dtype(), arolla::GetQType<int64_t>());
167+
EXPECT_FALSE(ds.is_empty_and_unknown());
168+
EXPECT_THAT(ds.allocation_ids(), IsEmpty());
169+
EXPECT_THAT(ds, ElementsAre(DataItem(), 75, DataItem()));
170+
EXPECT_EQ(ds.types_buffer().id_to_typeidx[0], TypesBuffer::kUnset);
171+
EXPECT_EQ(ds.types_buffer().id_to_typeidx[1], 0);
172+
EXPECT_EQ(ds.types_buffer().id_to_typeidx[2], TypesBuffer::kRemoved);
173+
}
174+
153175
TEST(DataSliceImpl, CreateFromDataItemSpan) {
154176
{
155177
auto array = std::vector<DataItem>{};

0 commit comments

Comments
 (0)