Skip to content

Commit 3fb36fe

Browse files
committed
add helper methods
1 parent 5f605a9 commit 3fb36fe

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

datafusion-examples/examples/csv_json_opener.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async fn csv_opener() -> Result<()> {
6969

7070
let config = CsvSource::new(true, b',', b'"')
7171
.with_comment(Some(b'#'))
72-
.with_schema(TableSchema::new(schema, vec![]))
72+
.with_schema(TableSchema::new_from_file_schema(schema))
7373
.with_batch_size(8192)
7474
.with_projection(&scan_config);
7575

datafusion/datasource/src/table_schema.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ impl TableSchema {
8585
/// The table schema is automatically computed by appending the partition columns
8686
/// to the file schema.
8787
///
88+
/// You should prefer calling this method over
89+
/// chaining [`TableSchema::new_from_file_schema`] and [`TableSchema::with_partition_cols`]
90+
/// if you have both the file schema and partition columns available at construction time
91+
/// since it avoids re-computing the table schema.
92+
///
8893
/// # Arguments
8994
///
9095
/// * `file_schema` - Schema of the data files (without partition columns)
@@ -121,6 +126,27 @@ impl TableSchema {
121126
}
122127
}
123128

129+
/// Create a new TableSchema with no partition columns.
130+
///
131+
/// You should prefer calling [`TableSchema::new`] if you have partition columns at
132+
/// construction time since it avoids re-computing the table schema.
133+
pub fn new_from_file_schema(file_schema: SchemaRef) -> Self {
134+
Self::new(file_schema, vec![])
135+
}
136+
137+
/// Add partition columns to an existing TableSchema, returning a new instance.
138+
///
139+
/// You should prefer calling [`TableSchema::new`] instead of chaining [`TableSchema::new_from_file_schema`]
140+
/// into [`TableSchema::with_partition_cols`] if you have partition columns at construction time
141+
/// since it avoids re-computing the table schema.
142+
pub fn with_partition_cols(mut self, partition_cols: Vec<FieldRef>) -> Self {
143+
self.table_partition_cols = partition_cols;
144+
let mut builder = SchemaBuilder::from(self.file_schema.as_ref());
145+
builder.extend(self.table_partition_cols.iter().cloned());
146+
self.table_schema = Arc::new(builder.finish());
147+
self
148+
}
149+
124150
/// Get the file schema (without partition columns).
125151
///
126152
/// This is the schema of the actual data files on disk.

0 commit comments

Comments
 (0)