8000 Better parsing errors · Issue #219 · tidyverse/duckplyr · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Better parsing errors #219

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

Closed
hadley opened this issue Jul 26, 2024 · 7 comments · Fixed by duckdb/duckdb-r#521
Closed

Better parsing errors #219

hadley opened this issue Jul 26, 2024 · 7 comments · Fixed by duckdb/duckdb-r#521
Labels
duckdb 🦆 Issues where work in the duckb package is needed
Milestone

Comments

@hadley
Copy link
Member
hadley commented Jul 26, 2024
> DBI::dbGetQuery(con, "SELECT count(*), date FROM SELECT CAST(time AS date), count(*) GROUP by date;")
Error:
! {"exception_type":"Parser","exception_message":"syntax error at or near \"SELECT\"","position":"27","error_subtype":"SYNTAX_ERROR"}

Would be nicer as:

Error in dbGetQuery():
* Failed to parse SQL
* syntax error at or near \"SELECT\"

Or similar

@hadley hadley added this to the 1.0.0 milestone Sep 25, 2024
@krlmlr
Copy link
Member
krlmlr commented Oct 16, 2024

DoD:

  • JSON from error message is parsed and translated to structured classed exceptions
  • print() method for our exceptions

@krlmlr krlmlr added the duckdb 🦆 Issues where work in the duckb package is needed label Oct 16, 2024
@krlmlr
Copy link
Member
krlmlr commented Oct 23, 2024

Parse JSON with duckdb, don't need jsonlite as dependency.

@krlmlr
Copy link
Member
krlmlr commented Oct 26, 2024
library(DBI)

con <- dbConnect(duckdb::duckdb())
sql <- "SELECT count(*), date FROM SELECT CAST(time AS date), count(*) GROUP by date;"
dbGetQuery(con, sql)
#> Error: {"exception_type":"Parser","exception_message":"syntax error at or near \"SELECT\"","position":"27","error_subtype":"SYNTAX_ERROR"}

e <- tryCatch(
  dbGetQuery(con, sql),
  error = identity
)
msg <- conditionMessage(e)

dbExecute(con, "LOAD json")
#> [1] 0
out <- dbGetQuery(con, "SELECT json_transform(?, json_structure(?)) AS msg", params = list(msg, msg))
as.list(out[[1]])
#> $exception_type
#> [1] "Parser"
#> 
#> $exception_message
#> [1] "syntax error at or near \"SELECT\""
#> 
#> $position
#> [1] "27"
#> 
#> $error_subtype
#> [1] "SYNTAX_ERROR"

dbDisconnect(con)

Created on 2024-10-26 with reprex v2.1.1

@krlmlr
Copy link
Member
krlmlr commented Oct 26, 2024

Currently on my machine:

library(DBI)

con <- dbConnect(duckdb::duckdb())
sql <- "SELECT count(*), date FROM SELECT CAST(time AS date), count(*) GROUP by date;"
dbGetQuery(con, sql)
#> Error in `dbSendQuery()` at DBI/R/dbGetQuery_DBIConnection_character.R:5:3:
#> ! syntax error at or near "SELECT"
#> ℹ exception_type: Parser
#> ℹ position: 27
#> ℹ error_subtype: SYNTAX_ERROR
rlang::last_error()
#> Error: Can't show last error because no error was recorded yet

Created on 2024-10-26 with reprex v2.1.1
We'd have to do additinal gymnastics to show dbGetQuery() in the error message. I wonder why last_error() and last_trace() don't work, I'm calling rlang::abort() internally.

@krlmlr
Copy link
Member
krlmlr commented Oct 26, 2024

Oh, it's a reprex thing, it's shown in the default backtrace.

8000

@krlmlr
Copy link
Member
krlmlr commented Oct 26, 2024

We can parse JSON, but this is really needed only for uncaught duckdb exceptions. We get much better results here with catching and adding context in the C++ glue:

library(DBI)

con <- dbConnect(duckdb::duckdb())
sql <- "SELECT count(*), date FROM SELECT CAST(time AS date), count(*) GROUP by date;"
dbGetQuery(con, sql)
#> Error in `dbSendQuery()` at DBI/R/dbGetQuery_DBIConnection_character.R:5:3:
#> ! rapi_prepare: Failed to extract statements:
#> Parser Error: syntax error at or near "SELECT"
#> LINE 1: SELECT count(*), date FROM SELECT CAST(time AS date), count(*) GRO...
#>                                    ^

Created on 2024-10-26 with reprex v2.1.1

I keep the JSON parsing code, but it will only be in effect when exceptions come through unfiltered. We could use JSON everywhere, though: duckdb/duckdb-r#519.

@krlmlr
Copy link
Member
krlmlr commented Oct 26, 2024

The JSON parsing now adds little extra value, I can't even test it. Whenever we see a JSON string on the R side, we want to catch and rethrow in C++. Much later, we can think about JSONifying everything in duckdb/duckdb-r#519.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duckdb 🦆 Issues where work in the duckb package is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0