Skip to main content

chronix_query/
lib.rs

1//! # chronix-query
2//!
3//! Query engine for the Chronix time-series database.
4//!
5//! This crate implements the full query pipeline:
6//!
7//! 1. **Planning** — [`QueryPlan`] and [`QueryBuilder`] for fluent query construction
8//! 2. **Pruning** — Multi-level segment elimination (time, bloom, stats)
9//! 3. **Projection** — Read only the columns needed by the query
10//! 4. **Filtering** — Vectorized predicate evaluation on Arrow arrays
11//! 5. **Deduplication** — Sort-merge dedup for overlapping segments
12//! 6. **Aggregation** — count, sum, min, max, avg, first, last
13//! 7. **Downsampling** — Time-bucket aggregation
14//! 8. **Window functions** — row_number, rank, lead, lag, delta, rate, irate,
15//!    moving average, cumulative sum, increase
16//!
17//! All results are returned as Apache Arrow `RecordBatch` for zero-copy
18//! interoperability with data processing ecosystems.
19
20#![warn(missing_docs)]
21#![deny(unsafe_code)]
22#![allow(clippy::module_name_repetitions)]
23
24pub mod aggregate;
25pub mod convert;
26pub mod dedup;
27pub mod downsample;
28pub mod encoded_pushdown;
29pub mod error;
30pub mod filter;
31pub mod memory;
32pub mod plan;
33pub mod pruning;
34pub mod window;
35
36pub use aggregate::AggFn;
37pub use aggregate::{AggResult, DecimalTrack, Num};
38pub use convert::points_to_record_batch;
39pub use encoded_pushdown::{ColumnPredicate, EncodedDomainEvaluator, RowGroupVerdict};
40pub use error::QueryError;
41pub use memory::MemoryTracker;
42pub use plan::{FieldPredicate, QueryBuilder, QueryPlan, ZoneMapOp};
43pub use window::WindowFn;
44
45/// The Arrow type a schema column decodes to — one answer, in one place.
46///
47/// It was four copies of this match (the scan schema, the SQL table
48/// provider, the server's schema endpoint, the segment reader), each ending
49/// in a catch-all that mapped anything it did not name to a string. That is
50/// harmless while every unnamed type *is* a string, and turns a decimal
51/// column into `Utf8` the moment one is not.
52///
53/// `Timestamp` maps to `Int64`, the storage representation. The SQL layer
54/// presents the time column as `Timestamp(Nanosecond)` and converts on the
55/// way out; see `chronix::sql`.
56#[must_use]
57pub fn column_type_to_arrow(ct: chronix_core::ColumnType) -> arrow::datatypes::DataType {
58    use arrow::datatypes::DataType;
59    use chronix_core::ColumnType;
60    match ct {
61        ColumnType::Timestamp | ColumnType::I64 => DataType::Int64,
62        ColumnType::U64 => DataType::UInt64,
63        ColumnType::F64 => DataType::Float64,
64        ColumnType::Bool => DataType::Boolean,
65        ColumnType::String => DataType::Utf8,
66        ColumnType::Decimal { scale } => DataType::Decimal128(
67            chronix_core::DECIMAL_PRECISION,
68            i8::try_from(scale).unwrap_or(0),
69        ),
70    }
71}
72
73/// The inverse of [`column_type_to_arrow`], for one cell.
74///
75/// `Ok(None)` means the cell is null. `Err` means the column's Arrow type is
76/// not one the storage layer can hold — which is a *schema* mistake by the
77/// caller, not a value, and must not be mistaken for "no data".
78///
79/// Callers *store* what this produces — a distributed read, and a Raft region
80/// snapshot — so it is one total conversion rather than a `match` at each
81/// site. A quiet arm there drops a field from a point with nothing to say so.
82///
83/// # Errors
84///
85/// Returns [`QueryError::Validation`] when the Arrow type has no
86/// [`ColumnType`](chronix_core::ColumnType), or when a decimal carries a
87/// negative scale — legal in Arrow, unrepresentable in storage.
88pub fn arrow_cell_to_field_value(
89    col: &dyn arrow::array::Array,
90    index: usize,
91) -> error::Result<Option<chronix_core::FieldValue>> {
92    use arrow::array::{
93        BooleanArray, Decimal128Array, Float64Array, Int64Array, StringArray, UInt64Array,
94    };
95    use arrow::datatypes::DataType;
96    use chronix_core::FieldValue;
97
98    if col.is_null(index) {
99        return Ok(None);
100    }
101
102    let bad = |what: &str| {
103        QueryError::Validation(format!(
104            "column has {what}, which no Chronix column type can hold"
105        ))
106    };
107    let mismatch = || bad("an array that disagrees with its own Arrow type");
108
109    // The six storage types, and nothing else. `Timestamp` shares `Int64`
110    // with `I64`; a timestamp *field* is an `I64` value here, which is what
111    // the schema layer stores it as.
112    let v = match col.data_type() {
113        DataType::Float64 => FieldValue::F64(
114            col.as_any()
115                .downcast_ref::<Float64Array>()
116                .ok_or_else(mismatch)?
117                .value(index),
118        ),
119        DataType::Int64 => FieldValue::I64(
120            col.as_any()
121                .downcast_ref::<Int64Array>()
122                .ok_or_else(mismatch)?
123                .value(index),
124        ),
125        DataType::UInt64 => FieldValue::U64(
126            col.as_any()
127                .downcast_ref::<UInt64Array>()
128                .ok_or_else(mismatch)?
129                .value(index),
130        ),
131        DataType::Boolean => FieldValue::Bool(
132            col.as_any()
133                .downcast_ref::<BooleanArray>()
134                .ok_or_else(mismatch)?
135                .value(index),
136        ),
137        DataType::Utf8 => FieldValue::String(
138            col.as_any()
139                .downcast_ref::<StringArray>()
140                .ok_or_else(mismatch)?
141                .value(index)
142                .to_owned(),
143        ),
144        DataType::Decimal128(_, scale) => {
145            let arr = col
146                .as_any()
147                .downcast_ref::<Decimal128Array>()
148                .ok_or_else(mismatch)?;
149            let scale = u8::try_from(*scale).map_err(|_| bad("a decimal with a negative scale"))?;
150            FieldValue::Decimal(
151                chronix_core::Decimal::new(arr.value(index), scale)
152                    .map_err(|e| QueryError::Validation(format!("decimal cell: {e}")))?,
153            )
154        }
155        other => return Err(bad(&format!("Arrow type {other}"))),
156    };
157    Ok(Some(v))
158}
159
160/// Consolidated Arrow column → `f64` extraction.
161///
162/// Supports `Float64Array`, `Int64Array`, `UInt64Array` and
163/// `Decimal128Array`. Returns `None` for null values or unsupported types.
164///
165/// # The one place a decimal becomes approximate
166///
167/// Storing or aggregating a `Decimal128` column keeps it exact: the segment
168/// holds the mantissa, `sum`/`min`/`max`/`first`/`last` fold in `i128`, and
169/// SQL sees `Decimal128(38, s)`.
170///
171/// What comes through here is the other kind of question — a moving average,
172/// a percentile, a forecast, an anomaly score, a chart's downsample, a
173/// PromQL sample. Those are defined in floating point and their answers are
174/// estimates whatever the input was, so converting is honest. Keeping it in
175/// one named function leaves one boundary to audit rather than an `as f64`
176/// in every module.
177#[allow(clippy::cast_precision_loss)]
178pub fn extract_f64(col: &dyn arrow::array::Array, index: usize) -> Option<f64> {
179    use arrow::array::{Decimal128Array, Float64Array, Int64Array, UInt64Array};
180    if col.is_null(index) {
181        return None;
182    }
183    col.as_any()
184        .downcast_ref::<Float64Array>()
185        .map(|a| a.value(index))
186        .or_else(|| {
187            col.as_any()
188                .downcast_ref::<Int64Array>()
189                .map(|a| a.value(index) as f64)
190        })
191        .or_else(|| {
192            col.as_any()
193                .downcast_ref::<UInt64Array>()
194                .map(|a| a.value(index) as f64)
195        })
196        .or_else(|| {
197            col.as_any().downcast_ref::<Decimal128Array>().map(|a| {
198                let scale = u32::try_from(a.scale()).unwrap_or(0);
199                a.value(index) as f64 / chronix_core::pow10(scale).unwrap_or(1) as f64
200            })
201        })
202}
203
204#[cfg(test)]
205#[allow(clippy::unwrap_used, clippy::expect_used)]
206mod cell_tests {
207    use super::{arrow_cell_to_field_value, column_type_to_arrow};
208    use arrow::array::{
209        BooleanArray, Decimal128Array, Float64Array, Int64Array, StringArray, UInt64Array,
210    };
211    use chronix_core::{ColumnType, FieldValue};
212
213    #[test]
214    fn every_storage_type_survives_the_round_trip() {
215        // The property that matters: whatever `column_type_to_arrow` can
216        // produce, this can read back. A type missing here is a field that
217        // disappears from a replicated region.
218        let cases: Vec<(ColumnType, arrow::array::ArrayRef, FieldValue)> = vec![
219            (
220                ColumnType::F64,
221                std::sync::Arc::new(Float64Array::from(vec![1.5])),
222                FieldValue::F64(1.5),
223            ),
224            (
225                ColumnType::I64,
226                std::sync::Arc::new(Int64Array::from(vec![i64::MIN])),
227                FieldValue::I64(i64::MIN),
228            ),
229            (
230                ColumnType::U64,
231                std::sync::Arc::new(UInt64Array::from(vec![u64::MAX])),
232                FieldValue::U64(u64::MAX),
233            ),
234            (
235                ColumnType::Bool,
236                std::sync::Arc::new(BooleanArray::from(vec![true])),
237                FieldValue::Bool(true),
238            ),
239            (
240                ColumnType::String,
241                std::sync::Arc::new(StringArray::from(vec!["s"])),
242                FieldValue::String("s".to_string()),
243            ),
244            (
245                ColumnType::Decimal { scale: 4 },
246                std::sync::Arc::new(
247                    Decimal128Array::from(vec![12_345_i128])
248                        .with_precision_and_scale(chronix_core::DECIMAL_PRECISION, 4)
249                        .unwrap(),
250                ),
251                FieldValue::Decimal(chronix_core::Decimal::new(12_345, 4).unwrap()),
252            ),
253        ];
254
255        for (ct, arr, want) in cases {
256            assert_eq!(
257                arr.data_type(),
258                &column_type_to_arrow(ct),
259                "test array does not match {ct:?}"
260            );
261            let got = arrow_cell_to_field_value(arr.as_ref(), 0).unwrap();
262            assert_eq!(got, Some(want), "round trip failed for {ct:?}");
263        }
264    }
265
266    #[test]
267    fn a_null_cell_is_absent_not_an_error() {
268        let arr = Float64Array::from(vec![None::<f64>]);
269        assert_eq!(arrow_cell_to_field_value(&arr, 0).unwrap(), None);
270    }
271
272    #[test]
273    fn a_type_storage_cannot_hold_is_an_error_not_a_silent_drop() {
274        // The defect this exists for: `_ => None` read as "no value here"
275        // for a column that in fact held one.
276        let arr = arrow::array::Int32Array::from(vec![1]);
277        let err = arrow_cell_to_field_value(&arr, 0).unwrap_err();
278        assert!(
279            err.to_string().contains("Int32"),
280            "the error should name the type: {err}"
281        );
282    }
283
284    #[test]
285    fn a_negative_scale_is_an_error_not_a_dropped_column() {
286        // Legal in Arrow, unrepresentable in storage. It used to be read
287        // into a `u8`, fail, and take the whole cell with it.
288        let arr = Decimal128Array::from(vec![123_i128])
289            .with_precision_and_scale(10, -2)
290            .unwrap();
291        let err = arrow_cell_to_field_value(&arr, 0).unwrap_err();
292        assert!(
293            err.to_string().contains("negative scale"),
294            "the error should say why: {err}"
295        );
296    }
297}