Skip to content

Commit

Permalink
Fix try_cast to stop processing rows with errors (#7352)
Browse files Browse the repository at this point in the history
Summary:
try_cast(varchar as timestamp) used to process all rows including the ones that
already generated errors. Rows with errors have undefined values in the result
vector, hence, processing these may result in new failures or even crashes.

Fixes #7350

Pull Request resolved: #7352

Reviewed By: xiaoxmeng

Differential Revision: D50856235

Pulled By: mbasmanova

fbshipit-source-id: a4c0bde2d7ce9fe0dff167eb9e79fb76a436fca6
  • Loading branch information
mbasmanova authored and facebook-github-bot committed Nov 1, 2023
1 parent aead17d commit fcc49a6
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions velox/expression/CastExpr-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,22 @@ void CastExpr::applyCastPrimitives(
if (queryConfig.adjustTimestampToTimezone()) {
auto sessionTzName = queryConfig.sessionTimezone();
if (!sessionTzName.empty()) {
// When context.throwOnError is false, some rows will be marked as
// 'failed'. These rows should not be processed further. 'remainingRows'
// will contain a subset of 'rows' that have passed all the checks (e.g.
// keys are not nulls and number of keys and values is the same).
exec::LocalSelectivityVector remainingRows(context, rows);
context.deselectErrors(*remainingRows);

// locate_zone throws runtime_error if the timezone couldn't be found
// (so we're safe to dereference the pointer).
auto* timeZone = date::locate_zone(sessionTzName);
auto rawTimestamps = resultFlatVector->mutableRawValues();

applyToSelectedNoThrowLocal(context, rows, result, [&](int row) {
rawTimestamps[row].toGMT(*timeZone);
});
applyToSelectedNoThrowLocal(
context, *remainingRows, result, [&](int row) {
rawTimestamps[row].toGMT(*timeZone);
});
}
}
}
Expand Down

0 comments on commit fcc49a6

Please sign in to comment.