thisisnic commented on code in PR #14361: URL: https://github.com/apache/arrow/pull/14361#discussion_r992347887
########## r/R/dplyr-slice.R: ########## @@ -0,0 +1,144 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +# The following S3 methods are registered on load if dplyr is present + +slice_head.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + head(.data, n) +} +slice_head.Dataset <- slice_head.ArrowTabular <- slice_head.RecordBatchReader <- slice_head.arrow_dplyr_query + +slice_tail.arrow_dplyr_query <- function(.data, ..., n, prop) { Review Comment: There's an error message for extraneous `slice_*()`-related arguments which dplyr has but we don't have that here: ``` r library(dplyr) library(arrow) mtcars %>% slice_tail(n = 3, with_ties = FALSE) %>% collect() #> Error in `slice_tail()`: #> ! `...` must be empty. #> ✖ Problematic argument: #> • with_ties = FALSE mtcars %>% arrow_table() %>% slice_tail(n = 3, with_ties = FALSE) %>% collect() #> mpg cyl disp hp drat wt qsec vs am gear carb #> 1 19.7 6 145 175 3.62 2.77 15.5 0 1 5 6 #> 2 15.0 8 301 335 3.54 3.57 14.6 0 1 5 8 #> 3 21.4 4 121 109 4.11 2.78 18.6 1 1 4 2 ``` ########## r/R/dplyr-slice.R: ########## @@ -0,0 +1,144 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +# The following S3 methods are registered on load if dplyr is present + +slice_head.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + head(.data, n) +} +slice_head.Dataset <- slice_head.ArrowTabular <- slice_head.RecordBatchReader <- slice_head.arrow_dplyr_query + +slice_tail.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + tail(.data, n) +} +slice_tail.Dataset <- slice_tail.ArrowTabular <- slice_tail.RecordBatchReader <- slice_tail.arrow_dplyr_query + +slice_min.arrow_dplyr_query <- function(.data, ..., n, prop, with_ties = TRUE) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") Review Comment: ```suggestion arrow_not_supported("Slicing grouped data") ``` Slightly more concise - feel free to disregard ########## r/R/dplyr-slice.R: ########## @@ -0,0 +1,144 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +# The following S3 methods are registered on load if dplyr is present + +slice_head.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + head(.data, n) +} +slice_head.Dataset <- slice_head.ArrowTabular <- slice_head.RecordBatchReader <- slice_head.arrow_dplyr_query + +slice_tail.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") Review Comment: ```suggestion arrow_not_supported("Slicing grouped data") ``` Slightly more concise - feel free to disregard ########## r/R/dplyr-slice.R: ########## @@ -0,0 +1,144 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +# The following S3 methods are registered on load if dplyr is present + +slice_head.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + head(.data, n) +} +slice_head.Dataset <- slice_head.ArrowTabular <- slice_head.RecordBatchReader <- slice_head.arrow_dplyr_query + +slice_tail.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + tail(.data, n) +} +slice_tail.Dataset <- slice_tail.ArrowTabular <- slice_tail.RecordBatchReader <- slice_tail.arrow_dplyr_query + +slice_min.arrow_dplyr_query <- function(.data, ..., n, prop, with_ties = TRUE) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + if (with_ties) { + arrow_not_supported("with_ties = TRUE") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + head(dplyr::arrange(.data, ...), n) +} +slice_min.Dataset <- slice_min.ArrowTabular <- slice_min.RecordBatchReader <- slice_min.arrow_dplyr_query + +slice_max.arrow_dplyr_query <- function(.data, ..., n, prop, with_ties = TRUE) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + if (with_ties) { + arrow_not_supported("with_ties = TRUE") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + sorted <- dplyr::arrange(.data, ...) + # Invert the sort order of the things in ... so they're descending + # TODO: handle possibility that .data was already sorted and we don't want + # to invert those sorts? Does that matter? Or no because there's no promise + # of order of which TopK elements you get if there are ties? + sorted$arrange_desc <- !sorted$arrange_desc + head(sorted, n) +} +slice_max.Dataset <- slice_max.ArrowTabular <- slice_max.RecordBatchReader <- slice_max.arrow_dplyr_query + +slice_sample.arrow_dplyr_query <- function(.data, + ..., + n, + prop, + weight_by = NULL, + replace = FALSE) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") Review Comment: ```suggestion arrow_not_supported("Slicing grouped data") ``` Slightly more concise - feel free to disregard ########## r/R/dplyr-slice.R: ########## @@ -0,0 +1,144 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +# The following S3 methods are registered on load if dplyr is present + +slice_head.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + head(.data, n) +} +slice_head.Dataset <- slice_head.ArrowTabular <- slice_head.RecordBatchReader <- slice_head.arrow_dplyr_query + +slice_tail.arrow_dplyr_query <- function(.data, ..., n, prop) { Review Comment: Something else isn't quite right here: ``` r library(dplyr) library(arrow) mtcars %>% slice_tail(prop = 0.1) %>% collect() #> mpg cyl disp hp drat wt qsec vs am gear carb #> Ferrari Dino 19.7 6 145 175 3.62 2.77 15.5 0 1 5 6 #> Maserati Bora 15.0 8 301 335 3.54 3.57 14.6 0 1 5 8 #> Volvo 142E 21.4 4 121 109 4.11 2.78 18.6 1 1 4 2 mtcars %>% arrow_table() %>% slice_tail(prop = 0.1) %>% collect() #> Error: Expected single integer value ``` ########## r/R/dplyr-slice.R: ########## @@ -0,0 +1,144 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +# The following S3 methods are registered on load if dplyr is present + +slice_head.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + head(.data, n) +} +slice_head.Dataset <- slice_head.ArrowTabular <- slice_head.RecordBatchReader <- slice_head.arrow_dplyr_query + +slice_tail.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + tail(.data, n) +} +slice_tail.Dataset <- slice_tail.ArrowTabular <- slice_tail.RecordBatchReader <- slice_tail.arrow_dplyr_query + +slice_min.arrow_dplyr_query <- function(.data, ..., n, prop, with_ties = TRUE) { Review Comment: I think you're missing the `order_by` argument to `slice_min()` and `slice_max()` here. ########## r/R/dplyr-slice.R: ########## @@ -0,0 +1,144 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +# The following S3 methods are registered on load if dplyr is present + +slice_head.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + head(.data, n) +} +slice_head.Dataset <- slice_head.ArrowTabular <- slice_head.RecordBatchReader <- slice_head.arrow_dplyr_query + +slice_tail.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + tail(.data, n) +} +slice_tail.Dataset <- slice_tail.ArrowTabular <- slice_tail.RecordBatchReader <- slice_tail.arrow_dplyr_query + +slice_min.arrow_dplyr_query <- function(.data, ..., n, prop, with_ties = TRUE) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") + } + if (with_ties) { + arrow_not_supported("with_ties = TRUE") + } + + if (missing(n)) { + n <- prop_to_n(.data, prop) + } + + head(dplyr::arrange(.data, ...), n) +} +slice_min.Dataset <- slice_min.ArrowTabular <- slice_min.RecordBatchReader <- slice_min.arrow_dplyr_query + +slice_max.arrow_dplyr_query <- function(.data, ..., n, prop, with_ties = TRUE) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") Review Comment: ```suggestion arrow_not_supported("Slicing grouped data") ``` Slightly more concise - feel free to disregard ########## r/R/dplyr-slice.R: ########## @@ -0,0 +1,144 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +# The following S3 methods are registered on load if dplyr is present + +slice_head.arrow_dplyr_query <- function(.data, ..., n, prop) { + if (length(group_vars(.data)) > 0) { + arrow_not_supported("Slicing Arrow data with groups") Review Comment: ```suggestion arrow_not_supported("Slicing grouped data") ``` Slightly more concise - feel free to disregard -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
