Skip to content

Conversation

@milesgranger
Copy link
Contributor

@milesgranger milesgranger commented Nov 22, 2022

Will fix ARROW-18265

  • Support list types other than Type::LIST
  • Add C++ tests

Example

In [1]: arr = pa.array([{'a': [{'b': 1}, {'c': 'hi'}]}])

In [2]: pc.struct_field(arr, '.a[1].c')
Out[2]:
<pyarrow.lib.StringArray object at 0x7efbc741f040>
[
  "hi"
]

In [3]: pc.struct_field(arr, '.a[0].b')
Out[3]:
<pyarrow.lib.Int64Array object at 0x7efc77398fa0>
[
  1
]

In [4]:

@github-actions
Copy link

@milesgranger milesgranger marked this pull request as ready for review December 2, 2022 10:45
Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(just a quick look at the tests)

parent was not passed in FieldPath first iteration
Comment on lines +480 to +482
static bool IsBaseListType(const DataType& type) {
return dynamic_cast<const BaseListType*>(&type) != nullptr;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have is_list_like helper defined (although that also includes Map type, not sure if the list_element kernel supports that)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if we keep this, using a combination of a few type.id() == .. checks (like ValidParentType just below) might be more readable to know which types are included)

if (!ValidParentType(type)) {
return Status::TypeError("struct_field: cannot subscript field of type ", type);
} else if (index < 0 || index >= type.num_fields()) {
} else if (!IsBaseListType(type) && (index < 0 || index >= type.num_fields())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, we could check the index for fixed sized list arrays as well (not for variable size list arrays, though, so not sure that is worth it)

// Children then are fields from list type, and out will be the children from
// that field, thus index 0.
if (out != nullptr && IsBaseListType(*(*out)->type())) {
children = get_children(*out);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand why we need the get_children here (because then we call get_children twice in a single iteration? since there is another call a bit below)
Can you add a clarifying comment about that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(or to put it differently: why isn't this initial get_children needed when parent is a struct type? What's the difference between both?)

// FindAll, only used by kernels for which list element to take. When FindAll
// encounters a list, it assumes the list value type. Not b/c they are numeric, but
// they are the position referencing a specific list element.
EXPECT_THAT(FieldRef("c", 0, "d").FindAll(*type), ElementsAre(FieldPath{0, 0, 0}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add variants of those tests that pass a schema instead of type to FindAll?

@pitrou
Copy link
Member

pitrou commented Dec 12, 2022

I find it a bit uneasy how this PR is conflating a purely metadata-based field lookup (the current semantics of FieldRef) and a dynamic index lookup into a list. Is this actually important to support?

@amol-
Copy link
Member

amol- commented Dec 12, 2022

I find it a bit uneasy how this PR is conflating a purely metadata-based field lookup (the current semantics of FieldRef) and a dynamic index lookup into a list. Is this actually important to support?

Not sure if FieldRef is the right answer to the problem, but in general I think that what we want to achieve is support for indexing specific subfields in entries field.subfield and field.arraysubfield.0 and so on. This seems a fairly standard feature and not far from what MongoDB provides for example ( https://www.mongodb.com/docs/manual/core/document/#dot-notation )

@jorisvandenbossche
Copy link
Member

a purely metadata-based field lookup (the current semantics of FieldRef)

I am not sure that this is still entirely correct nowadays, since we already do use FieldRef in other places in the compute engine to refer to fields (eg to specify the sort key, in projections (although there wrapped in an expression), ...).
And it is certainly true that right now we only support struct/union field lookups, but also those are not purely metadata-based, as the actual lookup of values (not just the type/field from a schema) also needs to potentially calculate validity bitmaps. While for list types, the lookup of the type is also metadata-only. It's only when looking up actual values that you need a nontrivial computation.

Is this actually important to support?

If you want to work with list types, this is a pretty basic operation. Now, the actual operation is already supported (list_element kernel), here it is only about being able to express this in a field reference. So this is somewhat in the bucket of "user convenience", as you can already achieve something similar with the kernel.
Now, for example also Substrait has this concept (https://github.com/substrait-io/substrait/blob/7f272f13f22cd5f5842baea42bcf7961e6251881/proto/substrait/algebra.proto#L932-L938)

Another example of database that supports this in queries: https://cloud.google.com/bigquery/docs/reference/standard-sql/arrays#accessing_array_elements

@jorisvandenbossche
Copy link
Member

And to be clear, that's also basically the question that I asked on the JIRA: "do we want to use FieldRef/FieldPath for this?" (because it does deviate from how it is currently implemented) It could also be a different (new to add) data structure (that would be used throughout the compute code instead of the current FieldRef/FieldPath)

@pitrou
Copy link
Member

pitrou commented Dec 13, 2022

Hmm... The convenience argument is convincing, I admit.

Still, I wonder if we shouldn't make it optional in FieldRef, perhaps by adding a bool allow_list_lookup = false argument to FieldRef lookup APIs?

@amol-
Copy link
Member

amol- commented Mar 30, 2023

Closing because it has been untouched for a while, in case it's still relevant feel free to reopen and move it forward 👍

@amol- amol- closed this Mar 30, 2023
@milesgranger milesgranger deleted the ARROW-18265_fieldpath-work-with-list-element branch March 31, 2023 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants