You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by al...@apache.org on 2025/10/02 02:14:30 UTC
(kudu) 03/03: KUDU-1261 update C++ client example with array column
This is an automated email from the ASF dual-hosted git repository.
alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git
commit 95abb8d6525d093b5961a7b9f2dc8c6193a47c33
Author: Alexey Serbin <al...@apache.org>
AuthorDate: Fri Sep 19 11:48:47 2025 -0700
KUDU-1261 update C++ client example with array column
Updated the Kudu C++ client application in examples/cpp/example.cc,
adding ARRAY<INT64> column into the test table and populating it with
data along with other already existing columns.
Also, added an example of using KuduArrayCellView to access an array
cell's data.
Change-Id: I0769b2ff2ddb3b6d551091ae498e9ddeb00bd881
Reviewed-on: http://gerrit.cloudera.org:8080/23445
Tested-by: Alexey Serbin <al...@apache.org>
Reviewed-by: Alexey Serbin <al...@apache.org>
---
examples/cpp/example.cc | 59 +++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 55 insertions(+), 4 deletions(-)
diff --git a/examples/cpp/example.cc b/examples/cpp/example.cc
index 84511cbea..22b7cb303 100644
--- a/examples/cpp/example.cc
+++ b/examples/cpp/example.cc
@@ -19,6 +19,7 @@
#include <iostream>
#include <sstream>
+#include "kudu/client/array_cell.h"
#include "kudu/client/callbacks.h"
#include "kudu/client/client.h"
#include "kudu/client/row_result.h"
@@ -28,6 +29,7 @@
#include "kudu/common/partial_row.h"
#include "kudu/util/monotime.h"
+using kudu::client::KuduArrayCellView;
using kudu::client::KuduClient;
using kudu::client::KuduClientBuilder;
using kudu::client::KuduColumnSchema;
@@ -62,13 +64,23 @@ static Status CreateClient(const vector<string>& master_addrs,
}
static KuduSchema CreateSchema() {
- KuduSchema schema;
KuduSchemaBuilder b;
b.AddColumn("key")->Type(KuduColumnSchema::INT32)->NotNull()->PrimaryKey();
b.AddColumn("int_val")->Type(KuduColumnSchema::INT32)->NotNull();
b.AddColumn("string_val")->Type(KuduColumnSchema::STRING)->NotNull();
- b.AddColumn("non_null_with_default")->Type(KuduColumnSchema::INT32)->NotNull()
- ->Default(KuduValue::FromInt(12345));
+ b.AddColumn("non_null_with_default")->
+ Type(KuduColumnSchema::INT32)->
+ NotNull()->
+ Default(KuduValue::FromInt(12345));
+
+ // In addition to scalar type columns, add one nullable nested type column
+ // to contain one-dimensional ararys of INT64 values.
+ const KuduColumnSchema::KuduArrayTypeDescriptor desc(KuduColumnSchema::INT64);
+ b.AddColumn("arr_int64")->
+ Type(KuduColumnSchema::NESTED)->
+ NestedType(KuduColumnSchema::KuduNestedTypeDescriptor(desc));
+
+ KuduSchema schema;
KUDU_CHECK_OK(b.Build(&schema));
return schema;
}
@@ -140,6 +152,16 @@ static Status InsertRows(const shared_ptr<KuduTable>& table, int num_rows) {
KUDU_CHECK_OK(row->SetInt32("key", i));
KUDU_CHECK_OK(row->SetInt32("integer_val", i * 2));
KUDU_CHECK_OK(row->SetInt32("non_null_with_default", i * 5));
+ {
+ // An array with three values, two non-nulls and one null in the middle.
+ vector<int64_t> values(3, 0);
+ values[0] = i * 6;
+ values[2] = i * 7;
+ vector<bool> validity(3, false);
+ validity[0] = true;
+ validity[2] = true;
+ KUDU_CHECK_OK(row->SetArrayInt64("arr_int64", values, validity));
+ }
KUDU_CHECK_OK(session->Apply(insert));
}
Status s = session->Flush();
@@ -208,6 +230,35 @@ static Status ScanRows(const shared_ptr<KuduTable>& table) {
<< next_row << " but got " << val;
return Status::IOError(out.str());
}
+
+ // An example of accessing a column by index in a row scan projection.
+ vector<int64_t> arr;
+ vector<bool> arr_notnull;
+ KUDU_RETURN_NOT_OK(row.GetArrayInt64(3, &arr, &arr_notnull));
+ KUDU_CHECK(arr.size() == arr_notnull.size());
+
+ // An example of accessing raw cell data in an array column.
+ const void* cell_ptr = row.cell(3);
+ KuduArrayCellView view(cell_ptr);
+ KUDU_RETURN_NOT_OK(view.Init());
+ KUDU_CHECK(view.elem_num() == arr.size());
+ const uint8_t* arr_raw_notnull_bitmap = view.not_null_bitmap();
+ KUDU_CHECK(arr_raw_notnull_bitmap);
+ const int64_t* arr_raw = reinterpret_cast<const int64_t*>(
+ view.data(KuduColumnSchema::INT64));
+ KUDU_CHECK(arr_raw);
+ for (size_t i = 0; i < arr_notnull.size(); ++i) {
+ if (arr_notnull[i]) {
+ // That's the same data for non-null array elements regardless
+ // of the way accessing it.
+ KUDU_CHECK(arr[i] == *(arr_raw + i));
+ }
+ // Check the validity bit: it should correspond to the boolean value
+ // at the same position in the 'arr_notnull' non-nullness array.
+ const bool arr_raw_elem_validity =
+ *(arr_raw_notnull_bitmap + (i >> 3)) & (1 << (i & 7));
+ KUDU_CHECK(arr_notnull[i] == arr_raw_elem_validity);
+ }
}
}
@@ -299,7 +350,7 @@ int main(int argc, char* argv[]) {
bool exists;
KUDU_CHECK_OK(DoesTableExist(client, kTableName, &exists));
if (exists) {
- client->DeleteTable(kTableName);
+ KUDU_CHECK_OK(client->DeleteTable(kTableName));
KUDU_LOG(INFO) << "Deleting old table before creating new one";
}
KUDU_CHECK_OK(CreateTable(client, kTableName, schema, 10));