Skip to content

Commit

Permalink
fix: resolve case where unsigned integers were cast to signed integer…
Browse files Browse the repository at this point in the history
…s in native (delta) storage (#2640)
  • Loading branch information
hemanth94 authored Feb 13, 2024
1 parent d67fe3d commit 564a3f0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
14 changes: 11 additions & 3 deletions crates/datasources/src/native/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ fn arrow_to_delta_safe(arrow_type: &DataType) -> DeltaResult<DeltaField> {
metadata: Some(metadata),
})
}
dtype @ (DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64) => {
let mut metadata = HashMap::new();
metadata.insert("arrow_type".to_string(), json!(dtype));

let delta_type = dtype.try_into()?;

Ok(DeltaField {
data_type: delta_type,
metadata: Some(metadata),
})
}
other => {
let delta_type = other.try_into()?;
Ok(DeltaField {
Expand All @@ -130,7 +141,6 @@ fn arrow_to_delta_safe(arrow_type: &DataType) -> DeltaResult<DeltaField> {
}
}


impl NativeTableStorage {
/// Create a native table storage provider from a URL and an object store instance
/// rooted at that location.
Expand Down Expand Up @@ -194,7 +204,6 @@ impl NativeTableStorage {
.with_table_name(&table.meta.name)
.with_log_store(delta_store);


for col in &opts.columns {
let delta_col = arrow_to_delta_safe(&col.arrow_type)?;

Expand Down Expand Up @@ -371,7 +380,6 @@ impl TableProvider for NativeTable {
if let Some(arrow_type) = metadata.get("arrow_type") {
// this is dumb AF, delta-lake is returning a string of a json object instead of a json object


// any panics here are bugs in writing the metadata in the first place
let s: String =
serde_json::from_str(arrow_type).expect("metadata was not correctly written");
Expand Down
18 changes: 17 additions & 1 deletion testdata/sqllogictests/datatypes.slt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,20 @@ statement error
select array_element(1, f) from test;

statement error
select array_append(f, 3) from test;
select array_append(f, 3) from test;



# 2588 Unsigned integers downgraded to integers in delta-lake
statement ok
create table datatypes (u8 tinyint unsigned, u16 smallint unsigned, u32 int unsigned, u64 bigint unsigned);


# Inserting negative values to test validity of unsigned integers
statement error
insert into datatypes (u8, u16, u32, u64) values (-1, -1, -1, -1);


# Inserting positive values
statement ok
insert into datatypes (u8, u16, u32, u64) values (1, 1, 1, 1);

0 comments on commit 564a3f0

Please sign in to comment.