|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/arrow_c_data_internal.h" |
| 21 | + |
| 22 | +#include <array> |
| 23 | +#include <string> |
| 24 | +#include <utility> |
| 25 | + |
| 26 | +namespace iceberg::internal { |
| 27 | + |
| 28 | +std::pair<ArrowSchema, ArrowArray> CreateExampleArrowSchemaAndArrayByNanoarrow() { |
| 29 | + ArrowSchema out_schema; |
| 30 | + |
| 31 | + // Initializes the root struct schema |
| 32 | + NANOARROW_THROW_NOT_OK(ArrowSchemaInitFromType(&out_schema, NANOARROW_TYPE_STRUCT)); |
| 33 | + NANOARROW_THROW_NOT_OK(ArrowSchemaAllocateChildren(&out_schema, 2)); |
| 34 | + |
| 35 | + // Set up the non-nullable int64 field |
| 36 | + struct ArrowSchema* int64_field = out_schema.children[0]; |
| 37 | + ArrowSchemaInit(int64_field); |
| 38 | + NANOARROW_THROW_NOT_OK(ArrowSchemaInitFromType(int64_field, NANOARROW_TYPE_INT64)); |
| 39 | + NANOARROW_THROW_NOT_OK(ArrowSchemaSetName(int64_field, "id")); |
| 40 | + int64_field->flags &= ~ARROW_FLAG_NULLABLE; |
| 41 | + |
| 42 | + // Set up the nullable string field |
| 43 | + struct ArrowSchema* string_field = out_schema.children[1]; |
| 44 | + ArrowSchemaInit(string_field); |
| 45 | + NANOARROW_THROW_NOT_OK(ArrowSchemaInitFromType(string_field, NANOARROW_TYPE_STRING)); |
| 46 | + NANOARROW_THROW_NOT_OK(ArrowSchemaSetName(string_field, "name")); |
| 47 | + string_field->flags |= ARROW_FLAG_NULLABLE; |
| 48 | + |
| 49 | + constexpr int64_t kNumValues = 3; |
| 50 | + std::array<int64_t, kNumValues> int64_values = {1, 2, 3}; |
| 51 | + std::array<std::string, kNumValues> string_values = {"a", "b", "c"}; |
| 52 | + |
| 53 | + ArrowArray out_array; |
| 54 | + NANOARROW_THROW_NOT_OK(ArrowArrayInitFromSchema(&out_array, &out_schema, nullptr)); |
| 55 | + ArrowArray* int64_array = out_array.children[0]; |
| 56 | + ArrowArray* string_array = out_array.children[1]; |
| 57 | + |
| 58 | + NANOARROW_THROW_NOT_OK(ArrowArrayStartAppending(int64_array)); |
| 59 | + NANOARROW_THROW_NOT_OK(ArrowArrayStartAppending(string_array)); |
| 60 | + |
| 61 | + for (int64_t i = 0; i < kNumValues; i++) { |
| 62 | + NANOARROW_THROW_NOT_OK(ArrowArrayAppendInt(int64_array, int64_values[i])); |
| 63 | + NANOARROW_THROW_NOT_OK( |
| 64 | + ArrowArrayAppendString(string_array, ArrowCharView(string_values[i].c_str()))); |
| 65 | + } |
| 66 | + |
| 67 | + NANOARROW_THROW_NOT_OK(ArrowArrayFinishBuildingDefault(int64_array, nullptr)); |
| 68 | + NANOARROW_THROW_NOT_OK(ArrowArrayFinishBuildingDefault(string_array, nullptr)); |
| 69 | + |
| 70 | + out_array.length = kNumValues; |
| 71 | + out_array.null_count = 0; |
| 72 | + |
| 73 | + return {out_schema, out_array}; |
| 74 | +} |
| 75 | + |
| 76 | +} // namespace iceberg::internal |
0 commit comments