| 116 | } |
| 117 | |
| 118 | Result<Partitioning::PartitionedBatches> KeyValuePartitioning::Partition( |
| 119 | const std::shared_ptr<RecordBatch>& batch) const { |
| 120 | std::vector<int> key_indices; |
| 121 | int num_keys = 0; |
| 122 | |
| 123 | // assemble vector of indices of fields in batch on which we'll partition |
| 124 | for (const auto& partition_field : schema_->fields()) { |
| 125 | ARROW_ASSIGN_OR_RAISE( |
| 126 | auto match, FieldRef(partition_field->name()).FindOneOrNone(*batch->schema())) |
| 127 | |
| 128 | if (match.empty()) continue; |
| 129 | key_indices.push_back(match[0]); |
| 130 | ++num_keys; |
| 131 | } |
| 132 | |
| 133 | if (key_indices.empty()) { |
| 134 | // no fields to group by; return the whole batch |
| 135 | return PartitionedBatches{{batch}, {compute::literal(true)}}; |
| 136 | } |
| 137 | |
| 138 | // assemble an ExecSpan of the key columns |
| 139 | compute::ExecSpan key_batch({}, batch->num_rows()); |
| 140 | for (int i : key_indices) { |
| 141 | key_batch.values.emplace_back(ArraySpan(*batch->column_data(i))); |
| 142 | } |
| 143 | |
| 144 | ARROW_ASSIGN_OR_RAISE(auto grouper, compute::Grouper::Make(key_batch.GetTypes())); |
| 145 | |
| 146 | ARROW_ASSIGN_OR_RAISE(Datum id_batch, grouper->Consume(key_batch)); |
| 147 | |
| 148 | auto ids = id_batch.array_as<UInt32Array>(); |
| 149 | ARROW_ASSIGN_OR_RAISE(auto groupings, |
| 150 | compute::Grouper::MakeGroupings(*ids, grouper->num_groups())); |
| 151 | |
| 152 | ARROW_ASSIGN_OR_RAISE(auto uniques, grouper->GetUniques()); |
| 153 | ArrayVector unique_arrays(num_keys); |
| 154 | for (int i = 0; i < num_keys; ++i) { |
| 155 | unique_arrays[i] = uniques.values[i].make_array(); |
| 156 | } |
| 157 | |
| 158 | PartitionedBatches out; |
| 159 | |
| 160 | // assemble partition expressions from the unique keys |
| 161 | out.expressions.resize(grouper->num_groups()); |
| 162 | for (uint32_t group = 0; group < grouper->num_groups(); ++group) { |
| 163 | std::vector<compute::Expression> exprs(num_keys); |
| 164 | |
| 165 | for (int i = 0; i < num_keys; ++i) { |
| 166 | ARROW_ASSIGN_OR_RAISE(auto val, unique_arrays[i]->GetScalar(group)); |
| 167 | const auto& name = batch->schema()->field(key_indices[i])->name(); |
| 168 | |
| 169 | exprs[i] = val->is_valid ? compute::equal(compute::field_ref(name), |
| 170 | compute::literal(std::move(val))) |
| 171 | : compute::is_null(compute::field_ref(name)); |
| 172 | } |
| 173 | out.expressions[group] = and_(std::move(exprs)); |
| 174 | } |
| 175 |
nothing calls this directly
no test coverage detected