1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
#include "value_consumer.h"
#include "helpers.h"
#include <yt/yt/client/table_client/name_table.h>
#include <yt/yt/client/table_client/row_buffer.h>
#include <yt/yt/client/table_client/schema.h>
#include <yt/yt/client/table_client/unversioned_writer.h>
#include <yt/yt/core/concurrency/scheduler.h>
#include <util/string/cast.h>
namespace NYT::NTableClient {
using namespace NConcurrency;
////////////////////////////////////////////////////////////////////////////////
bool ConvertToBooleanValue(TStringBuf stringValue)
{
if (stringValue == "true") {
return true;
} else if (stringValue == "false") {
return false;
} else {
THROW_ERROR_EXCEPTION("Unable to convert value to boolean")
<< TErrorAttribute("value", stringValue);
}
}
////////////////////////////////////////////////////////////////////////////////
TValueConsumerBase::TValueConsumerBase(
TTableSchemaPtr schema,
TTypeConversionConfigPtr typeConversionConfig)
: Schema_(std::move(schema))
, TypeConversionConfig_(std::move(typeConversionConfig))
{ }
void TValueConsumerBase::InitializeIdToTypeMapping()
{
const auto& nameTable = GetNameTable();
for (const auto& column : Schema_->Columns()) {
int id = nameTable->GetIdOrRegisterName(column.Name());
if (id >= static_cast<int>(NameTableIdToType_.size())) {
NameTableIdToType_.resize(id + 1, EValueType::Any);
}
NameTableIdToType_[id] = column.GetWireType();
}
}
template <typename T>
void TValueConsumerBase::ProcessIntegralValue(const TUnversionedValue& value, EValueType columnType)
{
auto integralValue = FromUnversionedValue<T>(value);
if (TypeConversionConfig_->EnableAllToStringConversion && columnType == EValueType::String) {
char buf[64];
char* end = buf + 64;
char* start = WriteDecIntToBufferBackwards(end, integralValue);
OnMyValue(MakeUnversionedStringValue(TStringBuf(start, end), value.Id));
} else if (TypeConversionConfig_->EnableIntegralToDoubleConversion && columnType == EValueType::Double) {
OnMyValue(MakeUnversionedDoubleValue(static_cast<double>(integralValue), value.Id));
} else {
OnMyValue(value);
}
}
void TValueConsumerBase::ProcessInt64Value(const TUnversionedValue& value, EValueType columnType)
{
if (TypeConversionConfig_->EnableIntegralTypeConversion && columnType == EValueType::Uint64) {
i64 integralValue = value.Data.Int64;
if (integralValue < 0) {
ThrowConversionException(
value,
columnType,
TError("Unable to convert negative int64 to uint64")
<< TErrorAttribute("value", integralValue));
} else {
OnMyValue(MakeUnversionedUint64Value(static_cast<ui64>(integralValue), value.Id));
}
} else {
ProcessIntegralValue<i64>(value, columnType);
}
}
void TValueConsumerBase::ProcessUint64Value(const TUnversionedValue& value, EValueType columnType)
{
if (TypeConversionConfig_->EnableIntegralTypeConversion && columnType == EValueType::Int64) {
ui64 integralValue = value.Data.Uint64;
if (integralValue > std::numeric_limits<i64>::max()) {
ThrowConversionException(
value,
columnType,
TError("Unable to convert uint64 to int64 as it leads to an overflow")
<< TErrorAttribute("value", integralValue));
} else {
OnMyValue(MakeUnversionedInt64Value(static_cast<i64>(integralValue), value.Id));
}
} else {
ProcessIntegralValue<ui64>(value, columnType);
}
}
void TValueConsumerBase::ProcessBooleanValue(const TUnversionedValue& value, EValueType columnType)
{
if (TypeConversionConfig_->EnableAllToStringConversion && columnType == EValueType::String) {
TStringBuf stringValue = value.Data.Boolean ? "true" : "false";
OnMyValue(MakeUnversionedStringValue(stringValue, value.Id));
} else {
OnMyValue(value);
}
}
void TValueConsumerBase::ProcessDoubleValue(const TUnversionedValue& value, EValueType columnType)
{
if (TypeConversionConfig_->EnableAllToStringConversion && columnType == EValueType::String) {
char buf[64];
auto length = FloatToString(value.Data.Double, buf, sizeof(buf));
TStringBuf stringValue(buf, buf + length);
OnMyValue(MakeUnversionedStringValue(stringValue, value.Id));
} else {
OnMyValue(value);
}
}
void TValueConsumerBase::ProcessStringValue(const TUnversionedValue& value, EValueType columnType)
{
if (TypeConversionConfig_->EnableStringToAllConversion) {
TUnversionedValue convertedValue;
TStringBuf stringValue(value.Data.String, value.Length);
try {
switch (columnType) {
case EValueType::Int64:
case EValueType::Uint64: {
auto adjustedStringValue = stringValue;
if (!stringValue.empty() && stringValue.back() == 'u') {
adjustedStringValue = TStringBuf(value.Data.String, value.Length - 1);
}
if (columnType == EValueType::Int64) {
convertedValue = MakeUnversionedInt64Value(FromString<i64>(adjustedStringValue), value.Id);
} else {
convertedValue = MakeUnversionedUint64Value(FromString<ui64>(adjustedStringValue), value.Id);
}
break;
}
case EValueType::Double:
convertedValue = MakeUnversionedDoubleValue(FromString<double>(stringValue), value.Id);
break;
case EValueType::Boolean:
convertedValue = MakeUnversionedBooleanValue(ConvertToBooleanValue(stringValue), value.Id);
break;
default:
convertedValue = value;
break;
}
} catch (const std::exception& ex) {
ThrowConversionException(value, columnType, TError(ex));
}
OnMyValue(convertedValue);
} else {
OnMyValue(value);
}
}
void TValueConsumerBase::OnValue(const TUnversionedValue& value)
{
EValueType columnType;
if (NameTableIdToType_.size() <= value.Id) {
columnType = EValueType::Any;
} else {
columnType = NameTableIdToType_[value.Id];
}
switch (value.Type) {
case EValueType::Int64:
ProcessInt64Value(value, columnType);
break;
case EValueType::Uint64:
ProcessUint64Value(value, columnType);
break;
case EValueType::Boolean:
ProcessBooleanValue(value, columnType);
break;
case EValueType::Double:
ProcessDoubleValue(value, columnType);
break;
case EValueType::String:
ProcessStringValue(value, columnType);
break;
default:
OnMyValue(value);
break;
}
}
const TTableSchemaPtr& TValueConsumerBase::GetSchema() const
{
return Schema_;
}
void TValueConsumerBase::ThrowConversionException(const TUnversionedValue& value, EValueType columnType, const TError& ex)
{
THROW_ERROR_EXCEPTION(EErrorCode::SchemaViolation, "Error while performing type conversion")
<< ex
<< TErrorAttribute("column", GetNameTable()->GetName(value.Id))
<< TErrorAttribute("value_type", value.Type)
<< TErrorAttribute("column_type", columnType);
}
////////////////////////////////////////////////////////////////////////////////
TBuildingValueConsumer::TBuildingValueConsumer(
TTableSchemaPtr schema,
NLogging::TLogger logger,
bool convertNullToEntity,
TTypeConversionConfigPtr typeConversionConfig)
: TValueConsumerBase(std::move(schema), std::move(typeConversionConfig))
, Logger(std::move(logger))
, NameTable_(TNameTable::FromSchema(*Schema_))
, ConvertNullToEntity_(convertNullToEntity)
, WrittenFlags_(NameTable_->GetSize())
{
InitializeIdToTypeMapping();
}
std::vector<TUnversionedRow> TBuildingValueConsumer::GetRows() const
{
std::vector<TUnversionedRow> result;
result.reserve(Rows_.size());
for (const auto& row : Rows_) {
result.push_back(row);
}
return result;
}
void TBuildingValueConsumer::SetAggregate(bool value)
{
Aggregate_ = value;
}
void TBuildingValueConsumer::SetTreatMissingAsNull(bool value)
{
TreatMissingAsNull_ = value;
}
void TBuildingValueConsumer::SetAllowMissingKeyColumns(bool value)
{
AllowMissingKeyColumns_ = value;
}
const TNameTablePtr& TBuildingValueConsumer::GetNameTable() const
{
return NameTable_;
}
bool TBuildingValueConsumer::GetAllowUnknownColumns() const
{
return false;
}
void TBuildingValueConsumer::OnBeginRow()
{
// Do nothing.
}
TUnversionedValue TBuildingValueConsumer::MakeAnyFromScalar(const TUnversionedValue& value)
{
NYson::TBufferedBinaryYsonWriter writer(&ValueBuffer_);
switch (value.Type) {
case EValueType::Int64:
writer.OnInt64Scalar(value.Data.Int64);
break;
case EValueType::Uint64:
writer.OnUint64Scalar(value.Data.Uint64);
break;
case EValueType::Double:
writer.OnDoubleScalar(value.Data.Double);
break;
case EValueType::Boolean:
writer.OnBooleanScalar(value.Data.Boolean);
break;
case EValueType::String:
writer.OnStringScalar(value.AsStringBuf());
break;
case EValueType::Null:
writer.OnEntity();
break;
default:
ThrowUnexpectedValueType(value.Type);
}
writer.Flush();
return MakeUnversionedAnyValue(
TStringBuf(
ValueBuffer_.Begin(),
ValueBuffer_.Begin() + ValueBuffer_.Size()),
value.Id,
value.Flags);
}
void TBuildingValueConsumer::OnMyValue(const TUnversionedValue& value)
{
if (value.Id >= Schema_->GetColumnCount()) {
return;
}
auto valueCopy = value;
const auto& columnSchema = Schema_->Columns()[valueCopy.Id];
if (columnSchema.Aggregate() && Aggregate_) {
valueCopy.Flags |= EValueFlags::Aggregate;
}
if (columnSchema.IsOfV1Type(ESimpleLogicalValueType::Any) &&
valueCopy.Type != EValueType::Any &&
(valueCopy.Type != EValueType::Null || ConvertNullToEntity_))
{
if (valueCopy.Type == EValueType::Null && LogNullToEntity_) {
YT_LOG_DEBUG("Detected conversion null to yson entity");
LogNullToEntity_ = false;
}
Builder_.AddValue(MakeAnyFromScalar(valueCopy));
ValueBuffer_.Clear();
} else {
Builder_.AddValue(valueCopy);
}
WrittenFlags_[valueCopy.Id] = true;
}
void TBuildingValueConsumer::OnEndRow()
{
for (int id = 0; id < std::ssize(WrittenFlags_); ++id) {
if (WrittenFlags_[id]) {
WrittenFlags_[id] = false;
} else if ((TreatMissingAsNull_ || id < Schema_->GetKeyColumnCount()) &&
!(AllowMissingKeyColumns_ && id < Schema_->GetKeyColumnCount()) &&
!Schema_->Columns()[id].Expression())
{
auto flags = EValueFlags::None;
if (Schema_->Columns()[id].Aggregate() && Aggregate_) {
flags |= EValueFlags::Aggregate;
}
Builder_.AddValue(MakeUnversionedSentinelValue(EValueType::Null, id, flags));
}
}
Rows_.emplace_back(Builder_.FinishRow());
}
////////////////////////////////////////////////////////////////////////////////
struct TWritingValueConsumerBufferTag
{ };
TWritingValueConsumer::TWritingValueConsumer(
IUnversionedWriterPtr writer,
TTypeConversionConfigPtr typeConversionConfig,
i64 maxRowBufferSize)
: TValueConsumerBase(writer->GetSchema(), std::move(typeConversionConfig))
, Writer_(std::move(writer))
, MaxRowBufferSize_(maxRowBufferSize)
, RowBuffer_(New<TRowBuffer>(TWritingValueConsumerBufferTag()))
{
YT_VERIFY(Writer_);
InitializeIdToTypeMapping();
}
TFuture<void> TWritingValueConsumer::Flush()
{
if (RowBuffer_->GetSize() == 0) {
return VoidFuture;
}
// We could have multiple value consumers writing into the same Writer.
// It means, that there could be multiple subscribers waiting to flush data on the same ready event.
// To make writing safe, we must double check that the writer is really ready, before flushing rows.
return
BIND([writer = Writer_, rowBuffer = RowBuffer_, rows = std::move(Rows_)] {
while (!writer->GetReadyEvent().IsSet() || !writer->GetReadyEvent().Get().IsOK()) {
WaitFor(writer->GetReadyEvent())
.ThrowOnError();
}
writer->Write(rows);
rowBuffer->Clear();
return writer->GetReadyEvent();
})
.AsyncVia(GetCurrentInvoker())
.Run();
}
const TNameTablePtr& TWritingValueConsumer::GetNameTable() const
{
return Writer_->GetNameTable();
}
bool TWritingValueConsumer::GetAllowUnknownColumns() const
{
return true;
}
void TWritingValueConsumer::OnBeginRow()
{
YT_ASSERT(Values_.empty());
}
void TWritingValueConsumer::OnMyValue(const TUnversionedValue& value)
{
Values_.push_back(RowBuffer_->CaptureValue(value));
}
void TWritingValueConsumer::OnEndRow()
{
auto row = RowBuffer_->CaptureRow(MakeRange(Values_), false);
Values_.clear();
Rows_.push_back(row);
if (RowBuffer_->GetSize() >= MaxRowBufferSize_) {
auto error = WaitFor(Flush());
THROW_ERROR_EXCEPTION_IF_FAILED(error, "Table writer failed")
}
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT::NTableClient
|