Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cpp/src/arrow/type_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2418,6 +2418,7 @@ TEST(TypesTest, TestMembership) {
for (auto type : PrimitiveTypes()) {
all_types.push_back(type);
}
TEST_PREDICATE(all_types, is_null);
TEST_PREDICATE(all_types, is_integer);
TEST_PREDICATE(all_types, is_signed_integer);
TEST_PREDICATE(all_types, is_unsigned_integer);
Expand All @@ -2430,6 +2431,8 @@ TEST(TypesTest, TestMembership) {
TEST_PREDICATE(all_types, is_large_binary_like);
TEST_PREDICATE(all_types, is_binary);
TEST_PREDICATE(all_types, is_string);
TEST_PREDICATE(all_types, is_binary_view)
TEST_PREDICATE(all_types, is_string_view);
TEST_PREDICATE(all_types, is_temporal);
TEST_PREDICATE(all_types, is_interval);
TEST_PREDICATE(all_types, is_dictionary);
Expand Down
62 changes: 62 additions & 0 deletions cpp/src/arrow/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,12 @@ using enable_if_physical_floating_point =
/// \addtogroup runtime-type-predicates
/// @{

/// \brief Check for a null type
///
/// \param[in] type_id the type-id to check
/// \return whether type-id is a null type one
constexpr bool is_null(Type::type type_id) { return type_id == Type::NA; }

/// \brief Check for an integer type (signed or unsigned)
///
/// \param[in] type_id the type-id to check
Expand Down Expand Up @@ -1200,6 +1206,34 @@ constexpr bool is_string(Type::type type_id) {
return false;
}

/// \brief Check for a binary-view type
///
/// \param[in] type_id the type-id to check
/// \return whether type-id is a binary-view type one
constexpr bool is_binary_view(Type::type type_id) {
switch (type_id) {
case Type::BINARY_VIEW:
return true;
default:
break;
}
return false;
}

/// \brief Check for a string-view type
///
/// \param[in] type_id the type-id to check
/// \return whether type-id is a string-view type one
constexpr bool is_string_view(Type::type type_id) {
switch (type_id) {
case Type::STRING_VIEW:
return true;
default:
break;
}
return false;
}

/// \brief Check for a temporal type
///
/// \param[in] type_id the type-id to check
Expand Down Expand Up @@ -1517,6 +1551,14 @@ static inline int offset_bit_width(Type::type type_id) {
/// \return the required value alignment in bytes (1 if no alignment required)
int RequiredValueAlignmentForBuffer(Type::type type_id, int buffer_index);

/// \brief Check for a null type
///
/// \param[in] type the type to check
/// \return whether type is a null type
///
/// Convenience for checking using the type's id
static inline bool is_null(const DataType& type) { return is_null(type.id()); }

/// \brief Check for an integer type (signed or unsigned)
///
/// \param[in] type the type to check
Expand Down Expand Up @@ -1623,6 +1665,26 @@ static inline bool is_binary(const DataType& type) { return is_binary(type.id())
/// Convenience for checking using the type's id
static inline bool is_string(const DataType& type) { return is_string(type.id()); }

/// \brief Check for a binary-view type
///
/// \param[in] type the type to check
/// \return whether type is a binary-view type
///
/// Convenience for checking using the type's id
static inline bool is_binary_view(const DataType& type) {
return is_binary_view(type.id());
}

/// \brief Check for a string-view type
///
/// \param[in] type the type to check
/// \return whether type is a string-view type
///
/// Convenience for checking using the type's id
static inline bool is_string_view(const DataType& type) {
return is_string_view(type.id());
}

/// \brief Check for a temporal type, including time and timestamps for each unit
///
/// \param[in] type the type to check
Expand Down
6 changes: 6 additions & 0 deletions python/pyarrow/includes/libarrow.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3019,3 +3019,9 @@ cdef extern from "arrow/python/udf.h" namespace "arrow::py" nogil:

cdef extern from "arrow/compute/cast.h" namespace "arrow::compute":
CResult[CDatum] Cast(const CDatum& value, const CCastOptions& options)

cdef extern from "arrow/type_traits.h" namespace "arrow":
c_bool is_null(Type type_id)
c_bool is_binary_view(Type type_id)
c_bool is_string_view(Type type_id)

3 changes: 3 additions & 0 deletions python/pyarrow/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ include "pandas-shim.pxi"
# Memory pools and allocation
include "memory.pxi"

# TypeTraits
include "type_traits.pxi"

# DataType, Field, Schema
include "types.pxi"

Expand Down
70 changes: 70 additions & 0 deletions python/pyarrow/type_traits.pxi
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from pyarrow.includes.libarrow cimport (is_null,
is_binary_view,
is_string_view)

def _is_null(data_type):
"""
This function checks whether the data type is null type.

Parameters
----------
data_type: DataType
The data type to check against the null type.

Returns
-------
bool
True if the data type is null type, False otherwise.
"""
return is_null(data_type.id)


def _is_binary_view(data_type):
"""
This function checks whether the data type is binary view type.

Parameters
----------
data_type: DataType
The data type to check against the binary view type.

Returns
-------
bool
True if the data type is binary view type, False otherwise.
"""
return is_binary_view(data_type.id)


def _is_string_view(data_type):
"""
This function checks whether the data type is string view type.

Parameters
----------
data_type: DataType
The data type to check against the string view type.

Returns
-------
bool
True if the data type is string view type, False otherwise.
"""
return is_string_view(data_type.id)
18 changes: 7 additions & 11 deletions python/pyarrow/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

from pyarrow.lib import (is_boolean_value, # noqa
is_integer_value,
is_float_value)
is_float_value,
_is_null,
_is_binary_view,
_is_string_view)

import pyarrow.lib as lib
from pyarrow.util import doc
Expand All @@ -47,14 +50,7 @@

@doc(datatype="null")
def is_null(t):
"""
Return True if value is an instance of type: {datatype}.

Parameters
----------
t : DataType
"""
return t.id == lib.Type_NA
return _is_null(t)


@doc(is_null, datatype="boolean")
Expand Down Expand Up @@ -256,12 +252,12 @@ def is_fixed_size_binary(t):

@doc(is_null, datatype="variable-length binary view")
def is_binary_view(t):
return t.id == lib.Type_BINARY_VIEW
return _is_binary_view(t)


@doc(is_null, datatype="variable-length string (utf-8) view")
def is_string_view(t):
return t.id == lib.Type_STRING_VIEW
return _is_string_view(t)


@doc(is_null, datatype="date")
Expand Down