Skip to content

Commit 79ec684

Browse files
committed
Fixed bugs
1 parent 68ff3ec commit 79ec684

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

ydb/public/lib/ydb_cli/commands/ydb_service_import.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ int TCommandImportFromCsv::Run(TConfig& config) {
244244
settings.Header(Header);
245245
settings.NewlineDelimited(NewlineDelimited);
246246
settings.HeaderRow(HeaderRow);
247-
settings.NullValue(NullValue);
247+
if (config.ParseResult->Has("null-value")) {
248+
settings.NullValue(NullValue);
249+
}
248250

249251
if (Delimiter.size() != 1) {
250252
throw TMisuseException()

ydb/public/lib/ydb_cli/common/csv_parser.cpp

+28-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace {
1010

1111
class TCsvToYdbConverter {
1212
public:
13-
explicit TCsvToYdbConverter(TTypeParser& parser, const TString& nullValue)
13+
explicit TCsvToYdbConverter(TTypeParser& parser, const std::optional<TString>& nullValue)
1414
: Parser(parser)
1515
, NullValue(nullValue)
1616
{
@@ -106,15 +106,30 @@ class TCsvToYdbConverter {
106106
case EPrimitiveType::DyNumber:
107107
Builder.DyNumber(token);
108108
break;
109-
case EPrimitiveType::Date:
110-
Builder.Date(TInstant::Days(GetArithmetic<ui16>(token)));
109+
case EPrimitiveType::Date: {
110+
TInstant date;
111+
if (!TInstant::TryParseIso8601(token, date)) {
112+
date = TInstant::Days(GetArithmetic<ui16>(token));
113+
}
114+
Builder.Date(date);
111115
break;
112-
case EPrimitiveType::Datetime:
113-
Builder.Datetime(TInstant::Seconds(GetArithmetic<ui32>(token)));
116+
}
117+
case EPrimitiveType::Datetime: {
118+
TInstant datetime;
119+
if (!TInstant::TryParseIso8601(token, datetime)) {
120+
datetime = TInstant::Seconds(GetArithmetic<ui32>(token));
121+
}
122+
Builder.Datetime(datetime);
114123
break;
115-
case EPrimitiveType::Timestamp:
116-
Builder.Timestamp(TInstant::MicroSeconds(GetArithmetic<ui64>(token)));
124+
}
125+
case EPrimitiveType::Timestamp: {
126+
TInstant timestamp;
127+
if (!TInstant::TryParseIso8601(token, timestamp)) {
128+
timestamp = TInstant::MicroSeconds(GetArithmetic<ui64>(token));
129+
}
130+
Builder.Timestamp(timestamp);
117131
break;
132+
}
118133
case EPrimitiveType::Interval:
119134
Builder.Interval(GetArithmetic<i64>(token));
120135
break;
@@ -144,7 +159,7 @@ class TCsvToYdbConverter {
144159
}
145160
case TTypeParser::ETypeKind::Optional: {
146161
Parser.OpenOptional();
147-
if (token == NullValue) {
162+
if (NullValue && token == NullValue) {
148163
Builder.EmptyOptional(GetType());
149164
} else {
150165
Builder.BeginOptional();
@@ -247,13 +262,13 @@ class TCsvToYdbConverter {
247262

248263
private:
249264
TTypeParser& Parser;
250-
const TString NullValue = "";
265+
const std::optional<TString> NullValue = "";
251266
TValueBuilder Builder;
252267
};
253268

254269
}
255270

256-
TCsvParser::TCsvParser(TString&& headerRow, const char delimeter, const TString& nullValue,
271+
TCsvParser::TCsvParser(TString&& headerRow, const char delimeter, const std::optional<TString>& nullValue,
257272
const std::map<TString, TType>* paramTypes,
258273
const std::map<TString, TString>* paramSources)
259274
: HeaderRow(std::move(headerRow))
@@ -266,7 +281,7 @@ TCsvParser::TCsvParser(TString&& headerRow, const char delimeter, const TString&
266281
Header = static_cast<TVector<TString>>(splitter);
267282
}
268283

269-
TCsvParser::TCsvParser(TVector<TString>&& header, const char delimeter, const TString& nullValue,
284+
TCsvParser::TCsvParser(TVector<TString>&& header, const char delimeter, const std::optional<TString>& nullValue,
270285
const std::map<TString, TType>* paramTypes,
271286
const std::map<TString, TString>* paramSources)
272287
: Header(std::move(header))
@@ -333,7 +348,7 @@ void TCsvParser::GetValue(TString&& data, TValueBuilder& builder, const TType& t
333348
parser.OpenStruct();
334349
while (parser.TryNextMember()) {
335350
TString name = parser.GetMemberName();
336-
if (name == NullValue) {
351+
if (name == "__ydb_skip_column_name") {
337352
continue;
338353
}
339354
auto fieldIt = fields.find(name);
@@ -353,7 +368,7 @@ TType TCsvParser::GetColumnsType() const {
353368
if (ParamTypes->find(colName) != ParamTypes->end()) {
354369
builder.AddMember(colName, ParamTypes->at(colName));
355370
} else {
356-
builder.AddMember(NullValue, TTypeBuilder().Build());
371+
builder.AddMember("__ydb_skip_column_name", TTypeBuilder().Build());
357372
}
358373
}
359374
builder.EndStruct();

ydb/public/lib/ydb_cli/common/csv_parser.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class TCsvParser {
1717
TCsvParser& operator=(TCsvParser&&) = default;
1818
~TCsvParser() = default;
1919

20-
TCsvParser(TString&& headerRow, const char delimeter, const TString& nullValue,
20+
TCsvParser(TString&& headerRow, const char delimeter, const std::optional<TString>& nullValue,
2121
const std::map<TString, TType>* paramTypes = nullptr,
2222
const std::map<TString, TString>* paramSources = nullptr);
23-
TCsvParser(TVector<TString>&& header, const char delimeter, const TString& nullValue,
23+
TCsvParser(TVector<TString>&& header, const char delimeter, const std::optional<TString>& nullValue,
2424
const std::map<TString, TType>* paramTypes = nullptr,
2525
const std::map<TString, TString>* paramSources = nullptr);
2626

@@ -34,7 +34,7 @@ class TCsvParser {
3434
TVector<TString> Header;
3535
TString HeaderRow;
3636
char Delimeter;
37-
TString NullValue;
37+
std::optional<TString> NullValue;
3838
const std::map<TString, TType>* ParamTypes;
3939
const std::map<TString, TString>* ParamSources;
4040
};

ydb/public/lib/ydb_cli/common/csv_parser_ut.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ Y_UNIT_TEST_SUITE(YdbCliCsvParserTests) {
7373
}
7474

7575
Y_UNIT_TEST(DateTypesTestParams) {
76+
CommonTestParams("name", "\"2001-01-01\"", {{"$name", TValueBuilder().Date(TInstant::ParseIso8601("2001-01-01")).Build()}});
77+
CommonTestParams("name", "\"2001-01-01T12:12:12\"", {{"$name", TValueBuilder().Datetime(TInstant::ParseIso8601("2001-01-01T12:12:12")).Build()}});
78+
CommonTestParams("name", "\"2001-01-01T12:12:12.111111\"", {{"$name", TValueBuilder().Timestamp(TInstant::ParseIso8601("2001-01-01T12:12:12.111111")).Build()}});
7679
CommonTestParams("name", "12000", {{"$name", TValueBuilder().Date(TInstant::Days(12000)).Build()}});
7780
CommonTestParams("name", "1200000", {{"$name", TValueBuilder().Datetime(TInstant::Seconds(1200000)).Build()}});
7881
CommonTestParams("name", "120000000", {{"$name", TValueBuilder().Timestamp(TInstant::MicroSeconds(120000000)).Build()}});
@@ -83,6 +86,9 @@ Y_UNIT_TEST_SUITE(YdbCliCsvParserTests) {
8386
}
8487

8588
Y_UNIT_TEST(DateTypesTestValue) {
89+
CommonTestValue("name", "\"2001-01-01\"", MakeStruct("name", TValueBuilder().Date(TInstant::ParseIso8601("2001-01-01")).Build()));
90+
CommonTestValue("name", "\"2001-01-01T12:12:12\"", MakeStruct("name", TValueBuilder().Datetime(TInstant::ParseIso8601("2001-01-01T12:12:12")).Build()));
91+
CommonTestValue("name", "\"2001-01-01T12:12:12.111111\"", MakeStruct("name", TValueBuilder().Timestamp(TInstant::ParseIso8601("2001-01-01T12:12:12.111111")).Build()));
8692
CommonTestValue("name", "12000", MakeStruct("name", TValueBuilder().Date(TInstant::Days(12000)).Build()));
8793
CommonTestValue("name", "1200000", MakeStruct("name", TValueBuilder().Datetime(TInstant::Seconds(1200000)).Build()));
8894
CommonTestValue("name", "120000000", MakeStruct("name", TValueBuilder().Timestamp(TInstant::MicroSeconds(120000000)).Build()));

ydb/public/lib/ydb_cli/import/import.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct TImportFileSettings : public TOperationRequestSettings<TImportFileSetting
5151
FLUENT_SETTING_DEFAULT(bool, NewlineDelimited, false);
5252
FLUENT_SETTING_DEFAULT(TString, HeaderRow, "");
5353
FLUENT_SETTING_DEFAULT(TString, Delimiter, DefaultDelimiter);
54-
FLUENT_SETTING_DEFAULT(TString, NullValue, "");
54+
FLUENT_SETTING_DEFAULT(std::optional<TString>, NullValue, std::nullopt);
5555
};
5656

5757
class TImportFileClient {

0 commit comments

Comments
 (0)