Skip to main content

vortex_functions/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Scalar functions built on the vortex-array scalar function framework that the core array
5//! crate does not itself depend on.
6//!
7//! The scalar functions that canonical arrays, validity, the optimizer and expression analysis
8//! rely on (cast, fill_null, mask, binary operators, get_item, pack, literal, ...) live in
9//! `vortex_array::scalar_fn::builtin`. This crate holds struct projection and merging ([`select`],
10//! [`merge`]), null checks ([`is_null`]), conditionals
11//! ([`case_when`], [`zip`]), list functions ([`list_length`], [`list_sum`], [`list_transform`]),
12//! [`byte_length`], [`ext_storage`] and [`replace_time_zone`], string functions ([`lower`],
13//! [`strpos`]), string and range predicates ([`like`], [`between`], [`list_contains`],
14//! [`is_nan`]) and the scan-time [`dynamic`] and
15//! [`dynamic_expression`] functions, whose value changes during a scan, along with their
16//! expression constructors in [`expr`] and their statistics-based [`pruning`] rewrites.
17//!
18//! Call [`initialize`] to register them with a session.
19
20use vortex_array::scalar_fn::ScalarFnId;
21use vortex_array::scalar_fn::ScalarFnVTable;
22use vortex_array::scalar_fn::builtin::get_item::GetItem;
23use vortex_array::scalar_fn::builtin::literal::Literal;
24use vortex_array::scalar_fn::session::ScalarFnSessionExt;
25use vortex_session::VortexSession;
26
27pub mod between;
28pub mod builtins;
29pub mod byte_length;
30pub mod case_when;
31pub mod change_time_unit;
32pub mod convert_time_zone;
33pub mod dynamic;
34pub mod dynamic_expression;
35pub mod expr;
36pub mod ext_storage;
37mod field;
38mod fill_null;
39pub mod is_nan;
40pub mod is_null;
41pub mod like;
42pub mod list_contains;
43pub mod list_length;
44pub mod list_sum;
45pub mod list_transform;
46pub mod lower;
47pub mod lower_bound;
48pub mod merge;
49pub mod pruning;
50pub mod replace_time_zone;
51pub mod select;
52pub mod strpos;
53pub mod timestamp_to_date;
54pub mod timestamp_to_time;
55pub mod variant_get;
56pub mod zip;
57
58#[cfg(test)]
59mod tests;
60
61/// Register this crate's scalar functions with the given session.
62pub fn initialize(session: &VortexSession) {
63    between::initialize(session);
64    fill_null::initialize(session);
65    like::initialize(session);
66    list_contains::initialize(session);
67    variant_get::initialize(session);
68    zip::initialize(session);
69    pruning::initialize(session);
70    let scalar_fns = session.scalar_fns();
71    scalar_fns.register(byte_length::ByteLength);
72    scalar_fns.register(case_when::CaseWhen);
73    scalar_fns.register(change_time_unit::ChangeTimeUnit);
74    scalar_fns.register(convert_time_zone::ConvertTimeZone);
75    scalar_fns.register(ext_storage::ExtStorage);
76    scalar_fns.register(is_nan::IsNan);
77    scalar_fns.register(is_null::IsNull);
78    scalar_fns.register(list_length::ListLength);
79    scalar_fns.register(list_sum::ListSum);
80    scalar_fns.register(lower::Lower);
81    scalar_fns.register(merge::Merge);
82    scalar_fns.register(replace_time_zone::ReplaceTimeZone);
83    scalar_fns.register(select::Select);
84    scalar_fns.register(strpos::Strpos);
85    scalar_fns.register(timestamp_to_date::TimestampToDate);
86    scalar_fns.register(timestamp_to_time::TimestampToTime);
87}
88
89/// A scalar function has a negative cost if applying it to an array and
90/// canonicalizing is cheaper than canonicalizing an array and applying it.
91///
92/// Example of negative cost expressions are byte_length(), ext_storage(), and get_item() since
93/// they don't depend on input size.
94///
95/// Example of non-negative cost expression is like() as it's linear over
96/// individual input.
97pub fn is_negative_cost(id: ScalarFnId) -> bool {
98    id == ScalarFnVTable::id(&byte_length::ByteLength)
99        || id == ScalarFnVTable::id(&ext_storage::ExtStorage)
100        || id == ScalarFnVTable::id(&GetItem)
101        || id == ScalarFnVTable::id(&Literal)
102}