Skip to content

Commit b249d4a

Browse files
Mikester95copybara-github
authored andcommitted
Omit schema attributes with None values from DataSlice repr.
PiperOrigin-RevId: 863378714 Change-Id: I3a189bf53197dcd9c896bd28f76d280341a8096f
1 parent b0d2224 commit b249d4a

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

koladata/data_slice_repr.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,17 @@ absl::StatusOr<std::vector<std::string>> AttrsToStrParts(
611611
break;
612612
}
613613
ASSIGN_OR_RETURN(DataSlice value, ds.GetAttr(attr_name));
614-
ASSIGN_OR_RETURN(std::string value_str,
615-
DataItemToStr(value.item(), value.GetSchemaImpl(), db,
616-
option, wrapping));
617-
parts.emplace_back(wrapping.FormatSchemaAttrAndValue(
618-
attr_name, value_str, value.item().is_list()));
619-
++item_count;
614+
bool skip_none_values =
615+
ds.GetSchemaImpl() != schema::kObject && option.show_schema;
616+
// We skip showing attrs with the value None.
617+
if (!value.IsEmpty() || !skip_none_values) {
618+
ASSIGN_OR_RETURN(std::string value_str,
619+
DataItemToStr(value.item(), value.GetSchemaImpl(), db,
620+
option, wrapping));
621+
parts.emplace_back(wrapping.FormatSchemaAttrAndValue(
622+
attr_name, value_str, value.item().is_list()));
623+
++item_count;
624+
}
620625
}
621626

622627
return parts;

koladata/data_slice_repr_test.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,5 +1968,46 @@ TEST(DataSliceReprTest, SchemaToStr) {
19681968
Eq("LIST[INT32]"));
19691969
}
19701970

1971+
TEST(DataSliceReprTest, TestDataItemStringRepresentation_SchemaName_SkipNone) {
1972+
{
1973+
// None values are skipped, but the schema values are not.
1974+
DataBagPtr bag = DataBag::EmptyMutable();
1975+
ASSERT_OK_AND_ASSIGN(
1976+
DataSlice entity_with_none,
1977+
EntityCreator::FromAttrs(
1978+
bag, {"a", "b"},
1979+
{test::DataItem(1), test::DataItem(std::nullopt, schema::kInt32)}));
1980+
EXPECT_THAT(DataSliceRepr(entity_with_none,
1981+
{.show_databag_id = false, .show_schema = true}),
1982+
"DataItem(Entity(a=1), schema: ENTITY(a=INT32, b=INT32))");
1983+
}
1984+
{
1985+
// None values are not skipped when show_schema is false.
1986+
DataBagPtr bag = DataBag::EmptyMutable();
1987+
ASSERT_OK_AND_ASSIGN(
1988+
DataSlice entity_with_none,
1989+
EntityCreator::FromAttrs(
1990+
bag, {"a", "b"},
1991+
{test::DataItem(1), test::DataItem(std::nullopt, schema::kInt32)}));
1992+
EXPECT_THAT(
1993+
DataSliceRepr(entity_with_none,
1994+
{.show_databag_id = false, .show_schema = false}),
1995+
"DataItem(Entity(a=1, b=None))");
1996+
}
1997+
{
1998+
// None values are not skipped for Objects.
1999+
DataBagPtr bag = DataBag::EmptyMutable();
2000+
ASSERT_OK_AND_ASSIGN(
2001+
DataSlice entity_with_none,
2002+
ObjectCreator::FromAttrs(
2003+
bag, {"a", "b"},
2004+
{test::DataItem(1), test::DataItem(std::nullopt, schema::kInt32)}));
2005+
EXPECT_THAT(
2006+
DataSliceRepr(entity_with_none,
2007+
{.show_databag_id = false, .show_schema = true}),
2008+
"DataItem(Obj(a=1, b=None), schema: OBJECT)");
2009+
}
2010+
}
2011+
19712012
} // namespace
19722013
} // namespace koladata

0 commit comments

Comments
 (0)