{"record":{"id":"d1dc5d4703ab54dc","repo":"dbt-labs/dbt-core","slug":"group-by-with-function-key","errorCode":null,"errorMessage":"group_by with function key","messagePattern":"group_by with function key","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/dbt-agate/src/table.rs","lineNumber":1179,"sourceCode":"            //     :param key_type:\n            //         An instance of any subclass of :class:`.DataType`. If not provided\n            //         it will default to a :class`.Text`.\n            //     :returns:\n            //         A :class:`.TableSet` mapping where the keys are unique values from\n            //         the :code:`key` and the values are new :class:`.Table` instances\n            //         containing the grouped rows.\n            //     \"\"\"\n            // ```\n            \"group_by\" => {\n                let iter = ArgsIter::new(\"Table.group_by\", &[\"key\"], args);\n                let key = iter.next_arg::<&Value>()?;\n                let key_name = iter.next_kwarg::<Option<&Value>>(\"key_name\")?;\n                let key_type = iter.next_kwarg::<Option<&Value>>(\"key_type\")?;\n                iter.finish()?;\n\n                let key = match key.as_str() {\n                    Some(s) => s,\n                    None => unimplemented!(\"group_by with function key\"),\n                };\n                let key_name = match key_name {\n                    Some(v) => match v.as_str() {\n                        Some(s) => s,\n                        None => unimplemented!(\"group_by with non-string key_name\"),\n                    },\n                    None => \"group\",\n                };\n                let key_type = match key_type {\n                    Some(ty) => match ty.downcast_object_ref::<crate::DataType>() {\n                        Some(dt) => Some(dt.clone()),\n                        None => {\n                            // TODO: support DataType class instances\n                            unimplemented!(\"group_by with non-string key_type\")\n                        }\n                    },\n                    None => None,\n                };","sourceCodeStart":1161,"sourceCodeEnd":1197,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-agate/src/table.rs#L1161-L1197","documentation":"In dbt-agate's table object, group_by accepts a key that may be a string column name or (in upstream agate) a function. The Rust port only resolves string keys; when the key kwarg is not a string (e.g. a callable/function Value), it panics with unimplemented!(\"group_by with function key\") instead of evaluating the function per row.","triggerScenarios":"Calling table.group_by(key=<function or non-string Value>) from Jinja/Python-interop code — the key kwarg resolves via as_str() to None, hitting the unimplemented!() branch.","commonSituations":"Porting Python agate code that groups by a lambda/key function into dbt Jinja; macros that compute dynamic group keys with callables; passing a non-string key object (e.g. a computed column expression) to group_by.","solutions":["Group by an existing string column name instead of a function; pre-compute the derived values into a column first, then group_by that column name.","Add a computed column to the table (table.compute / derive) holding the key expression's results, then call group_by(\"derived_col\").","If function keys are required, extend the Rust group_by to evaluate callable Values per row."],"exampleFix":"// before\ntable.group_by(lambda_fn, key_name=\"k\")\n\n// after\ntable = table.compute([[lambda_fn, \"k\"]])\ntable.group_by(\"k\", key_name=\"key_name\")","handlingStrategy":"validation","validationCode":"{% if key is string %}\n  {% set grouped = table.group_by(key) %}\n{% else %}\n  {% do exceptions.raise_compiler_error(\"group_by requires a string column key\") %}\n{% endif %}","typeGuard":"fn is_string_key(v: &Value) -> bool { v.as_str().is_some() }","tryCatchPattern":null,"preventionTips":["Always pass a string column name to group_by in dbt-agate.","Pre-compute derived grouping values into a column before grouping.","When translating Python agate code, replace key functions with computed columns."],"tags":["unimplemented","agate","group-by","jinja"],"backgroundTag":"method-not-implemented","analyzedSha":"0267ce9170576975b76b64ce856b2e5848e96617","analyzedAt":"2026-09-07T21:53:39.732Z","contentChangedAt":"2026-09-07T21:53:39.732Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}