5
5
using System . Collections . Generic ;
6
6
using System . Globalization ;
7
7
using System . Linq ;
8
- using System . Threading ;
9
8
using System . Threading . Tasks ;
10
9
using Microsoft . PowerFx . Core . Entities ;
11
10
using Microsoft . PowerFx . Core . Functions ;
@@ -1404,6 +1403,11 @@ public static FormulaValue PatchRecord(IRContext irContext, FormulaValue[] args)
1404
1403
1405
1404
public static async ValueTask < FormulaValue > Summarize ( EvalVisitor runner , EvalVisitorContext context , IRContext irContext , FormulaValue [ ] args )
1406
1405
{
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
+
1407
1411
if ( args [ 0 ] is BlankValue )
1408
1412
{
1409
1413
return new BlankValue ( irContext ) ;
@@ -1419,6 +1423,7 @@ public static async ValueTask<FormulaValue> Summarize(EvalVisitor runner, EvalVi
1419
1423
return CommonErrors . RuntimeTypeMismatch ( irContext ) ;
1420
1424
}
1421
1425
1426
+ // Building a dictionary of key records (based on groupping columns) and a subset of records that match the key.
1422
1427
var keyRecords = new Dictionary < string , RecordValue > ( ) ;
1423
1428
var groupByRecords = new Dictionary < string , List < RecordValue > > ( ) ;
1424
1429
@@ -1442,6 +1447,7 @@ public static async ValueTask<FormulaValue> Summarize(EvalVisitor runner, EvalVi
1442
1447
var showColumnsArgs = new List < FormulaValue > ( ) { row . Value } ;
1443
1448
showColumnsArgs . AddRange ( stringArgs ) ;
1444
1449
1450
+ // We call ShowColumns to keep only the columns that are part of the group by.
1445
1451
var keyRecord = await ShowColumns ( runner , context , IRContext . NotInSource ( FormulaType . Build ( irContext . ResultType . _type . ToRecord ( ) ) ) , showColumnsArgs . ToArray ( ) ) . ConfigureAwait ( false ) ;
1446
1452
var key = keyRecord . ToExpression ( ) ;
1447
1453
@@ -1456,20 +1462,21 @@ public static async ValueTask<FormulaValue> Summarize(EvalVisitor runner, EvalVi
1456
1462
1457
1463
var finalRecords = new List < DValue < RecordValue > > ( ) ;
1458
1464
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 )
1460
1467
{
1461
1468
runner . CancellationToken . ThrowIfCancellationRequested ( ) ;
1462
1469
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 ] ;
1465
1472
var fields = new Dictionary < string , FormulaValue > ( ) ;
1466
1473
1467
1474
foreach ( var field in record . Fields )
1468
1475
{
1469
1476
fields . Add ( field . Name , field . Value ) ;
1470
1477
}
1471
1478
1472
- SymbolContext childContext = context . SymbolContext . WithScopeValues ( newTable ) ;
1479
+ SymbolContext childContext = context . SymbolContext . WithScopeValues ( thisGroupTableValue ) ;
1473
1480
1474
1481
foreach ( LambdaFormulaValue arg in args . Where ( arg => arg is LambdaFormulaValue ) )
1475
1482
{
0 commit comments