-
Notifications
You must be signed in to change notification settings - Fork 750
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
Add support of sorting dictionary of other primitive arrays #2701
Merged
Merged
Changes from 2 commits
Commits
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 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 |
---|---|---|
|
@@ -314,119 +314,40 @@ pub fn sort_to_indices( | |
} | ||
}, | ||
DataType::Dictionary(_, _) => { | ||
let value_null_first = if options.descending { | ||
// When sorting dictionary in descending order, we take inverse of of null ordering | ||
// when sorting the values. Because if `nulls_first` is true, null must be in front | ||
// of non-null value. As we take the sorted order of value array to sort dictionary | ||
// keys, these null values will be treated as smallest ones and be sorted to the end | ||
// of sorted result. So we set `nulls_first` to false when sorting dictionary value | ||
// array to make them as largest ones, then null values will be put at the beginning | ||
// of sorted dictionary result. | ||
!options.nulls_first | ||
} else { | ||
options.nulls_first | ||
}; | ||
let value_options = Some(SortOptions { | ||
descending: false, | ||
nulls_first: value_null_first, | ||
}); | ||
downcast_dictionary_array!( | ||
values => match values.values().data_type() { | ||
DataType::Int8 => { | ||
let dict_values = values.values(); | ||
let value_null_first = if options.descending { | ||
// When sorting dictionary in descending order, we take inverse of of null ordering | ||
// when sorting the values. Because if `nulls_first` is true, null must be in front | ||
// of non-null value. As we take the sorted order of value array to sort dictionary | ||
// keys, these null values will be treated as smallest ones and be sorted to the end | ||
// of sorted result. So we set `nulls_first` to false when sorting dictionary value | ||
// array to make them as largest ones, then null values will be put at the beginning | ||
// of sorted dictionary result. | ||
!options.nulls_first | ||
} else { | ||
options.nulls_first | ||
}; | ||
let value_options = Some(SortOptions { descending: false, nulls_first: value_null_first }); | ||
let sorted_value_indices = sort_to_indices(dict_values, value_options, None)?; | ||
let value_indices_map = prepare_indices_map(&sorted_value_indices); | ||
sort_primitive_dictionary::<_, _>(values, &value_indices_map, v, n, options, limit, cmp) | ||
}, | ||
DataType::Int16 => { | ||
DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 | DataType::UInt8 | | ||
DataType::UInt16 | DataType::UInt32 | DataType::UInt64 | DataType::Float32 | DataType::Float64 | | ||
DataType::Date32 | DataType::Date64 | DataType::Time32(Second) | DataType::Time32(Millisecond) | | ||
DataType::Time64(Microsecond) | DataType::Time64(Nanosecond) | DataType::Timestamp(Second, _) | | ||
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. Do we need to match the specific TimeUnits/IntervalUnits? 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. No, I think. Just copied from above match statements. Removed them now. |
||
DataType::Timestamp(Millisecond, _) | DataType::Timestamp(Microsecond, _) | DataType::Timestamp(Nanosecond, _) | | ||
DataType::Interval(IntervalUnit::YearMonth) | DataType::Interval(IntervalUnit::DayTime) | | ||
DataType::Interval(IntervalUnit::MonthDayNano) | DataType::Duration(TimeUnit::Second) | | ||
DataType::Duration(TimeUnit::Millisecond) | DataType::Duration(TimeUnit::Microsecond) | | ||
DataType::Duration(TimeUnit::Nanosecond) => { | ||
let dict_values = values.values(); | ||
let value_null_first = if options.descending { | ||
!options.nulls_first | ||
} else { | ||
options.nulls_first | ||
}; | ||
let value_options = Some(SortOptions { descending: false, nulls_first: value_null_first }); | ||
let sorted_value_indices = sort_to_indices(dict_values, value_options, None)?; | ||
let value_indices_map = prepare_indices_map(&sorted_value_indices); | ||
sort_primitive_dictionary::<_, _>(values, &value_indices_map, v, n, options, limit, cmp) | ||
}, | ||
DataType::Int32 => { | ||
let dict_values = values.values(); | ||
let value_null_first = if options.descending { | ||
!options.nulls_first | ||
} else { | ||
options.nulls_first | ||
}; | ||
let value_options = Some(SortOptions { descending: false, nulls_first: value_null_first }); | ||
let sorted_value_indices = sort_to_indices(dict_values, value_options, None)?; | ||
let value_indices_map = prepare_indices_map(&sorted_value_indices); | ||
sort_primitive_dictionary::<_, _>(values, &value_indices_map, v, n, options, limit, cmp) | ||
}, | ||
DataType::Int64 => { | ||
let dict_values = values.values(); | ||
let value_null_first = if options.descending { | ||
!options.nulls_first | ||
} else { | ||
options.nulls_first | ||
}; | ||
let value_options = Some(SortOptions { descending: false, nulls_first: value_null_first }); | ||
let sorted_value_indices = sort_to_indices(dict_values, value_options, None)?; | ||
let value_indices_map = prepare_indices_map(&sorted_value_indices); | ||
sort_primitive_dictionary::<_, _>(values, &value_indices_map,v, n, options, limit, cmp) | ||
}, | ||
DataType::UInt8 => { | ||
let dict_values = values.values(); | ||
let value_null_first = if options.descending { | ||
!options.nulls_first | ||
} else { | ||
options.nulls_first | ||
}; | ||
let value_options = Some(SortOptions { descending: false, nulls_first: value_null_first }); | ||
let sorted_value_indices = sort_to_indices(dict_values, value_options, None)?; | ||
let value_indices_map = prepare_indices_map(&sorted_value_indices); | ||
sort_primitive_dictionary::<_, _>(values, &value_indices_map,v, n, options, limit, cmp) | ||
}, | ||
DataType::UInt16 => { | ||
let dict_values = values.values(); | ||
let value_null_first = if options.descending { | ||
!options.nulls_first | ||
} else { | ||
options.nulls_first | ||
}; | ||
let value_options = Some(SortOptions { descending: false, nulls_first: value_null_first }); | ||
let sorted_value_indices = sort_to_indices(dict_values, value_options, None)?; | ||
let value_indices_map = prepare_indices_map(&sorted_value_indices); | ||
sort_primitive_dictionary::<_, _>(values, &value_indices_map,v, n, options, limit, cmp) | ||
}, | ||
DataType::UInt32 => { | ||
let dict_values = values.values(); | ||
let value_null_first = if options.descending { | ||
!options.nulls_first | ||
} else { | ||
options.nulls_first | ||
}; | ||
let value_options = Some(SortOptions { descending: false, nulls_first: value_null_first }); | ||
let sorted_value_indices = sort_to_indices(dict_values, value_options, None)?; | ||
let value_indices_map = prepare_indices_map(&sorted_value_indices); | ||
sort_primitive_dictionary::<_, _>(values, &value_indices_map,v, n, options, limit, cmp) | ||
}, | ||
DataType::UInt64 => { | ||
let dict_values = values.values(); | ||
let value_null_first = if options.descending { | ||
!options.nulls_first | ||
} else { | ||
options.nulls_first | ||
}; | ||
let value_options = Some(SortOptions { descending: false, nulls_first: value_null_first }); | ||
let sorted_value_indices = sort_to_indices(dict_values, value_options, None)?; | ||
let value_indices_map = prepare_indices_map(&sorted_value_indices); | ||
sort_primitive_dictionary::<_, _>(values, &value_indices_map, v, n, options, limit, cmp) | ||
}, | ||
DataType::Utf8 => { | ||
let dict_values = values.values(); | ||
let value_null_first = if options.descending { | ||
!options.nulls_first | ||
} else { | ||
options.nulls_first | ||
}; | ||
let value_options = Some(SortOptions { descending: false, nulls_first: value_null_first }); | ||
let sorted_value_indices = sort_to_indices(dict_values, value_options, None)?; | ||
let value_indices_map = prepare_indices_map(&sorted_value_indices); | ||
sort_string_dictionary::<_>(values, &value_indices_map, v, n, &options, limit) | ||
|
@@ -3552,4 +3473,142 @@ mod tests { | |
vec![None, None, None, Some(5), Some(5), Some(3), Some(1)], | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_sort_f32_dicts() { | ||
let keys = | ||
Int8Array::from(vec![Some(1_i8), None, Some(2), None, Some(2), Some(0)]); | ||
let values = Float32Array::from(vec![1.2, 3.0, 5.1]); | ||
test_sort_primitive_dict_arrays::<Int8Type, Float32Type>( | ||
keys, | ||
values, | ||
None, | ||
None, | ||
vec![None, None, Some(1.2), Some(3.0), Some(5.1), Some(5.1)], | ||
); | ||
|
||
let keys = | ||
Int8Array::from(vec![Some(1_i8), None, Some(2), None, Some(2), Some(0)]); | ||
let values = Float32Array::from(vec![1.2, 3.0, 5.1]); | ||
test_sort_primitive_dict_arrays::<Int8Type, Float32Type>( | ||
keys, | ||
values, | ||
Some(SortOptions { | ||
descending: true, | ||
nulls_first: false, | ||
}), | ||
None, | ||
vec![Some(5.1), Some(5.1), Some(3.0), Some(1.2), None, None], | ||
); | ||
|
||
let keys = | ||
Int8Array::from(vec![Some(1_i8), None, Some(2), None, Some(2), Some(0)]); | ||
let values = Float32Array::from(vec![1.2, 3.0, 5.1]); | ||
test_sort_primitive_dict_arrays::<Int8Type, Float32Type>( | ||
keys, | ||
values, | ||
Some(SortOptions { | ||
descending: false, | ||
nulls_first: false, | ||
}), | ||
None, | ||
vec![Some(1.2), Some(3.0), Some(5.1), Some(5.1), None, None], | ||
); | ||
|
||
let keys = | ||
Int8Array::from(vec![Some(1_i8), None, Some(2), None, Some(2), Some(0)]); | ||
let values = Float32Array::from(vec![1.2, 3.0, 5.1]); | ||
test_sort_primitive_dict_arrays::<Int8Type, Float32Type>( | ||
keys, | ||
values, | ||
Some(SortOptions { | ||
descending: true, | ||
nulls_first: true, | ||
}), | ||
Some(3), | ||
vec![None, None, Some(5.1)], | ||
); | ||
|
||
// Values have `None`. | ||
let keys = Int8Array::from(vec![ | ||
Some(1_i8), | ||
None, | ||
Some(3), | ||
None, | ||
Some(2), | ||
Some(3), | ||
Some(0), | ||
]); | ||
let values = Float32Array::from(vec![Some(1.2), Some(3.0), None, Some(5.1)]); | ||
test_sort_primitive_dict_arrays::<Int8Type, Float32Type>( | ||
keys, | ||
values, | ||
None, | ||
None, | ||
vec![None, None, None, Some(1.2), Some(3.0), Some(5.1), Some(5.1)], | ||
); | ||
|
||
let keys = Int8Array::from(vec![ | ||
Some(1_i8), | ||
None, | ||
Some(3), | ||
None, | ||
Some(2), | ||
Some(3), | ||
Some(0), | ||
]); | ||
let values = Float32Array::from(vec![Some(1.2), Some(3.0), None, Some(5.1)]); | ||
test_sort_primitive_dict_arrays::<Int8Type, Float32Type>( | ||
keys, | ||
values, | ||
Some(SortOptions { | ||
descending: false, | ||
nulls_first: false, | ||
}), | ||
None, | ||
vec![Some(1.2), Some(3.0), Some(5.1), Some(5.1), None, None, None], | ||
); | ||
|
||
let keys = Int8Array::from(vec![ | ||
Some(1_i8), | ||
None, | ||
Some(3), | ||
None, | ||
Some(2), | ||
Some(3), | ||
Some(0), | ||
]); | ||
let values = Float32Array::from(vec![Some(1.2), Some(3.0), None, Some(5.1)]); | ||
test_sort_primitive_dict_arrays::<Int8Type, Float32Type>( | ||
keys, | ||
values, | ||
Some(SortOptions { | ||
descending: true, | ||
nulls_first: false, | ||
}), | ||
None, | ||
vec![Some(5.1), Some(5.1), Some(3.0), Some(1.2), None, None, None], | ||
); | ||
|
||
let keys = Int8Array::from(vec![ | ||
Some(1_i8), | ||
None, | ||
Some(3), | ||
None, | ||
Some(2), | ||
Some(3), | ||
Some(0), | ||
]); | ||
let values = Float32Array::from(vec![Some(1.2), Some(3.0), None, Some(5.1)]); | ||
test_sort_primitive_dict_arrays::<Int8Type, Float32Type>( | ||
keys, | ||
values, | ||
Some(SortOptions { | ||
descending: true, | ||
nulls_first: true, | ||
}), | ||
None, | ||
vec![None, None, None, Some(5.1), Some(5.1), Some(3.0), Some(1.2)], | ||
); | ||
} | ||
} |
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.
I wonder if this should be a member function somewhere...
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.
Added one helper function for that.