question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`getitem` access with `str` value to a struct array causes impl "no match"

See original GitHub issue

Hopefully this isn’t a case of doing something obviously naive but, more or less the issue is that this compile error is the result of passing in a str argument (to an @njit-ed @staticmethod) to read a (numpy) struct-array via field name:

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function getitem>) found for signature:

 >>> getitem(Record(time[type=float64;offset=0],open[type=float64;offset=8],high[type=float64;offset=16],low[type=float64;offset=24],close[type=float64;offset=32],volume[type=float64;offset=40],bar_wap[type=float64;offset=48];56;False), unicode_type)

There are 22 candidate implementations:
  - Of which 22 did not match due to:
  Overload of function 'getitem': File: <numerous>: Line N/A.
    With argument(s): '(Record(time[type=float64;offset=0],open[type=float64;offset=8],high[type=float64;offset=16],low[type=float64;offset=24],close[type=float64;offset=32],volume[type=float64;offset=40],bar_wap[type=float64;offset=48];56;False), unicode_type)':
   No match.

During: typing of intrinsic-call at /home/goodboy/repos/piker/piker/data/_pathops.py (562)

File "piker/data/_pathops.py", line 562:
    def path_arrays_from_ohlc(
        <source elided>
            close = q['close']
            index = float64(q[index_field])
            ^

Yes, i’m on 0.56.4.


source code function which triggers this is here: https://github.com/pikers/piker/blob/8ee964451b085dfb957160465018f4d4bd8ab696/piker/data/_pathops.py#L526

More or less it seems that there is an issue with the type signature matching of the input struct-array both in terms of inferred (unspecified in numba terms) type sig as well as a manual spec.

For eg. I’ve tried something like

    @staticmethod
    @njit(
        # TODO: need to construct this manually for readonly
        # arrays, see https://github.com/numba/numba/issues/4511
        (
            types.Array(
                numba_ohlc_dtype,
                1,
                'C',
                readonly=True,
            ),
            int64,
            types.unicode_type,
            optional(float64),
        ),
        nogil=True
    )
    def path_arrays_from_ohlc(
        data: np.ndarray,
        start: int64,
        index_field: str,
        bar_gap: float64 = 0.43,

    ) -> tuple[
        np.ndarray,
        np.ndarray,
        np.ndarray,
    ]:

And still get the same error.


Likely related issues include:

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
stuartarchibaldcommented, Nov 25, 2022

Thanks for the report. I think this potentially demonstrates what it is you are seeing:

from numba import njit
import numpy as np

dt = np.dtype([('field_a', np.float32), ('field_b', np.int8)])

@njit
def foo(record, field):
    x = record[field] # What type is x ?
    return x

arr = np.ones(1, dt)

print(foo(arr, 'field_a'))

the issue is essentially that Numba sees the type of field as a types.unicode_type, it doesn’t know at compile time to which field in the dt dtype this corresponds. As a result it cannot work out a return type of the getitem call which manifests as a typing error similar to that in the OP.

The following case does work because the types can be resolved at compile time through the use of a compile-time-constant string that addresses the field:

from numba import njit
import numpy as np

dt = np.dtype([('field_a', np.float32), ('field_b', np.int8)])

@njit
def foo(record):
    # access 'field_a' with a compile time constant string...
    # the type of x is known to be the type of that specific field
    x = record['field_a']
    return x

arr = np.ones(1, dt)

print(foo(arr))
0reactions
stuartarchibaldcommented, Dec 2, 2022

@goodboy I don’t think list based access to the fields is supported. If the list is declared as in the above, then it has an initial_value (the <iv=[...]> part of the type) which would potentially make it possible to implement this kind of indexing. If this is important to you then perhaps open a feature request? Many thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

add element to array of struct in C++ encountered "error no ...
You are trying to assign a pointer to Numbers into a Numbers value. The rest of the error message is not helpful. Assuming...
Read more >
BatchGetItem - Amazon DynamoDB - AWS Documentation
The BatchGetItem operation returns the attributes of one or more items from one or more tables. You identify requested items by primary key....
Read more >
enum — Support for enumerations — Python 3.11.1 ...
Allows Enum members to have attributes without conflicting with member names. unique(). Enum class decorator that ensures only one name is bound to...
Read more >
Troubleshooting and tips — Numba 0.50.1 documentation
Another common reason for Numba not being able to compile your code is that it cannot statically determine the return type of a...
Read more >
The Type Hierarchy - SQLAlchemy 1.4 Documentation
For all backends, only the Python values None , True , False , 1 or 0 are ... access the string values inside...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found