MCPcopy Create free account
hub / github.com/RedisGraph/RedisGraph / _reduceNodeCount

Function _reduceNodeCount

src/execution_plan/optimizations/reduce_count.c:79–124  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

77}
78
79bool _reduceNodeCount(ExecutionPlan *plan) {
80 /* We'll only modify execution plan if it is structured as follows:
81 * "Scan -> Aggregate -> Results" */
82 const char *label;
83 OpBase *opScan;
84 OpResult *opResult;
85 OpAggregate *opAggregate;
86
87 /* See if execution-plan matches the pattern:
88 * "Scan -> Aggregate -> Results".
89 * if that's not the case, simply return without making any modifications. */
90 if(!_identifyNodeCountPattern(plan->root, &opResult, &opAggregate, &opScan, &label)) return false;
91
92 /* User is trying to get total number of nodes in the graph
93 * optimize by skiping SCAN and AGGREGATE. */
94 SIValue nodeCount;
95 GraphContext *gc = QueryCtx_GetGraphCtx();
96
97 // If label is specified, count only labeled entities.
98 if(label) {
99 Schema *s = GraphContext_GetSchema(gc, label, SCHEMA_NODE);
100 if(s) nodeCount = SI_LongVal(Graph_LabeledNodeCount(gc->g, s->id));
101 else nodeCount = SI_LongVal(0); // Specified Label doesn't exists.
102 } else {
103 nodeCount = SI_LongVal(Graph_NodeCount(gc->g));
104 }
105
106 // Construct a constant expression, used by a new projection operation
107 AR_ExpNode *exp = AR_EXP_NewConstOperandNode(nodeCount);
108 // The new expression must be aliased to populate the Record.
109 exp->resolved_name = opAggregate->aggregate_exps[0]->resolved_name;
110 AR_ExpNode **exps = array_new(AR_ExpNode *, 1);
111 array_append(exps, exp);
112
113 OpBase *opProject = NewProjectOp(opAggregate->op.plan, exps);
114
115 // New execution plan: "Project -> Results"
116 ExecutionPlan_RemoveOp(plan, opScan);
117 OpBase_Free(opScan);
118
119 ExecutionPlan_RemoveOp(plan, (OpBase *)opAggregate);
120 OpBase_Free((OpBase *)opAggregate);
121
122 ExecutionPlan_AddOp((OpBase *)opResult, opProject);
123 return true;
124}
125
126/* Checks if execution plan solely performs edge count */
127static bool _identifyEdgeCountPattern(OpBase *root, OpResult **opResult,

Callers 1

reduceCountFunction · 0.85

Calls 11

QueryCtx_GetGraphCtxFunction · 0.85
GraphContext_GetSchemaFunction · 0.85
SI_LongValFunction · 0.85
Graph_LabeledNodeCountFunction · 0.85
Graph_NodeCountFunction · 0.85
NewProjectOpFunction · 0.85
ExecutionPlan_RemoveOpFunction · 0.85
OpBase_FreeFunction · 0.85
ExecutionPlan_AddOpFunction · 0.85

Tested by

no test coverage detected