Skip to content

Commit e5e082e

Browse files
authored
Merge 5a909e6 into ed91832
2 parents ed91832 + 5a909e6 commit e5e082e

File tree

4 files changed

+169
-4
lines changed

4 files changed

+169
-4
lines changed

ydb/core/tx/schemeshard/ut_export/ut_export.cpp

+64-2
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,42 @@ Y_UNIT_TEST_SUITE(TExportToS3Tests) {
281281

282282
const TVector<TString> tables = {R"(
283283
Name: "Table"
284-
Columns { Name: "key" Type: "Utf8" }
285-
Columns { Name: "value" Type: "Utf8" }
284+
Columns {
285+
Name: "key"
286+
Type: "Utf8"
287+
DefaultFromLiteral {
288+
type {
289+
optional_type {
290+
item {
291+
type_id: UTF8
292+
}
293+
}
294+
}
295+
value {
296+
items {
297+
text_value: "b"
298+
}
299+
}
300+
}
301+
}
302+
Columns {
303+
Name: "value"
304+
Type: "Utf8"
305+
DefaultFromLiteral {
306+
type {
307+
optional_type {
308+
item {
309+
type_id: UTF8
310+
}
311+
}
312+
}
313+
value {
314+
items {
315+
text_value: "a"
316+
}
317+
}
318+
}
319+
}
286320
KeyColumnNames: ["key"]
287321
PartitionConfig {
288322
ColumnFamilies {
@@ -331,6 +365,20 @@ Y_UNIT_TEST_SUITE(TExportToS3Tests) {
331365
}
332366
}
333367
not_null: false
368+
from_literal {
369+
type {
370+
optional_type {
371+
item {
372+
type_id: UTF8
373+
}
374+
}
375+
}
376+
value {
377+
items {
378+
text_value: "b"
379+
}
380+
}
381+
}
334382
}
335383
columns {
336384
name: "value"
@@ -342,6 +390,20 @@ columns {
342390
}
343391
}
344392
not_null: false
393+
from_literal {
394+
type {
395+
optional_type {
396+
item {
397+
type_id: UTF8
398+
}
399+
}
400+
}
401+
value {
402+
items {
403+
text_value: "a"
404+
}
405+
}
406+
}
345407
}
346408
primary_key: "key"
347409
storage_settings {

ydb/core/tx/schemeshard/ut_restore/ut_restore.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <ydb/core/tx/schemeshard/schemeshard_private.h>
1010
#include <ydb/core/tx/schemeshard/schemeshard_billing_helpers.h>
1111
#include <ydb/core/tx/datashard/datashard.h>
12+
#include <ydb/core/tx/datashard/datashard_ut_common_kqp.h>
1213
#include <ydb/core/wrappers/ut_helpers/s3_mock.h>
1314
#include <ydb/core/metering/metering.h>
1415
#include <ydb/core/ydb_convert/table_description.h>
@@ -373,6 +374,47 @@ Y_UNIT_TEST_SUITE(TRestoreTests) {
373374
NKqp::CompareYson(data.YsonStr, content);
374375
}
375376

377+
Y_UNIT_TEST_WITH_COMPRESSION(ShouldSucceedWithDefaultFromLiteral) {
378+
TTestBasicRuntime runtime;
379+
380+
const auto data = GenerateTestData(Codec, "a", 1);
381+
382+
Restore(runtime, R"(
383+
Name: "Table"
384+
Columns { Name: "key" Type: "Utf8" }
385+
Columns {
386+
Name: "value"
387+
Type: "Utf8"
388+
DefaultFromLiteral {
389+
type {
390+
optional_type {
391+
item {
392+
type_id: UTF8
393+
}
394+
}
395+
}
396+
value {
397+
items {
398+
text_value: "value1"
399+
}
400+
}
401+
}
402+
}
403+
KeyColumnNames: ["key"]
404+
)", {data});
405+
406+
{
407+
TString result = NKikimr::NDataShard::NKqpHelpers::KqpSimpleExec(
408+
runtime, Q_(R"("UPSERT INTO `/MyRoot/Table` (key) VALUES ("a"))"));
409+
UNIT_ASSERT_VALUES_EQUAL(result, "<empty>");
410+
}
411+
412+
{
413+
TString result = NKikimr::NDataShard::NKqpHelpers::KqpSimpleExec(runtime, "SELECT * FROM `/MyRoot/Table`;");
414+
UNIT_ASSERT_C(false, result);
415+
}
416+
}
417+
376418
Y_UNIT_TEST_WITH_COMPRESSION(ShouldSucceedOnMultiShardTable) {
377419
TTestBasicRuntime runtime;
378420

ydb/core/ydb_convert/table_description.cpp

+59-2
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ bool BuildAlterTableAddIndexRequest(const Ydb::Table::AlterTableRequest* req, NK
151151
if (flags & NKqpProto::TKqpSchemeOperation::FLAG_PG_MODE) {
152152
settings->set_pg_mode(true);
153153
}
154-
154+
155155
if (flags & NKqpProto::TKqpSchemeOperation::FLAG_IF_NOT_EXISTS) {
156156
settings->set_if_not_exist(true);
157157
}
158-
158+
159159
settings->set_source_path(req->path());
160160
auto tableIndex = settings->mutable_index();
161161
tableIndex->CopyFrom(req->add_indexes(0));
@@ -390,6 +390,54 @@ static Ydb::Type* AddColumn(Ydb::Table::ColumnMeta* newColumn, const TColumn& co
390390
}
391391
}
392392
newColumn->set_not_null(column.GetNotNull());
393+
return columnType;
394+
}
395+
396+
template <>
397+
Ydb::Type* AddColumn<NKikimrSchemeOp::TColumnDescription>(Ydb::Table::ColumnMeta* newColumn, const NKikimrSchemeOp::TColumnDescription& column) {
398+
newColumn->set_name(column.GetName());
399+
400+
Ydb::Type* columnType = nullptr;
401+
auto* typeDesc = NPg::TypeDescFromPgTypeName(column.GetType());
402+
if (typeDesc) {
403+
columnType = newColumn->mutable_type();
404+
auto* pg = columnType->mutable_pg_type();
405+
pg->set_type_name(NPg::PgTypeNameFromTypeDesc(typeDesc));
406+
pg->set_type_modifier(NPg::TypeModFromPgTypeName(column.GetType()));
407+
pg->set_oid(NPg::PgTypeIdFromTypeDesc(typeDesc));
408+
pg->set_typlen(0);
409+
pg->set_typmod(0);
410+
} else {
411+
NYql::NProto::TypeIds protoType;
412+
if (!NYql::NProto::TypeIds_Parse(column.GetType(), &protoType)) {
413+
throw NYql::TErrorException(NKikimrIssues::TIssuesIds::DEFAULT_ERROR)
414+
<< "Got invalid type: " << column.GetType() << " for column: " << column.GetName();
415+
}
416+
417+
if (column.GetNotNull()) {
418+
columnType = newColumn->mutable_type();
419+
} else {
420+
columnType = newColumn->mutable_type()->mutable_optional_type()->mutable_item();
421+
}
422+
Y_ENSURE(columnType);
423+
if (protoType == NYql::NProto::TypeIds::Decimal) {
424+
auto typeParams = columnType->mutable_decimal_type();
425+
// TODO: Change TEvDescribeSchemeResult to return decimal params
426+
typeParams->set_precision(22);
427+
typeParams->set_scale(9);
428+
} else {
429+
NMiniKQL::ExportPrimitiveTypeToProto(protoType, *columnType);
430+
}
431+
}
432+
newColumn->set_not_null(column.GetNotNull());
433+
switch (column.GetDefaultValueCase()) {
434+
case NKikimrSchemeOp::TColumnDescription::kDefaultFromLiteral: {
435+
auto fromLiteral = newColumn->mutable_from_literal();
436+
*fromLiteral = column.GetDefaultFromLiteral();
437+
break;
438+
}
439+
default: break;
440+
}
393441

394442
return columnType;
395443
}
@@ -607,6 +655,15 @@ bool FillColumnDescription(NKikimrSchemeOp::TTableDescription& out,
607655
if (!column.family().empty()) {
608656
cd->SetFamilyName(column.family());
609657
}
658+
659+
switch (column.default_policy_case()) {
660+
case Ydb::Table::ColumnMeta::kFromLiteral: {
661+
auto fromLiteral = cd->MutableDefaultFromLiteral();
662+
*fromLiteral = column.from_literal();
663+
break;
664+
}
665+
default: break;
666+
}
610667
}
611668

612669
return true;

ydb/public/api/protos/ydb_table.proto

+4
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ message ColumnMeta {
342342
string family = 3;
343343
// Column nullability
344344
optional bool not_null = 4;
345+
// Default value policy
346+
oneof default_policy {
347+
TypedValue from_literal = 6;
348+
}
345349
}
346350

347351
message DateTypeColumnModeSettings {

0 commit comments

Comments
 (0)