Skip to content

Commit ce3812d

Browse files
authored
GH-23970: [GLib] Add support for duration (#48564)
### Rationale for this change It's a primitive type. ### What changes are included in this PR? * Add `GArrowDurationDataType` * Add `GArrowDurationArray` * Add `GArrowDurationArrayBuilder` * Add `GArrowDurationScalar` ### Are these changes tested? Yes. ### Are there any user-facing changes? Yes. * GitHub Issue: #23970 Authored-by: Sutou Kouhei <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent eb73bc6 commit ce3812d

File tree

12 files changed

+576
-0
lines changed

12 files changed

+576
-0
lines changed

c_glib/arrow-glib/array-builder.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ G_BEGIN_DECLS
442442
* #GArrowMonthDayNanoArrayBuilder is the class to create a new
443443
* #GArrowMonthDayNanoArray.
444444
*
445+
* #GArrowDurationArrayBuilder is the class to create a new
446+
* #GArrowDurationArray.
447+
*
445448
* #GArrowStringDictionaryArrayBuilder is the class to create a new
446449
* #GArrowDictionaryArray with a dictionary array of #GArrowStringArray.
447450
*
@@ -4841,6 +4844,97 @@ garrow_month_day_nano_interval_array_builder_append_values(
48414844
});
48424845
}
48434846

4847+
G_DEFINE_TYPE(GArrowDurationArrayBuilder,
4848+
garrow_duration_array_builder,
4849+
GARROW_TYPE_ARRAY_BUILDER)
4850+
4851+
static void
4852+
garrow_duration_array_builder_init(GArrowDurationArrayBuilder *builder)
4853+
{
4854+
}
4855+
4856+
static void
4857+
garrow_duration_array_builder_class_init(GArrowDurationArrayBuilderClass *klass)
4858+
{
4859+
}
4860+
4861+
/**
4862+
* garrow_duration_array_builder_new:
4863+
* @data_type: A #GArrowDurationDataType.
4864+
*
4865+
* Returns: A newly created #GArrowDurationArrayBuilder.
4866+
*
4867+
* Since: 23.0.0
4868+
*/
4869+
GArrowDurationArrayBuilder *
4870+
garrow_duration_array_builder_new(GArrowDurationDataType *data_type)
4871+
{
4872+
auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type));
4873+
auto builder =
4874+
garrow_array_builder_new(arrow_data_type, NULL, "[duration-array-builder][new]");
4875+
return GARROW_DURATION_ARRAY_BUILDER(builder);
4876+
}
4877+
4878+
/**
4879+
* garrow_duration_array_builder_append_value:
4880+
* @builder: A #GArrowDurationArrayBuilder.
4881+
* @value: The elapsed time in 64-bit signed integer.
4882+
* @error: (nullable): Return location for a #GError or %NULL.
4883+
*
4884+
* Returns: %TRUE on success, %FALSE if there was an error.
4885+
*
4886+
* Since: 23.0.0
4887+
*/
4888+
gboolean
4889+
garrow_duration_array_builder_append_value(GArrowDurationArrayBuilder *builder,
4890+
gint64 value,
4891+
GError **error)
4892+
{
4893+
return garrow_array_builder_append_value<arrow::DurationBuilder>(
4894+
GARROW_ARRAY_BUILDER(builder),
4895+
value,
4896+
error,
4897+
"[duration-array-builder][append-value]");
4898+
}
4899+
4900+
/**
4901+
* garrow_duration_array_builder_append_values:
4902+
* @builder: A #GArrowDurationArrayBuilder.
4903+
* @values: (array length=values_length): The array of the elapsed time in
4904+
* 64-bit signed integer.
4905+
* @values_length: The length of `values`.
4906+
* @is_valids: (nullable) (array length=is_valids_length): The array of
4907+
* boolean that shows whether the Nth value is valid or not. If the
4908+
* Nth `is_valids` is %TRUE, the Nth `values` is valid value. Otherwise
4909+
* the Nth value is null value.
4910+
* @is_valids_length: The length of `is_valids`.
4911+
* @error: (nullable): Return location for a #GError or %NULL.
4912+
*
4913+
* Append multiple values at once. It's more efficient than multiple
4914+
* `append` and `append_null` calls.
4915+
*
4916+
* Returns: %TRUE on success, %FALSE if there was an error.
4917+
*
4918+
* Since: 23.0.0
4919+
*/
4920+
gboolean
4921+
garrow_duration_array_builder_append_values(GArrowDurationArrayBuilder *builder,
4922+
const gint64 *values,
4923+
gint64 values_length,
4924+
const gboolean *is_valids,
4925+
gint64 is_valids_length,
4926+
GError **error)
4927+
{
4928+
return garrow_array_builder_append_values<arrow::DurationBuilder>(
4929+
GARROW_ARRAY_BUILDER(builder),
4930+
reinterpret_cast<const int64_t *>(values),
4931+
values_length,
4932+
is_valids,
4933+
is_valids_length,
4934+
error,
4935+
"[duration-array-builder][append-values]");
4936+
}
4937+
48444938
G_DEFINE_TYPE(GArrowBinaryDictionaryArrayBuilder,
48454939
garrow_binary_dictionary_array_builder,
48464940
GARROW_TYPE_ARRAY_BUILDER)
@@ -6892,6 +6986,9 @@ garrow_array_builder_new_raw(std::shared_ptr<arrow::ArrayBuilder> *arrow_builder
68926986
case arrow::Type::type::INTERVAL_MONTH_DAY_NANO:
68936987
type = GARROW_TYPE_MONTH_DAY_NANO_INTERVAL_ARRAY_BUILDER;
68946988
break;
6989+
case arrow::Type::type::DURATION:
6990+
type = GARROW_TYPE_DURATION_ARRAY_BUILDER;
6991+
break;
68956992
case arrow::Type::type::LIST:
68966993
type = GARROW_TYPE_LIST_ARRAY_BUILDER;
68976994
break;

c_glib/arrow-glib/array-builder.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,37 @@ garrow_month_day_nano_interval_array_builder_append_values(
14421442
gint64 is_valids_length,
14431443
GError **error);
14441444

1445+
#define GARROW_TYPE_DURATION_ARRAY_BUILDER (garrow_duration_array_builder_get_type())
1446+
GARROW_AVAILABLE_IN_ALL
1447+
G_DECLARE_DERIVABLE_TYPE(GArrowDurationArrayBuilder,
1448+
garrow_duration_array_builder,
1449+
GARROW,
1450+
DURATION_ARRAY_BUILDER,
1451+
GArrowArrayBuilder)
1452+
struct _GArrowDurationArrayBuilderClass
1453+
{
1454+
GArrowArrayBuilderClass parent_class;
1455+
};
1456+
1457+
GARROW_AVAILABLE_IN_23_0
1458+
GArrowDurationArrayBuilder *
1459+
garrow_duration_array_builder_new(GArrowDurationDataType *data_type);
1460+
1461+
GARROW_AVAILABLE_IN_23_0
1462+
gboolean
1463+
garrow_duration_array_builder_append_value(GArrowDurationArrayBuilder *builder,
1464+
gint64 value,
1465+
GError **error);
1466+
1467+
GARROW_AVAILABLE_IN_23_0
1468+
gboolean
1469+
garrow_duration_array_builder_append_values(GArrowDurationArrayBuilder *builder,
1470+
const gint64 *values,
1471+
gint64 values_length,
1472+
const gboolean *is_valids,
1473+
gint64 is_valids_length,
1474+
GError **error);
1475+
14451476
#define GARROW_TYPE_BINARY_DICTIONARY_ARRAY_BUILDER \
14461477
(garrow_binary_dictionary_array_builder_get_type())
14471478
GARROW_AVAILABLE_IN_2_0

c_glib/arrow-glib/basic-array.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ G_BEGIN_DECLS
184184
* have Arrow format data, you need to use #GArrowMonthDayNanoIntervalArray
185185
* to create a new array.
186186
*
187+
* #GArrowDurationArray is a class for the elapsed time in 64-bit
188+
* signed integer array. It can store zero or more duration data. If
189+
* you don't have Arrow format data, you need to use
190+
* #GArrowDurationArrayBuilder to create a new array.
191+
*
187192
* #GArrowDecimal32Array is a class for 32-bit decimal array. It can
188193
* store zero or more 32-bit decimal data. If you don't have Arrow
189194
* format data, you need to use #GArrowDecimal32ArrayBuilder to
@@ -3452,6 +3457,83 @@ garrow_month_day_nano_interval_array_get_values(GArrowMonthDayNanoIntervalArray
34523457
return g_list_reverse(values);
34533458
}
34543459

3460+
G_DEFINE_TYPE(GArrowDurationArray, garrow_duration_array, GARROW_TYPE_NUMERIC_ARRAY)
3461+
3462+
static void
3463+
garrow_duration_array_init(GArrowDurationArray *object)
3464+
{
3465+
}
3466+
3467+
static void
3468+
garrow_duration_array_class_init(GArrowDurationArrayClass *klass)
3469+
{
3470+
}
3471+
3472+
/**
3473+
* garrow_duration_array_new:
3474+
* @data_type: The #GArrowDurationDataType.
3475+
* @length: The number of elements.
3476+
* @data: The binary data in Arrow format of the array.
3477+
* @null_bitmap: (nullable): The bitmap that shows null elements. The
3478+
* N-th element is null when the N-th bit is 0, not null otherwise.
3479+
* If the array has no null elements, the bitmap must be %NULL and
3480+
* @n_nulls is 0.
3481+
* @n_nulls: The number of null elements. If -1 is specified, the
3482+
* number of nulls are computed from @null_bitmap.
3483+
*
3484+
* Returns: A newly created #GArrowDurationArray.
3485+
*
3486+
* Since: 23.0.0
3487+
*/
3488+
GArrowDurationArray *
3489+
garrow_duration_array_new(GArrowDurationDataType *data_type,
3490+
gint64 length,
3491+
GArrowBuffer *data,
3492+
GArrowBuffer *null_bitmap,
3493+
gint64 n_nulls)
3494+
{
3495+
auto array =
3496+
garrow_primitive_array_new<arrow::DurationType>(GARROW_DATA_TYPE(data_type),
3497+
length,
3498+
data,
3499+
null_bitmap,
3500+
n_nulls);
3501+
return GARROW_DURATION_ARRAY(array);
3502+
}
3503+
3504+
/**
3505+
* garrow_duration_array_get_value:
3506+
* @array: A #GArrowDurationArray.
3507+
* @i: The index of the target value.
3508+
*
3509+
* Returns: The @i-th value.
3510+
*
3511+
* Since: 23.0.0
3512+
*/
3513+
gint64
3514+
garrow_duration_array_get_value(GArrowDurationArray *array, gint64 i)
3515+
{
3516+
auto arrow_array = garrow_array_get_raw(GARROW_ARRAY(array));
3517+
return static_cast<arrow::DurationArray *>(arrow_array.get())->Value(i);
3518+
}
3519+
3520+
/**
3521+
* garrow_duration_array_get_values:
3522+
* @array: A #GArrowDurationArray.
3523+
* @length: (out): The number of values.
3524+
*
3525+
* Returns: (array length=length): The raw values.
3526+
*
3527+
* Since: 23.0.0
3528+
*/
3529+
const gint64 *
3530+
garrow_duration_array_get_values(GArrowDurationArray *array, gint64 *length)
3531+
{
3532+
auto arrow_array = garrow_array_get_raw(GARROW_ARRAY(array));
3533+
auto values = garrow_array_get_values_raw<arrow::DurationType>(arrow_array, length);
3534+
return reinterpret_cast<const gint64 *>(values);
3535+
}
3536+
34553537
G_DEFINE_TYPE(GArrowFixedSizeBinaryArray,
34563538
garrow_fixed_size_binary_array,
34573539
GARROW_TYPE_PRIMITIVE_ARRAY)
@@ -3999,6 +4081,9 @@ garrow_array_new_raw_valist(std::shared_ptr<arrow::Array> *arrow_array,
39994081
case arrow::Type::type::INTERVAL_MONTH_DAY_NANO:
40004082
type = GARROW_TYPE_MONTH_DAY_NANO_INTERVAL_ARRAY;
40014083
break;
4084+
case arrow::Type::type::DURATION:
4085+
type = GARROW_TYPE_DURATION_ARRAY;
4086+
break;
40024087
case arrow::Type::type::LIST:
40034088
type = GARROW_TYPE_LIST_ARRAY;
40044089
break;

c_glib/arrow-glib/basic-array.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,31 @@ GARROW_AVAILABLE_IN_8_0
885885
GList *
886886
garrow_month_day_nano_interval_array_get_values(GArrowMonthDayNanoIntervalArray *array);
887887

888+
#define GARROW_TYPE_DURATION_ARRAY (garrow_duration_array_get_type())
889+
GARROW_AVAILABLE_IN_ALL
890+
G_DECLARE_DERIVABLE_TYPE(
891+
GArrowDurationArray, garrow_duration_array, GARROW, DURATION_ARRAY, GArrowNumericArray)
892+
struct _GArrowDurationArrayClass
893+
{
894+
GArrowNumericArrayClass parent_class;
895+
};
896+
897+
GARROW_AVAILABLE_IN_23_0
898+
GArrowDurationArray *
899+
garrow_duration_array_new(GArrowDurationDataType *data_type,
900+
gint64 length,
901+
GArrowBuffer *data,
902+
GArrowBuffer *null_bitmap,
903+
gint64 n_nulls);
904+
905+
GARROW_AVAILABLE_IN_23_0
906+
gint64
907+
garrow_duration_array_get_value(GArrowDurationArray *array, gint64 i);
908+
909+
GARROW_AVAILABLE_IN_23_0
910+
const gint64 *
911+
garrow_duration_array_get_values(GArrowDurationArray *array, gint64 *length);
912+
888913
#define GARROW_TYPE_FIXED_SIZE_BINARY_ARRAY (garrow_fixed_size_binary_array_get_type())
889914
GARROW_AVAILABLE_IN_3_0
890915
G_DECLARE_DERIVABLE_TYPE(GArrowFixedSizeBinaryArray,

c_glib/arrow-glib/basic-data-type.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ G_BEGIN_DECLS
114114
* #GArrowMonthDayNanoIntervalDataType is a class for the month day
115115
* nano intarval data type.
116116
*
117+
* #GArrowDurationDataType is a class for the elapsed time in the
118+
* 64-bit signed integer data type. It can use one of
119+
* seconds/milliseconds/microseconds/nanoseconds as unit.
120+
*
117121
* #GArrowDecimalDataType is a base class for the decimal data types.
118122
*
119123
* #GArrowDecimal32DataType is a class for the 32-bit decimal data type.
@@ -1482,6 +1486,56 @@ garrow_month_day_nano_interval_data_type_new(void)
14821486
return GARROW_MONTH_DAY_NANO_INTERVAL_DATA_TYPE(data_type);
14831487
}
14841488

1489+
G_DEFINE_TYPE(GArrowDurationDataType,
1490+
garrow_duration_data_type,
1491+
GARROW_TYPE_TEMPORAL_DATA_TYPE)
1492+
1493+
static void
1494+
garrow_duration_data_type_init(GArrowDurationDataType *object)
1495+
{
1496+
}
1497+
1498+
static void
1499+
garrow_duration_data_type_class_init(GArrowDurationDataTypeClass *klass)
1500+
{
1501+
}
1502+
1503+
/**
1504+
* garrow_duration_data_type_new:
1505+
* @unit: The unit of the duration data.
1506+
*
1507+
* Returns: A newly created the elapsed time in 64-bit signed integer
1508+
* data type.
1509+
*
1510+
* Since: 23.0.0
1511+
*/
1512+
GArrowDurationDataType *
1513+
garrow_duration_data_type_new(GArrowTimeUnit unit)
1514+
{
1515+
auto arrow_unit = garrow_time_unit_to_raw(unit);
1516+
auto arrow_data_type = arrow::duration(arrow_unit);
1517+
auto data_type = GARROW_DURATION_DATA_TYPE(
1518+
g_object_new(GARROW_TYPE_DURATION_DATA_TYPE, "data-type", &arrow_data_type, nullptr));
1519+
return data_type;
1520+
}
1521+
1522+
/**
1523+
* garrow_duration_data_type_get_unit:
1524+
* @data_type: The #GArrowDurationDataType.
1525+
*
1526+
* Returns: The unit of the duration data type.
1527+
*
1528+
* Since: 23.0.0
1529+
*/
1530+
GArrowTimeUnit
1531+
garrow_duration_data_type_get_unit(GArrowDurationDataType *data_type)
1532+
{
1533+
const auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type));
1534+
const auto arrow_duration_data_type =
1535+
std::static_pointer_cast<arrow::DurationType>(arrow_data_type);
1536+
return garrow_time_unit_from_raw(arrow_duration_data_type->unit());
1537+
}
1538+
14851539
G_DEFINE_ABSTRACT_TYPE(GArrowDecimalDataType,
14861540
garrow_decimal_data_type,
14871541
GARROW_TYPE_FIXED_SIZE_BINARY_DATA_TYPE)
@@ -2640,6 +2694,9 @@ garrow_data_type_new_raw(std::shared_ptr<arrow::DataType> *arrow_data_type)
26402694
case arrow::Type::type::INTERVAL_MONTH_DAY_NANO:
26412695
type = GARROW_TYPE_MONTH_DAY_NANO_INTERVAL_DATA_TYPE;
26422696
break;
2697+
case arrow::Type::type::DURATION:
2698+
type = GARROW_TYPE_DURATION_DATA_TYPE;
2699+
break;
26432700
case arrow::Type::type::EXTENSION:
26442701
{
26452702
auto g_extension_data_type =

c_glib/arrow-glib/basic-data-type.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,26 @@ GARROW_AVAILABLE_IN_7_0
584584
GArrowMonthDayNanoIntervalDataType *
585585
garrow_month_day_nano_interval_data_type_new(void);
586586

587+
#define GARROW_TYPE_DURATION_DATA_TYPE (garrow_duration_data_type_get_type())
588+
GARROW_AVAILABLE_IN_ALL
589+
G_DECLARE_DERIVABLE_TYPE(GArrowDurationDataType,
590+
garrow_duration_data_type,
591+
GARROW,
592+
DURATION_DATA_TYPE,
593+
GArrowTemporalDataType)
594+
struct _GArrowDurationDataTypeClass
595+
{
596+
GArrowTemporalDataTypeClass parent_class;
597+
};
598+
599+
GARROW_AVAILABLE_IN_23_0
600+
GArrowDurationDataType *
601+
garrow_duration_data_type_new(GArrowTimeUnit unit);
602+
603+
GARROW_AVAILABLE_IN_23_0
604+
GArrowTimeUnit
605+
garrow_duration_data_type_get_unit(GArrowDurationDataType *data_type);
606+
587607
#define GARROW_TYPE_DECIMAL_DATA_TYPE (garrow_decimal_data_type_get_type())
588608
GARROW_AVAILABLE_IN_ALL
589609
G_DECLARE_DERIVABLE_TYPE(GArrowDecimalDataType,

0 commit comments

Comments
 (0)