LoginSignup
1
0

More than 5 years have passed since last update.

RSQLite が Window関数のサポートを追加

Last updated at Posted at 2019-04-21

RSQLite は、R からSQLiteデータベースを操作するパッケージですが、つい最近 Window関数 に対応したようです。本家SQLiteが2018年9月15に機能を追加して、その後いくつかの改良がなされています (https://www.sqlite.org/draft/changes.html) 。RSQLite の対応は 2018年12月30日 (#273)、SQLite 3.25.3 を利用しているようです。CRAN 版はまだ更新されていないので、利用するには開発版をGitHubからインストールする必要があります。

windowfunc_test <- function() {
  db <- DBI::dbConnect(RSQLite::SQLite(), dbname=":memory:")

  DBI::dbExecute(db, "CREATE TABLE t0(x INTEGER PRIMARY KEY, y TEXT)")
  DBI::dbExecute(db, "INSERT INTO t0 VALUES (1, 'aaa'), (2, 'ccc'), (3, 'bbb')")

  res <- DBI::dbGetQuery(
    db, "SELECT x, y, row_number() OVER (ORDER BY y) AS row_number FROM t0 ORDER BY x")
  print(res)  
}

CRAN 版は未対応。

install.packages("RSQLite")
print(packageVersion("RSQLite"))
windowfunc_test()

[1] `2.1.1`
Error in result_create(conn@ptr, statement): near "(": syntax error
Traceback:

1. windowfunc_test()
2. DBI::dbSendQuery(db, "SELECT x, y, row_number() OVER (ORDER BY y) AS row_number FROM t0 ORDER BY x")
   # at line 7 of file <text>
3. DBI::dbSendQuery(db, "SELECT x, y, row_number() OVER (ORDER BY y) AS row_number FROM t0 ORDER BY x")
4. .local(conn, statement, ...)
5. new("SQLiteResult", sql = statement, ptr = result_create(conn@ptr, 
 .     statement), conn = conn, bigint = conn@bigint)
6. initialize(value, ...)
7. initialize(value, ...)
8. result_create(conn@ptr, statement)

GitHubの開発版では対応。

devtools::install_github("r-dbi/RSQLite")
print(packageVersion("RSQLite"))
windowfunc_test()

[1] `2.1.1.9002`
  x   y row_number
1 1 aaa          1
2 2 ccc          3
3 3 bbb          2
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0