Handling Errors

If your UDx encounters an unrecoverable error, it should throw a UdfException. The error triggers a rollback of the function call. In R, this function rollback occurs when the UDx executes an error action.

The following code shows how you can add error checking to your UDx. In this example, if the third column of the data frame does not match the specified Product ID, then the R UDx throws an error.

Calculate_Cost_w_Tax <- function(input.data.frame) {
  # Must match the Product ID 11444
  if ( !is.numeric(input.data.frame[, 3]) == 11444 ) {
    stop("Invalid Product ID!")
  } else {
    cost_w_tax <- data.frame(input.data.frame[, 1] * input.data.frame[, 2])
  }
  return(cost_w_tax)
}

Calculate_Cost_w_TaxFactory <- function() {
  list(name=Calculate_Cost_w_Tax,
       udxtype=c("scalar"),
       intype=c("float","float", "float"), 
       outtype=c("float"))
}

When an exception is raised in your R UDx, the UDx throws a UdfException and generates an error message.

=> SELECT Calculate_Cost_w_Tax(item_price, tax_rate, prod_id) FROM Inventory_Sales_Data;
vsql:sql_test_multiply.sql:21: ERROR 3399:  Failure in UDx RPC call InvokeProcessBlock():
Error calling processBlock() in User Defined Object [mul] at
[/scratch_a/release/svrtar30318/vbuild/vertica/OSS/UDxFence/RInterface.cpp:1308],
error code: 0, message: Exception in processBlockForR :Invalid Product ID!