* @brief Executes the read query for the array created in write_array. * * @param ctx The context. * @param qc The query condition to execute the query with. */
| 169 | * @param qc The query condition to execute the query with. |
| 170 | */ |
| 171 | void read_array_with_qc(Context& ctx, std::optional<QueryCondition> qc) { |
| 172 | // Create data buffers to read the values into. |
| 173 | std::vector<int> a_data(num_elems); |
| 174 | std::vector<uint8_t> a_data_validity(num_elems); |
| 175 | |
| 176 | // We initialize the string b_data to contain 44 characters because |
| 177 | // that is the combined size of all strings in attribute b. |
| 178 | std::string b_data; |
| 179 | b_data.resize(44); |
| 180 | |
| 181 | std::vector<uint64_t> b_data_offsets(num_elems); |
| 182 | std::vector<int32_t> c_data(num_elems); |
| 183 | std::vector<float> d_data(num_elems); |
| 184 | |
| 185 | // Execute the read query. |
| 186 | Array array(ctx, array_name, TILEDB_READ); |
| 187 | Subarray subarray(ctx, array); |
| 188 | subarray.add_range("index", 0, num_elems - 1); |
| 189 | Query query(ctx, array); |
| 190 | query.set_layout(TILEDB_ROW_MAJOR) |
| 191 | .set_data_buffer("a", a_data) |
| 192 | .set_validity_buffer("a", a_data_validity) |
| 193 | .set_data_buffer("b", b_data) |
| 194 | .set_offsets_buffer("b", b_data_offsets) |
| 195 | .set_data_buffer("c", c_data) |
| 196 | .set_data_buffer("d", d_data) |
| 197 | .set_subarray(subarray); |
| 198 | if (qc) { |
| 199 | query.set_condition(qc.value()); |
| 200 | } |
| 201 | |
| 202 | query.submit(); |
| 203 | |
| 204 | // Collect the results of the read query. The number of elements |
| 205 | // the filtered array contains is in num_elements_result. |
| 206 | // The length of the filtered substring of all the data is in |
| 207 | // b_data, and all the offsets for filtered individual elements |
| 208 | // are in b_data_offsets. |
| 209 | auto table = query.result_buffer_elements_nullable(); |
| 210 | size_t num_elements_result = std::get<1>(table["c"]); |
| 211 | uint64_t b_str_length = std::get<1>(table["b"]); |
| 212 | b_data_offsets.push_back(b_str_length); |
| 213 | |
| 214 | // Here we print all the elements that are returned by the query. |
| 215 | for (size_t i = 0; i < num_elements_result; ++i) { |
| 216 | // We can filter results based on whether the fill value is used in the |
| 217 | // result buffers. |
| 218 | if (c_data[i] != c_fill_value) { |
| 219 | // We pass in nullopt if the data is invalid, per the validity buffer. |
| 220 | std::optional<int> a_val; |
| 221 | if (a_data_validity[i]) { |
| 222 | a_val = a_data[i]; |
| 223 | } else { |
| 224 | a_val = std::nullopt; |
| 225 | } |
| 226 | print_elem( |
| 227 | a_val, |
| 228 | b_data.substr( |
no test coverage detected