Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Supports VERTICES, EDGES on cypher list predicate functions. #603

Merged
merged 6 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions src/backend/executor/execExpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
#include "parser/parse_type.h"
#include "utils/fmgroids.h"
#include "utils/syscache.h"


typedef struct LastAttnumInfo
Expand Down Expand Up @@ -4163,8 +4166,16 @@ ExecInitCypherListComp(ExprEvalStep *scratch, CypherListCompExpr *listcompexpr,
ExprEvalStep elem_step;
ExprEvalStep loop_step;
int end_stepno;
bool is_array_type;

Assert(exprType((Node *) listcompexpr->list) == JSONBOID);
Type targetType;
Form_pg_type typeForm;

targetType = typeidType(exprType((const Node *) listcompexpr->list));
typeForm = (Form_pg_type) GETSTRUCT(targetType);

is_array_type = IsTrueArrayType(typeForm);
ReleaseSysCache(targetType);

ExecInitExprRec(listcompexpr->list, state, scratch->resvalue,
scratch->resnull);
Expand All @@ -4186,8 +4197,24 @@ ExecInitCypherListComp(ExprEvalStep *scratch, CypherListCompExpr *listcompexpr,
init_step.opcode = EEOP_CYPHERLISTCOMP_ITER_INIT;
init_step.d.cypherlistcomp_iter.listvalue = scratch->resvalue;
init_step.d.cypherlistcomp_iter.listnull = scratch->resnull;
init_step.d.cypherlistcomp_iter.listiter =
(JsonbIterator **) palloc(sizeof(JsonbIterator *));

init_step.d.cypherlistcomp_iter.is_null_list_or_array =
(bool *) palloc(sizeof(bool));
*init_step.d.cypherlistcomp_iter.is_null_list_or_array = false;

if (is_array_type)
{
init_step.d.cypherlistcomp_iter.jsonb_list_iterator = NULL;
init_step.d.cypherlistcomp_iter.array_iterator =
(CypherListCompArrayIterator *) palloc(sizeof(CypherListCompArrayIterator));
}
else
{
init_step.d.cypherlistcomp_iter.jsonb_list_iterator =
(JsonbIterator **) palloc(sizeof(JsonbIterator *));
init_step.d.cypherlistcomp_iter.array_iterator = NULL;
}

ExprEvalPushStep(state, &init_step);

elem_resvalue = (Datum *) palloc(sizeof(Datum));
Expand All @@ -4196,8 +4223,13 @@ ExecInitCypherListComp(ExprEvalStep *scratch, CypherListCompExpr *listcompexpr,
next_step.opcode = EEOP_CYPHERLISTCOMP_ITER_NEXT;
next_step.resvalue = elem_resvalue;
next_step.resnull = elem_resnull;
next_step.d.cypherlistcomp_iter.listiter =
init_step.d.cypherlistcomp_iter.listiter;
next_step.d.cypherlistcomp_iter.is_null_list_or_array =
init_step.d.cypherlistcomp_iter.is_null_list_or_array;
next_step.d.cypherlistcomp_iter.jsonb_list_iterator =
init_step.d.cypherlistcomp_iter.jsonb_list_iterator;
next_step.d.cypherlistcomp_iter.array_iterator =
init_step.d.cypherlistcomp_iter.array_iterator;

ExprEvalPushStep(state, &next_step);
next_stepno = state->steps_len - 1;

Expand Down
110 changes: 84 additions & 26 deletions src/backend/executor/execExprInterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5277,43 +5277,101 @@ ExecEvalCypherListCompEnd(ExprState *state, ExprEvalStep *op)
void
ExecEvalCypherListCompIterInit(ExprState *state, ExprEvalStep *op)
{
Jsonb *listjb;
JsonbIterator **ji;
JsonbValue jv;
if (op->d.cypherlistcomp_iter.jsonb_list_iterator != NULL)
{
Jsonb *listjb;
JsonbIterator **ji;
JsonbValue jv;

Assert(!*op->d.cypherlistcomp_iter.listnull);
Assert(!*op->d.cypherlistcomp_iter.listnull);

listjb = DatumGetJsonbP(*op->d.cypherlistcomp_iter.listvalue);
if (!JB_ROOT_IS_ARRAY(listjb) || JB_ROOT_IS_SCALAR(listjb))
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("list is expected but %s",
JsonbToCString(NULL, &listjb->root,
VARSIZE(listjb)))));

ji = op->d.cypherlistcomp_iter.listiter;
*ji = JsonbIteratorInit(&listjb->root);
JsonbIteratorNext(ji, &jv, false);
listjb = DatumGetJsonbP(*op->d.cypherlistcomp_iter.listvalue);
*op->d.cypherlistcomp_iter.is_null_list_or_array = !JB_ROOT_IS_ARRAY(listjb) || JB_ROOT_IS_SCALAR(listjb);

ji = op->d.cypherlistcomp_iter.jsonb_list_iterator;
*ji = JsonbIteratorInit(&listjb->root);
JsonbIteratorNext(ji, &jv, false);
}
else
{
CypherListCompArrayIterator *cypher_array_iter = op->d.cypherlistcomp_iter.array_iterator;
array_iter *it = &cypher_array_iter->array_iter;
AnyArrayType *array = DatumGetAnyArrayP(*op->d.cypherlistcomp_iter.listvalue);

cypher_array_iter->array_size = ArrayGetNItems(AARR_NDIM(array),
AARR_DIMS(array));
cypher_array_iter->array_position = 0;
cypher_array_iter->array_typid = AARR_ELEMTYPE(array);

get_typlenbyvalalign(cypher_array_iter->array_typid,
&cypher_array_iter->typlen,
&cypher_array_iter->typbyval,
&cypher_array_iter->typalign);
array_iter_setup(it, array);
}
}

void
ExecEvalCypherListCompIterInitNext(ExprState *state, ExprEvalStep *op)
{
JsonbIterator **ji;
JsonbValue jv;
JsonbIteratorToken jt;

ji = op->d.cypherlistcomp_iter.listiter;
jt = JsonbIteratorNext(ji, &jv, true);
if (jt == WJB_ELEM)
if (op->d.cypherlistcomp_iter.jsonb_list_iterator != NULL)
{
*op->resvalue = JsonbPGetDatum(JsonbValueToJsonb(&jv));
*op->resnull = false;
JsonbIterator **ji;
JsonbValue jv;
JsonbIteratorToken jt;

if (*op->d.cypherlistcomp_iter.is_null_list_or_array)
{
*op->resvalue = (Datum) 0;
*op->resnull = true;
return;
}

ji = op->d.cypherlistcomp_iter.jsonb_list_iterator;
jt = JsonbIteratorNext(ji, &jv, true);
if (jt == WJB_ELEM)
{
*op->resvalue = JsonbPGetDatum(JsonbValueToJsonb(&jv));
*op->resnull = false;
}
else
{
*op->resvalue = (Datum) 0;
*op->resnull = true;
}
}
else
{
*op->resvalue = (Datum) 0;
*op->resnull = true;
CypherListCompArrayIterator *array_iter = op->d.cypherlistcomp_iter.array_iterator;
Datum vertex_or_edge_datum;

if (array_iter->array_position >= array_iter->array_size)
{
*op->resvalue = (Datum) 0;
*op->resnull = true;
return;
}

vertex_or_edge_datum = array_iter_next(&array_iter->array_iter,
op->resnull,
array_iter->array_position++,
array_iter->typlen,
array_iter->typbyval,
array_iter->typalign);

if (*op->resnull)
{
return;
}

if (array_iter->array_typid == VERTEXOID)
{
*op->resvalue = getVertexPropDatum(vertex_or_edge_datum);
}
else
{
*op->resvalue = getEdgePropDatum(vertex_or_edge_datum);
}
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/backend/parser/parse_cypher_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,15 @@ transformCypherListComp(ParseState *pstate, CypherListComp *clc)

list = transformCypherExprRecurse(pstate, (Node *) clc->list);
type = exprType(list);
if (type != JSONBOID)
{
list = coerce_all_to_jsonb(pstate, list);

switch (type) {
case JSONBOID:
case VERTEXARRAYOID:
case EDGEARRAYOID:
break;
default:
list = coerce_all_to_jsonb(pstate, list);
break;
}

save_varname = pstate->p_lc_varname;
Expand Down
27 changes: 27 additions & 0 deletions src/backend/utils/adt/cypher_empty_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,36 @@

#include "utils/cypher_empty_funcs.h"
#include "utils/fmgrprotos.h"
#include "utils/jsonb.h"

Datum
cypher_to_jsonb(PG_FUNCTION_ARGS)
{
return to_jsonb(fcinfo);
}

Datum
cypher_isempty_jsonb(PG_FUNCTION_ARGS)
{
Jsonb *jb = PG_GETARG_JSONB_P(0);

if (JB_ROOT_IS_SCALAR(jb))
{
JsonbValue *sjv;

sjv = getIthJsonbValueFromContainer(&jb->root, 0);
if (sjv->type == jbvString)
{
PG_RETURN_BOOL(sjv->val.string.len <= 0);
}
}
else if (JB_ROOT_IS_ARRAY(jb) || JB_ROOT_IS_OBJECT(jb))
{
PG_RETURN_BOOL(JB_ROOT_COUNT(jb) <= 0);
}

ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("isEmpty(): list or object or string is expected but %s",
JsonbToCString(NULL, &jb->root, VARSIZE(jb)))));
}
3 changes: 3 additions & 0 deletions src/include/catalog/pg_proc.dat
Original file line number Diff line number Diff line change
Expand Up @@ -12013,6 +12013,9 @@
{ oid => '7247', descr => 'map input to jsonb (cypher)',
proname => 'cypher_to_jsonb', prorettype => 'jsonb',
proargtypes => 'anyelement', prosrc => 'cypher_to_jsonb' },
{ oid => '7248', descr => 'is jsonb empty?',
proname => 'isempty', prorettype => 'bool',
proargtypes => 'jsonb', prosrc => 'cypher_isempty_jsonb' },

# agensgraph array functions
{ oid => '7300', descr => 'the first element in a array',
Expand Down
17 changes: 16 additions & 1 deletion src/include/executor/execExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
#include "executor/nodeAgg.h"
#include "nodes/execnodes.h"
#include "utils/jsonb.h"
#include "utils/arrayaccess.h"

/* forward references to avoid circularity */
struct ExprEvalStep;
struct SubscriptingRefState;
struct ScalarArrayOpExprHashTable;
struct CypherAccessPathElem;
struct CypherListCompArrayIterator;

/* Bits in ExprState->flags (see also execnodes.h for public flag bits): */
/* expression's interpreter has been initialized */
Expand Down Expand Up @@ -712,7 +714,9 @@ typedef struct ExprEvalStep
{
Datum *listvalue;
bool *listnull;
JsonbIterator **listiter;
JsonbIterator **jsonb_list_iterator;
struct CypherListCompArrayIterator *array_iterator;
bool *is_null_list_or_array;
} cypherlistcomp_iter;

struct
Expand Down Expand Up @@ -794,6 +798,17 @@ typedef struct CypherAccessPathElem
CypherIndexResult uidx;
} CypherAccessPathElem;

typedef struct CypherListCompArrayIterator
{
array_iter array_iter;
int array_size;
int array_position;
Oid array_typid;
int16 typlen;
bool typbyval;
char typalign;
} CypherListCompArrayIterator;


/* functions in execExpr.c */
extern void ExprEvalPushStep(ExprState *es, const ExprEvalStep *s);
Expand Down
2 changes: 2 additions & 0 deletions src/include/utils/cypher_empty_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
#include "fmgr.h"

extern Datum cypher_to_jsonb(PG_FUNCTION_ARGS);
extern Datum cypher_isempty_jsonb(PG_FUNCTION_ARGS);

#endif
Loading