Skip to content

Commit

Permalink
KUDU-1945 [client] Add UINT64 support to cpp client
Browse files Browse the repository at this point in the history
This patch adds cpp client support to UINT64 data type. Although it is
exposed to the end user by the name SERIAL. This is because this data
type is primarily expected to store auto-incrementing column data type.

Change-Id: I36da282f8f103d642256ea6aaadd1d052ab2f234
Reviewed-on: http://gerrit.cloudera.org:8080/19267
Tested-by: Alexey Serbin <[email protected]>
Reviewed-by: Alexey Serbin <[email protected]>
  • Loading branch information
achennaka authored and alexeyserbin committed Nov 29, 2022
1 parent 5085925 commit aec4c62
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/kudu/client/schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ kudu::DataType ToInternalDataType(KuduColumnSchema::DataType type,
case KuduColumnSchema::INT16: return kudu::INT16;
case KuduColumnSchema::INT32: return kudu::INT32;
case KuduColumnSchema::INT64: return kudu::INT64;
case KuduColumnSchema::SERIAL: return kudu::UINT64;
case KuduColumnSchema::UNIXTIME_MICROS: return kudu::UNIXTIME_MICROS;
case KuduColumnSchema::DATE: return kudu::DATE;
case KuduColumnSchema::FLOAT: return kudu::FLOAT;
Expand Down Expand Up @@ -156,6 +157,7 @@ KuduColumnSchema::DataType FromInternalDataType(kudu::DataType type) {
case kudu::INT16: return KuduColumnSchema::INT16;
case kudu::INT32: return KuduColumnSchema::INT32;
case kudu::INT64: return KuduColumnSchema::INT64;
case kudu::UINT64: return KuduColumnSchema::SERIAL;
case kudu::UNIXTIME_MICROS: return KuduColumnSchema::UNIXTIME_MICROS;
case kudu::DATE: return KuduColumnSchema::DATE;
case kudu::FLOAT: return KuduColumnSchema::FLOAT;
Expand Down Expand Up @@ -703,6 +705,8 @@ string KuduColumnSchema::DataTypeToString(DataType type) {
return "DECIMAL";
case VARCHAR:
return "VARCHAR";
case SERIAL:
return "SERIAL";
}
LOG(FATAL) << "Unhandled type " << type;
}
Expand Down Expand Up @@ -737,6 +741,8 @@ string KuduColumnSchema::DataTypeToString(DataType type) {
*type = VARCHAR;
} else if (type_uc == "DATE") {
*type = DATE;
} else if (type_uc == "SERIAL") {
*type = SERIAL;
} else {
return Status::InvalidArgument(Substitute(
"data type $0 is not supported", type_str));
Expand Down
3 changes: 2 additions & 1 deletion src/kudu/client/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ class KUDU_EXPORT KuduColumnSchema {
DECIMAL = 10,
VARCHAR = 11,
TIMESTAMP = UNIXTIME_MICROS, //!< deprecated, use UNIXTIME_MICROS
DATE = 12
DATE = 12,
SERIAL = 13
};

/// @param [in] type
Expand Down
72 changes: 46 additions & 26 deletions src/kudu/common/partial_row-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class PartialRowTest : public KuduTest {
PartialRowTest()
: schema_({ ColumnSchema("key", INT32),
ColumnSchema("int_val", INT32),
ColumnSchema("uint64_val", UINT64),
ColumnSchema("string_val", STRING, true),
ColumnSchema("binary_val", BINARY, true),
ColumnSchema("decimal_val", DECIMAL32, true, false, nullptr, nullptr,
Expand Down Expand Up @@ -134,6 +135,7 @@ TEST_F(PartialRowTest, UnitTest) {
EXPECT_FALSE(row.IsColumnSet(3));
EXPECT_FALSE(row.IsColumnSet(4));
EXPECT_FALSE(row.IsColumnSet(5));
EXPECT_FALSE(row.IsColumnSet(6));
EXPECT_FALSE(row.IsKeySet());
EXPECT_EQ("", row.ToString());

Expand All @@ -158,19 +160,29 @@ TEST_F(PartialRowTest, UnitTest) {

// Fill in the other columns.
EXPECT_OK(row.SetInt32("int_val", 54321));
EXPECT_OK(row.GetInt32("int_val", &x));
EXPECT_EQ(54321, x);
EXPECT_OK(row.SetSerial("uint64_val", 7654321));
uint64_t y;
EXPECT_OK(row.GetSerial("uint64_val", &y));
EXPECT_EQ(7654321, y);
EXPECT_OK(row.SetStringCopy("string_val", "hello world"));
Slice slice;
EXPECT_OK(row.GetString("string_val", &slice));
EXPECT_EQ("hello world", slice.ToString());
EXPECT_TRUE(row.IsColumnSet(1));
EXPECT_TRUE(row.IsColumnSet(2));
EXPECT_EQ(R"(int32 key=12345, int32 int_val=54321, string string_val="hello world")",
row.ToString());
Slice slice;
EXPECT_TRUE(row.IsColumnSet(3));
EXPECT_EQ(R"(int32 key=12345, int32 int_val=54321, uint64 uint64_val=7654321, )"
R"(string string_val="hello world")",row.ToString());
EXPECT_OK(row.GetString("string_val", &slice));
EXPECT_EQ("hello world", slice.ToString());
EXPECT_FALSE(row.IsNull("key"));

// Set a nullable entry to NULL
EXPECT_OK(row.SetNull("string_val"));
EXPECT_EQ("int32 key=12345, int32 int_val=54321, string string_val=NULL",
EXPECT_EQ("int32 key=12345, int32 int_val=54321, uint64 uint64_val=7654321, "
"string string_val=NULL",
row.ToString());
EXPECT_TRUE(row.IsNull("string_val"));

Expand All @@ -190,14 +202,17 @@ TEST_F(PartialRowTest, UnitTest) {

// Set the NULL string back to non-NULL
EXPECT_OK(row.SetStringCopy("string_val", "goodbye world"));
EXPECT_EQ(R"(int32 key=12345, int32 int_val=54321, string string_val="goodbye world")",
row.ToString());
EXPECT_EQ(R"(int32 key=12345, int32 int_val=54321, uint64 uint64_val=7654321, )"
R"(string string_val="goodbye world")",row.ToString());

// Unset some columns.
EXPECT_OK(row.Unset("string_val"));
EXPECT_EQ("int32 key=12345, int32 int_val=54321", row.ToString());
EXPECT_EQ("int32 key=12345, int32 int_val=54321, uint64 uint64_val=7654321", row.ToString());

EXPECT_OK(row.Unset("key"));
EXPECT_EQ("int32 int_val=54321, uint64 uint64_val=7654321", row.ToString());

EXPECT_OK(row.Unset("uint64_val"));
EXPECT_EQ("int32 int_val=54321", row.ToString());

// Set the column by index
Expand All @@ -218,7 +233,7 @@ TEST_F(PartialRowTest, UnitTest) {

// Set a decimal column
EXPECT_OK(row.SetUnscaledDecimal("decimal_val", 123456));
EXPECT_TRUE(row.IsColumnSet(4));
EXPECT_TRUE(row.IsColumnSet(5));
EXPECT_EQ("decimal decimal_val=123456_D32", row.ToString());

// Get a decimal value using the const version of the function.
Expand Down Expand Up @@ -261,10 +276,10 @@ TEST_F(PartialRowTest, UnitTest) {
EXPECT_FALSE(row.SetBinaryCopy("string_val", "oops").ok());
EXPECT_FALSE(row.SetStringCopy("binary_val", "oops").ok());

EXPECT_OK(row.Unset(4));
EXPECT_OK(row.Unset(5));

s = row.SetVarchar("varchar_val", "shortval");
EXPECT_TRUE(row.IsColumnSet(5));
EXPECT_TRUE(row.IsColumnSet(6));
EXPECT_EQ("varchar varchar_val=\"shortval\"", row.ToString());

s = row.SetVarchar("varchar_val", "shortval value ");
Expand Down Expand Up @@ -311,12 +326,15 @@ TEST_F(PartialRowTest, TestCopy) {
EXPECT_FALSE(copy.IsColumnSet(0));
EXPECT_FALSE(copy.IsColumnSet(1));
EXPECT_FALSE(copy.IsColumnSet(2));
EXPECT_FALSE(copy.IsColumnSet(3));

ASSERT_OK(row.SetInt32(0, 42));
ASSERT_OK(row.SetInt32(1, 99));
ASSERT_OK(row.SetStringCopy(2, "copied-string"));
ASSERT_OK(row.SetSerial(2, 9999));
ASSERT_OK(row.SetStringCopy(3, "copied-string"));

int32_t int_val;
uint64_t uint64_val;
Slice string_val;
Slice binary_val;

Expand All @@ -326,31 +344,33 @@ TEST_F(PartialRowTest, TestCopy) {
EXPECT_EQ(42, int_val);
ASSERT_OK(copy.GetInt32(1, &int_val));
EXPECT_EQ(99, int_val);
ASSERT_OK(copy.GetString(2, &string_val));
ASSERT_OK(copy.GetSerial(2, &uint64_val));
EXPECT_EQ(9999, uint64_val);
ASSERT_OK(copy.GetString(3, &string_val));
EXPECT_EQ("copied-string", string_val.ToString());

// Check a copy with a null value.
ASSERT_OK(row.SetNull(2));
ASSERT_OK(row.SetNull(3));
copy = row;
EXPECT_TRUE(copy.IsNull(2));
EXPECT_TRUE(copy.IsNull(3));

// Check a copy with a borrowed value.
string borrowed_string = "borrowed-string";
string borrowed_binary = "borrowed-binary";
ASSERT_OK(row.SetStringNoCopy(2, borrowed_string));
ASSERT_OK(row.SetBinaryNoCopy(3, borrowed_binary));
ASSERT_OK(row.SetStringNoCopy(3, borrowed_string));
ASSERT_OK(row.SetBinaryNoCopy(4, borrowed_binary));

copy = row;
ASSERT_OK(copy.GetString(2, &string_val));
ASSERT_OK(copy.GetString(3, &string_val));
EXPECT_EQ("borrowed-string", string_val.ToString());
ASSERT_OK(copy.GetBinary(3, &binary_val));
ASSERT_OK(copy.GetBinary(4, &binary_val));
EXPECT_EQ("borrowed-binary", binary_val.ToString());

borrowed_string.replace(0, 8, "mutated-");
borrowed_binary.replace(0, 8, "mutated-");
ASSERT_OK(copy.GetString(2, &string_val));
ASSERT_OK(copy.GetString(3, &string_val));
EXPECT_EQ("mutated--string", string_val.ToString());
ASSERT_OK(copy.GetBinary(3, &string_val));
ASSERT_OK(copy.GetBinary(4, &string_val));
EXPECT_EQ("mutated--binary", string_val.ToString());
}

Expand All @@ -359,47 +379,47 @@ TEST_F(PartialRowTest, TestSetBinaryCopy) {
BinaryDataSetterTest(
static_cast<BinaryGetter>(&KuduPartialRow::GetBinary),
static_cast<BinarySetter>(&KuduPartialRow::SetBinaryCopy),
3, COPY);
4, COPY);
}

// Check that PartialRow::SetStringCopy() copies the input data.
TEST_F(PartialRowTest, TestSetStringCopy) {
BinaryDataSetterTest(
static_cast<BinaryGetter>(&KuduPartialRow::GetString),
static_cast<BinarySetter>(&KuduPartialRow::SetStringCopy),
2, COPY);
3, COPY);
}

// Check that PartialRow::SetBinaryNoCopy() does not copy the input data.
TEST_F(PartialRowTest, TestSetBinaryNoCopy) {
BinaryDataSetterTest(
static_cast<BinaryGetter>(&KuduPartialRow::GetBinary),
static_cast<BinarySetter>(&KuduPartialRow::SetBinaryNoCopy),
3, NO_COPY);
4, NO_COPY);
}

// Check that PartialRow::SetStringNoCopy() does not copy the input data.
TEST_F(PartialRowTest, TestSetStringNoCopy) {
BinaryDataSetterTest(
static_cast<BinaryGetter>(&KuduPartialRow::GetString),
static_cast<BinarySetter>(&KuduPartialRow::SetStringNoCopy),
2, NO_COPY);
3, NO_COPY);
}

// Check that PartialRow::SetBinary() copies the input data.
TEST_F(PartialRowTest, TestSetBinary) {
BinaryDataSetterTest(
static_cast<BinaryGetter>(&KuduPartialRow::GetBinary),
static_cast<BinarySetter>(&KuduPartialRow::SetBinary),
3, COPY);
4, COPY);
}

// Check that PartialRow::SetString() copies the input data.
TEST_F(PartialRowTest, TestSetString) {
BinaryDataSetterTest(
static_cast<BinaryGetter>(&KuduPartialRow::GetString),
static_cast<BinarySetter>(&KuduPartialRow::SetString),
2, COPY);
3, COPY);
}

} // namespace kudu
12 changes: 12 additions & 0 deletions src/kudu/common/partial_row.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ Status KuduPartialRow::SetInt32(const Slice& col_name, int32_t val) {
Status KuduPartialRow::SetInt64(const Slice& col_name, int64_t val) {
return Set<TypeTraits<INT64> >(col_name, val);
}
Status KuduPartialRow::SetSerial(const Slice& col_name, uint64_t val) {
return Set<TypeTraits<UINT64> >(col_name, val);
}
Status KuduPartialRow::SetUnixTimeMicros(const Slice& col_name, int64_t micros_since_utc_epoch) {
return Set<TypeTraits<UNIXTIME_MICROS> >(col_name, micros_since_utc_epoch);
}
Expand Down Expand Up @@ -315,6 +318,9 @@ Status KuduPartialRow::SetInt32(int col_idx, int32_t val) {
Status KuduPartialRow::SetInt64(int col_idx, int64_t val) {
return Set<TypeTraits<INT64> >(col_idx, val);
}
Status KuduPartialRow::SetSerial(int col_idx, uint64_t val) {
return Set<TypeTraits<UINT64> >(col_idx, val);
}
Status KuduPartialRow::SetUnixTimeMicros(int col_idx, int64_t micros_since_utc_epoch) {
return Set<TypeTraits<UNIXTIME_MICROS> >(col_idx, micros_since_utc_epoch);
}
Expand Down Expand Up @@ -707,6 +713,9 @@ Status KuduPartialRow::GetInt32(const Slice& col_name, int32_t* val) const {
Status KuduPartialRow::GetInt64(const Slice& col_name, int64_t* val) const {
return Get<TypeTraits<INT64> >(col_name, val);
}
Status KuduPartialRow::GetSerial(const Slice& col_name, uint64_t* val) const {
return Get<TypeTraits<UINT64> >(col_name, val);
}
Status KuduPartialRow::GetUnixTimeMicros(const Slice& col_name,
int64_t* micros_since_utc_epoch) const {
return Get<TypeTraits<UNIXTIME_MICROS> >(col_name, micros_since_utc_epoch);
Expand Down Expand Up @@ -757,6 +766,9 @@ Status KuduPartialRow::GetInt32(int col_idx, int32_t* val) const {
Status KuduPartialRow::GetInt64(int col_idx, int64_t* val) const {
return Get<TypeTraits<INT64> >(col_idx, val);
}
Status KuduPartialRow::GetSerial(int col_idx, uint64_t* val) const {
return Get<TypeTraits<UINT64> >(col_idx, val);
}
Status KuduPartialRow::GetUnixTimeMicros(int col_idx, int64_t* micros_since_utc_epoch) const {
return Get<TypeTraits<UNIXTIME_MICROS> >(col_idx, micros_since_utc_epoch);
}
Expand Down
4 changes: 4 additions & 0 deletions src/kudu/common/partial_row.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class KUDU_EXPORT KuduPartialRow {
Status SetInt16(const Slice& col_name, int16_t val) WARN_UNUSED_RESULT;
Status SetInt32(const Slice& col_name, int32_t val) WARN_UNUSED_RESULT;
Status SetInt64(const Slice& col_name, int64_t val) WARN_UNUSED_RESULT;
Status SetSerial(const Slice& col_name, uint64_t val) WARN_UNUSED_RESULT;
/// Set value for a column by name.
///
/// @param [in] col_name
Expand Down Expand Up @@ -149,6 +150,7 @@ class KUDU_EXPORT KuduPartialRow {
Status SetInt16(int col_idx, int16_t val) WARN_UNUSED_RESULT;
Status SetInt32(int col_idx, int32_t val) WARN_UNUSED_RESULT;
Status SetInt64(int col_idx, int64_t val) WARN_UNUSED_RESULT;
Status SetSerial(int col_idx, uint64_t val) WARN_UNUSED_RESULT;

/// @param [in] col_idx
/// The index of the target column.
Expand Down Expand Up @@ -460,6 +462,7 @@ class KUDU_EXPORT KuduPartialRow {
Status GetInt16(const Slice& col_name, int16_t* val) const WARN_UNUSED_RESULT;
Status GetInt32(const Slice& col_name, int32_t* val) const WARN_UNUSED_RESULT;
Status GetInt64(const Slice& col_name, int64_t* val) const WARN_UNUSED_RESULT;
Status GetSerial(const Slice& col_name, uint64_t* val) const WARN_UNUSED_RESULT;

/// @param [in] col_name
/// The name of the column.
Expand Down Expand Up @@ -516,6 +519,7 @@ class KUDU_EXPORT KuduPartialRow {
Status GetInt16(int col_idx, int16_t* val) const WARN_UNUSED_RESULT;
Status GetInt32(int col_idx, int32_t* val) const WARN_UNUSED_RESULT;
Status GetInt64(int col_idx, int64_t* val) const WARN_UNUSED_RESULT;
Status GetSerial(int col_idx, uint64_t* val) const WARN_UNUSED_RESULT;

/// @param [in] col_idx
/// The index of the target column.
Expand Down
9 changes: 9 additions & 0 deletions src/kudu/tools/tool_action_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ Status LocateRow(const RunnerContext& context) {
case KuduColumnSchema::INT32:
case KuduColumnSchema::INT64:
case KuduColumnSchema::DATE:
case KuduColumnSchema::SERIAL:
case KuduColumnSchema::UNIXTIME_MICROS: {
int64_t value;
RETURN_NOT_OK_PREPEND(
Expand Down Expand Up @@ -996,6 +997,14 @@ Status ConvertToKuduPartialRow(
RETURN_NOT_OK(range_bound_partial_row->SetInt64(col_name, value));
break;
}
case KuduColumnSchema::SERIAL: {
uint64_t value;
RETURN_NOT_OK_PREPEND(
reader.ExtractUint64(values[i], /*field=*/nullptr, &value),
error_msg);
RETURN_NOT_OK(range_bound_partial_row->SetSerial(col_name, value));
break;
}
case KuduColumnSchema::DATE: {
int32_t value;
RETURN_NOT_OK_PREPEND(
Expand Down

0 comments on commit aec4c62

Please sign in to comment.