Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Simplified code. (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Aug 23, 2021
1 parent 0b0efe3 commit 824ad7e
Showing 1 changed file with 58 additions and 43 deletions.
101 changes: 58 additions & 43 deletions src/io/ipc/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,56 +363,71 @@ pub(crate) fn build_field<'a>(
field_builder.finish()
}

fn type_to_field_type(data_type: &DataType) -> ipc::Type {
match data_type {
Null => ipc::Type::Null,
Boolean => ipc::Type::Bool,
UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 => ipc::Type::Int,
Float16 | Float32 | Float64 => ipc::Type::FloatingPoint,
Decimal(_, _) => ipc::Type::Decimal,
Binary => ipc::Type::Binary,
LargeBinary => ipc::Type::LargeBinary,
Utf8 => ipc::Type::Utf8,
LargeUtf8 => ipc::Type::LargeUtf8,
FixedSizeBinary(_) => ipc::Type::FixedSizeBinary,
Date32 | Date64 => ipc::Type::Date,
Duration(_) => ipc::Type::Duration,
Time32(_) | Time64(_) => ipc::Type::Time,
Timestamp(_, _) => ipc::Type::Timestamp,
Interval(_) => ipc::Type::Interval,
List(_) => ipc::Type::List,
LargeList(_) => ipc::Type::LargeList,
FixedSizeList(_, _) => ipc::Type::FixedSizeList,
Union(_, _, _) => ipc::Type::Union,
Struct(_) => ipc::Type::Struct_,
Dictionary(_, v) => type_to_field_type(v),
}
}

/// Get the IPC type of a data type
pub(crate) fn get_fb_field_type<'a>(
data_type: &DataType,
is_nullable: bool,
fbb: &mut FlatBufferBuilder<'a>,
) -> FbFieldType<'a> {
let type_type = type_to_field_type(data_type);

// some IPC implementations expect an empty list for child data, instead of a null value.
// An empty field list is thus returned for primitive types
let empty_fields: Vec<WIPOffset<ipc::Field>> = vec![];
match data_type {
Null => FbFieldType {
type_type: ipc::Type::Null,
type_type,
type_: ipc::NullBuilder::new(fbb).finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
},
Boolean => FbFieldType {
type_type: ipc::Type::Bool,
type_type,
type_: ipc::BoolBuilder::new(fbb).finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
},
UInt8 | UInt16 | UInt32 | UInt64 => {
Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64 => {
let children = fbb.create_vector(&empty_fields[..]);
let mut builder = ipc::IntBuilder::new(fbb);
builder.add_is_signed(false);
match data_type {
UInt8 => builder.add_bitWidth(8),
UInt16 => builder.add_bitWidth(16),
UInt32 => builder.add_bitWidth(32),
UInt64 => builder.add_bitWidth(64),
_ => {}
};
FbFieldType {
type_type: ipc::Type::Int,
type_: builder.finish().as_union_value(),
children: Some(children),
if matches!(data_type, UInt8 | UInt16 | UInt32 | UInt64) {
builder.add_is_signed(false);
} else {
builder.add_is_signed(true);
}
}
Int8 | Int16 | Int32 | Int64 => {
let children = fbb.create_vector(&empty_fields[..]);
let mut builder = ipc::IntBuilder::new(fbb);
builder.add_is_signed(true);
match data_type {
Int8 => builder.add_bitWidth(8),
Int16 => builder.add_bitWidth(16),
Int32 => builder.add_bitWidth(32),
Int64 => builder.add_bitWidth(64),
Int8 | UInt8 => builder.add_bitWidth(8),
Int16 | UInt16 => builder.add_bitWidth(16),
Int32 | UInt32 => builder.add_bitWidth(32),
Int64 | UInt64 => builder.add_bitWidth(64),
_ => {}
};
FbFieldType {
type_type: ipc::Type::Int,
type_type,
type_: builder.finish().as_union_value(),
children: Some(children),
}
Expand All @@ -427,36 +442,36 @@ pub(crate) fn get_fb_field_type<'a>(
_ => {}
};
FbFieldType {
type_type: ipc::Type::FloatingPoint,
type_type,
type_: builder.finish().as_union_value(),
children: Some(children),
}
}
Binary => FbFieldType {
type_type: ipc::Type::Binary,
type_type,
type_: ipc::BinaryBuilder::new(fbb).finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
},
LargeBinary => FbFieldType {
type_type: ipc::Type::LargeBinary,
type_type,
type_: ipc::LargeBinaryBuilder::new(fbb).finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
},
Utf8 => FbFieldType {
type_type: ipc::Type::Utf8,
type_type,
type_: ipc::Utf8Builder::new(fbb).finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
},
LargeUtf8 => FbFieldType {
type_type: ipc::Type::LargeUtf8,
type_type,
type_: ipc::LargeUtf8Builder::new(fbb).finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
},
FixedSizeBinary(len) => {
let mut builder = ipc::FixedSizeBinaryBuilder::new(fbb);
builder.add_byteWidth(*len as i32);
FbFieldType {
type_type: ipc::Type::FixedSizeBinary,
type_type,
type_: builder.finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
}
Expand All @@ -465,7 +480,7 @@ pub(crate) fn get_fb_field_type<'a>(
let mut builder = ipc::DateBuilder::new(fbb);
builder.add_unit(ipc::DateUnit::DAY);
FbFieldType {
type_type: ipc::Type::Date,
type_type,
type_: builder.finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
}
Expand All @@ -474,7 +489,7 @@ pub(crate) fn get_fb_field_type<'a>(
let mut builder = ipc::DateBuilder::new(fbb);
builder.add_unit(ipc::DateUnit::MILLISECOND);
FbFieldType {
type_type: ipc::Type::Date,
type_type,
type_: builder.finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
}
Expand All @@ -500,7 +515,7 @@ pub(crate) fn get_fb_field_type<'a>(
}
}
FbFieldType {
type_type: ipc::Type::Time,
type_type,
type_: builder.finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
}
Expand All @@ -520,7 +535,7 @@ pub(crate) fn get_fb_field_type<'a>(
builder.add_timezone(tz_str);
}
FbFieldType {
type_type: ipc::Type::Timestamp,
type_type,
type_: builder.finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
}
Expand All @@ -533,7 +548,7 @@ pub(crate) fn get_fb_field_type<'a>(
};
builder.add_unit(interval_unit);
FbFieldType {
type_type: ipc::Type::Interval,
type_type,
type_: builder.finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
}
Expand All @@ -548,23 +563,23 @@ pub(crate) fn get_fb_field_type<'a>(
};
builder.add_unit(time_unit);
FbFieldType {
type_type: ipc::Type::Duration,
type_type,
type_: builder.finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
}
}
List(ref list_type) => {
let child = build_field(fbb, list_type);
FbFieldType {
type_type: ipc::Type::List,
type_type,
type_: ipc::ListBuilder::new(fbb).finish().as_union_value(),
children: Some(fbb.create_vector(&[child])),
}
}
LargeList(ref list_type) => {
let child = build_field(fbb, list_type);
FbFieldType {
type_type: ipc::Type::LargeList,
type_type,
type_: ipc::LargeListBuilder::new(fbb).finish().as_union_value(),
children: Some(fbb.create_vector(&[child])),
}
Expand All @@ -574,7 +589,7 @@ pub(crate) fn get_fb_field_type<'a>(
let mut builder = ipc::FixedSizeListBuilder::new(fbb);
builder.add_listSize(*len as i32);
FbFieldType {
type_type: ipc::Type::FixedSizeList,
type_type,
type_: builder.finish().as_union_value(),
children: Some(fbb.create_vector(&[child])),
}
Expand All @@ -599,7 +614,7 @@ pub(crate) fn get_fb_field_type<'a>(
));
}
FbFieldType {
type_type: ipc::Type::Struct_,
type_type,
type_: ipc::Struct_Builder::new(fbb).finish().as_union_value(),
children: Some(fbb.create_vector(&children[..])),
}
Expand All @@ -616,7 +631,7 @@ pub(crate) fn get_fb_field_type<'a>(
builder.add_scale(*scale as i32);
builder.add_bitWidth(128);
FbFieldType {
type_type: ipc::Type::Decimal,
type_type,
type_: builder.finish().as_union_value(),
children: Some(fbb.create_vector(&empty_fields[..])),
}
Expand All @@ -637,7 +652,7 @@ pub(crate) fn get_fb_field_type<'a>(
builder.add_typeIds(ids);
}
FbFieldType {
type_type: ipc::Type::Union,
type_type,
type_: builder.finish().as_union_value(),
children: Some(fbb.create_vector(&children)),
}
Expand Down

0 comments on commit 824ad7e

Please sign in to comment.