dhruv9vats commented on a change in pull request #12162:
URL: https://github.com/apache/arrow/pull/12162#discussion_r785454432



##########
File path: cpp/src/arrow/compute/kernels/scalar_nested.cc
##########
@@ -429,6 +430,97 @@ const FunctionDoc make_struct_doc{"Wrap Arrays into a 
StructArray",
                                   {"*args"},
                                   "MakeStructOptions"};
 
+struct MapArrayLookupFunctor {
+  static Status ExecMapArray(KernelContext* ctx, const ExecBatch& batch, 
Datum* out) {
+    const auto& options = OptionsWrapper<MapArrayLookupOptions>::Get(ctx);
+
+    MapArray map_array(batch[0].array());
+
+    // Offset differences will tell the number of Strcut = {key, value} pairs
+    // present in the current list.
+    // const std::shared_ptr<arrow::Buffer> offsets = 
map_array.value_offsets();
+
+    std::shared_ptr<arrow::Array> keys = map_array.keys();
+    std::shared_ptr<arrow::Array> items = map_array.items();
+
+    const auto& query_key = options.query_key;
+    const auto& occurence = options.occurence;
+
+    std::unique_ptr<ArrayBuilder> builder;
+    RETURN_NOT_OK(
+        MakeBuilder(ctx->memory_pool(), map_array.map_type()->item_type(), 
&builder));
+
+    int32_t last_key_idx_checked = 0;
+
+    // aka, number of {key, value} pairs in the current map
+    int32_t list_struct_len;
+    bool found_one_key = false;
+    for (int32_t map_array_idx = 0; map_array_idx < map_array.length(); 
++map_array_idx) {
+      // Number of Struct('s) = {key, value} in the list at the current index
+      list_struct_len = map_array.value_length(map_array_idx);
+      for (int32_t key_idx_to_check = last_key_idx_checked;
+           key_idx_to_check < last_key_idx_checked + list_struct_len;
+           ++key_idx_to_check) {
+        ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Scalar> key,
+                              keys->GetScalar(key_idx_to_check));
+        if (key->Equals(*query_key)) {
+          std::cout << "Key being checked: " << key->ToString() << "\n";
+          ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Scalar> item,
+                                items->GetScalar(key_idx_to_check));
+          std::cout << "Value at key: " << item->ToString() << "\n";
+          ARROW_ASSIGN_OR_RAISE(auto value,
+                                
item->CastTo(map_array.map_type()->item_type()));
+
+          std::cout << "Item being appended: " << value->ToString() << "\n";
+          RETURN_NOT_OK(builder->AppendScalar(*value));

Review comment:
       Had to use the `AppendScalar` approach, as using
   ```cpp
   RETURN_NOT_OK(builder->AppendArraySlice(*items->data(), key_idx_to_check, 
1));
   ```
   caused problems for me. (Is this even the correct way of using 
`AppendArraySlice`?)
   <details><summary>And for the current tests, AppendArraySlice failed with: 
</summary>
   
   ```bash
   ❯ GTEST_FILTER="TestScalarNested.MapArrayLookup" ctest -R 
"arrow-compute-scalar-test" -V
   UpdateCTestConfiguration  from 
:/home/dhruv/repos/arrow/cpp/out/build/ninja-debug/DartConfiguration.tcl
   UpdateCTestConfiguration  from 
:/home/dhruv/repos/arrow/cpp/out/build/ninja-debug/DartConfiguration.tcl
   Test project /home/dhruv/repos/arrow/cpp/out/build/ninja-debug
   Constructing a list of tests
   Done constructing a list of tests
   Updating test list for fixtures
   Added 0 tests to meet fixture requirements
   Checking test dependency graph...
   Checking test dependency graph end
   test 25
       Start 25: arrow-compute-scalar-test
   
   25: Test command: /home/dhruv/repos/arrow/cpp/build-support/run-test.sh 
"/home/dhruv/repos/arrow/cpp/out/build/ninja-debug" "test" 
"/home/dhruv/repos/arrow/cpp/out/build/ninja-debug/debug//arrow-compute-scalar-test"
   25: Test timeout computed to be: 10000000
   25: Running arrow-compute-scalar-test, redirecting output into 
/home/dhruv/repos/arrow/cpp/out/build/ninja-debug/build/test-logs/arrow-compute-scalar-test.txt
 (attempt 1/1)
   25: Running main() from ../googletest/src/gtest_main.cc
   25: Note: Google Test filter = TestScalarNested.MapArrayLookup
   25: [==========] Running 1 test from 1 test suite.
   25: [----------] Global test environment set-up.
   25: [----------] 1 test from TestScalarNested
   25: [ RUN      ] TestScalarNested.MapArrayLookup
   25: map type found!
   25: Value type: int32
   25: Key being checked: foo
   25: Item at key: 99
   25: Key being checked: foo
   25: Item at key: 3
   25: Key being checked: foo
   25: Item at key: 101
   25: Key being checked: foo
   25: Item at key: 22
   25: /home/dhruv/repos/arrow/cpp/src/arrow/testing/gtest_util.cc:134: Failure
   25: Failed
   25:
   25: @@ -0, +0 @@
   25: -99
   25: -3
   25: -101
   25: -22
   25: +65635
   25: +0
   25: +0
   25: +0
   25: Expected:
   25:   [
   25:     99,
   25:     3,
   25:     101,
   25:     22
   25:   ]
   25: Actual:
   25:   [
   25:     65635,
   25:     0,
   25:     0,
   25:     0
   25:   ]
   25: map type found!
   25: Value type: int32
   25: Key being checked: foo
   25: Item at key: 99
   25: /home/dhruv/repos/arrow/cpp/src/arrow/testing/gtest_util.cc:134: Failure
   25: Failed
   25:
   25: @@ -0, +0 @@
   25: -99
   25: +65635
   25: Expected:
   25:   [
   25:     99
   25:   ]
   25: Actual:
   25:   [
   25:     65635
   25:   ]
   25: [  FAILED  ] TestScalarNested.MapArrayLookup (8 ms)
   25: [----------] 1 test from TestScalarNested (8 ms total)
   25:
   25: [----------] Global test environment tear-down
   25: [==========] 1 test from 1 test suite ran. (8 ms total)
   25: [  PASSED  ] 0 tests.
   25: [  FAILED  ] 1 test, listed below:
   25: [  FAILED  ] TestScalarNested.MapArrayLookup
   25:
   25:  1 FAILED TEST
   25: ~/repos/arrow/cpp/out/build/ninja-debug/src/arrow/compute/kernels
   1/1 Test #25: arrow-compute-scalar-test ........***Failed    0.10 sec
   
   0% tests passed, 1 tests failed out of 1
   
   Label Time Summary:
   arrow_compute    =   0.10 sec*proc (1 test)
   unittest         =   0.10 sec*proc (1 test)
   
   Total Test time (real) =   0.10 sec
   
   The following tests FAILED:
            25 - arrow-compute-scalar-test (Failed)
   Errors while running CTest
   Output from these tests are in: 
/home/dhruv/repos/arrow/cpp/out/build/ninja-debug/Testing/Temporary/LastTest.log
   Use "--rerun-failed --output-on-failure" to re-run the failed cases 
verbosely.
   ```
   
   </details>
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to