1 /*
2 * contrib/hstore/hstore_io.c
3 */
4 #include "postgres.h"
5
6 #include <ctype.h>
7
8 #include "access/htup_details.h"
9 #include "catalog/pg_type.h"
10 #include "funcapi.h"
11 #include "lib/stringinfo.h"
12 #include "libpq/pqformat.h"
13 #include "utils/builtins.h"
14 #include "utils/json.h"
15 #include "utils/jsonapi.h"
16 #include "utils/jsonb.h"
17 #include "utils/lsyscache.h"
18 #include "utils/memutils.h"
19 #include "utils/typcache.h"
20
21 #include "hstore.h"
22
23 PG_MODULE_MAGIC;
24
25 /* old names for C functions */
26 HSTORE_POLLUTE(hstore_from_text, tconvert);
27
28
29 typedef struct
30 {
31 char *begin;
32 char *ptr;
33 char *cur;
34 char *word;
35 int wordlen;
36
37 Pairs *pairs;
38 int pcur;
39 int plen;
40 } HSParser;
41
42 #define RESIZEPRSBUF \
43 do { \
44 if ( state->cur - state->word + 1 >= state->wordlen ) \
45 { \
46 int32 clen = state->cur - state->word; \
47 state->wordlen *= 2; \
48 state->word = (char*)repalloc( (void*)state->word, state->wordlen ); \
49 state->cur = state->word + clen; \
50 } \
51 } while (0)
52
53
54 #define GV_WAITVAL 0
55 #define GV_INVAL 1
56 #define GV_INESCVAL 2
57 #define GV_WAITESCIN 3
58 #define GV_WAITESCESCIN 4
59
60 static bool
get_val(HSParser * state,bool ignoreeq,bool * escaped)61 get_val(HSParser *state, bool ignoreeq, bool *escaped)
62 {
63 int st = GV_WAITVAL;
64
65 state->wordlen = 32;
66 state->cur = state->word = palloc(state->wordlen);
67 *escaped = false;
68
69 while (1)
70 {
71 if (st == GV_WAITVAL)
72 {
73 if (*(state->ptr) == '"')
74 {
75 *escaped = true;
76 st = GV_INESCVAL;
77 }
78 else if (*(state->ptr) == '\0')
79 {
80 return false;
81 }
82 else if (*(state->ptr) == '=' && !ignoreeq)
83 {
84 elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
85 }
86 else if (*(state->ptr) == '\\')
87 {
88 st = GV_WAITESCIN;
89 }
90 else if (!isspace((unsigned char) *(state->ptr)))
91 {
92 *(state->cur) = *(state->ptr);
93 state->cur++;
94 st = GV_INVAL;
95 }
96 }
97 else if (st == GV_INVAL)
98 {
99 if (*(state->ptr) == '\\')
100 {
101 st = GV_WAITESCIN;
102 }
103 else if (*(state->ptr) == '=' && !ignoreeq)
104 {
105 state->ptr--;
106 return true;
107 }
108 else if (*(state->ptr) == ',' && ignoreeq)
109 {
110 state->ptr--;
111 return true;
112 }
113 else if (isspace((unsigned char) *(state->ptr)))
114 {
115 return true;
116 }
117 else if (*(state->ptr) == '\0')
118 {
119 state->ptr--;
120 return true;
121 }
122 else
123 {
124 RESIZEPRSBUF;
125 *(state->cur) = *(state->ptr);
126 state->cur++;
127 }
128 }
129 else if (st == GV_INESCVAL)
130 {
131 if (*(state->ptr) == '\\')
132 {
133 st = GV_WAITESCESCIN;
134 }
135 else if (*(state->ptr) == '"')
136 {
137 return true;
138 }
139 else if (*(state->ptr) == '\0')
140 {
141 elog(ERROR, "Unexpected end of string");
142 }
143 else
144 {
145 RESIZEPRSBUF;
146 *(state->cur) = *(state->ptr);
147 state->cur++;
148 }
149 }
150 else if (st == GV_WAITESCIN)
151 {
152 if (*(state->ptr) == '\0')
153 elog(ERROR, "Unexpected end of string");
154 RESIZEPRSBUF;
155 *(state->cur) = *(state->ptr);
156 state->cur++;
157 st = GV_INVAL;
158 }
159 else if (st == GV_WAITESCESCIN)
160 {
161 if (*(state->ptr) == '\0')
162 elog(ERROR, "Unexpected end of string");
163 RESIZEPRSBUF;
164 *(state->cur) = *(state->ptr);
165 state->cur++;
166 st = GV_INESCVAL;
167 }
168 else
169 elog(ERROR, "Unknown state %d at position line %d in file '%s'", st, __LINE__, __FILE__);
170
171 state->ptr++;
172 }
173 }
174
175 #define WKEY 0
176 #define WVAL 1
177 #define WEQ 2
178 #define WGT 3
179 #define WDEL 4
180
181
182 static void
parse_hstore(HSParser * state)183 parse_hstore(HSParser *state)
184 {
185 int st = WKEY;
186 bool escaped = false;
187
188 state->plen = 16;
189 state->pairs = (Pairs *) palloc(sizeof(Pairs) * state->plen);
190 state->pcur = 0;
191 state->ptr = state->begin;
192 state->word = NULL;
193
194 while (1)
195 {
196 if (st == WKEY)
197 {
198 if (!get_val(state, false, &escaped))
199 return;
200 if (state->pcur >= state->plen)
201 {
202 state->plen *= 2;
203 state->pairs = (Pairs *) repalloc(state->pairs, sizeof(Pairs) * state->plen);
204 }
205 state->pairs[state->pcur].key = state->word;
206 state->pairs[state->pcur].keylen = hstoreCheckKeyLen(state->cur - state->word);
207 state->pairs[state->pcur].val = NULL;
208 state->word = NULL;
209 st = WEQ;
210 }
211 else if (st == WEQ)
212 {
213 if (*(state->ptr) == '=')
214 {
215 st = WGT;
216 }
217 else if (*(state->ptr) == '\0')
218 {
219 elog(ERROR, "Unexpected end of string");
220 }
221 else if (!isspace((unsigned char) *(state->ptr)))
222 {
223 elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
224 }
225 }
226 else if (st == WGT)
227 {
228 if (*(state->ptr) == '>')
229 {
230 st = WVAL;
231 }
232 else if (*(state->ptr) == '\0')
233 {
234 elog(ERROR, "Unexpected end of string");
235 }
236 else
237 {
238 elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
239 }
240 }
241 else if (st == WVAL)
242 {
243 if (!get_val(state, true, &escaped))
244 elog(ERROR, "Unexpected end of string");
245 state->pairs[state->pcur].val = state->word;
246 state->pairs[state->pcur].vallen = hstoreCheckValLen(state->cur - state->word);
247 state->pairs[state->pcur].isnull = false;
248 state->pairs[state->pcur].needfree = true;
249 if (state->cur - state->word == 4 && !escaped)
250 {
251 state->word[4] = '\0';
252 if (0 == pg_strcasecmp(state->word, "null"))
253 state->pairs[state->pcur].isnull = true;
254 }
255 state->word = NULL;
256 state->pcur++;
257 st = WDEL;
258 }
259 else if (st == WDEL)
260 {
261 if (*(state->ptr) == ',')
262 {
263 st = WKEY;
264 }
265 else if (*(state->ptr) == '\0')
266 {
267 return;
268 }
269 else if (!isspace((unsigned char) *(state->ptr)))
270 {
271 elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
272 }
273 }
274 else
275 elog(ERROR, "Unknown state %d at line %d in file '%s'", st, __LINE__, __FILE__);
276
277 state->ptr++;
278 }
279 }
280
281 static int
comparePairs(const void * a,const void * b)282 comparePairs(const void *a, const void *b)
283 {
284 const Pairs *pa = a;
285 const Pairs *pb = b;
286
287 if (pa->keylen == pb->keylen)
288 {
289 int res = memcmp(pa->key, pb->key, pa->keylen);
290
291 if (res)
292 return res;
293
294 /* guarantee that needfree will be later */
295 if (pb->needfree == pa->needfree)
296 return 0;
297 else if (pa->needfree)
298 return 1;
299 else
300 return -1;
301 }
302 return (pa->keylen > pb->keylen) ? 1 : -1;
303 }
304
305 /*
306 * this code still respects pairs.needfree, even though in general
307 * it should never be called in a context where anything needs freeing.
308 * we keep it because (a) those calls are in a rare code path anyway,
309 * and (b) who knows whether they might be needed by some caller.
310 */
311 int
hstoreUniquePairs(Pairs * a,int32 l,int32 * buflen)312 hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen)
313 {
314 Pairs *ptr,
315 *res;
316
317 *buflen = 0;
318 if (l < 2)
319 {
320 if (l == 1)
321 *buflen = a->keylen + ((a->isnull) ? 0 : a->vallen);
322 return l;
323 }
324
325 qsort((void *) a, l, sizeof(Pairs), comparePairs);
326 ptr = a + 1;
327 res = a;
328 while (ptr - a < l)
329 {
330 if (ptr->keylen == res->keylen &&
331 memcmp(ptr->key, res->key, res->keylen) == 0)
332 {
333 if (ptr->needfree)
334 {
335 pfree(ptr->key);
336 pfree(ptr->val);
337 }
338 }
339 else
340 {
341 *buflen += res->keylen + ((res->isnull) ? 0 : res->vallen);
342 res++;
343 if (res != ptr)
344 memcpy(res, ptr, sizeof(Pairs));
345 }
346
347 ptr++;
348 }
349
350 *buflen += res->keylen + ((res->isnull) ? 0 : res->vallen);
351 return res + 1 - a;
352 }
353
354 size_t
hstoreCheckKeyLen(size_t len)355 hstoreCheckKeyLen(size_t len)
356 {
357 if (len > HSTORE_MAX_KEY_LEN)
358 ereport(ERROR,
359 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
360 errmsg("string too long for hstore key")));
361 return len;
362 }
363
364 size_t
hstoreCheckValLen(size_t len)365 hstoreCheckValLen(size_t len)
366 {
367 if (len > HSTORE_MAX_VALUE_LEN)
368 ereport(ERROR,
369 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
370 errmsg("string too long for hstore value")));
371 return len;
372 }
373
374
375 HStore *
hstorePairs(Pairs * pairs,int32 pcount,int32 buflen)376 hstorePairs(Pairs *pairs, int32 pcount, int32 buflen)
377 {
378 HStore *out;
379 HEntry *entry;
380 char *ptr;
381 char *buf;
382 int32 len;
383 int32 i;
384
385 len = CALCDATASIZE(pcount, buflen);
386 out = palloc(len);
387 SET_VARSIZE(out, len);
388 HS_SETCOUNT(out, pcount);
389
390 if (pcount == 0)
391 return out;
392
393 entry = ARRPTR(out);
394 buf = ptr = STRPTR(out);
395
396 for (i = 0; i < pcount; i++)
397 HS_ADDITEM(entry, buf, ptr, pairs[i]);
398
399 HS_FINALIZE(out, pcount, buf, ptr);
400
401 return out;
402 }
403
404
405 PG_FUNCTION_INFO_V1(hstore_in);
406 Datum
hstore_in(PG_FUNCTION_ARGS)407 hstore_in(PG_FUNCTION_ARGS)
408 {
409 HSParser state;
410 int32 buflen;
411 HStore *out;
412
413 state.begin = PG_GETARG_CSTRING(0);
414
415 parse_hstore(&state);
416
417 state.pcur = hstoreUniquePairs(state.pairs, state.pcur, &buflen);
418
419 out = hstorePairs(state.pairs, state.pcur, buflen);
420
421 PG_RETURN_POINTER(out);
422 }
423
424
425 PG_FUNCTION_INFO_V1(hstore_recv);
426 Datum
hstore_recv(PG_FUNCTION_ARGS)427 hstore_recv(PG_FUNCTION_ARGS)
428 {
429 int32 buflen;
430 HStore *out;
431 Pairs *pairs;
432 int32 i;
433 int32 pcount;
434 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
435
436 pcount = pq_getmsgint(buf, 4);
437
438 if (pcount == 0)
439 {
440 out = hstorePairs(NULL, 0, 0);
441 PG_RETURN_POINTER(out);
442 }
443
444 if (pcount < 0 || pcount > MaxAllocSize / sizeof(Pairs))
445 ereport(ERROR,
446 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
447 errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
448 pcount, (int) (MaxAllocSize / sizeof(Pairs)))));
449 pairs = palloc(pcount * sizeof(Pairs));
450
451 for (i = 0; i < pcount; ++i)
452 {
453 int rawlen = pq_getmsgint(buf, 4);
454 int len;
455
456 if (rawlen < 0)
457 ereport(ERROR,
458 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
459 errmsg("null value not allowed for hstore key")));
460
461 pairs[i].key = pq_getmsgtext(buf, rawlen, &len);
462 pairs[i].keylen = hstoreCheckKeyLen(len);
463 pairs[i].needfree = true;
464
465 rawlen = pq_getmsgint(buf, 4);
466 if (rawlen < 0)
467 {
468 pairs[i].val = NULL;
469 pairs[i].vallen = 0;
470 pairs[i].isnull = true;
471 }
472 else
473 {
474 pairs[i].val = pq_getmsgtext(buf, rawlen, &len);
475 pairs[i].vallen = hstoreCheckValLen(len);
476 pairs[i].isnull = false;
477 }
478 }
479
480 pcount = hstoreUniquePairs(pairs, pcount, &buflen);
481
482 out = hstorePairs(pairs, pcount, buflen);
483
484 PG_RETURN_POINTER(out);
485 }
486
487
488 PG_FUNCTION_INFO_V1(hstore_from_text);
489 Datum
hstore_from_text(PG_FUNCTION_ARGS)490 hstore_from_text(PG_FUNCTION_ARGS)
491 {
492 text *key;
493 text *val = NULL;
494 Pairs p;
495 HStore *out;
496
497 if (PG_ARGISNULL(0))
498 PG_RETURN_NULL();
499
500 p.needfree = false;
501 key = PG_GETARG_TEXT_PP(0);
502 p.key = VARDATA_ANY(key);
503 p.keylen = hstoreCheckKeyLen(VARSIZE_ANY_EXHDR(key));
504
505 if (PG_ARGISNULL(1))
506 {
507 p.vallen = 0;
508 p.isnull = true;
509 }
510 else
511 {
512 val = PG_GETARG_TEXT_PP(1);
513 p.val = VARDATA_ANY(val);
514 p.vallen = hstoreCheckValLen(VARSIZE_ANY_EXHDR(val));
515 p.isnull = false;
516 }
517
518 out = hstorePairs(&p, 1, p.keylen + p.vallen);
519
520 PG_RETURN_POINTER(out);
521 }
522
523
524 PG_FUNCTION_INFO_V1(hstore_from_arrays);
525 Datum
hstore_from_arrays(PG_FUNCTION_ARGS)526 hstore_from_arrays(PG_FUNCTION_ARGS)
527 {
528 int32 buflen;
529 HStore *out;
530 Pairs *pairs;
531 Datum *key_datums;
532 bool *key_nulls;
533 int key_count;
534 Datum *value_datums;
535 bool *value_nulls;
536 int value_count;
537 ArrayType *key_array;
538 ArrayType *value_array;
539 int i;
540
541 if (PG_ARGISNULL(0))
542 PG_RETURN_NULL();
543
544 key_array = PG_GETARG_ARRAYTYPE_P(0);
545
546 Assert(ARR_ELEMTYPE(key_array) == TEXTOID);
547
548 /*
549 * must check >1 rather than != 1 because empty arrays have 0 dimensions,
550 * not 1
551 */
552
553 if (ARR_NDIM(key_array) > 1)
554 ereport(ERROR,
555 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
556 errmsg("wrong number of array subscripts")));
557
558 deconstruct_array(key_array,
559 TEXTOID, -1, false, 'i',
560 &key_datums, &key_nulls, &key_count);
561
562 /* see discussion in hstoreArrayToPairs() */
563 if (key_count > MaxAllocSize / sizeof(Pairs))
564 ereport(ERROR,
565 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
566 errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
567 key_count, (int) (MaxAllocSize / sizeof(Pairs)))));
568
569 /* value_array might be NULL */
570
571 if (PG_ARGISNULL(1))
572 {
573 value_array = NULL;
574 value_count = key_count;
575 value_datums = NULL;
576 value_nulls = NULL;
577 }
578 else
579 {
580 value_array = PG_GETARG_ARRAYTYPE_P(1);
581
582 Assert(ARR_ELEMTYPE(value_array) == TEXTOID);
583
584 if (ARR_NDIM(value_array) > 1)
585 ereport(ERROR,
586 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
587 errmsg("wrong number of array subscripts")));
588
589 if ((ARR_NDIM(key_array) > 0 || ARR_NDIM(value_array) > 0) &&
590 (ARR_NDIM(key_array) != ARR_NDIM(value_array) ||
591 ARR_DIMS(key_array)[0] != ARR_DIMS(value_array)[0] ||
592 ARR_LBOUND(key_array)[0] != ARR_LBOUND(value_array)[0]))
593 ereport(ERROR,
594 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
595 errmsg("arrays must have same bounds")));
596
597 deconstruct_array(value_array,
598 TEXTOID, -1, false, 'i',
599 &value_datums, &value_nulls, &value_count);
600
601 Assert(key_count == value_count);
602 }
603
604 pairs = palloc(key_count * sizeof(Pairs));
605
606 for (i = 0; i < key_count; ++i)
607 {
608 if (key_nulls[i])
609 ereport(ERROR,
610 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
611 errmsg("null value not allowed for hstore key")));
612
613 if (!value_nulls || value_nulls[i])
614 {
615 pairs[i].key = VARDATA(key_datums[i]);
616 pairs[i].val = NULL;
617 pairs[i].keylen =
618 hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ);
619 pairs[i].vallen = 4;
620 pairs[i].isnull = true;
621 pairs[i].needfree = false;
622 }
623 else
624 {
625 pairs[i].key = VARDATA(key_datums[i]);
626 pairs[i].val = VARDATA(value_datums[i]);
627 pairs[i].keylen =
628 hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ);
629 pairs[i].vallen =
630 hstoreCheckValLen(VARSIZE(value_datums[i]) - VARHDRSZ);
631 pairs[i].isnull = false;
632 pairs[i].needfree = false;
633 }
634 }
635
636 key_count = hstoreUniquePairs(pairs, key_count, &buflen);
637
638 out = hstorePairs(pairs, key_count, buflen);
639
640 PG_RETURN_POINTER(out);
641 }
642
643
644 PG_FUNCTION_INFO_V1(hstore_from_array);
645 Datum
hstore_from_array(PG_FUNCTION_ARGS)646 hstore_from_array(PG_FUNCTION_ARGS)
647 {
648 ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
649 int ndims = ARR_NDIM(in_array);
650 int count;
651 int32 buflen;
652 HStore *out;
653 Pairs *pairs;
654 Datum *in_datums;
655 bool *in_nulls;
656 int in_count;
657 int i;
658
659 Assert(ARR_ELEMTYPE(in_array) == TEXTOID);
660
661 switch (ndims)
662 {
663 case 0:
664 out = hstorePairs(NULL, 0, 0);
665 PG_RETURN_POINTER(out);
666
667 case 1:
668 if ((ARR_DIMS(in_array)[0]) % 2)
669 ereport(ERROR,
670 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
671 errmsg("array must have even number of elements")));
672 break;
673
674 case 2:
675 if ((ARR_DIMS(in_array)[1]) != 2)
676 ereport(ERROR,
677 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
678 errmsg("array must have two columns")));
679 break;
680
681 default:
682 ereport(ERROR,
683 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
684 errmsg("wrong number of array subscripts")));
685 }
686
687 deconstruct_array(in_array,
688 TEXTOID, -1, false, 'i',
689 &in_datums, &in_nulls, &in_count);
690
691 count = in_count / 2;
692
693 /* see discussion in hstoreArrayToPairs() */
694 if (count > MaxAllocSize / sizeof(Pairs))
695 ereport(ERROR,
696 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
697 errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
698 count, (int) (MaxAllocSize / sizeof(Pairs)))));
699
700 pairs = palloc(count * sizeof(Pairs));
701
702 for (i = 0; i < count; ++i)
703 {
704 if (in_nulls[i * 2])
705 ereport(ERROR,
706 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
707 errmsg("null value not allowed for hstore key")));
708
709 if (in_nulls[i * 2 + 1])
710 {
711 pairs[i].key = VARDATA(in_datums[i * 2]);
712 pairs[i].val = NULL;
713 pairs[i].keylen =
714 hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ);
715 pairs[i].vallen = 4;
716 pairs[i].isnull = true;
717 pairs[i].needfree = false;
718 }
719 else
720 {
721 pairs[i].key = VARDATA(in_datums[i * 2]);
722 pairs[i].val = VARDATA(in_datums[i * 2 + 1]);
723 pairs[i].keylen =
724 hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ);
725 pairs[i].vallen =
726 hstoreCheckValLen(VARSIZE(in_datums[i * 2 + 1]) - VARHDRSZ);
727 pairs[i].isnull = false;
728 pairs[i].needfree = false;
729 }
730 }
731
732 count = hstoreUniquePairs(pairs, count, &buflen);
733
734 out = hstorePairs(pairs, count, buflen);
735
736 PG_RETURN_POINTER(out);
737 }
738
739 /* most of hstore_from_record is shamelessly swiped from record_out */
740
741 /*
742 * structure to cache metadata needed for record I/O
743 */
744 typedef struct ColumnIOData
745 {
746 Oid column_type;
747 Oid typiofunc;
748 Oid typioparam;
749 FmgrInfo proc;
750 } ColumnIOData;
751
752 typedef struct RecordIOData
753 {
754 Oid record_type;
755 int32 record_typmod;
756 int ncolumns;
757 ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER];
758 } RecordIOData;
759
760 PG_FUNCTION_INFO_V1(hstore_from_record);
761 Datum
hstore_from_record(PG_FUNCTION_ARGS)762 hstore_from_record(PG_FUNCTION_ARGS)
763 {
764 HeapTupleHeader rec;
765 int32 buflen;
766 HStore *out;
767 Pairs *pairs;
768 Oid tupType;
769 int32 tupTypmod;
770 TupleDesc tupdesc;
771 HeapTupleData tuple;
772 RecordIOData *my_extra;
773 int ncolumns;
774 int i,
775 j;
776 Datum *values;
777 bool *nulls;
778
779 if (PG_ARGISNULL(0))
780 {
781 Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
782
783 /*
784 * have no tuple to look at, so the only source of type info is the
785 * argtype. The lookup_rowtype_tupdesc call below will error out if we
786 * don't have a known composite type oid here.
787 */
788 tupType = argtype;
789 tupTypmod = -1;
790
791 rec = NULL;
792 }
793 else
794 {
795 rec = PG_GETARG_HEAPTUPLEHEADER(0);
796
797 /* Extract type info from the tuple itself */
798 tupType = HeapTupleHeaderGetTypeId(rec);
799 tupTypmod = HeapTupleHeaderGetTypMod(rec);
800 }
801
802 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
803 ncolumns = tupdesc->natts;
804
805 /*
806 * We arrange to look up the needed I/O info just once per series of
807 * calls, assuming the record type doesn't change underneath us.
808 */
809 my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
810 if (my_extra == NULL ||
811 my_extra->ncolumns != ncolumns)
812 {
813 fcinfo->flinfo->fn_extra =
814 MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
815 offsetof(RecordIOData, columns) +
816 ncolumns * sizeof(ColumnIOData));
817 my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
818 my_extra->record_type = InvalidOid;
819 my_extra->record_typmod = 0;
820 }
821
822 if (my_extra->record_type != tupType ||
823 my_extra->record_typmod != tupTypmod)
824 {
825 MemSet(my_extra, 0,
826 offsetof(RecordIOData, columns) +
827 ncolumns * sizeof(ColumnIOData));
828 my_extra->record_type = tupType;
829 my_extra->record_typmod = tupTypmod;
830 my_extra->ncolumns = ncolumns;
831 }
832
833 Assert(ncolumns <= MaxTupleAttributeNumber); /* thus, no overflow */
834 pairs = palloc(ncolumns * sizeof(Pairs));
835
836 if (rec)
837 {
838 /* Build a temporary HeapTuple control structure */
839 tuple.t_len = HeapTupleHeaderGetDatumLength(rec);
840 ItemPointerSetInvalid(&(tuple.t_self));
841 tuple.t_tableOid = InvalidOid;
842 tuple.t_data = rec;
843
844 values = (Datum *) palloc(ncolumns * sizeof(Datum));
845 nulls = (bool *) palloc(ncolumns * sizeof(bool));
846
847 /* Break down the tuple into fields */
848 heap_deform_tuple(&tuple, tupdesc, values, nulls);
849 }
850 else
851 {
852 values = NULL;
853 nulls = NULL;
854 }
855
856 for (i = 0, j = 0; i < ncolumns; ++i)
857 {
858 ColumnIOData *column_info = &my_extra->columns[i];
859 Oid column_type = tupdesc->attrs[i]->atttypid;
860 char *value;
861
862 /* Ignore dropped columns in datatype */
863 if (tupdesc->attrs[i]->attisdropped)
864 continue;
865
866 pairs[j].key = NameStr(tupdesc->attrs[i]->attname);
867 pairs[j].keylen = hstoreCheckKeyLen(strlen(NameStr(tupdesc->attrs[i]->attname)));
868
869 if (!nulls || nulls[i])
870 {
871 pairs[j].val = NULL;
872 pairs[j].vallen = 4;
873 pairs[j].isnull = true;
874 pairs[j].needfree = false;
875 ++j;
876 continue;
877 }
878
879 /*
880 * Convert the column value to text
881 */
882 if (column_info->column_type != column_type)
883 {
884 bool typIsVarlena;
885
886 getTypeOutputInfo(column_type,
887 &column_info->typiofunc,
888 &typIsVarlena);
889 fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
890 fcinfo->flinfo->fn_mcxt);
891 column_info->column_type = column_type;
892 }
893
894 value = OutputFunctionCall(&column_info->proc, values[i]);
895
896 pairs[j].val = value;
897 pairs[j].vallen = hstoreCheckValLen(strlen(value));
898 pairs[j].isnull = false;
899 pairs[j].needfree = false;
900 ++j;
901 }
902
903 ncolumns = hstoreUniquePairs(pairs, j, &buflen);
904
905 out = hstorePairs(pairs, ncolumns, buflen);
906
907 ReleaseTupleDesc(tupdesc);
908
909 PG_RETURN_POINTER(out);
910 }
911
912
913 PG_FUNCTION_INFO_V1(hstore_populate_record);
914 Datum
hstore_populate_record(PG_FUNCTION_ARGS)915 hstore_populate_record(PG_FUNCTION_ARGS)
916 {
917 Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
918 HStore *hs;
919 HEntry *entries;
920 char *ptr;
921 HeapTupleHeader rec;
922 Oid tupType;
923 int32 tupTypmod;
924 TupleDesc tupdesc;
925 HeapTupleData tuple;
926 HeapTuple rettuple;
927 RecordIOData *my_extra;
928 int ncolumns;
929 int i;
930 Datum *values;
931 bool *nulls;
932
933 if (!type_is_rowtype(argtype))
934 ereport(ERROR,
935 (errcode(ERRCODE_DATATYPE_MISMATCH),
936 errmsg("first argument must be a rowtype")));
937
938 if (PG_ARGISNULL(0))
939 {
940 if (PG_ARGISNULL(1))
941 PG_RETURN_NULL();
942
943 rec = NULL;
944
945 /*
946 * have no tuple to look at, so the only source of type info is the
947 * argtype. The lookup_rowtype_tupdesc call below will error out if we
948 * don't have a known composite type oid here.
949 */
950 tupType = argtype;
951 tupTypmod = -1;
952 }
953 else
954 {
955 rec = PG_GETARG_HEAPTUPLEHEADER(0);
956
957 if (PG_ARGISNULL(1))
958 PG_RETURN_POINTER(rec);
959
960 /* Extract type info from the tuple itself */
961 tupType = HeapTupleHeaderGetTypeId(rec);
962 tupTypmod = HeapTupleHeaderGetTypMod(rec);
963 }
964
965 hs = PG_GETARG_HS(1);
966 entries = ARRPTR(hs);
967 ptr = STRPTR(hs);
968
969 /*
970 * if the input hstore is empty, we can only skip the rest if we were
971 * passed in a non-null record, since otherwise there may be issues with
972 * domain nulls.
973 */
974
975 if (HS_COUNT(hs) == 0 && rec)
976 PG_RETURN_POINTER(rec);
977
978 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
979 ncolumns = tupdesc->natts;
980
981 if (rec)
982 {
983 /* Build a temporary HeapTuple control structure */
984 tuple.t_len = HeapTupleHeaderGetDatumLength(rec);
985 ItemPointerSetInvalid(&(tuple.t_self));
986 tuple.t_tableOid = InvalidOid;
987 tuple.t_data = rec;
988 }
989
990 /*
991 * We arrange to look up the needed I/O info just once per series of
992 * calls, assuming the record type doesn't change underneath us.
993 */
994 my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
995 if (my_extra == NULL ||
996 my_extra->ncolumns != ncolumns)
997 {
998 fcinfo->flinfo->fn_extra =
999 MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
1000 offsetof(RecordIOData, columns) +
1001 ncolumns * sizeof(ColumnIOData));
1002 my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
1003 my_extra->record_type = InvalidOid;
1004 my_extra->record_typmod = 0;
1005 }
1006
1007 if (my_extra->record_type != tupType ||
1008 my_extra->record_typmod != tupTypmod)
1009 {
1010 MemSet(my_extra, 0,
1011 offsetof(RecordIOData, columns) +
1012 ncolumns * sizeof(ColumnIOData));
1013 my_extra->record_type = tupType;
1014 my_extra->record_typmod = tupTypmod;
1015 my_extra->ncolumns = ncolumns;
1016 }
1017
1018 values = (Datum *) palloc(ncolumns * sizeof(Datum));
1019 nulls = (bool *) palloc(ncolumns * sizeof(bool));
1020
1021 if (rec)
1022 {
1023 /* Break down the tuple into fields */
1024 heap_deform_tuple(&tuple, tupdesc, values, nulls);
1025 }
1026 else
1027 {
1028 for (i = 0; i < ncolumns; ++i)
1029 {
1030 values[i] = (Datum) 0;
1031 nulls[i] = true;
1032 }
1033 }
1034
1035 for (i = 0; i < ncolumns; ++i)
1036 {
1037 ColumnIOData *column_info = &my_extra->columns[i];
1038 Oid column_type = tupdesc->attrs[i]->atttypid;
1039 char *value;
1040 int idx;
1041 int vallen;
1042
1043 /* Ignore dropped columns in datatype */
1044 if (tupdesc->attrs[i]->attisdropped)
1045 {
1046 nulls[i] = true;
1047 continue;
1048 }
1049
1050 idx = hstoreFindKey(hs, 0,
1051 NameStr(tupdesc->attrs[i]->attname),
1052 strlen(NameStr(tupdesc->attrs[i]->attname)));
1053
1054 /*
1055 * we can't just skip here if the key wasn't found since we might have
1056 * a domain to deal with. If we were passed in a non-null record
1057 * datum, we assume that the existing values are valid (if they're
1058 * not, then it's not our fault), but if we were passed in a null,
1059 * then every field which we don't populate needs to be run through
1060 * the input function just in case it's a domain type.
1061 */
1062 if (idx < 0 && rec)
1063 continue;
1064
1065 /*
1066 * Prepare to convert the column value from text
1067 */
1068 if (column_info->column_type != column_type)
1069 {
1070 getTypeInputInfo(column_type,
1071 &column_info->typiofunc,
1072 &column_info->typioparam);
1073 fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
1074 fcinfo->flinfo->fn_mcxt);
1075 column_info->column_type = column_type;
1076 }
1077
1078 if (idx < 0 || HSTORE_VALISNULL(entries, idx))
1079 {
1080 /*
1081 * need InputFunctionCall to happen even for nulls, so that domain
1082 * checks are done
1083 */
1084 values[i] = InputFunctionCall(&column_info->proc, NULL,
1085 column_info->typioparam,
1086 tupdesc->attrs[i]->atttypmod);
1087 nulls[i] = true;
1088 }
1089 else
1090 {
1091 vallen = HSTORE_VALLEN(entries, idx);
1092 value = palloc(1 + vallen);
1093 memcpy(value, HSTORE_VAL(entries, ptr, idx), vallen);
1094 value[vallen] = 0;
1095
1096 values[i] = InputFunctionCall(&column_info->proc, value,
1097 column_info->typioparam,
1098 tupdesc->attrs[i]->atttypmod);
1099 nulls[i] = false;
1100 }
1101 }
1102
1103 rettuple = heap_form_tuple(tupdesc, values, nulls);
1104
1105 ReleaseTupleDesc(tupdesc);
1106
1107 PG_RETURN_DATUM(HeapTupleGetDatum(rettuple));
1108 }
1109
1110
1111 static char *
cpw(char * dst,char * src,int len)1112 cpw(char *dst, char *src, int len)
1113 {
1114 char *ptr = src;
1115
1116 while (ptr - src < len)
1117 {
1118 if (*ptr == '"' || *ptr == '\\')
1119 *dst++ = '\\';
1120 *dst++ = *ptr++;
1121 }
1122 return dst;
1123 }
1124
1125 PG_FUNCTION_INFO_V1(hstore_out);
1126 Datum
hstore_out(PG_FUNCTION_ARGS)1127 hstore_out(PG_FUNCTION_ARGS)
1128 {
1129 HStore *in = PG_GETARG_HS(0);
1130 int buflen,
1131 i;
1132 int count = HS_COUNT(in);
1133 char *out,
1134 *ptr;
1135 char *base = STRPTR(in);
1136 HEntry *entries = ARRPTR(in);
1137
1138 if (count == 0)
1139 PG_RETURN_CSTRING(pstrdup(""));
1140
1141 buflen = 0;
1142
1143 /*
1144 * this loop overestimates due to pessimistic assumptions about escaping,
1145 * so very large hstore values can't be output. this could be fixed, but
1146 * many other data types probably have the same issue. This replaced code
1147 * that used the original varlena size for calculations, which was wrong
1148 * in some subtle ways.
1149 */
1150
1151 for (i = 0; i < count; i++)
1152 {
1153 /* include "" and => and comma-space */
1154 buflen += 6 + 2 * HSTORE_KEYLEN(entries, i);
1155 /* include "" only if nonnull */
1156 buflen += 2 + (HSTORE_VALISNULL(entries, i)
1157 ? 2
1158 : 2 * HSTORE_VALLEN(entries, i));
1159 }
1160
1161 out = ptr = palloc(buflen);
1162
1163 for (i = 0; i < count; i++)
1164 {
1165 *ptr++ = '"';
1166 ptr = cpw(ptr, HSTORE_KEY(entries, base, i), HSTORE_KEYLEN(entries, i));
1167 *ptr++ = '"';
1168 *ptr++ = '=';
1169 *ptr++ = '>';
1170 if (HSTORE_VALISNULL(entries, i))
1171 {
1172 *ptr++ = 'N';
1173 *ptr++ = 'U';
1174 *ptr++ = 'L';
1175 *ptr++ = 'L';
1176 }
1177 else
1178 {
1179 *ptr++ = '"';
1180 ptr = cpw(ptr, HSTORE_VAL(entries, base, i), HSTORE_VALLEN(entries, i));
1181 *ptr++ = '"';
1182 }
1183
1184 if (i + 1 != count)
1185 {
1186 *ptr++ = ',';
1187 *ptr++ = ' ';
1188 }
1189 }
1190 *ptr = '\0';
1191
1192 PG_RETURN_CSTRING(out);
1193 }
1194
1195
1196 PG_FUNCTION_INFO_V1(hstore_send);
1197 Datum
hstore_send(PG_FUNCTION_ARGS)1198 hstore_send(PG_FUNCTION_ARGS)
1199 {
1200 HStore *in = PG_GETARG_HS(0);
1201 int i;
1202 int count = HS_COUNT(in);
1203 char *base = STRPTR(in);
1204 HEntry *entries = ARRPTR(in);
1205 StringInfoData buf;
1206
1207 pq_begintypsend(&buf);
1208
1209 pq_sendint(&buf, count, 4);
1210
1211 for (i = 0; i < count; i++)
1212 {
1213 int32 keylen = HSTORE_KEYLEN(entries, i);
1214
1215 pq_sendint(&buf, keylen, 4);
1216 pq_sendtext(&buf, HSTORE_KEY(entries, base, i), keylen);
1217 if (HSTORE_VALISNULL(entries, i))
1218 {
1219 pq_sendint(&buf, -1, 4);
1220 }
1221 else
1222 {
1223 int32 vallen = HSTORE_VALLEN(entries, i);
1224
1225 pq_sendint(&buf, vallen, 4);
1226 pq_sendtext(&buf, HSTORE_VAL(entries, base, i), vallen);
1227 }
1228 }
1229
1230 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
1231 }
1232
1233
1234 /*
1235 * hstore_to_json_loose
1236 *
1237 * This is a heuristic conversion to json which treats
1238 * 't' and 'f' as booleans and strings that look like numbers as numbers,
1239 * as long as they don't start with a leading zero followed by another digit
1240 * (think zip codes or phone numbers starting with 0).
1241 */
1242 PG_FUNCTION_INFO_V1(hstore_to_json_loose);
1243 Datum
hstore_to_json_loose(PG_FUNCTION_ARGS)1244 hstore_to_json_loose(PG_FUNCTION_ARGS)
1245 {
1246 HStore *in = PG_GETARG_HS(0);
1247 int i;
1248 int count = HS_COUNT(in);
1249 char *base = STRPTR(in);
1250 HEntry *entries = ARRPTR(in);
1251 StringInfoData tmp,
1252 dst;
1253
1254 if (count == 0)
1255 PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
1256
1257 initStringInfo(&tmp);
1258 initStringInfo(&dst);
1259
1260 appendStringInfoChar(&dst, '{');
1261
1262 for (i = 0; i < count; i++)
1263 {
1264 resetStringInfo(&tmp);
1265 appendBinaryStringInfo(&tmp, HSTORE_KEY(entries, base, i),
1266 HSTORE_KEYLEN(entries, i));
1267 escape_json(&dst, tmp.data);
1268 appendStringInfoString(&dst, ": ");
1269 if (HSTORE_VALISNULL(entries, i))
1270 appendStringInfoString(&dst, "null");
1271 /* guess that values of 't' or 'f' are booleans */
1272 else if (HSTORE_VALLEN(entries, i) == 1 &&
1273 *(HSTORE_VAL(entries, base, i)) == 't')
1274 appendStringInfoString(&dst, "true");
1275 else if (HSTORE_VALLEN(entries, i) == 1 &&
1276 *(HSTORE_VAL(entries, base, i)) == 'f')
1277 appendStringInfoString(&dst, "false");
1278 else
1279 {
1280 resetStringInfo(&tmp);
1281 appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
1282 HSTORE_VALLEN(entries, i));
1283 if (IsValidJsonNumber(tmp.data, tmp.len))
1284 appendBinaryStringInfo(&dst, tmp.data, tmp.len);
1285 else
1286 escape_json(&dst, tmp.data);
1287 }
1288
1289 if (i + 1 != count)
1290 appendStringInfoString(&dst, ", ");
1291 }
1292 appendStringInfoChar(&dst, '}');
1293
1294 PG_RETURN_TEXT_P(cstring_to_text(dst.data));
1295 }
1296
1297 PG_FUNCTION_INFO_V1(hstore_to_json);
1298 Datum
hstore_to_json(PG_FUNCTION_ARGS)1299 hstore_to_json(PG_FUNCTION_ARGS)
1300 {
1301 HStore *in = PG_GETARG_HS(0);
1302 int i;
1303 int count = HS_COUNT(in);
1304 char *base = STRPTR(in);
1305 HEntry *entries = ARRPTR(in);
1306 StringInfoData tmp,
1307 dst;
1308
1309 if (count == 0)
1310 PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
1311
1312 initStringInfo(&tmp);
1313 initStringInfo(&dst);
1314
1315 appendStringInfoChar(&dst, '{');
1316
1317 for (i = 0; i < count; i++)
1318 {
1319 resetStringInfo(&tmp);
1320 appendBinaryStringInfo(&tmp, HSTORE_KEY(entries, base, i),
1321 HSTORE_KEYLEN(entries, i));
1322 escape_json(&dst, tmp.data);
1323 appendStringInfoString(&dst, ": ");
1324 if (HSTORE_VALISNULL(entries, i))
1325 appendStringInfoString(&dst, "null");
1326 else
1327 {
1328 resetStringInfo(&tmp);
1329 appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
1330 HSTORE_VALLEN(entries, i));
1331 escape_json(&dst, tmp.data);
1332 }
1333
1334 if (i + 1 != count)
1335 appendStringInfoString(&dst, ", ");
1336 }
1337 appendStringInfoChar(&dst, '}');
1338
1339 PG_RETURN_TEXT_P(cstring_to_text(dst.data));
1340 }
1341
1342 PG_FUNCTION_INFO_V1(hstore_to_jsonb);
1343 Datum
hstore_to_jsonb(PG_FUNCTION_ARGS)1344 hstore_to_jsonb(PG_FUNCTION_ARGS)
1345 {
1346 HStore *in = PG_GETARG_HS(0);
1347 int i;
1348 int count = HS_COUNT(in);
1349 char *base = STRPTR(in);
1350 HEntry *entries = ARRPTR(in);
1351 JsonbParseState *state = NULL;
1352 JsonbValue *res;
1353
1354 (void) pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
1355
1356 for (i = 0; i < count; i++)
1357 {
1358 JsonbValue key,
1359 val;
1360
1361 key.type = jbvString;
1362 key.val.string.len = HSTORE_KEYLEN(entries, i);
1363 key.val.string.val = HSTORE_KEY(entries, base, i);
1364
1365 (void) pushJsonbValue(&state, WJB_KEY, &key);
1366
1367 if (HSTORE_VALISNULL(entries, i))
1368 {
1369 val.type = jbvNull;
1370 }
1371 else
1372 {
1373 val.type = jbvString;
1374 val.val.string.len = HSTORE_VALLEN(entries, i);
1375 val.val.string.val = HSTORE_VAL(entries, base, i);
1376 }
1377 (void) pushJsonbValue(&state, WJB_VALUE, &val);
1378 }
1379
1380 res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
1381
1382 PG_RETURN_POINTER(JsonbValueToJsonb(res));
1383 }
1384
1385 PG_FUNCTION_INFO_V1(hstore_to_jsonb_loose);
1386 Datum
hstore_to_jsonb_loose(PG_FUNCTION_ARGS)1387 hstore_to_jsonb_loose(PG_FUNCTION_ARGS)
1388 {
1389 HStore *in = PG_GETARG_HS(0);
1390 int i;
1391 int count = HS_COUNT(in);
1392 char *base = STRPTR(in);
1393 HEntry *entries = ARRPTR(in);
1394 JsonbParseState *state = NULL;
1395 JsonbValue *res;
1396 StringInfoData tmp;
1397
1398 initStringInfo(&tmp);
1399
1400 (void) pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
1401
1402 for (i = 0; i < count; i++)
1403 {
1404 JsonbValue key,
1405 val;
1406
1407 key.type = jbvString;
1408 key.val.string.len = HSTORE_KEYLEN(entries, i);
1409 key.val.string.val = HSTORE_KEY(entries, base, i);
1410
1411 (void) pushJsonbValue(&state, WJB_KEY, &key);
1412
1413 if (HSTORE_VALISNULL(entries, i))
1414 {
1415 val.type = jbvNull;
1416 }
1417 /* guess that values of 't' or 'f' are booleans */
1418 else if (HSTORE_VALLEN(entries, i) == 1 &&
1419 *(HSTORE_VAL(entries, base, i)) == 't')
1420 {
1421 val.type = jbvBool;
1422 val.val.boolean = true;
1423 }
1424 else if (HSTORE_VALLEN(entries, i) == 1 &&
1425 *(HSTORE_VAL(entries, base, i)) == 'f')
1426 {
1427 val.type = jbvBool;
1428 val.val.boolean = false;
1429 }
1430 else
1431 {
1432 resetStringInfo(&tmp);
1433 appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
1434 HSTORE_VALLEN(entries, i));
1435 if (IsValidJsonNumber(tmp.data, tmp.len))
1436 {
1437 val.type = jbvNumeric;
1438 val.val.numeric = DatumGetNumeric(
1439 DirectFunctionCall3(numeric_in,
1440 CStringGetDatum(tmp.data), 0, -1));
1441 }
1442 else
1443 {
1444 val.type = jbvString;
1445 val.val.string.len = HSTORE_VALLEN(entries, i);
1446 val.val.string.val = HSTORE_VAL(entries, base, i);
1447 }
1448 }
1449 (void) pushJsonbValue(&state, WJB_VALUE, &val);
1450 }
1451
1452 res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
1453
1454 PG_RETURN_POINTER(JsonbValueToJsonb(res));
1455 }
1456