Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
numpy_interop.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef PYARROW_NUMPY_INTEROP_H
19 #define PYARROW_NUMPY_INTEROP_H
20 
21 #include "arrow/python/platform.h" // IWYU pragma: export
22 
23 #include <numpy/numpyconfig.h> // IWYU pragma: export
24 
25 // Don't use the deprecated Numpy functions
26 #ifdef NPY_1_7_API_VERSION
27 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
28 #else
29 #define NPY_ARRAY_NOTSWAPPED NPY_NOTSWAPPED
30 #define NPY_ARRAY_ALIGNED NPY_ALIGNED
31 #define NPY_ARRAY_WRITEABLE NPY_WRITEABLE
32 #define NPY_ARRAY_UPDATEIFCOPY NPY_UPDATEIFCOPY
33 #endif
34 
35 // This is required to be able to access the NumPy C API properly in C++ files
36 // other than this main one
37 #define PY_ARRAY_UNIQUE_SYMBOL arrow_ARRAY_API
38 #ifndef NUMPY_IMPORT_ARRAY
39 #define NO_IMPORT_ARRAY
40 #endif
41 
42 #include <numpy/arrayobject.h> // IWYU pragma: export
43 #include <numpy/arrayscalars.h> // IWYU pragma: export
44 #include <numpy/ufuncobject.h> // IWYU pragma: export
45 
46 // A bit subtle. Numpy has 5 canonical integer types:
47 // (or, rather, type pairs: signed and unsigned)
48 // NPY_BYTE, NPY_SHORT, NPY_INT, NPY_LONG, NPY_LONGLONG
49 // It also has 4 fixed-width integer aliases.
50 // When mapping Arrow integer types to these 4 fixed-width aliases,
51 // we always miss one of the canonical types (even though it may
52 // have the same width as one of the aliases).
53 // Which one depends on the platform...
54 // On a LP64 system, NPY_INT64 maps to NPY_LONG and
55 // NPY_LONGLONG needs to be handled separately.
56 // On a LLP64 system, NPY_INT32 maps to NPY_LONG and
57 // NPY_INT needs to be handled separately.
58 
59 #if NPY_BITSOF_LONG == 32 && NPY_BITSOF_LONGLONG == 64
60 #define NPY_INT64_IS_LONG_LONG 1
61 #else
62 #define NPY_INT64_IS_LONG_LONG 0
63 #endif
64 
65 #if NPY_BITSOF_INT == 32 && NPY_BITSOF_LONG == 64
66 #define NPY_INT32_IS_INT 1
67 #else
68 #define NPY_INT32_IS_INT 0
69 #endif
70 
71 namespace arrow {
72 namespace py {
73 
74 inline int import_numpy() {
75 #ifdef NUMPY_IMPORT_ARRAY
76  import_array1(-1);
77  import_umath1(-1);
78 #endif
79 
80  return 0;
81 }
82 
83 // See above about the missing Numpy integer type numbers
84 inline int fix_numpy_type_num(int type_num) {
85 #if !NPY_INT32_IS_INT && NPY_BITSOF_INT == 32
86  if (type_num == NPY_INT) return NPY_INT32;
87  if (type_num == NPY_UINT) return NPY_UINT32;
88 #endif
89 #if !NPY_INT64_IS_LONG_LONG && NPY_BITSOF_LONGLONG == 64
90  if (type_num == NPY_LONGLONG) return NPY_INT64;
91  if (type_num == NPY_ULONGLONG) return NPY_UINT64;
92 #endif
93  return type_num;
94 }
95 
96 } // namespace py
97 } // namespace arrow
98 
99 #endif // PYARROW_NUMPY_INTEROP_H
int import_numpy()
Definition: numpy_interop.h:74
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
int fix_numpy_type_num(int type_num)
Definition: numpy_interop.h:84