mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
IS JSON predicate
This patch intrdocuces the SQL standard IS JSON predicate. It operates on text and bytea values representing JSON as well as on the json and jsonb types. Each test has an IS and IS NOT variant. The tests are: IS JSON [VALUE] IS JSON ARRAY IS JSON OBJECT IS JSON SCALAR IS JSON WITH | WITHOUT UNIQUE KEYS These are mostly self-explanatory, but note that IS JSON WITHOUT UNIQUE KEYS is true whenever IS JSON is true, and IS JSON WITH UNIQUE KEYS is true whenever IS JSON is true except it IS JSON OBJECT is true and there are duplicate keys (which is never the case when applied to jsonb values). Nikita Glukhov Reviewers have included (in no particular order) Andres Freund, Alexander Korotkov, Pavel Stehule, Andrew Alsup, Erik Rijkers, Zihong Yu, Himanshu Upadhyaya, Daniel Gustafsson, Justin Pryzby. Discussion: https://postgr.es/m/cd0bb935-0158-78a7-08b5-904886deac4b@postgrespro.ru
This commit is contained in:
@@ -2491,6 +2491,23 @@ _copyJsonArrayQueryConstructor(const JsonArrayQueryConstructor *from)
|
||||
return newnode;
|
||||
}
|
||||
|
||||
/*
|
||||
* _copyJsonIsPredicate
|
||||
*/
|
||||
static JsonIsPredicate *
|
||||
_copyJsonIsPredicate(const JsonIsPredicate *from)
|
||||
{
|
||||
JsonIsPredicate *newnode = makeNode(JsonIsPredicate);
|
||||
|
||||
COPY_NODE_FIELD(expr);
|
||||
COPY_SCALAR_FIELD(format);
|
||||
COPY_SCALAR_FIELD(value_type);
|
||||
COPY_SCALAR_FIELD(unique_keys);
|
||||
COPY_LOCATION_FIELD(location);
|
||||
|
||||
return newnode;
|
||||
}
|
||||
|
||||
/* ****************************************************************
|
||||
* pathnodes.h copy functions
|
||||
*
|
||||
@@ -5625,6 +5642,9 @@ copyObjectImpl(const void *from)
|
||||
case T_JsonArrayAgg:
|
||||
retval = _copyJsonArrayAgg(from);
|
||||
break;
|
||||
case T_JsonIsPredicate:
|
||||
retval = _copyJsonIsPredicate(from);
|
||||
break;
|
||||
|
||||
/*
|
||||
* RELATION NODES
|
||||
|
||||
@@ -976,6 +976,18 @@ _equalJsonArrayQueryConstructor(const JsonArrayQueryConstructor *a,
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
_equalJsonIsPredicate(const JsonIsPredicate *a,
|
||||
const JsonIsPredicate *b)
|
||||
{
|
||||
COMPARE_NODE_FIELD(expr);
|
||||
COMPARE_SCALAR_FIELD(value_type);
|
||||
COMPARE_SCALAR_FIELD(unique_keys);
|
||||
COMPARE_LOCATION_FIELD(location);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Stuff from pathnodes.h
|
||||
*/
|
||||
@@ -3546,6 +3558,9 @@ equal(const void *a, const void *b)
|
||||
case T_JsonConstructorExpr:
|
||||
retval = _equalJsonConstructorExpr(a, b);
|
||||
break;
|
||||
case T_JsonIsPredicate:
|
||||
retval = _equalJsonIsPredicate(a, b);
|
||||
break;
|
||||
|
||||
/*
|
||||
* RELATION NODES
|
||||
|
||||
@@ -887,3 +887,22 @@ makeJsonKeyValue(Node *key, Node *value)
|
||||
|
||||
return (Node *) n;
|
||||
}
|
||||
|
||||
/*
|
||||
* makeJsonIsPredicate -
|
||||
* creates a JsonIsPredicate node
|
||||
*/
|
||||
Node *
|
||||
makeJsonIsPredicate(Node *expr, JsonFormat *format, JsonValueType value_type,
|
||||
bool unique_keys, int location)
|
||||
{
|
||||
JsonIsPredicate *n = makeNode(JsonIsPredicate);
|
||||
|
||||
n->expr = expr;
|
||||
n->format = format;
|
||||
n->value_type = value_type;
|
||||
n->unique_keys = unique_keys;
|
||||
n->location = location;
|
||||
|
||||
return (Node *) n;
|
||||
}
|
||||
|
||||
@@ -260,6 +260,9 @@ exprType(const Node *expr)
|
||||
case T_JsonConstructorExpr:
|
||||
type = ((const JsonConstructorExpr *) expr)->returning->typid;
|
||||
break;
|
||||
case T_JsonIsPredicate:
|
||||
type = BOOLOID;
|
||||
break;
|
||||
default:
|
||||
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
|
||||
type = InvalidOid; /* keep compiler quiet */
|
||||
@@ -985,6 +988,9 @@ exprCollation(const Node *expr)
|
||||
coll = InvalidOid;
|
||||
}
|
||||
break;
|
||||
case T_JsonIsPredicate:
|
||||
coll = InvalidOid; /* result is always an boolean type */
|
||||
break;
|
||||
default:
|
||||
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
|
||||
coll = InvalidOid; /* keep compiler quiet */
|
||||
@@ -1211,6 +1217,9 @@ exprSetCollation(Node *expr, Oid collation)
|
||||
Assert(!OidIsValid(collation)); /* result is always a json[b] type */
|
||||
}
|
||||
break;
|
||||
case T_JsonIsPredicate:
|
||||
Assert(!OidIsValid(collation)); /* result is always boolean */
|
||||
break;
|
||||
default:
|
||||
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
|
||||
break;
|
||||
@@ -1663,6 +1672,9 @@ exprLocation(const Node *expr)
|
||||
case T_JsonConstructorExpr:
|
||||
loc = ((const JsonConstructorExpr *) expr)->location;
|
||||
break;
|
||||
case T_JsonIsPredicate:
|
||||
loc = ((const JsonIsPredicate *) expr)->location;
|
||||
break;
|
||||
default:
|
||||
/* for any other node type it's just unknown... */
|
||||
loc = -1;
|
||||
@@ -2429,6 +2441,8 @@ expression_tree_walker(Node *node,
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case T_JsonIsPredicate:
|
||||
return walker(((JsonIsPredicate *) node)->expr, context);
|
||||
default:
|
||||
elog(ERROR, "unrecognized node type: %d",
|
||||
(int) nodeTag(node));
|
||||
@@ -3438,6 +3452,16 @@ expression_tree_mutator(Node *node,
|
||||
MUTATE(newnode->coercion, jve->coercion, Expr *);
|
||||
MUTATE(newnode->returning, jve->returning, JsonReturning *);
|
||||
|
||||
return (Node *) newnode;
|
||||
}
|
||||
case T_JsonIsPredicate:
|
||||
{
|
||||
JsonIsPredicate *pred = (JsonIsPredicate *) node;
|
||||
JsonIsPredicate *newnode;
|
||||
|
||||
FLATCOPY(newnode, pred, JsonIsPredicate);
|
||||
MUTATE(newnode->expr, pred->expr, Node *);
|
||||
|
||||
return (Node *) newnode;
|
||||
}
|
||||
default:
|
||||
@@ -4290,6 +4314,8 @@ raw_expression_tree_walker(Node *node,
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case T_JsonIsPredicate:
|
||||
return walker(((JsonIsPredicate *) node)->expr, context);
|
||||
default:
|
||||
elog(ERROR, "unrecognized node type: %d",
|
||||
(int) nodeTag(node));
|
||||
|
||||
@@ -1797,6 +1797,17 @@ _outJsonConstructorExpr(StringInfo str, const JsonConstructorExpr *node)
|
||||
WRITE_LOCATION_FIELD(location);
|
||||
}
|
||||
|
||||
static void
|
||||
_outJsonIsPredicate(StringInfo str, const JsonIsPredicate *node)
|
||||
{
|
||||
WRITE_NODE_TYPE("JSONISPREDICATE");
|
||||
|
||||
WRITE_NODE_FIELD(expr);
|
||||
WRITE_ENUM_FIELD(value_type, JsonValueType);
|
||||
WRITE_BOOL_FIELD(unique_keys);
|
||||
WRITE_LOCATION_FIELD(location);
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
* Stuff from pathnodes.h.
|
||||
@@ -4630,6 +4641,9 @@ outNode(StringInfo str, const void *obj)
|
||||
case T_JsonConstructorExpr:
|
||||
_outJsonConstructorExpr(str, obj);
|
||||
break;
|
||||
case T_JsonIsPredicate:
|
||||
_outJsonIsPredicate(str, obj);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
|
||||
@@ -1492,6 +1492,22 @@ _readJsonConstructorExpr(void)
|
||||
READ_DONE();
|
||||
}
|
||||
|
||||
/*
|
||||
* _readJsonIsPredicate
|
||||
*/
|
||||
static JsonIsPredicate *
|
||||
_readJsonIsPredicate()
|
||||
{
|
||||
READ_LOCALS(JsonIsPredicate);
|
||||
|
||||
READ_NODE_FIELD(expr);
|
||||
READ_ENUM_FIELD(value_type, JsonValueType);
|
||||
READ_BOOL_FIELD(unique_keys);
|
||||
READ_LOCATION_FIELD(location);
|
||||
|
||||
READ_DONE();
|
||||
}
|
||||
|
||||
/*
|
||||
* Stuff from pathnodes.h.
|
||||
*
|
||||
@@ -3090,6 +3106,8 @@ parseNodeString(void)
|
||||
return_value = _readJsonValueExpr();
|
||||
else if (MATCH("JSONCTOREXPR", 12))
|
||||
return_value = _readJsonConstructorExpr();
|
||||
else if (MATCH("JSONISPREDICATE", 15))
|
||||
return_value = _readJsonIsPredicate();
|
||||
else
|
||||
{
|
||||
elog(ERROR, "badly formatted node string \"%.32s\"...", token);
|
||||
|
||||
Reference in New Issue
Block a user