Skip to content

Commit b14cfc5

Browse files
committed
perf(bq_driver): optimize ARRAY and JSON data fetch throughput
1 parent 643c918 commit b14cfc5

4 files changed

Lines changed: 130 additions & 58 deletions

File tree

‎google/cloud/odbc/bq_driver/internal/data_translation.cc‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ odbc_internal::StatusRecord ConvertFromDateDSValue(DSValue const& src_dsval,
13991399
return status_record;
14001400
}
14011401

1402-
StatusRecord ConvertStringToJsonOutputBuffer(std::string const& src_str,
1402+
StatusRecord ConvertStringToJsonOutputBuffer(std::string_view src_str,
14031403
DataBuffer& dest_data) {
14041404
SQLSMALLINT dest_type = dest_data.type;
14051405
SQLPOINTER dest_buf = dest_data.buf;
@@ -1408,7 +1408,7 @@ StatusRecord ConvertStringToJsonOutputBuffer(std::string const& src_str,
14081408

14091409
switch (dest_type) {
14101410
case SQL_C_CHAR: {
1411-
return StringValueToOutputBufferResponse(src_str.c_str(), dest_data);
1411+
return StringValueToOutputBufferResponse(src_str, dest_data);
14121412
}
14131413
case SQL_C_WCHAR: {
14141414
StatusRecordOr<std::wstring> wide_string = Utf8ToUtf16(src_str);
@@ -1426,8 +1426,8 @@ StatusRecord ConvertStringToJsonOutputBuffer(std::string const& src_str,
14261426
reinterpret_cast<SQLLEN*>(res_len));
14271427
}
14281428
case SQL_C_BINARY: {
1429-
return StringValueToOutputBufferResponse<SQLLEN>(
1430-
src_str.c_str(), dest_buf, buffer_length, res_len);
1429+
return StringValueToOutputBufferResponse<SQLLEN>(src_str, dest_buf,
1430+
buffer_length, res_len);
14311431
}
14321432
default: {
14331433
LOG(ERROR) << "ConvertStringToJsonOutputBuffer:: Conversion is "
@@ -1441,9 +1441,8 @@ StatusRecord ConvertStringToJsonOutputBuffer(std::string const& src_str,
14411441

14421442
StatusRecord ConvertFromJsonDSValue(DSValue const& src_dsval,
14431443
DataBuffer& dest_data) {
1444-
std::string src_str;
1445-
DSValueToString(src_dsval, src_str);
1446-
return ConvertStringToJsonOutputBuffer(src_str, dest_data);
1444+
std::string_view src_view(src_dsval.data(), src_dsval.size());
1445+
return ConvertStringToJsonOutputBuffer(src_view, dest_data);
14471446
}
14481447

14491448
StatusRecord ConvertFromStructDSValue(DSValue const& src_dsval,
@@ -1468,17 +1467,16 @@ StatusRecord ConvertFromStructDSValue(DSValue const& src_dsval,
14681467

14691468
StatusRecord ConvertFromArrayDSValue(DSValue const& src_dsval,
14701469
DataBuffer& dest_data) {
1471-
std::string src_str;
1472-
DSValueToString(src_dsval, src_str);
1470+
std::string_view src_view(src_dsval.data(), src_dsval.size());
14731471

14741472
StatusRecord status_record = StatusRecord::Ok();
14751473

14761474
switch (dest_data.type) {
14771475
case SQL_C_CHAR: {
1478-
return StringValueToOutputBufferResponse(src_str.c_str(), dest_data);
1476+
return StringValueToOutputBufferResponse(src_view, dest_data);
14791477
}
14801478
case SQL_C_WCHAR: {
1481-
StatusRecordOr<std::wstring> wide_string = Utf8ToUtf16(src_str);
1479+
StatusRecordOr<std::wstring> wide_string = Utf8ToUtf16(src_view);
14821480
if (!wide_string.Ok()) {
14831481
return StatusRecord{SQLStates::k_HY000(), "Conversion Failed"};
14841482
}
@@ -1490,17 +1488,17 @@ StatusRecord ConvertFromArrayDSValue(DSValue const& src_dsval,
14901488
reinterpret_cast<SQLLEN*>(dest_data.result_len));
14911489
}
14921490
case SQL_C_BINARY: {
1493-
if (dest_data.buflen < src_str.length()) {
1494-
std::memcpy(dest_data.buf, src_str.c_str(), dest_data.buflen - 1);
1491+
if (dest_data.buflen < static_cast<SQLLEN>(src_view.length())) {
1492+
std::memcpy(dest_data.buf, src_view.data(), dest_data.buflen - 1);
14951493
if (dest_data.result_len) {
14961494
*dest_data.result_len = dest_data.buflen - 1;
14971495
}
14981496
return StatusRecord{SQLStates::k_01004(),
14991497
"Binary data, right truncated"};
15001498
}
1501-
std::memcpy(dest_data.buf, src_str.c_str(), src_str.length());
1499+
std::memcpy(dest_data.buf, src_view.data(), src_view.length());
15021500
if (dest_data.result_len) {
1503-
*dest_data.result_len = src_str.length();
1501+
*dest_data.result_len = src_view.length();
15041502
}
15051503
break;
15061504
}

‎google/cloud/odbc/bq_driver/internal/odbc_internal_commons.cc‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -562,16 +562,18 @@ StatusRecordOr<ResultSet> ProcessResultSetRows(
562562
result_set.row_schema.emplace_back(col_schema);
563563
}
564564
// Populate the data for each row.
565+
result_set.rows.reserve(rows.size());
565566
for (auto const& row : rows) {
566567
DSRow rs_row;
568+
rs_row.reserve(row.columns.size());
567569
int i = 0;
568570
for (auto const& col : row.columns) {
569571
BQDataType col_type;
570572
if (result_set.row_schema[i].is_mode_repeated)
571573
col_type = kArray;
572574
else
573575
col_type = result_set.row_schema[i].col_type;
574-
std::string data = col.value;
576+
std::string const& data = col.value;
575577
if (col.is_null) {
576578
rs_row.emplace_back(kNullValue);
577579
} else if (!data.empty()) {
@@ -662,10 +664,14 @@ StatusRecordOr<ResultSet> ProcessResultSetRows(
662664
}
663665
case BQDataType::kBool: {
664666
bool bool_val = false;
665-
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
666-
if (data == "1" || data == "true" || data == "yes") {
667+
std::string lower_data = data;
668+
std::transform(lower_data.begin(), lower_data.end(),
669+
lower_data.begin(), ::tolower);
670+
if (lower_data == "1" || lower_data == "true" ||
671+
lower_data == "yes") {
667672
bool_val = true;
668-
} else if (data == "0" || data == "false" || data == "no") {
673+
} else if (lower_data == "0" || lower_data == "false" ||
674+
lower_data == "no") {
669675
bool_val = false;
670676
}
671677
BooleanToDSValue(bool_val, row_val);
@@ -681,15 +687,15 @@ StatusRecordOr<ResultSet> ProcessResultSetRows(
681687
"Invalid or unsupported col BQ data type"};
682688
}
683689
}
684-
rs_row.emplace_back(row_val);
690+
rs_row.emplace_back(std::move(row_val));
685691
} else {
686692
DSValue empty_value;
687693
StringToDSValue("", empty_value);
688-
rs_row.emplace_back(empty_value);
694+
rs_row.emplace_back(std::move(empty_value));
689695
}
690696
i++;
691697
}
692-
result_set.rows.emplace_back(rs_row);
698+
result_set.rows.emplace_back(std::move(rs_row));
693699
}
694700
return result_set;
695701
}

‎google/cloud/odbc/bq_driver/internal/odbc_internal_commons.h‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <cstring>
3434
#include <sstream>
3535
#include <string>
36+
#include <string_view>
3637
#include <vector>
3738

3839
#ifdef _WIN32
@@ -207,9 +208,11 @@ inline bool IsDSValueNull(DSValue const& value) {
207208
// getting scale ,precision, sign and the value from sting parameter
208209
odbc_internal::StatusRecord GetNumericDetailsFromStr(
209210
std::string const& src_dsval, SQL_NUMERIC_STRUCT& numst);
210-
inline void StringToDSValue(std::string const& str, DSValue& value) {
211+
inline void StringToDSValue(std::string_view str, DSValue& value) {
211212
value.resize(str.size());
212-
std::copy(str.begin(), str.end(), value.begin());
213+
if (!str.empty()) {
214+
std::memcpy(value.data(), str.data(), str.size());
215+
}
213216
}
214217

215218
// Func to decode a Base64-encoded string into a vector of bytes.
@@ -457,8 +460,6 @@ inline void ArrayJsonToDSValue(std::string const& str, DSValue& value,
457460
BQDataType array_type) {
458461
nlohmann::json json_data = nlohmann::json::parse(str);
459462
std::string str_data;
460-
nlohmann::json obj;
461-
obj["v"] = json_data;
462463

463464
switch (array_type) {
464465
case BQDataType::kString:
@@ -474,34 +475,40 @@ inline void ArrayJsonToDSValue(std::string const& str, DSValue& value,
474475
case BQDataType::kJson:
475476
case BQDataType::kFloat64: {
476477
std::vector<std::string> result;
477-
for (auto const& item : obj["v"]) {
478+
if (json_data.is_array()) {
479+
result.reserve(json_data.size());
480+
}
481+
for (auto& item : json_data) {
478482
if (item.contains("v") && item["v"].is_string()) {
479-
result.emplace_back(item["v"].get<std::string>());
483+
result.emplace_back(std::move(item["v"].get_ref<std::string&>()));
480484
}
481485
}
482486
str_data = nlohmann::json(result).dump();
483487
break;
484488
}
485489
case BQDataType::kStruct: {
486490
nlohmann::json str_result = nlohmann::json::array();
487-
for (auto& item : obj["v"]) {
491+
for (auto& item : json_data) {
488492
std::string val = item["v"]["f"][0]["v"].get<std::string>();
489493
nlohmann::json obj;
490-
obj["value"] = val;
491-
str_result.emplace_back(obj);
494+
obj["value"] = std::move(val);
495+
str_result.emplace_back(std::move(obj));
492496
}
493497
str_data = str_result.dump();
494498
break;
495499
}
496500
case BQDataType::kInt64: {
497501
std::vector<std::string> result;
498-
for (auto& item : obj["v"]) {
502+
if (json_data.is_array()) {
503+
result.reserve(json_data.size());
504+
}
505+
for (auto& item : json_data) {
499506
if (item.contains("v") && !item["v"].is_null()) {
500-
try {
501-
result.emplace_back(std::to_string(item["v"].get<int64_t>()));
502-
} catch (nlohmann::json::exception const& e) {
507+
if (item["v"].is_string()) {
508+
result.emplace_back(std::move(item["v"].get_ref<std::string&>()));
509+
} else {
503510
try {
504-
result.emplace_back(item["v"].get<std::string>());
511+
result.emplace_back(std::to_string(item["v"].get<int64_t>()));
505512
} catch (...) {
506513
result.emplace_back("0");
507514
}
@@ -515,15 +522,18 @@ inline void ArrayJsonToDSValue(std::string const& str, DSValue& value,
515522
}
516523
case BQDataType::kTimeStamp: {
517524
std::vector<std::string> result;
518-
for (auto const& item : obj["v"]) {
525+
if (json_data.is_array()) {
526+
result.reserve(json_data.size());
527+
}
528+
for (auto const& item : json_data) {
519529
if (item.is_object() && item.contains("v") && item["v"].is_string()) {
520530
double unix_timestamp = std::stod(item["v"].get<std::string>());
521531
SQL_TIMESTAMP_STRUCT timestamp_struct;
522532
auto status_record = ConvertUnixTimestampToTimestampStruct(
523533
unix_timestamp, timestamp_struct);
524534
if (status_record.ok()) {
525535
auto timestamp_string = FormatTimestampToString(timestamp_struct);
526-
result.emplace_back(timestamp_string);
536+
result.emplace_back(std::move(timestamp_string));
527537
} else {
528538
result.emplace_back("NULL");
529539
}
@@ -537,8 +547,7 @@ inline void ArrayJsonToDSValue(std::string const& str, DSValue& value,
537547
break;
538548
}
539549
}
540-
value.resize(str_data.size());
541-
std::copy(str_data.begin(), str_data.end(), value.begin());
550+
StringToDSValue(str_data, value);
542551
}
543552

544553
// This is the result populated by performing a bq query API.

‎google/cloud/odbc/bq_driver/internal/odbc_sql_execute_utils.cc‎

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ StatusRecord ProcessRecordBatch(
509509
if (str_arr->IsNull(row)) {
510510
result_set.rows[row][col_i] = kNullValue;
511511
} else {
512-
StringToDSValue(str_arr->GetString(row),
512+
auto view = str_arr->GetView(row);
513+
StringToDSValue(std::string_view(view.data(), view.size()),
513514
result_set.rows[row][col_i]);
514515
}
515516
}
@@ -532,7 +533,8 @@ StatusRecord ProcessRecordBatch(
532533
if (bin_arr->IsNull(row)) {
533534
result_set.rows[row][col_i] = kNullValue;
534535
} else {
535-
StringToDSValue(bin_arr->GetString(row),
536+
auto view = bin_arr->GetView(row);
537+
StringToDSValue(std::string_view(view.data(), view.size()),
536538
result_set.rows[row][col_i]);
537539
}
538540
}
@@ -733,6 +735,78 @@ StatusRecord ProcessRecordBatch(
733735
}
734736
break;
735737
}
738+
case arrow::Type::LIST: {
739+
auto list_arr = std::static_pointer_cast<arrow::ListArray>(column);
740+
auto values = list_arr->values();
741+
if (list_arr->value_type()->id() == arrow::Type::STRING) {
742+
auto str_values =
743+
std::static_pointer_cast<arrow::StringArray>(values);
744+
for (int64_t row = 0; row < num_rows; ++row) {
745+
if (list_arr->IsNull(row)) {
746+
result_set.rows[row][col_i] = kNullValue;
747+
} else {
748+
int32_t start = list_arr->value_offset(row);
749+
int32_t end = list_arr->value_offset(row + 1);
750+
size_t total_len = 2;
751+
for (int32_t idx = start; idx < end; ++idx) {
752+
if (idx > start) total_len += 2;
753+
total_len +=
754+
str_values->IsNull(idx) ? 4 : str_values->value_length(idx);
755+
}
756+
DSValue& row_val = result_set.rows[row][col_i];
757+
row_val.resize(total_len);
758+
char* out = row_val.data();
759+
*out++ = '[';
760+
for (int32_t idx = start; idx < end; ++idx) {
761+
if (idx > start) {
762+
*out++ = ',';
763+
*out++ = ' ';
764+
}
765+
if (str_values->IsNull(idx)) {
766+
std::memcpy(out, "null", 4);
767+
out += 4;
768+
} else {
769+
auto view = str_values->GetView(idx);
770+
if (!view.empty()) {
771+
std::memcpy(out, view.data(), view.size());
772+
out += view.size();
773+
}
774+
}
775+
}
776+
*out = ']';
777+
}
778+
}
779+
} else {
780+
std::string buf;
781+
for (int64_t row = 0; row < num_rows; ++row) {
782+
if (list_arr->IsNull(row)) {
783+
result_set.rows[row][col_i] = kNullValue;
784+
} else {
785+
int32_t start = list_arr->value_offset(row);
786+
int32_t end = list_arr->value_offset(row + 1);
787+
buf.clear();
788+
buf.push_back('[');
789+
for (int32_t idx = start; idx < end; ++idx) {
790+
if (idx > start) buf.append(", ");
791+
if (values->IsNull(idx)) {
792+
buf.append("null");
793+
} else {
794+
auto scalar_res = values->GetScalar(idx);
795+
if (!scalar_res.ok()) {
796+
return StatusRecord{
797+
SQLStates::k_HY000(),
798+
"Internal Error: Unable to parse scalar"};
799+
}
800+
buf.append(scalar_res.ValueOrDie()->ToString());
801+
}
802+
}
803+
buf.push_back(']');
804+
StringToDSValue(buf, result_set.rows[row][col_i]);
805+
}
806+
}
807+
}
808+
break;
809+
}
736810
// For complex types, we fall back to the existing logic but apply it
737811
// column-wise. We still avoid the GetScalar() overhead where possible,
738812
// but use ToString() to maintain compatibility with the existing parsing
@@ -755,22 +829,7 @@ StatusRecord ProcessRecordBatch(
755829
std::string data = scalar_res.ValueOrDie()->ToString();
756830

757831
DSValue& row_val = result_set.rows[row][col_i];
758-
759-
switch (type_id) {
760-
case arrow::Type::LIST: {
761-
if (data.rfind("list<", 0) == 0) {
762-
auto pos = data.find('[');
763-
if (pos != std::string::npos) data = data.substr(pos);
764-
}
765-
StringToDSValue(data, row_val);
766-
break;
767-
}
768-
769-
default: {
770-
StringToDSValue(data, row_val);
771-
break;
772-
}
773-
}
832+
StringToDSValue(data, row_val);
774833
}
775834
break;
776835
}

0 commit comments

Comments
 (0)