Skip to content

Commit 8d2a190

Browse files
Summarize extra comments (#2779)
1 parent 6181382 commit 8d2a190

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryTable.cs

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.Globalization;
77
using System.Linq;
8-
using System.Threading;
98
using System.Threading.Tasks;
109
using Microsoft.PowerFx.Core.Entities;
1110
using Microsoft.PowerFx.Core.Functions;
@@ -1404,6 +1403,11 @@ public static FormulaValue PatchRecord(IRContext irContext, FormulaValue[] args)
14041403

14051404
public static async ValueTask<FormulaValue> Summarize(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, FormulaValue[] args)
14061405
{
1406+
// This function expects 3 types of arguments:
1407+
// 1. TableValue (arg0)
1408+
// 2. StringValue (columns to group by) at any position > 0.
1409+
// 3. LambdasFormulaValue (aggregates) at any position > 0. Represented by a record value eg {TotalAmount:Sum(Amount)}
1410+
14071411
if (args[0] is BlankValue)
14081412
{
14091413
return new BlankValue(irContext);
@@ -1419,6 +1423,7 @@ public static async ValueTask<FormulaValue> Summarize(EvalVisitor runner, EvalVi
14191423
return CommonErrors.RuntimeTypeMismatch(irContext);
14201424
}
14211425

1426+
// Building a dictionary of key records (based on groupping columns) and a subset of records that match the key.
14221427
var keyRecords = new Dictionary<string, RecordValue>();
14231428
var groupByRecords = new Dictionary<string, List<RecordValue>>();
14241429

@@ -1442,6 +1447,7 @@ public static async ValueTask<FormulaValue> Summarize(EvalVisitor runner, EvalVi
14421447
var showColumnsArgs = new List<FormulaValue>() { row.Value };
14431448
showColumnsArgs.AddRange(stringArgs);
14441449

1450+
// We call ShowColumns to keep only the columns that are part of the group by.
14451451
var keyRecord = await ShowColumns(runner, context, IRContext.NotInSource(FormulaType.Build(irContext.ResultType._type.ToRecord())), showColumnsArgs.ToArray()).ConfigureAwait(false);
14461452
var key = keyRecord.ToExpression();
14471453

@@ -1456,20 +1462,21 @@ public static async ValueTask<FormulaValue> Summarize(EvalVisitor runner, EvalVi
14561462

14571463
var finalRecords = new List<DValue<RecordValue>>();
14581464

1459-
foreach (var group in groupByRecords)
1465+
// Evaluate all aggregates for each group and include the result as columns name and column value in the final record.
1466+
foreach (var thisGroup in groupByRecords)
14601467
{
14611468
runner.CancellationToken.ThrowIfCancellationRequested();
14621469

1463-
var newTable = FormulaValue.NewTable((RecordType)FormulaType.Build(tableValue.Type._type.ToRecord()), group.Value);
1464-
var record = (InMemoryRecordValue)keyRecords[group.Key];
1470+
var thisGroupTableValue = FormulaValue.NewTable(tableValue.Type.ToRecord(), thisGroup.Value);
1471+
var record = (InMemoryRecordValue)keyRecords[thisGroup.Key];
14651472
var fields = new Dictionary<string, FormulaValue>();
14661473

14671474
foreach (var field in record.Fields)
14681475
{
14691476
fields.Add(field.Name, field.Value);
14701477
}
14711478

1472-
SymbolContext childContext = context.SymbolContext.WithScopeValues(newTable);
1479+
SymbolContext childContext = context.SymbolContext.WithScopeValues(thisGroupTableValue);
14731480

14741481
foreach (LambdaFormulaValue arg in args.Where(arg => arg is LambdaFormulaValue))
14751482
{

0 commit comments

Comments
 (0)