diff options
| author | Daniil Timižev <[email protected]> | 2026-07-06 11:03:13 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-07-06 11:03:13 +0300 |
| commit | f6ace39445dea0380d557204205dfdaf5ec4a9d0 (patch) | |
| tree | c49371cceab4d21c9d9e5572aba265048dbdfd43 | |
| parent | c97a871fd1a93db1f50febcd9cb9cfce3d3eb3da (diff) | |
Format and tidy KQP JI code (#45535)
| -rw-r--r-- | ydb/core/kqp/executer_actor/kqp_tasks_graph.cpp | 23 | ||||
| -rw-r--r-- | ydb/core/kqp/opt/kqp_query_plan.cpp | 4 | ||||
| -rw-r--r-- | ydb/core/kqp/opt/logical/kqp_opt_log_indexes.cpp | 19 | ||||
| -rw-r--r-- | ydb/core/kqp/opt/logical/kqp_opt_log_json_index.cpp | 210 | ||||
| -rw-r--r-- | ydb/core/kqp/opt/logical/kqp_opt_log_json_index.h | 6 | ||||
| -rw-r--r-- | ydb/library/json_index/json_index.cpp | 148 | ||||
| -rw-r--r-- | ydb/library/json_index/json_index.h | 22 |
7 files changed, 202 insertions, 230 deletions
diff --git a/ydb/core/kqp/executer_actor/kqp_tasks_graph.cpp b/ydb/core/kqp/executer_actor/kqp_tasks_graph.cpp index b80bbe7f887..e880c0e71bb 100644 --- a/ydb/core/kqp/executer_actor/kqp_tasks_graph.cpp +++ b/ydb/core/kqp/executer_actor/kqp_tasks_graph.cpp @@ -462,8 +462,8 @@ void AppendMKQLValueToToken(TString& token, NKikimr::NMiniKQL::TType* type, NUdf break; case NUdf::EDataSlot::Bool: - NJsonIndex::AppendJsonIndexLiteral(token, - value.Get<bool>() ? NBinaryJson::EEntryType::BoolTrue : NBinaryJson::EEntryType::BoolFalse); + NJsonIndex::AppendJsonIndexLiteral( + token, value.Get<bool>() ? NBinaryJson::EEntryType::BoolTrue : NBinaryJson::EEntryType::BoolFalse); break; case NUdf::EDataSlot::Int8: { @@ -539,9 +539,7 @@ void AppendMKQLValueToToken(TString& token, NKikimr::NMiniKQL::TType* type, NUdf } } -TString ResolveFullTextQueryToken(const NKqpProto::TKqpFullTextSource::TKqpQuerySettings::TQueryToken& token, - const TStageInfo& stageInfo) -{ +TString ResolveFullTextQueryToken(const NKqpProto::TKqpFullTextSource::TKqpQuerySettings::TQueryToken& token, const TStageInfo& stageInfo) { TString fullToken = token.GetToken(); if (token.GetParamName().empty()) { return fullToken; @@ -558,18 +556,17 @@ TString ResolveFullTextQueryToken(const NKqpProto::TKqpFullTextSource::TKqpQuery return fullToken; } -TVector<TString> ResolveFullTextQueryTokenExpanded(const NKqpProto::TKqpFullTextSource::TKqpQuerySettings::TQueryToken& token, - const TStageInfo& stageInfo) -{ +TVector<TString> ResolveFullTextQueryTokenExpanded( + const NKqpProto::TKqpFullTextSource::TKqpQuerySettings::TQueryToken& token, const TStageInfo& stageInfo) { const TString baseToken = token.GetToken(); if (token.GetParamName().empty()) { - return {baseToken}; + return { baseToken }; } auto* paramPtr = stageInfo.Meta.Tx.Params->GetParameterUnboxedValuePtr(token.GetParamName()); if (!paramPtr) { LOG_W("Failed to get parameter value for token: " << token.GetParamName()); - return {baseToken}; + return { baseToken }; } auto [type, value] = *paramPtr; @@ -602,10 +599,10 @@ TVector<TString> ResolveFullTextQueryTokenExpanded(const NKqpProto::TKqpFullText result.emplace_back(std::move(currentToken)); } } else { - return {ResolveFullTextQueryToken(token, stageInfo)}; + return { ResolveFullTextQueryToken(token, stageInfo) }; } - return result.empty() ? TVector<TString>{baseToken} : result; + return result.empty() ? TVector<TString>{ baseToken } : result; } void AddQueryPathParam(TKqpTasksGraph::TTaskType& task, const TIntrusivePtr<NKikimr::NKqp::TUserRequestContext>& userRequestContext) { @@ -1596,7 +1593,7 @@ void TKqpTasksGraph::FillInputDesc(NYql::NDqProto::TTaskInput& inputDesc, const input.Meta.StreamLookupSettings->MutableSnapshot()->SetStep(snapshot.Step); input.Meta.StreamLookupSettings->MutableSnapshot()->SetTxId(snapshot.TxId); if (input.Meta.StreamLookupSettings->GetLookupStrategy() == NKqpProto::EStreamLookupStrategy::LOCK_AND_LOOKUP) { - input.Meta.StreamLookupSettings->SetAllowInconsistentReads(true); + input.Meta.StreamLookupSettings->SetAllowInconsistentReads(true); } } else { YQL_ENSURE(GetMeta().AllowInconsistentReads || isTableImmutable, "Expected valid snapshot or enabled inconsistent read mode"); diff --git a/ydb/core/kqp/opt/kqp_query_plan.cpp b/ydb/core/kqp/opt/kqp_query_plan.cpp index db5d9a367b6..dd050c891a5 100644 --- a/ydb/core/kqp/opt/kqp_query_plan.cpp +++ b/ydb/core/kqp/opt/kqp_query_plan.cpp @@ -842,8 +842,8 @@ private: std::vector<TString> tokens; if (settings.Tokens) { - YQL_ENSURE(indexDesc->Type == TIndexDescription::EType::GlobalJson || - indexDesc->Type == TIndexDescription::EType::GlobalJsonCompact); + YQL_ENSURE( + indexDesc->Type == TIndexDescription::EType::GlobalJson || indexDesc->Type == TIndexDescription::EType::GlobalJsonCompact); for (const auto& token : TExprBase(settings.Tokens).Cast<TExprList>()) { auto pair = token.Cast<TExprList>(); diff --git a/ydb/core/kqp/opt/logical/kqp_opt_log_indexes.cpp b/ydb/core/kqp/opt/logical/kqp_opt_log_indexes.cpp index f91fe798e81..6f77c74e32b 100644 --- a/ydb/core/kqp/opt/logical/kqp_opt_log_indexes.cpp +++ b/ydb/core/kqp/opt/logical/kqp_opt_log_indexes.cpp @@ -2103,8 +2103,7 @@ TMaybeNode<TExprBase> KqpSelectJsonIndex(const NYql::NNodes::TExprBase& node, NY THashSet<TString> jsonIndexedColumns; for (const auto& indexInfo : mainTableDesc.Metadata->Indexes) { - if (indexInfo.Type != TIndexDescription::EType::GlobalJson && - indexInfo.Type != TIndexDescription::EType::GlobalJsonCompact) { + if (indexInfo.Type != TIndexDescription::EType::GlobalJson && indexInfo.Type != TIndexDescription::EType::GlobalJsonCompact) { continue; } @@ -2125,8 +2124,7 @@ TMaybeNode<TExprBase> KqpSelectJsonIndex(const NYql::NNodes::TExprBase& node, NY std::optional<TString> selectedIndex; for (const auto& indexInfo : mainTableDesc.Metadata->Indexes) { - if (indexInfo.Type != TIndexDescription::EType::GlobalJson && - indexInfo.Type != TIndexDescription::EType::GlobalJsonCompact) { + if (indexInfo.Type != TIndexDescription::EType::GlobalJson && indexInfo.Type != TIndexDescription::EType::GlobalJsonCompact) { continue; } @@ -2148,6 +2146,8 @@ TMaybeNode<TExprBase> KqpSelectJsonIndex(const NYql::NNodes::TExprBase& node, NY } const auto& jsonIndexSettings = expectedSettings.value(); + + // clang-format off auto searchColumns = Build<TCoAtomList>(ctx, node.Pos()) .Add(Build<TCoAtom>(ctx, node.Pos()).Value(jsonIndexSettings.ColumnName).Done()) .Done(); @@ -2165,9 +2165,11 @@ TMaybeNode<TExprBase> KqpSelectJsonIndex(const NYql::NNodes::TExprBase& node, NY .Input(newInput) .Lambda(flatMap.Lambda()) .Done(); + // clang-format on } -TMaybeNode<TExprBase> KqpRewriteFlatMapOverJsonRead(const NYql::NNodes::TExprBase& node, NYql::TExprContext& ctx, const TKqpOptimizeContext& kqpCtx) { +TMaybeNode<TExprBase> KqpRewriteFlatMapOverJsonRead( + const NYql::NNodes::TExprBase& node, NYql::TExprContext& ctx, const TKqpOptimizeContext& kqpCtx) { if (!node.Maybe<TCoFlatMap>()) { return node; } @@ -2182,8 +2184,7 @@ TMaybeNode<TExprBase> KqpRewriteFlatMapOverJsonRead(const NYql::NNodes::TExprBas YQL_ENSURE(tableDesc.Metadata); auto [implTable, indexDesc] = tableDesc.Metadata->GetIndex(read.Index().Value()); - if (indexDesc->Type != TIndexDescription::EType::GlobalJson && - indexDesc->Type != TIndexDescription::EType::GlobalJsonCompact) { + if (indexDesc->Type != TIndexDescription::EType::GlobalJson && indexDesc->Type != TIndexDescription::EType::GlobalJsonCompact) { return {}; } @@ -2197,10 +2198,11 @@ TMaybeNode<TExprBase> KqpRewriteFlatMapOverJsonRead(const NYql::NNodes::TExprBas auto expectedSettings = CollectJsonIndexPredicate(flatMap.Lambda().Body(), node, ctx, jsonIndexedColumns); if (!expectedSettings.has_value()) { - ctx.AddError(std::move(expectedSettings.error())); + ctx.AddError(expectedSettings.error()); return {}; } + // clang-format off const auto& jsonIndexSettings = expectedSettings.value(); auto searchColumns = Build<TCoAtomList>(ctx, node.Pos()) .Add(Build<TCoAtom>(ctx, node.Pos()).Value(jsonIndexSettings.ColumnName).Done()) @@ -2219,6 +2221,7 @@ TMaybeNode<TExprBase> KqpRewriteFlatMapOverJsonRead(const NYql::NNodes::TExprBas .Input(newInput) .Lambda(flatMap.Lambda()) .Done(); + // clang-format on } // Parse-once accessor over a HybridRank node's arguments. diff --git a/ydb/core/kqp/opt/logical/kqp_opt_log_json_index.cpp b/ydb/core/kqp/opt/logical/kqp_opt_log_json_index.cpp index 499e58f9b44..a8ae0cbe91e 100644 --- a/ydb/core/kqp/opt/logical/kqp_opt_log_json_index.cpp +++ b/ydb/core/kqp/opt/logical/kqp_opt_log_json_index.cpp @@ -1,16 +1,18 @@ #include "kqp_opt_log_json_index.h" -#include <expected> - -#include <ydb/library/json_index/json_index.h> #include <ydb/core/kqp/common/kqp_yql.h> #include <ydb/core/kqp/opt/kqp_opt_impl.h> #include <ydb/core/kqp/provider/yql_kikimr_provider_impl.h> +#include <ydb/library/json_index/json_index.h> + #include <yql/essentials/core/sql_types/yql_atom_enums.h> #include <yql/essentials/core/yql_expr_type_annotation.h> #include <yql/essentials/core/yql_opt_utils.h> +#include <expected> +#include <utility> + namespace NKikimr::NKqp::NOpt { using namespace NYql; @@ -19,7 +21,7 @@ using namespace NJsonIndex; namespace { -static constexpr char kErrorMessage[] = "Failed to extract jsonpath tokens from the predicate: "; +constexpr std::string_view kErrorMessage = "Failed to extract jsonpath tokens from the predicate: "; struct TPredicateCollectResult { TString ColumnName; @@ -35,8 +37,7 @@ struct TJsonNodeParams { }; TPredicateCollectResult MakeCollectError(TExprContext& ctx, TPositionHandle pos, TStringBuf message) { - return TPredicateCollectResult{"", TCollectResult( - TIssue(ctx.GetPosition(pos), TString{message}))}; + return TPredicateCollectResult{ "", TCollectResult(TIssue(ctx.GetPosition(pos), TString{ message })) }; } bool IsJsonValueReturningNonIndexable(std::optional<EDataSlot> slot) { @@ -88,7 +89,7 @@ TExprBase UnwrapPredicate(TExprBase node) { // Returns true if a cast/convert from `from` to `to` is allowed for JSON index predicate extraction bool IsAllowedCastForJsonIndex(EDataSlot from, EDataSlot to) { - static const THashSet<EDataSlot> allowedStringTypes = {EDataSlot::String, EDataSlot::Utf8}; + static const THashSet<EDataSlot> allowedStringTypes = { EDataSlot::String, EDataSlot::Utf8 }; if (allowedStringTypes.contains(from) && allowedStringTypes.contains(to)) { return true; } @@ -123,7 +124,7 @@ std::optional<EDataSlot> GetBaseDataSlot(const TExprBase& node) { bool IsSupportedCast(const TExprBase& from, const TExprBase& to) { const auto fromSlot = GetBaseDataSlot(from); - const auto toSlot = GetBaseDataSlot(to); + const auto toSlot = GetBaseDataSlot(to); return fromSlot.has_value() && toSlot.has_value() && IsAllowedCastForJsonIndex(*fromSlot, *toSlot); } @@ -172,9 +173,7 @@ std::optional<TString> EncodeValueToJsonPath(const TExprBase& node, bool negativ if (node.Maybe<TCoBool>()) { const auto boolValue = FromString<bool>(node.Cast<TCoBool>().Literal().Value()); - AppendJsonIndexLiteral(value, boolValue - ? NBinaryJson::EEntryType::BoolTrue - : NBinaryJson::EEntryType::BoolFalse); + AppendJsonIndexLiteral(value, boolValue ? NBinaryJson::EEntryType::BoolTrue : NBinaryJson::EEntryType::BoolFalse); return value; } @@ -192,35 +191,45 @@ std::optional<TString> EncodeValueToJsonPath(const TExprBase& node, bool negativ if (node.Maybe<TCoFloat>()) { double literalValue = static_cast<double>(FromString<float>(node.Cast<TCoFloat>().Literal().Value())); - if (negative) literalValue = -literalValue; + if (negative) { + literalValue = -literalValue; + } AppendJsonIndexLiteral(value, NBinaryJson::EEntryType::Number, {}, &literalValue); return value; } if (node.Maybe<TCoDouble>()) { double literalValue = FromString<double>(node.Cast<TCoDouble>().Literal().Value()); - if (negative) literalValue = -literalValue; + if (negative) { + literalValue = -literalValue; + } AppendJsonIndexLiteral(value, NBinaryJson::EEntryType::Number, {}, &literalValue); return value; } if (node.Maybe<TCoInt8>()) { double literalValue = static_cast<double>(FromString<i8>(node.Cast<TCoInt8>().Literal().Value())); - if (negative) literalValue = -literalValue; + if (negative) { + literalValue = -literalValue; + } AppendJsonIndexLiteral(value, NBinaryJson::EEntryType::Number, {}, &literalValue); return value; } if (node.Maybe<TCoInt16>()) { double literalValue = static_cast<double>(FromString<i16>(node.Cast<TCoInt16>().Literal().Value())); - if (negative) literalValue = -literalValue; + if (negative) { + literalValue = -literalValue; + } AppendJsonIndexLiteral(value, NBinaryJson::EEntryType::Number, {}, &literalValue); return value; } if (node.Maybe<TCoInt32>()) { double literalValue = static_cast<double>(FromString<i32>(node.Cast<TCoInt32>().Literal().Value())); - if (negative) literalValue = -literalValue; + if (negative) { + literalValue = -literalValue; + } AppendJsonIndexLiteral(value, NBinaryJson::EEntryType::Number, {}, &literalValue); return value; } @@ -231,28 +240,36 @@ std::optional<TString> EncodeValueToJsonPath(const TExprBase& node, bool negativ return std::nullopt; } double literalValue = static_cast<double>(intValue); - if (negative) literalValue = -literalValue; + if (negative) { + literalValue = -literalValue; + } AppendJsonIndexLiteral(value, NBinaryJson::EEntryType::Number, {}, &literalValue); return value; } if (node.Maybe<TCoUint8>()) { double literalValue = static_cast<double>(FromString<ui8>(node.Cast<TCoUint8>().Literal().Value())); - if (negative) literalValue = -literalValue; + if (negative) { + literalValue = -literalValue; + } AppendJsonIndexLiteral(value, NBinaryJson::EEntryType::Number, {}, &literalValue); return value; } if (node.Maybe<TCoUint16>()) { double literalValue = static_cast<double>(FromString<ui16>(node.Cast<TCoUint16>().Literal().Value())); - if (negative) literalValue = -literalValue; + if (negative) { + literalValue = -literalValue; + } AppendJsonIndexLiteral(value, NBinaryJson::EEntryType::Number, {}, &literalValue); return value; } if (node.Maybe<TCoUint32>()) { double literalValue = static_cast<double>(FromString<ui32>(node.Cast<TCoUint32>().Literal().Value())); - if (negative) literalValue = -literalValue; + if (negative) { + literalValue = -literalValue; + } AppendJsonIndexLiteral(value, NBinaryJson::EEntryType::Number, {}, &literalValue); return value; } @@ -263,7 +280,9 @@ std::optional<TString> EncodeValueToJsonPath(const TExprBase& node, bool negativ return std::nullopt; } double literalValue = static_cast<double>(uintValue); - if (negative) literalValue = -literalValue; + if (negative) { + literalValue = -literalValue; + } AppendJsonIndexLiteral(value, NBinaryJson::EEntryType::Number, {}, &literalValue); return value; } @@ -296,14 +315,12 @@ bool IsSupportedJsonParamType(const TTypeAnnotationNode* type) { } } -std::expected<TJsonNodeParams, TString> VisitJsonNode(const TCoJsonQueryBase& jsonNode, - const THashSet<TString>& indexedColumns) -{ +std::expected<TJsonNodeParams, TString> VisitJsonNode(const TCoJsonQueryBase& jsonNode, const THashSet<TString>& indexedColumns) { if (!jsonNode.Json().Maybe<TCoMember>()) { return std::unexpected("JSON source must be a column reference"); } - const auto columnName = TString(jsonNode.Json().Cast<TCoMember>().Name().StringValue()); + auto columnName = TString(jsonNode.Json().Cast<TCoMember>().Name().StringValue()); if (!indexedColumns.contains(columnName)) { return std::unexpected("JSON source column is not indexed"); } @@ -332,13 +349,11 @@ std::expected<TJsonNodeParams, TString> VisitJsonNode(const TCoJsonQueryBase& js const auto applyExpr = TExprBase(pair.ChildPtr(1)); if (!applyExpr.Maybe<TCoApply>()) { - return std::unexpected(TStringBuilder() << "Variable '" << varName - << "' is bound to unsupported expression"); + return std::unexpected(TStringBuilder() << "Variable '" << varName << "' is bound to unsupported expression"); } if (applyExpr.Ref().ChildrenSize() <= 1) { - return std::unexpected(TStringBuilder() << "Variable '" << varName - << "' is bound to malformed expression"); + return std::unexpected(TStringBuilder() << "Variable '" << varName << "' is bound to malformed expression"); } const auto innerValue = UnwrapValue(TExprBase(applyExpr.Ref().ChildPtr(1))); @@ -347,8 +362,7 @@ std::expected<TJsonNodeParams, TString> VisitJsonNode(const TCoJsonQueryBase& js const auto paramName = TString(innerValue.Cast<TCoParameter>().Name().Value()); const auto paramType = innerValue.Cast<TCoParameter>().Ref().GetTypeAnn(); if (!IsSupportedJsonParamType(paramType)) { - return std::unexpected(TStringBuilder() << "Variable '" << varName - << "' is bound to a parameter with unsupported type"); + return std::unexpected(TStringBuilder() << "Variable '" << varName << "' is bound to a parameter with unsupported type"); } paramVariables.emplace(varName, paramName); continue; @@ -356,8 +370,7 @@ std::expected<TJsonNodeParams, TString> VisitJsonNode(const TCoJsonQueryBase& js const auto encoded = EncodeValueToJsonPath(innerValue); if (!encoded) { - return std::unexpected(TStringBuilder() << "Variable '" << varName - << "' is bound to unsupported expression"); + return std::unexpected(TStringBuilder() << "Variable '" << varName << "' is bound to unsupported expression"); } variables.emplace(varName, *encoded); @@ -396,8 +409,7 @@ std::expected<TJsonNodeParams, TString> VisitJsonNode(const TCoJsonQueryBase& js return std::unexpected("RETURNING clause is required for JSON_VALUE in JSON index predicates"); } - const auto* returningTypeAnn = jsonValue.ReturningType().Ref() - .GetTypeAnn()->Cast<TTypeExprType>()->GetType(); + const auto* returningTypeAnn = jsonValue.ReturningType().Ref().GetTypeAnn()->Cast<TTypeExprType>()->GetType(); returningType = returningTypeAnn->Cast<TDataExprType>()->GetSlot(); if (IsJsonValueReturningNonIndexable(returningType)) { @@ -405,19 +417,16 @@ std::expected<TJsonNodeParams, TString> VisitJsonNode(const TCoJsonQueryBase& js } } - return TJsonNodeParams{ - .ColumnName = std::move(columnName), + return TJsonNodeParams{ .ColumnName = std::move(columnName), .JsonPath = jsonNode.JsonPath().Cast<TCoUtf8>().Literal().StringValue(), .ReturningType = returningType, .Variables = std::move(variables), - .ParamVariables = std::move(paramVariables) - }; + .ParamVariables = std::move(paramVariables) }; } std::optional<TPredicateCollectResult> MergePredicateResults(std::optional<TPredicateCollectResult> left, - std::optional<TPredicateCollectResult> right, TCollectResult::ETokensMode mode, TExprContext& ctx, TPositionHandle pos) -{ - const bool leftValid = left.has_value() && !left->Collect.IsError(); + std::optional<TPredicateCollectResult> right, TCollectResult::ETokensMode mode, TExprContext& ctx, TPositionHandle pos) { + const bool leftValid = left.has_value() && !left->Collect.IsError(); const bool rightValid = right.has_value() && !right->Collect.IsError(); if (leftValid && rightValid) { @@ -425,10 +434,9 @@ std::optional<TPredicateCollectResult> MergePredicateResults(std::optional<TPred return MakeCollectError(ctx, pos, "Cross-column predicates are not supported"); } - const auto merged = (mode == TCollectResult::ETokensMode::And) - ? MergeAnd(std::move(left->Collect), std::move(right->Collect)) - : MergeOr(std::move(left->Collect), std::move(right->Collect)); - return TPredicateCollectResult{std::move(left->ColumnName), std::move(merged)}; + auto merged = (mode == TCollectResult::ETokensMode::And) ? MergeAnd(std::move(left->Collect), std::move(right->Collect)) + : MergeOr(std::move(left->Collect), std::move(right->Collect)); + return TPredicateCollectResult{ .ColumnName = std::move(left->ColumnName), .Collect = std::move(merged) }; } // AND semantics: one of the operands must be valid @@ -465,12 +473,11 @@ std::optional<TPredicateCollectResult> MergePredicateResults(std::optional<TPred return std::nullopt; } - Y_UNREACHABLE(); + YQL_ENSURE(false, "unreachable"); } -TPredicateCollectResult AppendComparisonValue(const TString& columnName, TCollectResult collectResult, - std::optional<TExprBase> comparisonValue) -{ +TPredicateCollectResult AppendComparisonValue( + const TString& columnName, TCollectResult collectResult, std::optional<TExprBase> comparisonValue) { YQL_ENSURE(!collectResult.IsError(), "Expected valid collect result"); auto& tokens = collectResult.GetTokens(); @@ -489,13 +496,11 @@ TPredicateCollectResult AppendComparisonValue(const TString& columnName, TCollec collectResult.StopCollecting(); } - return TPredicateCollectResult{columnName, std::move(collectResult)}; + return TPredicateCollectResult{ .ColumnName = columnName, .Collect = std::move(collectResult) }; } -TPredicateCollectResult ParseAndCollectJson(const TJsonNodeParams& params, - ECallableType callableType, std::optional<TExprBase> comparisonValue, - TExprContext& ctx, TPositionHandle pos) -{ +TPredicateCollectResult ParseAndCollectJson(const TJsonNodeParams& params, ECallableType callableType, std::optional<TExprBase> comparisonValue, + TExprContext& ctx, TPositionHandle pos) { TIssues parseIssues; const auto path = NJsonPath::ParseJsonPath(params.JsonPath, parseIssues, 1); if (!parseIssues.Empty()) { @@ -507,7 +512,7 @@ TPredicateCollectResult ParseAndCollectJson(const TJsonNodeParams& params, return MakeCollectError(ctx, pos, collectResult.GetError().GetMessage()); } - return AppendComparisonValue(params.ColumnName, std::move(collectResult), comparisonValue); + return AppendComparisonValue(params.ColumnName, std::move(collectResult), std::move(comparisonValue)); } std::expected<std::optional<TExprBase>, TString> TryExtractComparisonValue(const TExprBase& value) { @@ -534,11 +539,10 @@ std::expected<std::optional<TExprBase>, TString> TryExtractComparisonValue(const return std::optional<TExprBase>(std::nullopt); } -std::optional<TPredicateCollectResult> VisitJsonBinaryOperator(const TExprBase& node, TExprBase left, TExprBase right, - TExprContext& ctx, const THashSet<TString>& indexedColumns) -{ +std::optional<TPredicateCollectResult> VisitJsonBinaryOperator( + const TExprBase& node, const TExprBase& left, TExprBase right, TExprContext& ctx, const THashSet<TString>& indexedColumns) { auto jsonSide = UnwrapValue(left); - auto otherSide = UnwrapValue(right); + auto otherSide = UnwrapValue(std::move(right)); std::expected<TJsonNodeParams, TString> leftParams; std::expected<TJsonNodeParams, TString> rightParams; @@ -585,16 +589,13 @@ std::optional<TPredicateCollectResult> VisitJsonBinaryOperator(const TExprBase& auto leftResult = ParseAndCollectJson(*leftParams, ECallableType::JsonValue, comparisonValue, ctx, left.Pos()); if (rightParams.has_value()) { auto rightResult = ParseAndCollectJson(*rightParams, ECallableType::JsonValue, std::nullopt, ctx, otherSide.Pos()); - return MergePredicateResults(std::move(leftResult), std::move(rightResult), - TCollectResult::ETokensMode::And, ctx, otherSide.Pos()); + return MergePredicateResults(std::move(leftResult), std::move(rightResult), TCollectResult::ETokensMode::And, ctx, otherSide.Pos()); } return leftResult; } -std::optional<TPredicateCollectResult> VisitJsonSqlIn(const TCoSqlIn& node, TExprContext& ctx, - const THashSet<TString>& indexedColumns) -{ +std::optional<TPredicateCollectResult> VisitJsonSqlIn(const TCoSqlIn& node, TExprContext& ctx, const THashSet<TString>& indexedColumns) { auto lookup = UnwrapValue(node.Lookup()); auto collection = UnwrapValue(node.Collection()); @@ -629,31 +630,29 @@ std::optional<TPredicateCollectResult> VisitJsonSqlIn(const TCoSqlIn& node, TExp const auto& items = paramTypeAnn->Cast<TTupleExprType>()->GetItems(); for (const auto* itemType : items) { if (!IsSupportedJsonParamType(itemType)) { - return MakeCollectError(ctx, param.Pos(), - "Tuple parameter item type is not supported for JSON index"); + return MakeCollectError(ctx, param.Pos(), "Tuple parameter item type is not supported for JSON index"); } } } else if (paramTypeAnn->GetKind() == ETypeAnnotationKind::Dict) { const auto* keyType = paramTypeAnn->Cast<TDictExprType>()->GetKeyType(); if (!IsSupportedJsonParamType(keyType)) { - return MakeCollectError(ctx, param.Pos(), - "Dict parameter key type is not supported for JSON index"); + return MakeCollectError(ctx, param.Pos(), "Dict parameter key type is not supported for JSON index"); } } else { return MakeCollectError(ctx, param.Pos(), "Unsupported parameter type in SQL IN"); } - auto baseResult = ParseAndCollectJson(*jsonParams, ECallableType::JsonValue, - std::nullopt, ctx, jsonLookup.Pos()); + auto baseResult = ParseAndCollectJson(*jsonParams, ECallableType::JsonValue, std::nullopt, ctx, jsonLookup.Pos()); if (baseResult.Collect.IsError()) { return baseResult; } - const auto paramName = TString(param.Name().Value()); if (baseResult.Collect.CanCollect()) { auto& tokens = baseResult.Collect.GetTokens(); YQL_ENSURE(tokens.size() == 1); + auto paramName = TString(param.Name().Value()); + auto nodeHandle = tokens.extract(tokens.begin()); nodeHandle.value().ParamName = std::move(paramName); tokens.insert(std::move(nodeHandle)); @@ -679,16 +678,16 @@ std::optional<TPredicateCollectResult> VisitJsonSqlIn(const TCoSqlIn& node, TExp if (pairRef.ChildrenSize() != 2) { return MakeCollectError(ctx, pair.Pos(), "Dict literal must have exactly two elements"); } - items.push_back(TExprBase(pairRef.ChildPtr(0))); + items.emplace_back(pairRef.ChildPtr(0)); } - } else if (collection.Ref().IsCallable({"AsSet", "AsSetStrict", "AsSetMayWarn"})) { - for (size_t i = 0; i < collection.Ref().ChildrenSize(); ++i) { - items.push_back(TExprBase(collection.Ref().ChildPtr(i))); + } else if (collection.Ref().IsCallable({ "AsSet", "AsSetStrict", "AsSetMayWarn" })) { + for (ui32 i = 0; i < collection.Ref().ChildrenSize(); ++i) { + items.emplace_back(collection.Ref().ChildPtr(i)); } } else if (collection.Maybe<TExprList>()) { auto list = collection.Cast<TExprList>(); for (const auto& item : list) { - items.push_back(TExprBase(item)); + items.emplace_back(item); } } else { return MakeCollectError(ctx, collection.Pos(), "Unsupported collection type in SQL IN"); @@ -711,17 +710,14 @@ std::optional<TPredicateCollectResult> VisitJsonSqlIn(const TCoSqlIn& node, TExp if (!acc.has_value()) { acc = std::move(itemResult); } else { - acc = MergePredicateResults(std::move(acc), std::move(itemResult), - TCollectResult::ETokensMode::Or, ctx, literal.Pos()); + acc = MergePredicateResults(std::move(acc), std::move(itemResult), TCollectResult::ETokensMode::Or, ctx, literal.Pos()); } } return acc; } -std::optional<TPredicateCollectResult> VisitJsonExists(const TExprBase& node, TExprContext& ctx, - const THashSet<TString>& indexedColumns) -{ +std::optional<TPredicateCollectResult> VisitJsonExists(const TExprBase& node, TExprContext& ctx, const THashSet<TString>& indexedColumns) { if (node.Maybe<TCoJsonExists>()) { const auto params = VisitJsonNode(node.Cast<TCoJsonExists>(), indexedColumns); if (!params) { @@ -734,9 +730,7 @@ std::optional<TPredicateCollectResult> VisitJsonExists(const TExprBase& node, TE return std::nullopt; } -std::optional<TPredicateCollectResult> VisitJsonValue(const TExprBase& node, TExprContext& ctx, - const THashSet<TString>& indexedColumns) -{ +std::optional<TPredicateCollectResult> VisitJsonValue(const TExprBase& node, TExprContext& ctx, const THashSet<TString>& indexedColumns) { if (const auto cmp = node.Maybe<TCoCompare>()) { return VisitJsonBinaryOperator(node, cmp.Cast().Left(), cmp.Cast().Right(), ctx, indexedColumns); } @@ -763,62 +757,58 @@ std::optional<TPredicateCollectResult> VisitJsonValue(const TExprBase& node, TEx return std::nullopt; } -std::optional<TPredicateCollectResult> VisitJsonPredicate(const TExprBase& predicate, TExprContext& ctx, - const THashSet<TString>& indexedColumns) -{ - const auto node = UnwrapPredicate(predicate); +std::optional<TPredicateCollectResult> VisitJsonPredicate( + const TExprBase& predicate, TExprContext& ctx, const THashSet<TString>& indexedColumns) { + auto node = UnwrapPredicate(predicate); if (const auto maybeAnd = node.Maybe<TCoAnd>()) { - const auto andNode = maybeAnd.Cast(); + auto andNode = maybeAnd.Cast(); if (andNode.ArgCount() == 0) { return std::nullopt; } auto result = VisitJsonPredicate(andNode.Arg(0), ctx, indexedColumns); for (size_t i = 1; i < andNode.ArgCount(); ++i) { - const auto nextNode = andNode.Arg(i); - const auto nextResult = VisitJsonPredicate(nextNode, ctx, indexedColumns); - result = MergePredicateResults(std::move(result), std::move(nextResult), - TCollectResult::ETokensMode::And, ctx, nextNode.Pos()); + auto nextNode = andNode.Arg(i); + auto nextResult = VisitJsonPredicate(nextNode, ctx, indexedColumns); + result = MergePredicateResults(std::move(result), std::move(nextResult), TCollectResult::ETokensMode::And, ctx, nextNode.Pos()); } return result; } - if (const auto maybeOr = node.Maybe<TCoOr>()) { - const auto orNode = maybeOr.Cast(); + if (auto maybeOr = node.Maybe<TCoOr>()) { + auto orNode = maybeOr.Cast(); if (orNode.ArgCount() == 0) { return std::nullopt; } auto result = VisitJsonPredicate(orNode.Arg(0), ctx, indexedColumns); for (size_t i = 1; i < orNode.ArgCount(); ++i) { - const auto nextNode = orNode.Arg(i); - const auto nextResult = VisitJsonPredicate(nextNode, ctx, indexedColumns); - result = MergePredicateResults(std::move(result), std::move(nextResult), - TCollectResult::ETokensMode::Or, ctx, nextNode.Pos()); + auto nextNode = orNode.Arg(i); + auto nextResult = VisitJsonPredicate(nextNode, ctx, indexedColumns); + result = MergePredicateResults(std::move(result), std::move(nextResult), TCollectResult::ETokensMode::Or, ctx, nextNode.Pos()); } return result; } - if (const auto jsonExists = VisitJsonExists(node, ctx, indexedColumns)) { + if (auto jsonExists = VisitJsonExists(node, ctx, indexedColumns)) { return jsonExists; } - if (const auto jsonValue = VisitJsonValue(node, ctx, indexedColumns)) { + if (auto jsonValue = VisitJsonValue(node, ctx, indexedColumns)) { return jsonValue; } return std::nullopt; } -} // namespace +} // namespace -std::expected<TJsonIndexSettings, TIssue> CollectJsonIndexPredicate(const TExprBase& body, const TExprBase& node, TExprContext& ctx, - const THashSet<TString>& indexedColumns) -{ - const auto result = VisitJsonPredicate(body, ctx, indexedColumns); +std::expected<TJsonIndexSettings, TIssue> CollectJsonIndexPredicate( + const TExprBase& body, const TExprBase& node, TExprContext& ctx, const THashSet<TString>& indexedColumns) { + auto result = VisitJsonPredicate(body, ctx, indexedColumns); if (!result.has_value()) { return std::unexpected(TIssue(ctx.GetPosition(node.Pos()), TStringBuilder() << kErrorMessage << "nothing to extract")); } @@ -836,10 +826,12 @@ std::expected<TJsonIndexSettings, TIssue> CollectJsonIndexPredicate(const TExprB tokenNodes.reserve(collectResult.GetTokens().size()); for (const auto& tokenPair : collectResult.GetTokens()) { + // clang-format off auto pair = Build<TExprList>(ctx, node.Pos()) .Add<TCoString>().Literal().Build(tokenPair.PathToken).Build() .Add<TCoString>().Literal().Build(tokenPair.ParamName).Build() .Done().Ptr(); + // clang-format on tokenNodes.push_back(std::move(pair)); } @@ -851,7 +843,7 @@ std::expected<TJsonIndexSettings, TIssue> CollectJsonIndexPredicate(const TExprB settings.SetMinimumShouldMatch(Build<TCoString>(ctx, node.Pos()).Literal().Build("").Done().Ptr()); settings.SetTokens(ctx.NewList(node.Pos(), std::move(tokenNodes))); - return TJsonIndexSettings{std::move(result->ColumnName), std::move(settings)}; + return TJsonIndexSettings{ .ColumnName = std::move(result->ColumnName), .Settings = std::move(settings) }; } -} // namespace NKikimr::NKqp::NOpt +} // namespace NKikimr::NKqp::NOpt diff --git a/ydb/core/kqp/opt/logical/kqp_opt_log_json_index.h b/ydb/core/kqp/opt/logical/kqp_opt_log_json_index.h index d508fbca8ae..8b4ab0ad48d 100644 --- a/ydb/core/kqp/opt/logical/kqp_opt_log_json_index.h +++ b/ydb/core/kqp/opt/logical/kqp_opt_log_json_index.h @@ -1,10 +1,10 @@ #pragma once #include <ydb/core/kqp/common/kqp_yql.h> -#include <yql/essentials/ast/yql_expr.h> -#include <yql/essentials/public/issue/yql_issue.h> #include <util/generic/hash.h> +#include <yql/essentials/ast/yql_expr.h> +#include <yql/essentials/public/issue/yql_issue.h> #include <expected> @@ -18,4 +18,4 @@ struct TJsonIndexSettings { std::expected<TJsonIndexSettings, NYql::TIssue> CollectJsonIndexPredicate(const NYql::NNodes::TExprBase& body, const NYql::NNodes::TExprBase& node, NYql::TExprContext& ctx, const THashSet<TString>& jsonIndexedColumns); -} // namespace NKikimr::NKqp::NOpt +} // namespace NKikimr::NKqp::NOpt diff --git a/ydb/library/json_index/json_index.cpp b/ydb/library/json_index/json_index.cpp index 0133d34ecc6..e4a2455bf4f 100644 --- a/ydb/library/json_index/json_index.cpp +++ b/ydb/library/json_index/json_index.cpp @@ -3,17 +3,17 @@ #include <library/cpp/json/json_writer.h> #include <util/string/join.h> #include <yql/essentials/public/udf/udf_types.h> +#include <yql/essentials/types/binary_json/format.h> #include <yql/essentials/types/binary_json/read.h> #include <yql/essentials/types/binary_json/write.h> -#include <yql/essentials/types/binary_json/format.h> #include <ranges> namespace NKikimr::NJsonIndex { using NYql::TIssue; -using NYql::TIssues; using namespace NYql::NJsonPath; + namespace { NBinaryJson::EEntryType GetEntryType(const TJsonPathItem& item) { @@ -43,14 +43,14 @@ NBinaryJson::EEntryType GetEntryType(const TJsonPathItem& item) { // preventing ambiguity between an empty-key path component and the start of a literal suffix void AppendKey(TString& prefix, TStringBuf key) { size_t size = key.size() + 1; - do { + while (size > 0) { if (size < 0x80) { - prefix.push_back((ui8)size); + prefix.push_back(static_cast<char>(size)); } else { - prefix.push_back(0x80 | (ui8)(size & 0x7F)); + prefix.push_back(static_cast<char>(0x80 | (size & 0x7F))); } size >>= 7; - } while (size > 0); + }; prefix += key; } @@ -172,9 +172,8 @@ void PruneRedundantTokens(TTokens& tokens, TCollectResult::ETokensMode mode) { tokens = std::move(result); } -TCollectResult MergeBooleanOperands(TCollectResult left, TCollectResult right, - TCollectResult::ETokensMode incompatibleMode, TCollectResult::ETokensMode combinedMode) -{ +TCollectResult MergeBooleanOperands( + TCollectResult left, TCollectResult right, TCollectResult::ETokensMode incompatibleMode, TCollectResult::ETokensMode combinedMode) { if (left.IsError()) { return left; } @@ -214,8 +213,7 @@ TCollectResult MergeComparisonPathResults(TCollectResult left, TCollectResult ri bool hasMix = false; if (!leftTokens.empty() && !rightTokens.empty()) { - if (left.GetTokensMode() == TCollectResult::ETokensMode::Or || - right.GetTokensMode() == TCollectResult::ETokensMode::Or) { + if (left.GetTokensMode() == TCollectResult::ETokensMode::Or || right.GetTokensMode() == TCollectResult::ETokensMode::Or) { hasMix = true; } } @@ -229,7 +227,7 @@ TCollectResult MergeComparisonPathResults(TCollectResult left, TCollectResult ri } class TQueryCollector { - enum class EMode { + enum class EMode : ui8 { // Main path from the context item ($): accumulate index tokens along the query // Predicate operators are disallowed when the callable is JSON_EXISTS (existence vs. boolean result) Context = 0, @@ -248,16 +246,12 @@ class TQueryCollector { }; public: - TQueryCollector( - const TJsonPathPtr path, ECallableType callableType, - const std::unordered_map<TString, TString>& variables, - const std::unordered_map<TString, TString>& paramVariables - ) + TQueryCollector(const TJsonPathPtr& path, ECallableType callableType, const std::unordered_map<TString, TString>& variables, + const std::unordered_map<TString, TString>& paramVariables) : Reader(path) , CallableType(callableType) , Variables(variables) - , ParamVariables(paramVariables.begin(), paramVariables.end()) - { + , ParamVariables(paramVariables.begin(), paramVariables.end()) { } TCollectResult Collect() { @@ -270,7 +264,7 @@ private: TCollectResult CollectEqualOperands(const TJsonPathItem& leftItem, const TJsonPathItem& rightItem); TCollectResult CollectArithmeticOperand(const TJsonPathItem& item, EMode mode); - TCollectResult ContextObject(EMode mode); + static TCollectResult ContextObject(EMode mode); TCollectResult MemberAccess(const TJsonPathItem& item, EMode mode); TCollectResult WildcardMemberAccess(const TJsonPathItem& item, EMode mode); @@ -291,13 +285,12 @@ private: TCollectResult FilterObject(const TJsonPathItem& item, EMode mode); TCollectResult FilterPredicate(const TJsonPathItem& item, EMode mode); - TCollectResult Literal(const TJsonPathItem& item, EMode mode); + static TCollectResult Literal(const TJsonPathItem& item, EMode mode); TCollectResult Variable(const TJsonPathItem& item, EMode mode); std::optional<double> EvaluteNumericLiteral(const TJsonPathItem& item); bool ArePredicatesAllowed(EMode mode) const; -private: TJsonPathReader Reader; ECallableType CallableType; std::unordered_map<TString, TString> Variables; @@ -383,7 +376,7 @@ TCollectResult TQueryCollector::Literal(const TJsonPathItem& item, EMode mode) { TString value; switch (item.Type) { case EJsonPathItemType::StringLiteral: - AppendJsonIndexLiteral(value, GetEntryType(item), item.GetString(), nullptr); + AppendJsonIndexLiteral(value, GetEntryType(item), item.GetString(), /*numberPayload=*/nullptr); break; case EJsonPathItemType::NumberLiteral: { double number = item.GetNumber(); @@ -392,7 +385,7 @@ TCollectResult TQueryCollector::Literal(const TJsonPathItem& item, EMode mode) { } case EJsonPathItemType::BooleanLiteral: case EJsonPathItemType::NullLiteral: - AppendJsonIndexLiteral(value, GetEntryType(item), {}, nullptr); + AppendJsonIndexLiteral(value, GetEntryType(item), {}, /*numberPayload=*/nullptr); break; default: return TCollectResult(TIssue("Expected a literal expression")); @@ -461,8 +454,8 @@ TCollectResult TQueryCollector::BinaryAnd(const TJsonPathItem& item, EMode mode) const auto& rightItem = Reader.ReadRightOperand(item); auto leftCollectResult = Collect(leftItem, mode); auto rightCollectResult = Collect(rightItem, mode); - return MergeBooleanOperands(std::move(leftCollectResult), std::move(rightCollectResult), - TCollectResult::ETokensMode::Or, TCollectResult::ETokensMode::And); + return MergeBooleanOperands( + std::move(leftCollectResult), std::move(rightCollectResult), TCollectResult::ETokensMode::Or, TCollectResult::ETokensMode::And); } TCollectResult TQueryCollector::BinaryOr(const TJsonPathItem& item, EMode mode) { @@ -470,8 +463,8 @@ TCollectResult TQueryCollector::BinaryOr(const TJsonPathItem& item, EMode mode) const auto& rightItem = Reader.ReadRightOperand(item); auto leftCollectResult = Collect(leftItem, mode); auto rightCollectResult = Collect(rightItem, mode); - return MergeBooleanOperands(std::move(leftCollectResult), std::move(rightCollectResult), - TCollectResult::ETokensMode::And, TCollectResult::ETokensMode::Or); + return MergeBooleanOperands( + std::move(leftCollectResult), std::move(rightCollectResult), TCollectResult::ETokensMode::And, TCollectResult::ETokensMode::Or); } TCollectResult TQueryCollector::BinaryEqual(const TJsonPathItem& item, EMode mode) { @@ -490,7 +483,7 @@ TCollectResult TQueryCollector::BinaryEqual(const TJsonPathItem& item, EMode mod } if (leftIsLiteral && !rightIsLiteral) { - return CollectEqualOperands(rightItem, leftItem); + return CollectEqualOperands(rightItem, leftItem); // NOLINT } if (!leftIsLiteral && !rightIsLiteral) { @@ -685,29 +678,29 @@ bool TQueryCollector::ArePredicatesAllowed(EMode mode) const { void TokenizeBinaryJson(const NBinaryJson::TContainerCursor& root, const TString& prefix, TVector<TString>& tokens); -void TokenizeBinaryJson(NBinaryJson::TEntryCursor element, const TString& prefix, TVector<TString>& tokens) { +void TokenizeBinaryJson(const NBinaryJson::TEntryCursor& element, const TString& prefix, TVector<TString>& tokens) { if (element.GetType() == NBinaryJson::EEntryType::Container) { TokenizeBinaryJson(element.GetContainer(), prefix, tokens); return; } TString token = prefix; switch (element.GetType()) { - case NBinaryJson::EEntryType::String: - AppendJsonIndexLiteral(token, element.GetType(), element.GetString(), nullptr); - break; - case NBinaryJson::EEntryType::Number: { - double number = element.GetNumber(); - AppendJsonIndexLiteral(token, element.GetType(), {}, &number); - break; - } - case NBinaryJson::EEntryType::BoolFalse: - case NBinaryJson::EEntryType::BoolTrue: - case NBinaryJson::EEntryType::Null: - AppendJsonIndexLiteral(token, element.GetType(), {}, nullptr); - break; - case NBinaryJson::EEntryType::Container: - Y_ENSURE(false, "Unreachable"); - break; + case NBinaryJson::EEntryType::String: + AppendJsonIndexLiteral(token, element.GetType(), element.GetString(), /*numberPayload=*/nullptr); + break; + case NBinaryJson::EEntryType::Number: { + double number = element.GetNumber(); + AppendJsonIndexLiteral(token, element.GetType(), {}, &number); + break; + } + case NBinaryJson::EEntryType::BoolFalse: + case NBinaryJson::EEntryType::BoolTrue: + case NBinaryJson::EEntryType::Null: + AppendJsonIndexLiteral(token, element.GetType(), {}, /*numberPayload=*/nullptr); + break; + case NBinaryJson::EEntryType::Container: + Y_ENSURE(false, "Unreachable"); + break; } tokens.push_back(token); } @@ -720,32 +713,30 @@ TString TokenizeJsonNextPrefix(const TString& prefix, TStringBuf key) { void TokenizeBinaryJson(const NBinaryJson::TContainerCursor& root, const TString& prefix, TVector<TString>& tokens) { switch (root.GetType()) { - case NBinaryJson::EContainerType::TopLevelScalar: - TokenizeBinaryJson(root.GetElement(0), prefix, tokens); - break; - case NBinaryJson::EContainerType::Array: - for (ui32 pos = 0; pos < root.GetSize(); pos++) { - TokenizeBinaryJson(root.GetElement(pos), prefix, tokens); - } - break; - case NBinaryJson::EContainerType::Object: - auto it = root.GetObjectIterator(); - while (it.HasNext()) { - auto kv = it.Next(); - Y_ENSURE(kv.first.GetType() == NBinaryJson::EEntryType::String); - TString nextPrefix = TokenizeJsonNextPrefix(prefix, kv.first.GetString()); - tokens.push_back(nextPrefix); - TokenizeBinaryJson(kv.second, nextPrefix, tokens); - } - break; + case NBinaryJson::EContainerType::TopLevelScalar: + TokenizeBinaryJson(root.GetElement(0), prefix, tokens); + break; + case NBinaryJson::EContainerType::Array: + for (ui32 pos = 0; pos < root.GetSize(); pos++) { + TokenizeBinaryJson(root.GetElement(pos), prefix, tokens); + } + break; + case NBinaryJson::EContainerType::Object: + auto it = root.GetObjectIterator(); + while (it.HasNext()) { + auto kv = it.Next(); + Y_ENSURE(kv.first.GetType() == NBinaryJson::EEntryType::String); + TString nextPrefix = TokenizeJsonNextPrefix(prefix, kv.first.GetString()); + tokens.push_back(nextPrefix); + TokenizeBinaryJson(kv.second, nextPrefix, tokens); + } + break; } } -} // namespace +} // namespace -void AppendJsonIndexLiteral(TString& out, NBinaryJson::EEntryType type, TStringBuf stringPayload, - const double* numberPayload) -{ +void AppendJsonIndexLiteral(TString& out, NBinaryJson::EEntryType type, TStringBuf stringPayload, const double* numberPayload) { out.push_back(0); out.push_back(static_cast<char>(type)); switch (type) { @@ -767,8 +758,7 @@ void AppendJsonIndexLiteral(TString& out, NBinaryJson::EEntryType type, TStringB } TCollectResult::TCollectResult(TTokens&& tokens) - : Result(std::move(tokens)) -{ + : Result(std::move(tokens)) { } TCollectResult::TCollectResult(TString&& token) { @@ -778,8 +768,7 @@ TCollectResult::TCollectResult(TString&& token) { } TCollectResult::TCollectResult(TCollectResult::TError&& issue) - : Result(std::move(issue)) -{ + : Result(std::move(issue)) { } const TTokens& TCollectResult::GetTokens() const { @@ -839,9 +828,8 @@ TVector<TString> TokenizeJson(TStringBuf text, TString& error) { return TokenizeBinaryJson(TStringBuf(buffer.data(), buffer.size())); } -TCollectResult CollectJsonPath(const TJsonPathPtr& path, ECallableType callableType, - const std::unordered_map<TString, TString>& variables, const std::unordered_map<TString, TString>& paramVariables) -{ +TCollectResult CollectJsonPath(const TJsonPathPtr& path, ECallableType callableType, const std::unordered_map<TString, TString>& variables, + const std::unordered_map<TString, TString>& paramVariables) { auto result = TQueryCollector(path, callableType, variables, paramVariables).Collect(); if (!result.IsError()) { if (result.GetTokens().empty()) { @@ -857,13 +845,11 @@ TCollectResult CollectJsonPath(const TJsonPathPtr& path, ECallableType callableT } TCollectResult MergeAnd(TCollectResult left, TCollectResult right) { - return MergeBooleanOperands(std::move(left), std::move(right), - TCollectResult::ETokensMode::Or, TCollectResult::ETokensMode::And); + return MergeBooleanOperands(std::move(left), std::move(right), TCollectResult::ETokensMode::Or, TCollectResult::ETokensMode::And); } TCollectResult MergeOr(TCollectResult left, TCollectResult right) { - return MergeBooleanOperands(std::move(left), std::move(right), - TCollectResult::ETokensMode::And, TCollectResult::ETokensMode::Or); + return MergeBooleanOperands(std::move(left), std::move(right), TCollectResult::ETokensMode::And, TCollectResult::ETokensMode::Or); } TString FormatJsonIndexToken(const TString& pathToken, const TString& paramName) { @@ -928,7 +914,7 @@ TString FormatJsonIndexToken(const TString& pathToken, const TString& paramName) break; case NBinaryJson::EEntryType::Number: if (nullPos + 2 + sizeof(double) <= pathToken.size()) { - double d; + double d = 0.0; memcpy(&d, pathToken.data() + nullPos + 2, sizeof(double)); writer.Write("literal", d); } @@ -948,4 +934,4 @@ TString FormatJsonIndexToken(const TString& pathToken, const TString& paramName) return ss.Str(); } -} // namespace NKikimr::NJsonIndex +} // namespace NKikimr::NJsonIndex diff --git a/ydb/library/json_index/json_index.h b/ydb/library/json_index/json_index.h index bbc8be11978..e6050ffaadb 100644 --- a/ydb/library/json_index/json_index.h +++ b/ydb/library/json_index/json_index.h @@ -1,32 +1,29 @@ #pragma once -#include <yql/essentials/public/issue/yql_issue.h> #include <yql/essentials/minikql/jsonpath/parser/parser.h> +#include <yql/essentials/public/issue/yql_issue.h> #include <yql/essentials/types/binary_json/format.h> -#include <compare> #include <set> #include <variant> namespace NKikimr::NJsonIndex { // YQL json numbers are double precision floats. The maximum supported integer is +-2^53. -static constexpr i64 MaxSupportedInt = 9007199254740992ll; +static constexpr i64 MaxSupportedInt = 9007199254740992LL; // A token paired with an optional parameter name struct TToken { public: // Constructs a token with the given path token (full or partial path-prefix token) explicit TToken(TString pathToken) - : PathToken(std::move(pathToken)) - { + : PathToken(std::move(pathToken)) { } // Constructs a token with the given path token and parameter name (partial path-prefix and YQL param name) explicit TToken(TString pathToken, TString paramName) : PathToken(std::move(pathToken)) - , ParamName(std::move(paramName)) - { + , ParamName(std::move(paramName)) { } auto operator<=>(const TToken& other) const { @@ -37,7 +34,6 @@ public: return std::tie(PathToken, ParamName) == std::tie(other.PathToken, other.ParamName); } -public: // Full or partial path-prefix token TString PathToken; @@ -53,13 +49,12 @@ class TCollectResult { public: using TError = NYql::TIssue; - enum class ETokensMode { + enum class ETokensMode : ui8 { NotSet = 0, And = 1, Or = 2, }; -public: // Constructs a collect result with the given tokens explicit TCollectResult(TTokens&& tokens); @@ -105,7 +100,7 @@ private: // Type of the callable function that is used for the JSON index collection // It is given from the predicate of the SELECT statement -enum class ECallableType { +enum class ECallableType : ui8 { JsonExists = 0, JsonValue = 1, JsonQuery = 2, @@ -131,8 +126,7 @@ TCollectResult MergeAnd(TCollectResult left, TCollectResult right); TCollectResult MergeOr(TCollectResult left, TCollectResult right); // Appends NULL, binary JSON entry type byte, and scalar payload (the index token layout) -void AppendJsonIndexLiteral(TString& out, NBinaryJson::EEntryType type, TStringBuf stringPayload = {}, - const double* numberPayload = nullptr); +void AppendJsonIndexLiteral(TString& out, NBinaryJson::EEntryType type, TStringBuf stringPayload = {}, const double* numberPayload = nullptr); // Formats an index token and optional parameter name as a JSON-like object string. // The path portion is decoded from length-prefixed key segments joined by '.'. @@ -146,4 +140,4 @@ void AppendJsonIndexLiteral(TString& out, NBinaryJson::EEntryType type, TStringB // ("", "") -> {} TString FormatJsonIndexToken(const TString& pathToken, const TString& paramName); -} // namespace NKikimr::NJsonIndex +} // namespace NKikimr::NJsonIndex |
