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; useCastFunctionInfoto 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 aConversion Errorcarrying the message fromCastFunctionInfo::set_errororset_row_error. Set one before returningfalse. With none, acast_callback!function reportsCAST_FAILED_WITHOUT_MESSAGE; a hand-written callback makesDuckDBreportConversion Error:followed by nothing.Tryin aTRY_CAST: nothing.DuckDBdiscards the return value (src/execution/expression_executor/execute_cast.cppcalls the bound cast function and ignores what it returns;CAPICastFunctiononly records the message). Every row ofoutputis 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 inTrymode, callset_row_error(or set the row toNULLyourself) for every row that failed; returningfalseis not a substitute.Tryin an internal value cast (Value::CastAs, used for example to convert a table function’s argument to its parameter type):falsemakesDuckDBthrowFailed to cast value:followed by the message you set;truereturns the row as written, so a failed row you nulled becomes a silentNULLargument.
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.