Skip to content

[KQP Constant folding] Do not fold SafeCast because it could produce unexpected result #20457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: stable-25-1-analytics
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions ydb/core/kqp/opt/kqp_constant_folding_transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,30 @@ using namespace NKikimr::NKqp;
using namespace NYql::NDq;

namespace {
THashSet<TString> notAllowedDataTypeForSafeCast{"JsonDocument"};

bool IsSuitableToExtractExpr(const TExprNode::TPtr &input) {
if (auto maybeSafeCast = TExprBase(input).Maybe<TCoSafeCast>()) {
auto maybeDataType = maybeSafeCast.Cast().Type().Maybe<TCoDataType>();
if (!maybeDataType) {
if (const auto maybeOptionalType = maybeSafeCast.Cast().Type().Maybe<TCoOptionalType>()) {
maybeDataType = maybeOptionalType.Cast().ItemType().Maybe<TCoDataType>();
}
}
return (maybeDataType && !notAllowedDataTypeForSafeCast.contains(maybeDataType.Cast().Type().Value()));
}
return true;
}

/**
* Traverse a lambda and create a mapping from nodes to nodes wrapped in EvaluateExpr callable
* We check for literals specifically, since they shouldn't be evaluated
*/
void ExtractConstantExprs(const TExprNode::TPtr& input, TNodeOnNodeOwnedMap& replaces, TExprContext& ctx, bool foldUdfs = true) {
if (!IsSuitableToExtractExpr(input)) {
return;
}

if (TCoLambda::Match(input.Get())) {
auto lambda = TExprBase(input).Cast<TCoLambda>();
return ExtractConstantExprs(lambda.Body().Ptr(), replaces, ctx);
Expand Down
3 changes: 3 additions & 0 deletions ydb/core/kqp/ut/join/data/queries/udf_constant_fold.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
SELECT *
FROM `/Root/S` as S
WHERE S.payload2 = String::HexDecode("54");

SELECT CAST("[6]" AS JsonDocument) as doc
FROM `/Root/R` as R;
Loading