YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/pggate/ybc_pg_typedefs.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
13
// This module contains C definitions for all YugaByte structures that are used to exhange data
14
// and metadata between Postgres and YBClient libraries.
15
16
#ifndef YB_YQL_PGGATE_YBC_PG_TYPEDEFS_H
17
#define YB_YQL_PGGATE_YBC_PG_TYPEDEFS_H
18
19
#include <stddef.h>
20
#include <stdint.h>
21
22
#ifdef __cplusplus
23
24
#define YB_DEFINE_HANDLE_TYPE(name) \
25
    namespace yb { \
26
    namespace pggate { \
27
    class name; \
28
    } \
29
    } \
30
    typedef class yb::pggate::name *YBC##name;
31
32
#else
33
#define YB_DEFINE_HANDLE_TYPE(name) typedef struct name *YBC##name;
34
#endif  // __cplusplus
35
36
#ifdef __cplusplus
37
extern "C" {
38
#endif  // __cplusplus
39
40
// TODO(neil) Handle to Env. Each Postgres process might need just one ENV, maybe more.
41
YB_DEFINE_HANDLE_TYPE(PgEnv)
42
43
// Handle to a session. Postgres should create one YBCPgSession per client connection.
44
YB_DEFINE_HANDLE_TYPE(PgSession)
45
46
// Handle to a statement.
47
YB_DEFINE_HANDLE_TYPE(PgStatement)
48
49
// Handle to an expression.
50
YB_DEFINE_HANDLE_TYPE(PgExpr);
51
52
// Handle to a table description
53
YB_DEFINE_HANDLE_TYPE(PgTableDesc);
54
55
// Handle to a memory context.
56
YB_DEFINE_HANDLE_TYPE(PgMemctx);
57
58
//--------------------------------------------------------------------------------------------------
59
// Other definitions are the same between C++ and C.
60
//--------------------------------------------------------------------------------------------------
61
// Use YugaByte (YQL) datatype numeric representation for now, as provided in common.proto.
62
// TODO(neil) This should be change to "PgType *" and convert Postgres's TypeName struct to our
63
// class PgType or QLType.
64
typedef enum PgDataType {
65
  YB_YQL_DATA_TYPE_NOT_SUPPORTED = -1,
66
  YB_YQL_DATA_TYPE_UNKNOWN_DATA = 999,
67
  YB_YQL_DATA_TYPE_NULL_VALUE_TYPE = 0,
68
  YB_YQL_DATA_TYPE_INT8 = 1,
69
  YB_YQL_DATA_TYPE_INT16 = 2,
70
  YB_YQL_DATA_TYPE_INT32 = 3,
71
  YB_YQL_DATA_TYPE_INT64 = 4,
72
  YB_YQL_DATA_TYPE_STRING = 5,
73
  YB_YQL_DATA_TYPE_BOOL = 6,
74
  YB_YQL_DATA_TYPE_FLOAT = 7,
75
  YB_YQL_DATA_TYPE_DOUBLE = 8,
76
  YB_YQL_DATA_TYPE_BINARY = 9,
77
  YB_YQL_DATA_TYPE_TIMESTAMP = 10,
78
  YB_YQL_DATA_TYPE_DECIMAL = 11,
79
  YB_YQL_DATA_TYPE_VARINT = 12,
80
  YB_YQL_DATA_TYPE_INET = 13,
81
  YB_YQL_DATA_TYPE_LIST = 14,
82
  YB_YQL_DATA_TYPE_MAP = 15,
83
  YB_YQL_DATA_TYPE_SET = 16,
84
  YB_YQL_DATA_TYPE_UUID = 17,
85
  YB_YQL_DATA_TYPE_TIMEUUID = 18,
86
  YB_YQL_DATA_TYPE_TUPLE = 19,
87
  YB_YQL_DATA_TYPE_TYPEARGS = 20,
88
  YB_YQL_DATA_TYPE_USER_DEFINED_TYPE = 21,
89
  YB_YQL_DATA_TYPE_FROZEN = 22,
90
  YB_YQL_DATA_TYPE_DATE = 23,
91
  YB_YQL_DATA_TYPE_TIME = 24,
92
  YB_YQL_DATA_TYPE_JSONB = 25,
93
  YB_YQL_DATA_TYPE_UINT8 = 100,
94
  YB_YQL_DATA_TYPE_UINT16 = 101,
95
  YB_YQL_DATA_TYPE_UINT32 = 102,
96
  YB_YQL_DATA_TYPE_UINT64 = 103,
97
  YB_YQL_DATA_TYPE_GIN_NULL = 104,
98
} YBCPgDataType;
99
100
// Datatypes that are internally designated to be unsupported.
101
// (See similar QL_UNSUPPORTED_TYPES_IN_SWITCH.)
102
#define YB_PG_UNSUPPORTED_TYPES_IN_SWITCH \
103
0
  case YB_YQL_DATA_TYPE_NOT_SUPPORTED: \
104
0
  case YB_YQL_DATA_TYPE_UNKNOWN_DATA
105
106
// Datatypes that are not used in YSQL.
107
// (See similar QL_INVALID_TYPES_IN_SWITCH.)
108
#define YB_PG_INVALID_TYPES_IN_SWITCH \
109
0
  case YB_YQL_DATA_TYPE_NULL_VALUE_TYPE: \
110
0
  case YB_YQL_DATA_TYPE_VARINT: \
111
0
  case YB_YQL_DATA_TYPE_INET: \
112
0
  case YB_YQL_DATA_TYPE_LIST: \
113
0
  case YB_YQL_DATA_TYPE_MAP: \
114
0
  case YB_YQL_DATA_TYPE_SET: \
115
0
  case YB_YQL_DATA_TYPE_UUID: \
116
0
  case YB_YQL_DATA_TYPE_TIMEUUID: \
117
0
  case YB_YQL_DATA_TYPE_TUPLE: \
118
0
  case YB_YQL_DATA_TYPE_TYPEARGS: \
119
0
  case YB_YQL_DATA_TYPE_USER_DEFINED_TYPE: \
120
0
  case YB_YQL_DATA_TYPE_FROZEN: \
121
0
  case YB_YQL_DATA_TYPE_DATE: \
122
0
  case YB_YQL_DATA_TYPE_TIME: \
123
0
  case YB_YQL_DATA_TYPE_JSONB: \
124
0
  case YB_YQL_DATA_TYPE_UINT8: \
125
0
  case YB_YQL_DATA_TYPE_UINT16
126
127
// Datatype representation:
128
// Definition of a datatype is divided into two different sections.
129
// - YBCPgTypeEntity is used to keep static information of a datatype.
130
// - YBCPgTypeAttrs is used to keep customizable information of a datatype.
131
//
132
// Example:
133
//   For type CHAR(20), its associated YugaByte internal type (YB_YQL_DATA_TYPE_STRING) is
134
//   static while its typemod (size 20) can be customized for each usage.
135
typedef struct PgTypeAttrs {
136
  // Currently, we only need typmod, but we might need more datatype information in the future.
137
  // For example, array dimensions might be needed.
138
  int32_t typmod;
139
} YBCPgTypeAttrs;
140
141
// Datatype conversion functions.
142
typedef void (*YBCPgDatumToData)(uint64_t datum, void *ybdata, int64_t *bytes);
143
typedef uint64_t (*YBCPgDatumFromData)(const void *ybdata, int64_t bytes,
144
                                       const YBCPgTypeAttrs *type_attrs);
145
typedef struct PgTypeEntity {
146
  // Postgres type OID.
147
  int type_oid;
148
149
  // YugaByte storage (DocDB) type.
150
  YBCPgDataType yb_type;
151
152
  // Allow to be used for primary key.
153
  bool allow_for_primary_key;
154
155
  // Datum in-memory fixed size.
156
  // - Size of in-memory representation for a type. Usually it's sizeof(a_struct).
157
  //   Example: BIGINT in-memory size === sizeof(int64)
158
  //            POINT in-memory size === sizeof(struct Point)
159
  // - Set to (-1) for types of variable in-memory size - VARSIZE_ANY should be used.
160
  int64_t datum_fixed_size;
161
162
  // Converting Postgres datum to YugaByte expression.
163
  YBCPgDatumToData datum_to_yb;
164
165
  // Converting YugaByte values to Postgres in-memory-formatted datum.
166
  YBCPgDatumFromData yb_to_datum;
167
} YBCPgTypeEntity;
168
169
// Kind of a datum.
170
// In addition to datatype, a "datum" is also specified by "kind".
171
// - Standard value.
172
// - MIN limit value, which can be infinite, represents an absolute mininum value of a datatype.
173
// - MAX limit value, which can be infinite, represents an absolute maximum value of a datatype.
174
//
175
// NOTE: Currently Postgres use a separate boolean flag for null instead of datum.
176
typedef enum PgDatumKind {
177
  YB_YQL_DATUM_STANDARD_VALUE = 0,
178
  YB_YQL_DATUM_LIMIT_MAX,
179
  YB_YQL_DATUM_LIMIT_MIN,
180
} YBCPgDatumKind;
181
182
// API to read type information.
183
const YBCPgTypeEntity *YBCPgFindTypeEntity(int type_oid);
184
YBCPgDataType YBCPgGetType(const YBCPgTypeEntity *type_entity);
185
bool YBCPgAllowForPrimaryKey(const YBCPgTypeEntity *type_entity);
186
187
// PostgreSQL can represent text strings up to 1 GB minus a four-byte header.
188
static const int64_t kYBCMaxPostgresTextSizeBytes = 1024ll * 1024 * 1024 - 4;
189
190
// Postgres object identifier (OID) defined in Postgres' postgres_ext.h
191
typedef unsigned int YBCPgOid;
192
193
// These OIDs are defined here to work around the build dependency problem.
194
// In YBCheckDefinedOids(), we have assertions to ensure that they are in sync
195
// with their definitions which are generated by Postgres and not available
196
// yet in the build process when PgGate files are compiled.
197
35.6k
#define kInvalidOid ((YBCPgOid) 0)
198
3.13M
#define kByteArrayOid ((YBCPgOid) 17)
199
200
// Structure to hold the values of hidden columns when passing tuple from YB to PG.
201
typedef struct PgSysColumns {
202
  // Postgres system columns.
203
  uint32_t oid;
204
  uint32_t tableoid;
205
  uint32_t xmin;
206
  uint32_t cmin;
207
  uint32_t xmax;
208
  uint32_t cmax;
209
  uint64_t ctid;
210
211
  // Yugabyte system columns.
212
  uint8_t *ybctid;
213
  uint8_t *ybbasectid;
214
} YBCPgSysColumns;
215
216
// Structure to hold parameters for preparing query plan.
217
//
218
// Index-related parameters are used to describe different types of scan.
219
//   - Sequential scan: Index parameter is not used.
220
//     { index_oid, index_only_scan, use_secondary_index } = { kInvalidOid, false, false }
221
//   - IndexScan:
222
//     { index_oid, index_only_scan, use_secondary_index } = { IndexOid, false, true }
223
//   - IndexOnlyScan:
224
//     { index_oid, index_only_scan, use_secondary_index } = { IndexOid, true, true }
225
//   - PrimaryIndexScan: This is a special case as YugaByte doesn't have a separated
226
//     primary-index database object from table object.
227
//       index_oid = TableOid
228
//       index_only_scan = true if ROWID is wanted. Otherwise, regular rowset is wanted.
229
//       use_secondary_index = false
230
//
231
// Attribute "querying_colocated_table"
232
//   - If 'true', SELECT from SQL system catalogs or colocated tables.
233
//   - Note that the system catalogs are specifically for Postgres API and not Yugabyte
234
//     system-tables.
235
typedef struct PgPrepareParameters {
236
  YBCPgOid index_oid;
237
  bool index_only_scan;
238
  bool use_secondary_index;
239
  bool querying_colocated_table;
240
} YBCPgPrepareParameters;
241
242
// Opaque type for output parameter.
243
typedef struct YbPgExecOutParam PgExecOutParam;
244
245
// Structure for output value.
246
typedef struct PgExecOutParamValue {
247
#ifdef __cplusplus
248
  const char *bfoutput = NULL;
249
250
  // The following parameters are not yet used.
251
  // Detailing execution status in yugabyte.
252
  const char *status = NULL;
253
  int64_t status_code = 0;
254
255
#else
256
  const char *bfoutput;
257
258
  // The following parameters are not yet used.
259
  // Detailing execution status in yugabyte.
260
  const char *status;
261
  int64_t status_code;
262
#endif
263
} YbcPgExecOutParamValue;
264
265
// Structure to hold the execution-control parameters.
266
typedef struct PgExecParameters {
267
  // TODO(neil) Move forward_scan flag here.
268
  // Scan parameters.
269
  // bool is_forward_scan;
270
271
  // LIMIT parameters for executing DML read.
272
  // - limit_count is the value of SELECT ... LIMIT
273
  // - limit_offset is value of SELECT ... OFFSET
274
  // - limit_use_default: Although count and offset are pushed down to YugaByte from Postgres,
275
  //   they are not always being used to identify the number of rows to be read from DocDB.
276
  //   Full-scan is needed when further operations on the rows are not done by YugaByte.
277
  // - out_param is an output parameter of an execution while all other parameters are IN params.
278
  //
279
  //   Examples:
280
  //   o WHERE clause is not processed by YugaByte. All rows must be sent to Postgres code layer
281
  //     for filtering before LIMIT is applied.
282
  //   o ORDER BY clause is not processed by YugaByte. Similarly all rows must be fetched and sent
283
  //     to Postgres code layer.
284
  // For now we only support one rowmark.
285
#ifdef __cplusplus
286
  uint64_t limit_count = 0;
287
  uint64_t limit_offset = 0;
288
  bool limit_use_default = true;
289
  int rowmark = -1;
290
  int wait_policy = 2; // Cast to yb::WaitPolicy for C++ use. (2 is for yb::WAIT_ERROR)
291
  char *bfinstr = NULL;
292
  uint64_t* statement_read_time = NULL;
293
  char *partition_key = NULL;
294
  PgExecOutParam *out_param = NULL;
295
  bool is_index_backfill = false;
296
#else
297
  uint64_t limit_count;
298
  uint64_t limit_offset;
299
  bool limit_use_default;
300
  int rowmark;
301
  int wait_policy; // Cast to LockWaitPolicy for C use
302
  char *bfinstr;
303
  uint64_t* statement_read_time;
304
  char *partition_key;
305
  PgExecOutParam *out_param;
306
  bool is_index_backfill;
307
#endif
308
} YBCPgExecParameters;
309
310
typedef struct PgCollationInfo {
311
  bool collate_is_valid_non_c;
312
  const char *sortkey;
313
} YBCPgCollationInfo;
314
315
typedef struct PgAttrValueDescriptor {
316
  int attr_num;
317
  uint64_t datum;
318
  bool is_null;
319
  const YBCPgTypeEntity *type_entity;
320
  YBCPgCollationInfo collation_info;
321
  int collation_id;
322
} YBCPgAttrValueDescriptor;
323
324
typedef struct PgCallbacks {
325
  void (*FetchUniqueConstraintName)(YBCPgOid, char*, size_t);
326
  YBCPgMemctx (*GetCurrentYbMemctx)();
327
  const char* (*GetDebugQueryString)();
328
  void (*WriteExecOutParam)(PgExecOutParam *, const YbcPgExecOutParamValue *);
329
} YBCPgCallbacks;
330
331
typedef struct PgGFlagsAccessor {
332
  const bool*     log_ysql_catalog_versions;
333
  const bool*     ysql_disable_index_backfill;
334
  const int32_t*  ysql_max_read_restart_attempts;
335
  const int32_t*  ysql_max_write_restart_attempts;
336
  const int32_t*  ysql_output_buffer_size;
337
  const int32_t*  ysql_sequence_cache_minval;
338
  const uint64_t* ysql_session_max_batch_size;
339
  const bool*     ysql_sleep_before_retry_on_txn_conflict;
340
} YBCPgGFlagsAccessor;
341
342
typedef struct PgTableProperties {
343
  uint64_t num_tablets;
344
  uint64_t num_hash_key_columns;
345
  bool is_colocated;
346
} YBCPgTableProperties;
347
348
typedef struct PgYBTupleIdDescriptor {
349
  YBCPgOid database_oid;
350
  YBCPgOid table_oid;
351
  size_t nattrs;
352
  YBCPgAttrValueDescriptor *attrs;
353
} YBCPgYBTupleIdDescriptor;
354
355
typedef struct PgServerDescriptor {
356
  const char *host;
357
  const char *cloud;
358
  const char *region;
359
  const char *zone;
360
  const char *public_ip;
361
  bool is_primary;
362
  uint16_t pg_port;
363
} YBCServerDescriptor;
364
365
typedef struct PgColumnInfo {
366
  bool is_primary;
367
  bool is_hash;
368
} YBCPgColumnInfo;
369
370
#ifdef __cplusplus
371
}  // extern "C"
372
#endif  // __cplusplus
373
374
#undef YB_DEFINE_HANDLE_TYPE
375
376
#endif  // YB_YQL_PGGATE_YBC_PG_TYPEDEFS_H