summaryrefslogtreecommitdiffstats
path: root/contrib/libs/apache/arrow_next/cpp/src/parquet/arrow/reader_internal.h
blob: bb5d0bbeb2ea5e5277e00c80385ce2e6cb2dd9a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#pragma clang system_header
// 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.

#pragma once

#include <algorithm>
#include <cstdint>
#include <deque>
#include <functional>
#include <memory>
#include <unordered_set>
#include <utility>
#include <vector>

#include "contrib/libs/apache/arrow_next/cpp/src/parquet/arrow/schema.h"
#include "contrib/libs/apache/arrow_next/cpp/src/parquet/column_reader.h"
#include "contrib/libs/apache/arrow_next/cpp/src/parquet/file_reader.h"
#include "contrib/libs/apache/arrow_next/cpp/src/parquet/metadata.h"
#include "contrib/libs/apache/arrow_next/cpp/src/parquet/platform.h"
#include "contrib/libs/apache/arrow_next/cpp/src/parquet/schema.h"

namespace arrow20 {

class Array;
class ChunkedArray;
class DataType;
class Field;
class KeyValueMetadata;
class Schema;

}  // namespace arrow20

using arrow20::Status;

namespace parquet20 {

class ArrowReaderProperties;

namespace arrow20 {

class ColumnReaderImpl;

// ----------------------------------------------------------------------
// Iteration utilities

// Abstraction to decouple row group iteration details from the ColumnReader,
// so we can read only a single row group if we want
class FileColumnIterator {
 public:
  explicit FileColumnIterator(int column_index, ParquetFileReader* reader,
                              std::vector<int> row_groups)
      : column_index_(column_index),
        reader_(reader),
        schema_(reader->metadata()->schema()),
        row_groups_(row_groups.begin(), row_groups.end()),
        row_group_index_(-1) {}

  virtual ~FileColumnIterator() {}

  std::unique_ptr<::parquet20::PageReader> NextChunk() {
    if (row_groups_.empty()) {
      return nullptr;
    }

    row_group_index_ = row_groups_.front();
    auto row_group_reader = reader_->RowGroup(row_group_index_);
    row_groups_.pop_front();
    return row_group_reader->GetColumnPageReader(column_index_);
  }

  const SchemaDescriptor* schema() const { return schema_; }

  const ColumnDescriptor* descr() const { return schema_->Column(column_index_); }

  std::shared_ptr<FileMetaData> metadata() const { return reader_->metadata(); }

  std::unique_ptr<RowGroupMetaData> row_group_metadata() const {
    return metadata()->RowGroup(row_group_index_);
  }

  std::unique_ptr<ColumnChunkMetaData> column_chunk_metadata() const {
    return row_group_metadata()->ColumnChunk(column_index_);
  }

  int column_index() const { return column_index_; }

  int row_group_index() const { return row_group_index_; }

 protected:
  int column_index_;
  ParquetFileReader* reader_;
  const SchemaDescriptor* schema_;
  std::deque<int> row_groups_;
  int row_group_index_;
};

using FileColumnIteratorFactory =
    std::function<FileColumnIterator*(int, ParquetFileReader*)>;

struct ReaderContext {
  ParquetFileReader* reader;
  ::arrow20::MemoryPool* pool;
  FileColumnIteratorFactory iterator_factory;
  bool filter_leaves;
  std::shared_ptr<std::unordered_set<int>> included_leaves;
  ArrowReaderProperties* reader_properties;

  bool IncludesLeaf(int leaf_index) const {
    if (this->filter_leaves) {
      return this->included_leaves->find(leaf_index) != this->included_leaves->end();
    }
    return true;
  }
};

Status TransferColumnData(::parquet20::internal::RecordReader* reader,
                          std::unique_ptr<::parquet20::ColumnChunkMetaData> metadata,
                          const std::shared_ptr<::arrow20::Field>& value_field,
                          const ColumnDescriptor* descr, const ReaderContext* ctx,
                          std::shared_ptr<::arrow20::ChunkedArray>* out);

}  // namespace arrow20
}  // namespace parquet20