diff options
author | gvit <gvit@ydb.tech> | 2023-02-03 20:46:19 +0300 |
---|---|---|
committer | gvit <gvit@ydb.tech> | 2023-02-03 20:46:19 +0300 |
commit | 6d8a9540c8f443be7228254169cca75484412034 (patch) | |
tree | ee4c27684f92e81462ab9feeefd5bc1c3a664094 | |
parent | f59ed1832f837a49d0ad528fa3e04dbe7ebf8335 (diff) | |
download | ydb-6d8a9540c8f443be7228254169cca75484412034.tar.gz |
remove one impl from code
-rw-r--r-- | ydb/core/engine/mkql_proto.cpp | 159 | ||||
-rw-r--r-- | ydb/core/engine/mkql_proto.h | 2 | ||||
-rw-r--r-- | ydb/core/yq/libs/result_formatter/result_formatter.cpp | 12 | ||||
-rw-r--r-- | ydb/library/mkql_proto/mkql_proto.cpp | 87 | ||||
-rw-r--r-- | ydb/library/mkql_proto/mkql_proto.h | 2 |
5 files changed, 82 insertions, 180 deletions
diff --git a/ydb/core/engine/mkql_proto.cpp b/ydb/core/engine/mkql_proto.cpp index 6fcb2de9fb..1db52c6e37 100644 --- a/ydb/core/engine/mkql_proto.cpp +++ b/ydb/core/engine/mkql_proto.cpp @@ -9,161 +9,7 @@ #include <ydb/core/scheme_types/scheme_types_defs.h> -namespace NKikimr { - -namespace NMiniKQL { - -namespace { - - Y_FORCE_INLINE NUdf::TUnboxedValue HandleKindDataImport(TType* type, const Ydb::Value& value) { - auto dataType = static_cast<TDataType*>(type); - switch (dataType->GetSchemeType()) { - case NUdf::TDataType<bool>::Id: - return NUdf::TUnboxedValuePod(value.bool_value()); - case NUdf::TDataType<ui8>::Id: - return NUdf::TUnboxedValuePod(ui8(value.uint32_value())); - case NUdf::TDataType<i8>::Id: - return NUdf::TUnboxedValuePod(i8(value.int32_value())); - case NUdf::TDataType<ui16>::Id: - return NUdf::TUnboxedValuePod(ui16(value.uint32_value())); - case NUdf::TDataType<i16>::Id: - return NUdf::TUnboxedValuePod(i16(value.int32_value())); - case NUdf::TDataType<i32>::Id: - return NUdf::TUnboxedValuePod(value.int32_value()); - case NUdf::TDataType<ui32>::Id: - return NUdf::TUnboxedValuePod(value.uint32_value()); - case NUdf::TDataType<i64>::Id: - return NUdf::TUnboxedValuePod(value.int64_value()); - case NUdf::TDataType<ui64>::Id: - return NUdf::TUnboxedValuePod(value.uint64_value()); - case NUdf::TDataType<float>::Id: - return NUdf::TUnboxedValuePod(value.float_value()); - case NUdf::TDataType<double>::Id: - return NUdf::TUnboxedValuePod(value.double_value()); - case NUdf::TDataType<NUdf::TJson>::Id: - case NUdf::TDataType<NUdf::TUtf8>::Id: - return MakeString(value.text_value()); - case NUdf::TDataType<NUdf::TDate>::Id: - return NUdf::TUnboxedValuePod(ui16(value.uint32_value())); - case NUdf::TDataType<NUdf::TDatetime>::Id: - return NUdf::TUnboxedValuePod(value.uint32_value()); - case NUdf::TDataType<NUdf::TTimestamp>::Id: - return NUdf::TUnboxedValuePod(value.uint64_value()); - case NUdf::TDataType<NUdf::TInterval>::Id: - return NUdf::TUnboxedValuePod(value.int64_value()); - case NUdf::TDataType<NUdf::TJsonDocument>::Id: - return ValueFromString(NUdf::EDataSlot::JsonDocument, value.text_value()); - case NUdf::TDataType<NUdf::TDyNumber>::Id: - return ValueFromString(NUdf::EDataSlot::DyNumber, value.text_value()); - case NUdf::TDataType<NUdf::TDecimal>::Id: - return NUdf::TUnboxedValuePod(NYql::NDecimal::FromHalfs(value.low_128(), value.high_128())); - default: - return MakeString(value.bytes_value()); - } - } - -} - -NUdf::TUnboxedValue ImportValueFromProto(TType* type, const Ydb::Value& value, const THolderFactory& factory) { - switch (type->GetKind()) { - case TType::EKind::Void: - return NUdf::TUnboxedValuePod::Void(); - - case TType::EKind::Null: - case TType::EKind::EmptyList: - case TType::EKind::EmptyDict: - return NUdf::TUnboxedValue(); - - case TType::EKind::Data: - return HandleKindDataImport(type, value); - - case TType::EKind::Optional: { - auto optionalType = static_cast<TOptionalType*>(type); - switch (value.value_case()) { - case Ydb::Value::kNestedValue: - return ImportValueFromProto(optionalType->GetItemType(), value.nested_value(), factory).MakeOptional(); - case Ydb::Value::kNullFlagValue: - return NUdf::TUnboxedValue(); - default: - return ImportValueFromProto(optionalType->GetItemType(), value, factory).MakeOptional(); - } - } - - case TType::EKind::List: { - auto listType = static_cast<TListType*>(type); - auto itemType = listType->GetItemType(); - const auto& list = value.items(); - NUdf::TUnboxedValue *items = nullptr; - auto array = factory.CreateDirectArrayHolder(list.size(), items); - for (const auto& x : list) { - *items++ = ImportValueFromProto(itemType, x, factory); - } - - return std::move(array); - } - - case TType::EKind::Struct: { - auto structType = static_cast<TStructType*>(type); - NUdf::TUnboxedValue* itemsPtr = nullptr; - auto res = factory.CreateDirectArrayHolder(structType->GetMembersCount(), itemsPtr); - for (ui32 index = 0; index < structType->GetMembersCount(); ++index) { - auto memberType = structType->GetMemberType(index); - itemsPtr[index] = ImportValueFromProto(memberType, value.items(index), factory); - } - - return std::move(res); - } - - case TType::EKind::Tuple: { - auto tupleType = static_cast<TTupleType*>(type); - NUdf::TUnboxedValue* itemsPtr = nullptr; - auto res = factory.CreateDirectArrayHolder(tupleType->GetElementsCount(), itemsPtr); - for (ui32 index = 0; index < tupleType->GetElementsCount(); ++index) { - auto elementType = tupleType->GetElementType(index); - itemsPtr[index] = ImportValueFromProto(elementType, value.items(index), factory); - } - - return std::move(res); - } - - case TType::EKind::Dict: { - auto dictType = static_cast<TDictType*>(type); - auto keyType = dictType->GetKeyType(); - auto payloadType = dictType->GetPayloadType(); - auto dictBuilder = factory.NewDict(dictType, NUdf::TDictFlags::EDictKind::Hashed); - - for (const auto& x : value.pairs()) { - dictBuilder->Add( - ImportValueFromProto(keyType, x.key(), factory), - ImportValueFromProto(payloadType, x.payload(), factory) - ); - } - - return dictBuilder->Build(); - } - - case TType::EKind::Variant: { - auto variantType = static_cast<TVariantType*>(type); - auto index = value.variant_index(); - auto unboxedValue = ImportValueFromProto(variantType->GetAlternativeType(index), value, factory); - auto res = factory.CreateVariantHolder(std::move(unboxedValue.Release()), index); - return std::move(res); - } - - case TType::EKind::Tagged: { - auto taggedType = static_cast<TTaggedType*>(type); - auto unboxedValue = ImportValueFromProto(taggedType->GetBaseType(), value, factory); - return std::move(unboxedValue); - } - - case TType::EKind::Pg: - // TODO: support pg types - MKQL_ENSURE(false, "pg types are not supported"); - - default: - MKQL_ENSURE(false, TStringBuilder() << "Unknown kind: " << type->GetKindAsStr()); - } -} +namespace NKikimr::NMiniKQL { // NOTE: TCell's can reference memomry from tupleValue bool CellsFromTuple(const NKikimrMiniKQL::TType* tupleType, @@ -424,5 +270,4 @@ bool CellToValue(NScheme::TTypeInfo type, const TCell& c, NKikimrMiniKQL::TValue } -} // namspace NMiniKQL -} // namspace NKikimr +} // namspace NKikimr::NMiniKQL diff --git a/ydb/core/engine/mkql_proto.h b/ydb/core/engine/mkql_proto.h index 06d1bbce15..1c316119c5 100644 --- a/ydb/core/engine/mkql_proto.h +++ b/ydb/core/engine/mkql_proto.h @@ -12,8 +12,6 @@ namespace NMiniKQL { class THolderFactory; -NUdf::TUnboxedValue ImportValueFromProto(TType* type, const Ydb::Value& value, const THolderFactory& factory); - // NOTE: TCell's can reference memomry from tupleValue bool CellsFromTuple(const NKikimrMiniKQL::TType* tupleType, const NKikimrMiniKQL::TValue& tupleValue, diff --git a/ydb/core/yq/libs/result_formatter/result_formatter.cpp b/ydb/core/yq/libs/result_formatter/result_formatter.cpp index dde4ac6886..736c7292c4 100644 --- a/ydb/core/yq/libs/result_formatter/result_formatter.cpp +++ b/ydb/core/yq/libs/result_formatter/result_formatter.cpp @@ -331,6 +331,7 @@ template <typename F> NJson::TJsonValue GenericFormatColumnValue( const NYdb::TValue& value, NKikimr::NMiniKQL::TType* type, + const TTypeEnvironment& typeEnv, const THolderFactory& holderFactory, F f) { @@ -353,6 +354,7 @@ NJson::TJsonValue GenericFormatColumnValue( auto unboxed = ImportValueFromProto( type, rawProtoValue, + typeEnv, holderFactory); return f(unboxed); @@ -361,9 +363,10 @@ NJson::TJsonValue GenericFormatColumnValue( NJson::TJsonValue FormatColumnValue( const NYdb::TValue& value, NKikimr::NMiniKQL::TType* type, + const TTypeEnvironment& typeEnv, const THolderFactory& holderFactory) { - return GenericFormatColumnValue(value, type, holderFactory, [type](auto unboxed) { + return GenericFormatColumnValue(value, type, typeEnv, holderFactory, [type](auto unboxed) { NJson::TJsonValue v; NJson::ReadJsonTree( NJson2Yson::ConvertYson2Json(NYql::NCommon::WriteYsonValue(unboxed, type)), @@ -375,6 +378,7 @@ NJson::TJsonValue FormatColumnValue( NJson::TJsonValue FormatColumnPrettyValue( const NYdb::TValue& value, NKikimr::NMiniKQL::TType* type, + const TTypeEnvironment& typeEnv, const THolderFactory& holderFactory) { @@ -382,7 +386,7 @@ NJson::TJsonValue FormatColumnPrettyValue( static const TValueConvertPolicy convertPolicy{ UNSAFE_NUMBER_AS_STRING }; - return GenericFormatColumnValue(value, type, holderFactory, [type](auto unboxed) { + return GenericFormatColumnValue(value, type, typeEnv, holderFactory, [type](auto unboxed) { NJson::TJsonValue v; TStringStream out; NJson::TJsonWriter jsonWriter(&out, MakeJsonConfig()); @@ -440,8 +444,8 @@ void FormatResultSet(NJson::TJsonValue& root, const NYdb::TResultSet& resultSet, for (size_t columnNum = 0; columnNum < columnsMeta.size(); ++columnNum) { const NYdb::TColumn& columnMeta = columnsMeta[columnNum]; NJson::TJsonValue v = prettyValueFormat - ? FormatColumnPrettyValue(rsParser.GetValue(columnNum), columnTypes[columnNum].MiniKQLType, holderFactory) - : FormatColumnValue(rsParser.GetValue(columnNum), columnTypes[columnNum].MiniKQLType, holderFactory); + ? FormatColumnPrettyValue(rsParser.GetValue(columnNum), columnTypes[columnNum].MiniKQLType, typeEnv, holderFactory) + : FormatColumnValue(rsParser.GetValue(columnNum), columnTypes[columnNum].MiniKQLType, typeEnv, holderFactory); if (prettyValueFormat) { row.AppendValue(std::move(v)); } else { diff --git a/ydb/library/mkql_proto/mkql_proto.cpp b/ydb/library/mkql_proto/mkql_proto.cpp index a88e95785d..c1b5abea1d 100644 --- a/ydb/library/mkql_proto/mkql_proto.cpp +++ b/ydb/library/mkql_proto/mkql_proto.cpp @@ -1375,7 +1375,8 @@ TType* TProtoImporter::ImportTypeFromProto(const Ydb::Type& input) { Y_FORCE_INLINE void CheckTypeId(i32 id, i32 expected, std::string_view typeName) { if (id != expected) { - throw yexception() << "Invalid value representation for type: " << typeName; + throw yexception() << "Invalid value representation for type: " << typeName + << ", expected value case: " << expected << ", but current: " << id; } } @@ -1543,14 +1544,45 @@ NUdf::TUnboxedValue TProtoImporter::ImportValueFromProto(const TType* type, cons return KindDataImport(type, value); case TType::EKind::Optional: { - const TOptionalType* optionalType = static_cast<const TOptionalType*>(type); - switch (value.value_case()) { - case Ydb::Value::kNestedValue: - return ImportValueFromProto(optionalType->GetItemType(), value.nested_value(), factory).MakeOptional(); - case Ydb::Value::kNullFlagValue: - return NUdf::TUnboxedValue(); - default: - return ImportValueFromProto(optionalType->GetItemType(), value, factory).MakeOptional(); + const TType* innerType = type; + const Ydb::Value* innerValue = &value; + ui32 level = 0; + ui32 nestLevel = 0; + while(innerType->GetKind() == TType::EKind::Optional) { + const TOptionalType* optionalType = static_cast<const TOptionalType*>(innerType); + innerType = optionalType->GetItemType(); + ++level; + if (innerValue->value_case() == Ydb::Value::kNestedValue) { + innerValue = &(innerValue->nested_value()); + ++nestLevel; + } + } + + const Ydb::Value* tmpValue = innerValue; + while(tmpValue->value_case() == Ydb::Value::kNestedValue) { + tmpValue = &(tmpValue->nested_value()); + } + + if (innerType->GetKind() == TType::EKind::Variant) { + if (tmpValue->value_case() == Ydb::Value::kNullFlagValue) { + auto res = ImportValueFromProto(innerType, *innerValue, factory); + while (level-->0) { res = res.MakeOptional(); } + return res; + } + auto res = ImportValueFromProto(innerType, value, factory); + while (level-->0) { res = res.MakeOptional(); } + return res; + } else { + MKQL_ENSURE(innerValue->value_case() != Ydb::Value::kNestedValue, "unexpected nested value"); + if (innerValue->value_case() != Ydb::Value::kNullFlagValue) { + auto res = ImportValueFromProto(innerType, *innerValue, factory); + while (level-->0) { res = res.MakeOptional(); } + return res; + } else { + auto res = NUdf::TUnboxedValue(); + while (nestLevel-->0) { res = res.MakeOptional(); } + return res; + } } } @@ -1564,7 +1596,7 @@ NUdf::TUnboxedValue TProtoImporter::ImportValueFromProto(const TType* type, cons *items++ = ImportValueFromProto(itemType, x, factory); } - return std::move(array); + return array; } case TType::EKind::Struct: { @@ -1572,25 +1604,31 @@ NUdf::TUnboxedValue TProtoImporter::ImportValueFromProto(const TType* type, cons NUdf::TUnboxedValue* itemsPtr = nullptr; auto res = factory.CreateDirectArrayHolder(structType->GetMembersCount(), itemsPtr); TRemapArray remap = TRemapArray::FromCookie(structType->GetCookie()); + MKQL_ENSURE((ui32)value.items_size() == structType->GetMembersCount(), + "Member size mismatch. members in value: " << value.items_size() + << ", members in type: " << structType->GetMembersCount()); for (ui32 index = 0; index < structType->GetMembersCount(); ++index) { ui32 remapped = remap.empty() ? index : remap[index]; auto memberType = structType->GetMemberType(index); itemsPtr[index] = ImportValueFromProto(memberType, value.items(remapped), factory); } - return std::move(res); + return res; } case TType::EKind::Tuple: { const TTupleType* tupleType = static_cast<const TTupleType*>(type); NUdf::TUnboxedValue* itemsPtr = nullptr; auto res = factory.CreateDirectArrayHolder(tupleType->GetElementsCount(), itemsPtr); + MKQL_ENSURE_S((ui32)value.items_size() == tupleType->GetElementsCount(), + "Elements size mismatch. Elements in value: " << value.items_size() + << ", elements in type: " << tupleType->GetElementsCount()); for (ui32 index = 0; index < tupleType->GetElementsCount(); ++index) { auto elementType = tupleType->GetElementType(index); itemsPtr[index] = ImportValueFromProto(elementType, value.items(index), factory); } - return std::move(res); + return res; } case TType::EKind::Dict: { @@ -1606,21 +1644,27 @@ NUdf::TUnboxedValue TProtoImporter::ImportValueFromProto(const TType* type, cons ); } - return std::move(dictBuilder->Build()); + return dictBuilder->Build(); } case TType::EKind::Variant: { const TVariantType* variantType = static_cast<const TVariantType*>(type); auto index = value.variant_index(); - auto unboxedValue = ImportValueFromProto(variantType->GetAlternativeType(index), value, factory); - auto res = factory.CreateVariantHolder(std::move(unboxedValue.Release()), index); - return std::move(res); + MKQL_ENSURE_S(index < variantType->GetAlternativesCount(), "type has " << variantType->GetAlternativesCount() + << " alternatives, but requested " << index); + auto alternative = variantType->GetAlternativeType(index); + if (value.value_case() == Ydb::Value::kNestedValue) { + auto unboxedValue = ImportValueFromProto(alternative, value.nested_value(), factory); + return factory.CreateVariantHolder(std::move(unboxedValue.Release()), index); + } + auto unboxedValue = ImportValueFromProto(alternative, value, factory); + return factory.CreateVariantHolder(std::move(unboxedValue.Release()), index); } case TType::EKind::Tagged: { const TTaggedType* taggedType = static_cast<const TTaggedType*>(type); auto unboxedValue = ImportValueFromProto(taggedType->GetBaseType(), value, factory); - return std::move(unboxedValue); + return unboxedValue; } case TType::EKind::Pg: @@ -1740,6 +1784,15 @@ std::pair<TType*, NUdf::TUnboxedValue> ImportValueFromProto(const Ydb::Type& typ return {nodeType, unboxedValue}; } +NUdf::TUnboxedValue ImportValueFromProto(TType* type, + const Ydb::Value& value, const TTypeEnvironment& env, const THolderFactory& factory) +{ + TProtoImporter importer(env); + auto unboxedValue = importer.ImportValueFromProto(type, value, factory); + return unboxedValue; + +} + TType* ImportTypeFromProto(const NKikimrMiniKQL::TType& type, const TTypeEnvironment& env) { TProtoImporter importer(env); return importer.ImportTypeFromProto(type); diff --git a/ydb/library/mkql_proto/mkql_proto.h b/ydb/library/mkql_proto/mkql_proto.h index 3514d2a297..44d8003cfc 100644 --- a/ydb/library/mkql_proto/mkql_proto.h +++ b/ydb/library/mkql_proto/mkql_proto.h @@ -22,6 +22,8 @@ TType* ImportTypeFromProto(const NKikimrMiniKQL::TType& type, const TTypeEnviron std::pair<TType*, NUdf::TUnboxedValue> ImportValueFromProto(const Ydb::Type& type, const Ydb::Value& value, const TTypeEnvironment& env, const THolderFactory& factory); +NUdf::TUnboxedValue ImportValueFromProto(TType* type, const Ydb::Value& value, + const TTypeEnvironment& env, const THolderFactory& factory); std::pair<TType*, NUdf::TUnboxedValue> ImportValueFromProto(const NKikimrMiniKQL::TType& type, const NKikimrMiniKQL::TValue& value, const TTypeEnvironment& env, const THolderFactory& factory); TRuntimeNode ImportValueFromProto(const NKikimrMiniKQL::TType& type, const NKikimrMiniKQL::TValue& value, |