Kudu C++ client API
Loading...
Searching...
No Matches
schema.h
1//
2// Licensed to the Apache Software Foundation (ASF) under one
3// or more contributor license agreements. See the NOTICE file
4// distributed with this work for additional information
5// regarding copyright ownership. The ASF licenses this file
6// to you under the Apache License, Version 2.0 (the
7// "License"); you may not use this file except in compliance
8// with the License. You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing,
13// software distributed under the License is distributed on an
14// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15// KIND, either express or implied. See the License for the
16// specific language governing permissions and limitations
17// under the License.
18#ifndef KUDU_CLIENT_SCHEMA_H
19#define KUDU_CLIENT_SCHEMA_H
20
21// NOTE: using stdint.h instead of cstdint because this file is supposed
22// to be processed by a compiler lacking C++11 support.
23#include <stdint.h>
24
25#include <cstddef>
26#include <string>
27#include <vector>
28
29#ifdef KUDU_HEADERS_NO_STUBS
30#include <gtest/gtest_prod.h>
31
32#include "kudu/gutil/macros.h"
33#include "kudu/gutil/port.h"
34#else
35#include "kudu/client/stubs.h"
36#endif
37
38#include "kudu/util/kudu_export.h"
39#include "kudu/util/status.h"
40
41namespace kudu {
42
43class ColumnSchema;
44class KuduPartialRow;
45class Schema;
46class Slice;
47struct ColumnSchemaDelta;
48
49namespace tools {
50class RemoteKsckCluster;
51class ReplicaDumper;
52}
53
54namespace client {
55
56namespace internal {
57class GetTableSchemaRpc;
58class LookupRpc;
59class MetaCache;
60class MetaCacheEntry;
61class WriteRpc;
62} // namespace internal
63
64class KuduSchema;
65class KuduValue;
66
68class KUDU_EXPORT KuduColumnTypeAttributes {
69 public:
71
77
84 KuduColumnTypeAttributes(int8_t precision, int8_t scale);
85
90 explicit KuduColumnTypeAttributes(uint16_t length);
91
93
96
101
106
108 int8_t precision() const;
109
111 int8_t scale() const;
112
114 uint16_t length() const;
115
116 private:
117 friend class KuduColumnSchema;
118 friend class KuduColumnSpec;
119 friend class KuduSchema;
120
121 KuduColumnTypeAttributes(int8_t precision, int8_t scale, uint16_t length);
122
123 class KUDU_NO_EXPORT Data;
124
125 // Owned.
126 Data* data_;
127};
128
131 public:
134 AUTO_ENCODING = 0,
135 PLAIN_ENCODING = 1,
136 PREFIX_ENCODING = 2,
137 RLE = 4,
138 DICT_ENCODING = 5,
139 BIT_SHUFFLE = 6,
140
143 GROUP_VARINT = 3
144 };
145
148 DEFAULT_COMPRESSION = 0,
149 NO_COMPRESSION = 1,
150 SNAPPY = 2,
151 LZ4 = 3,
152 ZLIB = 4,
153 };
154
155
168 EncodingType encoding = AUTO_ENCODING,
169 CompressionType compression = DEFAULT_COMPRESSION,
170 int32_t block_size = 0)
171 ATTRIBUTE_DEPRECATED("this constructor will be private in a future release")
172 : encoding_(encoding),
173 compression_(compression),
174 block_size_(block_size) {
175 }
176
178 const EncodingType encoding() const {
179 return encoding_;
180 }
181
184 return compression_;
185 }
186
188 std::string ToString() const;
189
196 static Status StringToEncodingType(const std::string& encoding,
197 EncodingType* type);
198
205 static Status StringToCompressionType(const std::string& compression,
206 CompressionType* type);
207
208 private:
209 EncodingType encoding_;
210 CompressionType compression_;
211 int32_t block_size_;
212};
213
215class KUDU_EXPORT KuduColumnSchema {
216 public:
218 enum DataType {
219 INT8 = 0,
220 INT16 = 1,
221 INT32 = 2,
222 INT64 = 3,
223 STRING = 4,
224 BOOL = 5,
225 FLOAT = 6,
226 DOUBLE = 7,
227 BINARY = 8,
228 UNIXTIME_MICROS = 9,
229 DECIMAL = 10,
230 VARCHAR = 11,
231 TIMESTAMP = UNIXTIME_MICROS,
232 DATE = 12,
233 SERIAL = 13
234 };
235
239 static std::string DataTypeToString(DataType type);
240
246 static Status StringToDataType(const std::string& type_str, DataType* type);
247
254
261
266 void CopyFrom(const KuduColumnSchema& other);
267
275 bool Equals(const KuduColumnSchema& other) const
276 ATTRIBUTE_DEPRECATED("use operator==(const KuduColumnSchema&) instead");
277
284 bool operator==(const KuduColumnSchema& rhs) const;
285
292 bool operator!=(const KuduColumnSchema& rhs) const;
293
300 const std::string& name() const;
301
303 DataType type() const;
304
306 bool is_nullable() const;
307
309 bool is_immutable() const;
311
314
317
321 const std::string& comment() const;
322
323 private:
324 friend class KuduColumnSpec;
325 friend class KuduSchema;
326 friend class KuduSchemaBuilder;
327 // KuduTableAlterer::Data needs to be a friend. Friending the parent class
328 // is transitive to nested classes. See https://s.apache.org/inner-class-friends
329 friend class KuduTableAlterer;
330
331#ifdef KUDU_HEADERS_NO_STUBS
332 FRIEND_TEST(KuduColumnSchemaTest, TestEquals);
333#endif
334
336
337#if defined(__clang__) || \
338 (defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40600)
339#pragma GCC diagnostic push
340#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
341#endif
344 const std::string &name,
345 DataType type,
346 bool is_nullable = false,
347 bool is_immutable = false,
348 bool is_auto_incrementing = false,
349 const void* default_value = NULL, //NOLINT(modernize-use-nullptr)
350 const KuduColumnStorageAttributes& storage_attributes = KuduColumnStorageAttributes(),
351 const KuduColumnTypeAttributes& type_attributes = KuduColumnTypeAttributes(),
352 const std::string& comment = "");
353#if defined(__clang__) || \
354 (defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40600)
355#pragma GCC diagnostic pop
356#endif
357
358 // Owned.
359 ColumnSchema* col_;
360};
361
370class KUDU_EXPORT KuduColumnSpec {
371 public:
384
391
400
420 KuduColumnSpec* BlockSize(int32_t block_size);
421
423
440 KuduColumnSpec* Precision(int8_t precision);
441
457 KuduColumnSpec* Scale(int8_t scale);
459
461
475 KuduColumnSpec* Length(uint16_t length);
477
479
494
516
523
530
535
540
550
552
561
567 KuduColumnSpec* RenameTo(const std::string& new_name);
569
575 KuduColumnSpec* Comment(const std::string& comment);
576
577 private:
578 class KUDU_NO_EXPORT Data;
579
580 friend class KuduSchemaBuilder;
581 friend class KuduTableAlterer;
582
583 // This class should always be owned and deleted by one of its friends,
584 // not the user.
586
587 explicit KuduColumnSpec(const std::string& col_name);
588
589 Status ToColumnSchema(KuduColumnSchema* col) const;
590
591 Status ToColumnSchemaDelta(ColumnSchemaDelta* col_delta) const;
592
593 Slice DefaultValueAsSlice() const;
594
595 // Owned.
596 Data* data_;
597
598 DISALLOW_COPY_AND_ASSIGN(KuduColumnSpec);
599};
600
622class KUDU_EXPORT KuduSchemaBuilder {
623 public:
626
633 KuduColumnSpec* AddColumn(const std::string& name);
634
645 KuduSchemaBuilder* SetPrimaryKey(const std::vector<std::string>& key_col_names);
646
665 const std::vector<std::string>& key_col_names);
666
677
678 private:
679 class KUDU_NO_EXPORT Data;
680
681 // Owned.
682 Data* data_;
683
684 DISALLOW_COPY_AND_ASSIGN(KuduSchemaBuilder);
685};
686
688class KUDU_EXPORT KuduSchema {
689 public:
690 KuduSchema();
691
696 KuduSchema(const KuduSchema& other);
697 ~KuduSchema();
698
701
706
709 void CopyFrom(const KuduSchema& other);
711
724 Status Reset(const std::vector<KuduColumnSchema>& columns, int key_columns)
725 ATTRIBUTE_DEPRECATED("this method will be removed in a future release")
726 WARN_UNUSED_RESULT;
727
736 bool Equals(const KuduSchema& other) const
737 ATTRIBUTE_DEPRECATED("use operator==(const KuduSchema&) instead");
738
745 bool operator==(const KuduSchema& rhs) const;
746
753 bool operator!=(const KuduSchema& rhs) const;
754
758 KuduColumnSchema Column(size_t idx) const;
759
765 bool HasColumn(const std::string& col_name, KuduColumnSchema* col_schema) const;
766
768 size_t num_columns() const;
769
778 void GetPrimaryKeyColumnIndexes(std::vector<int>* indexes) const;
779
789 int GetAutoIncrementingColumnIndex() const;
790
794 static const char* const GetAutoIncrementingColumnName();
795
803 KuduPartialRow* NewRow() const;
804
808 std::string ToString() const;
809
811
819 static KuduSchema FromSchema(const Schema& schema) KUDU_NO_EXPORT;
820
828 static Schema ToSchema(const KuduSchema& kudu_schema) KUDU_NO_EXPORT;
829
831
832 private:
833 friend class ClientTest;
834 friend class KuduClient;
835 friend class KuduScanner;
836 friend class KuduScanToken;
837 friend class KuduScanTokenBuilder;
838 friend class KuduSchemaBuilder;
839 friend class KuduTable;
840 friend class KuduTableCreator;
841 friend class KuduWriteOperation;
842 friend class ScanConfiguration;
843 friend class internal::GetTableSchemaRpc;
844 friend class internal::LookupRpc;
845 friend class internal::MetaCache;
846 friend class internal::MetaCacheEntry;
847 friend class internal::WriteRpc;
848 friend class tools::RemoteKsckCluster;
849 friend class tools::ReplicaDumper;
850
851 // For use by KuduSchema::FromSchema.
852 explicit KuduSchema(const Schema& schema);
853#if __cplusplus >= 201103
854 explicit KuduSchema(Schema&& schema);
855#endif
856
857 // Private since we don't want users to rely on the first N columns
858 // being the keys.
859 size_t num_key_columns() const;
860
861 // Owned.
862 Schema* schema_;
863};
864
865} // namespace client
866} // namespace kudu
867#endif // KUDU_CLIENT_SCHEMA_H
A row which may only contain values for a subset of the columns.
Definition partial_row.h:72
A wrapper around externally allocated data.
Definition slice.h:51
A representation of an operation's outcome.
Definition status.h:165
A handle for a connection to a cluster.
Definition client.h:687
Representation of the column schema.
Definition schema.h:215
const std::string & name() const
void CopyFrom(const KuduColumnSchema &other)
bool operator==(const KuduColumnSchema &rhs) const
KuduColumnTypeAttributes type_attributes() const
bool operator!=(const KuduColumnSchema &rhs) const
KuduColumnSchema & operator=(const KuduColumnSchema &other)
bool Equals(const KuduColumnSchema &other) const
DataType
Supported data types for columns.
Definition schema.h:218
static Status StringToDataType(const std::string &type_str, DataType *type)
const std::string & comment() const
static std::string DataTypeToString(DataType type)
KuduColumnStorageAttributes storage_attributes() const
KuduColumnSchema(const KuduColumnSchema &other)
Builder API for specifying or altering a column within a table schema.
Definition schema.h:370
KuduColumnSpec * NonUniquePrimaryKey()
KuduColumnSpec * Encoding(KuduColumnStorageAttributes::EncodingType encoding)
KuduColumnSpec * NotNull()
KuduColumnSpec * RenameTo(const std::string &new_name)
KuduColumnSpec * RemoveDefault()
KuduColumnSpec * Type(KuduColumnSchema::DataType type)
KuduColumnSpec * BlockSize(int32_t block_size)
KuduColumnSpec * Default(KuduValue *value)
KuduColumnSpec * Immutable()
KuduColumnSpec * Compression(KuduColumnStorageAttributes::CompressionType compression)
KuduColumnSpec * PrimaryKey()
KuduColumnSpec * Precision(int8_t precision)
KuduColumnSpec * Comment(const std::string &comment)
KuduColumnSpec * Nullable()
KuduColumnSpec * Scale(int8_t scale)
KuduColumnSpec * Length(uint16_t length)
KuduColumnSpec * Mutable()
Representation of column storage attributes.
Definition schema.h:130
const CompressionType compression() const
Definition schema.h:183
static Status StringToEncodingType(const std::string &encoding, EncodingType *type)
const EncodingType encoding() const
Definition schema.h:178
KuduColumnStorageAttributes(EncodingType encoding=AUTO_ENCODING, CompressionType compression=DEFAULT_COMPRESSION, int32_t block_size=0)
Definition schema.h:167
EncodingType
Column encoding types.
Definition schema.h:133
static Status StringToCompressionType(const std::string &compression, CompressionType *type)
CompressionType
Column compression types.
Definition schema.h:147
Representation of column type attributes.
Definition schema.h:68
void CopyFrom(const KuduColumnTypeAttributes &other)
KuduColumnTypeAttributes(int8_t precision, int8_t scale)
KuduColumnTypeAttributes & operator=(const KuduColumnTypeAttributes &other)
KuduColumnTypeAttributes(const KuduColumnTypeAttributes &other)
Builds scan tokens for a table.
Definition client.h:3271
A scan descriptor limited to a single physical contiguous location.
Definition client.h:3210
This class is a representation of a single scan.
Definition client.h:2712
Builder API for constructing a KuduSchema object.
Definition schema.h:622
KuduColumnSpec * AddColumn(const std::string &name)
KuduSchemaBuilder * SetPrimaryKey(const std::vector< std::string > &key_col_names)
KuduSchemaBuilder * SetNonUniquePrimaryKey(const std::vector< std::string > &key_col_names)
Status Build(KuduSchema *schema)
A representation of a table's schema.
Definition schema.h:688
KuduSchema & operator=(const KuduSchema &other)
Status Reset(const std::vector< KuduColumnSchema > &columns, int key_columns) WARN_UNUSED_RESULT
KuduSchema(const KuduSchema &other)
void CopyFrom(const KuduSchema &other)
Alters an existing table based on the provided steps.
Definition client.h:1896
A helper class to create a new table with the desired options.
Definition client.h:1225
A representation of a table on a particular cluster.
Definition client.h:1613
A constant cell value with a specific type.
Definition value.h:37
A single-row write operation to be sent to a Kudu table.
Definition write_op.h:66