|
| 1 | +#include "result_set_parquet_printer.h" |
| 2 | + |
| 3 | +#include <ydb/public/sdk/cpp/client/ydb_value/value.h> |
| 4 | +#include <ydb/public/sdk/cpp/client/ydb_result/result.h> |
| 5 | + |
| 6 | +#include <contrib/libs/apache/arrow/cpp/src/arrow/io/file.h> |
| 7 | +#include <contrib/libs/apache/arrow/cpp/src/arrow/io/stdio.h> |
| 8 | +#include <contrib/libs/apache/arrow/cpp/src/parquet/stream_writer.h> |
| 9 | +#include <contrib/libs/apache/arrow/cpp/src/parquet/schema.h> |
| 10 | + |
| 11 | +#include <util/folder/path.h> |
| 12 | + |
| 13 | +namespace NYdb { |
| 14 | + |
| 15 | + class TResultSetParquetPrinter::TImpl { |
| 16 | + public: |
| 17 | + explicit TImpl(const std::string& outputPath, ui64 rowGroupSize); |
| 18 | + void Reset(); |
| 19 | + void Print(const TResultSet& resultSet); |
| 20 | + |
| 21 | + private: |
| 22 | + void InitStream(const TResultSet& resultSet); |
| 23 | + static parquet::schema::NodePtr ToParquetType(const char* name, const TTypeParser& type, bool nullable); |
| 24 | + |
| 25 | + private: |
| 26 | + std::unique_ptr<parquet::StreamWriter> Stream; |
| 27 | + const std::string OutputPath; |
| 28 | + const ui64 RowGroupSize; |
| 29 | + }; |
| 30 | + |
| 31 | + void TResultSetParquetPrinter::TImpl::InitStream(const TResultSet& resultSet) { |
| 32 | + parquet::schema::NodeVector fields; |
| 33 | + for (const auto& field : resultSet.GetColumnsMeta()) { |
| 34 | + TTypeParser type(field.Type); |
| 35 | + bool nullable = false; |
| 36 | + if (type.GetKind() == TTypeParser::ETypeKind::Optional) { |
| 37 | + nullable = true; |
| 38 | + type.OpenOptional(); |
| 39 | + } |
| 40 | + fields.emplace_back(ToParquetType(field.Name.c_str(), type, nullable)); |
| 41 | + } |
| 42 | + auto schema = std::static_pointer_cast<parquet::schema::GroupNode>( |
| 43 | + parquet::schema::GroupNode::Make("schema", parquet::Repetition::REQUIRED, fields)); |
| 44 | + parquet::WriterProperties::Builder builder; |
| 45 | + builder.compression(parquet::Compression::ZSTD); |
| 46 | + builder.disable_dictionary(); |
| 47 | + std::shared_ptr<arrow::io::OutputStream> outstream; |
| 48 | + if (OutputPath.empty()) { |
| 49 | + outstream = std::make_shared<arrow::io::StdoutStream>(); |
| 50 | + } else { |
| 51 | + if (auto parent = TFsPath(OutputPath.c_str()).Parent()) { |
| 52 | + parent.MkDirs(); |
| 53 | + } |
| 54 | + outstream = *arrow::io::FileOutputStream::Open(OutputPath); |
| 55 | + } |
| 56 | + Stream = std::make_unique<parquet::StreamWriter>(parquet::ParquetFileWriter::Open(outstream, schema, builder.build())); |
| 57 | + Stream->SetMaxRowGroupSize(RowGroupSize); |
| 58 | + } |
| 59 | + |
| 60 | + parquet::schema::NodePtr TResultSetParquetPrinter::TImpl::ToParquetType(const char* name, const TTypeParser& type, bool nullable) { |
| 61 | + if (type.GetKind() != TTypeParser::ETypeKind::Primitive) { |
| 62 | + ythrow yexception() << "Cannot save not primitive type to parquet: " << type.GetKind(); |
| 63 | + } |
| 64 | + const auto repType = nullable ? parquet::Repetition::OPTIONAL : parquet::Repetition::REQUIRED; |
| 65 | + switch (type.GetPrimitive()) { |
| 66 | + case EPrimitiveType::Bool: |
| 67 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::BOOLEAN); |
| 68 | + case EPrimitiveType::Int8: |
| 69 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::INT32, parquet::ConvertedType::INT_8); |
| 70 | + case EPrimitiveType::Uint8: |
| 71 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::INT32, parquet::ConvertedType::UINT_8); |
| 72 | + case EPrimitiveType::Int16: |
| 73 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::INT32, parquet::ConvertedType::INT_16); |
| 74 | + case EPrimitiveType::Uint16: |
| 75 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::INT32, parquet::ConvertedType::UINT_16); |
| 76 | + case EPrimitiveType::Int32: |
| 77 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::INT32, parquet::ConvertedType::INT_32); |
| 78 | + case EPrimitiveType::Uint32: |
| 79 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::INT32, parquet::ConvertedType::UINT_32); |
| 80 | + case EPrimitiveType::Int64: |
| 81 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::INT64, parquet::ConvertedType::INT_64); |
| 82 | + case EPrimitiveType::Uint64: |
| 83 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::INT64, parquet::ConvertedType::UINT_64); |
| 84 | + case EPrimitiveType::Float: |
| 85 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::FLOAT); |
| 86 | + case EPrimitiveType::Double: |
| 87 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::DOUBLE); |
| 88 | + case EPrimitiveType::Date: |
| 89 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::INT32, parquet::ConvertedType::UINT_32); |
| 90 | + case EPrimitiveType::Timestamp: |
| 91 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::INT64, parquet::ConvertedType::INT_64); |
| 92 | + case EPrimitiveType::Interval: |
| 93 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::INT64, parquet::ConvertedType::INT_64); |
| 94 | + case EPrimitiveType::String: |
| 95 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::BYTE_ARRAY, parquet::ConvertedType::UTF8); |
| 96 | + case EPrimitiveType::Utf8: |
| 97 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::BYTE_ARRAY, parquet::ConvertedType::UTF8); |
| 98 | + case EPrimitiveType::Yson: |
| 99 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::BYTE_ARRAY, parquet::ConvertedType::UTF8); |
| 100 | + case EPrimitiveType::Json: |
| 101 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::BYTE_ARRAY, parquet::ConvertedType::UTF8); |
| 102 | + case EPrimitiveType::JsonDocument: |
| 103 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::BYTE_ARRAY, parquet::ConvertedType::UTF8); |
| 104 | + case EPrimitiveType::DyNumber: |
| 105 | + return parquet::schema::PrimitiveNode::Make(name, repType, parquet::Type::BYTE_ARRAY, parquet::ConvertedType::UTF8); |
| 106 | + default: |
| 107 | + ythrow yexception() << "Cannot save type to parquet: " << type.GetPrimitive(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + TResultSetParquetPrinter::TImpl::TImpl(const std::string& outputPath, ui64 rowGroupSize) |
| 112 | + : OutputPath(outputPath) |
| 113 | + , RowGroupSize(rowGroupSize) |
| 114 | + {} |
| 115 | + |
| 116 | + void TResultSetParquetPrinter::TImpl::Reset() { |
| 117 | + Stream.reset(); |
| 118 | + } |
| 119 | + |
| 120 | + void TResultSetParquetPrinter::TImpl::Print(const TResultSet& resultSet) { |
| 121 | + if (!Stream) { |
| 122 | + InitStream(resultSet); |
| 123 | + } |
| 124 | + auto& os = *Stream; |
| 125 | + TResultSetParser parser(resultSet); |
| 126 | + while (parser.TryNextRow()) { |
| 127 | + for (ui32 i = 0; i < resultSet.GetColumnsMeta().size(); ++i) { |
| 128 | + TValueParser value(parser.GetValue(i)); |
| 129 | + bool nullable = value.GetKind() == TTypeParser::ETypeKind::Optional; |
| 130 | + if (nullable) { |
| 131 | + value.OpenOptional(); |
| 132 | + if (value.IsNull()) { |
| 133 | + os.SkipColumns(1); |
| 134 | + continue; |
| 135 | + } |
| 136 | + } |
| 137 | + if (value.GetKind() != TTypeParser::ETypeKind::Primitive) { |
| 138 | + ythrow yexception() << "Cannot save not primitive type to parquet: " << value.GetKind(); |
| 139 | + } |
| 140 | + switch (value.GetPrimitiveType()) { |
| 141 | + case EPrimitiveType::Bool: |
| 142 | + os << value.GetBool(); |
| 143 | + break; |
| 144 | + case EPrimitiveType::Int8: |
| 145 | + os << value.GetInt8(); |
| 146 | + break; |
| 147 | + case EPrimitiveType::Uint8: |
| 148 | + os << value.GetUint8(); |
| 149 | + break; |
| 150 | + case EPrimitiveType::Int16: |
| 151 | + os << value.GetInt16(); |
| 152 | + break; |
| 153 | + case EPrimitiveType::Uint16: |
| 154 | + os << value.GetUint16(); |
| 155 | + break; |
| 156 | + case EPrimitiveType::Int32: |
| 157 | + os << value.GetInt32(); |
| 158 | + break; |
| 159 | + case EPrimitiveType::Uint32: |
| 160 | + os << value.GetUint32(); |
| 161 | + break; |
| 162 | + case EPrimitiveType::Int64: |
| 163 | + os << (std::int64_t)value.GetInt64(); |
| 164 | + break; |
| 165 | + case EPrimitiveType::Uint64: |
| 166 | + os << (std::uint64_t)value.GetUint64(); |
| 167 | + break; |
| 168 | + case EPrimitiveType::Float: |
| 169 | + os << value.GetFloat(); |
| 170 | + break; |
| 171 | + case EPrimitiveType::Double: |
| 172 | + os << value.GetDouble(); |
| 173 | + break; |
| 174 | + case EPrimitiveType::Date: |
| 175 | + os << (ui32)value.GetDate().Seconds(); |
| 176 | + break; |
| 177 | + case EPrimitiveType::Timestamp: |
| 178 | + os << (std::int64_t)value.GetTimestamp().MicroSeconds(); |
| 179 | + break; |
| 180 | + case EPrimitiveType::Interval: |
| 181 | + os << (std::int64_t)value.GetInterval(); |
| 182 | + break; |
| 183 | + case EPrimitiveType::String: |
| 184 | + os << arrow::util::string_view(value.GetString().c_str(), value.GetString().length()); |
| 185 | + break; |
| 186 | + case EPrimitiveType::Utf8: |
| 187 | + os << arrow::util::string_view(value.GetUtf8().c_str(), value.GetUtf8().length()); |
| 188 | + break; |
| 189 | + case EPrimitiveType::Yson: |
| 190 | + os << arrow::util::string_view(value.GetYson().c_str(), value.GetYson().length()); |
| 191 | + break; |
| 192 | + case EPrimitiveType::Json: |
| 193 | + os << arrow::util::string_view(value.GetJson().c_str(), value.GetJson().length()); |
| 194 | + break; |
| 195 | + case EPrimitiveType::JsonDocument: |
| 196 | + os << arrow::util::string_view(value.GetJsonDocument().c_str(), value.GetJsonDocument().length()); |
| 197 | + break; |
| 198 | + case EPrimitiveType::DyNumber: |
| 199 | + os << arrow::util::string_view(value.GetDyNumber().c_str(), value.GetDyNumber().length()); |
| 200 | + break; |
| 201 | + default: |
| 202 | + ythrow yexception() << "Cannot save type to parquet: " << value.GetPrimitiveType(); |
| 203 | + } |
| 204 | + } |
| 205 | + os.EndRow(); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + TResultSetParquetPrinter::TResultSetParquetPrinter(const std::string& outputPath, ui64 rowGroupSize /*= 100000*/) |
| 210 | + : Impl(std::make_unique<TImpl>(outputPath, rowGroupSize)) |
| 211 | + {} |
| 212 | + |
| 213 | + TResultSetParquetPrinter::~TResultSetParquetPrinter() { |
| 214 | + } |
| 215 | + |
| 216 | + void TResultSetParquetPrinter::Reset() { |
| 217 | + Impl->Reset(); |
| 218 | + } |
| 219 | + |
| 220 | + void TResultSetParquetPrinter::Print(const TResultSet& resultSet) { |
| 221 | + Impl->Print(resultSet); |
| 222 | + } |
| 223 | + |
| 224 | +} |
0 commit comments