parquet 0.0.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +50 -0
- data/README.md +92 -2
- data/ext/parquet/Cargo.toml +1 -0
- data/ext/parquet/src/lib.rs +5 -3
- data/ext/parquet/src/{reader.rs → reader/mod.rs} +5 -2
- data/ext/parquet/src/types/core_types.rs +73 -0
- data/ext/parquet/src/types/mod.rs +30 -0
- data/ext/parquet/src/types/parquet_value.rs +462 -0
- data/ext/parquet/src/types/record_types.rs +204 -0
- data/ext/parquet/src/types/timestamp.rs +85 -0
- data/ext/parquet/src/types/type_conversion.rs +753 -0
- data/ext/parquet/src/types/writer_types.rs +275 -0
- data/ext/parquet/src/utils.rs +16 -5
- data/ext/parquet/src/writer/mod.rs +403 -0
- data/lib/parquet/version.rb +1 -1
- data/lib/parquet.rbi +33 -2
- metadata +13 -6
- data/ext/parquet/src/types.rs +0 -763
- /data/ext/parquet/src/{parquet_column_reader.rs → reader/parquet_column_reader.rs} +0 -0
- /data/ext/parquet/src/{parquet_row_reader.rs → reader/parquet_row_reader.rs} +0 -0
@@ -0,0 +1,275 @@
|
|
1
|
+
use std::{
|
2
|
+
io::{self, Write},
|
3
|
+
str::FromStr,
|
4
|
+
sync::Arc,
|
5
|
+
};
|
6
|
+
|
7
|
+
use arrow_array::{Array, RecordBatch};
|
8
|
+
use magnus::{value::ReprValue, Error as MagnusError, RString, Ruby, Symbol, TryConvert, Value};
|
9
|
+
use parquet::{arrow::ArrowWriter, errors::ParquetError};
|
10
|
+
use tempfile::NamedTempFile;
|
11
|
+
|
12
|
+
use crate::types::{ListField, MapField, ParquetSchemaType};
|
13
|
+
|
14
|
+
#[derive(Debug)]
|
15
|
+
pub struct SchemaField {
|
16
|
+
pub name: String,
|
17
|
+
pub type_: ParquetSchemaType,
|
18
|
+
}
|
19
|
+
|
20
|
+
#[derive(Debug)]
|
21
|
+
pub struct ParquetWriteArgs {
|
22
|
+
pub read_from: Value,
|
23
|
+
pub write_to: Value,
|
24
|
+
pub schema: Vec<SchemaField>,
|
25
|
+
pub batch_size: Option<usize>,
|
26
|
+
}
|
27
|
+
|
28
|
+
pub trait SendableWrite: Send + Write {}
|
29
|
+
impl<T: Send + Write> SendableWrite for T {}
|
30
|
+
|
31
|
+
pub struct IoLikeValue(pub(crate) Value);
|
32
|
+
|
33
|
+
impl Write for IoLikeValue {
|
34
|
+
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
|
35
|
+
let ruby_bytes = RString::from_slice(buf);
|
36
|
+
|
37
|
+
let bytes_written = self
|
38
|
+
.0
|
39
|
+
.funcall::<_, _, usize>("write", (ruby_bytes,))
|
40
|
+
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
|
41
|
+
|
42
|
+
Ok(bytes_written)
|
43
|
+
}
|
44
|
+
|
45
|
+
fn flush(&mut self) -> Result<(), io::Error> {
|
46
|
+
self.0
|
47
|
+
.funcall::<_, _, Value>("flush", ())
|
48
|
+
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
|
49
|
+
|
50
|
+
Ok(())
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
impl FromStr for ParquetSchemaType {
|
55
|
+
type Err = MagnusError;
|
56
|
+
|
57
|
+
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
58
|
+
match s {
|
59
|
+
"int8" => Ok(ParquetSchemaType::Int8),
|
60
|
+
"int16" => Ok(ParquetSchemaType::Int16),
|
61
|
+
"int32" => Ok(ParquetSchemaType::Int32),
|
62
|
+
"int64" => Ok(ParquetSchemaType::Int64),
|
63
|
+
"uint8" => Ok(ParquetSchemaType::UInt8),
|
64
|
+
"uint16" => Ok(ParquetSchemaType::UInt16),
|
65
|
+
"uint32" => Ok(ParquetSchemaType::UInt32),
|
66
|
+
"uint64" => Ok(ParquetSchemaType::UInt64),
|
67
|
+
"float" | "float32" => Ok(ParquetSchemaType::Float),
|
68
|
+
"double" | "float64" => Ok(ParquetSchemaType::Double),
|
69
|
+
"string" | "utf8" => Ok(ParquetSchemaType::String),
|
70
|
+
"binary" => Ok(ParquetSchemaType::Binary),
|
71
|
+
"boolean" | "bool" => Ok(ParquetSchemaType::Boolean),
|
72
|
+
"date32" => Ok(ParquetSchemaType::Date32),
|
73
|
+
"timestamp_millis" => Ok(ParquetSchemaType::TimestampMillis),
|
74
|
+
"timestamp_micros" => Ok(ParquetSchemaType::TimestampMicros),
|
75
|
+
"list" => Ok(ParquetSchemaType::List(Box::new(ListField {
|
76
|
+
item_type: ParquetSchemaType::Int8,
|
77
|
+
}))),
|
78
|
+
"map" => Ok(ParquetSchemaType::Map(Box::new(MapField {
|
79
|
+
key_type: ParquetSchemaType::String,
|
80
|
+
value_type: ParquetSchemaType::Int8,
|
81
|
+
}))),
|
82
|
+
_ => Err(MagnusError::new(
|
83
|
+
magnus::exception::runtime_error(),
|
84
|
+
format!("Invalid schema type: {}", s),
|
85
|
+
)),
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
impl TryConvert for ParquetSchemaType {
|
91
|
+
fn try_convert(value: Value) -> Result<Self, MagnusError> {
|
92
|
+
let ruby = unsafe { Ruby::get_unchecked() };
|
93
|
+
let schema_type = parse_string_or_symbol(&ruby, value)?;
|
94
|
+
|
95
|
+
schema_type.unwrap().parse()
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
// We know this type is safe to move between threads because it's just an enum
|
100
|
+
// with simple primitive types and strings
|
101
|
+
unsafe impl Send for ParquetSchemaType {}
|
102
|
+
|
103
|
+
fn parse_string_or_symbol(ruby: &Ruby, value: Value) -> Result<Option<String>, MagnusError> {
|
104
|
+
if value.is_nil() {
|
105
|
+
Ok(None)
|
106
|
+
} else if value.is_kind_of(ruby.class_string()) {
|
107
|
+
RString::from_value(value)
|
108
|
+
.ok_or_else(|| {
|
109
|
+
MagnusError::new(magnus::exception::type_error(), "Invalid string value")
|
110
|
+
})?
|
111
|
+
.to_string()
|
112
|
+
.map(|s| Some(s))
|
113
|
+
} else if value.is_kind_of(ruby.class_symbol()) {
|
114
|
+
Symbol::from_value(value)
|
115
|
+
.ok_or_else(|| {
|
116
|
+
MagnusError::new(magnus::exception::type_error(), "Invalid symbol value")
|
117
|
+
})?
|
118
|
+
.funcall("to_s", ())
|
119
|
+
.map(|s| Some(s))
|
120
|
+
} else {
|
121
|
+
Err(MagnusError::new(
|
122
|
+
magnus::exception::type_error(),
|
123
|
+
"Value must be a String or Symbol",
|
124
|
+
))
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
pub enum WriterOutput {
|
129
|
+
File(ArrowWriter<Box<dyn SendableWrite>>),
|
130
|
+
TempFile(ArrowWriter<Box<dyn SendableWrite>>, NamedTempFile),
|
131
|
+
}
|
132
|
+
|
133
|
+
impl WriterOutput {
|
134
|
+
pub fn write(&mut self, batch: &RecordBatch) -> Result<(), ParquetError> {
|
135
|
+
match self {
|
136
|
+
WriterOutput::File(writer) | WriterOutput::TempFile(writer, _) => writer.write(batch),
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
pub fn close(self) -> Result<Option<NamedTempFile>, ParquetError> {
|
141
|
+
match self {
|
142
|
+
WriterOutput::File(writer) => {
|
143
|
+
writer.close()?;
|
144
|
+
Ok(None)
|
145
|
+
}
|
146
|
+
WriterOutput::TempFile(writer, temp_file) => {
|
147
|
+
writer.close()?;
|
148
|
+
Ok(Some(temp_file))
|
149
|
+
}
|
150
|
+
}
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
pub struct ParquetErrorWrapper(pub ParquetError);
|
155
|
+
|
156
|
+
impl From<ParquetErrorWrapper> for MagnusError {
|
157
|
+
fn from(err: ParquetErrorWrapper) -> Self {
|
158
|
+
MagnusError::new(
|
159
|
+
magnus::exception::runtime_error(),
|
160
|
+
format!("Parquet error: {}", err.0),
|
161
|
+
)
|
162
|
+
}
|
163
|
+
}
|
164
|
+
|
165
|
+
pub struct ColumnCollector {
|
166
|
+
pub name: String,
|
167
|
+
pub type_: ParquetSchemaType,
|
168
|
+
pub values: Vec<crate::types::ParquetValue>,
|
169
|
+
}
|
170
|
+
|
171
|
+
impl ColumnCollector {
|
172
|
+
pub fn new(name: String, type_: ParquetSchemaType) -> Self {
|
173
|
+
Self {
|
174
|
+
name,
|
175
|
+
type_,
|
176
|
+
values: Vec::new(),
|
177
|
+
}
|
178
|
+
}
|
179
|
+
|
180
|
+
pub fn push_value(&mut self, value: Value) -> Result<(), MagnusError> {
|
181
|
+
use crate::types::ParquetValue;
|
182
|
+
use crate::{
|
183
|
+
convert_to_binary, convert_to_boolean, convert_to_date32, convert_to_list,
|
184
|
+
convert_to_map, convert_to_timestamp_micros, convert_to_timestamp_millis,
|
185
|
+
NumericConverter,
|
186
|
+
};
|
187
|
+
|
188
|
+
if value.is_nil() {
|
189
|
+
self.values.push(ParquetValue::Null);
|
190
|
+
return Ok(());
|
191
|
+
}
|
192
|
+
|
193
|
+
let parquet_value = match &self.type_ {
|
194
|
+
ParquetSchemaType::Int8 => {
|
195
|
+
let v = NumericConverter::<i8>::convert_with_string_fallback(value)?;
|
196
|
+
ParquetValue::Int8(v)
|
197
|
+
}
|
198
|
+
ParquetSchemaType::Int16 => {
|
199
|
+
let v = NumericConverter::<i16>::convert_with_string_fallback(value)?;
|
200
|
+
ParquetValue::Int16(v)
|
201
|
+
}
|
202
|
+
ParquetSchemaType::Int32 => {
|
203
|
+
let v = NumericConverter::<i32>::convert_with_string_fallback(value)?;
|
204
|
+
ParquetValue::Int32(v)
|
205
|
+
}
|
206
|
+
ParquetSchemaType::Int64 => {
|
207
|
+
let v = NumericConverter::<i64>::convert_with_string_fallback(value)?;
|
208
|
+
ParquetValue::Int64(v)
|
209
|
+
}
|
210
|
+
ParquetSchemaType::UInt8 => {
|
211
|
+
let v = NumericConverter::<u8>::convert_with_string_fallback(value)?;
|
212
|
+
ParquetValue::UInt8(v)
|
213
|
+
}
|
214
|
+
ParquetSchemaType::UInt16 => {
|
215
|
+
let v = NumericConverter::<u16>::convert_with_string_fallback(value)?;
|
216
|
+
ParquetValue::UInt16(v)
|
217
|
+
}
|
218
|
+
ParquetSchemaType::UInt32 => {
|
219
|
+
let v = NumericConverter::<u32>::convert_with_string_fallback(value)?;
|
220
|
+
ParquetValue::UInt32(v)
|
221
|
+
}
|
222
|
+
ParquetSchemaType::UInt64 => {
|
223
|
+
let v = NumericConverter::<u64>::convert_with_string_fallback(value)?;
|
224
|
+
ParquetValue::UInt64(v)
|
225
|
+
}
|
226
|
+
ParquetSchemaType::Float => {
|
227
|
+
let v = NumericConverter::<f32>::convert_with_string_fallback(value)?;
|
228
|
+
ParquetValue::Float32(v)
|
229
|
+
}
|
230
|
+
ParquetSchemaType::Double => {
|
231
|
+
let v = NumericConverter::<f64>::convert_with_string_fallback(value)?;
|
232
|
+
ParquetValue::Float64(v)
|
233
|
+
}
|
234
|
+
ParquetSchemaType::String => {
|
235
|
+
let v = String::try_convert(value)?;
|
236
|
+
ParquetValue::String(v)
|
237
|
+
}
|
238
|
+
ParquetSchemaType::Binary => {
|
239
|
+
let v = convert_to_binary(value)?;
|
240
|
+
ParquetValue::Bytes(v)
|
241
|
+
}
|
242
|
+
ParquetSchemaType::Boolean => {
|
243
|
+
let v = convert_to_boolean(value)?;
|
244
|
+
ParquetValue::Boolean(v)
|
245
|
+
}
|
246
|
+
ParquetSchemaType::Date32 => {
|
247
|
+
let v = convert_to_date32(value)?;
|
248
|
+
ParquetValue::Date32(v)
|
249
|
+
}
|
250
|
+
ParquetSchemaType::TimestampMillis => {
|
251
|
+
let v = convert_to_timestamp_millis(value)?;
|
252
|
+
ParquetValue::TimestampMillis(v, None)
|
253
|
+
}
|
254
|
+
ParquetSchemaType::TimestampMicros => {
|
255
|
+
let v = convert_to_timestamp_micros(value)?;
|
256
|
+
ParquetValue::TimestampMicros(v, None)
|
257
|
+
}
|
258
|
+
ParquetSchemaType::List(list_field) => {
|
259
|
+
let values = convert_to_list(value, list_field)?;
|
260
|
+
ParquetValue::List(values)
|
261
|
+
}
|
262
|
+
ParquetSchemaType::Map(map_field) => {
|
263
|
+
let map = convert_to_map(value, map_field)?;
|
264
|
+
ParquetValue::Map(map)
|
265
|
+
}
|
266
|
+
};
|
267
|
+
self.values.push(parquet_value);
|
268
|
+
Ok(())
|
269
|
+
}
|
270
|
+
|
271
|
+
pub fn take_array(&mut self) -> Result<Arc<dyn Array>, MagnusError> {
|
272
|
+
let values = std::mem::take(&mut self.values);
|
273
|
+
crate::convert_parquet_values_to_arrow(values, &self.type_)
|
274
|
+
}
|
275
|
+
}
|
data/ext/parquet/src/utils.rs
CHANGED
@@ -39,7 +39,7 @@ pub fn parse_parquet_rows_args(ruby: &Ruby, args: &[Value]) -> Result<ParquetRow
|
|
39
39
|
let parsed_args = scan_args::<(Value,), (), (), (), _, ()>(args)?;
|
40
40
|
let (to_read,) = parsed_args.required;
|
41
41
|
|
42
|
-
let kwargs = get_kwargs::<_, (), (Option<Value
|
42
|
+
let kwargs = get_kwargs::<_, (), (Option<Option<Value>>, Option<Option<Vec<String>>>), ()>(
|
43
43
|
parsed_args.keywords,
|
44
44
|
&[],
|
45
45
|
&["result_type", "columns"],
|
@@ -48,6 +48,7 @@ pub fn parse_parquet_rows_args(ruby: &Ruby, args: &[Value]) -> Result<ParquetRow
|
|
48
48
|
let result_type: ParserResultType = match kwargs
|
49
49
|
.optional
|
50
50
|
.0
|
51
|
+
.flatten()
|
51
52
|
.map(|value| parse_string_or_symbol(ruby, value))
|
52
53
|
{
|
53
54
|
Some(Ok(Some(parsed))) => parsed.try_into().map_err(|e| {
|
@@ -75,7 +76,7 @@ pub fn parse_parquet_rows_args(ruby: &Ruby, args: &[Value]) -> Result<ParquetRow
|
|
75
76
|
Ok(ParquetRowsArgs {
|
76
77
|
to_read,
|
77
78
|
result_type,
|
78
|
-
columns: kwargs.optional.1,
|
79
|
+
columns: kwargs.optional.1.flatten(),
|
79
80
|
})
|
80
81
|
}
|
81
82
|
|
@@ -95,7 +96,16 @@ pub fn parse_parquet_columns_args(
|
|
95
96
|
let parsed_args = scan_args::<(Value,), (), (), (), _, ()>(args)?;
|
96
97
|
let (to_read,) = parsed_args.required;
|
97
98
|
|
98
|
-
let kwargs = get_kwargs::<
|
99
|
+
let kwargs = get_kwargs::<
|
100
|
+
_,
|
101
|
+
(),
|
102
|
+
(
|
103
|
+
Option<Option<Value>>,
|
104
|
+
Option<Option<Vec<String>>>,
|
105
|
+
Option<Option<usize>>,
|
106
|
+
),
|
107
|
+
(),
|
108
|
+
>(
|
99
109
|
parsed_args.keywords,
|
100
110
|
&[],
|
101
111
|
&["result_type", "columns", "batch_size"],
|
@@ -104,6 +114,7 @@ pub fn parse_parquet_columns_args(
|
|
104
114
|
let result_type: ParserResultType = match kwargs
|
105
115
|
.optional
|
106
116
|
.0
|
117
|
+
.flatten()
|
107
118
|
.map(|value| parse_string_or_symbol(ruby, value))
|
108
119
|
{
|
109
120
|
Some(Ok(Some(parsed))) => parsed.try_into().map_err(|e| {
|
@@ -131,7 +142,7 @@ pub fn parse_parquet_columns_args(
|
|
131
142
|
Ok(ParquetColumnsArgs {
|
132
143
|
to_read,
|
133
144
|
result_type,
|
134
|
-
columns: kwargs.optional.1,
|
135
|
-
batch_size: kwargs.optional.2,
|
145
|
+
columns: kwargs.optional.1.flatten(),
|
146
|
+
batch_size: kwargs.optional.2.flatten(),
|
136
147
|
})
|
137
148
|
}
|