This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
read.rs
139 lines (114 loc) · 3.81 KB
/
read.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use stdext::function_name;
use arrow2::array::{Array, BinaryArray, BooleanArray, Int32Array, Utf8Array};
use arrow2::chunk::Chunk;
use arrow2::datatypes::Field;
use arrow2::error::Result;
use arrow2::io::odbc::api::{Connection, Cursor};
use arrow2::io::odbc::read::{buffer_from_metadata, deserialize, infer_schema};
use super::{setup_empty_table, ENV, MSSQL};
#[test]
fn int() -> Result<()> {
let table_name = function_name!().rsplit_once(':').unwrap().1;
let expected = vec![Chunk::new(vec![Box::new(Int32Array::from_slice([1])) as _])];
test(expected, "INT", "(1)", table_name)
}
#[test]
fn int_nullable() -> Result<()> {
let table_name = function_name!().rsplit_once(':').unwrap().1;
let expected = vec![Chunk::new(vec![
Box::new(Int32Array::from([Some(1), None])) as _,
])];
test(expected, "INT", "(1),(NULL)", table_name)
}
#[test]
fn bool() -> Result<()> {
let table_name = function_name!().rsplit_once(':').unwrap().1;
let expected = vec![Chunk::new(vec![
Box::new(BooleanArray::from_slice([true])) as _
])];
test(expected, "BIT", "(1)", table_name)
}
#[test]
fn bool_nullable() -> Result<()> {
let table_name = function_name!().rsplit_once(':').unwrap().1;
let expected = vec![Chunk::new(vec![
Box::new(BooleanArray::from([Some(true), None])) as _,
])];
test(expected, "BIT", "(1),(NULL)", table_name)
}
#[test]
fn binary() -> Result<()> {
let table_name = function_name!().rsplit_once(':').unwrap().1;
let expected = vec![Chunk::new(vec![
Box::new(BinaryArray::<i32>::from([Some(b"ab")])) as _,
])];
test(
expected,
"VARBINARY(2)",
"(CAST('ab' AS VARBINARY(2)))",
table_name,
)
}
#[test]
fn binary_nullable() -> Result<()> {
let table_name = function_name!().rsplit_once(':').unwrap().1;
let expected =
vec![Chunk::new(vec![
Box::new(BinaryArray::<i32>::from([Some(b"ab"), None, Some(b"ac")])) as _,
])];
test(
expected,
"VARBINARY(2)",
"(CAST('ab' AS VARBINARY(2))),(NULL),(CAST('ac' AS VARBINARY(2)))",
table_name,
)
}
#[test]
fn utf8_nullable() -> Result<()> {
let table_name = function_name!().rsplit_once(':').unwrap().1;
let expected =
vec![Chunk::new(vec![
Box::new(Utf8Array::<i32>::from([Some("ab"), None, Some("ac")])) as _,
])];
test(expected, "VARCHAR(2)", "('ab'),(NULL),('ac')", table_name)
}
fn test(
expected: Vec<Chunk<Box<dyn Array>>>,
type_: &str,
insert: &str,
table_name: &str,
) -> Result<()> {
let connection = ENV.connect_with_connection_string(MSSQL).unwrap();
setup_empty_table(&connection, table_name, &[type_]).unwrap();
connection
.execute(&format!("INSERT INTO {table_name} (a) VALUES {insert}"), ())
.unwrap();
// When
let query = format!("SELECT a FROM {table_name} ORDER BY id");
let chunks = read(&connection, &query)?.1;
assert_eq!(chunks, expected);
Ok(())
}
pub fn read(
connection: &Connection<'_>,
query: &str,
) -> Result<(Vec<Field>, Vec<Chunk<Box<dyn Array>>>)> {
let mut a = connection.prepare(query).unwrap();
let fields = infer_schema(&a)?;
let max_batch_size = 100;
let buffer = buffer_from_metadata(&a, max_batch_size).unwrap();
let cursor = a.execute(()).unwrap().unwrap();
let mut cursor = cursor.bind_buffer(buffer).unwrap();
let mut chunks = vec![];
while let Some(batch) = cursor.fetch().unwrap() {
let arrays = (0..batch.num_cols())
.zip(fields.iter())
.map(|(index, field)| {
let column_view = batch.column(index);
deserialize(column_view, field.data_type.clone())
})
.collect::<Vec<_>>();
chunks.push(Chunk::new(arrays));
}
Ok((fields, chunks))
}