Skip to content

ARROW-12763: [R] Optimize dplyr queries that use head/tail after arrange - #11405

Closed
nealrichardson wants to merge 4 commits into
apache:masterfrom
nealrichardson:r-topk
Closed

nealrichardson wants to merge 4 commits into
apache:masterfrom
nealrichardson:r-topk

Conversation

@nealrichardson

Copy link
Copy Markdown
Member
  • Uses SelectKSinkNode for head/tail on sorted query. tail() is implemented by reversing the sort orders, taking the top K rows, and then reversing the resulting row order to match as if we had taken the (ordered) bottom K rows.
  • Some subtle differences in expectation: row order appears to be locally deterministic (within chunks), but SelectK doesn't necessarily follow that when there are ties. Also, missing value handling by SelectK doesn't match R's expectations--there may be a sort option that isn't handled the same way in the SelectK algorithm (I don't see anywhere else to pass in another option but maybe I missed it)

@github-actions

Copy link
Copy Markdown

@westonpace westonpace left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me. I don't think it covers pathological cases like airquality %>% arrange(Temp) %>% head(10) %>% arrange(Wind) or airquality %>% head(10) %>% tail(5) but I'm pretty sure that's a given for our current dplyr implementation anyways.

Comment thread r/R/arrow-datum.R Resolved
Comment thread r/R/query-engine.R Resolved

test_that("tail", {
batch <- record_batch(tbl)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add a test case for a bare tail with no arrange? I think it might be treated as a head but I might not be following the logic right.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think there already was one from the last PR.

@nealrichardson

Copy link
Copy Markdown
Member Author

Looks good to me. I don't think it covers pathological cases like airquality %>% arrange(Temp) %>% head(10) %>% arrange(Wind) or airquality %>% head(10) %>% tail(5) but I'm pretty sure that's a given for our current dplyr implementation anyways.

Those do work, though internally they can't be done in a single ExecPlan:

Table$create(airquality) %>% 
  arrange(Temp) %>% 
  head(10) %>% 
  arrange(Wind) %>%
  collect()
  
   Ozone Solar.R Wind Temp Month Day
1     NA      NA  8.0   57     5  27
2      1       8  9.7   59     5  21
3      4      25  9.7   61     5  23
4     32      92 12.0   61     5  24
5     18      65 13.2   58     5  15
6     19      99 13.8   59     5   8
7     NA      NA 14.3   56     5   5
8     NA     266 14.9   58     5  26
9     NA      66 16.6   57     5  25
10     6      78 18.4   57     5  18


Table$create(airquality) %>% 
  head(10) %>% 
  tail(5) %>%
  collect()

  Ozone Solar.R Wind Temp Month Day
1    28      NA 14.9   66     5   6
2    23     299  8.6   65     5   7
3    19      99 13.8   59     5   8
4     8      19 20.1   61     5   9
5    NA     194  8.6   69     5  10

@jonkeane

Copy link
Copy Markdown
Member

I don't think it covers pathological cases like airquality %>% arrange(Temp) %>% head(10) %>% arrange(Wind) or airquality %>% head(10) %>% tail(5) but I'm pretty sure that's a given for our current dplyr implementation anyways.

Also I will note that neither of those looks pathological to me. Both are similar to queries I've seen in the wild frequently:

  • The first is a pretty common "give me the top 5 in sales, but I want to go down the list by region" type question.
  • The second I've seen in slightly different form (and could be specified differently too, but head/tail combo is not uncommon!), but as a rudimentary outlier removal: "gimme the mean of the top 100 values, but remove the the top 5 cause they tend to be extreme"

@jonkeane

Copy link
Copy Markdown
Member

I'm running tpc-h benchmarks against these locally (we haven't yet re-enabled 2-6, 8-10 on conbench)

@jonkeane

Copy link
Copy Markdown
Member

Ran the TPC-H benchmarks against apache/arrow@HEAD and not seeing much speed up here (the queries 2, 3, and 10 all have arrange() %>% head() going on). Though also no slow downs, so it's not worse, but surprisingly(?) not much faster.

Rplot

@nealrichardson

Copy link
Copy Markdown
Member Author

surprisingly(?) not much faster

Turns out there is a 7.0.0 followup to make TopK more efficient, so this is apparently correct.

@westonpace

Copy link
Copy Markdown
Member

surprisingly(?) not much faster

Turns out there is a 7.0.0 followup to make TopK more efficient, so this is apparently correct.

Even then, more efficient in terms of RAM, but not in terms of speed. arrange() %>% head() requires reading all of the data (you never know when that last item will be your biggest).

@jonkeane jonkeane closed this in 02f11b9 Oct 15, 2021
@ursabot

ursabot commented Oct 15, 2021 •

Copy link
Copy Markdown

Benchmark runs are scheduled for baseline = 8650c23 and contender = 02f11b9. 02f11b9 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Finished ⬇️0.0% ⬆️0.0%] ec2-t3-xlarge-us-east-2
[Finished ⬇️0.51% ⬆️0.0%] ursa-i9-9960x
[Finished ⬇️0.13% ⬆️0.13%] ursa-thinkcentre-m75q
Supported benchmarks:
ursa-i9-9960x: langs = Python, R, JavaScript
ursa-thinkcentre-m75q: langs = C++, Java
ec2-t3-xlarge-us-east-2: cloud = True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants