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+ #  cython: profile=False
19+ #  distutils: language = c++
20+ #  cython: embedsignature = True
21+ 
22+ from  libcpp cimport bool  as  c_bool, nullptr
23+ from  libcpp.memory cimport shared_ptr, unique_ptr, make_shared
24+ from  libcpp.string cimport string as  c_string
25+ from  libcpp.vector cimport vector as  c_vector
26+ from  libc.stdint cimport int64_t, uint8_t, uintptr_t
27+ 
28+ from  pyarrow.includes.libarrow cimport * 
29+ from  pyarrow.compat import  frombytes
30+ 
31+ from  pyarrow.includes.libgandiva cimport (GStatus, CExpression,
32+                                           CNode, CProjector,
33+                                           TreeExprBuilder_MakeExpression,
34+                                           TreeExprBuilder_MakeFunction,
35+                                           TreeExprBuilder_MakeLiteral,
36+                                           TreeExprBuilder_MakeField,
37+                                           TreeExprBuilder_MakeIf,
38+                                           Projector_Make)
39+ 
40+ from  pyarrow.lib cimport (Array, DataType, Field, MemoryPool,
41+                           RecordBatch, Schema)
42+ 
43+ cdef int  check_status(const GStatus&  status) nogil except  - 1 :
44+     if  status.ok():
45+         return  0 
46+ 
47+     with  gil:
48+         message =  frombytes(status.message())
49+         raise  Exception (message)
50+ 
51+ cdef class  Node:
52+     cdef:
53+         shared_ptr[CNode] node
54+ 
55+     cdef void  init(self , shared_ptr[CNode] node):
56+         self .node =  node
57+ 
58+ cdef make_node(shared_ptr[CNode] node):
59+     cdef Node result =  Node()
60+     result.init(node)
61+     return  result
62+ 
63+ cdef class  Expression:
64+     cdef:
65+         shared_ptr[CExpression] expression
66+ 
67+     cdef void  init(self , shared_ptr[CExpression] expression):
68+         self .expression =  expression
69+ 
70+ cdef make_array(shared_ptr[CArray] array):
71+     cdef Array result =  Array()
72+     result.init(array)
73+     return  result
74+ 
75+ cdef class  Projector:
76+     cdef:
77+         shared_ptr[CProjector] projector
78+ 
79+     cdef void  init(self , shared_ptr[CProjector] projector):
80+         self .projector =  projector
81+ 
82+     def  evaluate (self , RecordBatch batch ):
83+         cdef vector[shared_ptr[CArray]] results
84+         check_status(self .projector.get().Evaluate(
85+             batch.sp_batch.get()[0 ], c_get_memory_pool(), & results))
86+         cdef shared_ptr[CArray] result
87+         arrays =  []
88+         for  result in  results:
89+             arrays.append(make_array(result))
90+         return  arrays
91+ 
92+ cdef class  TreeExprBuilder:
93+ 
94+     def  make_literal (self , value ):
95+         cdef shared_ptr[CNode] r =  TreeExprBuilder_MakeLiteral(value)
96+         return  make_node(r)
97+ 
98+     def  make_expression (self , Node root_node , Field return_field ):
99+         cdef shared_ptr[CExpression] r =  TreeExprBuilder_MakeExpression(
100+             root_node.node, return_field.sp_field)
101+         cdef Expression expression =  Expression()
102+         expression.init(r)
103+         return  expression
104+ 
105+     def  make_function (self , name , children , DataType return_type ):
106+         cdef c_vector[shared_ptr[CNode]] c_children
107+         cdef Node child
108+         for  child in  children:
109+             c_children.push_back(child.node)
110+         cdef shared_ptr[CNode] r =  TreeExprBuilder_MakeFunction(
111+             name, c_children, return_type.sp_type)
112+         return  make_node(r)
113+ 
114+     def  make_field (self , Field field ):
115+         cdef shared_ptr[CNode] r =  TreeExprBuilder_MakeField(field.sp_field)
116+         return  make_node(r)
117+ 
118+     def  make_if (self , Node condition , Node this_node ,
119+                 Node else_node , DataType return_type ):
120+         cdef shared_ptr[CNode] r =  TreeExprBuilder_MakeIf(
121+             condition.node, this_node.node, else_node.node,
122+             return_type.sp_type)
123+         return  make_node(r)
124+ 
125+ cpdef make_projector(Schema schema, children, MemoryPool pool):
126+     cdef c_vector[shared_ptr[CExpression]] c_children
127+     cdef Expression child
128+     for  child in  children:
129+         c_children.push_back(child.expression)
130+     cdef shared_ptr[CProjector] result
131+     check_status(Projector_Make(schema.sp_schema, c_children,
132+                                 & result))
133+     cdef Projector projector =  Projector()
134+     projector.init(result)
135+     return  projector
0 commit comments