qfrjava
2025-04-18 ccbda82934b9c0c22633414f8d3fa66de278c93d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
 
#ifndef AG_CYPHER_UTILS_H
#define AG_CYPHER_UTILS_H
 
#include "access/heapam.h"
#include "access/table.h"
#include "access/tableam.h"
#include "nodes/execnodes.h"
#include "nodes/extensible.h"
#include "nodes/nodes.h"
#include "nodes/plannodes.h"
 
#include "nodes/cypher_nodes.h"
#include "utils/agtype.h"
 
// declaration of a useful postgres macro that isn't in a header file
#define DatumGetItemPointer(X)     ((ItemPointer) DatumGetPointer(X))
#define ItemPointerGetDatum(X)     PointerGetDatum(X)
 
/*
 * When executing the children of the CREATE, SET, REMOVE, and
 * DELETE clauses, we need to alter the command id in the estate
 * and the snapshot. That way we can hide the modified tuples from
 * the sub clauses that should not know what their parent clauses are
 * doing.
 */
#define Increment_Estate_CommandId(estate) \
    estate->es_output_cid++; \
    estate->es_snapshot->curcid++;
 
#define Decrement_Estate_CommandId(estate) \
    estate->es_output_cid--; \
    estate->es_snapshot->curcid--;
 
#define DELETE_VERTEX_HTAB_NAME "delete_vertex_htab"
#define DELETE_VERTEX_HTAB_SIZE 1000000
 
typedef struct cypher_create_custom_scan_state
{
    CustomScanState css;
    CustomScan *cs;
    List *pattern;
    List *path_values;
    uint32 flags;
    TupleTableSlot *slot;
    Oid graph_oid;
} cypher_create_custom_scan_state;
 
typedef struct cypher_set_custom_scan_state
{
    CustomScanState css;
    CustomScan *cs;
    cypher_update_information *set_list;
    int flags;
} cypher_set_custom_scan_state;
 
typedef struct cypher_delete_custom_scan_state
{
    CustomScanState css;
    CustomScan *cs;
    cypher_delete_information *delete_data;
    int flags;
    List *edge_labels;
 
    /*
     * Deleted vertex IDs are stored in this hashtable.
     *
     * When a vertex item is deleted, it must be checked if there is any edges
     * connected to it. The connected edges are either deleted or an error is
     * thrown depending on the DETACH option. However, the check for connected
     * edges is not done immediately. Instead the deleted vertex IDs are stored
     * in the hashtable. Once all vertices are deleted, this hashtable is used
     * to process the connected edges with only one scan of the edge tables.
     *
     * Note on performance: Additional performance gain may be possible if
     * the standard DELETE .. USING .. command can be used instead of this
     * hashtable. Because Postgres may create a better plan to execute that
     * command depending on the statistics and available indexes on start_id
     * and end_id column.
     */
    HTAB *vertex_id_htab;
} cypher_delete_custom_scan_state;
 
typedef struct cypher_merge_custom_scan_state
{
    CustomScanState css;
    CustomScan *cs;
    cypher_merge_information *merge_information;
    int flags;
    cypher_create_path *path;
    List *path_values;
    Oid graph_oid;
    AttrNumber merge_function_attr;
    bool created_new_path;
    bool found_a_path;
    CommandId base_currentCommandId;
} cypher_merge_custom_scan_state;
 
TupleTableSlot *populate_vertex_tts(TupleTableSlot *elemTupleSlot,
                                    agtype_value *id, agtype_value *properties);
TupleTableSlot *populate_edge_tts(
    TupleTableSlot *elemTupleSlot, agtype_value *id, agtype_value *startid,
    agtype_value *endid, agtype_value *properties);
 
ResultRelInfo *create_entity_result_rel_info(EState *estate, char *graph_name,
                                             char *label_name);
void destroy_entity_result_rel_info(ResultRelInfo *result_rel_info);
 
bool entity_exists(EState *estate, Oid graph_oid, graphid id);
HeapTuple insert_entity_tuple(ResultRelInfo *resultRelInfo,
                              TupleTableSlot *elemTupleSlot,
                              EState *estate);
HeapTuple insert_entity_tuple_cid(ResultRelInfo *resultRelInfo,
                                  TupleTableSlot *elemTupleSlot,
                                  EState *estate, CommandId cid);
 
#endif