Skip to content
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

fix: set map entries/key to non-nullable #107

Merged
merged 5 commits into from
Feb 14, 2023
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
2 changes: 1 addition & 1 deletion r/tests/testthat/test-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ test_that("list constructors assign the correct child type", {
})

test_that("map constructor assigns the correct key and value types", {
schema <- na_map(na_int32(), na_int64())
schema <- na_map(na_int32(nullable = FALSE), na_int64())
expect_named(schema$children, "entries")
expect_named(schema$children$entries$children, c("key", "value"))
expect_identical(schema$children$entries$children$key$format, "i")
Expand Down
13 changes: 13 additions & 0 deletions src/nanoarrow/schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ static int ArrowSchemaInitChildrenIfNeeded(struct ArrowSchema* schema,
NANOARROW_RETURN_NOT_OK(
ArrowSchemaInitFromType(schema->children[0], NANOARROW_TYPE_STRUCT));
NANOARROW_RETURN_NOT_OK(ArrowSchemaSetName(schema->children[0], "entries"));
schema->children[0]->flags &= ~ARROW_FLAG_NULLABLE;
NANOARROW_RETURN_NOT_OK(ArrowSchemaAllocateChildren(schema->children[0], 2));
ArrowSchemaInit(schema->children[0]->children[0]);
ArrowSchemaInit(schema->children[0]->children[1]);
NANOARROW_RETURN_NOT_OK(
ArrowSchemaSetName(schema->children[0]->children[0], "key"));
schema->children[0]->children[0]->flags &= ~ARROW_FLAG_NULLABLE;
NANOARROW_RETURN_NOT_OK(
ArrowSchemaSetName(schema->children[0]->children[1], "value"));
break;
Expand Down Expand Up @@ -991,6 +993,17 @@ static ArrowErrorCode ArrowSchemaViewValidateMap(struct ArrowSchemaView* schema_
return EINVAL;
}

if (schema_view->schema->children[0]->flags & ARROW_FLAG_NULLABLE) {
ArrowErrorSet(error,
"Expected child of map type to be non-nullable but was nullable");
return EINVAL;
}

if (schema_view->schema->children[0]->children[0]->flags & ARROW_FLAG_NULLABLE) {
ArrowErrorSet(error, "Expected key of map type to be non-nullable but was nullable");
return EINVAL;
}

return NANOARROW_OK;
}

Expand Down
28 changes: 26 additions & 2 deletions src/nanoarrow/schema_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,16 @@ TEST(SchemaTest, SchemaTestInitNestedMap) {

EXPECT_EQ(ArrowSchemaInitFromType(&schema, NANOARROW_TYPE_MAP), NANOARROW_OK);
EXPECT_STREQ(schema.format, "+m");
ASSERT_EQ(ArrowSchemaSetType(schema.children[0]->children[0], NANOARROW_TYPE_INT32),
EXPECT_EQ(ArrowSchemaSetType(schema.children[0]->children[0], NANOARROW_TYPE_INT32),
NANOARROW_OK);
ASSERT_EQ(ArrowSchemaSetType(schema.children[0]->children[1], NANOARROW_TYPE_STRING),
EXPECT_EQ(ArrowSchemaSetType(schema.children[0]->children[1], NANOARROW_TYPE_STRING),
NANOARROW_OK);
EXPECT_STREQ(schema.children[0]->name, "entries");
EXPECT_STREQ(schema.children[0]->children[0]->name, "key");
EXPECT_STREQ(schema.children[0]->children[1]->name, "value");

EXPECT_FALSE(schema.children[0]->flags & ARROW_FLAG_NULLABLE);
EXPECT_FALSE(schema.children[0]->children[0]->flags & ARROW_FLAG_NULLABLE);

auto arrow_type = ImportType(&schema);
ARROW_EXPECT_OK(arrow_type);
Expand Down Expand Up @@ -1184,6 +1190,24 @@ TEST(SchemaViewTest, SchemaViewInitNestedMapErrors) {
EXPECT_STREQ(ArrowErrorMessage(&error),
"Expected format of child of map type to be '+s' but found '+us:0,1'");
schema.release(&schema);

EXPECT_EQ(ArrowSchemaInitFromType(&schema, NANOARROW_TYPE_MAP), NANOARROW_OK);
EXPECT_EQ(ArrowSchemaSetType(schema.children[0]->children[0], NANOARROW_TYPE_INT32),
NANOARROW_OK);
EXPECT_EQ(ArrowSchemaSetType(schema.children[0]->children[1], NANOARROW_TYPE_STRING),
NANOARROW_OK);

schema.children[0]->flags |= ARROW_FLAG_NULLABLE;
EXPECT_EQ(ArrowSchemaViewInit(&schema_view, &schema, &error), EINVAL);
EXPECT_STREQ(ArrowErrorMessage(&error),
"Expected child of map type to be non-nullable but was nullable");
schema.children[0]->flags &= ~ARROW_FLAG_NULLABLE;

schema.children[0]->children[0]->flags |= ARROW_FLAG_NULLABLE;
EXPECT_EQ(ArrowSchemaViewInit(&schema_view, &schema, &error), EINVAL);
EXPECT_STREQ(ArrowErrorMessage(&error),
"Expected key of map type to be non-nullable but was nullable");
schema.release(&schema);
}

TEST(SchemaViewTest, SchemaViewInitNestedUnion) {
Expand Down