forked from apache/datafusion-comet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparquet_support.rs
More file actions
607 lines (559 loc) · 23 KB
/
parquet_support.rs
File metadata and controls
607 lines (559 loc) · 23 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// 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.
use crate::execution::operators::ExecutionError;
use arrow::array::{FixedSizeBinaryArray, ListArray, MapArray, StringArray};
use arrow::buffer::NullBuffer;
use arrow::compute::can_cast_types;
use arrow::datatypes::{FieldRef, Fields};
use arrow::{
array::{
cast::AsArray, new_null_array, types::Int32Type, types::TimestampMicrosecondType, Array,
ArrayRef, DictionaryArray, StructArray,
},
compute::{cast_with_options, take, CastOptions},
datatypes::{DataType, TimeUnit},
util::display::FormatOptions,
};
use datafusion::common::{Result as DataFusionResult, ScalarValue};
use datafusion::error::DataFusionError;
use datafusion::execution::object_store::ObjectStoreUrl;
use datafusion::execution::runtime_env::RuntimeEnv;
use datafusion::physical_plan::ColumnarValue;
use datafusion_comet_spark_expr::EvalMode;
use object_store::path::Path;
use object_store::{parse_url, ObjectStore};
use std::collections::HashMap;
use std::time::Duration;
use std::{fmt::Debug, hash::Hash, sync::Arc};
use url::Url;
use super::objectstore;
// This file originates from cast.rs. While developing native scan support and implementing
// SparkSchemaAdapter we observed that Spark's type conversion logic on Parquet reads does not
// always align to the CAST expression's logic, so it was duplicated here to adapt its behavior.
static TIMESTAMP_FORMAT: Option<&str> = Some("%Y-%m-%d %H:%M:%S%.f");
static PARQUET_OPTIONS: CastOptions = CastOptions {
safe: true,
format_options: FormatOptions::new()
.with_timestamp_tz_format(TIMESTAMP_FORMAT)
.with_timestamp_format(TIMESTAMP_FORMAT),
};
/// Spark Parquet type conversion options
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct SparkParquetOptions {
/// Spark evaluation mode
pub eval_mode: EvalMode,
/// When cast from/to timezone related types, we need timezone, which will be resolved with
/// session local timezone by an analyzer in Spark.
// TODO we should change timezone to Tz to avoid repeated parsing
pub timezone: String,
/// Allow casts that are supported but not guaranteed to be 100% compatible
pub allow_incompat: bool,
/// Support casting unsigned ints to signed ints (used by Parquet SchemaAdapter)
pub allow_cast_unsigned_ints: bool,
/// Whether to always represent decimals using 128 bits. If false, the native reader may represent decimals using 32 or 64 bits, depending on the precision.
pub use_decimal_128: bool,
/// Whether to read dates/timestamps that were written in the legacy hybrid Julian + Gregorian calendar as it is. If false, throw exceptions instead. If the spark type is TimestampNTZ, this should be true.
pub use_legacy_date_timestamp_or_ntz: bool,
// Whether schema field names are case sensitive
pub case_sensitive: bool,
/// Whether to validate schema compatibility (type coercions) in a Spark-compatible way
pub schema_validation_enabled: bool,
/// Whether schema evolution (type widening) is enabled
pub schema_evolution_enabled: bool,
}
impl SparkParquetOptions {
pub fn new(eval_mode: EvalMode, timezone: &str, allow_incompat: bool) -> Self {
Self {
eval_mode,
timezone: timezone.to_string(),
allow_incompat,
allow_cast_unsigned_ints: false,
use_decimal_128: false,
use_legacy_date_timestamp_or_ntz: false,
case_sensitive: false,
schema_validation_enabled: true,
schema_evolution_enabled: false,
}
}
pub fn new_without_timezone(eval_mode: EvalMode, allow_incompat: bool) -> Self {
Self {
eval_mode,
timezone: "".to_string(),
allow_incompat,
allow_cast_unsigned_ints: false,
use_decimal_128: false,
use_legacy_date_timestamp_or_ntz: false,
case_sensitive: false,
schema_validation_enabled: true,
schema_evolution_enabled: false,
}
}
}
/// Spark-compatible cast implementation. Defers to DataFusion's cast where that is known
/// to be compatible, and returns an error when a not supported and not DF-compatible cast
/// is requested.
pub fn spark_parquet_convert(
arg: ColumnarValue,
data_type: &DataType,
parquet_options: &SparkParquetOptions,
) -> DataFusionResult<ColumnarValue> {
match arg {
ColumnarValue::Array(array) => Ok(ColumnarValue::Array(parquet_convert_array(
array,
data_type,
parquet_options,
)?)),
ColumnarValue::Scalar(scalar) => {
// Note that normally CAST(scalar) should be fold in Spark JVM side. However, for
// some cases e.g., scalar subquery, Spark will not fold it, so we need to handle it
// here.
let array = scalar.to_array()?;
let scalar = ScalarValue::try_from_array(
&parquet_convert_array(array, data_type, parquet_options)?,
0,
)?;
Ok(ColumnarValue::Scalar(scalar))
}
}
}
fn parquet_convert_array(
array: ArrayRef,
to_type: &DataType,
parquet_options: &SparkParquetOptions,
) -> DataFusionResult<ArrayRef> {
use DataType::*;
let from_type = array.data_type().clone();
let array = match &from_type {
Dictionary(key_type, value_type)
if key_type.as_ref() == &Int32
&& (value_type.as_ref() == &Utf8 || value_type.as_ref() == &LargeUtf8) =>
{
let dict_array = array
.as_any()
.downcast_ref::<DictionaryArray<Int32Type>>()
.expect("Expected a dictionary array");
let casted_dictionary = DictionaryArray::<Int32Type>::new(
dict_array.keys().clone(),
parquet_convert_array(Arc::clone(dict_array.values()), to_type, parquet_options)?,
);
let casted_result = match to_type {
Dictionary(_, _) => Arc::new(casted_dictionary.clone()),
_ => take(casted_dictionary.values().as_ref(), dict_array.keys(), None)?,
};
return Ok(casted_result);
}
_ => array,
};
let from_type = array.data_type();
// Try Comet specific handlers first, then arrow-rs cast if supported,
// return uncasted data otherwise
match (from_type, to_type) {
(Struct(_), Struct(_)) => Ok(parquet_convert_struct_to_struct(
array.as_struct(),
from_type,
to_type,
parquet_options,
)?),
(List(_), List(to_inner_type)) => {
let list_arr: &ListArray = array.as_list();
let cast_field = parquet_convert_array(
Arc::clone(list_arr.values()),
to_inner_type.data_type(),
parquet_options,
)?;
Ok(Arc::new(ListArray::new(
Arc::clone(to_inner_type),
list_arr.offsets().clone(),
cast_field,
list_arr.nulls().cloned(),
)))
}
(Timestamp(TimeUnit::Microsecond, None), Timestamp(TimeUnit::Microsecond, Some(tz))) => {
Ok(Arc::new(
array
.as_primitive::<TimestampMicrosecondType>()
.reinterpret_cast::<TimestampMicrosecondType>()
.with_timezone(Arc::clone(tz)),
))
}
(Map(_, ordered_from), Map(_, ordered_to)) if ordered_from == ordered_to =>
parquet_convert_map_to_map(array.as_map(), to_type, parquet_options, *ordered_to)
,
// Iceberg stores UUIDs as 16-byte fixed binary but Spark expects string representation.
// Arrow doesn't support casting FixedSizeBinary to Utf8, so we handle it manually.
(FixedSizeBinary(16), Utf8) => {
let binary_array = array
.as_any()
.downcast_ref::<FixedSizeBinaryArray>()
.expect("Expected a FixedSizeBinaryArray");
let string_array: StringArray = binary_array
.iter()
.map(|opt_bytes| {
opt_bytes.map(|bytes| {
let uuid = uuid::Uuid::from_bytes(
bytes.try_into().expect("Expected 16 bytes")
);
uuid.to_string()
})
})
.collect();
Ok(Arc::new(string_array))
}
// If Arrow cast supports the cast, delegate the cast to Arrow
_ if can_cast_types(from_type, to_type) => {
Ok(cast_with_options(&array, to_type, &PARQUET_OPTIONS)?)
}
_ => Ok(array),
}
}
/// Cast between struct types based on logic in
/// `org.apache.spark.sql.catalyst.expressions.Cast#castStruct`.
fn parquet_convert_struct_to_struct(
array: &StructArray,
from_type: &DataType,
to_type: &DataType,
parquet_options: &SparkParquetOptions,
) -> DataFusionResult<ArrayRef> {
match (from_type, to_type) {
(DataType::Struct(from_fields), DataType::Struct(to_fields)) => {
// if dest and target schemas has any column in common
let mut field_overlap = false;
// TODO some of this logic may be specific to converting Parquet to Spark
let mut field_name_to_index_map = HashMap::new();
for (i, field) in from_fields.iter().enumerate() {
if parquet_options.case_sensitive {
field_name_to_index_map.insert(field.name().clone(), i);
} else {
field_name_to_index_map.insert(field.name().to_lowercase(), i);
}
}
assert_eq!(field_name_to_index_map.len(), from_fields.len());
let mut cast_fields: Vec<ArrayRef> = Vec::with_capacity(to_fields.len());
for i in 0..to_fields.len() {
// Fields in the to_type schema may not exist in the from_type schema
// i.e. the required schema may have fields that the file does not
// have
let key = if parquet_options.case_sensitive {
to_fields[i].name().clone()
} else {
to_fields[i].name().to_lowercase()
};
if field_name_to_index_map.contains_key(&key) {
let from_index = field_name_to_index_map[&key];
let cast_field = parquet_convert_array(
Arc::clone(array.column(from_index)),
to_fields[i].data_type(),
parquet_options,
)?;
cast_fields.push(cast_field);
field_overlap = true;
} else {
cast_fields.push(new_null_array(to_fields[i].data_type(), array.len()));
}
}
// If target schema doesn't contain any of the existing fields
// mark such a column in array as NULL
let nulls = if field_overlap {
array.nulls().cloned()
} else {
Some(NullBuffer::new_null(array.len()))
};
Ok(Arc::new(StructArray::new(
to_fields.clone(),
cast_fields,
nulls,
)))
}
_ => unreachable!(),
}
}
/// Cast a map type to another map type. The same as arrow-cast except we recursively call our own
/// parquet_convert_array
fn parquet_convert_map_to_map(
from: &MapArray,
to_data_type: &DataType,
parquet_options: &SparkParquetOptions,
to_ordered: bool,
) -> Result<ArrayRef, DataFusionError> {
match to_data_type {
DataType::Map(entries_field, _) => {
let key_field = key_field(entries_field).ok_or(DataFusionError::Internal(
"map is missing key field".to_string(),
))?;
let value_field = value_field(entries_field).ok_or(DataFusionError::Internal(
"map is missing value field".to_string(),
))?;
let key_array = parquet_convert_array(
Arc::clone(from.keys()),
key_field.data_type(),
parquet_options,
)?;
let value_array = parquet_convert_array(
Arc::clone(from.values()),
value_field.data_type(),
parquet_options,
)?;
Ok(Arc::new(MapArray::new(
Arc::<arrow::datatypes::Field>::clone(entries_field),
from.offsets().clone(),
StructArray::new(
Fields::from(vec![key_field, value_field]),
vec![key_array, value_array],
from.entries().nulls().cloned(),
),
from.nulls().cloned(),
to_ordered,
)))
}
dt => Err(DataFusionError::Internal(format!(
"Expected MapType. Got: {dt}"
))),
}
}
/// Gets the key field from the entries of a map. For all other types returns None.
fn key_field(entries_field: &FieldRef) -> Option<FieldRef> {
if let DataType::Struct(fields) = entries_field.data_type() {
fields.first().cloned()
} else {
None
}
}
/// Gets the value field from the entries of a map. For all other types returns None.
fn value_field(entries_field: &FieldRef) -> Option<FieldRef> {
if let DataType::Struct(fields) = entries_field.data_type() {
fields.get(1).cloned()
} else {
None
}
}
pub fn is_hdfs_scheme(url: &Url, object_store_configs: &HashMap<String, String>) -> bool {
const COMET_LIBHDFS_SCHEMES_KEY: &str = "fs.comet.libhdfs.schemes";
let scheme = url.scheme();
if let Some(libhdfs_schemes) = object_store_configs.get(COMET_LIBHDFS_SCHEMES_KEY) {
use itertools::Itertools;
libhdfs_schemes.split(",").contains(scheme)
} else {
scheme == "hdfs"
}
}
// Creates an HDFS object store from a URL using the native HDFS implementation
#[cfg(all(feature = "hdfs", not(feature = "hdfs-opendal")))]
fn create_hdfs_object_store(
url: &Url,
) -> Result<(Box<dyn ObjectStore>, Path), object_store::Error> {
match datafusion_comet_objectstore_hdfs::object_store::hdfs::HadoopFileSystem::new(url.as_ref())
{
Some(object_store) => {
let path = object_store.get_path(url.as_str());
Ok((Box::new(object_store), path))
}
_ => Err(object_store::Error::Generic {
store: "HadoopFileSystem",
source: "Could not create hdfs object store".into(),
}),
}
}
// Creates an OpenDAL HDFS Operator from a URL with optional configuration
#[cfg(feature = "hdfs-opendal")]
pub(crate) fn create_hdfs_operator(url: &Url) -> Result<opendal::Operator, object_store::Error> {
let name_node = get_name_node_uri(url)?;
let builder = opendal::services::Hdfs::default().name_node(&name_node);
opendal::Operator::new(builder)
.map_err(|error| object_store::Error::Generic {
store: "hdfs-opendal",
source: error.into(),
})
.map(|op| op.finish())
}
// Creates an HDFS object store from a URL using OpenDAL
#[cfg(feature = "hdfs-opendal")]
pub(crate) fn create_hdfs_object_store(
url: &Url,
) -> Result<(Box<dyn ObjectStore>, Path), object_store::Error> {
let op = create_hdfs_operator(url)?;
let store = object_store_opendal::OpendalStore::new(op);
let path = Path::parse(url.path())?;
Ok((Box::new(store), path))
}
#[cfg(feature = "hdfs-opendal")]
fn get_name_node_uri(url: &Url) -> Result<String, object_store::Error> {
use std::fmt::Write;
if let Some(host) = url.host() {
let schema = url.scheme();
let mut uri_builder = String::new();
write!(&mut uri_builder, "{schema}://{host}").unwrap();
if let Some(port) = url.port() {
write!(&mut uri_builder, ":{port}").unwrap();
}
Ok(uri_builder)
} else {
Err(object_store::Error::InvalidPath {
source: object_store::path::Error::InvalidPath {
path: std::path::PathBuf::from(url.as_str()),
},
})
}
}
// Stub implementation when HDFS support is not enabled
#[cfg(all(not(feature = "hdfs"), not(feature = "hdfs-opendal")))]
fn create_hdfs_object_store(
_url: &Url,
) -> Result<(Box<dyn ObjectStore>, Path), object_store::Error> {
Err(object_store::Error::Generic {
store: "HadoopFileSystem",
source: "Hdfs support is not enabled in this build".into(),
})
}
/// Parses the url, registers the object store with configurations, and returns a tuple of the object store url
/// and object store path
pub(crate) fn prepare_object_store_with_configs(
runtime_env: Arc<RuntimeEnv>,
url: String,
object_store_configs: &HashMap<String, String>,
) -> Result<(ObjectStoreUrl, Path), ExecutionError> {
let mut url = Url::parse(url.as_str())
.map_err(|e| ExecutionError::GeneralError(format!("Error parsing URL {url}: {e}")))?;
let is_hdfs_scheme = is_hdfs_scheme(&url, object_store_configs);
let mut scheme = url.scheme();
if !is_hdfs_scheme && scheme == "s3a" {
scheme = "s3";
url.set_scheme("s3").map_err(|_| {
ExecutionError::GeneralError("Could not convert scheme from s3a to s3".to_string())
})?;
}
let url_key = format!(
"{}://{}",
scheme,
&url[url::Position::BeforeHost..url::Position::AfterPort],
);
let (object_store, object_store_path): (Box<dyn ObjectStore>, Path) = if is_hdfs_scheme {
create_hdfs_object_store(&url)
} else if scheme == "s3" {
objectstore::s3::create_store(&url, object_store_configs, Duration::from_secs(300))
} else {
parse_url(&url)
}
.map_err(|e| ExecutionError::GeneralError(e.to_string()))?;
let object_store_url = ObjectStoreUrl::parse(url_key.clone())?;
runtime_env.register_object_store(&url, Arc::from(object_store));
Ok((object_store_url, object_store_path))
}
#[cfg(test)]
mod tests {
#[cfg(any(
all(not(feature = "hdfs"), not(feature = "hdfs-opendal")),
feature = "hdfs"
))]
use datafusion::execution::object_store::ObjectStoreUrl;
#[cfg(any(
all(not(feature = "hdfs"), not(feature = "hdfs-opendal")),
feature = "hdfs"
))]
use datafusion::execution::runtime_env::RuntimeEnv;
#[cfg(any(
all(not(feature = "hdfs"), not(feature = "hdfs-opendal")),
feature = "hdfs"
))]
use object_store::path::Path;
#[cfg(any(
all(not(feature = "hdfs"), not(feature = "hdfs-opendal")),
feature = "hdfs"
))]
use std::sync::Arc;
#[cfg(any(
all(not(feature = "hdfs"), not(feature = "hdfs-opendal")),
feature = "hdfs"
))]
use url::Url;
#[cfg(all(not(feature = "hdfs"), not(feature = "hdfs-opendal")))]
use crate::execution::operators::ExecutionError;
#[cfg(all(not(feature = "hdfs"), not(feature = "hdfs-opendal")))]
use std::collections::HashMap;
/// Parses the url, registers the object store, and returns a tuple of the object store url and object store path
#[cfg(all(not(feature = "hdfs"), not(feature = "hdfs-opendal")))]
pub(crate) fn prepare_object_store(
runtime_env: Arc<RuntimeEnv>,
url: String,
) -> Result<(ObjectStoreUrl, Path), ExecutionError> {
use crate::parquet::parquet_support::prepare_object_store_with_configs;
prepare_object_store_with_configs(runtime_env, url, &HashMap::new())
}
/// Parses the url, registers the object store, and returns a tuple of the object store url and object store path
#[cfg(feature = "hdfs")]
pub(crate) fn prepare_object_store(
runtime_env: Arc<RuntimeEnv>,
url: String,
) -> Result<(ObjectStoreUrl, Path), crate::execution::operators::ExecutionError> {
use crate::parquet::parquet_support::prepare_object_store_with_configs;
use std::collections::HashMap;
prepare_object_store_with_configs(runtime_env, url, &HashMap::new())
}
#[cfg(all(not(feature = "hdfs"), not(feature = "hdfs-opendal")))]
#[test]
fn test_prepare_object_store() {
use crate::execution::operators::ExecutionError;
let local_file_system_url = "file:///comet/spark-warehouse/part-00000.snappy.parquet";
let hdfs_url = "hdfs://localhost:8020/comet/spark-warehouse/part-00000.snappy.parquet";
let all_urls = [local_file_system_url, hdfs_url];
let expected: Vec<Result<(ObjectStoreUrl, Path), ExecutionError>> = vec![
Ok((
ObjectStoreUrl::parse("file://").unwrap(),
Path::from("/comet/spark-warehouse/part-00000.snappy.parquet"),
)),
Err(ExecutionError::GeneralError(
"Generic HadoopFileSystem error: Hdfs support is not enabled in this build"
.parse()
.unwrap(),
)),
];
for (i, url_str) in all_urls.iter().enumerate() {
let url = &Url::parse(url_str).unwrap();
let res = prepare_object_store(Arc::new(RuntimeEnv::default()), url.to_string());
let expected = expected.get(i).unwrap();
match expected {
Ok((o, p)) => {
let (r_o, r_p) = res.unwrap();
assert_eq!(r_o, *o);
assert_eq!(r_p, *p);
}
Err(e) => {
assert!(res.is_err());
let Err(res_e) = res else {
panic!("test failed")
};
assert_eq!(e.to_string(), res_e.to_string())
}
}
}
}
#[test]
#[cfg(feature = "hdfs")]
fn test_prepare_object_store() {
// we use a local file system url instead of an hdfs url because the latter requires
// a running namenode
let hdfs_url = "file:///comet/spark-warehouse/part-00000.snappy.parquet";
let expected: (ObjectStoreUrl, Path) = (
ObjectStoreUrl::parse("file://").unwrap(),
Path::from("/comet/spark-warehouse/part-00000.snappy.parquet"),
);
let url = &Url::parse(hdfs_url).unwrap();
let res = prepare_object_store(Arc::new(RuntimeEnv::default()), url.to_string());
let res = res.unwrap();
assert_eq!(res.0, expected.0);
assert_eq!(res.1, expected.1);
}
}