OmniSciDB  16c4e035a1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParquetEncoder.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "DataMgr/AbstractBuffer.h"
20 #include "ForeignStorageBuffer.h"
21 #include "ParquetShared.h"
22 
23 #include <parquet/metadata.h>
24 
25 namespace foreign_storage {
26 
28  public:
30  virtual ~ParquetEncoder() = default;
31 
32  virtual void appendData(const int16_t* def_levels,
33  const int16_t* rep_levels,
34  const int64_t values_read,
35  const int64_t levels_read,
36  int8_t* values) = 0;
37 
38  virtual std::shared_ptr<ChunkMetadata> getRowGroupMetadata(
39  const parquet::RowGroupMetaData* group_metadata,
40  const int parquet_column_index,
41  const SQLTypeInfo& column_type) {
42  // update statistics
43  auto column_metadata = group_metadata->ColumnChunk(parquet_column_index);
44  auto stats = validate_and_get_column_metadata_statistics(column_metadata.get());
45  auto metadata = createMetadata(column_type);
46 
47  auto null_count = stats->null_count();
48  validateNullCount(group_metadata->schema()->Column(parquet_column_index)->name(),
49  null_count,
50  column_type);
51  metadata->chunkStats.has_nulls = null_count > 0;
52 
53  // update sizing
54  metadata->numElements = group_metadata->num_rows();
55  return metadata;
56  }
57 
58  protected:
60 
61  static std::shared_ptr<ChunkMetadata> createMetadata(const SQLTypeInfo& column_type) {
62  auto metadata = std::make_shared<ChunkMetadata>();
63  ForeignStorageBuffer buffer;
64  buffer.initEncoder(column_type.is_array() ? column_type.get_elem_type()
65  : column_type);
66  auto encoder = buffer.getEncoder();
67  encoder->getMetadata(metadata);
68  metadata->sqlType = column_type;
69  return metadata;
70  }
71 
72  static void throwNotNullViolation(const std::string& parquet_column_name) {
73  std::stringstream error_message;
74  error_message << "A null value was detected in Parquet column '"
75  << parquet_column_name << "' but OmniSci column is set to not null";
76  throw std::runtime_error(error_message.str());
77  }
78 
79  static void validateNullCount(const std::string& parquet_column_name,
80  int64_t null_count,
81  const SQLTypeInfo& column_type) {
82  bool has_nulls = null_count > 0;
83  if (has_nulls && column_type.get_notnull()) {
84  throwNotNullViolation(parquet_column_name);
85  }
86  }
87 };
88 
89 using InvalidRowGroupIndices = std::set<int64_t>;
90 
92  public:
93  virtual void eraseInvalidIndicesInBuffer(
94  const InvalidRowGroupIndices& invalid_indices) = 0;
95 
96  virtual void validateAndAppendData(const int16_t* def_levels,
97  const int16_t* rep_levels,
98  const int64_t values_read,
99  const int64_t levels_read,
100  int8_t* values,
101  const SQLTypeInfo& column_type, /* may not be used */
102  InvalidRowGroupIndices& invalid_indices) = 0;
103 };
104 
106  public:
108 
109  virtual void setNull(int8_t* omnisci_data_bytes) = 0;
110  virtual void copy(const int8_t* omnisci_data_bytes_source,
111  int8_t* omnisci_data_bytes_destination) = 0;
112  virtual void encodeAndCopy(const int8_t* parquet_data_bytes,
113  int8_t* omnisci_data_bytes) = 0;
114 
115  virtual void encodeAndCopyContiguous(const int8_t* parquet_data_bytes,
116  int8_t* omnisci_data_bytes,
117  const size_t num_elements) = 0;
118 
119  virtual void validate(const int8_t* parquet_data,
120  const int64_t j,
121  const SQLTypeInfo& column_type) const = 0;
122 };
123 
124 } // namespace foreign_storage
ParquetEncoder(Data_Namespace::AbstractBuffer *buffer)
virtual ~ParquetEncoder()=default
std::shared_ptr< parquet::Statistics > validate_and_get_column_metadata_statistics(const parquet::ColumnChunkMetaData *column_metadata)
static void throwNotNullViolation(const std::string &parquet_column_name)
virtual void encodeAndCopy(const int8_t *parquet_data_bytes, int8_t *omnisci_data_bytes)=0
virtual void appendData(const int16_t *def_levels, const int16_t *rep_levels, const int64_t values_read, const int64_t levels_read, int8_t *values)=0
void initEncoder(const SQLTypeInfo &tmp_sql_type)
std::set< int64_t > InvalidRowGroupIndices
static void validateNullCount(const std::string &parquet_column_name, int64_t null_count, const SQLTypeInfo &column_type)
virtual void setNull(int8_t *omnisci_data_bytes)=0
An AbstractBuffer is a unit of data management for a data manager.
virtual void validate(const int8_t *parquet_data, const int64_t j, const SQLTypeInfo &column_type) const =0
static std::shared_ptr< ChunkMetadata > createMetadata(const SQLTypeInfo &column_type)
virtual std::shared_ptr< ChunkMetadata > getRowGroupMetadata(const parquet::RowGroupMetaData *group_metadata, const int parquet_column_index, const SQLTypeInfo &column_type)
virtual void encodeAndCopyContiguous(const int8_t *parquet_data_bytes, int8_t *omnisci_data_bytes, const size_t num_elements)=0
virtual void validateAndAppendData(const int16_t *def_levels, const int16_t *rep_levels, const int64_t values_read, const int64_t levels_read, int8_t *values, const SQLTypeInfo &column_type, InvalidRowGroupIndices &invalid_indices)=0
virtual void eraseInvalidIndicesInBuffer(const InvalidRowGroupIndices &invalid_indices)=0
ParquetScalarEncoder(Data_Namespace::AbstractBuffer *buffer)
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:336
Data_Namespace::AbstractBuffer * buffer_
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:861
bool is_array() const
Definition: sqltypes.h:527
virtual void copy(const int8_t *omnisci_data_bytes_source, int8_t *omnisci_data_bytes_destination)=0