import { SqlEvaluationError } from './errors.js'; /** * Builtin functions callable from expressions, keyed by uppercase name. * Extend/override at call time via `evaluate(ast, ctx, { functions })`. * @type {Record any>} */ export const builtins = { LOWER: (v) => (v === null || v === undefined ? null : String(v).toLowerCase()), UPPER: (v) => (v === null || v === undefined ? null : String(v).toUpperCase()), LENGTH: (v) => { if (v === null || v === undefined) return null; if (typeof v === 'string' || Array.isArray(v)) return v.length; throw new SqlEvaluationError('LENGTH requires a string or array'); }, ABS: (v) => { if (v === null || v === undefined) return null; if (typeof v !== 'number') throw new SqlEvaluationError('ABS requires a number'); return Math.abs(v); }, COALESCE: (...args) => { for (const a of args) { if (a !== null && a !== undefined) return a; } return null; }, };