Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
GH-36845: [C++][Python] Allow type promotion on pa.concat_tables#36846
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
dfcefbc0b223be7abdc13f993f902dc904adaf3392a2547f834020dc651c4cc940cfe5682dc98b4a18a0057ded12de01edf09ab8dfde4c52d14814ffffb846e4fc346ceac6921bb664269929349d5195d16bdc92cbedc8b535efb9ff9f14f2451387f6a43ff8808f28c37f9991c5643ceb4514545f15ccb16e8495290949a17c4a58f996f9f1521a0be0db91b2b8b4ccc00894895f87acad5e1df1301bda8802c53a25ac79722b74af62b9978460ec31ec8e6a9452c4ed58763d749851beaceadd35c446c3ca1f694dc45fefcee5a81573a9e47b41d015b5a8318d65e742156a5f570a70d225ac8ade8fcc9062b8a1cdba76f93f144ecc909f7c6328ea860d9b12cc6642facccde7677409b58d70fa2e1a2a505b1a76f7f359a267f5eb6c29977c703b0384918beb9a1cfc739b493c4e9e19f06461ed125bf2842e5dfae58e10aed9a216d48File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -313,16 +313,23 @@ Result<std::shared_ptr<Table>> ConcatenateTables( | ||
| ConcatenateTablesOptions options = ConcatenateTablesOptions::Defaults(), | ||
| MemoryPool* memory_pool = default_memory_pool()); | ||
| namespace compute { | ||
| class CastOptions; | ||
| } | ||
| /// \brief Promotes a table to conform to the given schema. | ||
| /// | ||
| /// If a field in the schema does not have a corresponding column in the | ||
| /// table, a column of nulls will be added to the resulting table. | ||
| /// If the corresponding column is of type Null, it will be promoted to | ||
| /// the type specified by schema, with null values filled. | ||
| /// If a field in the schema does not have a corresponding column in | ||
| /// the table, a column of nulls will be added to the resulting table. | ||
| /// If the corresponding column is of type Null, it will be promoted | ||
| /// to the type specified by schema, with null values filled. The | ||
| /// column will be casted to the type specified by the schema. | ||
| /// | ||
| /// Returns an error: | ||
| /// - if the corresponding column's type is not compatible with the | ||
| /// schema. | ||
| /// - if there is a column in the table that does not exist in the schema. | ||
| /// - if the cast fails or casting would be required but is not available. | ||
Fokko marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /// | ||
| /// \param[in] table the input Table | ||
| /// \param[in] schema the target schema to promote to | ||
| @@ -333,4 +340,28 @@ Result<std::shared_ptr<Table>> PromoteTableToSchema( | ||
| const std::shared_ptr<Table>& table, const std::shared_ptr<Schema>& schema, | ||
| MemoryPool* pool = default_memory_pool()); | ||
| /// \brief Promotes a table to conform to the given schema. | ||
| /// | ||
| /// If a field in the schema does not have a corresponding column in | ||
| /// the table, a column of nulls will be added to the resulting table. | ||
| /// If the corresponding column is of type Null, it will be promoted | ||
| /// to the type specified by schema, with null values filled. The column | ||
| /// will be casted to the type specified by the schema. | ||
| /// | ||
| /// Returns an error: | ||
| /// - if the corresponding column's type is not compatible with the | ||
| /// schema. | ||
| /// - if there is a column in the table that does not exist in the schema. | ||
| /// - if the cast fails or casting would be required but is not available. | ||
| /// | ||
| /// \param[in] table the input Table | ||
| /// \param[in] schema the target schema to promote to | ||
| /// \param[in] options The cast options to allow promotion of types | ||
| /// \param[in] pool The memory pool to be used if null-filled arrays need to | ||
| /// be created. | ||
| ARROW_EXPORT | ||
| Result<std::shared_ptr<Table>> PromoteTableToSchema( | ||
| const std::shared_ptr<Table>& table, const std::shared_ptr<Schema>& schema, | ||
| const compute::CastOptions& options, MemoryPool* pool = default_memory_pool()); | ||
| } // namespace arrow | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -29,6 +29,7 @@ | ||
| #include "arrow/array/data.h" | ||
| #include "arrow/array/util.h" | ||
| #include "arrow/chunked_array.h" | ||
| #include "arrow/compute/cast.h" | ||
| #include "arrow/record_batch.h" | ||
| #include "arrow/status.h" | ||
| #include "arrow/testing/gtest_util.h" | ||
| @@ -418,16 +419,17 @@ TEST_F(TestPromoteTableToSchema, IncompatibleTypes) { | ||
| auto table = MakeTableWithOneNullFilledColumn("field", int32(), length); | ||
| // Invalid promotion: int32 to null. | ||
| ASSERT_RAISES(Invalid, PromoteTableToSchema(table, schema({field("field", null())}))); | ||
| ASSERT_RAISES(TypeError, PromoteTableToSchema(table, schema({field("field", null())}))); | ||
| // Invalid promotion: int32 to uint32. | ||
| ASSERT_RAISES(Invalid, PromoteTableToSchema(table, schema({field("field", uint32())}))); | ||
| // Invalid promotion: int32 to list. | ||
| ASSERT_RAISES(TypeError, | ||
| PromoteTableToSchema(table, schema({field("field", list(int32()))}))); | ||
| } | ||
| TEST_F(TestPromoteTableToSchema, IncompatibleNullity) { | ||
| const int length = 10; | ||
| auto table = MakeTableWithOneNullFilledColumn("field", int32(), length); | ||
| ASSERT_RAISES(Invalid, | ||
| ASSERT_RAISES(TypeError, | ||
| PromoteTableToSchema( | ||
| table, schema({field("field", uint32())->WithNullable(false)}))); | ||
| } | ||
| @@ -520,6 +522,36 @@ TEST_F(ConcatenateTablesWithPromotionTest, Simple) { | ||
| AssertTablesEqualUnorderedFields(*expected, *result); | ||
| } | ||
| TEST_F(ConcatenateTablesWithPromotionTest, Unify) { | ||
| auto t_i32 = TableFromJSON(schema({field("f0", int32())}), {"[[0], [1]]"}); | ||
| auto t_i64 = TableFromJSON(schema({field("f0", int64())}), {"[[2], [3]]"}); | ||
| auto t_null = TableFromJSON(schema({field("f0", null())}), {"[[null], [null]]"}); | ||
| auto expected_int64 = | ||
| TableFromJSON(schema({field("f0", int64())}), {"[[0], [1], [2], [3]]"}); | ||
| auto expected_null = | ||
| TableFromJSON(schema({field("f0", int32())}), {"[[0], [1], [null], [null]]"}); | ||
| ConcatenateTablesOptions options; | ||
| EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, | ||
| ::testing::HasSubstr("Schema at index 1 was different"), | ||
| ConcatenateTables({t_i32, t_i64}, options)); | ||
| EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, | ||
| ::testing::HasSubstr("Schema at index 1 was different"), | ||
| ConcatenateTables({t_i32, t_null}, options)); | ||
| options.unify_schemas = true; | ||
| EXPECT_RAISES_WITH_MESSAGE_THAT(TypeError, | ||
| ::testing::HasSubstr("Field f0 has incompatible types"), | ||
| ConcatenateTables({t_i64, t_i32}, options)); | ||
| ASSERT_OK_AND_ASSIGN(auto actual, ConcatenateTables({t_i32, t_null}, options)); | ||
| AssertTablesEqual(*expected_null, *actual, /*same_chunk_layout=*/false); | ||
| options.field_merge_options.promote_numeric_width = true; | ||
| ASSERT_OK_AND_ASSIGN(actual, ConcatenateTables({t_i32, t_i64}, options)); | ||
| AssertTablesEqual(*expected_int64, *actual, /*same_chunk_layout=*/false); | ||
| } | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you tests for when casting fails because of overflow or other issues? It would also showcase why the distinction between Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you haven't addressed this comment? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added a test for combining a | ||
| TEST_F(TestTable, Slice) { | ||
| const int64_t length = 10; | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.