Skip to content

Commit eb18af2

Browse files
[KQP Constant folding] Do not fold SafeCast because it could produce … (#20276)
2 parents 0d17f55 + a238aa3 commit eb18af2

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

ydb/core/kqp/opt/kqp_constant_folding_transformer.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,30 @@ using namespace NKikimr::NKqp;
1010
using namespace NYql::NDq;
1111

1212
namespace {
13+
THashSet<TString> notAllowedDataTypeForSafeCast{"JsonDocument"};
14+
15+
bool IsSuitableToExtractExpr(const TExprNode::TPtr &input) {
16+
if (auto maybeSafeCast = TExprBase(input).Maybe<TCoSafeCast>()) {
17+
auto maybeDataType = maybeSafeCast.Cast().Type().Maybe<TCoDataType>();
18+
if (!maybeDataType) {
19+
if (const auto maybeOptionalType = maybeSafeCast.Cast().Type().Maybe<TCoOptionalType>()) {
20+
maybeDataType = maybeOptionalType.Cast().ItemType().Maybe<TCoDataType>();
21+
}
22+
}
23+
return (maybeDataType && !notAllowedDataTypeForSafeCast.contains(maybeDataType.Cast().Type().Value()));
24+
}
25+
return true;
26+
}
27+
1328
/**
1429
* Traverse a lambda and create a mapping from nodes to nodes wrapped in EvaluateExpr callable
1530
* We check for literals specifically, since they shouldn't be evaluated
1631
*/
1732
void ExtractConstantExprs(const TExprNode::TPtr& input, TNodeOnNodeOwnedMap& replaces, TExprContext& ctx, bool foldUdfs = true) {
33+
if (!IsSuitableToExtractExpr(input)) {
34+
return;
35+
}
36+
1837
if (TCoLambda::Match(input.Get())) {
1938
auto lambda = TExprBase(input).Cast<TCoLambda>();
2039
return ExtractConstantExprs(lambda.Body().Ptr(), replaces, ctx);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
SELECT *
22
FROM `/Root/S` as S
33
WHERE S.payload2 = String::HexDecode("54");
4+
5+
SELECT CAST("[6]" AS JsonDocument) as doc
6+
FROM `/Root/R` as R;

0 commit comments

Comments
 (0)