-
Notifications
You must be signed in to change notification settings - Fork 4.1k
GH-48470: [Python] Construct UuidArray from list of UuidScalars #48746
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8fc6921
Initial commit
tadeja 3f255f2
Adding tests
tadeja dbf1194
Add tests and replace to_pylist with equals() post #48727
tadeja c00e6be
Apply suggestions from code review
tadeja 483e866
Adjust all to use Result and remove mismatch test
tadeja b220ebc
Formatting
tadeja 0238774
Remove redundant tests
tadeja e47182d
Remove Result from GetStorageScalar
tadeja f0a8428
Move stdlib import from test
tadeja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1486,6 +1486,71 @@ def bytes(self): | |
| pa.scalar(bad) | ||
|
|
||
|
|
||
|
tadeja marked this conversation as resolved.
|
||
| def test_array_from_extension_scalars(): | ||
| import datetime | ||
|
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 move this stdlib import to the top of file. |
||
| from decimal import Decimal | ||
|
|
||
| builtin_cases = [ | ||
| (pa.uuid(), [b"0123456789abcdef"]), | ||
| (pa.bool8(), [0, 1]), | ||
| (pa.json_(pa.string()), ['{"a":1}', '{"b":2}']), | ||
| (pa.opaque(pa.binary(), "t", "v"), [b"x", b"y"]), | ||
| ] | ||
| for ext_type, values in builtin_cases: | ||
| scalars = [pa.scalar(v, type=ext_type) for v in values] | ||
| result = pa.array(scalars, type=ext_type) | ||
| expected = pa.array(values, type=ext_type) | ||
| assert result.equals(expected) | ||
|
|
||
| # Custom extension types requiring registration | ||
| custom_cases = [ | ||
| (TinyIntType(), [1, 2]), | ||
| (IntegerType(), [100, 200]), | ||
| (LabelType(), ["a", "b"]), | ||
| (MyStructType(), [{"left": 1, "right": 2}]), | ||
| (AnnotatedType(pa.timestamp("us"), "ts"), | ||
| [datetime.datetime(2023, 1, 1)]), | ||
| (AnnotatedType(pa.duration("s"), "dur"), | ||
| [datetime.timedelta(seconds=100)]), | ||
| (AnnotatedType(pa.date32(), "date"), | ||
| [datetime.date(2023, 1, 1)]), | ||
| (AnnotatedType(pa.float64(), "f"), [1.5, 2.5]), | ||
| (AnnotatedType(pa.bool_(), "b"), [True, False]), | ||
| (AnnotatedType(pa.binary(), "bin"), [b"x", b"y"]), | ||
| (AnnotatedType(pa.decimal128(10, 2), "dec"), | ||
| [Decimal("1.50"), Decimal("2.75")]), | ||
| (AnnotatedType(pa.large_string(), "lstr"), ["hello", "world"]), | ||
| (AnnotatedType(pa.large_binary(), "lbin"), [b"ab", b"cd"]), | ||
| ] | ||
| for ext_type, values in custom_cases: | ||
| with registered_extension_type(ext_type): | ||
| scalars = [pa.scalar(v, type=ext_type) for v in values] | ||
| result = pa.array(scalars, type=ext_type) | ||
| expected = pa.array(values, type=ext_type) | ||
| assert result.equals(expected) | ||
|
|
||
| # Null handling | ||
| uuid_type = pa.uuid() | ||
| scalars = [pa.scalar(b"0123456789abcdef", type=uuid_type), | ||
| pa.scalar(None, type=uuid_type)] | ||
| result = pa.array(scalars, type=uuid_type) | ||
| assert result[0].is_valid and not result[1].is_valid | ||
|
|
||
|
tadeja marked this conversation as resolved.
|
||
| # Type inference without explicit type | ||
| u = uuid4() | ||
| scalars = [pa.scalar(u, type=pa.uuid()), None] | ||
| result = pa.array(scalars) | ||
| assert result.type == pa.uuid() | ||
| assert result[0].as_py() == u | ||
| assert not result[1].is_valid | ||
|
|
||
| # Mixed extension scalars and raw Python objects | ||
| u1, u2 = uuid4(), uuid4() | ||
| result = pa.array([pa.scalar(u1, type=pa.uuid()), u2], type=pa.uuid()) | ||
| expected = pa.array([u1, u2], type=pa.uuid()) | ||
| assert result.equals(expected) | ||
|
|
||
|
|
||
| def test_tensor_type(): | ||
| tensor_type = pa.fixed_shape_tensor(pa.int8(), [2, 3]) | ||
| assert tensor_type.extension_name == "arrow.fixed_shape_tensor" | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using
Resulthere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps
Resultis not needed here - if its kind of failure path is unlikely thenconst Scalar&might be better?Looking at the line 757 check if (PyValue::IsNull(this->options_, value)) { ...
( For an (unlikely?) non-None extension scalar with internal storage .value of a null shared_ptr both approaches don't avoid it? )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resultis probably not needed indeed, since no error can be returned here. As you prefer @tadeja.