{"record":{"id":"6f2cb56c5d84a1ca","repo":"influxdata/influxdb","slug":"key-column-name-of-last-batch-has-no-data","errorCode":null,"errorMessage":"Key column, {name}, of last_batch has no data","messagePattern":"Key column, (.+?), of last_batch has no data","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/iox_query/src/provider/deduplicate/algo.rs","lineNumber":122,"sourceCode":"        // Take the previous batch, if any, out of it storage self.last_batch\n        if let Some(last_batch) = self.last_batch.take() {\n            // Build sorted columns for last_batch and current one\n            let schema = last_batch.schema();\n            // is_sort_key[col_idx] = true if it is present in sort keys\n            let mut is_sort_key: Vec<bool> = vec![false; last_batch.columns().len()];\n            let last_batch_key_columns = self\n                .sort_keys\n                .iter()\n                .map(|skey| {\n                    // figure out the index of the key columns\n                    let name = get_col_name(skey.expr.as_ref());\n                    let index = schema.index_of(name).unwrap();\n                    is_sort_key[index] = true;\n\n                    // Key column of last_batch of this index\n                    let last_batch_array = last_batch.column(index);\n                    if last_batch_array.is_empty() {\n                        panic!(\"Key column, {name}, of last_batch has no data\");\n                    }\n                    DedupSortColumn {\n                        array: last_batch_array,\n                        options: skey.options,\n                    }\n                })\n                .collect::<Vec<_>>();\n\n            // Build sorted columns for current batch\n            // Schema of both batches are the same\n            let batch_key_columns = self\n                .sort_keys\n                .iter()\n                .map(|skey| {\n                    // figure out the index of the key columns\n                    let name = get_col_name(skey.expr.as_ref());\n                    let index = schema.index_of(name).unwrap();\n","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/influxdata/influxdb/blob/06200ef96ba82c5f6727e5038a83af8e722c6875/core/iox_query/src/provider/deduplicate/algo.rs#L104-L140","documentation":"During deduplication (deduplicate / last_batch_with_no_same_sort_key), the sort-key column is taken from the last batch to seed the algorithm. If that column exists in the schema but its RecordBatch array is empty (zero rows), the algorithm cannot establish ordering state and panics naming the key column.","triggerScenarios":"Calling deduplicate with a batch list whose final batch has zero rows while the schema declares a sort key on that column.","commonSituations":"Feeding empty trailing batches produced by upstream filters/limits; concatenated chunk scans where the last chunk returned no rows; tests constructing batches with one empty record batch.","solutions":["Filter out empty RecordBatches before calling deduplicate.","Ensure the last batch passed has at least one row, or reorder so a non-empty batch is last.","Check upstream operators (filter, limit) that may produce zero-row batches and drop them.","If you maintain a fork, return an error instead of panicking on the empty key column."],"exampleFix":"// before\ndeduplicate(&batches, &sort_key)?;\n// after\nlet batches: Vec<_> = batches.into_iter().filter(|b| b.num_rows() > 0).collect();\ndeduplicate(&batches, &sort_key)?;","handlingStrategy":"validation","validationCode":"let batches: Vec<RecordBatch> = batches.into_iter().filter(|b| b.num_rows() > 0).collect();\nassert!(!batches.is_empty(), \"no non-empty batches to deduplicate\");","typeGuard":null,"tryCatchPattern":"// deduplicate panics; filter empty batches before invoking\nlet kept: Vec<_> = batches.into_iter().filter(|b| b.num_rows() > 0).collect();\ndeduplicate(&kept, &sort_key)?;","preventionTips":["Filter zero-row batches out of dedup inputs.","Ensure the final batch passed has rows for every sort-key column.","Sanity-check upstream filter/limit operators for empty outputs."],"tags":["query","deduplicate","panic","record-batch"],"backgroundTag":"empty-required-field","analyzedSha":"06200ef96ba82c5f6727e5038a83af8e722c6875","analyzedAt":"2026-09-19T12:55:30.003Z","contentChangedAt":"2026-09-19T12:55:30.003Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}