Skip to content

Instantly share code, notes, and snippets.

@jagill
Created October 7, 2024 22:09
Show Gist options
  • Save jagill/1dd71ff6413002a042089feca89c6eab to your computer and use it in GitHub Desktop.
Save jagill/1dd71ff6413002a042089feca89c6eab to your computer and use it in GitHub Desktop.
use arrow::datatypes::DataType;
use arrow::datatypes::Field;
use arrow::datatypes::Fields;
use arrow::datatypes::Schema;
use std::io::BufReader;
use std::sync::Arc;
pub fn map_of(key_type: DataType, value_type: DataType) -> DataType {
let field = Arc::new(Field::new(
"entries",
DataType::Struct(Fields::from(vec![
Field::new("keys", key_type, /*nullable*/ false),
Field::new("values", value_type, true),
])),
false,
));
DataType::Map(field, false)
}
fn main() {
let int_schema = Arc::new(Schema::new(vec![Field::new(
"map_i32_i32",
map_of(DataType::Int32, DataType::Int32),
true,
)]));
let input_int_data = r#"{"map_i32_i32": {"1": 2}}"#;
let mut int_json = arrow_json::ReaderBuilder::new(int_schema)
.build(BufReader::new(input_int_data.as_bytes()))
.unwrap();
let int_batch = int_json.next().unwrap().unwrap();
println!("{int_batch:?}");
let bool_schema = Arc::new(Schema::new(vec![Field::new(
"map_bool_i32",
map_of(DataType::Boolean, DataType::Int32),
true,
)]));
let input_bool_data = r#"{"map_bool_i32": {"true": 1}}"#;
let mut bool_json = arrow_json::ReaderBuilder::new(bool_schema)
.build(BufReader::new(input_bool_data.as_bytes()))
.unwrap();
let bool_batch = bool_json.next().unwrap().unwrap();
println!("{bool_batch:?}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment