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 dynamic;
32pub mod dynamic_expression;
33pub mod expr;
34pub mod ext_storage;
35mod field;
36mod fill_null;
37pub mod is_nan;
38pub mod is_null;
39pub mod like;
40pub mod list_contains;
41pub mod list_length;
42pub mod list_sum;
43pub mod list_transform;
44pub mod lower;
45pub mod lower_bound;
46pub mod merge;
47pub mod pruning;
48pub mod replace_time_zone;
49pub mod select;
50pub mod strpos;
51pub mod variant_get;
52pub mod zip;
53
54#[cfg(test)]
55mod tests;
56
57/// Register this crate's scalar functions with the given session.
58pub fn initialize(session: &VortexSession) {
59    between::initialize(session);
60    fill_null::initialize(session);
61    like::initialize(session);
62    list_contains::initialize(session);
63    variant_get::initialize(session);
64    zip::initialize(session);
65    pruning::initialize(session);
66    let scalar_fns = session.scalar_fns();
67    scalar_fns.register(byte_length::ByteLength);
68    scalar_fns.register(case_when::CaseWhen);
69    scalar_fns.register(ext_storage::ExtStorage);
70    scalar_fns.register(is_nan::IsNan);
71    scalar_fns.register(is_null::IsNull);
72    scalar_fns.register(list_length::ListLength);
73    scalar_fns.register(list_sum::ListSum);
74    scalar_fns.register(lower::Lower);
75    scalar_fns.register(merge::Merge);
76    scalar_fns.register(replace_time_zone::ReplaceTimeZone);
77    scalar_fns.register(select::Select);
78    scalar_fns.register(strpos::Strpos);
79}
80
81/// A scalar function has a negative cost if applying it to an array and
82/// canonicalizing is cheaper than canonicalizing an array and applying it.
83///
84/// Example of negative cost expressions are byte_length(), ext_storage(), and get_item() since
85/// they don't depend on input size.
86///
87/// Example of non-negative cost expression is like() as it's linear over
88/// individual input.
89pub fn is_negative_cost(id: ScalarFnId) -> bool {
90    id == ScalarFnVTable::id(&byte_length::ByteLength)
91        || id == ScalarFnVTable::id(&ext_storage::ExtStorage)
92        || id == ScalarFnVTable::id(&GetItem)
93        || id == ScalarFnVTable::id(&Literal)
94}