-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Introduce RowLayout to represent rows for different purposes #2261
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
68c46e8
Introduce RowLayout to represent rows for different purposes
yjshen d31b392
revert default
yjshen 2480338
Apply suggestions from code review
yjshen 0c088aa
more &schema
yjshen 0428300
more tests and refactor
yjshen ab15376
logs for flasky test
yjshen aaa1ab7
unwanted cargo change
yjshen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,26 +17,94 @@ | |
|
|
||
| //! Various row layout for different use case | ||
|
|
||
| use crate::row::{schema_null_free, var_length}; | ||
| use crate::row::{row_supported, schema_null_free, var_length}; | ||
| use arrow::datatypes::{DataType, Schema}; | ||
| use arrow::util::bit_util::{ceil, round_upto_power_of_2}; | ||
| use std::fmt::{Debug, Formatter}; | ||
| use std::sync::Arc; | ||
|
|
||
| const UTF8_DEFAULT_SIZE: usize = 20; | ||
| const BINARY_DEFAULT_SIZE: usize = 100; | ||
|
|
||
| #[derive(Copy, Clone, Debug)] | ||
| /// Type of a RowLayout | ||
| pub enum RowType { | ||
|
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. 👍 |
||
| /// This type of layout will store each field with minimum bytes for space efficiency. | ||
| /// Its typical use case represents a sorting payload that accesses all row fields as a unit. | ||
| Compact, | ||
| /// This type of layout will store one 8-byte word per field for CPU-friendly, | ||
| /// It is mainly used to represent the rows with frequently updated content, | ||
| /// for example, grouping state for hash aggregation. | ||
| WordAligned, | ||
| // RawComparable, | ||
| } | ||
|
|
||
| /// Reveals how the fields of a record are stored in the raw-bytes format | ||
| pub(crate) struct RowLayout { | ||
| /// Type of the layout | ||
| type_: RowType, | ||
| /// If a row is null free according to its schema | ||
| pub(crate) null_free: bool, | ||
| /// The number of bytes used to store null bits for each field. | ||
| pub(crate) null_width: usize, | ||
| /// Length in bytes for `values` part of the current tuple. | ||
| pub(crate) values_width: usize, | ||
| /// Total number of fields for each tuple. | ||
| pub(crate) field_count: usize, | ||
| /// Starting offset for each fields in the raw bytes. | ||
| pub(crate) field_offsets: Vec<usize>, | ||
| } | ||
|
|
||
| impl RowLayout { | ||
| pub(crate) fn new(schema: &Arc<Schema>, type_: RowType) -> Self { | ||
yjshen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert!(row_supported(schema)); | ||
| let null_free = schema_null_free(schema); | ||
| let field_count = schema.fields().len(); | ||
| let null_width = if null_free { 0 } else { ceil(field_count, 8) }; | ||
| let (field_offsets, values_width) = match type_ { | ||
| RowType::Compact => compact_offsets(null_width, schema), | ||
| RowType::WordAligned => word_aligned_offsets(null_width, schema), | ||
| }; | ||
| Self { | ||
| type_, | ||
| null_free, | ||
| null_width, | ||
| values_width, | ||
| field_count, | ||
| field_offsets, | ||
| } | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| pub(crate) fn init_varlena_offset(&self) -> usize { | ||
| self.null_width + self.values_width | ||
| } | ||
| } | ||
|
|
||
| impl Debug for RowLayout { | ||
yjshen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("RowLayout") | ||
| .field("type", &self.type_) | ||
| .field("null_width", &self.null_width) | ||
| .field("values_width", &self.values_width) | ||
| .field("field_count", &self.field_count) | ||
| .field("offsets", &self.field_offsets) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| /// Get relative offsets for each field and total width for values | ||
| pub fn get_offsets(null_width: usize, schema: &Arc<Schema>) -> (Vec<usize>, usize) { | ||
| fn compact_offsets(null_width: usize, schema: &Arc<Schema>) -> (Vec<usize>, usize) { | ||
yjshen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let mut offsets = vec![]; | ||
| let mut offset = null_width; | ||
| for f in schema.fields() { | ||
| offsets.push(offset); | ||
| offset += type_width(f.data_type()); | ||
| offset += compact_type_width(f.data_type()); | ||
| } | ||
| (offsets, offset - null_width) | ||
| } | ||
|
|
||
| fn type_width(dt: &DataType) -> usize { | ||
| fn compact_type_width(dt: &DataType) -> usize { | ||
| use DataType::*; | ||
| if var_length(dt) { | ||
| return std::mem::size_of::<u64>(); | ||
|
|
@@ -50,13 +118,23 @@ fn type_width(dt: &DataType) -> usize { | |
| } | ||
| } | ||
|
|
||
| fn word_aligned_offsets(null_width: usize, schema: &Arc<Schema>) -> (Vec<usize>, usize) { | ||
| let mut offsets = vec![]; | ||
| let mut offset = null_width; | ||
| for _ in schema.fields() { | ||
| offsets.push(offset); | ||
| offset += 8; // a 8-bytes word for each field | ||
yjshen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| (offsets, offset - null_width) | ||
| } | ||
|
|
||
| /// Estimate row width based on schema | ||
| pub fn estimate_row_width(schema: &Arc<Schema>) -> usize { | ||
| let null_free = schema_null_free(schema); | ||
| let field_count = schema.fields().len(); | ||
| let mut width = if null_free { 0 } else { ceil(field_count, 8) }; | ||
| for f in schema.fields() { | ||
| width += type_width(f.data_type()); | ||
| width += compact_type_width(f.data_type()); | ||
| match f.data_type() { | ||
| DataType::Utf8 => width += UTF8_DEFAULT_SIZE, | ||
| DataType::Binary => width += BINARY_DEFAULT_SIZE, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The main changes are below. Other files changes are almost mechanical.