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

Function AR_REDUCE

src/arithmetic/list_funcs/list_funcs.c:696–749  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

694}
695
696SIValue AR_REDUCE
697(
698 SIValue *argv,
699 int argc,
700 void *private_data
701) {
702 // reduce(sum = 0, n IN [1,2,3] | sum + n)
703 // argv[0] - accumulator initial value
704 // argv[1] - array
705 // argv[2] - input record
706
707 // return NULL if expected array is NULL
708 if(SI_TYPE(argv[1]) == T_NULL) return SI_NullVal();
709
710 // set arguments
711 SIValue accum = SI_ShareValue(argv[0]);
712 SIValue list = argv[1];
713 Record rec = argv[2].ptrval;
714 ListReduceCtx *ctx = private_data;
715
716 // on first invocation build the internal record
717 if(ctx->record == NULL) _PopulateReduceCtx(ctx, rec);
718 Record r = ctx->record;
719
720 // populate record with the contents of the input record
721 Record_Clone(rec, r);
722
723 // init accumulator within internal record
724 Record_AddScalar(r, ctx->accumulator_idx, accum);
725
726 // evaluate expression for each list element
727 // e.g. for `n` in `list`, compute: sum = sum + n
728 uint len = SIArray_Length(list);
729 for(uint i = 0; i < len; i++) {
730 // retrieve the current element
731 SIValue elem = SIArray_Get(list, i);
732
733 // set current element to the record
734 Record_AddScalar(r, ctx->variable_idx, elem);
735 // compute sum = sum + i
736 SIValue new_accum = AR_EXP_Evaluate_NoThrow(ctx->exp, r);
737 SIValue_Free(accum);
738 accum = new_accum;
739 // update accumulator within internal record
740 Record_AddScalar(r, ctx->accumulator_idx, accum);
741 }
742
743 // clear internal record
744 Record_Remove(r, ctx->variable_idx);
745 Record_Remove(r, ctx->accumulator_idx);
746
747 SIValue_Persist(&accum);
748 return accum;
749}
750
751void Register_ListFuncs() {
752 SIType *types;

Callers

nothing calls this directly

Calls 11

SI_NullValFunction · 0.85
SI_ShareValueFunction · 0.85
_PopulateReduceCtxFunction · 0.85
Record_CloneFunction · 0.85
Record_AddScalarFunction · 0.85
SIArray_LengthFunction · 0.85
SIArray_GetFunction · 0.85
AR_EXP_Evaluate_NoThrowFunction · 0.85
SIValue_FreeFunction · 0.85
Record_RemoveFunction · 0.85
SIValue_PersistFunction · 0.85

Tested by

no test coverage detected