20
20
use crate :: datasource:: datasource:: TableProviderFactory ;
21
21
use crate :: datasource:: file_format:: avro:: AvroFormat ;
22
22
use crate :: datasource:: file_format:: csv:: CsvFormat ;
23
- use crate :: datasource:: file_format:: file_type:: { FileType , GetExt } ;
23
+ use crate :: datasource:: file_format:: file_type:: { FileCompressionType , FileType } ;
24
24
use crate :: datasource:: file_format:: json:: JsonFormat ;
25
25
use crate :: datasource:: file_format:: parquet:: ParquetFormat ;
26
26
use crate :: datasource:: file_format:: FileFormat ;
@@ -30,18 +30,24 @@ use crate::datasource::listing::{
30
30
use crate :: datasource:: TableProvider ;
31
31
use crate :: execution:: context:: SessionState ;
32
32
use async_trait:: async_trait;
33
+ use datafusion_common:: DataFusionError ;
33
34
use datafusion_expr:: CreateExternalTable ;
35
+ use std:: str:: FromStr ;
34
36
use std:: sync:: Arc ;
35
37
36
38
/// A `TableProviderFactory` capable of creating new `ListingTable`s
37
- pub struct ListingTableFactory {
38
- file_type : FileType ,
39
- }
39
+ pub struct ListingTableFactory { }
40
40
41
41
impl ListingTableFactory {
42
42
/// Creates a new `ListingTableFactory`
43
- pub fn new ( file_type : FileType ) -> Self {
44
- Self { file_type }
43
+ pub fn new ( ) -> Self {
44
+ Self { }
45
+ }
46
+ }
47
+
48
+ impl Default for ListingTableFactory {
49
+ fn default ( ) -> Self {
50
+ Self :: new ( )
45
51
}
46
52
}
47
53
@@ -52,24 +58,59 @@ impl TableProviderFactory for ListingTableFactory {
52
58
state : & SessionState ,
53
59
cmd : & CreateExternalTable ,
54
60
) -> datafusion_common:: Result < Arc < dyn TableProvider > > {
55
- let file_extension = self . file_type . get_ext ( ) ;
61
+ let file_compression_type = FileCompressionType :: from_str (
62
+ cmd. file_compression_type . as_str ( ) ,
63
+ )
64
+ . map_err ( |_| {
65
+ DataFusionError :: Execution ( format ! (
66
+ "Unknown FileCompressionType {}" ,
67
+ cmd. file_compression_type. as_str( )
68
+ ) )
69
+ } ) ?;
70
+ let file_type = FileType :: from_str ( cmd. file_type . as_str ( ) ) . map_err ( |_| {
71
+ DataFusionError :: Execution ( format ! ( "Unknown FileType {}" , cmd. file_type) )
72
+ } ) ?;
56
73
57
- let file_format: Arc < dyn FileFormat > = match self . file_type {
58
- FileType :: CSV => Arc :: new ( CsvFormat :: default ( ) ) ,
74
+ let file_extension =
75
+ file_type. get_ext_with_compression ( file_compression_type. to_owned ( ) ) ?;
76
+
77
+ let file_format: Arc < dyn FileFormat > = match file_type {
78
+ FileType :: CSV => Arc :: new (
79
+ CsvFormat :: default ( )
80
+ . with_has_header ( cmd. has_header )
81
+ . with_delimiter ( cmd. delimiter as u8 )
82
+ . with_file_compression_type ( file_compression_type) ,
83
+ ) ,
59
84
FileType :: PARQUET => Arc :: new ( ParquetFormat :: default ( ) ) ,
60
85
FileType :: AVRO => Arc :: new ( AvroFormat :: default ( ) ) ,
61
- FileType :: JSON => Arc :: new ( JsonFormat :: default ( ) ) ,
86
+ FileType :: JSON => Arc :: new (
87
+ JsonFormat :: default ( ) . with_file_compression_type ( file_compression_type) ,
88
+ ) ,
89
+ } ;
90
+
91
+ let provided_schema = if cmd. schema . fields ( ) . is_empty ( ) {
92
+ None
93
+ } else {
94
+ Some ( Arc :: new ( cmd. schema . as_ref ( ) . to_owned ( ) . into ( ) ) )
62
95
} ;
63
96
64
- let options =
65
- ListingOptions :: new ( file_format) . with_file_extension ( file_extension) ;
97
+ let options = ListingOptions :: new ( file_format)
98
+ . with_collect_stat ( state. config . collect_statistics )
99
+ . with_file_extension ( file_extension)
100
+ . with_target_partitions ( state. config . target_partitions )
101
+ . with_table_partition_cols ( cmd. table_partition_cols . clone ( ) )
102
+ . with_file_sort_order ( None ) ;
66
103
67
104
let table_path = ListingTableUrl :: parse ( & cmd. location ) ?;
68
- let resolved_schema = options. infer_schema ( state, & table_path) . await ?;
105
+ let resolved_schema = match provided_schema {
106
+ None => options. infer_schema ( state, & table_path) . await ?,
107
+ Some ( s) => s,
108
+ } ;
69
109
let config = ListingTableConfig :: new ( table_path)
70
110
. with_listing_options ( options)
71
111
. with_schema ( resolved_schema) ;
72
- let table = ListingTable :: try_new ( config) ?;
112
+ let table =
113
+ ListingTable :: try_new ( config) ?. with_definition ( cmd. definition . clone ( ) ) ;
73
114
Ok ( Arc :: new ( table) )
74
115
}
75
116
}
0 commit comments