Skip to content
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

[RUST] argmin and argmax kernel for numeric array #432

Merged
merged 2 commits into from
Jan 13, 2023
Merged

Conversation

eddyxu
Copy link
Contributor

@eddyxu eddyxu commented Jan 13, 2023

No description provided.

@eddyxu eddyxu requested a review from changhiskhan January 13, 2023 05:41
@eddyxu eddyxu self-assigned this Jan 13, 2023
Copy link
Contributor

@changhiskhan changhiskhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arrow has "sort_valids" like

fn sort_valids<T, U>(
    descending: bool,
    valids: &mut [(u32, T)],
    nulls: &mut [U],
    len: usize,
    mut cmp: impl FnMut(T, T) -> Ordering,
) where
    T: ?Sized + Copy,
{
    let valids_len = valids.len();
    if !descending {
        sort_unstable_by(valids, len.min(valids_len), |a, b| cmp(a.1, b.1));
    } else {
        sort_unstable_by(valids, len.min(valids_len), |a, b| cmp(a.1, b.1).reverse());
        // reverse to keep a stable ordering
        nulls.reverse();
    }
}

fn sort_valids_array<T>(
    descending: bool,
    valids: &mut [(u32, ArrayRef)],
    nulls: &mut [T],
    len: usize,
) {
    let valids_len = valids.len();
    if !descending {
        sort_unstable_by(valids, len.min(valids_len), |a, b| {
            cmp_array(a.1.as_ref(), b.1.as_ref())
        });
    } else {
        sort_unstable_by(valids, len.min(valids_len), |a, b| {
            cmp_array(a.1.as_ref(), b.1.as_ref()).reverse()
        });
        // reverse to keep a stable ordering
        nulls.reverse();
    }
}

does it make sense to do the same here? curious what the perf difference is

@eddyxu
Copy link
Contributor Author

eddyxu commented Jan 13, 2023

I feel that we can add validity check later if we (lance) have a use case. For one, we are not using it as general purpose kernel yet. The second, this code does not have memory copy/allocation, which supposes to be faster.

     // create tuples that are used for sorting
    let valids = {
        let values = as_primitive_array::<T>(values);
        value_indices
            .into_iter()
            .map(|index| (index, values.value(index as usize)))
            .collect::<Vec<(u32, T::Native)>>(). <----- HERE
    };
    sort_primitive_inner(values.len(), null_indices, cmp, options, limit, valids)

@eddyxu eddyxu merged commit efd6e18 into main Jan 13, 2023
@eddyxu eddyxu deleted the lei/argmin_max branch January 13, 2023 06:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants