计算与计算函数
通用compute API
函数和函数注册表
函数表示对可能不同类型的输入执行的计算操作。在内部,函数由一个或多个kernal实现,具体取决于具体
的输入类型(例如,将两个输入的值相加的函数可以具有不同的内核,具体取决于输入是整数还是浮点数)。
函数存储在全局FunctionRegistry中, 可以通过名称进行查找。
输入数据形状
计算输入表示为通用的 :class:Datum 类,它是多种数据形状(如 :class:Scalar、
:class:Array 和 :class:ChunkedArray)的标记联合。许多计算函数都支持数
组(分块或非分块)和标量输入,但有些函数会要求特定的输入类型。例如,虽
然 array_sort_indices 要求其第一个也是唯一的输入是数组,但通用的 sort_indices 函
数接受数组、分块数组、记录批处理或表。
调用函数
可以使用名称调用计算函数alkaid::compute::call_function:
std::shared_ptr<alkaid::Array> numbers_array = ...;
std::shared_ptr<alkaid::Scalar> increment = ...;
alkaid::Datum incremented_datum;
ARROW_ASSIGN_OR_RAISE(incremented_datum,
alkaid::compute::call_function("add", {numbers_array, increment}));
std::shared_ptr<Array> incremented_array = std::move(incremented_datum).make_array();
(请注意,此示例使用了从std::shared_ptr<Array> to Datum)
许多计算函数也可直接作为具体 API 使用,此处 alkaid::compute::Add:
std::shared_ptr<alkaid::Array> numbers_array = ...;
std::shared_ptr<alkaid::Scalar> increment = ...;
alkaid::Datum incremented_datum;
ARROW_ASSIGN_OR_RAISE(incremented_datum,
alkaid::compute::Add(numbers_array, increment));
std::shared_ptr<Array> incremented_array = std::move(incremented_datum).make_array();
某些函数接受或需要选项结构来确定函数的确切语义:
ScalarAggregateOptions scalar_aggregate_options;
scalar_aggregate_options.skip_nulls = false;
std::shared_ptr<alkaid::Array> array = ...;
alkaid::Datum min_max;
ARROW_ASSIGN_OR_RAISE(min_max,
alkaid::compute::call_function("min_max", {array},
&scalar_aggregate_options));
// Unpack struct scalar result (a two-field {"min", "max"} scalar)
std::shared_ptr<alkaid::Scalar> min_value, max_value;
min_value = min_max.scalar_as<alkaid::StructScalar>().value[0];
max_value = min_max.scalar_as<alkaid::StructScalar>().value[1];
However,
Grouped Aggregations <grouped-aggregations-group-by> are not invocable via call_function.
Compute API reference <api/compute>
隐式强制转换
如果内核与参数类型不完全匹配,函数可能需要在执行之前转换其参数。 例如,任何内核都不直接支持字典编码数组的比较,但可以进行隐式转换, 允许与解码数组进行比较。每个函数可以根据需要定义隐式转换行为。例如, 比较和算术内核需要相同类型的参数,并且通过将其参数提升为可以容纳来自 任一输入的任何值的数字类型来支持针对不同数字类型的执行。
常见数字类型
一组输入数字类型的公共数字类型是可以容纳任何输入的任何值的最小数字类型。 如果任何输入是浮点类型,则公共数字类型是输入中最宽的浮点类型。否则,公 共数字类型是整数,并且如果有任何输入是有符号的,则公共数字类型是有符号 的。例如:
| Input types | Common numeric type | Notes |
|---|---|---|
| int32, int32 | int32 | |
| int16, int32 | int32 | Max width is 32, promote LHS to int32 |
| uint16, int32 | int32 | One input signed, override unsigned |
| uint32, int32 | int64 | Widen to accommodate range of uint32 |
| uint16, uint32 | uint32 | All inputs unsigned, maintain unsigned |
| int16, uint32 | int64 | |
| uint64, int16 | int64 | int64 cannot accommodate all uint64 values |
| float32, int32 | float32 | Promote RHS to float32 |
| float32, float64 | float64 | |
| float32, int64 | float32 | int64 is wider, still promotes to float32 |
特别需要注意的是,如果其中一个 uint64 值不能表示为通用类型 int64(例如 2 ** 63),则将 uint64 列与 int16 列进行比较可能会引发错误。
可用函数
类型类别
为了避免详尽列出支持的类型,下表使用了许多通用类型类别:
Numeric:整数类型(Int8 等)和浮点类型(Float32、Float64,有时为 Float16)。某些函数还接受 Decimal128 和 Decimal256 输入。Temporal:日期类型(Date32、Date64)、时间类型(Time32、Time64)、时间戳、持续时间、间隔。Binary-like:二进制、LargeBinary,有时也为 FixedSizeBinary。String-like:字符串、LargeString。List-like:列表、LargeList、ListView、LargeListView,有时也为 FixedSizeList。Nested:列表类型(包括 FixedSizeList)、结构、联合和相关类型(如 Map)。
如果您不确定某个函数是否支持具体的输入类型,我们建议您尝试一下。不支持的输入类型会返回TypeError
Status。
聚合
标量聚合对(分块)数组或标量值进行操作,并将输入减少为单个输出值。
| Function name | Arity | Input types | Output type | Options class | Notes |
|---|---|---|---|---|---|
| all | Unary | Boolean | Scalar Boolean | ScalarAggregateOptions | (1) |
| any | Unary | Boolean | Scalar Boolean | ScalarAggregateOptions | (1) |
| approximate_median | Unary | Numeric | Scalar Float64 | ScalarAggregateOptions | |
| count | Unary | Any | Scalar Int64 | CountOptions | (2) |
| count_all | Nullary | Scalar Int64 | |||
| count_distinct | Unary | Non-nested types | Scalar Int64 | CountOptions | (2) |
| first | Unary | Numeric, Binary | Scalar Input type | ScalarAggregateOptions | (11) |
| first_last | Unary | Numeric, Binary | Scalar Struct | ScalarAggregateOptions | (11) |
| index | Unary | Any | Scalar Int64 | IndexOptions | (3) |
| last | Unary | Numeric, Binary | Scalar Input type | ScalarAggregateOptions | (11) |
| max | Unary | Non-nested types | Scalar Input type | ScalarAggregateOptions | |
| mean | Unary | Numeric | Scalar Decimal/Float64 | ScalarAggregateOptions | (4) |
| min | Unary | Non-nested types | Scalar Input type | ScalarAggregateOptions | |
| min_max | Unary | Non-nested types | Scalar Struct | ScalarAggregateOptions | (5) |
| mode | Unary | Numeric | Struct | ModeOptions | (6) |
| product | Unary | Numeric | Scalar Numeric | ScalarAggregateOptions | (7) |
| quantile | Unary | Numeric | Scalar Numeric QuantileOptions | (8) | |
| stddev | Unary | Numeric | Scalar Float64 | VarianceOptions | (9) |
| sum | Unary | Numeric | Scalar Numeric | ScalarAggregateOptions | (7) |
| tdigest | Unary | Numeric | Float64 | TDigestOptions | (10) |
| variance | Unary | Numeric | Scalar Float64 | VarianceOptions | (9) |
- (1) 如果通过设置 ScalarAggregateOptions 参数 skip_nulls = false 来考虑空值, 则应用 Kleene 逻辑 逻辑。 不遵守 min_count 选项。
- (2) CountMode 控制是否仅计算非空值(默认)、仅计算空值或计算所有值。
- (3) 如果未找到该值,则返回 -1。无论输入中是否有空值,空值的索引始终为 -1。
- (4) 对于十进制输入,生成的十进制将具有相同的精度和小数位数。结果从零开始四舍五入。
- (5) 输出是
{"min": 输入类型,"max": 输入类型}结构。
在间隔类型中,仅支持月份间隔,因为 day-time 和 month-day-nano 类型不可排序。
- (6) 输出是一个
{"mode": 输入类型, "count": Int64}结构数组。
它包含输入中最常见的 N 个元素,按降序排列,其中 N 在 ModeOptions::n 中给出。如果两个值的计数相同,则最小的值排在最前面。请注意,如果输入具有少于 N 个不同值,则输出可以具有少于 N 个元素。
-
(7) 输出为 Int64、UInt64、Float64 或 Decimal128/256,具体取决于输入类型。
-
(8) 输出为 Float64 或输入类型,具体取决于 QuantileOptions。
-
(9) Decimal 参数首先转换为 Float64。
-
(10) tdigest/t-digest 计算近似分位数,因此只需要固定数量的内存。有关详细信息,请参阅参考 实现。
-
(11) 结果基于输入数据的排序
十进制参数首先转换为 Float64。
分组聚合 ("group by")
分组聚合不能直接调用,但可用作 SQL 样式group by操作的一部分。与标量聚合一样,分组聚合将
多个输入值简化为单个输出值。但是,分组聚合不会聚合输入的所有值,而是根据一组key列对输入值进
行分区 ,然后分别聚合每个组,为每个输入组发出一个输出值。例如,对于下表:
Column key | Column x |
|---|---|
| "a" | 2 |
| "a" | 5 |
| "b" | null |
| "b" | null |
| null | null |
| null | 9 |
我们可以计算列x的总和,并按列key分组。
这为我们提供了三组,结果如下。请注意,null
被视为不同的键值。
Column key | Column sum(x) |
|---|---|
| "a" | 7 |
| "b" | null |
| null | 9 |
支持的聚合函数如下。所有函数名都以 hash_ 为前缀,这将它们与上面的标量等价函数区分开来,并反映了它们的内部实现方式。
| Function name | Arity | Input types | Output type | Options class | Notes |
|---|---|---|---|---|---|
| hash_all | Unary | Boolean | Boolean | ScalarAggregateOptions | (1) |
| hash_any | Unary | Boolean | Boolean | ScalarAggregateOptions | (1) |
| hash_approximate_median | Unary | Numeric | Float64 | ScalarAggregateOptions | |
| hash_count | Unary | Any | Int64 | CountOptions | (2) |
| hash_count_all | Nullary | Int64 | |||
| hash_count_distinct | Unary | Any | Int64 | CountOptions | (2) |
| hash_distinct | Unary | Any | List of input type | CountOptions | (2) (3) |
| hash_first | Unary | Numeric, Binary | Input type | ScalarAggregateOptions | (10) |
| hash_first_last | Unary | Numeric, Binary | Struct | ScalarAggregateOptions | (10) |
| hash_last | Unary | Numeric, Binary | Input type | ScalarAggregateOptions | (10) |
| hash_list | Unary | Any | List of input type | (3) | |
| hash_max | Unary | Non-nested, non-binary/string-like | Input type | ScalarAggregateOptions | |
| hash_mean | Unary | Numeric | Decimal/Float64 | ScalarAggregateOptions | (4) |
| hash_min | Unary | Non-nested, non-binary/string-like | Input type | ScalarAggregateOptions | |
| hash_min_max | Unary | Non-nested types | Struct | ScalarAggregateOptions | (5) |
| hash_one | Unary | Any | Input type | (6) | |
| hash_product | Unary | Numeric | Numeric | ScalarAggregateOptions | (7) |
| hash_stddev | Unary | Numeric | Float64 | VarianceOptions | (8) |
| hash_sum | Unary | Numeric | Numeric | ScalarAggregateOptions | (7) |
| hash_tdigest | Unary | Numeric | FixedSizeList[Float64] | TDigestOptions | (9) |
| hash_variance | Unary | Numeric | Float64 | VarianceOptions | (8) |
- (1) 如果考虑了空值,则通过将
ScalarAggregateOptions::skip_nulls设置为 false,然后应用 Kleene logic 逻辑。不遵守 min_count 选项。 - (2) CountMode 控制是否仅计算非空值(默认)、仅计算空值或计算所有值。对于 hash_distinct,它控制是否发出空值。 这永远不会影响分组键,只会影响组值(即,您可能会得到一个键为空的组)。
- (3)
hash_distinct和hash_list将分组值收集到列表数组中。 - (4) 对于十进制输入,结果十进制将具有相同的 精度和小数位数。结果从零开始四舍五入。
- (5) 输出为
{"min": 输入类型,"max": 输入类型}结构数组。
在间隔类型中,仅支持月份间隔,因为 day-time 和 month-day-nano 类型不可排序。
- (6)
hash_one为每个组返回一个来自输入的任意值。该函数偏向于非空值:如果某个组至少有一个非空值,则返回该值,并且只有当该组的所有值均为null时,函数才会返回null。 - (7) 输出为 Int64、UInt64、Float64 或 Decimal128/256,具体取决于输入类型。
- (8) Decimal 参数首先转换为 Float64。
- (9) T-digest 计算近似分位数,因此只需要固定数量的内存。有关详细信息,请参阅 参考实现。
- (10) 结果基于输入数据的排序。
十进制参数首先转换为 Float64。
functions 元素级 ("scalar") 函数
所有元素函 数都接受数组和标量作为输入。 一元函数的语义如下:
- 标量输入产生标量输出
- 数组输入产生数组输出
二元函数具有以下语义(在其他系统(如 NumPy)中有时称为 "broadcasting"):
(scalar, scalar)输入产生标量输出(array, array)输入产生数组输出(并且两个输入 必须具有相同的长度)(scalar, array)和(array, scalar)产生数组输出。 标量输入被处理为与其他输入长度相同的数组 N,并且相同的值重复 N 次。
算术函数
这些函数需要数字类型的输入,并对从输入中收集的每个元素应用给定的
算术运算。如果
任何输入元素为空,则相应的输出元素为
空。对于二进制函数,在应用操作之前,输入将转换为
通用数字类型 <common-numeric-type>(如果适用,则对字典进行解码)。
这些函数的默认变体不检测溢出(结果通常会回绕)。大多数函数也提供溢出检查变体,后缀 为“_checked”,当检测到溢出时,它会返回“无效”状态。
对于支持十进制输入的函数(目前为“加”、“减”、“乘”和“除”及其已检查的变体),将 适当提升不同精度/小数位数的十进制。混合十进制和浮点参数会将所有参数转换为浮点, 而混合十进制和整数参数会将所有参数转换为十进制。混合时间分辨率时间输入将转换为最精细的输入分辨率。
| Function name | Arity | Input types | Output type | Notes |
|---|---|---|---|---|
| abs | Unary | Numeric/Duration | Numeric/Duration | |
| abs_checked | Unary | Numeric/Duration | Numeric/Duration | |
| add | Binary | Numeric/Temporal | Numeric/Temporal | (1) |
| add_checked | Binary | Numeric/Temporal | Numeric/Temporal | (1) |
| divide | Binary | Numeric/Temporal | Numeric/Temporal | (1) |
| divide_checked | Binary Numeric/Temporal | Numeric/Temporal | (1) | |
| exp | Unary | Numeric | Float32/Float64 | |
| expm1 | Unary | Numeric | Float32/Float64 | |
| multiply | Binary | Numeric/Temporal | Numeric/Temporal | (1) |
| multiply_checked | Binary | Numeric/Temporal | Numeric/Temporal | (1) |
| negate | Unary | Numeric/Duration | Numeric/Duration | |
| negate_checked | Unary | Signed Numeric/Duration | Signed Numeric/Duration | |
| power | Binary | Numeric | Numeric | |
| power_checked | Binary | Numeric | Numeric | |
| sign | Unary | Numeric/Duration | Int8/Float32/Float64 | (2) |
| sqrt | Unary | Numeric | Numeric | |
| sqrt_checked | Unary | Numeric | Numeric | |
| subtract | Binary | Numeric/Temporal | Numeric/Temporal | (1) |
| subtract_checked | Binary | Numeric/Temporal | Numeric/Temporal | (1) |
-
(1) 计算 DECIMAL 结果的精度和小数位数
Operation Result precision and scale add scale = max(s1, s2) subtract precision = max(p1-s1, p2-s2) + 1 + scale multiply scale = s1 + s2 precision = p1 + p2 + 1 divide scale = max(4, s1 + p2 - s2 + 1) precision = p1 - s1 + s2 + scale 它与 Redshift 的十进制提升规则兼容。对于“加”、“减”和“乘”运算,所有十进制数字均会保留。除法的结果精度至少是两 个操作数的精度之和,且保留足够的小数位数。如果结果精度超出十进制值范围,则会返回错误。
-
(2) 对于非零输入,输出为 (-1,1) 中的任意一个,对于零输入,输出为 0。NaN 值返回 NaN。整数和十进制值以 Int8的形式返回符号,浮点值以与输入 值相同的类型返回符号。
按位函数
| Function name | Arity | Input types | Output type |
|---|---|---|---|
| bit_wise_and | Binary | Numeric | Numeric |
| bit_wise_not | Unary | Numeric | Numeric |
| bit_wise_or | Binary | Numeric | Numeric |
| bit_wise_xor | Binary | Numeric | Numeric |
| shift_left | Binary | Numeric | Numeric |
| shift_left_checked | Binary | Numeric | Numeric (1) |
| shift_right | Binary | Numeric | Numeric |
| shift_right_checked | Binary | Numeric | Numeric (1) |
- (1) 如果移位量(即第二个输入)超出数据类型的范围,则会发出错误。但是,移位第一个输入时溢出不会出错(截断的位会被默默丢弃)。
舍入函数
舍入函数根据舍入标准,将数字输入替换为具有更简单表示形式的近似值。
| Function name | Arity | Input types | Output type | Options class | Notes |
|---|---|---|---|---|---|
| ceil | Unary | Numeric | Float32/Float64/Decimal | ||
| floor | Unary | Numeric | Float32/Float64/Decimal | ||
| round | Unary | Numeric | Input Type | RoundOptions | (1)(2) |
| round_to_multiple | Unary | Numeric | Input Type | RoundToMultipleOptions | (1)(3) |
| round_binary | Binary | Numeric | Input Type | RoundBinaryOptions | (1)(4) |
| trunc | Unary | Numeric | Float32/Float64/Decimal |
- (1) 默认情况下,舍入函数会将值更改为最接近的整数
使用 HALF_TO_EVEN 来解决平局。可以使用选项来控制
舍入标准。所有
round函数都有round_mode选项来设置舍入模式。 - (2) 四舍五入到一定位数,其中
RoundOptions的ndigits选项指定以位数表示的四舍五入 精度。负值对应于非小数部分的数字。例如,-2 对应于四舍五入到最接近的 100 的倍数(将个位和十位清零)。ndigits的默认值为 0,四舍五入到最接近的整数。对于整数输入,非负ndigits值将被忽略,输入将保 持不变。对于整数输入,如果-ndigits大于输入类型可以容纳的最大位数,则返回错误。 - (3) 四舍五入为倍数,其中
RoundToMultipleOptions的multiple选项指定 舍入比例。舍入倍数必须是正值,并且可以转换为输入类型。例如,100 对应于舍入到 100 的 最接近的倍数(将个位和十位数字归零)。倍数的默认值为 1,即舍入到最接近的整数。 - (4) 将第一个输入四舍五入为第二个输入的倍数。四舍五入倍数必须是正值,并且可以转换为第一 个输入类型。例如,100 对应于四舍五入到最接近的 100 的倍数(将个位和十位数字清零)。
对于 round 函数,可以使用以下舍入模式。
平局决胜模式以 HALF 为前缀,并将非平局舍入为最接近的整数。示例值针对 ndigits 和 multiple 的默认值给出。
round_mode | Operation performed | Example values |
|---|---|---|
| DOWN | Round to nearest integer less than or equal in magnitude; also known as floor(x) | 3.2 -> 3, 3.7-> 3, -3.2 ->-4, -3.7 -> -4 |
| UP | Round to nearest integer greater than or equal in magnitude; also known as ceil(x) | 3.2 -> 4, 3.7-> 4, -3.2 -> -3, -3.7 -> -3 |
| TOWARDS_ZERO | Get the integral part without fractional digits; also known as trunc(x) | 3.2 -> 3, 3.7 -> 3, -3.2 -> -3, -3.7 -> -3 |
| TOWARDS_INFINITY | Round negative values with DOWN rule, round positive values with UP rule | 3.2 -> 4, 3.7 -> 4, -3.2 -> -4, -3.7 -> -4 |
| HALF_DOWN | Round ties with DOWN rule | 3.5 -> 3, 4.5 -> 4, -3.5 -> -4, -4.5 -> -5 |
| HALF_UP | Round ties with UP rule | 3.5 -> 4, 4.5 -> 5, -3.5 -> -3, -4.5 -> -4 |
| HALF_TOWARDS_ZERO | Round ties with TOWARDS_ZERO rule | 3.5 -> 3, 4.5 -> 4, -3.5 -> -3, -4.5 -> -4 |
| HALF_TOWARDS_INFINITY | Round ties with TOWARDS_INFINITY rule | 3.5 -> 4, 4.5 -> 5, -3.5 -> -4, -4.5 -> -5 |
| HALF_TO_EVEN | Round ties to nearest even integer | 3.5 -> 4, 4.5 -> 4, -3.5 -> -4, -4.5 -> -4 |
| HALF_TO_ODD | Round ties to nearest odd integer | 3.5 -> 3, 4.5 -> 5, -3.5 -> -3, -4.5 -> -5 |
下表分别举例说明了 ndigits(用于 round 和
round_binary 函数)和 multiple(用于 round_to_multiple)如何影响所执行的运算。
Round multiple | Round ndigits | Operation performed |
|---|---|---|
| 1 | 0 | Round to integer |
| 0.001 | 3 | Round to 3 decimal places |
| 10 | -1 | Round to multiple of 10 |
| 2 | NA | Round to multiple of 2 |
对数函数
还支持对数函数,并且还提供 _checked变体,用于在需要时检查域错误。
接受十进制值,但首先将其转换为 Float64。
| Function name | Arity | Input types | Output type |
|---|---|---|---|
| ln | Unary | Float32/Float64/Decimal | Float32/Float64 |
| ln_checked | Unary | Float32/Float64/Decimal | Float32/Float64 |
| log10 | Unary | Float32/Float64/Decimal | Float32/Float64 |
| log10_checked | Unary | Float32/Float64/Decimal | Float32/Float64 |
| log1p | Unary | Float32/Float64/Decimal | Float32/Float64 |
| log1p_checked | Unary | Float32/Float64/Decimal | Float32/Float64 |
| log2 | Unary | Float32/Float64/Decimal | Float32/Float64 |
| log2_checked | Unary | Float32/Float64/Decimal | Float32/Float64 |
| logb | Binary | Float32/Float64/Decimal | Float32/Float64 |
| logb_checked | Binary | Float32/Float64/Decimal | Float32/Float64 |
三角函数
还支持三角函数,并提供 _checked
变体,用于在需要时检查域错误。
接受十进制值,但首先将其转换为 Float64。
| Function name | Arity | Input types | Output type |
|---|---|---|---|
| acos | Unary | Float32/Float64/Decimal | Float32/Float64 |
| acos_checked | Unary | Float32/Float64/Decimal | Float32/Float64 |
| asin | Unary | Float32/Float64/Decimal | Float32/Float64 |
| asin_checked | Unary | Float32/Float64/Decimal | Float32/Float64 |
| atan | Unary | Float32/Float64/Decimal | Float32/Float64 |
| atan2 | Binary | Float32/Float64/Decimal | Float32/Float64 |
| cos | Unary | Float32/Float64/Decimal | Float32/Float64 |
| cos_checked | Unary | Float32/Float64/Decimal | Float32/Float64 |
| sin | Unary | Float32/Float64/Decimal | Float32/Float64 |
| sin_checked | Unary | Float32/Float64/Decimal | Float32/Float64 |
| tan | Unary | Float32/Float64/Decimal | Float32/Float64 |
| tan_checked | Unary | Float32/Float64/Decimal | Float32/Float64 |
双曲三角函数
还支持双曲三角函数,并且在适用的情况下,还提供_checked变体,用于检查域错误(如果需要)。
接受十进制值,但首先转换为Float64。
| Function name | Arity | Input types | Output type |
|---|---|---|---|
| acosh | Unary | Float32/Float64/Decimal | Float32/Float64 |
| acosh_checked | Unary | Float32/Float64/Decimal | Float32/Float64 |
| asinh | Unary | Float32/Float64/Decimal | Float32/Float64 |
| atanh | Unary | Float32/Float64/Decimal | Float32/Float64 |
| atanh_checked | Unary | Float32/Float64/Decimal | Float32/Float64 |
| cosh | Unary | Float32/Float64/Decimal | Float32/Float64 |
| sinh | Unary | Float32/Float64/Decimal | Float32/Float64 |
| tanh | Unary | Float32/Float64/Decimal | Float32/Float64 |
比较函数
这些函数需要两个数字类型的输入(在这种情况下,它们将在比较之前转换为通用数字类型 <common-numeric-type>),或两个
二进制或字符串类型的输入,或两个时间类型的输入。如果任何输入是字典编码的,它将被扩展以进行比较。如果一对中的任何输入元素为空,
则相应的输出元素为空。十进制参数将以与add和subtract相同的方式提升。
| Function names | Arity | Input types | Output type |
|---|---|---|---|
| equal | Binary | Numeric, Temporal, Binary- and String-like | Boolean |
| greater | Binary | Numeric, Temporal, Binary- and String-like | Boolean |
| greater_equal | Binary | Numeric, Temporal, Binary- and String-like | Boolean |
| less | Binary | Numeric, Temporal, Binary- and String-like | Boolean |
| less_equal | Binary | Numeric, Temporal, Binary- and String-like | Boolean |
| not_equal | Binary | Numeric, Temporal, Binary- and String-like | Boolean |
这些函数接受任意数量的数字类型输入(在这种情况下,它们将在比较之前转换为通用数字类型 <common-numeric-type>)或时间
类型输入。如果任何输入是字典编码的,它将被扩展以用于比较。
| Function names | Arity | Input types | Output type | Options class | Notes |
|---|---|---|---|---|---|
| max_element_wise | Varargs | Numeric, Temporal, Binary- and String-like | Numeric or Temporal | :struct:ElementWiseAggregateOptions | (1) |
| min_element_wise | Varargs | Numeric, Temporal, Binary- and String-like | Numeric or Temporal | :struct:ElementWiseAggregateOptions | (1) |
- (1) 默认情况下,会跳过空值(但可以配置内核以传播空值)。对于浮点值,NaN 将取代空值,但不会取代任何其他值。对于二进制和字符串类值,仅支持相同类型参数。
逻辑函数
这些函数的正常行为是,如果任何输入为空,则发出一个空值(类似于浮点计算中“NaN”的语义)。
其中一些也可用于 Kleene 逻辑 变体(后缀为 _kleene),其中 null 被视为“未定义”。
例如,这是 SQL 系统以及 R 和 Julia 中使用的 null 的解释。
因此,对于 Kleene 逻辑变体:
- “true AND null”、“null AND true” 给出“null”(结果为 未定义)
- “true OR null”、“null OR true” 给出“true”
- “false AND null”、“null AND false” 给出“false”
- “false OR null”、“null OR false” 给出“null”(结果为 未定义)
| Function name | Arity | Input types | Output type |
|---|---|---|---|
| and | Binary | Boolean | Boolean |
| and_kleene | Binary | Boolean | Boolean |
| and_not | Binary | Boolean | Boolean |
| and_not_kleene | Binary | Boolean | Boolean |
| invert | Unary | Boolean | Boolean |
| or | Binary | Boolean | Boolean |
| or_kleene | Binary | Boolean | Boolean |
| xor | Binary | Boolean | Boolean |
字符串谓词
这些函数根据输入字符串元素 的字符内容对其进行分类。空字符串元素在输出中发出 false。
对于函数的 ASCII 变体(前缀为 ascii_),包含非 ASCII 字符的字符串元素在输出中发出 false。
第一组函数以每个字符为基础进行操作, 如果输入仅包含给定类别的字符,则在输出中发出 true:
| Function name | Arity | Input types | Output type | Matched character class | Notes |
|---|---|---|---|---|---|
| ascii_is_alnum | Unary | String-like | Boolean | Alphanumeric ASCII | |
| ascii_is_alpha | Unary | String-like | Boolean | Alphabetic ASCII | |
| ascii_is_decimal | Unary | String-like | Boolean | Decimal ASCII | (1) |
| ascii_is_lower | Unary | String-like | Boolean | Lowercase ASCII | (2) |
| ascii_is_printable | Unary | String-like | Boolean | Printable ASCII | |
| ascii_is_space | Unary | String-like | Boolean | Whitespace ASCII | |
| ascii_is_upper | Unary | String-like | Boolean | Uppercase ASCII | (2) |
| utf8_is_alnum | Unary | String-like | Boolean | Alphanumeric Unicode | |
| utf8_is_alpha | Unary | String-like | Boolean | Alphabetic Unicode | |
| utf8_is_decimal | Unary | String-like | Boolean | Decimal Unicode | |
| utf8_is_digit | Unary | String-like | Boolean | Unicode digit | (3) |
| utf8_is_lower | Unary | String-like | Boolean | Lowercase Unicode | (2) |
| utf8_is_numeric | Unary | String-like | Boolean | Numeric Unicode | (4) |
| utf8_is_printable | Unary | String-like | Boolean | Printable Unicode | |
| utf8_is_space | Unary | String-like | Boolean | Whitespace Unicode | |
| utf8_is_upper | Unary | String-like | Boolean | Uppercase Unicode | (2) |
- (1) 还匹配所有数字 ASCII 字符和所有 ASCII 数字。
- (2) 非大小写字符(如标点符号)不匹配。
- (3) 目前与
utf8_is_decimal相同。 - (4) 与
utf8_is_decimal不同,非十进制数字字符也匹配。
第二组函数还考虑字符串元素中的字符顺序:
| Function name | Arity | Input types | Output type | Notes |
|---|---|---|---|---|
| ascii_is_title | Unary | String-like | Boolean | (1) |
| utf8_is_title | Unary | String-like | Boolean | (1) |
- (1) 当且仅当输入字符串元素为标题大小写时,输出为真,即任何单词都以大写字母开头,后跟小写字母。单词边界由非大小写字符定义。
第三组函数以逐字节为基础检查字符串元素:
| Function name | Arity | Input types | Output type | Notes |
|---|---|---|---|---|
| string_is_ascii | Unary | String-like | Boolean | (1) |
- (1) 当且仅当输入字符串元素仅包含 ASCII 字符(即仅包含 [0, 127] 内的字节)时,输出为真。
String transforms
| Function name | Arity | Input types | Output type | Options class | Notes |
|---|---|---|---|---|---|
| ascii_capitalize | Unary | String-like | String-like | (1) | |
| ascii_lower | Unary | String-like | String-like | (1) | |
| ascii_reverse | Unary | String-like | String-like | (2) | |
| ascii_swapcase | Unary | String-like | String-like | (1) | |
| ascii_title | Unary | String-like | String-like | (1) | |
| ascii_upper | Unary | String-like | String-like | (1) | |
| binary_length | Unary | Binary- or String-like | Int32 or Int64 | (3) | |
| binary_repeat | Binary | Binary/String (Arg 0); Integral (Arg 1) | Binary- or String-like | (4) | |
| binary_replace_slice | Unary | String-like | Binary- or String-like | :struct:ReplaceSliceOptions | (5) |
| binary_reverse | Unary | Binary | Binary | (6) | |
| replace_substring | Unary | String-like | String-like | :struct:ReplaceSubstringOptions | (7) |
| replace_substring_regex | Unary | String-like | String-like | :struct:ReplaceSubstringOptions | (8) |
| utf8_capitalize | Unary | String-like | String-like | (9) | |
| utf8_length | Unary | String-like | Int32 or Int64 | (10) | |
| utf8_lower | Unary | String-like | String-like | (9) | |
| utf8_replace_slice | Unary | String-like | String-like | :struct:ReplaceSliceOptions | (7) |
| utf8_reverse | Unary | String-like | String-like | (11) | |
| utf8_swapcase | Unary | String-like | String-like | (9) | |
| utf8_title | Unary | String-like | String-like | (9) | |
| utf8_upper | Unary | String-like | String-like | (9) |
- (1) 输入中的每个 ASCII 字符都转换为小写或 大写。非 ASCII 字符保持不变。
- (2) ASCII 输入与输出相反。如果存在非 ASCII 字符,则将返回
InvalidStatus。 - (3) 输出是每个输入元素的物理长度(以字节为单位)。输出 类型为二进制/字符串的 Int32,大型二进制/大型字符串的 Int64。
- (4) 重复输入二进制字符串给定次数。
- (5) 将子字符串的切片从
ReplaceSliceOptions::start(含) 替换为ReplaceSliceOptions::stop(不含),替换为ReplaceSubstringOptions::replacement。 二进制内核以字节为单位测量切片,而 UTF8 内核以代码单元为单位测量切片。 - (6) 执行字节级反转。
- (7) 替换与匹配的非重叠子字符串
ReplaceSubstringOptions::pattern由ReplaceSubstringOptions::replacement实现。 如果ReplaceSubstringOptions::max_replacements!= -1,则确定进行的最大替换次数,从左侧开始计数。 - (8) 使用 Google RE2 库,通过
ReplaceSubstringOptions::replacement替换与正则表达式ReplaceSubstringOptions::pattern匹配的非重叠子字符串。如果ReplaceSubstringOptions::max_replacements!= -1,则确定进行的最大替换次数,从左侧开始计数。请注意,如果模式包含组,则可以使用反向引用。 - (9) 输入中的每个 UTF8 编码字符都转换为小写或大写。
- (10) 输出是每个输入元素的字符数(而非字节数)。对于字符串,输出类型为 Int32;对于 LargeString,输出类型为 Int64。
- (11) 每个 UTF8 编码的代码单元都以相反的顺序写入输出。如果输入不是有效的 UTF8,则输出未定义(但输出缓冲区的大小将保留)。
字符串填充
这些函数附加/添加给定的填充字节 (ASCII) 或代码点 (UTF8),以便将字符串居中 (center)、右对齐 (lpad) 或左对齐 (rpad)。
| Function name | Arity | Input types | Output type | Options class |
|---|---|---|---|---|
| ascii_center | Unary | String-like | String-like | :struct:PadOptions |
| ascii_lpad | Unary | String-like | String-like | :struct:PadOptions |
| ascii_rpad | Unary | String-like | String-like | :struct:PadOptions |
| utf8_center | Unary | String-like | String-like | :struct:PadOptions |
| utf8_lpad | Unary | String-like | String-like | :struct:PadOptions |
| utf8_rpad | Unary | String-like | String-like | :struct:PadOptions |
字符串修剪
这些函数会修剪两侧 (trim) 或左侧 (ltrim) 或右侧 (rtrim) 的字符。
| Function name | Arity | Input types | Output type | Options class | Notes |
|---|---|---|---|---|---|
| ascii_ltrim | Unary | String-like | String-like | :struct:TrimOptions | (1) |
| ascii_ltrim_whitespace | Unary | String-like | String-like | (2) | |
| ascii_rtrim | Unary | String-like | String-like | :struct:TrimOptions | (1) |
| ascii_rtrim_whitespace | Unary | String-like | String-like | (2) | |
| ascii_trim | Unary | String-like | String-like | :struct:TrimOptions | (1) |
| ascii_trim_whitespace | Unary | String-like | String-like | (2) | |
| utf8_ltrim | Unary | String-like | String-like | :struct:TrimOptions | (3) |
| utf8_ltrim_whitespace | Unary | String-like | String-like | (4) | |
| utf8_rtrim | Unary | String-like | String-like | :struct:TrimOptions | (3) |
| utf8_rtrim_whitespace | Unary | String-like | String-like | (4) | |
| utf8_trim | Unary | String-like | String-like | :struct:TrimOptions | (3) |
| utf8_trim_whitespace | Unary | String-like | String-like | (4) |
- (1) 仅会修剪掉
TrimOptions::characters中指定的字符。输入字符串和characters参数均被解释为 ASCII 字符。 - (2) 仅会修剪掉 ASCII 空格字符(“\t”、“\n”、“\v”、“\f”、“\r”和“ ”)。
- (3) 仅会修剪掉
TrimOptions::characters中指定的字符。 - (4) 仅会修剪掉 Unicode 空格字符。
字符串分割
这些函数将字符串拆分为字符串列表。所有内核都可以选择配置 max_splits 和 reverse 参数,其中 max_splits == -1 表示无限制(默认值)。当 reverse 为真时,拆分从字符串末尾开始;这仅在给定正 max_splits 时才有意义。
| Function name | Arity | Input types | Output type | Options class | Notes |
|---|---|---|---|---|---|
| ascii_split_whitespace | Unary | String-like | List-like | :struct:SplitOptions | (1) |
| split_pattern | Unary | Binary- or String-like | List-like | :struct:SplitPatternOptions | (2) |
| split_pattern_regex | Unary | Binary- or String-like | List-like | :struct:SplitPatternOptions | (3) |
| utf8_split_whitespace | Unary | String-like | List-like | :struct:SplitOptions | (4) |
- (1) 长度非零的 ASCII 定义空白字节序列
(
'\t'、'\n'、'\v'、'\f'、'\r'和' ') 被视为分隔符。 - (2) 当找到精确模式时,字符串会被拆分(模式 本身不包含在输出中)。
- (3) 当找到正则表达式匹配时,字符串会被拆分(匹配的 子字符串本身不包含在输出中)。
- (4) 长度非零的 Unicode 定义空白代码点序列 被视为分隔符。