Skip to content

Commit 66c697e

Browse files
committed
YT-25099: Add support for type_v3 in record codegen
Adds column_type_v3 in record codegen field description. Example usage: ``` - cpp_name: Value cpp_type: std::optional<NYson::TYsonString> column_name: value column_type_v3: type_name: optional item: type_name: list item: string ``` commit_hash:11bafe0bbc55babce6482c0e66ae30ee74b5b6f2
1 parent 8cf1071 commit 66c697e

File tree

8 files changed

+220
-0
lines changed

8 files changed

+220
-0
lines changed

yt/yt/client/table_client/helpers-inl.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,35 @@ T FromUnversionedValue(TUnversionedValue unversionedValue)
574574

575575
////////////////////////////////////////////////////////////////////////////////
576576

577+
template <class T>
578+
TUnversionedValue ToUnversionedCompositeValue(
579+
T&& value,
580+
const TRowBufferPtr& rowBuffer,
581+
int id,
582+
EValueFlags flags)
583+
{
584+
TUnversionedValue unversionedValue;
585+
ToUnversionedCompositeValue(&unversionedValue, std::forward<T>(value), rowBuffer, id, flags);
586+
return unversionedValue;
587+
}
588+
589+
template <class T>
590+
void ToUnversionedCompositeValue(
591+
TUnversionedValue* unversionedValue,
592+
const std::optional<T>& value,
593+
const TRowBufferPtr& rowBuffer,
594+
int id,
595+
EValueFlags flags)
596+
{
597+
if (value) {
598+
ToUnversionedCompositeValue(unversionedValue, *value, rowBuffer, id, flags);
599+
} else {
600+
*unversionedValue = MakeUnversionedSentinelValue(EValueType::Null, id, flags);
601+
}
602+
}
603+
604+
////////////////////////////////////////////////////////////////////////////////
605+
577606
template <class... Ts>
578607
TUnversionedOwningRow MakeUnversionedOwningRow(Ts&&... values)
579608
{

yt/yt/client/table_client/helpers.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,19 @@ void FromUnversionedValue(TError* value, TUnversionedValue unversionedValue)
791791

792792
////////////////////////////////////////////////////////////////////////////////
793793

794+
void ToUnversionedCompositeValue(
795+
TUnversionedValue* unversionedValue,
796+
NYson::TYsonStringBuf value,
797+
const TRowBufferPtr& rowBuffer,
798+
int id,
799+
EValueFlags flags)
800+
{
801+
YT_VERIFY(value.GetType() == EYsonType::Node);
802+
*unversionedValue = rowBuffer->CaptureValue(MakeUnversionedCompositeValue(value.AsStringBuf(), id, flags));
803+
}
804+
805+
////////////////////////////////////////////////////////////////////////////////
806+
794807
void ProtobufToUnversionedValueImpl(
795808
TUnversionedValue* unversionedValue,
796809
const Message& value,

yt/yt/client/table_client/helpers.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,33 @@ auto ToUnversionedValues(
245245
Ts&&... values)
246246
-> std::array<TUnversionedValue, sizeof...(Ts)>;
247247

248+
/////////////////////////////////////////////////////////////////////////////////
249+
250+
// NB(apachee): FromUnversionedValue can handle unversioned composite value, but ToUnversionedValue always returns unversioned any value,
251+
// to produce unversioned composite values these helpers were introduced.
252+
253+
template <class T>
254+
TUnversionedValue ToUnversionedCompositeValue(
255+
T&& value,
256+
const TRowBufferPtr& rowBuffer,
257+
int id = 0,
258+
EValueFlags flags = EValueFlags::None);
259+
260+
template <class T>
261+
void ToUnversionedCompositeValue(
262+
TUnversionedValue* unversionedValue,
263+
const std::optional<T>& value,
264+
const TRowBufferPtr& rowBuffer,
265+
int id = 0,
266+
EValueFlags flags = EValueFlags::None);
267+
268+
void ToUnversionedCompositeValue(
269+
TUnversionedValue* unversionedValue,
270+
NYson::TYsonStringBuf value,
271+
const TRowBufferPtr& rowBuffer,
272+
int id = 0,
273+
EValueFlags flags = EValueFlags::None);
274+
248275
////////////////////////////////////////////////////////////////////////////////
249276

250277
template <class T>

yt/yt/client/table_client/record_codegen_cpp.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#include "record_codegen_cpp.h"
22

3+
#include <yt/yt/library/formats/format.h>
4+
5+
#include <yt/yt/core/ytree/convert.h>
6+
37
namespace NYT::NTableClient::NDetail {
48

9+
using namespace NFormats;
10+
511
////////////////////////////////////////////////////////////////////////////////
612

713
void ValidateKeyValueCount(TLegacyKey key, int count)
@@ -31,6 +37,14 @@ void ValidateRowValueCount(TUnversionedRow row, int id)
3137
}
3238
}
3339

40+
TLogicalTypePtr FromRecordCodegenTypeV3(TStringBuf data)
41+
{
42+
TMemoryInput input(data);
43+
auto producer = CreateProducerForFormat(TFormat(EFormatType::Json), EDataType::Structured, &input);
44+
45+
return ConvertTo<TLogicalTypePtr>(producer);
46+
}
47+
3448
////////////////////////////////////////////////////////////////////////////////
3549

3650
} // namespace NYT::NTableClient::NDetail

yt/yt/client/table_client/record_codegen_cpp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <yt/yt/client/table_client/comparator.h>
44
#include <yt/yt/client/table_client/helpers.h>
5+
#include <yt/yt/client/table_client/logical_type.h>
56
#include <yt/yt/client/table_client/name_table.h>
67
#include <yt/yt/client/table_client/row_base.h>
78
#include <yt/yt/client/table_client/schema.h>
@@ -18,6 +19,7 @@ namespace NYT::NTableClient::NDetail {
1819
void ValidateKeyValueCount(TLegacyKey key, int count);
1920
int GetColumnIdOrThrow(std::optional<int> optionalId, TStringBuf name);
2021
void ValidateRowValueCount(TUnversionedRow row, int id);
22+
TLogicalTypePtr FromRecordCodegenTypeV3(TStringBuf data);
2123

2224
////////////////////////////////////////////////////////////////////////////////
2325

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <yt/yt/client/table_client/record_codegen_cpp.h>
2+
3+
#include <yt/yt/core/test_framework/framework.h>
4+
5+
#include <library/cpp/yt/yson_string/string.h>
6+
7+
namespace NYT::NTableClient {
8+
namespace {
9+
10+
using namespace NYson;
11+
12+
////////////////////////////////////////////////////////////////////////////////
13+
14+
class TRecordCodegenTypeV3Test
15+
: public ::testing::TestWithParam<std::pair<TString, TLogicalTypePtr>>
16+
{ };
17+
18+
////////////////////////////////////////////////////////////////////////////////
19+
20+
TEST_P(TRecordCodegenTypeV3Test, Parse)
21+
{
22+
auto [rawTypeV3, expectedLogicalType] = GetParam();
23+
24+
auto logicalType = NDetail::FromRecordCodegenTypeV3(rawTypeV3);
25+
EXPECT_EQ(*logicalType, *expectedLogicalType);
26+
}
27+
28+
////////////////////////////////////////////////////////////////////////////////
29+
30+
INSTANTIATE_TEST_SUITE_P(
31+
TRecordCodegenTypeV3Test,
32+
TRecordCodegenTypeV3Test,
33+
::testing::Values(
34+
std::pair{
35+
TString(R"({"type_name": "optional", "item": {"type_name": "list", "item": "string"}})"),
36+
ConvertTo<TLogicalTypePtr>(TYsonString(TString("{type_name=optional;item={type_name=list;item=string}}")))
37+
},
38+
std::pair{
39+
TString(R"({"type_name": "int64"})"),
40+
ConvertTo<TLogicalTypePtr>(TYsonString(TString("{type_name=\"int64\"}")))
41+
}));
42+
43+
////////////////////////////////////////////////////////////////////////////////
44+
45+
} // namespace
46+
} // namespace NYT::NTableClient

yt/yt/client/table_client/unittests/unversioned_row_ut.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
#include <yt/yt/client/table_client/helpers.h>
12
#include <yt/yt/client/table_client/unversioned_row.h>
23
#include <yt/yt/client/table_client/row_buffer.h>
34

45
#include <yt/yt/core/test_framework/framework.h>
56

7+
#include <library/cpp/yt/yson_string/string.h>
8+
69
namespace NYT::NTableClient {
710
namespace {
811

12+
using namespace NYson;
13+
914
////////////////////////////////////////////////////////////////////////////////
1015

1116
TEST(TUnversionedOwningRowTest, DefaultCtor)
@@ -28,5 +33,88 @@ TEST(TUnversionedOwningRowTest, ConstructFromUnversionedRow)
2833

2934
////////////////////////////////////////////////////////////////////////////////
3035

36+
class TToUnversionedCompositeValueTest
37+
: public testing::TestWithParam<TYsonStringBuf>
38+
{ };
39+
40+
INSTANTIATE_TEST_SUITE_P(
41+
Basic,
42+
TToUnversionedCompositeValueTest,
43+
::testing::Values(
44+
"{foo=bar;baz=1234}",
45+
"[1;2;3;4;5]"));
46+
47+
TEST_P(TToUnversionedCompositeValueTest, Basic)
48+
{
49+
auto buffer = New<TRowBuffer>();
50+
51+
#define XX(paramType) \
52+
do { \
53+
paramType param{GetParam()}; \
54+
auto value = ToUnversionedCompositeValue(param, buffer, 42); \
55+
EXPECT_EQ(value.Id, 42); \
56+
EXPECT_EQ(value.Type, EValueType::Composite); \
57+
EXPECT_EQ(value.AsStringBuf(), param.AsStringBuf()); \
58+
\
59+
paramType reconstructedParam; \
60+
FromUnversionedValue(&reconstructedParam, value); \
61+
EXPECT_EQ(param, reconstructedParam); \
62+
} while (false)
63+
64+
XX(TYsonStringBuf);
65+
XX(TYsonString);
66+
67+
#undef XX
68+
}
69+
70+
TEST_P(TToUnversionedCompositeValueTest, OptionalValue)
71+
{
72+
auto buffer = New<TRowBuffer>();
73+
74+
#define XX(paramType) \
75+
do { \
76+
paramType param{GetParam()}; \
77+
auto value = ToUnversionedCompositeValue(param, buffer, 42); \
78+
EXPECT_EQ(value.Id, 42); \
79+
EXPECT_EQ(value.Type, EValueType::Composite); \
80+
EXPECT_EQ(value.AsStringBuf(), param->AsStringBuf()); \
81+
\
82+
paramType reconstructedParam; \
83+
FromUnversionedValue(&reconstructedParam, value); \
84+
EXPECT_EQ(param, reconstructedParam); \
85+
} while (false)
86+
87+
XX(std::optional<TYsonStringBuf>);
88+
XX(std::optional<TYsonString>);
89+
90+
#undef XX
91+
}
92+
93+
94+
TEST_F(TToUnversionedCompositeValueTest, NullValue)
95+
{
96+
auto buffer = New<TRowBuffer>();
97+
98+
#define XX(paramType) \
99+
do { \
100+
paramType param{}; \
101+
auto value = ToUnversionedCompositeValue(param, buffer, 42); \
102+
EXPECT_EQ(value.Id, 42); \
103+
EXPECT_EQ(value.Type, EValueType::Null); \
104+
\
105+
paramType reconstructedParam; \
106+
FromUnversionedValue(&reconstructedParam, value); \
107+
EXPECT_EQ(param, reconstructedParam); \
108+
} while (false)
109+
110+
XX(std::optional<TYsonStringBuf>);
111+
XX(std::optional<TYsonString>);
112+
113+
#undef XX
114+
115+
}
116+
117+
////////////////////////////////////////////////////////////////////////////////
118+
31119
} // namespace
32120
} // namespace NYT::NTableClient

yt/yt/client/table_client/unittests/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ SRCS(
66
columnar_statistics_ut.cpp
77
columnar_ut.cpp
88
logical_type_short_notation_ut.cpp
9+
record_codegen_ut.cpp
910
serialization_ut.cpp
1011
unversioned_row_ut.cpp
1112
)

0 commit comments

Comments
 (0)