R/go_through_merges.R

Defines functions go_through_merges

Documented in go_through_merges

#' @title Compute all merges
#' 
#' @description Loops the input table to add all the merges to the R file.
#' 
#' @author Briac LE RAY (briac.leray@partnre.com)
#' 
#' @return Does not retunr anything.
#' @param merging_table The filled merging template. Type = dataframe
#' @param file_path The file to the path to write the code in. Type = character
#' @examples go_through_merges(merging_table = my_template_filled, file_path = "path/to/the/file/file_name.extension")

go_through_merges <- function(merging_table, file_path) {
  # merging_table = template table from the requirement. http://confluence.intra.arkea.com:8080/confluence/display/RISBAL/Template+table+de+jointures?ticket=ST-70083-l4sO1He0TEgDA6eMQJqaPfAzzQFO3TlPeip-20
  # file_path = path to the file to create
  
  for (i in 1:nrow(merging_table)) {
    row = merging_table[i,]
    table_x = ifelse(is.na(row[["Table_de_sortie_utilisee"]]), 
                     row[["Nom_de_la_table_x"]], 
                     row[["Table_de_sortie_utilisee"]])
    table_y = row[["Nom_de_la_table_y"]]
    result = row[["Nom_de_la_table_de_sortie"]]
    by_x = row[["Attributs_de_la_table_x"]]
    by_y = row[["Attributs_de_la_table_y"]]
    type_of_merge = row[["Type_de_jointure"]]
    
    # Launch control on data types
    if (exists(table_x) & exists(table_y)) {
      print("Control des types des attributs avant jointures:")
      control_type_data_before_merging(df1 = table_x, df2 = table_y, by_df1 = by_x, by_df2 = by_y)
    }
    #else {print("Le control des types des attributs avant jointures ne peut être effectué à cause de l'absence des tables dans l'environnement.")}
    
    # Transorm string to match the merge function format
    by_x <- transform_by_x_y(by_x)
    by_y <- transform_by_x_y(by_y)
    
    row <- lapply(X = row, FUN = as.character)
    
    texte_to_add <- get_template_merge(table_x = table_x, table_y = table_y, result = result, by_x = by_x, by_y = by_y, type_of_merge = type_of_merge)
    add_to_file(text = texte_to_add, file_path = file_path, append = TRUE)
    
    if(i%%3 == 0) { # do a temporary save every 3 merges
      temp_save_to_hdfs(db = result, path_hdfs = paste("/user/y8202/temp_save/", result, ".rds", sep = ""), file_path = file_path)
    }
  }
  temp_save_to_hdfs(db = result, path_hdfs = paste("/user/y8202/temp_save/", result, ".rds", sep = ""), file_path = file_path) # perform a save in HDFS after the last merge
}
Tiipiac/StressTestOptimization documentation built on Dec. 18, 2021, 5:08 p.m.