[ 
https://issues.apache.org/jira/browse/ARROW-1209?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16239263#comment-16239263
 ] 

ASF GitHub Bot commented on ARROW-1209:
---------------------------------------

cpcloud commented on a change in pull request #1026: ARROW-1209: [C++] [WIP] 
Support for reading avro from an AvroFileReader
URL: https://github.com/apache/arrow/pull/1026#discussion_r148942232
 
 

 ##########
 File path: cpp/src/arrow/adapters/avro/adapter.cc
 ##########
 @@ -0,0 +1,325 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <cstdint>
+#include <memory>
+#include <string>
+#include <sstream>
+#include <arrow/util/logging.h>
+
+#include "arrow/adapters/avro/adapter.h"
+
+#include "arrow/builder.h"
+#include "arrow/buffer.h"
+#include "arrow/memory_pool.h"
+#include "arrow/status.h"
+#include "arrow/table.h"
+#include "arrow/type.h"
+#include "arrow/type_traits.h"
+#include "arrow/util/bit-util.h"
+#include "arrow/util/macros.h"
+#include "arrow/util/visibility.h"
+
+#include "avro.h"
+
+namespace arrow {
+namespace adapters {
+namespace avro {
+
+class AvroArrowReader::AvroArrowReaderImpl {
+public:
+  Status GenerateAvroSchema(avro_file_reader_t *file_reader,
+                            std::shared_ptr<DataType> *out) {
+    auto wschema = avro_file_reader_get_writer_schema(*file_reader);
+    std::shared_ptr<DataType> arrow_schema = get_arrow_type(wschema);
+    // make array builder
+    out = &arrow_schema;
+    return Status::OK();
+  }
+
+  std::shared_ptr<DataType> get_arrow_type(avro_schema_t schema) {
+    avro_schema_t child;
+    size_t size, idx;
+    int64_t length;
+    const char *name;
+
+    auto avro_type = schema->type;
+    switch (avro_type) {
+      case AVRO_BOOLEAN:
+        return boolean();
+      case AVRO_INT32:
+        return int32();
+      case AVRO_INT64:
+        return int64();
+      case AVRO_FLOAT:
+        return float32();
+      case AVRO_DOUBLE:
+        return float64();
+      case AVRO_STRING:
+        return utf8();
+      case AVRO_BYTES:
+        return binary();
+      case AVRO_FIXED: {
+        length = avro_schema_fixed_size(schema);
+        return fixed_size_binary(length);
+      }
+      case AVRO_ARRAY: {
+        auto elem_type = get_arrow_type(avro_schema_array_items(schema));
+        return list(elem_type);
+      }
+
+      case AVRO_MAP: {
+        auto value_type = get_arrow_type(avro_schema_map_values(schema));
+        auto fields = {field("key", utf8()), field("value", value_type)};
+        return list(struct_(fields));
+      }
+
+      case AVRO_UNION: {
+        size = avro_schema_union_size(schema);
+        auto fields = std::vector<std::shared_ptr<Field>>();
+        std::vector<uint8_t> type_codes;
+
+        for (idx = 0; idx < size; idx++) {
+          child = avro_schema_union_branch(schema, idx);
+          auto f = field("_union_" + std::to_string(idx),
+                         get_arrow_type(child));
+          fields.push_back(f);
+          type_codes.push_back((uint8_t) idx);
+        };
+        return union_(fields, type_codes);
+      }
+
+      case AVRO_RECORD: {
+        size = avro_schema_record_size(schema);
+        auto fields = std::vector<std::shared_ptr<Field>>();
+        for (idx = 0; idx < size; idx++) {
+          child = avro_schema_record_field_get_by_index(schema, idx);
+          auto elem_type = get_arrow_type(child);
+          name = avro_schema_record_field_name(schema, idx);
+          auto f = field(name, elem_type);
+          fields.insert(fields.end(), f);
+        }
+        return struct_(fields);
+      }
+    }
+  }
+
+  Status generic_read(const avro_value_t val, ArrayBuilder *builder) {
+    // Generic avro type read dispatcher. Dispatches to the various 
specializations by AVRO_TYPE.
+    // This is used by the various readers for complex types"""
+    avro_type_t avro_type;
+    avro_type = avro_value_get_type(&val);
+
+    switch (avro_type) {
+      case AVRO_BOOLEAN:
+        return this->read_bool(val, builder);
+      case AVRO_INT32:
+        return this->read_int32(val, builder);
+      case AVRO_INT64:
+        return this->read_int64(val, builder);
+      case AVRO_FLOAT:
+        return this->read_float32(val, builder);
+      case AVRO_DOUBLE:
+        return this->read_float64(val, builder);
+      case AVRO_STRING:
+        return this->read_string(val, builder);
+      case AVRO_BYTES:
+        return this->read_binary(val, builder);
+      case AVRO_FIXED:
+        return this->read_fixed(val, builder);
+      case AVRO_MAP:
+        return this->read_map(val, builder);
+      case AVRO_RECORD:
+        return this->read_record(val, builder);
+      case AVRO_ARRAY:
+        return this->read_array(val, builder);
+      default:
+        std::stringstream ss;
+        ss << "Unhandled avro type: " << avro_type;
+        return Status::NotImplemented(ss.str());
+    }
+  }
+
+  Status read_string(const avro_value_t val, ArrayBuilder *builder) {
+    size_t strlen;
+    const char *c_str = NULL;
+    avro_value_get_string(&val, &c_str, &strlen);
+    return static_cast<StringBuilder *>(builder)->Append(c_str);
+  }
+
+  Status read_binary(const avro_value_t val, ArrayBuilder *builder) {
+    size_t strlen;
+    const char *c_str = NULL;
+    avro_value_get_bytes(&val, (const void **) &c_str, &strlen);
 
 Review comment:
   Hm, if `avro_value_get_bytes`'s second argument is `void**` then you 
shouldn't have to cast, but if you definitely do need to then you should do 
this with `reinterpret_cast<void**>(&c_str)`.
   
   This should also fail because you can't mutate anything of type `const 
void**` unless someone is doing something nasty like casting away the `const` 
with `const_cast`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> [C++] Implement converter between Arrow record batches and Avro records
> -----------------------------------------------------------------------
>
>                 Key: ARROW-1209
>                 URL: https://issues.apache.org/jira/browse/ARROW-1209
>             Project: Apache Arrow
>          Issue Type: New Feature
>          Components: C++
>            Reporter: Wes McKinney
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 1.0.0
>
>
> This would be useful for streaming systems that need to consume or produce 
> Avro in C/C++



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to