forked from apache/datafusion-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_provider.rs
More file actions
102 lines (90 loc) · 3.5 KB
/
table_provider.rs
File metadata and controls
102 lines (90 loc) · 3.5 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
// 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 arrow_array::{ArrayRef, RecordBatch};
use arrow_schema::{DataType, Field, Schema};
use datafusion::catalog::MemTable;
use datafusion::error::{DataFusionError, Result as DataFusionResult};
use datafusion_ffi::table_provider::FFI_TableProvider;
use datafusion_python::utils::table_provider_capsule_name;
use pyo3::exceptions::PyRuntimeError;
use pyo3::types::PyCapsule;
use pyo3::{pyclass, pymethods, Bound, PyResult, Python};
use std::sync::Arc;
/// In order to provide a test that demonstrates different sized record batches,
/// the first batch will have num_rows, the second batch num_rows+1, and so on.
#[pyclass(name = "MyTableProvider", module = "datafusion_ffi_example", subclass)]
#[derive(Clone)]
pub(crate) struct MyTableProvider {
num_cols: usize,
num_rows: usize,
num_batches: usize,
}
fn create_record_batch(
schema: &Arc<Schema>,
num_cols: usize,
start_value: i32,
num_values: usize,
) -> DataFusionResult<RecordBatch> {
let end_value = start_value + num_values as i32;
let row_values: Vec<i32> = (start_value..end_value).collect();
let columns: Vec<_> = (0..num_cols)
.map(|_| Arc::new(arrow::array::Int32Array::from(row_values.clone())) as ArrayRef)
.collect();
RecordBatch::try_new(Arc::clone(schema), columns).map_err(DataFusionError::from)
}
impl MyTableProvider {
pub fn create_table(&self) -> DataFusionResult<MemTable> {
let fields: Vec<_> = (0..self.num_cols)
.map(|idx| (b'A' + idx as u8) as char)
.map(|col_name| Field::new(col_name, DataType::Int32, true))
.collect();
let schema = Arc::new(Schema::new(fields));
let batches: DataFusionResult<Vec<_>> = (0..self.num_batches)
.map(|batch_idx| {
let start_value = batch_idx * self.num_rows;
create_record_batch(
&schema,
self.num_cols,
start_value as i32,
self.num_rows + batch_idx,
)
})
.collect();
MemTable::try_new(schema, vec![batches?])
}
}
#[pymethods]
impl MyTableProvider {
#[new]
pub fn new(num_cols: usize, num_rows: usize, num_batches: usize) -> Self {
Self {
num_cols,
num_rows,
num_batches,
}
}
pub fn __datafusion_table_provider__<'py>(
&self,
py: Python<'py>,
) -> PyResult<Bound<'py, PyCapsule>> {
let provider = self
.create_table()
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
let provider = FFI_TableProvider::new(Arc::new(provider), false, None);
PyCapsule::new(py, provider, Some(table_provider_capsule_name().to_owned()))
}
}