Skip to main content

CastFn

Type Alias CastFn 

Source
pub type CastFn = unsafe extern "C" fn(info: duckdb_function_info, count: idx_t, input: duckdb_vector, output: duckdb_vector) -> bool;
Expand description

The cast function callback signature.

  • info — cast function info; use CastFunctionInfo to wrap it.
  • count — number of rows in this chunk.
  • input — source vector (read from this).
  • output — destination vector (write results here).

§The return value

Return true when every row converted. What false does depends on the CastMode:

  • Normal (CAST): the query fails with a Conversion Error carrying the message from CastFunctionInfo::set_error or set_row_error. Set one before returning false. With none, a cast_callback! function reports CAST_FAILED_WITHOUT_MESSAGE; a hand-written callback makes DuckDB report Conversion Error: followed by nothing.
  • Try in a TRY_CAST: nothing. DuckDB discards the return value (src/execution/expression_executor/execute_cast.cpp calls the bound cast function and ignores what it returns; CAPICastFunction only records the message). Every row of output is returned as it stands — a row the callback never wrote holds whatever the vector held before, which can be a previous chunk’s value. So in Try mode, call set_row_error (or set the row to NULL yourself) for every row that failed; returning false is not a substitute.
  • Try in an internal value cast (Value::CastAs, used for example to convert a table function’s argument to its parameter type): false makes DuckDB throw Failed to cast value: followed by the message you set; true returns the row as written, so a failed row you nulled becomes a silent NULL argument.

So null each failed row and return false if any row failed: that is right in all three cases, and it is what DuckDB’s own casts do — their TRY_CAST loops null each failed row and return false as a summary.

A panic inside a cast_callback! body is the one case quack-rs handles for you: in Try mode every row of the chunk is set to NULL, because what the body had written before it panicked cannot be trusted.