Skip to content

Commit

Permalink
ARROW-3746: [Gandiva] [Python] Print list of functions registered wit…
Browse files Browse the repository at this point in the history
…h gandiva

I'm also making the iterators of the Function registry static, can you check if that's ok @praveenbingo and @pravindra

Author: Philipp Moritz <pcmoritz@gmail.com>

Closes #2933 from pcmoritz/gandiva-print-functions and squashes the following commits:

58fb14b <Philipp Moritz> linting
09fe76b <Philipp Moritz> documentation fix and cleanup
1bc904f <Philipp Moritz> add test and simplify
3b7d579 <Philipp Moritz> lint
947dd64 <Philipp Moritz> lint
c02dc7c <Philipp Moritz> fix
cd520f8 <Philipp Moritz> fix
4a23c50 <Philipp Moritz> update
0790705 <Philipp Moritz> python linting
cf7fa35 <Philipp Moritz> fix lint
eacceec <Philipp Moritz> add documentation
3a1b78c <Philipp Moritz> print list of functions registered with gandiva
  • Loading branch information
pcmoritz committed Nov 11, 2018
1 parent ffcc363 commit 1ef6c26
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
11 changes: 11 additions & 0 deletions cpp/src/gandiva/expression_registry.cc
Expand Up @@ -150,4 +150,15 @@ void ExpressionRegistry::AddArrowTypesToVector(arrow::Type::type& type,
}
}

std::vector<std::shared_ptr<FunctionSignature>> GetRegisteredFunctionSignatures() {
ExpressionRegistry registry;
std::vector<std::shared_ptr<FunctionSignature>> signatures;
for (auto iter = registry.function_signature_begin();
iter != registry.function_signature_end(); iter++) {
signatures.push_back(std::make_shared<FunctionSignature>(
(*iter).base_name(), (*iter).param_types(), (*iter).ret_type()));
}
return signatures;
}

} // namespace gandiva
3 changes: 3 additions & 0 deletions cpp/src/gandiva/expression_registry.h
Expand Up @@ -61,5 +61,8 @@ class ExpressionRegistry {
static void AddArrowTypesToVector(arrow::Type::type& type, DataTypeVector& vector);
std::unique_ptr<FunctionRegistry> function_registry_;
};

std::vector<std::shared_ptr<FunctionSignature>> GetRegisteredFunctionSignatures();

} // namespace gandiva
#endif // GANDIVA_TYPES_H
64 changes: 62 additions & 2 deletions python/pyarrow/gandiva.pyx
Expand Up @@ -29,7 +29,8 @@ from pyarrow.includes.libarrow cimport *
from pyarrow.compat import frombytes
from pyarrow.types import _as_type
from pyarrow.lib cimport (Array, DataType, Field, MemoryPool, RecordBatch,
Schema, check_status, pyarrow_wrap_array)
Schema, check_status, pyarrow_wrap_array,
pyarrow_wrap_data_type)

from pyarrow.includes.libgandiva cimport (CCondition, CExpression,
CNode, CProjector, CFilter,
Expand All @@ -56,7 +57,9 @@ from pyarrow.includes.libgandiva cimport (CCondition, CExpression,
SelectionVector_MakeInt32,
SelectionVector_MakeInt64,
Projector_Make,
Filter_Make)
Filter_Make,
CFunctionSignature,
GetRegisteredFunctionSignatures)


cdef class Node:
Expand Down Expand Up @@ -257,3 +260,60 @@ cpdef make_filter(Schema schema, Condition condition):
cdef shared_ptr[CFilter] result
check_status(Filter_Make(schema.sp_schema, condition.condition, &result))
return Filter.create(result)

cdef class FunctionSignature:
"""
Signature of a Gandiva function including name, parameter types
and return type.
"""

cdef:
shared_ptr[CFunctionSignature] signature

def __init__(self):
raise TypeError("Do not call {}'s constructor directly."
.format(self.__class__.__name__))

@staticmethod
cdef create(shared_ptr[CFunctionSignature] signature):
cdef FunctionSignature self = FunctionSignature.__new__(
FunctionSignature)
self.signature = signature
return self

def return_type(self):
return pyarrow_wrap_data_type(self.signature.get().ret_type())

def param_types(self):
result = []
cdef vector[shared_ptr[CDataType]] types = \
self.signature.get().param_types()
for t in types:
result.append(pyarrow_wrap_data_type(t))
return result

def name(self):
return self.signature.get().base_name().decode()

def __repr__(self):
signature = self.signature.get().ToString().decode()
return "FunctionSignature(" + signature + ")"


def get_registered_function_signatures():
"""
Return the function in Gandiva's ExpressionRegistry.
Returns
-------
registry: a list of registered function signatures
"""
results = []

cdef vector[shared_ptr[CFunctionSignature]] signatures = \
GetRegisteredFunctionSignatures()

for signature in signatures:
results.append(FunctionSignature.create(signature))

return results
21 changes: 21 additions & 0 deletions python/pyarrow/includes/libgandiva.pxd
Expand Up @@ -151,3 +151,24 @@ cdef extern from "gandiva/filter.h" namespace "gandiva" nogil:
"gandiva::Filter::Make"(
shared_ptr[CSchema] schema, shared_ptr[CCondition] condition,
shared_ptr[CFilter]* filter)

cdef extern from "gandiva/function_signature.h" namespace "gandiva" nogil:

cdef cppclass CFunctionSignature" gandiva::FunctionSignature":

CFunctionSignature(const c_string& base_name,
vector[shared_ptr[CDataType]] param_types,
shared_ptr[CDataType] ret_type)

shared_ptr[CDataType] ret_type() const

const c_string& base_name() const

vector[shared_ptr[CDataType]] param_types() const

c_string ToString() const

cdef extern from "gandiva/expression_registry.h" namespace "gandiva" nogil:

cdef vector[shared_ptr[CFunctionSignature]] \
GetRegisteredFunctionSignatures()
10 changes: 10 additions & 0 deletions python/pyarrow/tests/test_gandiva.py
Expand Up @@ -162,3 +162,13 @@ def test_regex():
r, = projector.evaluate(table.to_batches()[0])
b = pa.array([False, True, True, True], type=pa.bool_())
assert r.equals(b)


@pytest.mark.gandiva
def test_get_registered_function_signatures():
import pyarrow.gandiva as gandiva
signatures = gandiva.get_registered_function_signatures()

assert type(signatures[0].return_type()) is pa.DataType
assert type(signatures[0].param_types()) is list
assert hasattr(signatures[0], "name")

0 comments on commit 1ef6c26

Please sign in to comment.