rustledger-query 0.16.4

Beancount query engine (BQL) with SQL-like syntax for ledger queries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Utility function implementations for the BQL executor.
//!
//! This module includes metadata, conversion, casting, and helper functions.

use rust_decimal::Decimal;
use rustledger_core::{Amount, Inventory, MetaValue, Position};

use crate::ast::FunctionCall;
use crate::error::QueryError;

use super::super::Executor;
use super::super::types::{PostingContext, Value};

impl Executor<'_> {
    /// Evaluate metadata functions: `META`, `ENTRY_META`, `ANY_META`.
    ///
    /// - `META(key)` - Get metadata value from the posting
    /// - `ENTRY_META(key)` - Get metadata value from the transaction
    /// - `ANY_META(key)` - Get metadata value from posting, falling back to transaction
    pub(crate) fn eval_meta_function(
        &self,
        name: &str,
        func: &FunctionCall,
        ctx: &PostingContext,
    ) -> Result<Value, QueryError> {
        Self::require_args(name, func, 1)?;

        let key = match self.evaluate_expr(&func.args[0], ctx)? {
            Value::String(s) => s,
            _ => {
                return Err(QueryError::Type(format!(
                    "{name}: argument must be a string key"
                )));
            }
        };

        let posting = &ctx.transaction.postings[ctx.posting_index];

        let meta_value = match name {
            "META" | "POSTING_META" => posting.meta.get(&key),
            "ENTRY_META" => ctx.transaction.meta.get(&key),
            "ANY_META" => posting
                .meta
                .get(&key)
                .or_else(|| ctx.transaction.meta.get(&key)),
            _ => unreachable!(),
        };

        Ok(Self::meta_value_to_value(meta_value))
    }

    /// Convert a `MetaValue` to a `Value`.
    pub(crate) fn meta_value_to_value(mv: Option<&MetaValue>) -> Value {
        match mv {
            None => Value::Null,
            Some(MetaValue::String(s)) => Value::String(s.clone()),
            Some(MetaValue::Number(n)) => Value::Number(*n),
            Some(MetaValue::Date(d)) => Value::Date(*d),
            Some(MetaValue::Bool(b)) => Value::Boolean(*b),
            Some(MetaValue::Amount(a)) => Value::Amount(a.clone()),
            // Lower typed meta values to BQL String at the query boundary
            // (matches bean-query semantics — no first-class Account/Currency
            // type in the SQL surface).
            Some(MetaValue::Account(a)) => Value::String(a.to_string()),
            Some(MetaValue::Currency(c)) => Value::String(c.to_string()),
            Some(MetaValue::Tag(t)) => Value::String(t.to_string()),
            Some(MetaValue::Link(l)) => Value::String(l.to_string()),
            Some(MetaValue::None) => Value::Null,
        }
    }

    /// Evaluate CONVERT function (currency conversion).
    ///
    /// `CONVERT(position, currency)` - Convert position/amount to target currency.
    /// `CONVERT(position, currency, date)` - Convert using price at specific date.
    pub(crate) fn eval_convert(
        &self,
        func: &FunctionCall,
        ctx: &PostingContext,
    ) -> Result<Value, QueryError> {
        if func.args.len() < 2 || func.args.len() > 3 {
            return Err(QueryError::InvalidArguments(
                "CONVERT".to_string(),
                "expected 2 or 3 arguments: (value, currency[, date])".to_string(),
            ));
        }

        let val = self.evaluate_expr(&func.args[0], ctx)?;

        let target_currency = match self.evaluate_expr(&func.args[1], ctx)? {
            Value::String(s) => s,
            Value::Null => {
                return Err(QueryError::Type(
                    concat!(
                        "CONVERT: second argument evaluated to NULL; ",
                        "expected a currency string ",
                        "(this often means an aggregate expression couldn't ",
                        "evaluate against an empty group — see issue #902)",
                    )
                    .to_string(),
                ));
            }
            _ => {
                return Err(QueryError::Type(
                    "CONVERT: second argument must be a currency string".to_string(),
                ));
            }
        };

        // When no date is specified, use the latest price (matches Python beancount behavior)
        let date: Option<rustledger_core::NaiveDate> = if func.args.len() == 3 {
            match self.evaluate_expr(&func.args[2], ctx)? {
                Value::Date(d) => Some(d),
                _ => {
                    return Err(QueryError::Type(
                        "CONVERT: third argument must be a date".to_string(),
                    ));
                }
            }
        } else {
            None // Use latest price when no date specified
        };

        // Helper to convert an amount, using latest price if no date specified
        let convert_amount = |amt: &Amount| -> Option<Amount> {
            if let Some(d) = date {
                self.price_db.convert(amt, &target_currency, d)
            } else {
                self.price_db.convert_latest(amt, &target_currency)
            }
        };

        match val {
            Value::Position(p) => {
                if p.units.currency == target_currency {
                    Ok(Value::Amount(p.units))
                } else if let Some(converted) = convert_amount(&p.units) {
                    Ok(Value::Amount(converted))
                } else {
                    // Return original units if no conversion available
                    Ok(Value::Amount(p.units))
                }
            }
            Value::Amount(a) => {
                if a.currency == target_currency {
                    Ok(Value::Amount(a))
                } else if let Some(converted) = convert_amount(&a) {
                    Ok(Value::Amount(converted))
                } else {
                    Ok(Value::Amount(a))
                }
            }
            Value::Inventory(inv) => {
                // Convert each position, keeping originals when no conversion available
                // (matches Python beancount behavior)
                let mut result = Inventory::default();
                for pos in inv.positions() {
                    if pos.units.currency == target_currency {
                        result.add(Position::simple(pos.units.clone()));
                    } else if let Some(converted) = convert_amount(&pos.units) {
                        result.add(Position::simple(converted));
                    } else {
                        // No conversion available - keep original (Python beancount behavior)
                        result.add(Position::simple(pos.units.clone()));
                    }
                }
                // If result has single currency matching target, return as Amount.
                // If result is empty, return zero in target currency (issue #586).
                // Peek the first two positions to decide; the single-match Amount
                // is cloned eagerly (O(1) — `Decimal` + interned currency) so the
                // iterator borrow is dropped before we move `result` below.
                let single_match: Option<Amount> = {
                    let mut iter = result.positions();
                    match (iter.next(), iter.next()) {
                        (Some(only), None) if only.units.currency == target_currency => {
                            Some(only.units.clone())
                        }
                        _ => None,
                    }
                };
                if let Some(units) = single_match {
                    Ok(Value::Amount(units))
                } else if result.is_empty() {
                    Ok(Value::Amount(Amount::new(Decimal::ZERO, &target_currency)))
                } else {
                    Ok(Value::Inventory(Box::new(result)))
                }
            }
            Value::Number(n) => {
                // Just wrap the number as an amount with the target
                // currency. Note the asymmetry with the `Value::String`
                // arm below: `CONVERT(100, 'EUR')` returns `100 EUR`
                // (no conversion — the bare number has no source
                // currency), but `CONVERT('100 USD', 'EUR')` does an
                // actual conversion.
                Ok(Value::Amount(Amount::new(n, &target_currency)))
            }
            Value::String(s) => {
                // String input is a rustledger extension (issue #1179),
                // not present in Python beancount. Lets users write
                // ad-hoc currency conversions like
                // `SELECT CONVERT('100 USD', 'EUR')` without anchoring
                // them to a posting. Strict parser (see
                // `Amount::from_str`): malformed input surfaces as a
                // typed `QueryError` rather than a silent zero or a
                // panic.
                let amt: Amount = s.parse().map_err(|e| {
                    QueryError::Type(format!("CONVERT: first argument {e} (e.g. \"100 USD\")"))
                })?;
                if amt.currency == target_currency {
                    Ok(Value::Amount(amt))
                } else if let Some(converted) = convert_amount(&amt) {
                    Ok(Value::Amount(converted))
                } else {
                    // Match the `Value::Amount` arm: no price available
                    // → return original unchanged. Switching to an
                    // error here would diverge from
                    // `CONVERT(<amount>, ...)` for the same
                    // "no rate found" case.
                    Ok(Value::Amount(amt))
                }
            }
            Value::Null => {
                // For null values (e.g., empty sum), return zero in target currency
                // This matches Python beancount behavior for empty balances
                Ok(Value::Amount(Amount::new(Decimal::ZERO, &target_currency)))
            }
            _ => Err(QueryError::Type(
                "CONVERT expects a position, amount, inventory, number, or amount-string"
                    .to_string(),
            )),
        }
    }

    /// Evaluate INT function (convert to integer).
    pub(crate) fn eval_int(
        &self,
        func: &FunctionCall,
        ctx: &PostingContext,
    ) -> Result<Value, QueryError> {
        Self::require_args("INT", func, 1)?;
        let val = self.evaluate_expr(&func.args[0], ctx)?;
        Self::value_to_int(&val)
    }

    /// Evaluate DECIMAL function (convert to decimal).
    pub(crate) fn eval_decimal(
        &self,
        func: &FunctionCall,
        ctx: &PostingContext,
    ) -> Result<Value, QueryError> {
        Self::require_args("DECIMAL", func, 1)?;
        let val = self.evaluate_expr(&func.args[0], ctx)?;
        Self::value_to_decimal(&val)
    }

    /// Evaluate STR function (convert to string).
    pub(crate) fn eval_str(
        &self,
        func: &FunctionCall,
        ctx: &PostingContext,
    ) -> Result<Value, QueryError> {
        Self::require_args("STR", func, 1)?;
        let val = self.evaluate_expr(&func.args[0], ctx)?;
        Self::value_to_str(&val)
    }

    /// Evaluate BOOL function (convert to boolean).
    pub(crate) fn eval_bool(
        &self,
        func: &FunctionCall,
        ctx: &PostingContext,
    ) -> Result<Value, QueryError> {
        Self::require_args("BOOL", func, 1)?;
        let val = self.evaluate_expr(&func.args[0], ctx)?;
        Self::value_to_bool(&val)
    }

    // =========================================================================
    // Value conversion helpers (shared between eval_* and evaluate_function_on_values)
    // =========================================================================

    /// Convert a Value to string.
    pub(crate) fn value_to_str(val: &Value) -> Result<Value, QueryError> {
        match val {
            Value::String(s) => Ok(Value::String(s.clone())),
            Value::Integer(i) => Ok(Value::String(i.to_string())),
            Value::Number(n) => Ok(Value::String(n.to_string())),
            Value::Boolean(b) => Ok(Value::String(if *b { "TRUE" } else { "FALSE" }.to_string())),
            Value::Date(d) => Ok(Value::String(d.to_string())),
            Value::Amount(a) => Ok(Value::String(format!("{} {}", a.number, a.currency))),
            Value::Null => Ok(Value::Null),
            _ => Err(QueryError::Type(
                "STR expects a string, integer, number, boolean, date, or amount".to_string(),
            )),
        }
    }

    /// Convert a Value to integer.
    pub(crate) fn value_to_int(val: &Value) -> Result<Value, QueryError> {
        use rust_decimal::prelude::ToPrimitive;
        match val {
            Value::Integer(i) => Ok(Value::Integer(*i)),
            Value::Number(n) => {
                let truncated = n.trunc();
                truncated.to_i64().map(Value::Integer).ok_or_else(|| {
                    QueryError::Type(format!("INT: cannot convert '{n}' to integer"))
                })
            }
            Value::Boolean(b) => Ok(Value::Integer(i64::from(*b))),
            Value::String(s) => s
                .parse::<i64>()
                .map(Value::Integer)
                .map_err(|_| QueryError::Type(format!("INT: cannot parse '{s}' as integer"))),
            Value::Null => Ok(Value::Null),
            _ => Err(QueryError::Type(
                "INT expects a number, integer, boolean, or string".to_string(),
            )),
        }
    }

    /// Convert a Value to decimal.
    pub(crate) fn value_to_decimal(val: &Value) -> Result<Value, QueryError> {
        match val {
            Value::Number(n) => Ok(Value::Number(*n)),
            Value::Integer(i) => Ok(Value::Number(Decimal::from(*i))),
            Value::Boolean(b) => Ok(Value::Number(if *b { Decimal::ONE } else { Decimal::ZERO })),
            Value::String(s) => s
                .parse::<Decimal>()
                .map(Value::Number)
                .map_err(|_| QueryError::Type(format!("DECIMAL: cannot parse '{s}' as decimal"))),
            Value::Null => Ok(Value::Null),
            _ => Err(QueryError::Type(
                "DECIMAL expects a number, integer, boolean, or string".to_string(),
            )),
        }
    }

    /// Convert a Value to boolean.
    pub(crate) fn value_to_bool(val: &Value) -> Result<Value, QueryError> {
        match val {
            Value::Boolean(b) => Ok(Value::Boolean(*b)),
            Value::Integer(i) => Ok(Value::Boolean(*i != 0)),
            Value::Number(n) => Ok(Value::Boolean(!n.is_zero())),
            Value::String(s) => {
                let s_upper = s.to_uppercase();
                match s_upper.as_str() {
                    "TRUE" | "YES" | "1" | "T" | "Y" => Ok(Value::Boolean(true)),
                    "FALSE" | "NO" | "0" | "F" | "N" | "" => Ok(Value::Boolean(false)),
                    _ => Err(QueryError::Type(format!(
                        "BOOL: cannot parse '{s}' as boolean"
                    ))),
                }
            }
            Value::Null => Ok(Value::Null),
            _ => Err(QueryError::Type(
                "BOOL expects a boolean, number, integer, or string".to_string(),
            )),
        }
    }

    /// Evaluate COALESCE function.
    pub(crate) fn eval_coalesce(
        &self,
        func: &FunctionCall,
        ctx: &PostingContext,
    ) -> Result<Value, QueryError> {
        for arg in &func.args {
            let val = self.evaluate_expr(arg, ctx)?;
            if !matches!(val, Value::Null) {
                return Ok(val);
            }
        }
        Ok(Value::Null)
    }

    /// Evaluate ONLY function.
    ///
    /// `ONLY(key, inventory)` - Extract amount with given currency from inventory.
    /// Returns the amount if exactly one position matches, NULL otherwise.
    pub(crate) fn eval_only(
        &self,
        func: &FunctionCall,
        ctx: &PostingContext,
    ) -> Result<Value, QueryError> {
        Self::require_args("ONLY", func, 2)?;

        // Get the currency key
        let key = match self.evaluate_expr(&func.args[0], ctx)? {
            Value::String(s) => s,
            _ => {
                return Err(QueryError::Type(
                    "ONLY: first argument must be a currency string".to_string(),
                ));
            }
        };

        // Get the inventory
        let inv = match self.evaluate_expr(&func.args[1], ctx)? {
            Value::Inventory(inv) => inv,
            Value::Position(pos) => {
                // If it's a single position, check if it matches
                if pos.units.currency == key {
                    return Ok(Value::Amount(pos.units));
                }
                return Ok(Value::Null);
            }
            Value::Amount(amt) => {
                // If it's a single amount, check if it matches
                if amt.currency == key {
                    return Ok(Value::Amount(amt));
                }
                return Ok(Value::Null);
            }
            Value::Null => return Ok(Value::Null),
            _ => {
                return Err(QueryError::Type(
                    "ONLY: second argument must be an inventory, position, or amount".to_string(),
                ));
            }
        };

        // Find positions matching the currency
        let matching: Vec<_> = inv
            .positions()
            .filter(|p| p.units.currency == key)
            .collect();

        match matching.len() {
            0 => Ok(Value::Null),
            1 => Ok(Value::Amount(matching[0].units.clone())),
            _ => Ok(Value::Null), // Multiple matches, return NULL
        }
    }
}