{"record":{"id":"cde93798d99f0ea0","repo":"databendlabs/databend","slug":"unsupported-unit","errorCode":null,"errorMessage":"Unsupported unit: {}","messagePattern":"Unsupported unit: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/query/functions/src/scalars/timestamp/src/date_arithmetic.rs","lineNumber":616,"sourceCode":"        let date_start = date_start / (MICROS_PER_SEC * factor);\n        let date_end = date_end / (MICROS_PER_SEC * factor);\n        date_end - date_start\n    }\n\n    fn eval_timestamp_between(unit: &str, start: i64, end: i64) -> i64 {\n        if start == end {\n            return 0;\n        }\n        if start > end {\n            return -Self::eval_timestamp_between(unit, end, start);\n        }\n\n        let micros = end - start;\n        match unit {\n            \"hours\" => micros / (3600 * MICROS_PER_SEC),\n            \"minutes\" => micros / (60 * MICROS_PER_SEC),\n            \"seconds\" => micros / MICROS_PER_SEC,\n            _ => unreachable!(\"Unsupported unit: {}\", unit),\n        }\n    }\n}\n\npub(super) fn register(registry: &mut FunctionRegistry) {\n    register_add_functions(registry);\n    register_sub_functions(registry);\n    register_diff_functions(registry);\n    register_between_functions(registry);\n}\n\nfn scale_delta(delta: i64, multiplier: i64) -> Result<i64, String> {\n    delta\n        .checked_mul(multiplier)\n        .ok_or_else(|| \"Invalid date: interval arithmetic overflow\".to_string())\n}\n\nfn push_result<T: Default>(result: Result<T, String>, output: &mut Vec<T>, ctx: &mut EvalContext) {","sourceCodeStart":598,"sourceCodeEnd":634,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/query/functions/src/scalars/timestamp/src/date_arithmetic.rs#L598-L634","documentation":"In eval_timestamp_between (src/query/functions/src/scalars/timestamp/src/date_arithmetic.rs:616), the computed microsecond difference is divided by a unit-specific divisor for exactly three accepted units: \"hours\", \"minutes\", \"seconds\". Any other unit string reaches `unreachable!(\"Unsupported unit: {}\", unit)`. The invariant is that only these three unit names ever flow into this helper.","triggerScenarios":"Invoking the timestamp-between evaluation path with a unit string other than \"hours\", \"minutes\", or \"seconds\" — e.g. extending the timestamp_diff family with new units (days, weeks) and routing them into this helper, or binding a user-supplied unit argument directly.","commonSituations":"A developer adds \"days\" or \"milliseconds\" support to date arithmetic and wires it into this helper without adding a match arm; a caller passes a user-provided TIMESTAMPDIFF-style unit straight through instead of converting.","solutions":["Add a match arm for the unit you need (e.g. \"days\" => micros / (24 * 3600 * MICROS_PER_SEC)).","Ensure only units this function supports are routed here; map other units to a different implementation upstream.","Replace unreachable!() with a returned error/ErrorCode so unsupported units surface as a clean query error instead of a panic.","Add a unit test per supported unit string."],"exampleFix":"// before\n_ => unreachable!(\"Unsupported unit: {}\", unit),\n// after\n\"days\" => micros / (24 * 3600 * MICROS_PER_SEC),\nother => return Err(ErrorCode::BadArguments(format!(\"Unsupported unit: {}\", other))),","handlingStrategy":"validation","validationCode":"fn validate_date_unit(unit: &str) -> Result<(), String> {\n    match unit {\n        \"hours\" | \"minutes\" | \"seconds\" => Ok(()),\n        other => Err(format!(\"unsupported unit: {}\", other)),\n    }\n}","typeGuard":null,"tryCatchPattern":"// Normalize/whitelist user-supplied units before date arithmetic:\nlet unit = normalize_unit(user_unit)?; // maps DAY->hours-level handling elsewhere\nif !matches!(unit, \"hours\" | \"minutes\" | \"seconds\") {\n    return Err(ErrorCode::BadArguments(format!(\"Unsupported unit: {}\", unit)));\n}","preventionTips":["Whitelist unit strings at the SQL layer before date arithmetic.","When adding TIMESTAMPDIFF-style units, route only supported units into this helper.","Add tests for every unit string the function registry advertises."],"tags":["timestamps","date-arithmetic","rust","panic","internal-invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"288d84d76e20a2f8f7173bda9691eb6ece301aa9","analyzedAt":"2026-09-11T11:29:36.208Z","contentChangedAt":"2026-09-11T11:29:36.208Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}