aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/jsonb_op.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils/adt/jsonb_op.c')
-rw-r--r--src/backend/utils/adt/jsonb_op.c106
1 files changed, 52 insertions, 54 deletions
diff --git a/src/backend/utils/adt/jsonb_op.c b/src/backend/utils/adt/jsonb_op.c
index 38bd5676739..2d071b2523b 100644
--- a/src/backend/utils/adt/jsonb_op.c
+++ b/src/backend/utils/adt/jsonb_op.c
@@ -13,6 +13,7 @@
*/
#include "postgres.h"
+#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "utils/jsonb.h"
@@ -34,10 +35,9 @@ jsonb_exists(PG_FUNCTION_ARGS)
kval.val.string.val = VARDATA_ANY(key);
kval.val.string.len = VARSIZE_ANY_EXHDR(key);
- v = findJsonbValueFromSuperHeader(VARDATA(jb),
- JB_FOBJECT | JB_FARRAY,
- NULL,
- &kval);
+ v = findJsonbValueFromContainer(&jb->root,
+ JB_FOBJECT | JB_FARRAY,
+ &kval);
PG_RETURN_BOOL(v != NULL);
}
@@ -47,29 +47,28 @@ jsonb_exists_any(PG_FUNCTION_ARGS)
{
Jsonb *jb = PG_GETARG_JSONB(0);
ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1);
- JsonbValue *arrKey = arrayToJsonbSortedArray(keys);
- uint32 *plowbound = NULL,
- lowbound = 0;
int i;
+ Datum *key_datums;
+ bool *key_nulls;
+ int elem_count;
- if (arrKey == NULL || arrKey->val.object.nPairs == 0)
- PG_RETURN_BOOL(false);
-
- if (JB_ROOT_IS_OBJECT(jb))
- plowbound = &lowbound;
+ deconstruct_array(keys, TEXTOID, -1, false, 'i', &key_datums, &key_nulls,
+ &elem_count);
- /*
- * We exploit the fact that the pairs list is already sorted into strictly
- * increasing order to narrow the findJsonbValueFromSuperHeader search;
- * each search can start one entry past the previous "found" entry, or at
- * the lower bound of the last search.
- */
- for (i = 0; i < arrKey->val.array.nElems; i++)
+ for (i = 0; i < elem_count; i++)
{
- if (findJsonbValueFromSuperHeader(VARDATA(jb),
- JB_FOBJECT | JB_FARRAY,
- plowbound,
- arrKey->val.array.elems + i) != NULL)
+ JsonbValue strVal;
+
+ if (key_nulls[i])
+ continue;
+
+ strVal.type = jbvString;
+ strVal.val.string.val = VARDATA(key_datums[i]);
+ strVal.val.string.len = VARSIZE(key_datums[i]) - VARHDRSZ;
+
+ if (findJsonbValueFromContainer(&jb->root,
+ JB_FOBJECT | JB_FARRAY,
+ &strVal) != NULL)
PG_RETURN_BOOL(true);
}
@@ -81,29 +80,28 @@ jsonb_exists_all(PG_FUNCTION_ARGS)
{
Jsonb *jb = PG_GETARG_JSONB(0);
ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1);
- JsonbValue *arrKey = arrayToJsonbSortedArray(keys);
- uint32 *plowbound = NULL;
- uint32 lowbound = 0;
int i;
+ Datum *key_datums;
+ bool *key_nulls;
+ int elem_count;
- if (arrKey == NULL || arrKey->val.array.nElems == 0)
- PG_RETURN_BOOL(true);
+ deconstruct_array(keys, TEXTOID, -1, false, 'i', &key_datums, &key_nulls,
+ &elem_count);
- if (JB_ROOT_IS_OBJECT(jb))
- plowbound = &lowbound;
-
- /*
- * We exploit the fact that the pairs list is already sorted into strictly
- * increasing order to narrow the findJsonbValueFromSuperHeader search;
- * each search can start one entry past the previous "found" entry, or at
- * the lower bound of the last search.
- */
- for (i = 0; i < arrKey->val.array.nElems; i++)
+ for (i = 0; i < elem_count; i++)
{
- if (findJsonbValueFromSuperHeader(VARDATA(jb),
- JB_FOBJECT | JB_FARRAY,
- plowbound,
- arrKey->val.array.elems + i) == NULL)
+ JsonbValue strVal;
+
+ if (key_nulls[i])
+ continue;
+
+ strVal.type = jbvString;
+ strVal.val.string.val = VARDATA(key_datums[i]);
+ strVal.val.string.len = VARSIZE(key_datums[i]) - VARHDRSZ;
+
+ if (findJsonbValueFromContainer(&jb->root,
+ JB_FOBJECT | JB_FARRAY,
+ &strVal) == NULL)
PG_RETURN_BOOL(false);
}
@@ -123,8 +121,8 @@ jsonb_contains(PG_FUNCTION_ARGS)
JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl))
PG_RETURN_BOOL(false);
- it1 = JsonbIteratorInit(VARDATA(val));
- it2 = JsonbIteratorInit(VARDATA(tmpl));
+ it1 = JsonbIteratorInit(&val->root);
+ it2 = JsonbIteratorInit(&tmpl->root);
PG_RETURN_BOOL(JsonbDeepContains(&it1, &it2));
}
@@ -143,8 +141,8 @@ jsonb_contained(PG_FUNCTION_ARGS)
JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl))
PG_RETURN_BOOL(false);
- it1 = JsonbIteratorInit(VARDATA(val));
- it2 = JsonbIteratorInit(VARDATA(tmpl));
+ it1 = JsonbIteratorInit(&val->root);
+ it2 = JsonbIteratorInit(&tmpl->root);
PG_RETURN_BOOL(JsonbDeepContains(&it1, &it2));
}
@@ -156,7 +154,7 @@ jsonb_ne(PG_FUNCTION_ARGS)
Jsonb *jbb = PG_GETARG_JSONB(1);
bool res;
- res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) != 0);
+ res = (compareJsonbContainers(&jba->root, &jbb->root) != 0);
PG_FREE_IF_COPY(jba, 0);
PG_FREE_IF_COPY(jbb, 1);
@@ -173,7 +171,7 @@ jsonb_lt(PG_FUNCTION_ARGS)
Jsonb *jbb = PG_GETARG_JSONB(1);
bool res;
- res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) < 0);
+ res = (compareJsonbContainers(&jba->root, &jbb->root) < 0);
PG_FREE_IF_COPY(jba, 0);
PG_FREE_IF_COPY(jbb, 1);
@@ -187,7 +185,7 @@ jsonb_gt(PG_FUNCTION_ARGS)
Jsonb *jbb = PG_GETARG_JSONB(1);
bool res;
- res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) > 0);
+ res = (compareJsonbContainers(&jba->root, &jbb->root) > 0);
PG_FREE_IF_COPY(jba, 0);
PG_FREE_IF_COPY(jbb, 1);
@@ -201,7 +199,7 @@ jsonb_le(PG_FUNCTION_ARGS)
Jsonb *jbb = PG_GETARG_JSONB(1);
bool res;
- res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) <= 0);
+ res = (compareJsonbContainers(&jba->root, &jbb->root) <= 0);
PG_FREE_IF_COPY(jba, 0);
PG_FREE_IF_COPY(jbb, 1);
@@ -215,7 +213,7 @@ jsonb_ge(PG_FUNCTION_ARGS)
Jsonb *jbb = PG_GETARG_JSONB(1);
bool res;
- res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) >= 0);
+ res = (compareJsonbContainers(&jba->root, &jbb->root) >= 0);
PG_FREE_IF_COPY(jba, 0);
PG_FREE_IF_COPY(jbb, 1);
@@ -229,7 +227,7 @@ jsonb_eq(PG_FUNCTION_ARGS)
Jsonb *jbb = PG_GETARG_JSONB(1);
bool res;
- res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) == 0);
+ res = (compareJsonbContainers(&jba->root, &jbb->root) == 0);
PG_FREE_IF_COPY(jba, 0);
PG_FREE_IF_COPY(jbb, 1);
@@ -243,7 +241,7 @@ jsonb_cmp(PG_FUNCTION_ARGS)
Jsonb *jbb = PG_GETARG_JSONB(1);
int res;
- res = compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb));
+ res = compareJsonbContainers(&jba->root, &jbb->root);
PG_FREE_IF_COPY(jba, 0);
PG_FREE_IF_COPY(jbb, 1);
@@ -265,7 +263,7 @@ jsonb_hash(PG_FUNCTION_ARGS)
if (JB_ROOT_COUNT(jb) == 0)
PG_RETURN_INT32(0);
- it = JsonbIteratorInit(VARDATA(jb));
+ it = JsonbIteratorInit(&jb->root);
while ((r = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
{