PostgreSQL Source Code git master
Loading...
Searching...
No Matches
mac.c File Reference
#include "postgres.h"
#include "common/hashfn.h"
#include "lib/hyperloglog.h"
#include "libpq/pqformat.h"
#include "port/pg_bswap.h"
#include "utils/fmgrprotos.h"
#include "utils/guc.h"
#include "utils/inet.h"
#include "utils/sortsupport.h"
Include dependency graph for mac.c:

Go to the source code of this file.

Data Structures

struct  macaddr_sortsupport_state
 

Macros

#define hibits(addr)    ((unsigned long)(((addr)->a<<16)|((addr)->b<<8)|((addr)->c)))
 
#define lobits(addr)    ((unsigned long)(((addr)->d<<16)|((addr)->e<<8)|((addr)->f)))
 

Functions

static int macaddr_cmp_internal (macaddr *a1, macaddr *a2)
 
static int macaddr_fast_cmp (Datum x, Datum y, SortSupport ssup)
 
static bool macaddr_abbrev_abort (int memtupcount, SortSupport ssup)
 
static Datum macaddr_abbrev_convert (Datum original, SortSupport ssup)
 
Datum macaddr_in (PG_FUNCTION_ARGS)
 
Datum macaddr_out (PG_FUNCTION_ARGS)
 
Datum macaddr_recv (PG_FUNCTION_ARGS)
 
Datum macaddr_send (PG_FUNCTION_ARGS)
 
Datum macaddr_cmp (PG_FUNCTION_ARGS)
 
Datum macaddr_lt (PG_FUNCTION_ARGS)
 
Datum macaddr_le (PG_FUNCTION_ARGS)
 
Datum macaddr_eq (PG_FUNCTION_ARGS)
 
Datum macaddr_ge (PG_FUNCTION_ARGS)
 
Datum macaddr_gt (PG_FUNCTION_ARGS)
 
Datum macaddr_ne (PG_FUNCTION_ARGS)
 
Datum hashmacaddr (PG_FUNCTION_ARGS)
 
Datum hashmacaddrextended (PG_FUNCTION_ARGS)
 
Datum macaddr_not (PG_FUNCTION_ARGS)
 
Datum macaddr_and (PG_FUNCTION_ARGS)
 
Datum macaddr_or (PG_FUNCTION_ARGS)
 
Datum macaddr_trunc (PG_FUNCTION_ARGS)
 
Datum macaddr_sortsupport (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ hibits

#define hibits (   addr)     ((unsigned long)(((addr)->a<<16)|((addr)->b<<8)|((addr)->c)))

Definition at line 30 of file mac.c.

37{
38 int64 input_count; /* number of non-null values seen */
39 bool estimating; /* true if estimating cardinality */
40
41 hyperLogLogState abbr_card; /* cardinality estimator */
43
45static int macaddr_fast_cmp(Datum x, Datum y, SortSupport ssup);
46static bool macaddr_abbrev_abort(int memtupcount, SortSupport ssup);
47static Datum macaddr_abbrev_convert(Datum original, SortSupport ssup);
48
49/*
50 * MAC address reader. Accepts several common notations.
51 */
52
55{
56 char *str = PG_GETARG_CSTRING(0);
57 Node *escontext = fcinfo->context;
58 macaddr *result;
59 int a,
60 b,
61 c,
62 d,
63 e,
64 f;
65 char junk[2];
66 int count;
67
68 /* %1s matches iff there is trailing non-whitespace garbage */
69
70 count = sscanf(str, "%x:%x:%x:%x:%x:%x%1s",
71 &a, &b, &c, &d, &e, &f, junk);
72 if (count != 6)
73 count = sscanf(str, "%x-%x-%x-%x-%x-%x%1s",
74 &a, &b, &c, &d, &e, &f, junk);
75 if (count != 6)
76 count = sscanf(str, "%2x%2x%2x:%2x%2x%2x%1s",
77 &a, &b, &c, &d, &e, &f, junk);
78 if (count != 6)
79 count = sscanf(str, "%2x%2x%2x-%2x%2x%2x%1s",
80 &a, &b, &c, &d, &e, &f, junk);
81 if (count != 6)
82 count = sscanf(str, "%2x%2x.%2x%2x.%2x%2x%1s",
83 &a, &b, &c, &d, &e, &f, junk);
84 if (count != 6)
85 count = sscanf(str, "%2x%2x-%2x%2x-%2x%2x%1s",
86 &a, &b, &c, &d, &e, &f, junk);
87 if (count != 6)
88 count = sscanf(str, "%2x%2x%2x%2x%2x%2x%1s",
89 &a, &b, &c, &d, &e, &f, junk);
90 if (count != 6)
91 ereturn(escontext, (Datum) 0,
93 errmsg("invalid input syntax for type %s: \"%s\"", "macaddr",
94 str)));
95
96 if ((a < 0) || (a > 255) || (b < 0) || (b > 255) ||
97 (c < 0) || (c > 255) || (d < 0) || (d > 255) ||
98 (e < 0) || (e > 255) || (f < 0) || (f > 255))
99 ereturn(escontext, (Datum) 0,
101 errmsg("invalid octet value in \"macaddr\" value: \"%s\"", str)));
102
103 result = palloc_object(macaddr);
104
105 result->a = a;
106 result->b = b;
107 result->c = c;
108 result->d = d;
109 result->e = e;
110 result->f = f;
111
112 PG_RETURN_MACADDR_P(result);
113}
114
115/*
116 * MAC address output function. Fixed format.
117 */
118
119Datum
121{
122 macaddr *addr = PG_GETARG_MACADDR_P(0);
123 char *result;
124
125 result = (char *) palloc(32);
126
127 snprintf(result, 32, "%02x:%02x:%02x:%02x:%02x:%02x",
128 addr->a, addr->b, addr->c, addr->d, addr->e, addr->f);
129
130 PG_RETURN_CSTRING(result);
131}
132
133/*
134 * macaddr_recv - converts external binary format to macaddr
135 *
136 * The external representation is just the six bytes, MSB first.
137 */
138Datum
140{
142 macaddr *addr;
143
144 addr = palloc_object(macaddr);
145
146 addr->a = pq_getmsgbyte(buf);
147 addr->b = pq_getmsgbyte(buf);
148 addr->c = pq_getmsgbyte(buf);
149 addr->d = pq_getmsgbyte(buf);
150 addr->e = pq_getmsgbyte(buf);
151 addr->f = pq_getmsgbyte(buf);
152
154}
155
156/*
157 * macaddr_send - converts macaddr to binary format
158 */
159Datum
161{
162 macaddr *addr = PG_GETARG_MACADDR_P(0);
164
166 pq_sendbyte(&buf, addr->a);
167 pq_sendbyte(&buf, addr->b);
168 pq_sendbyte(&buf, addr->c);
169 pq_sendbyte(&buf, addr->d);
170 pq_sendbyte(&buf, addr->e);
171 pq_sendbyte(&buf, addr->f);
173}
174
175
176/*
177 * Comparison function for sorting:
178 */
179
180static int
182{
183 if (hibits(a1) < hibits(a2))
184 return -1;
185 else if (hibits(a1) > hibits(a2))
186 return 1;
187 else if (lobits(a1) < lobits(a2))
188 return -1;
189 else if (lobits(a1) > lobits(a2))
190 return 1;
191 else
192 return 0;
193}
194
195Datum
197{
200
202}
203
204/*
205 * Boolean comparisons.
206 */
207
208Datum
210{
213
215}
216
217Datum
219{
222
224}
225
226Datum
228{
231
233}
234
235Datum
237{
240
242}
243
244Datum
246{
249
251}
252
253Datum
255{
258
260}
261
262/*
263 * Support function for hash indexes on macaddr.
264 */
265Datum
267{
269
270 return hash_any((unsigned char *) key, sizeof(macaddr));
271}
272
273Datum
275{
277
278 return hash_any_extended((unsigned char *) key, sizeof(macaddr),
279 PG_GETARG_INT64(1));
280}
281
282/*
283 * Arithmetic functions: bitwise NOT, AND, OR.
284 */
285Datum
287{
288 macaddr *addr = PG_GETARG_MACADDR_P(0);
289 macaddr *result;
290
291 result = palloc_object(macaddr);
292 result->a = ~addr->a;
293 result->b = ~addr->b;
294 result->c = ~addr->c;
295 result->d = ~addr->d;
296 result->e = ~addr->e;
297 result->f = ~addr->f;
298 PG_RETURN_MACADDR_P(result);
299}
300
301Datum
303{
306 macaddr *result;
307
308 result = palloc_object(macaddr);
309 result->a = addr1->a & addr2->a;
310 result->b = addr1->b & addr2->b;
311 result->c = addr1->c & addr2->c;
312 result->d = addr1->d & addr2->d;
313 result->e = addr1->e & addr2->e;
314 result->f = addr1->f & addr2->f;
315 PG_RETURN_MACADDR_P(result);
316}
317
318Datum
320{
323 macaddr *result;
324
325 result = palloc_object(macaddr);
326 result->a = addr1->a | addr2->a;
327 result->b = addr1->b | addr2->b;
328 result->c = addr1->c | addr2->c;
329 result->d = addr1->d | addr2->d;
330 result->e = addr1->e | addr2->e;
331 result->f = addr1->f | addr2->f;
332 PG_RETURN_MACADDR_P(result);
333}
334
335/*
336 * Truncation function to allow comparing mac manufacturers.
337 * From suggestion by Alex Pilosov <alex@pilosoft.com>
338 */
339Datum
341{
342 macaddr *addr = PG_GETARG_MACADDR_P(0);
343 macaddr *result;
344
345 result = palloc_object(macaddr);
346
347 result->a = addr->a;
348 result->b = addr->b;
349 result->c = addr->c;
350 result->d = 0;
351 result->e = 0;
352 result->f = 0;
353
354 PG_RETURN_MACADDR_P(result);
355}
356
357/*
358 * SortSupport strategy function. Populates a SortSupport struct with the
359 * information necessary to use comparison by abbreviated keys.
360 */
361Datum
363{
365
367 ssup->ssup_extra = NULL;
368
369 if (ssup->abbreviate)
370 {
372 MemoryContext oldcontext;
373
374 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
375
377 uss->input_count = 0;
378 uss->estimating = true;
379 initHyperLogLog(&uss->abbr_card, 10);
380
381 ssup->ssup_extra = uss;
382
387
388 MemoryContextSwitchTo(oldcontext);
389 }
390
392}
393
394/*
395 * SortSupport "traditional" comparison function. Pulls two MAC addresses from
396 * the heap and runs a standard comparison on them.
397 */
398static int
400{
403
405}
406
407/*
408 * Callback for estimating effectiveness of abbreviated key optimization.
409 *
410 * We pay no attention to the cardinality of the non-abbreviated data, because
411 * there is no equality fast-path within authoritative macaddr comparator.
412 */
413static bool
414macaddr_abbrev_abort(int memtupcount, SortSupport ssup)
415{
417 double abbr_card;
418
420 return false;
421
422 abbr_card = estimateHyperLogLog(&uss->abbr_card);
423
424 /*
425 * If we have >100k distinct values, then even if we were sorting many
426 * billion rows we'd likely still break even, and the penalty of undoing
427 * that many rows of abbrevs would probably not be worth it. At this point
428 * we stop counting because we know that we're now fully committed.
429 */
430 if (abbr_card > 100000.0)
431 {
432 if (trace_sort)
433 elog(LOG,
434 "macaddr_abbrev: estimation ends at cardinality %f"
435 " after " INT64_FORMAT " values (%d rows)",
436 abbr_card, uss->input_count, memtupcount);
437 uss->estimating = false;
438 return false;
439 }
440
441 /*
442 * Target minimum cardinality is 1 per ~2k of non-null inputs. 0.5 row
443 * fudge factor allows us to abort earlier on genuinely pathological data
444 * where we've had exactly one abbreviated value in the first 2k
445 * (non-null) rows.
446 */
447 if (abbr_card < uss->input_count / 2000.0 + 0.5)
448 {
449 if (trace_sort)
450 elog(LOG,
451 "macaddr_abbrev: aborting abbreviation at cardinality %f"
452 " below threshold %f after " INT64_FORMAT " values (%d rows)",
453 abbr_card, uss->input_count / 2000.0 + 0.5, uss->input_count,
454 memtupcount);
455 return true;
456 }
457
458 if (trace_sort)
459 elog(LOG,
460 "macaddr_abbrev: cardinality %f after " INT64_FORMAT
461 " values (%d rows)", abbr_card, uss->input_count, memtupcount);
462
463 return false;
464}
465
466/*
467 * SortSupport conversion routine. Converts original macaddr representation
468 * to abbreviated key representation.
469 *
470 * Packs the bytes of a 6-byte MAC address into a Datum and treats it as an
471 * unsigned integer for purposes of comparison. On a 64-bit machine, there
472 * will be two zeroed bytes of padding. The integer is converted to native
473 * endianness to facilitate easy comparison.
474 */
475static Datum
477{
480 Datum res;
481
482 /*
483 * Zero out the 8-byte Datum and copy in the 6 bytes of the MAC address.
484 * There will be two bytes of zero padding on the end of the least
485 * significant bits.
486 */
487 StaticAssertDecl(sizeof(res) >= sizeof(macaddr),
488 "Datum is too small for macaddr");
489 memset(&res, 0, sizeof(res));
490 memcpy(&res, authoritative, sizeof(macaddr));
491 uss->input_count += 1;
492
493 /*
494 * Cardinality estimation. The estimate uses uint32, so XOR the two 32-bit
495 * halves together to produce slightly more entropy. The two zeroed bytes
496 * won't have any practical impact on this operation.
497 */
498 if (uss->estimating)
499 {
500 uint32 tmp;
501
502 tmp = DatumGetUInt32(res) ^ (uint32) (DatumGetUInt64(res) >> 32);
503
504 addHyperLogLog(&uss->abbr_card, DatumGetUInt32(hash_uint32(tmp)));
505 }
506
507 /*
508 * Byteswap on little-endian machines.
509 *
510 * This is needed so that ssup_datum_unsigned_cmp() (an unsigned integer
511 * 3-way comparator) works correctly on all platforms. Without this, the
512 * comparator would have to call memcmp() with a pair of pointers to the
513 * first byte of each abbreviated key, which is slower.
514 */
515 res = DatumBigEndianToNative(res);
516
517 return res;
518}
#define INT64_FORMAT
Definition c.h:564
int64_t int64
Definition c.h:543
uint32_t uint32
Definition c.h:546
#define StaticAssertDecl(condition, errmessage)
Definition c.h:942
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define LOG
Definition elog.h:31
#define ereturn(context, dummy_value,...)
Definition elog.h:278
#define elog(elevel,...)
Definition elog.h:226
#define palloc_object(type)
Definition fe_memutils.h:74
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
static Datum hash_uint32(uint32 k)
Definition hashfn.h:43
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition hashfn.h:37
static Datum hash_any(const unsigned char *k, int keylen)
Definition hashfn.h:31
const char * str
static const FormData_pg_attribute a1
Definition heap.c:144
static const FormData_pg_attribute a2
Definition heap.c:157
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
Definition hyperloglog.c:66
double estimateHyperLogLog(hyperLogLogState *cState)
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
int y
Definition isn.c:76
int b
Definition isn.c:74
int x
Definition isn.c:75
int a
Definition isn.c:73
Datum macaddr_lt(PG_FUNCTION_ARGS)
Definition mac.c:210
#define hibits(addr)
Definition mac.c:30
Datum macaddr_cmp(PG_FUNCTION_ARGS)
Definition mac.c:197
static int macaddr_cmp_internal(macaddr *a1, macaddr *a2)
Definition mac.c:182
Datum hashmacaddrextended(PG_FUNCTION_ARGS)
Definition mac.c:275
Datum hashmacaddr(PG_FUNCTION_ARGS)
Definition mac.c:267
static Datum macaddr_abbrev_convert(Datum original, SortSupport ssup)
Definition mac.c:477
Datum macaddr_or(PG_FUNCTION_ARGS)
Definition mac.c:320
Datum macaddr_recv(PG_FUNCTION_ARGS)
Definition mac.c:140
Datum macaddr_ne(PG_FUNCTION_ARGS)
Definition mac.c:255
Datum macaddr_trunc(PG_FUNCTION_ARGS)
Definition mac.c:341
Datum macaddr_eq(PG_FUNCTION_ARGS)
Definition mac.c:228
Datum macaddr_in(PG_FUNCTION_ARGS)
Definition mac.c:55
Datum macaddr_not(PG_FUNCTION_ARGS)
Definition mac.c:287
static bool macaddr_abbrev_abort(int memtupcount, SortSupport ssup)
Definition mac.c:415
Datum macaddr_and(PG_FUNCTION_ARGS)
Definition mac.c:303
Datum macaddr_send(PG_FUNCTION_ARGS)
Definition mac.c:161
Datum macaddr_sortsupport(PG_FUNCTION_ARGS)
Definition mac.c:363
#define lobits(addr)
Definition mac.c:33
Datum macaddr_ge(PG_FUNCTION_ARGS)
Definition mac.c:237
static int macaddr_fast_cmp(Datum x, Datum y, SortSupport ssup)
Definition mac.c:400
Datum macaddr_le(PG_FUNCTION_ARGS)
Definition mac.c:219
Datum macaddr_out(PG_FUNCTION_ARGS)
Definition mac.c:121
Datum macaddr_gt(PG_FUNCTION_ARGS)
Definition mac.c:246
void * palloc(Size size)
Definition mcxt.c:1387
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
#define DatumBigEndianToNative(x)
Definition pg_bswap.h:145
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define snprintf
Definition port.h:260
static uint32 DatumGetUInt32(Datum X)
Definition postgres.h:232
static uint64 DatumGetUInt64(Datum X)
Definition postgres.h:433
uint64_t Datum
Definition postgres.h:70
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
int pq_getmsgbyte(StringInfo msg)
Definition pqformat.c:398
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition pqformat.h:160
char * c
e
static int fb(int x)
struct SortSupportData * SortSupport
Definition sortsupport.h:58
struct StringInfoData * StringInfo
Definition string.h:15
Definition nodes.h:135
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
MemoryContext ssup_cxt
Definition sortsupport.h:66
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
Definition inet.h:95
unsigned char e
Definition inet.h:100
unsigned char b
Definition inet.h:97
unsigned char f
Definition inet.h:101
unsigned char c
Definition inet.h:98
unsigned char a
Definition inet.h:96
unsigned char d
Definition inet.h:99
int ssup_datum_unsigned_cmp(Datum x, Datum y, SortSupport ssup)
Definition tuplesort.c:3122
bool trace_sort
Definition tuplesort.c:122
#define PG_GETARG_MACADDR_P(n)
Definition inet.h:158
#define PG_RETURN_MACADDR_P(x)
Definition inet.h:159
static macaddr * DatumGetMacaddrP(Datum X)
Definition inet.h:147

◆ lobits

#define lobits (   addr)     ((unsigned long)(((addr)->d<<16)|((addr)->e<<8)|((addr)->f)))

Definition at line 33 of file mac.c.

Function Documentation

◆ hashmacaddr()

Datum hashmacaddr ( PG_FUNCTION_ARGS  )

Definition at line 267 of file mac.c.

268{
270
271 return hash_any((unsigned char *) key, sizeof(macaddr));
272}

References hash_any(), and PG_GETARG_MACADDR_P.

◆ hashmacaddrextended()

Datum hashmacaddrextended ( PG_FUNCTION_ARGS  )

Definition at line 275 of file mac.c.

276{
278
279 return hash_any_extended((unsigned char *) key, sizeof(macaddr),
280 PG_GETARG_INT64(1));
281}

References hash_any_extended(), PG_GETARG_INT64, and PG_GETARG_MACADDR_P.

◆ macaddr_abbrev_abort()

static bool macaddr_abbrev_abort ( int  memtupcount,
SortSupport  ssup 
)
static

Definition at line 415 of file mac.c.

416{
418 double abbr_card;
419
421 return false;
422
423 abbr_card = estimateHyperLogLog(&uss->abbr_card);
424
425 /*
426 * If we have >100k distinct values, then even if we were sorting many
427 * billion rows we'd likely still break even, and the penalty of undoing
428 * that many rows of abbrevs would probably not be worth it. At this point
429 * we stop counting because we know that we're now fully committed.
430 */
431 if (abbr_card > 100000.0)
432 {
433 if (trace_sort)
434 elog(LOG,
435 "macaddr_abbrev: estimation ends at cardinality %f"
436 " after " INT64_FORMAT " values (%d rows)",
437 abbr_card, uss->input_count, memtupcount);
438 uss->estimating = false;
439 return false;
440 }
441
442 /*
443 * Target minimum cardinality is 1 per ~2k of non-null inputs. 0.5 row
444 * fudge factor allows us to abort earlier on genuinely pathological data
445 * where we've had exactly one abbreviated value in the first 2k
446 * (non-null) rows.
447 */
448 if (abbr_card < uss->input_count / 2000.0 + 0.5)
449 {
450 if (trace_sort)
451 elog(LOG,
452 "macaddr_abbrev: aborting abbreviation at cardinality %f"
453 " below threshold %f after " INT64_FORMAT " values (%d rows)",
454 abbr_card, uss->input_count / 2000.0 + 0.5, uss->input_count,
455 memtupcount);
456 return true;
457 }
458
459 if (trace_sort)
460 elog(LOG,
461 "macaddr_abbrev: cardinality %f after " INT64_FORMAT
462 " values (%d rows)", abbr_card, uss->input_count, memtupcount);
463
464 return false;
465}

References elog, estimateHyperLogLog(), fb(), INT64_FORMAT, LOG, SortSupportData::ssup_extra, and trace_sort.

Referenced by macaddr_sortsupport().

◆ macaddr_abbrev_convert()

static Datum macaddr_abbrev_convert ( Datum  original,
SortSupport  ssup 
)
static

Definition at line 477 of file mac.c.

478{
481 Datum res;
482
483 /*
484 * Zero out the 8-byte Datum and copy in the 6 bytes of the MAC address.
485 * There will be two bytes of zero padding on the end of the least
486 * significant bits.
487 */
488 StaticAssertDecl(sizeof(res) >= sizeof(macaddr),
489 "Datum is too small for macaddr");
490 memset(&res, 0, sizeof(res));
491 memcpy(&res, authoritative, sizeof(macaddr));
492 uss->input_count += 1;
493
494 /*
495 * Cardinality estimation. The estimate uses uint32, so XOR the two 32-bit
496 * halves together to produce slightly more entropy. The two zeroed bytes
497 * won't have any practical impact on this operation.
498 */
499 if (uss->estimating)
500 {
501 uint32 tmp;
502
503 tmp = DatumGetUInt32(res) ^ (uint32) (DatumGetUInt64(res) >> 32);
504
505 addHyperLogLog(&uss->abbr_card, DatumGetUInt32(hash_uint32(tmp)));
506 }
507
508 /*
509 * Byteswap on little-endian machines.
510 *
511 * This is needed so that ssup_datum_unsigned_cmp() (an unsigned integer
512 * 3-way comparator) works correctly on all platforms. Without this, the
513 * comparator would have to call memcmp() with a pair of pointers to the
514 * first byte of each abbreviated key, which is slower.
515 */
516 res = DatumBigEndianToNative(res);
517
518 return res;
519}

References addHyperLogLog(), DatumBigEndianToNative, DatumGetMacaddrP(), DatumGetUInt32(), DatumGetUInt64(), fb(), hash_uint32(), SortSupportData::ssup_extra, and StaticAssertDecl.

Referenced by macaddr_sortsupport().

◆ macaddr_and()

Datum macaddr_and ( PG_FUNCTION_ARGS  )

Definition at line 303 of file mac.c.

304{
307 macaddr *result;
308
309 result = palloc_object(macaddr);
310 result->a = addr1->a & addr2->a;
311 result->b = addr1->b & addr2->b;
312 result->c = addr1->c & addr2->c;
313 result->d = addr1->d & addr2->d;
314 result->e = addr1->e & addr2->e;
315 result->f = addr1->f & addr2->f;
316 PG_RETURN_MACADDR_P(result);
317}

References macaddr::a, macaddr::b, macaddr::c, macaddr::d, macaddr::e, macaddr::f, fb(), palloc_object, PG_GETARG_MACADDR_P, and PG_RETURN_MACADDR_P.

◆ macaddr_cmp()

Datum macaddr_cmp ( PG_FUNCTION_ARGS  )

◆ macaddr_cmp_internal()

static int macaddr_cmp_internal ( macaddr a1,
macaddr a2 
)
static

Definition at line 182 of file mac.c.

183{
184 if (hibits(a1) < hibits(a2))
185 return -1;
186 else if (hibits(a1) > hibits(a2))
187 return 1;
188 else if (lobits(a1) < lobits(a2))
189 return -1;
190 else if (lobits(a1) > lobits(a2))
191 return 1;
192 else
193 return 0;
194}

References a1, a2, hibits, and lobits.

Referenced by macaddr_cmp(), macaddr_eq(), macaddr_fast_cmp(), macaddr_ge(), macaddr_gt(), macaddr_le(), macaddr_lt(), and macaddr_ne().

◆ macaddr_eq()

Datum macaddr_eq ( PG_FUNCTION_ARGS  )

Definition at line 228 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

Referenced by gbt_macadeq().

◆ macaddr_fast_cmp()

static int macaddr_fast_cmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 400 of file mac.c.

401{
404
406}

References DatumGetMacaddrP(), fb(), macaddr_cmp_internal(), x, and y.

Referenced by macaddr_sortsupport().

◆ macaddr_ge()

Datum macaddr_ge ( PG_FUNCTION_ARGS  )

Definition at line 237 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

Referenced by gbt_macadge().

◆ macaddr_gt()

Datum macaddr_gt ( PG_FUNCTION_ARGS  )

Definition at line 246 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

Referenced by gbt_macadgt().

◆ macaddr_in()

Datum macaddr_in ( PG_FUNCTION_ARGS  )

Definition at line 55 of file mac.c.

56{
57 char *str = PG_GETARG_CSTRING(0);
58 Node *escontext = fcinfo->context;
59 macaddr *result;
60 int a,
61 b,
62 c,
63 d,
64 e,
65 f;
66 char junk[2];
67 int count;
68
69 /* %1s matches iff there is trailing non-whitespace garbage */
70
71 count = sscanf(str, "%x:%x:%x:%x:%x:%x%1s",
72 &a, &b, &c, &d, &e, &f, junk);
73 if (count != 6)
74 count = sscanf(str, "%x-%x-%x-%x-%x-%x%1s",
75 &a, &b, &c, &d, &e, &f, junk);
76 if (count != 6)
77 count = sscanf(str, "%2x%2x%2x:%2x%2x%2x%1s",
78 &a, &b, &c, &d, &e, &f, junk);
79 if (count != 6)
80 count = sscanf(str, "%2x%2x%2x-%2x%2x%2x%1s",
81 &a, &b, &c, &d, &e, &f, junk);
82 if (count != 6)
83 count = sscanf(str, "%2x%2x.%2x%2x.%2x%2x%1s",
84 &a, &b, &c, &d, &e, &f, junk);
85 if (count != 6)
86 count = sscanf(str, "%2x%2x-%2x%2x-%2x%2x%1s",
87 &a, &b, &c, &d, &e, &f, junk);
88 if (count != 6)
89 count = sscanf(str, "%2x%2x%2x%2x%2x%2x%1s",
90 &a, &b, &c, &d, &e, &f, junk);
91 if (count != 6)
92 ereturn(escontext, (Datum) 0,
94 errmsg("invalid input syntax for type %s: \"%s\"", "macaddr",
95 str)));
96
97 if ((a < 0) || (a > 255) || (b < 0) || (b > 255) ||
98 (c < 0) || (c > 255) || (d < 0) || (d > 255) ||
99 (e < 0) || (e > 255) || (f < 0) || (f > 255))
100 ereturn(escontext, (Datum) 0,
102 errmsg("invalid octet value in \"macaddr\" value: \"%s\"", str)));
103
104 result = palloc_object(macaddr);
105
106 result->a = a;
107 result->b = b;
108 result->c = c;
109 result->d = d;
110 result->e = e;
111 result->f = f;
112
113 PG_RETURN_MACADDR_P(result);
114}

References a, macaddr::a, b, macaddr::b, macaddr::c, macaddr::d, macaddr::e, ereturn, errcode(), errmsg(), macaddr::f, fb(), palloc_object, PG_GETARG_CSTRING, PG_RETURN_MACADDR_P, and str.

◆ macaddr_le()

Datum macaddr_le ( PG_FUNCTION_ARGS  )

Definition at line 219 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

Referenced by gbt_macadle().

◆ macaddr_lt()

Datum macaddr_lt ( PG_FUNCTION_ARGS  )

Definition at line 210 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

Referenced by gbt_macadlt().

◆ macaddr_ne()

Datum macaddr_ne ( PG_FUNCTION_ARGS  )

Definition at line 255 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

◆ macaddr_not()

Datum macaddr_not ( PG_FUNCTION_ARGS  )

Definition at line 287 of file mac.c.

288{
289 macaddr *addr = PG_GETARG_MACADDR_P(0);
290 macaddr *result;
291
292 result = palloc_object(macaddr);
293 result->a = ~addr->a;
294 result->b = ~addr->b;
295 result->c = ~addr->c;
296 result->d = ~addr->d;
297 result->e = ~addr->e;
298 result->f = ~addr->f;
299 PG_RETURN_MACADDR_P(result);
300}

References macaddr::a, macaddr::b, macaddr::c, macaddr::d, macaddr::e, macaddr::f, fb(), palloc_object, PG_GETARG_MACADDR_P, and PG_RETURN_MACADDR_P.

◆ macaddr_or()

Datum macaddr_or ( PG_FUNCTION_ARGS  )

Definition at line 320 of file mac.c.

321{
324 macaddr *result;
325
326 result = palloc_object(macaddr);
327 result->a = addr1->a | addr2->a;
328 result->b = addr1->b | addr2->b;
329 result->c = addr1->c | addr2->c;
330 result->d = addr1->d | addr2->d;
331 result->e = addr1->e | addr2->e;
332 result->f = addr1->f | addr2->f;
333 PG_RETURN_MACADDR_P(result);
334}

References macaddr::a, macaddr::b, macaddr::c, macaddr::d, macaddr::e, macaddr::f, fb(), palloc_object, PG_GETARG_MACADDR_P, and PG_RETURN_MACADDR_P.

◆ macaddr_out()

Datum macaddr_out ( PG_FUNCTION_ARGS  )

Definition at line 121 of file mac.c.

122{
123 macaddr *addr = PG_GETARG_MACADDR_P(0);
124 char *result;
125
126 result = (char *) palloc(32);
127
128 snprintf(result, 32, "%02x:%02x:%02x:%02x:%02x:%02x",
129 addr->a, addr->b, addr->c, addr->d, addr->e, addr->f);
130
131 PG_RETURN_CSTRING(result);
132}

References macaddr::a, macaddr::b, macaddr::c, macaddr::d, macaddr::e, macaddr::f, palloc(), PG_GETARG_MACADDR_P, PG_RETURN_CSTRING, and snprintf.

◆ macaddr_recv()

Datum macaddr_recv ( PG_FUNCTION_ARGS  )

Definition at line 140 of file mac.c.

141{
143 macaddr *addr;
144
145 addr = palloc_object(macaddr);
146
147 addr->a = pq_getmsgbyte(buf);
148 addr->b = pq_getmsgbyte(buf);
149 addr->c = pq_getmsgbyte(buf);
150 addr->d = pq_getmsgbyte(buf);
151 addr->e = pq_getmsgbyte(buf);
152 addr->f = pq_getmsgbyte(buf);
153
155}

References macaddr::a, macaddr::b, buf, macaddr::c, macaddr::d, macaddr::e, macaddr::f, palloc_object, PG_GETARG_POINTER, PG_RETURN_MACADDR_P, and pq_getmsgbyte().

◆ macaddr_send()

Datum macaddr_send ( PG_FUNCTION_ARGS  )

Definition at line 161 of file mac.c.

162{
163 macaddr *addr = PG_GETARG_MACADDR_P(0);
165
167 pq_sendbyte(&buf, addr->a);
168 pq_sendbyte(&buf, addr->b);
169 pq_sendbyte(&buf, addr->c);
170 pq_sendbyte(&buf, addr->d);
171 pq_sendbyte(&buf, addr->e);
172 pq_sendbyte(&buf, addr->f);
174}

References macaddr::a, macaddr::b, buf, macaddr::c, macaddr::d, macaddr::e, macaddr::f, PG_GETARG_MACADDR_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendbyte().

◆ macaddr_sortsupport()

◆ macaddr_trunc()

Datum macaddr_trunc ( PG_FUNCTION_ARGS  )

Definition at line 341 of file mac.c.

342{
343 macaddr *addr = PG_GETARG_MACADDR_P(0);
344 macaddr *result;
345
346 result = palloc_object(macaddr);
347
348 result->a = addr->a;
349 result->b = addr->b;
350 result->c = addr->c;
351 result->d = 0;
352 result->e = 0;
353 result->f = 0;
354
355 PG_RETURN_MACADDR_P(result);
356}

References macaddr::a, macaddr::b, macaddr::c, macaddr::d, macaddr::e, macaddr::f, palloc_object, PG_GETARG_MACADDR_P, and PG_RETURN_MACADDR_P.