Как поменять "аранжировать" в sparklyr? - PullRequest
0 голосов
/ 18 января 2019

Вот мой пример

my_df <- data.frame(letters_col = sample(letters, 50, replace  = TRUE),
                    numbers_col =  rnorm(100), 
                    stringsAsFactors = FALSE)  %>% 
  copy_to(sc,., 'my_df_spark')



my_df %>% 
  arrange(letters_col, numbers_col) %>% 
  arrange(letters_col, numbers_col) %>%  
  arrange(letters_col, numbers_col) %>%  
  head 
# # Source:     lazy query [?? x 2]
# # Database:   spark_connection
# # Ordered by: letters_col, numbers_col, letters_col, numbers_col, letters_col, numbers_col
# letters_col numbers_col
# <chr>             <dbl>
#   1 a                -2.29 
# 2 a                 0.107
# 3 a                 0.489
# 4 a                 1.29 
# 5 b                -0.473
# 6 b                 0.632

как видите, фрейм данных упорядочен несколько раз по одним и тем же столбцам, мои вопросы:

  1. Это имеет значение?
  2. Как "отменить" заказ?

1 Ответ

0 голосов
/ 01 февраля 2019

В теории это не должно иметь значения. Оптимизатор запросов должен удалять избыточные вызовы аранжировки:

my_df %>% 
  arrange(letters_col, numbers_col) %>% 
  arrange(letters_col, numbers_col) %>%  
  dbplyr::remote_query_plan()
== Physical Plan ==
*(1) Sort [letters_col#10 ASC NULLS FIRST, numbers_col#11 ASC NULLS FIRST], true, 0
+- Exchange rangepartitioning(letters_col#10 ASC NULLS FIRST, numbers_col#11 ASC NULLS FIRST, 4)
   +- InMemoryTableScan [letters_col#10, numbers_col#11]
         +- InMemoryRelation [letters_col#10, numbers_col#11], StorageLevel(disk, memory, deserialized, 1 replicas)
               +- Scan ExistingRDD[letters_col#10,numbers_col#11]

однако, похоже, что несколько повторных сортировок в настоящий момент отключают оптимизатор (Spark 2.4.0, вы можете открыть билет JIRA для решения этой проблемы):

my_df %>% 
  arrange(letters_col, numbers_col) %>% 
  arrange(letters_col, numbers_col) %>%  
  arrange(letters_col, numbers_col) %>% 
  dbplyr::remote_query_plan()
== Physical Plan ==
*(2) Sort [letters_col#10 ASC NULLS FIRST, numbers_col#11 ASC NULLS FIRST], true, 0
+- Exchange rangepartitioning(letters_col#10 ASC NULLS FIRST, numbers_col#11 ASC NULLS FIRST, 4)
   +- *(1) Sort [letters_col#10 ASC NULLS FIRST, numbers_col#11 ASC NULLS FIRST], true, 0
      +- Exchange rangepartitioning(letters_col#10 ASC NULLS FIRST, numbers_col#11 ASC NULLS FIRST, 4)
         +- InMemoryTableScan [letters_col#10, numbers_col#11]
               +- InMemoryRelation [letters_col#10, numbers_col#11], StorageLevel(disk, memory, deserialized, 1 replicas)
                     +- Scan ExistingRDD[letters_col#10,numbers_col#11]

Как видите, Exchange rangepartitioning дважды включен в план выполнения.

Опция отмены отсутствует. Вы должны вернуться к my_df и начать заново.

...