OmniSciDB  85c2d10cdc
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
20 #include "DataMgr/AbstractBuffer.h"
21 #include "ForeignStorageBuffer.h"
22 #include "ParquetShared.h"
23 
24 #include <parquet/metadata.h>
25 
26 namespace foreign_storage {
27 
29  public:
31  virtual ~ParquetEncoder() = default;
32 
33  virtual void appendData(const int16_t* def_levels,
34  const int16_t* rep_levels,
35  const int64_t values_read,
36  const int64_t levels_read,
37  const bool is_last_batch,
38  int8_t* values) = 0;
39 
40  virtual std::shared_ptr<ChunkMetadata> getRowGroupMetadata(
41  const parquet::RowGroupMetaData* group_metadata,
42  const int parquet_column_index,
43  const SQLTypeInfo& column_type) {
44  // update statistics
45  auto column_metadata = group_metadata->ColumnChunk(parquet_column_index);
46  auto stats = validate_and_get_column_metadata_statistics(column_metadata.get());
47  auto metadata = createMetadata(column_type);
48 
49  auto null_count = stats->null_count();
50  validateNullCount(group_metadata->schema()->Column(parquet_column_index)->name(),
51  null_count,
52  column_type);
53  metadata->chunkStats.has_nulls = null_count > 0;
54 
55  // update sizing
56  metadata->numElements = group_metadata->num_rows();
57  return metadata;
58  }
59 
60  protected:
62 
63  static std::shared_ptr<ChunkMetadata> createMetadata(const SQLTypeInfo& column_type) {
64  auto metadata = std::make_shared<ChunkMetadata>();
65  ForeignStorageBuffer buffer;
66  buffer.initEncoder(column_type.is_array() ? column_type.get_elem_type()
67  : column_type);
68  auto encoder = buffer.getEncoder();
69  encoder->getMetadata(metadata);
70  metadata->sqlType = column_type;
71  return metadata;
72  }
73 
74  static void throwNotNullViolation(const std::string& parquet_column_name) {
75  std::stringstream error_message;
76  error_message << "A null value was detected in Parquet column '"
77  << parquet_column_name << "' but OmniSci column is set to not null";
78  throw std::runtime_error(error_message.str());
79  }
80 
81  static void validateNullCount(const std::string& parquet_column_name,
82  int64_t null_count,
83  const SQLTypeInfo& column_type) {
84  bool has_nulls = null_count > 0;
85  if (has_nulls && column_type.get_notnull()) {
86  throwNotNullViolation(parquet_column_name);
87  }
88  }
89 };
90 
92  public:
94 
95  virtual void setNull(int8_t* omnisci_data_bytes) = 0;
96  virtual void copy(const int8_t* omnisci_data_bytes_source,
97  int8_t* omnisci_data_bytes_destination) = 0;
98  virtual void encodeAndCopy(const int8_t* parquet_data_bytes,
99  int8_t* omnisci_data_bytes) = 0;
100 
101  virtual void encodeAndCopyContiguous(const int8_t* parquet_data_bytes,
102  int8_t* omnisci_data_bytes,
103  const size_t num_elements) = 0;
104 };
105 
106 } // 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
void initEncoder(const SQLTypeInfo &tmp_sql_type)
virtual void appendData(const int16_t *def_levels, const int16_t *rep_levels, const int64_t values_read, const int64_t levels_read, const bool is_last_batch, int8_t *values)=0
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.
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
ParquetScalarEncoder(Data_Namespace::AbstractBuffer *buffer)
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:321
Data_Namespace::AbstractBuffer * buffer_
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:712
bool is_array() const
Definition: sqltypes.h:496
virtual void copy(const int8_t *omnisci_data_bytes_source, int8_t *omnisci_data_bytes_destination)=0