Skip to content

Commit

Permalink
feat: <SQLContext>$register_globals() (#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi authored Apr 27, 2024
1 parent b51c397 commit 7d8ee7f
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 22 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Polars R Package (development version)

### New features

- New method `<SQLContext>$register_globals()` (#1064).

## Polars R Package 0.16.2

### New features
Expand Down
74 changes: 58 additions & 16 deletions R/sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ print.RPolarsSQLContext = function(x, ...) {


#' Initialise a new SQLContext
#' @name pl_SQLContext
#' @description Create a new SQLContext and register the given LazyFrames.
#'
#' Create a new SQLContext and register the given LazyFrames.
#' @param ... Name-value pairs of [LazyFrame][LazyFrame_class] like objects to register.
#' @return RPolarsSQLContext
#' @return An [SQLContext][SQLContext_class]
#' @examplesIf polars_info()$features$sql
#' ctx = pl$SQLContext(mtcars = mtcars)
#' ctx
Expand All @@ -57,8 +57,9 @@ pl_SQLContext = function(...) {
}


#' @title Execute SQL query against the registered data
#' @description Parse the given SQL query and execute it against the registered frame data.
#' Execute SQL query against the registered data
#'
#' Parse the given SQL query and execute it against the registered frame data.
#' @param query A valid string SQL query.
#' @return A [LazyFrame][LazyFrame_class]
#' @examplesIf polars_info()$features$sql
Expand All @@ -71,11 +72,14 @@ SQLContext_execute = function(query) {
}


#' @title Register a single data as a table
#' @description Register a single frame as a table, using the given name.
#' Register a single data as a table
#'
#' Register a single frame as a table, using the given name.
#'
#' If a table with the same name is already registered, it will be overwritten.
#' @param name A string name to register the frame as.
#' @param frame A [LazyFrame][LazyFrame_class] like object to register.
#' @return Returns the [SQLContext_class] object invisibly.
#' @return Returns the [SQLContext][SQLContext_class] object invisibly.
#' @examplesIf polars_info()$features$sql
#' ctx = pl$SQLContext()
#' ctx$register("mtcars", mtcars)
Expand All @@ -88,10 +92,11 @@ SQLContext_register = function(name, frame) {
}


#' @title Register multiple data as tables
#' @description Register multiple frames as tables.
#' Register multiple data as tables
#'
#' Register multiple frames as tables.
#' @inherit SQLContext_register details return
#' @param ... Name-value pairs of [LazyFrame][LazyFrame_class] like objects to register.
#' @return Returns the [SQLContext_class] object invisibly.
#' @examplesIf polars_info()$features$sql
#' ctx = pl$SQLContext()
#' r_df = mtcars
Expand Down Expand Up @@ -125,10 +130,11 @@ SQLContext_register_many = function(...) {
}


#' @title Unregister tables by name
#' @description Unregister tables by name.
#' Unregister tables by name
#'
#' Unregister tables by name.
#' @inherit SQLContext_register return
#' @param names A character vector of table names to unregister.
#' @return Returns the [SQLContext_class] object invisibly.
#' @examplesIf polars_info()$features$sql
#' # Initialise a new SQLContext and register the given tables.
#' ctx = pl$SQLContext(x = mtcars, y = mtcars, z = mtcars)
Expand All @@ -146,8 +152,9 @@ SQLContext_unregister = function(names) {
}


#' @title List registered tables
#' @description Return a character vector of the registered table names.
#' List registered tables
#'
#' Return a character vector of the registered table names.
#' @return A character vector of the registered table names.
#' @examplesIf polars_info()$features$sql
#' ctx = pl$SQLContext()
Expand All @@ -160,3 +167,38 @@ SQLContext_tables = function() {
.pr$SQLContext$get_tables(self) |>
unwrap("in $tables()")
}


#' Register all polars DataFrames/LazyFrames found in the environment
#'
#' Automatically maps variable names to table names.
#' @inherit SQLContext_register details return
#' @param ... Ignored.
#' @param envir The environment to search for polars DataFrames/LazyFrames.
#' @seealso
#' - [`<SQLContext>$register()`][SQLContext_register]
#' - [`<SQLContext>$register_many()`][SQLContext_register_many]
#' - [`<SQLContext>$unregister()`][SQLContext_unregister]
#' @examplesIf polars_info()$features$sql
#' df1 = pl$DataFrame(a = 1:3, b = c("x", NA, "z"))
#' df2 = pl$LazyFrame(a = 2:4, c = c("t", "w", "v"))
#'
#' # Register frames directly from variables found in the current environment.
#' ctx = pl$SQLContext()$register_globals()
#' ctx$tables()
#'
#' ctx$execute(
#' "SELECT a, b, c FROM df1 LEFT JOIN df2 USING (a) ORDER BY a DESC"
#' )$collect()
SQLContext_register_globals = function(..., envir = parent.frame()) {
Filter(
\(x) inherits(get(x, envir = envir), c("RPolarsDataFrame", "RPolarsLazyFrame")),
ls(envir = envir)
) |>
sapply(\(x) get(x, envir = envir), simplify = FALSE, USE.NAMES = TRUE) |>
do.call(self$register_many, args = _) |>
result() |>
unwrap("in $register_globals()")

invisible(self)
}
5 changes: 4 additions & 1 deletion man/SQLContext_register.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions man/SQLContext_register_globals.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/SQLContext_register_many.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/SQLContext_unregister.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/pl_SQLContext.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/after-wrappers.md
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,8 @@
Code
ls(.pr$env[[class_name]])
Output
[1] "execute" "register" "register_many" "tables"
[5] "unregister"
[1] "execute" "register" "register_globals" "register_many"
[5] "tables" "unregister"

---

Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,32 @@ test_that("SQLContext_register, register_many, unregister", {
ctx$unregister(c("mtcars", "mtcars2"))
expect_equal(sort(ctx$tables()), sort(c("mtcars3", "mtcars4")))
})


test_that("SQLContext_register_globals", {
skip_if_not(polars_info()$features$sql)

ctx = pl$SQLContext()
f1 = pl$DataFrame(x = 1)
f2 = pl$LazyFrame(x = 2)

ctx$register_globals()
expect_equal(ctx$tables(), c("f1", "f2"))

func1 = function(ctx) {
f3 = pl$DataFrame(x = 3)
ctx$register_globals()
}
func2 = function(ctx) {
f4 = pl$LazyFrame(x = 4)
ctx$register_globals(envir = parent.frame())
}

ctx2 = pl$SQLContext()
func1(ctx2)
expect_equal(ctx2$tables(), c("f3"))

ctx3 = pl$SQLContext()
func2(ctx3)
expect_equal(ctx3$tables(), c("f1", "f2"))
})

0 comments on commit 7d8ee7f

Please sign in to comment.