MCPcopy Create free account
hub / github.com/apache/arrow / Exec

Method Exec

cpp/src/arrow/compute/kernels/scalar_nested.cc:776–858  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

774 }
775
776 static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
777 const auto& options = OptionsWrapper<MapLookupOptions>::Get(ctx);
778 const UnboxedKey query_key = UnboxScalar<KeyType>::Unbox(*options.query_key);
779
780 const ArraySpan& map = batch[0].array;
781 const int32_t* offsets = map.GetValues<int32_t>(1);
782
783 // The struct holding the keys and values may have an offset
784 int64_t kv_offset = map.child_data[0].offset;
785
786 // We create a copy of the keys array because we will adjust the
787 // offset and length for the map probes below
788 ArraySpan map_keys = map.child_data[0].child_data[0];
789 const ArraySpan& map_items = map.child_data[0].child_data[1];
790
791 std::shared_ptr<DataType> item_type =
792 checked_cast<const MapType*>(map.type)->item_type();
793
794 std::unique_ptr<ArrayBuilder> builder;
795 if (options.occurrence == MapLookupOptions::Occurrence::ALL) {
796 RETURN_NOT_OK(MakeBuilder(ctx->memory_pool(), list(item_type), &builder));
797 auto list_builder = checked_cast<ListBuilder*>(builder.get());
798 auto value_builder = list_builder->value_builder();
799
800 for (int64_t map_index = 0; map_index < map.length; ++map_index) {
801 if (!map.IsValid(map_index)) {
802 RETURN_NOT_OK(list_builder->AppendNull());
803 continue;
804 }
805
806 const int64_t item_offset = offsets[map_index] + kv_offset;
807 const int32_t item_size = offsets[map_index + 1] - offsets[map_index];
808
809 // Adjust the keys view to just the map slot that we are about to search
810 map_keys.SetSlice(item_offset, item_size);
811
812 bool found_at_least_one_key = false;
813 RETURN_NOT_OK(FindMatchingIndices(map_keys, query_key, [&](int64_t key_index) {
814 if (!found_at_least_one_key) {
815 RETURN_NOT_OK(list_builder->Append(true));
816 }
817 found_at_least_one_key = true;
818 return value_builder->AppendArraySlice(map_items, item_offset + key_index, 1);
819 }));
820 if (!found_at_least_one_key) {
821 // Key was not found in this map element, so we append a null list
822 RETURN_NOT_OK(list_builder->AppendNull());
823 }
824 }
825 ARROW_ASSIGN_OR_RAISE(auto result, list_builder->Finish());
826 out->value = std::move(result->data());
827 } else { /* occurrence == FIRST || LAST */
828 RETURN_NOT_OK(MakeBuilder(ctx->memory_pool(), item_type, &builder));
829 RETURN_NOT_OK(builder->Reserve(batch.length));
830 for (int64_t map_index = 0; map_index < map.length; ++map_index) {
831 if (!map.IsValid(map_index)) {
832 RETURN_NOT_OK(builder->AppendNull());
833 continue;

Callers

nothing calls this directly

Calls 13

listFunction · 0.85
GetFunction · 0.50
MakeBuilderFunction · 0.50
OKFunction · 0.50
memory_poolMethod · 0.45
getMethod · 0.45
value_builderMethod · 0.45
IsValidMethod · 0.45
AppendNullMethod · 0.45
AppendMethod · 0.45
AppendArraySliceMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected