Skip to content

Commit fe42c0e

Browse files
authored
Merge 93a8f8b into bc0a601
2 parents bc0a601 + 93a8f8b commit fe42c0e

File tree

9 files changed

+664
-65
lines changed

9 files changed

+664
-65
lines changed

ydb/core/sys_view/show_create/create_table_formatter.cpp

Lines changed: 311 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,10 +612,11 @@ bool TCreateTableFormatter::Format(const TFamilyDescription& familyDesc) {
612612
compression = "off";
613613
break;
614614
case NKikimrSchemeOp::ColumnCodecLZ4:
615-
compression = "lz4";
615+
compression = "lz4";
616616
break;
617617
case NKikimrSchemeOp::ColumnCodecZSTD:
618-
ythrow TFormatFail(Ydb::StatusIds::UNSUPPORTED, "ZSTD COMPRESSION codec is not supported");
618+
compression = "zstd";
619+
break;
619620
}
620621
} else if (familyDesc.HasCodec()) {
621622
if (familyDesc.GetCodec() == 1) {
@@ -643,7 +644,7 @@ bool TCreateTableFormatter::Format(const TFamilyDescription& familyDesc) {
643644
del = ", ";
644645
}
645646

646-
if (dataName) {
647+
if (compression) {
647648
Stream << del << "COMPRESSION = " << "\"" << compression << "\"";
648649
}
649650

@@ -884,5 +885,312 @@ bool TCreateTableFormatter::Format(const Ydb::Table::TtlSettings& ttlSettings, T
884885
return true;
885886
}
886887

888+
TCreateTableFormatter::TResult TCreateTableFormatter::Format(const TString& tablePath, const TColumnTableDescription& tableDesc, bool temporary) {
889+
Stream.Clear();
890+
891+
TStringStreamWrapper wrapper(Stream);
892+
893+
Ydb::Table::CreateTableRequest createRequest;
894+
if (temporary) {
895+
Stream << "CREATE TEMPORARY TABLE ";
896+
} else {
897+
Stream << "CREATE TABLE ";
898+
}
899+
EscapeName(tablePath);
900+
Stream << " (\n";
901+
902+
const auto& schema = tableDesc.GetSchema();
903+
904+
std::map<ui32, const TOlapColumnDescription*> columns;
905+
for (const auto& column : schema.GetColumns()) {
906+
columns[column.GetId()] = &column;
907+
}
908+
909+
try {
910+
auto it = columns.cbegin();
911+
Format(*it->second);
912+
std::advance(it, 1);
913+
for (; it != columns.end(); ++it) {
914+
Stream << ",\n";
915+
Format(*it->second);
916+
}
917+
} catch (const TFormatFail& ex) {
918+
return TResult(ex.Status, ex.Error);
919+
} catch (const yexception& e) {
920+
return TResult(Ydb::StatusIds::UNSUPPORTED, e.what());
921+
}
922+
Stream << ",\n";
923+
924+
if (!schema.GetIndexes().empty()) {
925+
return TResult(Ydb::StatusIds::UNSUPPORTED, "Indexes are not supported yet for column tables.");
926+
}
927+
928+
bool isFamilyPrinted = false;
929+
if (!schema.GetColumnFamilies().empty()) {
930+
try {
931+
isFamilyPrinted = Format(schema.GetColumnFamilies(0));
932+
for (int i = 1; i < schema.GetColumnFamilies().size(); i++) {
933+
if (isFamilyPrinted) {
934+
Stream << ",\n";
935+
}
936+
isFamilyPrinted = Format(schema.GetColumnFamilies(i));
937+
}
938+
} catch (const TFormatFail& ex) {
939+
return TResult(ex.Status, ex.Error);
940+
} catch (const yexception& e) {
941+
return TResult(Ydb::StatusIds::UNSUPPORTED, e.what());
942+
}
943+
}
944+
945+
Y_ENSURE(!schema.GetKeyColumnNames().empty());
946+
if (isFamilyPrinted) {
947+
Stream << ",\n";
948+
}
949+
Stream << "\tPRIMARY KEY (";
950+
EscapeName(schema.GetKeyColumnNames(0));
951+
for (int i = 1; i < schema.GetKeyColumnNames().size(); i++) {
952+
Stream << ", ";
953+
EscapeName(schema.GetKeyColumnNames(i));
954+
}
955+
Stream << ")\n";
956+
Stream << ") ";
957+
958+
if (schema.HasOptions()) {
959+
const auto& options = schema.GetOptions();
960+
if (options.GetSchemeNeedActualization()) {
961+
return TResult(Ydb::StatusIds::UNSUPPORTED, "Unsupported setting: SCHEME_NEED_ACTUALIZATION");
962+
}
963+
if (options.HasScanReaderPolicyName() && !options.GetScanReaderPolicyName().empty()) {
964+
return TResult(Ydb::StatusIds::UNSUPPORTED, "Unsupported setting: SCAN_READER_POLICY_NAME");
965+
}
966+
if (options.HasCompactionPlannerConstructor()) {
967+
return TResult(Ydb::StatusIds::UNSUPPORTED, "Unsupported setting: COMPACTION_PLANNER");
968+
}
969+
if (options.HasMetadataManagerConstructor()) {
970+
return TResult(Ydb::StatusIds::UNSUPPORTED, "Unsupported setting: METADATA_MEMORY_MANAGER");
971+
}
972+
}
973+
974+
if (tableDesc.HasSharding()) {
975+
Format(tableDesc.GetSharding());
976+
}
977+
978+
Stream << "WITH (\n";
979+
Stream << "\tSTORE = COLUMN";
980+
981+
if (tableDesc.HasColumnShardCount()) {
982+
Stream << ",\n";
983+
Stream << "\tAUTO_PARTITIONING_MIN_PARTITIONS_COUNT = " << tableDesc.GetColumnShardCount();
984+
}
985+
986+
if (tableDesc.HasTtlSettings()) {
987+
Format(tableDesc.GetTtlSettings());
988+
}
989+
990+
Stream << "\n);";
991+
992+
TString statement = Stream.Str();
993+
TString formattedStatement;
994+
NYql::TIssues issues;
995+
if (!NYdb::NDump::Format(statement, formattedStatement, issues)) {
996+
return TResult(Ydb::StatusIds::INTERNAL_ERROR, issues.ToString());
997+
}
998+
999+
auto result = TResult(std::move(formattedStatement));
1000+
1001+
return result;
1002+
}
1003+
1004+
void TCreateTableFormatter::Format(const TOlapColumnDescription& olapColumnDesc) {
1005+
Stream << "\t";
1006+
EscapeName(olapColumnDesc.GetName());
1007+
Stream << " " << olapColumnDesc.GetType();
1008+
1009+
if (olapColumnDesc.HasColumnFamilyName()) {
1010+
Stream << " FAMILY ";
1011+
EscapeName(olapColumnDesc.GetColumnFamilyName());
1012+
}
1013+
if (olapColumnDesc.GetNotNull()) {
1014+
Stream << " NOT NULL";
1015+
}
1016+
if (olapColumnDesc.HasDefaultValue()) {
1017+
Format(olapColumnDesc.GetDefaultValue());
1018+
}
1019+
1020+
if (olapColumnDesc.HasStorageId() && !olapColumnDesc.GetStorageId().empty()) {
1021+
ythrow TFormatFail(Ydb::StatusIds::UNSUPPORTED, "Unsupported setting: STORAGE_ID");
1022+
}
1023+
1024+
if (olapColumnDesc.HasDataAccessorConstructor()) {
1025+
ythrow TFormatFail(Ydb::StatusIds::UNSUPPORTED, "Unsupported setting: DATA_ACCESSOR_CONSTRUCTOR");
1026+
}
1027+
1028+
if (olapColumnDesc.HasDictionaryEncoding()) {
1029+
ythrow TFormatFail(Ydb::StatusIds::UNSUPPORTED, "Unsupported setting: ENCODING.DICTIONARY");
1030+
}
1031+
}
1032+
1033+
void TCreateTableFormatter::Format(const NKikimrColumnShardColumnDefaults::TColumnDefault& defaultValue) {
1034+
if (!defaultValue.HasScalar()) {
1035+
return;
1036+
}
1037+
1038+
Stream << " DEFAULT ";
1039+
1040+
TGuard<NMiniKQL::TScopedAlloc> guard(Alloc);
1041+
const auto& scalar = defaultValue.GetScalar();
1042+
if (scalar.HasBool()) {
1043+
if (scalar.GetBool() == true) {
1044+
Stream << "true";
1045+
} else {
1046+
Stream << "false";
1047+
}
1048+
} else if (scalar.HasUint8()) {
1049+
const NUdf::TUnboxedValue str = NMiniKQL::ValueToString(NUdf::EDataSlot::Uint8, NUdf::TUnboxedValuePod(scalar.GetUint8()));
1050+
Y_ENSURE(str.HasValue());
1051+
Stream << TString(str.AsStringRef());
1052+
} else if (scalar.HasUint16()) {
1053+
const NUdf::TUnboxedValue str = NMiniKQL::ValueToString(NUdf::EDataSlot::Uint16, NUdf::TUnboxedValuePod(scalar.GetUint16()));
1054+
Y_ENSURE(str.HasValue());
1055+
Stream << TString(str.AsStringRef());
1056+
} else if (scalar.HasUint32()) {
1057+
const NUdf::TUnboxedValue str = NMiniKQL::ValueToString(NUdf::EDataSlot::Uint32, NUdf::TUnboxedValuePod(scalar.GetUint32()));
1058+
Y_ENSURE(str.HasValue());
1059+
Stream << TString(str.AsStringRef());
1060+
} else if (scalar.HasUint64()) {
1061+
const NUdf::TUnboxedValue str = NMiniKQL::ValueToString(NUdf::EDataSlot::Uint64, NUdf::TUnboxedValuePod(static_cast<ui64>(scalar.GetUint64())));
1062+
Y_ENSURE(str.HasValue());
1063+
Stream << TString(str.AsStringRef());
1064+
} else if (scalar.HasInt8()) {
1065+
const NUdf::TUnboxedValue str = NMiniKQL::ValueToString(NUdf::EDataSlot::Int8, NUdf::TUnboxedValuePod(scalar.GetInt8()));
1066+
Y_ENSURE(str.HasValue());
1067+
Stream << TString(str.AsStringRef());
1068+
} else if (scalar.HasInt16()) {
1069+
const NUdf::TUnboxedValue str = NMiniKQL::ValueToString(NUdf::EDataSlot::Int16, NUdf::TUnboxedValuePod(scalar.GetInt16()));
1070+
Y_ENSURE(str.HasValue());
1071+
Stream << TString(str.AsStringRef());
1072+
} else if (scalar.HasInt32()) {
1073+
const NUdf::TUnboxedValue str = NMiniKQL::ValueToString(NUdf::EDataSlot::Int32, NUdf::TUnboxedValuePod(scalar.GetInt32()));
1074+
Y_ENSURE(str.HasValue());
1075+
Stream << TString(str.AsStringRef());
1076+
} else if (scalar.HasInt64()) {
1077+
const NUdf::TUnboxedValue str = NMiniKQL::ValueToString(NUdf::EDataSlot::Int64, NUdf::TUnboxedValuePod(static_cast<i64>(scalar.GetInt64())));
1078+
Y_ENSURE(str.HasValue());
1079+
Stream << TString(str.AsStringRef());
1080+
} else if (scalar.HasDouble()) {
1081+
const NUdf::TUnboxedValue str = NMiniKQL::ValueToString(NUdf::EDataSlot::Double, NUdf::TUnboxedValuePod(scalar.GetDouble()));
1082+
Y_ENSURE(str.HasValue());
1083+
Stream << TString(str.AsStringRef());
1084+
} else if (scalar.HasFloat()) {
1085+
const NUdf::TUnboxedValue str = NMiniKQL::ValueToString(NUdf::EDataSlot::Float, NUdf::TUnboxedValuePod(scalar.GetFloat()));
1086+
Y_ENSURE(str.HasValue());
1087+
Stream << TString(str.AsStringRef());
1088+
} else if (scalar.HasTimestamp()) {
1089+
ui64 value = scalar.GetTimestamp().GetValue();
1090+
arrow::TimeUnit::type unit = arrow::TimeUnit::type(scalar.GetTimestamp().GetUnit());
1091+
switch (unit) {
1092+
case arrow::TimeUnit::SECOND:
1093+
value *= 1000000;
1094+
break;
1095+
case arrow::TimeUnit::MILLI:
1096+
value *= 1000;
1097+
break;
1098+
case arrow::TimeUnit::MICRO:
1099+
break;
1100+
case arrow::TimeUnit::NANO:
1101+
value /= 1000;
1102+
break;
1103+
}
1104+
Stream << "TIMESTAMP(";
1105+
const NUdf::TUnboxedValue str = NMiniKQL::ValueToString(NUdf::EDataSlot::Timestamp, NUdf::TUnboxedValuePod(value));
1106+
Y_ENSURE(str.HasValue());
1107+
EscapeString(TString(str.AsStringRef()));
1108+
Stream << ")";
1109+
} else if (scalar.HasString()) {
1110+
EscapeString(TString(scalar.GetString()));
1111+
} else {
1112+
ythrow TFormatFail(Ydb::StatusIds::UNSUPPORTED, "Unsupported type for default value");
1113+
}
1114+
}
1115+
1116+
void TCreateTableFormatter::Format(const NKikimrSchemeOp::TColumnTableSharding& sharding) {
1117+
switch (sharding.GetMethodCase()) {
1118+
case NKikimrSchemeOp::TColumnTableSharding::kHashSharding: {
1119+
const auto& hashSharding = sharding.GetHashSharding();
1120+
Y_ENSURE(!hashSharding.GetColumns().empty());
1121+
Stream << "PARTITION BY HASH(";
1122+
EscapeName(hashSharding.GetColumns(0));
1123+
for (int i = 1; i < hashSharding.GetColumns().size(); i++) {
1124+
Stream << ", ";
1125+
EscapeName(hashSharding.GetColumns(i));
1126+
}
1127+
Stream << ")\n";
1128+
break;
1129+
}
1130+
case NKikimrSchemeOp::TColumnTableSharding::kRandomSharding:
1131+
ythrow TFormatFail(Ydb::StatusIds::UNSUPPORTED, "Random sharding is not supported yet.");
1132+
default:
1133+
ythrow TFormatFail(Ydb::StatusIds::INTERNAL_ERROR, "Unsupported unit");
1134+
}
1135+
}
1136+
1137+
void TCreateTableFormatter::Format(const NKikimrSchemeOp::TColumnDataLifeCycle& ttlSettings) {
1138+
if (!ttlSettings.HasEnabled()) {
1139+
return;
1140+
}
1141+
1142+
const auto& enabled = ttlSettings.GetEnabled();
1143+
1144+
if (enabled.HasExpireAfterBytes()) {
1145+
ythrow TFormatFail(Ydb::StatusIds::UNSUPPORTED, "TTL by size is not supported.");
1146+
}
1147+
1148+
Stream << ",\n";
1149+
Stream << "\tTTL =\n\t ";
1150+
bool first = true;
1151+
1152+
if (!enabled.TiersSize()) {
1153+
Y_ENSURE(enabled.HasExpireAfterSeconds());
1154+
Format(enabled.GetExpireAfterSeconds());
1155+
} else {
1156+
for (const auto& tier : enabled.GetTiers()) {
1157+
if (!first) {
1158+
Stream << ", ";
1159+
}
1160+
switch (tier.GetActionCase()) {
1161+
case NKikimrSchemeOp::TTTLSettings::TTier::ActionCase::kDelete:
1162+
Format(tier.GetApplyAfterSeconds());
1163+
break;
1164+
case NKikimrSchemeOp::TTTLSettings::TTier::ActionCase::kEvictToExternalStorage:
1165+
Format(tier.GetApplyAfterSeconds(), tier.GetEvictToExternalStorage().GetStorage());
1166+
break;
1167+
case NKikimrSchemeOp::TTTLSettings::TTier::ActionCase::ACTION_NOT_SET:
1168+
ythrow TFormatFail(Ydb::StatusIds::UNSUPPORTED, "Undefined tier action");
1169+
}
1170+
first = false;
1171+
}
1172+
}
1173+
1174+
Stream << "\n\t ON " << enabled.GetColumnName();
1175+
switch (enabled.GetColumnUnit()) {
1176+
case NKikimrSchemeOp::TTTLSettings::UNIT_AUTO:
1177+
break;
1178+
case NKikimrSchemeOp::TTTLSettings::UNIT_SECONDS:
1179+
Stream << " AS SECONDS";
1180+
break;
1181+
case NKikimrSchemeOp::TTTLSettings::UNIT_MILLISECONDS:
1182+
Stream << " AS MILLISECONDS";
1183+
break;
1184+
case NKikimrSchemeOp::TTTLSettings::UNIT_MICROSECONDS:
1185+
Stream << " AS MICROSECONDS";
1186+
break;
1187+
case NKikimrSchemeOp::TTTLSettings::UNIT_NANOSECONDS:
1188+
Stream << " AS NANOSECONDS";
1189+
break;
1190+
default:
1191+
ythrow TFormatFail(Ydb::StatusIds::INTERNAL_ERROR, "Unsupported unit");
1192+
}
1193+
}
1194+
8871195
} // NSysView
8881196
} // NKikimr

ydb/core/sys_view/show_create/create_table_formatter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <ydb/core/protos/flat_scheme_op.pb.h>
66

7+
#include <ydb/core/tx/columnshard/engines/scheme/defaults/protos/data.pb.h>
8+
79
#include <ydb/public/api/protos/ydb_table.pb.h>
810

911
#include <yql/essentials/minikql/mkql_alloc.h>
@@ -74,6 +76,7 @@ class TCreateTableFormatter {
7476
}
7577

7678
TResult Format(const TString& tablePath, const NKikimrSchemeOp::TTableDescription& tableDesc, bool temporary);
79+
TResult Format(const TString& tablePath, const NKikimrSchemeOp::TColumnTableDescription& tableDesc, bool temporary);
7780

7881
private:
7982

@@ -88,6 +91,12 @@ class TCreateTableFormatter {
8891

8992
void Format(ui64 expireAfterSeconds, std::optional<TString> storage = std::nullopt);
9093

94+
void Format(const NKikimrSchemeOp::TOlapColumnDescription& olapColumnDesc);
95+
void Format(const NKikimrSchemeOp::TColumnTableSharding& tableSharding);
96+
void Format(const NKikimrSchemeOp::TColumnDataLifeCycle& ttlSettings);
97+
98+
void Format(const NKikimrColumnShardColumnDefaults::TColumnDefault& defaultValue);
99+
91100
void Format(const Ydb::TypedValue& value, bool isPartition = false);
92101
void FormatValue(NYdb::TValueParser& parser, bool isPartition = false, TString del = "");
93102
void FormatPrimitive(NYdb::TValueParser& parser);

0 commit comments

Comments
 (0)