-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-7775: Don't let safe code arbitrarily transmute readers and writers #6256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,8 @@ use std::mem; | |
| use byteorder::{BigEndian, ByteOrder}; | ||
|
|
||
| use crate::basic::Type; | ||
| use crate::column::reader::{ColumnReader, ColumnReaderImpl}; | ||
| use crate::column::writer::{ColumnWriter, ColumnWriterImpl}; | ||
| use crate::errors::{ParquetError, Result}; | ||
| use crate::util::memory::{ByteBuffer, ByteBufferPtr}; | ||
| use std::str::from_utf8; | ||
|
|
@@ -376,10 +378,30 @@ pub trait DataType: 'static { | |
|
|
||
| /// Returns size in bytes for Rust representation of the physical type. | ||
| fn get_type_size() -> usize; | ||
|
|
||
| fn get_column_reader(column_writer: ColumnReader) -> Option<ColumnReaderImpl<Self>> | ||
| where | ||
| Self: Sized; | ||
|
|
||
| fn get_column_writer(column_writer: ColumnWriter) -> Option<ColumnWriterImpl<Self>> | ||
| where | ||
| Self: Sized; | ||
|
|
||
| fn get_column_writer_ref( | ||
| column_writer: &ColumnWriter, | ||
| ) -> Option<&ColumnWriterImpl<Self>> | ||
| where | ||
| Self: Sized; | ||
|
|
||
| fn get_column_writer_mut( | ||
| column_writer: &mut ColumnWriter, | ||
| ) -> Option<&mut ColumnWriterImpl<Self>> | ||
| where | ||
| Self: Sized; | ||
| } | ||
|
|
||
| macro_rules! make_type { | ||
| ($name:ident, $physical_ty:path, $native_ty:ty, $size:expr) => { | ||
| ($name:ident, $physical_ty:path, $reader_ident: ident, $writer_ident: ident, $native_ty:ty, $size:expr) => { | ||
| pub struct $name {} | ||
|
|
||
| impl DataType for $name { | ||
|
|
@@ -392,27 +414,109 @@ macro_rules! make_type { | |
| fn get_type_size() -> usize { | ||
| $size | ||
| } | ||
|
|
||
| fn get_column_reader( | ||
| column_writer: ColumnReader, | ||
| ) -> Option<ColumnReaderImpl<Self>> { | ||
| match column_writer { | ||
| ColumnReader::$reader_ident(w) => Some(w), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| fn get_column_writer( | ||
| column_writer: ColumnWriter, | ||
| ) -> Option<ColumnWriterImpl<Self>> { | ||
| match column_writer { | ||
| ColumnWriter::$writer_ident(w) => Some(w), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| fn get_column_writer_ref( | ||
| column_writer: &ColumnWriter, | ||
| ) -> Option<&ColumnWriterImpl<Self>> { | ||
| match column_writer { | ||
| ColumnWriter::$writer_ident(w) => Some(w), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| fn get_column_writer_mut( | ||
| column_writer: &mut ColumnWriter, | ||
| ) -> Option<&mut ColumnWriterImpl<Self>> { | ||
| match column_writer { | ||
| ColumnWriter::$writer_ident(w) => Some(w), | ||
| _ => None, | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| // Generate struct definitions for all physical types | ||
|
|
||
| make_type!(BoolType, Type::BOOLEAN, bool, 1); | ||
| make_type!(Int32Type, Type::INT32, i32, 4); | ||
| make_type!(Int64Type, Type::INT64, i64, 8); | ||
| make_type!(Int96Type, Type::INT96, Int96, mem::size_of::<Int96>()); | ||
| make_type!(FloatType, Type::FLOAT, f32, 4); | ||
| make_type!(DoubleType, Type::DOUBLE, f64, 8); | ||
| make_type!( | ||
| BoolType, | ||
| Type::BOOLEAN, | ||
| BoolColumnReader, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about this. We generate column readers and writers when making types?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It generates the unpacking functions needed to unpack the enum. I don't know of another, better way of doing this so 🤷♂
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, this is just an odd place to put them.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Marwes@eb1ba90 is the only alternative I know which I would argue is a lot more verbose for no gain. |
||
| BoolColumnWriter, | ||
| bool, | ||
| 1 | ||
| ); | ||
| make_type!( | ||
| Int32Type, | ||
| Type::INT32, | ||
| Int32ColumnReader, | ||
| Int32ColumnWriter, | ||
| i32, | ||
| 4 | ||
| ); | ||
| make_type!( | ||
| Int64Type, | ||
| Type::INT64, | ||
| Int64ColumnReader, | ||
| Int64ColumnWriter, | ||
| i64, | ||
| 8 | ||
| ); | ||
| make_type!( | ||
| Int96Type, | ||
| Type::INT96, | ||
| Int96ColumnReader, | ||
| Int96ColumnWriter, | ||
| Int96, | ||
| mem::size_of::<Int96>() | ||
| ); | ||
| make_type!( | ||
| FloatType, | ||
| Type::FLOAT, | ||
| FloatColumnReader, | ||
| FloatColumnWriter, | ||
| f32, | ||
| 4 | ||
| ); | ||
| make_type!( | ||
| DoubleType, | ||
| Type::DOUBLE, | ||
| DoubleColumnReader, | ||
| DoubleColumnWriter, | ||
| f64, | ||
| 8 | ||
| ); | ||
| make_type!( | ||
| ByteArrayType, | ||
| Type::BYTE_ARRAY, | ||
| ByteArrayColumnReader, | ||
| ByteArrayColumnWriter, | ||
| ByteArray, | ||
| mem::size_of::<ByteArray>() | ||
| ); | ||
| make_type!( | ||
| FixedLenByteArrayType, | ||
| Type::FIXED_LEN_BYTE_ARRAY, | ||
| FixedLenByteArrayColumnReader, | ||
| FixedLenByteArrayColumnWriter, | ||
| ByteArray, | ||
| mem::size_of::<ByteArray>() | ||
| ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That could result in a circular dependency, we should not be doing that, or we should at least factor out the interfaces and structs there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rust handles circular, inter crate dependencies fine 🤷♂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It still does not mean we should be doing it.