-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Importing large objects from client side #472
Open
toppyy
wants to merge
10
commits into
r-dbi:main
Choose a base branch
from
toppyy:import_large_object
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+156
−26
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f432ec8
Add postgresImportLargeObject and postgresWriteToLargeObject
toppyy 878d932
rm WriteToLargeObject as unnecessary
toppyy 32fd13f
filename -> filepath
toppyy 2476b0e
check file access in R; add tests for errors
toppyy 56ae5bd
fix example for ImportLargeObject
toppyy 7574866
Separate NULL/NA check for oid
toppyy 5fae348
don't run example for ImportLargeObject as depends on files
toppyy e71926b
add postgresImportLargeObject under pg misc. functions
toppyy 490dcd1
Retrieve error msg with PQerrorMessage; use the Oid-type for oids
toppyy f30f6ac
styler applied
toppyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#include <cpp11.hpp> | ||
#include <libpq-fe.h> | ||
#include <libpq/libpq-fs.h> | ||
|
||
#include <plogr.h> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
postgres |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
test_that("can import and read a large object", { | ||
con <- postgresDefault() | ||
on.exit(dbDisconnect(con)) | ||
test_file_path <- paste0(test_path(),'/data/large_object.txt') | ||
dbWithTransaction(con, { oid <- postgresImportLargeObject(con, test_file_path) }) | ||
expect_gt(oid,0) | ||
lo_data <- unlist(dbGetQuery(con, "select lo_get($1) as lo_data", params=list(oid))$lo_data[1]) | ||
large_object_txt <- as.raw(c(0x70, 0x6f, 0x73, 0x74, 0x67, 0x72, 0x65, 0x73)) # the string 'postgres' | ||
expect_equal(lo_data, large_object_txt) | ||
}) | ||
|
||
|
||
test_that("importing to an existing oid throws error", { | ||
con <- postgresDefault() | ||
on.exit(dbDisconnect(con)) | ||
test_file_path <- paste0(test_path(),'/data/large_object.txt') | ||
oid <- 1234 | ||
dbWithTransaction(con, { oid <- postgresImportLargeObject(con, test_file_path, oid) }) | ||
|
||
expect_error( | ||
dbWithTransaction(con, { oid <- postgresImportLargeObject(con, test_file_path, oid) }) | ||
) | ||
dbExecute(con, "select lo_unlink($1) as lo_data", params=list(oid)) | ||
}) | ||
|
||
|
||
test_that("import from a non-existing path throws error", { | ||
con <- postgresDefault() | ||
on.exit(dbDisconnect(con)) | ||
test_file_path <- paste0(test_path(),'/data/large_object_that_does_not_exist.txt') | ||
expect_error( | ||
dbWithTransaction(con, { oid <- postgresImportLargeObject(con, test_file_path) }) | ||
) | ||
}) | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will the error look like if we let libpq do the check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libpq returns an InvalidOid (a zero). Letting libpq do the check has the downside that we cannot inform the user of the specific problem as libpq's
lo_import_with_oid()
returns a zero if the 1) file does not exists or 2) the assigned oid is already in use.So currently if the accessibility of the file has changed, an error is thrown with the message
Import failed. Maybe you tried to write to an existing oid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see:
Would that work well enough?
Also: how do we auto-assign the OID?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I was unaware of
PQerrorMessage()
- that solves the problem! With the changes in the latest commit, when importing to an existing oid we get:(It's a bit clunky as the term "error" is repeated, but informative nonetheless.)
pqlib auto-assigns the oid if the argument 'oid' is zero. From the docs:
I also changed the return- and argument types to 'Oid' from int as oid's are implemented as unsigned integers.