YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/common/partial_row-test.cc
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include <gtest/gtest.h>
34
35
#include "yb/common/partial_row.h"
36
#include "yb/common/schema.h"
37
38
#include "yb/util/test_util.h"
39
40
namespace yb {
41
42
class PartialRowTest : public YBTest {
43
 public:
44
  PartialRowTest()
45
    : schema_({ ColumnSchema("key", INT32),
46
                ColumnSchema("int_val", INT32),
47
                ColumnSchema("string_val", STRING, true),
48
                ColumnSchema("binary_val", BINARY, true) },
49
2
              1) {
50
2
    SeedRandom();
51
2
  }
52
 protected:
53
  Schema schema_;
54
};
55
56
1
TEST_F(PartialRowTest, UnitTest) {
57
1
  YBPartialRow row(&schema_);
58
1
  string enc_key;
59
60
  // Initially all columns are unset.
61
1
  EXPECT_FALSE(row.IsColumnSet(0));
62
1
  EXPECT_FALSE(row.IsColumnSet(1));
63
1
  EXPECT_FALSE(row.IsColumnSet(2));
64
1
  EXPECT_FALSE(row.IsKeySet());
65
1
  EXPECT_EQ("", row.ToString());
66
67
  // Encoding the key when it is not set should give an error.
68
1
  EXPECT_EQ("Invalid argument: All key columns must be set: key",
69
1
            row.EncodeRowKey(&enc_key).ToString(/* no file/line */ false));
70
71
  // Set just the key.
72
1
  EXPECT_OK(row.SetInt32("key", 12345));
73
1
  EXPECT_TRUE(row.IsKeySet());
74
1
  EXPECT_FALSE(row.IsColumnSet(1));
75
1
  EXPECT_FALSE(row.IsColumnSet(2));
76
1
  EXPECT_EQ("int32 key=12345", row.ToString());
77
1
  int32_t x;
78
1
  EXPECT_OK(row.GetInt32("key", &x));
79
1
  EXPECT_EQ(12345, x);
80
1
  EXPECT_FALSE(row.IsNull("key"));
81
82
  // Test key encoding.
83
1
  EXPECT_EQ("OK", row.EncodeRowKey(&enc_key).ToString());
84
1
  EXPECT_EQ("80003039", Slice(enc_key).ToDebugString());
85
86
  // Fill in the other columns.
87
1
  EXPECT_OK(row.SetInt32("int_val", 54321));
88
1
  EXPECT_OK(row.SetStringCopy("string_val", "hello world"));
89
1
  EXPECT_TRUE(row.IsColumnSet(1));
90
1
  EXPECT_TRUE(row.IsColumnSet(2));
91
1
  EXPECT_EQ("int32 key=12345, int32 int_val=54321, string string_val=hello world",
92
1
            row.ToString());
93
1
  Slice slice;
94
1
  EXPECT_OK(row.GetString("string_val", &slice));
95
1
  EXPECT_EQ("hello world", slice.ToString());
96
1
  EXPECT_FALSE(row.IsNull("key"));
97
98
  // Set a nullable entry to NULL
99
1
  EXPECT_OK(row.SetNull("string_val"));
100
1
  EXPECT_EQ("int32 key=12345, int32 int_val=54321, string string_val=NULL",
101
1
            row.ToString());
102
1
  EXPECT_TRUE(row.IsNull("string_val"));
103
104
  // Try to set an entry with the wrong type
105
1
  Status s = row.SetStringCopy("int_val", "foo");
106
1
  EXPECT_EQ("Invalid argument: invalid type string provided for column 'int_val' (expected int32)",
107
1
            s.ToString(/* no file/line */ false));
108
109
  // Try to get an entry with the wrong type
110
1
  s = row.GetString("int_val", &slice);
111
1
  EXPECT_EQ("Invalid argument: invalid type string provided for column 'int_val' (expected int32)",
112
1
            s.ToString(false));
113
114
  // Try to set a non-nullable entry to NULL
115
1
  s = row.SetNull("key");
116
1
  EXPECT_EQ("Invalid argument: column not nullable: key[int32 NOT NULL NOT A PARTITION KEY]",
117
1
            s.ToString(false));
118
119
  // Set the NULL string back to non-NULL
120
1
  EXPECT_OK(row.SetStringCopy("string_val", "goodbye world"));
121
1
  EXPECT_EQ("int32 key=12345, int32 int_val=54321, string string_val=goodbye world",
122
1
            row.ToString());
123
124
  // Unset some columns.
125
1
  EXPECT_OK(row.Unset("string_val"));
126
1
  EXPECT_EQ("int32 key=12345, int32 int_val=54321", row.ToString());
127
128
1
  EXPECT_OK(row.Unset("key"));
129
1
  EXPECT_EQ("int32 int_val=54321", row.ToString());
130
131
  // Set the column by index
132
1
  EXPECT_OK(row.SetInt32(1, 99999));
133
1
  EXPECT_EQ("int32 int_val=99999", row.ToString());
134
135
  // Set the binary column as a copy.
136
1
  EXPECT_OK(row.SetBinaryCopy("binary_val", "hello_world"));
137
1
  EXPECT_EQ("int32 int_val=99999, binary binary_val=hello_world",
138
1
              row.ToString());
139
  // Unset the binary column.
140
1
  EXPECT_OK(row.Unset("binary_val"));
141
1
  EXPECT_EQ("int32 int_val=99999", row.ToString());
142
143
  // Even though the storage is actually the same at the moment, we shouldn't be
144
  // able to set string columns with SetBinary and vice versa.
145
1
  EXPECT_FALSE(row.SetBinaryCopy("string_val", "oops").ok());
146
1
  EXPECT_FALSE(row.SetStringCopy("binary_val", "oops").ok());
147
1
}
148
149
1
TEST_F(PartialRowTest, TestCopy) {
150
1
  YBPartialRow row(&schema_);
151
152
  // The assignment operator is used in this test because it internally calls
153
  // the copy constructor.
154
155
  // Check an empty copy.
156
1
  YBPartialRow copy = row;
157
1
  EXPECT_FALSE(copy.IsColumnSet(0));
158
1
  EXPECT_FALSE(copy.IsColumnSet(1));
159
1
  EXPECT_FALSE(copy.IsColumnSet(2));
160
161
1
  ASSERT_OK(row.SetInt32(0, 42));
162
1
  ASSERT_OK(row.SetInt32(1, 99));
163
1
  ASSERT_OK(row.SetStringCopy(2, "copied-string"));
164
165
1
  int32_t int_val;
166
1
  Slice string_val;
167
1
  Slice binary_val;
168
169
  // Check a copy with values.
170
1
  copy = row;
171
1
  ASSERT_OK(copy.GetInt32(0, &int_val));
172
1
  EXPECT_EQ(42, int_val);
173
1
  ASSERT_OK(copy.GetInt32(1, &int_val));
174
1
  EXPECT_EQ(99, int_val);
175
1
  ASSERT_OK(copy.GetString(2, &string_val));
176
1
  EXPECT_EQ("copied-string", string_val.ToString());
177
178
  // Check a copy with a null value.
179
1
  ASSERT_OK(row.SetNull(2));
180
1
  copy = row;
181
1
  EXPECT_TRUE(copy.IsNull(2));
182
183
  // Check a copy with a borrowed value.
184
1
  string borrowed_string = "borrowed-string";
185
1
  string borrowed_binary = "borrowed-binary";
186
1
  ASSERT_OK(row.SetString(2, borrowed_string));
187
1
  ASSERT_OK(row.SetBinary(3, borrowed_binary));
188
189
1
  copy = row;
190
1
  ASSERT_OK(copy.GetString(2, &string_val));
191
1
  EXPECT_EQ("borrowed-string", string_val.ToString());
192
1
  ASSERT_OK(copy.GetBinary(3, &binary_val));
193
1
  EXPECT_EQ("borrowed-binary", binary_val.ToString());
194
195
1
  borrowed_string.replace(0, 8, "mutated-");
196
1
  borrowed_binary.replace(0, 8, "mutated-");
197
1
  ASSERT_OK(copy.GetString(2, &string_val));
198
1
  EXPECT_EQ("mutated--string", string_val.ToString());
199
1
  ASSERT_OK(copy.GetBinary(3, &string_val));
200
1
  EXPECT_EQ("mutated--binary", string_val.ToString());
201
1
}
202
203
} // namespace yb