forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
row.rs
122 lines (108 loc) · 4.32 KB
/
row.rs
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
// 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 datafusion::datasource::file_format::parquet::ParquetFormat;
use datafusion::datasource::file_format::FileFormat;
use datafusion::datasource::object_store::ObjectStoreUrl;
use datafusion::error::Result;
use datafusion::execution::context::SessionState;
use datafusion::physical_plan::file_format::FileScanConfig;
use datafusion::physical_plan::{collect, ExecutionPlan};
use datafusion::prelude::SessionContext;
use datafusion_row::layout::RowType::{Compact, WordAligned};
use datafusion_row::reader::read_as_batch;
use datafusion_row::writer::write_batch_unchecked;
use object_store::{local::LocalFileSystem, path::Path, ObjectStore};
use std::sync::Arc;
#[tokio::test]
async fn test_with_parquet() -> Result<()> {
let ctx = SessionContext::new();
let state = ctx.state();
let task_ctx = state.task_ctx();
let projection = Some(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
let exec =
get_exec(&state, "alltypes_plain.parquet", projection.as_ref(), None).await?;
let schema = exec.schema().clone();
let batches = collect(exec, task_ctx).await?;
assert_eq!(1, batches.len());
let batch = &batches[0];
let mut vector = vec![0; 20480];
let row_offsets =
{ write_batch_unchecked(&mut vector, 0, batch, 0, schema.clone(), Compact) };
let output_batch = { read_as_batch(&vector, schema, &row_offsets, Compact)? };
assert_eq!(*batch, output_batch);
Ok(())
}
#[tokio::test]
async fn test_with_parquet_word_aligned() -> Result<()> {
let ctx = SessionContext::new();
let state = ctx.state();
let task_ctx = state.task_ctx();
let projection = Some(vec![0, 1, 2, 3, 4, 5, 6, 7]);
let exec =
get_exec(&state, "alltypes_plain.parquet", projection.as_ref(), None).await?;
let schema = exec.schema().clone();
let batches = collect(exec, task_ctx).await?;
assert_eq!(1, batches.len());
let batch = &batches[0];
let mut vector = vec![0; 20480];
let row_offsets =
{ write_batch_unchecked(&mut vector, 0, batch, 0, schema.clone(), WordAligned) };
let output_batch = { read_as_batch(&vector, schema, &row_offsets, WordAligned)? };
assert_eq!(*batch, output_batch);
Ok(())
}
async fn get_exec(
state: &SessionState,
file_name: &str,
projection: Option<&Vec<usize>>,
limit: Option<usize>,
) -> Result<Arc<dyn ExecutionPlan>> {
let testdata = datafusion::test_util::parquet_test_data();
let filename = format!("{testdata}/{file_name}");
let path = Path::from_filesystem_path(filename).unwrap();
let format = ParquetFormat::default();
let object_store = Arc::new(LocalFileSystem::new()) as Arc<dyn ObjectStore>;
let object_store_url = ObjectStoreUrl::local_filesystem();
let meta = object_store.head(&path).await.unwrap();
let file_schema = format
.infer_schema(state, &object_store, &[meta.clone()])
.await
.expect("Schema inference");
let statistics = format
.infer_stats(state, &object_store, file_schema.clone(), &meta)
.await
.expect("Stats inference");
let file_groups = vec![vec![meta.into()]];
let exec = format
.create_physical_plan(
state,
FileScanConfig {
object_store_url,
file_schema,
file_groups,
statistics,
projection: projection.cloned(),
limit,
table_partition_cols: vec![],
output_ordering: None,
infinite_source: false,
},
None,
)
.await?;
Ok(exec)
}