Skip to content

Commit 8b46f4c

Browse files
nbjohnson0copybara-github
authored andcommitted
Add DataSlice::CreateWithFlatShape helper.
PiperOrigin-RevId: 715891339 Change-Id: I79d3f4df03a045c97116b7619ee2259ab6e83ced
1 parent a4cda18 commit 8b46f4c

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

koladata/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ cc_test(
256256
"@com_google_absl//absl/strings:str_format",
257257
"@com_google_arolla//arolla/dense_array",
258258
"@com_google_arolla//arolla/dense_array/qtype",
259+
"@com_google_arolla//arolla/jagged_shape/testing",
259260
"@com_google_arolla//arolla/qtype",
260261
"@com_google_arolla//arolla/util",
261262
"@com_google_arolla//arolla/util/testing",

koladata/data_slice.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,14 @@ absl::StatusOr<DataSlice> DataSlice::CreateWithSchemaFromData(
848848
std::move(db), wholeness);
849849
}
850850

851+
absl::StatusOr<DataSlice> DataSlice::CreateWithFlatShape(
852+
internal::DataSliceImpl impl, internal::DataItem schema, DataBagPtr db,
853+
Wholeness wholeness) {
854+
const auto size = impl.size();
855+
return Create(std::move(impl), JaggedShape::FlatFromSize(size),
856+
std::move(schema), std::move(db), wholeness);
857+
}
858+
851859
absl::StatusOr<DataSlice> DataSlice::Create(const internal::DataItem& item,
852860
JaggedShape shape,
853861
internal::DataItem schema,

koladata/data_slice.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ class DataSlice {
111111
internal::DataSliceImpl impl, JaggedShape shape, DataBagPtr db = nullptr,
112112
Wholeness wholeness = Wholeness::kNotWhole);
113113

114+
// Creates a DataSlice with shape JaggedShape::FlatFromSize(impl.size()).
115+
static absl::StatusOr<DataSlice> CreateWithFlatShape(
116+
internal::DataSliceImpl impl, internal::DataItem schema,
117+
DataBagPtr db = nullptr, Wholeness wholeness = Wholeness::kNotWhole);
118+
114119
// Convenience factory method that accepts JaggedShape, so that we can use
115120
// implementation-agnostic constructions in visitors passed to VisitImpl.
116121
static absl::StatusOr<DataSlice> Create(

koladata/data_slice_test.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "koladata/testing/matchers.h"
4848
#include "arolla/dense_array/dense_array.h"
4949
#include "arolla/dense_array/qtype/types.h"
50+
#include "arolla/jagged_shape/testing/matchers.h"
5051
#include "arolla/qtype/qtype_traits.h"
5152
#include "arolla/qtype/typed_value.h"
5253
#include "arolla/util/bytes.h"
@@ -336,6 +337,25 @@ TEST(DataSliceUtils, CreateWithSchemaFromDataError) {
336337
HasSubstr("for primitive types")));
337338
}
338339

340+
TEST(DataSliceTest, CreateWithFlatShape) {
341+
{
342+
auto slice = *DataSlice::CreateWithFlatShape(
343+
internal::DataSliceImpl::Create(std::vector<internal::DataItem>{}),
344+
internal::DataItem(schema::kNone));
345+
EXPECT_THAT(slice.GetShape(),
346+
IsEquivalentTo(DataSlice::JaggedShape::FlatFromSize(0)));
347+
}
348+
{
349+
auto slice = *DataSlice::CreateWithFlatShape(
350+
internal::DataSliceImpl::Create(std::vector<internal::DataItem>{
351+
internal::DataItem(1), internal::DataItem(2),
352+
internal::DataItem(3)}),
353+
internal::DataItem(schema::kInt32));
354+
EXPECT_THAT(slice.GetShape(),
355+
IsEquivalentTo(DataSlice::JaggedShape::FlatFromSize(3)));
356+
}
357+
}
358+
339359
TEST(DataSliceTest, IsWhole) {
340360
{
341361
// No DataBag, trivially whole.

0 commit comments

Comments
 (0)