Skip to content

Commit 485849b

Browse files
committed
Made snapshot.schema() return a SchemaRef
1 parent 34b9d1a commit 485849b

File tree

7 files changed

+36
-40
lines changed

7 files changed

+36
-40
lines changed

ffi/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ pub unsafe extern "C" fn version(snapshot: Handle<SharedSnapshot>) -> u64 {
619619
#[no_mangle]
620620
pub unsafe extern "C" fn logical_schema(snapshot: Handle<SharedSnapshot>) -> Handle<SharedSchema> {
621621
let snapshot = unsafe { snapshot.as_ref() };
622-
Arc::new(snapshot.schema().clone()).into()
622+
snapshot.schema().into()
623623
}
624624

625625
/// Free a schema

kernel/src/scan/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ impl ScanBuilder {
9696
/// perform actual data reads.
9797
pub fn build(self) -> DeltaResult<Scan> {
9898
// if no schema is provided, use snapshot's entire schema (e.g. SELECT *)
99-
let logical_schema = self
100-
.schema
101-
.unwrap_or_else(|| self.snapshot.schema().clone().into());
99+
let logical_schema = self.schema.unwrap_or_else(|| self.snapshot.schema());
102100
let state_info = get_state_info(
103101
logical_schema.as_ref(),
104102
&self.snapshot.metadata().partition_columns,

kernel/src/schema/mod.rs

+19-16
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,9 @@ impl InvariantChecker {
331331
///
332332
/// This traverses the entire schema to check for the presence of the "delta.invariants"
333333
/// metadata key.
334-
pub(crate) fn has_invariants(schema: &Schema) -> bool {
334+
pub(crate) fn has_invariants(schema: SchemaRef) -> bool {
335335
let mut checker = InvariantChecker::default();
336-
let _ = checker.transform_struct(schema);
336+
let _ = checker.transform_struct(schema.as_ref());
337337
checker.has_invariants
338338
}
339339
}
@@ -1251,11 +1251,11 @@ mod tests {
12511251
#[test]
12521252
fn test_has_invariants() {
12531253
// Schema with no invariants
1254-
let schema = StructType::new([
1254+
let schema = Arc::new(StructType::new([
12551255
StructField::nullable("a", DataType::STRING),
12561256
StructField::nullable("b", DataType::INTEGER),
1257-
]);
1258-
assert!(!InvariantChecker::has_invariants(&schema));
1257+
]));
1258+
assert!(!InvariantChecker::has_invariants(schema));
12591259

12601260
// Schema with top-level invariant
12611261
let mut field = StructField::nullable("c", DataType::STRING);
@@ -1264,8 +1264,11 @@ mod tests {
12641264
MetadataValue::String("c > 0".to_string()),
12651265
);
12661266

1267-
let schema = StructType::new([StructField::nullable("a", DataType::STRING), field]);
1268-
assert!(InvariantChecker::has_invariants(&schema));
1267+
let schema = Arc::new(StructType::new([
1268+
StructField::nullable("a", DataType::STRING),
1269+
field,
1270+
]));
1271+
assert!(InvariantChecker::has_invariants(schema));
12691272

12701273
// Schema with nested invariant in a struct
12711274
let nested_field = StructField::nullable(
@@ -1280,12 +1283,12 @@ mod tests {
12801283
}]),
12811284
);
12821285

1283-
let schema = StructType::new([
1286+
let schema = Arc::new(StructType::new([
12841287
StructField::nullable("a", DataType::STRING),
12851288
StructField::nullable("b", DataType::INTEGER),
12861289
nested_field,
1287-
]);
1288-
assert!(InvariantChecker::has_invariants(&schema));
1290+
]));
1291+
assert!(InvariantChecker::has_invariants(schema));
12891292

12901293
// Schema with nested invariant in an array of structs
12911294
let array_field = StructField::nullable(
@@ -1303,12 +1306,12 @@ mod tests {
13031306
),
13041307
);
13051308

1306-
let schema = StructType::new([
1309+
let schema = Arc::new(StructType::new([
13071310
StructField::nullable("a", DataType::STRING),
13081311
StructField::nullable("b", DataType::INTEGER),
13091312
array_field,
1310-
]);
1311-
assert!(InvariantChecker::has_invariants(&schema));
1313+
]));
1314+
assert!(InvariantChecker::has_invariants(schema));
13121315

13131316
// Schema with nested invariant in a map value that's a struct
13141317
let map_field = StructField::nullable(
@@ -1327,11 +1330,11 @@ mod tests {
13271330
),
13281331
);
13291332

1330-
let schema = StructType::new([
1333+
let schema = Arc::new(StructType::new([
13311334
StructField::nullable("a", DataType::STRING),
13321335
StructField::nullable("b", DataType::INTEGER),
13331336
map_field,
1334-
]);
1335-
assert!(InvariantChecker::has_invariants(&schema));
1337+
]));
1338+
assert!(InvariantChecker::has_invariants(schema));
13361339
}
13371340
}

kernel/src/snapshot.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use url::Url;
99
use crate::actions::{Metadata, Protocol};
1010
use crate::log_segment::LogSegment;
1111
use crate::scan::ScanBuilder;
12-
use crate::schema::Schema;
12+
use crate::schema::{Schema, SchemaRef};
1313
use crate::table_configuration::TableConfiguration;
1414
use crate::table_features::ColumnMappingMode;
1515
use crate::table_properties::TableProperties;
@@ -98,8 +98,7 @@ impl Snapshot {
9898
}
9999

100100
/// Table [`Schema`] at this `Snapshot`s version.
101-
// TODO should this return SchemaRef?
102-
pub fn schema(&self) -> &Schema {
101+
pub fn schema(&self) -> SchemaRef {
103102
self.table_configuration.schema()
104103
}
105104

@@ -203,7 +202,6 @@ mod tests {
203202
use crate::engine::default::filesystem::ObjectStoreFileSystemClient;
204203
use crate::engine::sync::SyncEngine;
205204
use crate::path::ParsedLogPath;
206-
use crate::schema::StructType;
207205

208206
#[test]
209207
fn test_snapshot_read_metadata() {
@@ -219,8 +217,8 @@ mod tests {
219217
assert_eq!(snapshot.protocol(), &expected);
220218

221219
let schema_string = r#"{"type":"struct","fields":[{"name":"value","type":"integer","nullable":true,"metadata":{}}]}"#;
222-
let expected: StructType = serde_json::from_str(schema_string).unwrap();
223-
assert_eq!(snapshot.schema(), &expected);
220+
let expected: SchemaRef = serde_json::from_str(schema_string).unwrap();
221+
assert_eq!(snapshot.schema(), expected);
224222
}
225223

226224
#[test]
@@ -237,8 +235,8 @@ mod tests {
237235
assert_eq!(snapshot.protocol(), &expected);
238236

239237
let schema_string = r#"{"type":"struct","fields":[{"name":"value","type":"integer","nullable":true,"metadata":{}}]}"#;
240-
let expected: StructType = serde_json::from_str(schema_string).unwrap();
241-
assert_eq!(snapshot.schema(), &expected);
238+
let expected: SchemaRef = serde_json::from_str(schema_string).unwrap();
239+
assert_eq!(snapshot.schema(), expected);
242240
}
243241

244242
#[test]

kernel/src/table_changes/scan.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl TableChangesScan {
197197
PhysicalPredicate::Some(predicate, schema) => Some((predicate, schema)),
198198
PhysicalPredicate::None => None,
199199
};
200-
let schema = self.table_changes.end_snapshot.schema().clone().into();
200+
let schema = self.table_changes.end_snapshot.schema();
201201
let it = table_changes_action_iter(engine, commits, schema, physical_predicate)?;
202202
Ok(Some(it).into_iter().flatten())
203203
}

kernel/src/table_configuration.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::sync::{Arc, LazyLock};
1414
use url::Url;
1515

1616
use crate::actions::{ensure_supported_features, Metadata, Protocol};
17-
use crate::schema::{InvariantChecker, Schema, SchemaRef};
17+
use crate::schema::{InvariantChecker, SchemaRef};
1818
use crate::table_features::{
1919
column_mapping_mode, validate_schema_column_mapping, ColumnMappingMode, ReaderFeatures,
2020
WriterFeatures,
@@ -101,10 +101,10 @@ impl TableConfiguration {
101101
&self.protocol
102102
}
103103

104-
/// The [`Schema`] of for this table at this version.
104+
/// The logical schema ([`SchemaRef`]) of this table at this version.
105105
#[cfg_attr(feature = "developer-visibility", visibility::make(pub))]
106-
pub(crate) fn schema(&self) -> &Schema {
107-
self.schema.as_ref()
106+
pub(crate) fn schema(&self) -> SchemaRef {
107+
self.schema.clone()
108108
}
109109

110110
/// The [`TableProperties`] of this table at this version.

kernel/src/transaction.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ impl Transaction {
151151
// for now, we just pass through all the columns except partition columns.
152152
// note this is _incorrect_ if table config deems we need partition columns.
153153
let partition_columns = &self.read_snapshot.metadata().partition_columns;
154-
let fields = self.read_snapshot.schema().fields();
155-
let fields = fields
154+
let schema = self.read_snapshot.schema();
155+
let fields = schema
156+
.fields()
156157
.filter(|f| !partition_columns.contains(f.name()))
157158
.map(|f| Expression::column([f.name()]));
158159
Expression::struct_from(fields)
@@ -167,11 +168,7 @@ impl Transaction {
167168
let target_dir = self.read_snapshot.table_root();
168169
let snapshot_schema = self.read_snapshot.schema();
169170
let logical_to_physical = self.generate_logical_to_physical();
170-
WriteContext::new(
171-
target_dir.clone(),
172-
Arc::new(snapshot_schema.clone()),
173-
logical_to_physical,
174-
)
171+
WriteContext::new(target_dir.clone(), snapshot_schema, logical_to_physical)
175172
}
176173

177174
/// Add write metadata about files to include in the transaction. This API can be called

0 commit comments

Comments
 (0)