voltrondata / substrait-r

An R Interface to the 'Substrait' Cross-Language Serialization for Relational Algebra
Other
27 stars 7 forks source link

Improve error messages for unsupported aggregate expressions #261

Closed paleolimbot closed 1 year ago

paleolimbot commented 1 year ago

Replaces the totally useless error messages we were getting before:

library(substrait, warn.conflicts = FALSE)

compiler <- substrait_compiler(data.frame(x = 1L))
compiler$.fns[["+"]] <- function(lhs, rhs) {
  substrait_call("+", lhs, rhs)
}

compiler$.fns[["sum"]] <- function(x) {
  substrait_call_agg("sum", x)
}

substrait_aggregate(compiler, new_col = 1L)
#> Error in as_substrait.default(result, "substrait.AggregateFunction"): Can't create substrait.AggregateFunction from object of type 'integer'
substrait_aggregate(compiler, new_col = sum(x) + 1L)
#> Error in as_substrait.substrait_proto_message(X[[i]], ...): identical(x_qualified_name, .qualified_name) is not TRUE

...with slightly better error messages:

library(substrait, warn.conflicts = FALSE)

compiler <- substrait_compiler(data.frame(x = 1L))
compiler$.fns[["+"]] <- function(lhs, rhs) {
  substrait_call("+", lhs, rhs)
}

compiler$.fns[["sum"]] <- function(x) {
  substrait_call_agg("sum", x)
}

substrait_aggregate(compiler, new_col = 1L)
#> Error in `FUN()`:
#> ! Can't convert non-aggregate expression to AggregateRel.Measure

#> Backtrace:
#>     ▆
#>  1. └─substrait::substrait_aggregate(compiler, new_col = 1L)
#>  2.   └─base::lapply(...) at substrait-r/R/aggregate-rel.R:22:2
#>  3.     ├─substrait (local) FUN(X[[i]], ...)
#>  4.     └─substrait:::as_substrait.quosure(X[[i]], ...) at substrait-r/R/substrait-proto.R:79:2
#>  5.       └─rlang::abort(...) at substrait-r/R/expression.R:19:8
substrait_aggregate(compiler, new_col = sum(x) + 1L)
#> Error in `FUN()`:
#> ! Can't convert AggregateFunction to Expression

#> Backtrace:
#>      ▆
#>   1. ├─substrait::substrait_aggregate(compiler, new_col = sum(x) + 1L)
#>   2. │ └─base::lapply(...) at substrait-r/R/aggregate-rel.R:22:2
#>   3. │   ├─substrait (local) FUN(X[[i]], ...)
#>   4. │   └─substrait:::as_substrait.quosure(X[[i]], ...) at substrait-r/R/substrait-proto.R:79:2
#>   5. │     └─substrait:::substrait_eval_quo(x) at substrait-r/R/expression.R:11:6
#>   6. │       └─rlang::eval_tidy(expr_quo, data = if (is.null(compiler)) NULL else compiler$eval_mask(.data)) at substrait-r/R/compiler.R:458:2
#>   7. └─sum(x) + 1L
#>   8.   └─substrait::substrait_call("+", lhs, rhs)
#>   9.     └─compiler$resolve_function(...) at substrait-r/R/compiler.R:426:2
#>  10.       └─base::lapply(args[!is_function_arg], as_substrait, "substrait.Expression") at substrait-r/R/compiler.R:235:6
#>  11.         ├─substrait (local) FUN(X[[i]], ...)
#>  12.         └─substrait:::as_substrait.substrait_AggregateFunction(X[[i]], ...) at substrait-r/R/substrait-proto.R:79:2
#>  13.           └─rlang::abort(...) at substrait-r/R/expression.R:157:6

Created on 2023-03-13 with reprex v2.0.2