Skip to content

Commit

Permalink
fix(firebolt-schema-builder): more cases to fallback to projection ty…
Browse files Browse the repository at this point in the history
…pe resolution
  • Loading branch information
ptiurin authored and jgraettinger committed Dec 4, 2024
1 parent 0c46064 commit b88f891
Showing 1 changed file with 224 additions and 2 deletions.
226 changes: 224 additions & 2 deletions crates/schemalate/src/firebolt/firebolt_schema_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::errors::*;
use super::firebolt_queries::{CreateTable, DropTable, InsertFromTable};
use super::firebolt_types::{Column, FireboltType, Table, TableSchema, TableType};
use doc::shape::location::Exists::Implicit;
use doc::shape::Shape;
use doc::{Annotation, Pointer};
use json::schema::{self, types};
Expand Down Expand Up @@ -84,7 +83,7 @@ pub fn build_firebolt_schema(binding: &Binding) -> Result<TableSchema, Error> {

let mut nullable = !exists.must();
let fb_type;
if exists == Implicit && !shape.type_.is_single_type() {
if !shape.type_.is_single_type() {
// If there's no specific type in schema, try to infer it from projection
let inferred_type = projection.inference.as_ref().ok_or(Error::UnknownType {
r#type: shape.type_.to_json_array(),
Expand Down Expand Up @@ -557,5 +556,228 @@ mod tests {
}],
},
);

assert_eq!(
build_firebolt_schema(&Binding {
field_selection: Some(FieldSelection {
keys: vec!["test".to_string()],
..Default::default()
}),
collection: Some(CollectionSpec {
write_schema_json: json!({
"properties": {
"test": {}
}
})
.to_string(),
projections: vec![Projection {
field: "test".to_string(),
ptr: "/test".to_string(),
inference: Some(Inference {
types: vec!["string".to_string()],
..Default::default()
}),
..Default::default()
}],
..Default::default()
}),
..Default::default()
})
.unwrap(),
TableSchema {
columns: vec![Column {
key: "test".to_string(),
r#type: FireboltType::Text,
nullable: true,
is_key: true,
}],
},
);

assert_eq!(
build_firebolt_schema(&Binding {
field_selection: Some(FieldSelection {
keys: vec!["test".to_string()],
..Default::default()
}),
collection: Some(CollectionSpec {
write_schema_json: json!({
"properties": {
"test": {}
}
})
.to_string(),
projections: vec![Projection {
field: "test".to_string(),
ptr: "/test".to_string(),
inference: Some(Inference {
types: vec!["boolean".to_string()],
..Default::default()
}),
..Default::default()
}],
..Default::default()
}),
..Default::default()
})
.unwrap(),
TableSchema {
columns: vec![Column {
key: "test".to_string(),
r#type: FireboltType::Boolean,
nullable: true,
is_key: true,
}],
},
);

assert_eq!(
build_firebolt_schema(&Binding {
field_selection: Some(FieldSelection {
keys: vec!["test".to_string()],
..Default::default()
}),
collection: Some(CollectionSpec {
write_schema_json: json!({
"properties": {
"test": {}
}
})
.to_string(),
projections: vec![Projection {
field: "test".to_string(),
ptr: "/test".to_string(),
inference: Some(Inference {
types: vec!["number".to_string()],
..Default::default()
}),
..Default::default()
}],
..Default::default()
}),
..Default::default()
})
.unwrap(),
TableSchema {
columns: vec![Column {
key: "test".to_string(),
r#type: FireboltType::Double,
nullable: true,
is_key: true,
}],
},
);

assert_eq!(
build_firebolt_schema(&Binding {
field_selection: Some(FieldSelection {
values: vec!["test".to_string()],
..Default::default()
}),
collection: Some(CollectionSpec {
write_schema_json: json!({
"properties": {
"test": {}
}
})
.to_string(),
projections: vec![Projection {
field: "test".to_string(),
ptr: "/test".to_string(),
inference: Some(Inference {
types: vec!["integer".to_string()],
..Default::default()
}),
..Default::default()
}],
..Default::default()
}),
..Default::default()
})
.unwrap(),
TableSchema {
columns: vec![Column {
key: "test".to_string(),
r#type: FireboltType::Int,
nullable: true,
is_key: false,
}],
},
);

assert_eq!(
build_firebolt_schema(&Binding {
field_selection: Some(FieldSelection {
keys: vec!["test".to_string()],
..Default::default()
}),
collection: Some(CollectionSpec {
write_schema_json: json!({
"properties": {
"test": {}
}
})
.to_string(),
projections: vec![Projection {
field: "test".to_string(),
ptr: "/test".to_string(),
inference: Some(Inference {
types: vec!["object".to_string()],
..Default::default()
}),
..Default::default()
}],
..Default::default()
}),
..Default::default()
})
.unwrap(),
TableSchema {
columns: vec![Column {
key: "test".to_string(),
r#type: FireboltType::Text,
nullable: true,
is_key: true,
}],
},
);

// Ensure explicit schema takes precedence over inferred type
assert_eq!(
build_firebolt_schema(&Binding {
field_selection: Some(FieldSelection {
keys: vec!["test".to_string()],
..Default::default()
}),
collection: Some(CollectionSpec {
write_schema_json: json!({
"properties": {
"test": {"type": "boolean"},
}
})
.to_string(),
projections: vec![Projection {
field: "test".to_string(),
ptr: "/test".to_string(),
inference: Some(Inference {
types: vec!["object".to_string()], // Should be ignored
..Default::default()
}),
..Default::default()
}],
..Default::default()
}),
..Default::default()
})
.unwrap(),
TableSchema {
columns: vec![Column {
key: "test".to_string(),
r#type: FireboltType::Boolean,
nullable: true,
is_key: true,
}],
},
);
}
}

0 comments on commit b88f891

Please sign in to comment.