@@ -4,54 +4,64 @@ use duckdb::vtab::arrow::{
4
4
} ;
5
5
use vortex_array:: arrays:: StructArray ;
6
6
use vortex_array:: arrow:: { FromArrowArray , IntoArrowArray } ;
7
- use vortex_array:: builders:: ArrayBuilder ;
8
7
use vortex_array:: validity:: Validity ;
9
- use vortex_array:: { Array , ArrayRef , Canonical } ;
10
- use vortex_error:: VortexResult ;
8
+ use vortex_array:: { Array , ArrayRef } ;
9
+ use vortex_dtype:: FieldNames ;
10
+ use vortex_error:: { VortexResult , vortex_err} ;
11
11
12
12
pub trait ToDuckDB {
13
13
fn to_duckdb ( & self , chunk : & mut dyn WritableVector ) -> VortexResult < ( ) > ;
14
14
}
15
15
16
16
impl ToDuckDB for ArrayRef {
17
17
fn to_duckdb ( & self , chunk : & mut dyn WritableVector ) -> VortexResult < ( ) > {
18
- write_arrow_array_to_vector ( & self . clone ( ) . into_arrow_preferred ( ) ?, chunk) ?;
18
+ write_arrow_array_to_vector ( & self . clone ( ) . into_arrow_preferred ( ) ?, chunk)
19
+ . map_err ( |e| vortex_err ! ( "Failed to convert vrotex duckdb array: {}" , e. to_string( ) ) )
19
20
}
20
21
}
21
22
22
- struct SizedFlatVector < ' a > {
23
- pub vector : & ' a FlatVector ,
23
+ struct NamedDataChunk < ' a > {
24
+ pub chunk : & ' a DataChunkHandle ,
25
+ pub names : FieldNames ,
26
+ }
27
+
28
+ struct SizedFlatVector {
29
+ pub vector : FlatVector ,
24
30
pub len : usize ,
25
31
}
26
32
27
33
pub trait FromDuckDB < V > {
28
- fn from_duckdb ( vector : & V ) -> VortexResult < ArrayRef > ;
34
+ fn from_duckdb ( vector : V ) -> VortexResult < ArrayRef > ;
29
35
}
30
36
31
- impl FromDuckDB < DataChunkHandle > for ArrayRef {
32
- fn from_duckdb ( chunk : & DataChunkHandle ) -> VortexResult < ArrayRef > {
37
+ impl < ' a > FromDuckDB < & ' a NamedDataChunk < ' a > > for ArrayRef {
38
+ fn from_duckdb ( named_chunk : & ' a NamedDataChunk < ' a > ) -> VortexResult < ArrayRef > {
39
+ let chunk = & named_chunk. chunk ;
40
+ let names = & named_chunk. names ;
33
41
let len = chunk. len ( ) ;
34
42
35
43
let columns = ( 0 ..chunk. num_columns ( ) )
36
44
. map ( |i| {
37
45
let vector = chunk. flat_vector ( i) ;
38
- ArrayRef :: from_duckdb ( & SizedFlatVector {
39
- vector : & vector,
40
- len,
41
- } )
46
+ let array = ArrayRef :: from_duckdb ( SizedFlatVector { vector, len } ) ?;
47
+ // Figure out the column names
48
+ Ok ( ( names[ i] . clone ( ) , array) )
42
49
} )
43
50
. collect :: < VortexResult < Vec < _ > > > ( ) ?;
44
51
45
- let ( names, arrays) = columns. iter ( ) . unzip ( ) ;
52
+ let ( names, arrays) : ( Vec < _ > , Vec < _ > ) = columns. into_iter ( ) . unzip ( ) ;
46
53
47
54
// TODO(joe): extract validity
48
- StructArray :: try_new ( names, arrays, len, Validity :: AllValid ) . map ( StructArray :: to_array)
55
+ StructArray :: try_new ( names. into ( ) , arrays, len, Validity :: AllValid )
56
+ . map ( StructArray :: into_array)
49
57
}
50
58
}
51
59
52
60
impl FromDuckDB < SizedFlatVector > for ArrayRef {
53
- fn from_duckdb ( vector : & SizedFlatVector ) -> VortexResult < ArrayRef > {
54
- let arrow_arr = flat_vector_to_arrow_array ( & mut vector. vector . clone ( ) , vector. len ) ?;
61
+ fn from_duckdb ( mut sized_vector : SizedFlatVector ) -> VortexResult < ArrayRef > {
62
+ let len = sized_vector. len ;
63
+ let arrow_arr = flat_vector_to_arrow_array ( & mut sized_vector. vector , len)
64
+ . map_err ( |e| vortex_err ! ( "Failed to convert duckdb array to vortex: {}" , e) ) ?;
55
65
Ok ( ArrayRef :: from_arrow ( arrow_arr, true ) )
56
66
}
57
67
}
0 commit comments