{"record":{"id":"736af2a5aac30837","repo":"risingwavelabs/risingwave","slug":"failed-to-fold-constant","errorCode":null,"errorMessage":"failed to fold constant","messagePattern":"failed to fold constant","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/frontend/src/optimizer/rule/table_function_to_internal_get_channel_delta_stats.rs","lineNumber":55,"sourceCode":"        Some(Ok(value)) => {\n            let Some(scalar) = value else {\n                return Ok(None);\n            };\n            match scalar {\n                ScalarImpl::Int64(value) => {\n                    if value < 0 {\n                        Err(anyhow::anyhow!(\n                            \"time parameter cannot be negative, got {}\",\n                            value\n                        ))\n                    } else {\n                        Ok(Some(value as u64))\n                    }\n                }\n                _ => Err(anyhow::anyhow!(\"expected int64, got {:?}\", scalar)),\n            }\n        }\n        Some(Err(err)) => Err(anyhow!(err).context(\"failed to fold constant\")),\n        None => Err(anyhow::anyhow!(\"expression must be a constant value\")),\n    }\n}\n\n/// Transform the `internal_get_channel_delta_stats()` table function\n/// into a plan graph which will return channel statistics from the dashboard API.\n/// It will return channel stats with `upstream_fragment_id` and `downstream_fragment_id` as primary key.\npub struct TableFunctionToInternalGetChannelDeltaStatsRule {}\nimpl FallibleRule<Logical> for TableFunctionToInternalGetChannelDeltaStatsRule {\n    fn apply(&self, plan: PlanRef) -> ApplyResult<PlanRef> {\n        let logical_table_function: &LogicalTableFunction = plan.as_logical_table_function()?;\n        if logical_table_function.table_function().function_type\n            != TableFunctionType::InternalGetChannelDeltaStats\n        {\n            return ApplyResult::NotApplicable;\n        }\n        let plan = Self::build_plan(plan.ctx(), logical_table_function.table_function())?;\n        ApplyResult::Ok(plan)","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/risingwavelabs/risingwave/blob/6469eb736d691e8e9b8a419a57edd6429ca77417/src/frontend/src/optimizer/rule/table_function_to_internal_get_channel_delta_stats.rs#L37-L73","documentation":"Arguments to `internal_get_channel_delta_stats` must be constants so they can be evaluated at plan time. If constant folding of the argument expression itself returns an error, that error is wrapped with the context `failed to fold constant`.","triggerScenarios":"`build_plan` calls `expr_impl_to_u64_fn` on an argument whose fold produces `Some(Err(err))` — e.g. the expression contains a volatile function, division by zero during folding, or a fold-time type error.","commonSituations":"Passing expressions that reference columns or volatile functions (`now()`), or arithmetic that fails during evaluation (overflow, division by zero).","solutions":["Replace the argument with a plain constant literal.","Remove volatile or non-deterministic functions from the argument expression.","Fix the inner fold error shown by the anyhow cause chain (e.g. overflow or division by zero).","Precompute the value client-side and inline it as a literal."],"exampleFix":"-- before\nSELECT * FROM internal_get_channel_delta_stats(now()::bigint, 0);\n-- after\nSELECT * FROM internal_get_channel_delta_stats(1726000000, 0);","handlingStrategy":"validation","validationCode":"-- Only pass literals, not expressions\nSELECT * FROM internal_get_channel_delta_stats(1726000000, 0);","typeGuard":null,"tryCatchPattern":"match expr_impl_to_u64_fn(expr) {\n    Ok(v) => /* proceed */,\n    Err(e) => eprintln!(\"arg fold failed: {e:#}\"), // inspect cause chain\n}","preventionTips":["Use plain numeric literals for arguments","Avoid volatile functions like now() in arguments","Avoid arithmetic that can overflow/divide-by-zero at fold time"],"tags":["rust","optimizer","constant-folding"],"backgroundTag":"invalid-argument-value","analyzedSha":"6469eb736d691e8e9b8a419a57edd6429ca77417","analyzedAt":"2026-09-11T21:06:21.487Z","contentChangedAt":"2026-09-11T21:06:21.487Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}