Skip to content

Commit 5ae4652

Browse files
cherepashkablinkov
cherepashka
authored andcommitted
YT-21910: Master compact table schema
- Changelog entry Type: feature Component: master Introduce TCompactTableSchema, that holds wire protobuf schema representation and lighter than TTableSchema commit_hash:21801854b37fc25c5004ee01e5b79a3b3b6ea983
1 parent a30e201 commit 5ae4652

File tree

9 files changed

+81
-43
lines changed

9 files changed

+81
-43
lines changed

library/cpp/yt/misc/property.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ public: \
175175
} \
176176
static_assert(true)
177177

178+
//! Defines a trivial public read-only boolean property that is passed by value.
179+
//! All arguments after name are used as default value (via braced-init-list).
180+
#define DEFINE_BYVAL_RO_BOOLEAN_PROPERTY(name, ...) \
181+
protected: \
182+
bool name##_ { __VA_ARGS__ }; \
183+
\
184+
public: \
185+
Y_FORCE_INLINE bool Is##name() const \
186+
{ \
187+
return name##_; \
188+
} \
189+
static_assert(true)
190+
178191
//! Defines a trivial public read-write property that is passed by value.
179192
//! All arguments after name are used as default value (via braced-init-list).
180193
#define DEFINE_BYVAL_RW_PROPERTY_WITH_FLUENT_SETTER(declaringType, type, name, ...) \

yt/yt/client/api/rpc_proxy/helpers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,8 @@ void FromProto(NTableClient::TColumnSchema* schema, const NProto::TColumnSchema&
505505
void ToProto(NProto::TTableSchema* protoSchema, const NTableClient::TTableSchema& schema)
506506
{
507507
ToProto(protoSchema->mutable_columns(), schema.Columns());
508-
protoSchema->set_strict(schema.GetStrict());
509-
protoSchema->set_unique_keys(schema.GetUniqueKeys());
508+
protoSchema->set_strict(schema.IsStrict());
509+
protoSchema->set_unique_keys(schema.IsUniqueKeys());
510510
}
511511

512512
void FromProto(NTableClient::TTableSchema* schema, const NProto::TTableSchema& protoSchema)

yt/yt/client/table_client/check_schema_compatibility.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ std::pair<ESchemaCompatibility, TError> CheckTableSchemaCompatibilityImpl(
1818
TTableSchemaCompatibilityOptions options)
1919
{
2020
// If output schema is strict, check that input columns are subset of output columns.
21-
if (outputSchema.GetStrict()) {
22-
if (!inputSchema.GetStrict()) {
21+
if (outputSchema.IsStrict()) {
22+
if (!inputSchema.IsStrict()) {
2323
return {
2424
ESchemaCompatibility::Incompatible,
2525
TError("Incompatible strictness: input schema is not strict while output schema is"),
@@ -116,7 +116,7 @@ std::pair<ESchemaCompatibility, TError> CheckTableSchemaCompatibilityImpl(
116116
TError("Unexpected computed column %v in output schema",
117117
outputColumn.GetDiagnosticNameString()),
118118
};
119-
} else if (!inputSchema.GetStrict()) {
119+
} else if (!inputSchema.IsStrict()) {
120120
return {
121121
ESchemaCompatibility::Incompatible,
122122
TError("Column %v is present in output schema and is missing in non-strict input schema",
@@ -160,7 +160,7 @@ std::pair<ESchemaCompatibility, TError> CheckTableSchemaCompatibilityImpl(
160160
// Check that we don't lose complex types.
161161
// We never want to teleport complex types to schemaless part of the chunk because we want to change their type from
162162
// EValueType::Composite to EValueType::Any.
163-
if (!outputSchema.GetStrict()) {
163+
if (!outputSchema.IsStrict()) {
164164
for (const auto& inputColumn : inputSchema.Columns()) {
165165
if (!IsV3Composite(inputColumn.LogicalType())) {
166166
continue;
@@ -189,8 +189,8 @@ std::pair<ESchemaCompatibility, TError> CheckTableSchemaCompatibilityImpl(
189189
};
190190
}
191191

192-
if (outputSchema.GetUniqueKeys()) {
193-
if (!inputSchema.GetUniqueKeys()) {
192+
if (outputSchema.IsUniqueKeys()) {
193+
if (!inputSchema.IsUniqueKeys()) {
194194
return {
195195
ESchemaCompatibility::Incompatible,
196196
TError("Input schema \"unique_keys\" attribute is false"),

yt/yt/client/table_client/helpers.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,9 @@ TUnversionedOwningRow YsonToSchemafulRow(
253253
for (const auto& [name, value] : rowParts) {
254254
int id = nameTable->GetIdOrRegisterName(name);
255255
if (id >= std::ssize(tableSchema.Columns())) {
256-
if (validateValues && tableSchema.GetStrict()) {
257-
THROW_ERROR_EXCEPTION(NTableClient::EErrorCode::SchemaViolation,
256+
if (validateValues && tableSchema.IsStrict()) {
257+
THROW_ERROR_EXCEPTION(
258+
EErrorCode::SchemaViolation,
258259
"Unknown column %Qv in strict schema",
259260
name);
260261
}

yt/yt/client/table_client/merge_table_schemas.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ TTableSchemaPtr MergeTableSchemas(
8080
<< ex;
8181
}
8282

83-
} else if (!firstSchema->GetStrict()) {
83+
} else if (!firstSchema->IsStrict()) {
8484
THROW_ERROR_EXCEPTION("Column %v is present in second schema and is missing in non-strict first schema",
8585
secondSchemaColumn.GetDiagnosticNameString());
8686
} else {
@@ -90,7 +90,7 @@ TTableSchemaPtr MergeTableSchemas(
9090

9191
for (const auto& firstSchemaColumn : firstSchema->Columns()) {
9292
if (!secondSchema->FindColumn(firstSchemaColumn.Name())) {
93-
if (!secondSchema->GetStrict()) {
93+
if (!secondSchema->IsStrict()) {
9494
THROW_ERROR_EXCEPTION("Column %v is present in first schema and is missing in non-strict second schema",
9595
firstSchemaColumn.GetDiagnosticNameString());
9696
}
@@ -114,17 +114,17 @@ TTableSchemaPtr MergeTableSchemas(
114114
return {
115115
New<TTableSchema>(
116116
resultColumns,
117-
/*strict*/ firstSchema->GetStrict() && secondSchema->GetStrict(),
118-
firstSchema->GetUniqueKeys() && secondSchema->GetUniqueKeys(),
117+
firstSchema->IsStrict() && secondSchema->IsStrict(),
118+
firstSchema->IsUniqueKeys() && secondSchema->IsUniqueKeys(),
119119
ETableSchemaModification::None,
120120
firstSchema->DeletedColumns())
121121
};
122122
} else {
123123
return {
124124
New<TTableSchema>(
125125
resultColumns,
126-
/*strict*/ firstSchema->GetStrict() && secondSchema->GetStrict(),
127-
firstSchema->GetUniqueKeys() && secondSchema->GetUniqueKeys())
126+
firstSchema->IsStrict() && secondSchema->IsStrict(),
127+
firstSchema->IsUniqueKeys() && secondSchema->IsUniqueKeys())
128128
};
129129
}
130130
}

yt/yt/client/table_client/schema.cpp

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ std::string TTableSchema::TNameMapping::StableNameToName(const TColumnStableName
498498
{
499499
auto* column = Schema_.FindColumnByStableName(stableName);
500500
if (!column) {
501-
if (Schema_.GetStrict()) {
501+
if (Schema_.IsStrict()) {
502502
THROW_ERROR_EXCEPTION("No column with stable name %Qv in strict schema", stableName);
503503
}
504504
return stableName.Underlying();
@@ -510,7 +510,7 @@ TColumnStableName TTableSchema::TNameMapping::NameToStableName(TStringBuf name)
510510
{
511511
auto* column = Schema_.FindColumn(name);
512512
if (!column) {
513-
if (Schema_.GetStrict()) {
513+
if (Schema_.IsStrict()) {
514514
if (auto originalColumnName = GetTimestampColumnOriginalNameOrNull(name);
515515
!originalColumnName || !Schema_.FindColumn(*originalColumnName))
516516
{
@@ -759,11 +759,6 @@ bool TTableSchema::IsSorted() const
759759
return KeyColumnCount_ > 0;
760760
}
761761

762-
bool TTableSchema::IsUniqueKeys() const
763-
{
764-
return UniqueKeys_;
765-
}
766-
767762
bool TTableSchema::HasRenamedColumns() const
768763
{
769764
return std::any_of(Columns().begin(), Columns().end(), [] (const TColumnSchema& column) {
@@ -873,7 +868,7 @@ std::vector<TColumnStableName> MapNamesToStableNames(
873868
const auto* column = schema.FindColumn(name);
874869
if (column) {
875870
stableNames.push_back(column->StableName());
876-
} else if (!schema.GetStrict()) {
871+
} else if (!schema.IsStrict()) {
877872
stableNames.push_back(TColumnStableName(name));
878873
} else if (missingColumnReplacement) {
879874
stableNames.push_back(TColumnStableName(std::string(*missingColumnReplacement)));
@@ -1496,7 +1491,7 @@ TKeyColumnTypes TTableSchema::GetKeyColumnTypes() const
14961491

14971492
void FormatValue(TStringBuilderBase* builder, const TTableSchema& schema, TStringBuf /*spec*/)
14981493
{
1499-
builder->AppendFormat("<strict=%v;unique_keys=%v", schema.GetStrict(), schema.GetUniqueKeys());
1494+
builder->AppendFormat("<strict=%v;unique_keys=%v", schema.IsStrict(), schema.IsUniqueKeys());
15001495
if (schema.HasNontrivialSchemaModification()) {
15011496
builder->AppendFormat(";schema_modification=%v", schema.GetSchemaModification());
15021497
}
@@ -1530,10 +1525,18 @@ void FormatValue(TStringBuilderBase* builder, const TTableSchemaPtr& schema, TSt
15301525
}
15311526

15321527
std::string SerializeToWireProto(const TTableSchemaPtr& schema)
1528+
{
1529+
return schema ? SerializeToWireProto(*schema) : "";
1530+
}
1531+
1532+
std::string SerializeToWireProto(const TTableSchema& schema)
15331533
{
15341534
NTableClient::NProto::TTableSchemaExt protoSchema;
15351535
ToProto(&protoSchema, schema);
1536-
return protoSchema.SerializeAsString();
1536+
if (protoSchema.IsInitialized()) {
1537+
return protoSchema.SerializeAsString();
1538+
}
1539+
THROW_ERROR_EXCEPTION("Table schema is not initialized");
15371540
}
15381541

15391542
void DeserializeFromWireProto(TTableSchemaPtr* schema, const std::string& serializedProto)
@@ -1549,8 +1552,8 @@ void ToProto(NProto::TTableSchemaExt* protoSchema, const TTableSchema& schema)
15491552
{
15501553
ToProto(protoSchema->mutable_columns(), schema.Columns());
15511554
ToProto(protoSchema->mutable_deleted_columns(), schema.DeletedColumns());
1552-
protoSchema->set_strict(schema.GetStrict());
1553-
protoSchema->set_unique_keys(schema.GetUniqueKeys());
1555+
protoSchema->set_strict(schema.IsStrict());
1556+
protoSchema->set_unique_keys(schema.IsUniqueKeys());
15541557
protoSchema->set_schema_modification(ToProto(schema.GetSchemaModification()));
15551558
}
15561559

@@ -1674,8 +1677,8 @@ bool operator==(const TTableSchema& lhs, const TTableSchema& rhs)
16741677
{
16751678
return
16761679
lhs.Columns() == rhs.Columns() &&
1677-
lhs.GetStrict() == rhs.GetStrict() &&
1678-
lhs.GetUniqueKeys() == rhs.GetUniqueKeys() &&
1680+
lhs.IsStrict() == rhs.IsStrict() &&
1681+
lhs.IsUniqueKeys() == rhs.IsUniqueKeys() &&
16791682
lhs.GetSchemaModification() == rhs.GetSchemaModification() &&
16801683
lhs.DeletedColumns() == rhs.DeletedColumns();
16811684
}
@@ -1691,7 +1694,7 @@ bool IsEqualIgnoringRequiredness(const TTableSchema& lhs, const TTableSchema& rh
16911694
}
16921695
resultColumns.emplace_back(column);
16931696
}
1694-
return TTableSchema(resultColumns, schema.GetStrict(), schema.GetUniqueKeys());
1697+
return TTableSchema(resultColumns, schema.IsStrict(), schema.IsUniqueKeys());
16951698
};
16961699
return dropRequiredness(lhs) == dropRequiredness(rhs);
16971700
}
@@ -2090,11 +2093,11 @@ void ValidateColumnSchema(
20902093

20912094
void ValidateDynamicTableConstraints(const TTableSchema& schema)
20922095
{
2093-
if (!schema.GetStrict()) {
2096+
if (!schema.IsStrict()) {
20942097
THROW_ERROR_EXCEPTION("\"strict\" cannot be \"false\" for a dynamic table");
20952098
}
20962099

2097-
if (schema.IsSorted() && !schema.GetUniqueKeys()) {
2100+
if (schema.IsSorted() && !schema.IsUniqueKeys()) {
20982101
THROW_ERROR_EXCEPTION("\"unique_keys\" cannot be \"false\" for a sorted dynamic table");
20992102
}
21002103

@@ -2308,7 +2311,7 @@ void ValidateCumulativeDataWeightColumn(const TTableSchema& schema)
23082311
// Validate schema attributes.
23092312
void ValidateSchemaAttributes(const TTableSchema& schema)
23102313
{
2311-
if (schema.GetUniqueKeys() && schema.GetKeyColumnCount() == 0) {
2314+
if (schema.IsUniqueKeys() && schema.GetKeyColumnCount() == 0) {
23122315
THROW_ERROR_EXCEPTION("\"unique_keys\" can only be true if key columns are present");
23132316
}
23142317
}
@@ -2325,7 +2328,7 @@ void ValidateTableSchema(
23252328
schema.IsSorted(),
23262329
isTableDynamic,
23272330
options);
2328-
if (!schema.GetStrict() && column.IsRenamed()) {
2331+
if (!schema.IsStrict() && column.IsRenamed()) {
23292332
THROW_ERROR_EXCEPTION("Renamed column %v in non-strict schema",
23302333
column.GetDiagnosticNameString());
23312334
}
@@ -2396,6 +2399,24 @@ void ValidateNoDescendingSortOrder(const TTableSchema& schema)
23962399
}
23972400
}
23982401

2402+
void ValidateNoDescendingSortOrder(
2403+
const std::vector<ESortOrder>& sortOrders,
2404+
const TKeyColumns& keyColumns)
2405+
{
2406+
YT_VERIFY(keyColumns.size() == sortOrders.size());
2407+
2408+
for (int index = 0; index < std::ssize(sortOrders); ++index) {
2409+
auto sortOrder = sortOrders[index];
2410+
const auto& column = keyColumns[index];
2411+
if (sortOrder == ESortOrder::Descending) {
2412+
THROW_ERROR_EXCEPTION(
2413+
NTableClient::EErrorCode::InvalidSchemaValue,
2414+
"Descending sort order is not available in this context yet")
2415+
<< TErrorAttribute("column_name", column);
2416+
}
2417+
}
2418+
}
2419+
23992420
void ValidateNoRenamedColumns(const TTableSchema& schema)
24002421
{
24012422
for (const auto& column : schema.Columns()) {
@@ -2556,7 +2577,7 @@ size_t THash<NYT::NTableClient::TDeletedColumn>::operator()(const NYT::NTableCli
25562577

25572578
size_t THash<NYT::NTableClient::TTableSchema>::operator()(const NYT::NTableClient::TTableSchema& tableSchema) const
25582579
{
2559-
size_t result = CombineHashes(THash<bool>()(tableSchema.GetUniqueKeys()), THash<bool>()(tableSchema.GetStrict()));
2580+
size_t result = CombineHashes(THash<bool>()(tableSchema.IsUniqueKeys()), THash<bool>()(tableSchema.IsStrict()));
25602581
if (tableSchema.HasNontrivialSchemaModification()) {
25612582
result = CombineHashes(
25622583
result,

yt/yt/client/table_client/schema.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ class TTableSchema final
242242
const std::vector<TDeletedColumn>& DeletedColumns() const;
243243

244244
//! Strict schema forbids columns not specified in the schema.
245-
DEFINE_BYVAL_RO_PROPERTY(bool, Strict, false);
246-
DEFINE_BYVAL_RO_PROPERTY(bool, UniqueKeys, false);
245+
DEFINE_BYVAL_RO_BOOLEAN_PROPERTY(Strict, false);
246+
DEFINE_BYVAL_RO_BOOLEAN_PROPERTY(UniqueKeys, false);
247247
DEFINE_BYVAL_RO_PROPERTY(ETableSchemaModification, SchemaModification, ETableSchemaModification::None);
248248

249249
//! Constructs an empty non-strict schema.
@@ -296,7 +296,6 @@ class TTableSchema final
296296
bool HasTimestampColumn() const;
297297
bool HasTtlColumn() const;
298298
bool IsSorted() const;
299-
bool IsUniqueKeys() const;
300299
bool HasRenamedColumns() const;
301300
bool IsEmpty() const;
302301
bool IsCGComparatorApplicable() const;
@@ -446,6 +445,7 @@ void FormatValue(TStringBuilderBase* builder, const TTableSchemaPtr& schema, TSt
446445

447446
//! Returns serialized NTableClient.NProto.TTableSchemaExt.
448447
std::string SerializeToWireProto(const TTableSchemaPtr& schema);
448+
std::string SerializeToWireProto(const TTableSchema& schema);
449449

450450
void DeserializeFromWireProto(TTableSchemaPtr* schema, const std::string& serializedProto);
451451

@@ -554,6 +554,9 @@ void ValidateTableSchema(
554554
////////////////////////////////////////////////////////////////////////////////
555555

556556
void ValidateNoDescendingSortOrder(const TTableSchema& schema);
557+
void ValidateNoDescendingSortOrder(
558+
const std::vector<ESortOrder>& sortOrders,
559+
const TKeyColumns& keyColumns);
557560

558561
void ValidateNoRenamedColumns(const TTableSchema& schema);
559562

yt/yt/client/table_client/schema_serialization_helpers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ void Serialize(const TTableSchema& schema, NYson::IYsonConsumer* consumer)
231231
{
232232
auto position = NYTree::BuildYsonFluently(consumer)
233233
.BeginAttributes()
234-
.Item("strict").Value(schema.GetStrict())
235-
.Item("unique_keys").Value(schema.GetUniqueKeys())
234+
.Item("strict").Value(schema.IsStrict())
235+
.Item("unique_keys").Value(schema.IsUniqueKeys())
236236
.DoIf(schema.HasNontrivialSchemaModification(), [&] (NYTree::TFluentMap fluent) {
237237
fluent.Item("schema_modification").Value(schema.GetSchemaModification());
238238
})

yt/yt/client/table_client/timestamped_schema_helpers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ TTableSchemaPtr ToLatestTimestampSchema(const TTableSchemaPtr& schema)
2121

2222
return New<TTableSchema>(
2323
std::move(columns),
24-
schema->GetStrict(),
25-
schema->GetUniqueKeys(),
24+
schema->IsStrict(),
25+
schema->IsUniqueKeys(),
2626
schema->GetSchemaModification(),
2727
schema->DeletedColumns());
2828
}

0 commit comments

Comments
 (0)