Skip to content

Commit

Permalink
draft query_aggregate_and_match with precision
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Dec 17, 2024
1 parent 4401cd5 commit 4546f1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
14 changes: 2 additions & 12 deletions R/geocode.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ geocode <- function(input_table,
checkmate::assert_logical(cache)
checkmate::assert_names(
names(input_table),
must.include = "ID",
.var.name = "input_table"
must.include = "ID"
)


Expand Down Expand Up @@ -572,16 +571,7 @@ geocode <- function(input_table,
# DBI::dbReadTable(con, 'output_caso_01')

# DBI::dbRemoveTable(con, 'output_caso_01')
# DBI::dbRemoveTable(con, 'output_caso_02')
# DBI::dbRemoveTable(con, 'output_caso_03')
# DBI::dbRemoveTable(con, 'output_caso_04')
# DBI::dbRemoveTable(con, 'output_caso_05')
# DBI::dbRemoveTable(con, 'output_caso_06')
# DBI::dbRemoveTable(con, 'output_caso_07')
# DBI::dbRemoveTable(con, 'output_caso_08')
# DBI::dbRemoveTable(con, 'output_caso_09')
# DBI::dbRemoveTable(con, 'output_caso_10')
# DBI::dbRemoveTable(con, 'output_caso_11')




Expand Down
35 changes: 22 additions & 13 deletions tests/tests_rafa/tests_arrow_vs_duckdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,28 @@ range_lat <- sd(df$lat) *2



query_aggregate_and_match <- glue::glue(
"CREATE TABLE {output_tb} AS
WITH pre_aggregated_cnefe AS (
SELECT {cols_select}, AVG(lon) AS lon, AVG(lat) AS lat,
MAX(lon) - MIN(lon) as range_lon, MAX(lat) - MIN(lat) as range_lat,
FROM {y}
GROUP BY {cols_group}
)
SELECT {x}.ID, pre_aggregated_cnefe.lon, pre_aggregated_cnefe.lat
FROM {x} AS {x}
LEFT JOIN pre_aggregated_cnefe
ON {join_condition}
WHERE pre_aggregated_cnefe.lon IS NOT NULL;"
query_aggregate_and_match <- sprintf(
"CREATE TABLE %s AS
WITH pre_aggregated_cnefe AS (
SELECT %s AVG(lon) AS lon, AVG(lat) AS lat,
2 * STDDEV_SAMP(lon) as range_lon, 2 * STDDEV_SAMP(lat) as range_lat
FROM %s
GROUP BY %s
)
SELECT %s.ID, pre_aggregated_cnefe.lon, pre_aggregated_cnefe.lat,
pre_aggregated_cnefe.range_lon, pre_aggregated_cnefe.range_lat
FROM %s AS %s
LEFT JOIN pre_aggregated_cnefe
ON %s
WHERE pre_aggregated_cnefe.lon IS NOT NULL;",
output_tb, # new table
cols_select, # select
y, # from
cols_group, # group
x, # select
x, x, # from
join_condition # on
)



0 comments on commit 4546f1d

Please sign in to comment.