Skip to content

Commit c7ade6d

Browse files
author
kvk1920
committed
Optimize alter-table verb
commit_hash:dbdf35744550a5a2b4a9b94f9b325b60f1dde718
1 parent 1b8df99 commit c7ade6d

File tree

4 files changed

+218
-26
lines changed

4 files changed

+218
-26
lines changed

yt/yt/client/table_client/logical_type.cpp

Lines changed: 111 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,18 @@ TDecimalLogicalType::TDecimalLogicalType(int precision, int scale)
320320
, Scale_(scale)
321321
{ }
322322

323-
size_t TDecimalLogicalType::GetMemoryUsage() const
323+
i64 TDecimalLogicalType::GetMemoryUsage() const
324324
{
325325
return sizeof(*this);
326326
}
327327

328+
i64 TDecimalLogicalType::GetMemoryUsage(i64 limit) const
329+
{
330+
YT_ASSERT(limit > 0);
331+
332+
return sizeof(*this);
333+
}
334+
328335
int TDecimalLogicalType::GetTypeComplexity() const
329336
{
330337
return 1;
@@ -372,7 +379,7 @@ std::optional<ESimpleLogicalValueType> TOptionalLogicalType::Simplify() const
372379
}
373380
}
374381

375-
size_t TOptionalLogicalType::GetMemoryUsage() const
382+
i64 TOptionalLogicalType::GetMemoryUsage() const
376383
{
377384
if (Element_->GetMetatype() == ELogicalMetatype::Simple) {
378385
// All optionals of simple logical types are singletons and therefore we assume they use no space.
@@ -382,6 +389,20 @@ size_t TOptionalLogicalType::GetMemoryUsage() const
382389
}
383390
}
384391

392+
i64 TOptionalLogicalType::GetMemoryUsage(i64 limit) const
393+
{
394+
YT_ASSERT(limit > 0);
395+
396+
if (Element_->GetMetatype() == ELogicalMetatype::Simple) {
397+
// NB: see TOptionalLogicalType::GetMemoryUsage().
398+
return 0;
399+
} else if (auto sizeOfThis = static_cast<i64>(sizeof(*this)); sizeOfThis >= limit) {
400+
return sizeof(*this);
401+
} else {
402+
return sizeOfThis + Element_->GetMemoryUsage(limit - sizeOfThis);
403+
}
404+
}
405+
385406
int TOptionalLogicalType::GetTypeComplexity() const
386407
{
387408
if (Element_->GetMetatype() == ELogicalMetatype::Simple) {
@@ -406,12 +427,19 @@ TSimpleLogicalType::TSimpleLogicalType(ESimpleLogicalValueType element)
406427
, Element_(element)
407428
{ }
408429

409-
size_t TSimpleLogicalType::GetMemoryUsage() const
430+
i64 TSimpleLogicalType::GetMemoryUsage() const
410431
{
411432
// All simple logical types are singletons and therefore we assume they use no space.
412433
return 0;
413434
}
414435

436+
i64 TSimpleLogicalType::GetMemoryUsage(i64 limit) const
437+
{
438+
YT_ASSERT(limit > 0);
439+
440+
return 0;
441+
}
442+
415443
int TSimpleLogicalType::GetTypeComplexity() const
416444
{
417445
return 1;
@@ -439,11 +467,22 @@ TListLogicalType::TListLogicalType(TLogicalTypePtr element)
439467
, Element_(std::move(element))
440468
{ }
441469

442-
size_t TListLogicalType::GetMemoryUsage() const
470+
i64 TListLogicalType::GetMemoryUsage() const
443471
{
444472
return sizeof(*this) + Element_->GetMemoryUsage();
445473
}
446474

475+
i64 TListLogicalType::GetMemoryUsage(i64 limit) const
476+
{
477+
YT_ASSERT(limit > 0);
478+
479+
if (auto sizeOfThis = static_cast<i64>(sizeof(*this)); sizeOfThis >= limit) {
480+
return sizeOfThis;
481+
} else {
482+
return sizeOfThis + Element_->GetMemoryUsage(limit - sizeOfThis);
483+
}
484+
}
485+
447486
int TListLogicalType::GetTypeComplexity() const
448487
{
449488
return 1 + Element_->GetTypeComplexity();
@@ -678,14 +717,28 @@ TStructLogicalTypeBase::TStructLogicalTypeBase(ELogicalMetatype metatype, std::v
678717
, Fields_(std::move(fields))
679718
{ }
680719

681-
size_t TStructLogicalTypeBase::GetMemoryUsage() const
720+
i64 TStructLogicalTypeBase::GetMemoryUsage() const
682721
{
683-
size_t result = sizeof(*this);
684-
result += sizeof(TStructField) * Fields_.size();
722+
auto usage = static_cast<i64>(sizeof(*this));
723+
usage += sizeof(TStructField) * Fields_.size();
685724
for (const auto& field : Fields_) {
686-
result += field.Type->GetMemoryUsage();
725+
usage += field.Type->GetMemoryUsage();
687726
}
688-
return result;
727+
return usage;
728+
}
729+
730+
i64 TStructLogicalTypeBase::GetMemoryUsage(i64 limit) const
731+
{
732+
YT_ASSERT(limit > 0);
733+
734+
auto usage = static_cast<i64>(sizeof(*this) + sizeof(TStructField) * Fields_.size());
735+
for (const auto& field : Fields_) {
736+
if (usage >= limit) {
737+
return usage;
738+
}
739+
usage += field.Type->GetMemoryUsage(limit - usage);
740+
}
741+
return usage;
689742
}
690743

691744
int TStructLogicalTypeBase::GetTypeComplexity() const
@@ -736,14 +789,28 @@ TTupleLogicalTypeBase::TTupleLogicalTypeBase(ELogicalMetatype metatype, std::vec
736789
, Elements_(std::move(elements))
737790
{ }
738791

739-
size_t TTupleLogicalTypeBase::GetMemoryUsage() const
792+
i64 TTupleLogicalTypeBase::GetMemoryUsage() const
740793
{
741-
size_t result = sizeof(*this);
742-
result += sizeof(TLogicalTypePtr) * Elements_.size();
794+
auto usage = static_cast<i64>(sizeof(*this));
795+
usage += sizeof(TLogicalTypePtr) * Elements_.size();
743796
for (const auto& element : Elements_) {
744-
result += element->GetMemoryUsage();
797+
usage += element->GetMemoryUsage();
745798
}
746-
return result;
799+
return usage;
800+
}
801+
802+
i64 TTupleLogicalTypeBase::GetMemoryUsage(i64 limit) const
803+
{
804+
YT_ASSERT(limit > 0);
805+
806+
auto usage = static_cast<i64>(sizeof(*this) + sizeof(TLogicalTypePtr) * Elements_.size());
807+
for (const auto& element : Elements_) {
808+
if (usage >= limit) {
809+
return usage;
810+
}
811+
usage += element->GetMemoryUsage(limit - usage);
812+
}
813+
return usage;
747814
}
748815

749816
int TTupleLogicalTypeBase::GetTypeComplexity() const
@@ -795,11 +862,27 @@ TDictLogicalType::TDictLogicalType(TLogicalTypePtr key, TLogicalTypePtr value)
795862
, Value_(std::move(value))
796863
{ }
797864

798-
size_t TDictLogicalType::GetMemoryUsage() const
865+
i64 TDictLogicalType::GetMemoryUsage() const
799866
{
800867
return sizeof(*this) + Key_->GetMemoryUsage() + Value_->GetMemoryUsage();
801868
}
802869

870+
i64 TDictLogicalType::GetMemoryUsage(i64 limit) const
871+
{
872+
YT_ASSERT(limit > 0);
873+
874+
auto usage = static_cast<i64>(sizeof(*this));
875+
if (usage >= limit) {
876+
return usage;
877+
}
878+
usage += Key_->GetMemoryUsage(limit - usage);
879+
if (usage >= limit) {
880+
return usage;
881+
}
882+
usage += Value_->GetMemoryUsage(limit - usage);
883+
return usage;
884+
}
885+
803886
int TDictLogicalType::GetTypeComplexity() const
804887
{
805888
return 1 + Key_->GetTypeComplexity() + Value_->GetTypeComplexity();
@@ -877,11 +960,23 @@ TTaggedLogicalType::TTaggedLogicalType(TString tag, NYT::NTableClient::TLogicalT
877960
, Element_(std::move(element))
878961
{ }
879962

880-
size_t TTaggedLogicalType::GetMemoryUsage() const
963+
i64 TTaggedLogicalType::GetMemoryUsage() const
881964
{
882965
return sizeof(*this) + GetElement()->GetMemoryUsage();
883966
}
884967

968+
i64 TTaggedLogicalType::GetMemoryUsage(i64 limit) const
969+
{
970+
YT_ASSERT(limit > 0);
971+
972+
auto usage = static_cast<i64>(sizeof(*this));
973+
if (usage >= limit) {
974+
return usage;
975+
}
976+
usage += GetElement()->GetMemoryUsage(limit - usage);
977+
return usage;
978+
}
979+
885980
int TTaggedLogicalType::GetTypeComplexity() const
886981
{
887982
return 1 + GetElement()->GetTypeComplexity();

yt/yt/client/table_client/logical_type.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class TLogicalType
7171
const TTaggedLogicalType& AsTaggedTypeRef() const;
7272
Y_FORCE_INLINE const TTaggedLogicalType& UncheckedAsTaggedTypeRef() const;
7373

74-
virtual size_t GetMemoryUsage() const = 0;
74+
virtual i64 GetMemoryUsage() const = 0;
75+
virtual i64 GetMemoryUsage(i64 limit) const = 0;
7576
virtual int GetTypeComplexity() const = 0;
7677

7778
// This function doesn't validate children of current node.
@@ -175,7 +176,8 @@ class TDecimalLogicalType
175176
public:
176177
TDecimalLogicalType(int precision, int scale);
177178

178-
size_t GetMemoryUsage() const override;
179+
i64 GetMemoryUsage() const override;
180+
i64 GetMemoryUsage(i64 limit) const override;
179181
int GetTypeComplexity() const override;
180182
void ValidateNode(const TWalkContext& context) const override;
181183
bool IsNullable() const override;
@@ -203,7 +205,8 @@ class TOptionalLogicalType
203205
// Cached value of GetElement()->IsNullable(), useful for performance reasons.
204206
Y_FORCE_INLINE bool IsElementNullable() const;
205207

206-
size_t GetMemoryUsage() const override;
208+
i64 GetMemoryUsage() const override;
209+
i64 GetMemoryUsage(i64 limit) const override;
207210
int GetTypeComplexity() const override;
208211
void ValidateNode(const TWalkContext& context) const override;
209212
bool IsNullable() const override;
@@ -223,7 +226,8 @@ class TSimpleLogicalType
223226

224227
Y_FORCE_INLINE ESimpleLogicalValueType GetElement() const;
225228

226-
size_t GetMemoryUsage() const override;
229+
i64 GetMemoryUsage() const override;
230+
i64 GetMemoryUsage(i64 limit) const override;
227231
int GetTypeComplexity() const override;
228232
void ValidateNode(const TWalkContext& context) const override;
229233
bool IsNullable() const override;
@@ -242,7 +246,8 @@ class TListLogicalType
242246

243247
Y_FORCE_INLINE const TLogicalTypePtr& GetElement() const;
244248

245-
size_t GetMemoryUsage() const override;
249+
i64 GetMemoryUsage() const override;
250+
i64 GetMemoryUsage(i64 limit) const override;
246251
int GetTypeComplexity() const override;
247252
void ValidateNode(const TWalkContext& context) const override;
248253
bool IsNullable() const override;
@@ -302,7 +307,8 @@ class TStructLogicalTypeBase
302307
TStructLogicalTypeBase(ELogicalMetatype metatype, std::vector<TStructField> fields);
303308
Y_FORCE_INLINE const std::vector<TStructField>& GetFields() const;
304309

305-
size_t GetMemoryUsage() const override;
310+
i64 GetMemoryUsage() const override;
311+
i64 GetMemoryUsage(i64 limit) const override;
306312
int GetTypeComplexity() const override;
307313
void ValidateNode(const TWalkContext& context) const override;
308314
bool IsNullable() const override;
@@ -321,7 +327,8 @@ class TTupleLogicalTypeBase
321327

322328
Y_FORCE_INLINE const std::vector<TLogicalTypePtr>& GetElements() const;
323329

324-
size_t GetMemoryUsage() const override;
330+
i64 GetMemoryUsage() const override;
331+
i64 GetMemoryUsage(i64 limit) const override;
325332
int GetTypeComplexity() const override;
326333
void ValidateNode(const TWalkContext& context) const override;
327334
bool IsNullable() const override;
@@ -377,7 +384,8 @@ class TDictLogicalType
377384
Y_FORCE_INLINE const TLogicalTypePtr& GetKey() const;
378385
Y_FORCE_INLINE const TLogicalTypePtr& GetValue() const;
379386

380-
size_t GetMemoryUsage() const override;
387+
i64 GetMemoryUsage() const override;
388+
i64 GetMemoryUsage(i64 limit) const override;
381389
int GetTypeComplexity() const override;
382390
void ValidateNode(const TWalkContext& context) const override;
383391
bool IsNullable() const override;
@@ -398,7 +406,8 @@ class TTaggedLogicalType
398406
Y_FORCE_INLINE const TString& GetTag() const;
399407
Y_FORCE_INLINE const TLogicalTypePtr& GetElement() const;
400408

401-
size_t GetMemoryUsage() const override;
409+
i64 GetMemoryUsage() const override;
410+
i64 GetMemoryUsage(i64 limit) const override;
402411
int GetTypeComplexity() const override;
403412
void ValidateNode(const TWalkContext& context) const override;
404413
bool IsNullable() const override;

0 commit comments

Comments
 (0)