exon_common/
table_schema.rs1use std::sync::Arc;
16
17use arrow::datatypes::{Field, Fields, Schema, SchemaRef};
18
19use datafusion::error::Result;
20
21pub struct TableSchemaBuilder {
23 file_fields: Vec<Field>,
24 partition_fields: Vec<Field>,
25}
26
27impl Default for TableSchemaBuilder {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl TableSchemaBuilder {
34 pub fn new() -> Self {
36 Self {
37 file_fields: Vec::new(),
38 partition_fields: Vec::new(),
39 }
40 }
41
42 pub fn new_with_field_fields(file_fields: Vec<Field>) -> Self {
44 Self {
45 file_fields,
46 partition_fields: Vec::new(),
47 }
48 }
49
50 pub fn add_file_fields(mut self, fields: Vec<Field>) -> Self {
52 self.file_fields.extend(fields);
53 self
54 }
55
56 pub fn add_partition_fields(mut self, fields: Vec<Field>) -> Self {
58 self.partition_fields.extend(fields);
59 self
60 }
61
62 pub fn build(self) -> TableSchema {
64 let mut fields = self.file_fields.clone();
67 fields.extend(self.partition_fields);
68
69 let schema = Schema::new(fields);
70
71 let projection: Vec<usize> = (0..self.file_fields.len()).collect();
72
73 TableSchema::new(Arc::new(schema.clone()), projection.clone())
74 }
75}
76
77#[derive(Debug, Clone)]
78pub struct TableSchema {
80 schema: SchemaRef,
81 file_projection: Vec<usize>,
82}
83
84impl TableSchema {
85 pub fn new(schema: SchemaRef, file_projection: Vec<usize>) -> Self {
87 Self {
88 schema,
89 file_projection,
90 }
91 }
92
93 pub fn file_schema(&self) -> Result<SchemaRef> {
95 let file_schema = &self.schema.project(&self.file_projection).map_err(|e| {
96 datafusion::error::DataFusionError::Execution(format!(
97 "Error projecting schema: {:?}",
98 e
99 ))
100 })?;
101 Ok(Arc::new(file_schema.clone()))
102 }
103
104 pub fn fields(&self) -> Fields {
106 self.schema.fields().clone()
107 }
108
109 pub fn table_schema(&self) -> SchemaRef {
111 self.schema.clone()
112 }
113}