@@ -59,7 +59,7 @@ public class UpdateByOperatorFactory {
59
59
private final MatchPair [] groupByColumns ;
60
60
@ NotNull
61
61
private final UpdateByControl control ;
62
- private Map <String , ColumnDefinition <?>> vectorColumnNameMap ;
62
+ private Map <String , ColumnDefinition <?>> vectorColumnDefinitions ;
63
63
64
64
public UpdateByOperatorFactory (
65
65
@ NotNull final TableDefinition tableDef ,
@@ -1437,7 +1437,6 @@ private UpdateByOperator makeRollingFormulaOperator(@NotNull final MatchPair pai
1437
1437
private UpdateByOperator makeRollingFormulaMultiColumnOperator (
1438
1438
@ NotNull final TableDefinition tableDef ,
1439
1439
@ NotNull final RollingFormulaSpec rs ) {
1440
-
1441
1440
final long prevWindowScaleUnits = rs .revWindowScale ().getTimeScaleUnits ();
1442
1441
final long fwdWindowScaleUnits = rs .fwdWindowScale ().getTimeScaleUnits ();
1443
1442
@@ -1446,42 +1445,58 @@ private UpdateByOperator makeRollingFormulaMultiColumnOperator(
1446
1445
// Create the colum
1447
1446
final SelectColumn selectColumn = SelectColumn .of (Selectable .parse (rs .formula ()));
1448
1447
1449
- // Get or create a column definition map where the definitions are vectors of the original column types.
1450
- if (vectorColumnNameMap == null ) {
1451
- vectorColumnNameMap = new HashMap <>();
1452
- columnDefinitionMap .forEach ((key , value ) -> {
1453
- final ColumnDefinition <?> columnDef = ColumnDefinition .fromGenericType (
1454
- key ,
1455
- VectorFactory .forElementType (value .getDataType ()).vectorType (),
1456
- value .getDataType ());
1457
- vectorColumnNameMap .put (key , columnDef );
1458
- });
1448
+ // Get or create a column definition map composed of vectors of the original column types (or scalars when
1449
+ // part of the group_by columns).
1450
+ final Set <String > groupByColumnSet =
1451
+ Arrays .stream (groupByColumns ).map (MatchPair ::rightColumn ).collect (Collectors .toSet ());
1452
+ if (vectorColumnDefinitions == null ) {
1453
+ vectorColumnDefinitions = tableDef .getColumnStream ().collect (Collectors .toMap (
1454
+ ColumnDefinition ::getName ,
1455
+ (final ColumnDefinition <?> cd ) -> groupByColumnSet .contains (cd .getName ())
1456
+ ? cd
1457
+ : ColumnDefinition .fromGenericType (
1458
+ cd .getName (),
1459
+ VectorFactory .forElementType (cd .getDataType ()).vectorType (),
1460
+ cd .getDataType ())));
1459
1461
}
1460
1462
1461
- // Get the input column names and data types from the formula.
1462
- final String [] inputColumnNames =
1463
- selectColumn .initDef (vectorColumnNameMap , compilationProcessor ).toArray (String []::new );
1463
+ // Get the input column names from the formula and provide them to the rolling formula operator
1464
+ final String [] allInputColumns =
1465
+ selectColumn .initDef (vectorColumnDefinitions , compilationProcessor ).toArray (String []::new );
1464
1466
if (!selectColumn .getColumnArrays ().isEmpty ()) {
1465
1467
throw new IllegalArgumentException ("RollingFormulaMultiColumnOperator does not support column arrays ("
1466
1468
+ selectColumn .getColumnArrays () + ")" );
1467
1469
}
1468
1470
if (selectColumn .hasVirtualRowVariables ()) {
1469
1471
throw new IllegalArgumentException ("RollingFormula does not support virtual row variables" );
1470
1472
}
1471
- final Class <?>[] inputColumnTypes = new Class [inputColumnNames .length ];
1472
- final Class <?>[] inputVectorTypes = new Class [inputColumnNames .length ];
1473
1473
1474
- for (int i = 0 ; i < inputColumnNames .length ; i ++) {
1475
- final ColumnDefinition <?> columnDef = columnDefinitionMap .get (inputColumnNames [i ]);
1476
- inputColumnTypes [i ] = columnDef .getDataType ();
1477
- inputVectorTypes [i ] = vectorColumnNameMap .get (inputColumnNames [i ]).getDataType ();
1474
+ final Map <Boolean , List <String >> partitioned = Arrays .stream (allInputColumns )
1475
+ .collect (Collectors .partitioningBy (groupByColumnSet ::contains ));
1476
+ final String [] inputKeyColumns = partitioned .get (true ).toArray (String []::new );
1477
+ final String [] inputNonKeyColumns = partitioned .get (false ).toArray (String []::new );
1478
+
1479
+ final Class <?>[] inputKeyColumnTypes = new Class [inputKeyColumns .length ];
1480
+ final Class <?>[] inputKeyComponentTypes = new Class [inputKeyColumns .length ];
1481
+ for (int i = 0 ; i < inputKeyColumns .length ; i ++) {
1482
+ final ColumnDefinition <?> columnDef = columnDefinitionMap .get (inputKeyColumns [i ]);
1483
+ inputKeyColumnTypes [i ] = columnDef .getDataType ();
1484
+ inputKeyComponentTypes [i ] = columnDef .getComponentType ();
1485
+ }
1486
+
1487
+ final Class <?>[] inputNonKeyColumnTypes = new Class [inputNonKeyColumns .length ];
1488
+ final Class <?>[] inputNonKeyVectorTypes = new Class [inputNonKeyColumns .length ];
1489
+ for (int i = 0 ; i < inputNonKeyColumns .length ; i ++) {
1490
+ final ColumnDefinition <?> columnDef = columnDefinitionMap .get (inputNonKeyColumns [i ]);
1491
+ inputNonKeyColumnTypes [i ] = columnDef .getDataType ();
1492
+ inputNonKeyVectorTypes [i ] = vectorColumnDefinitions .get (inputNonKeyColumns [i ]).getDataType ();
1478
1493
}
1479
1494
1480
1495
final String [] affectingColumns ;
1481
1496
if (rs .revWindowScale ().timestampCol () == null ) {
1482
- affectingColumns = inputColumnNames ;
1497
+ affectingColumns = inputNonKeyColumns ;
1483
1498
} else {
1484
- affectingColumns = ArrayUtils .add (inputColumnNames , rs .revWindowScale ().timestampCol ());
1499
+ affectingColumns = ArrayUtils .add (inputNonKeyColumns , rs .revWindowScale ().timestampCol ());
1485
1500
}
1486
1501
1487
1502
// Create a new column pair with the same name for the left and right columns
@@ -1494,9 +1509,12 @@ private UpdateByOperator makeRollingFormulaMultiColumnOperator(
1494
1509
prevWindowScaleUnits ,
1495
1510
fwdWindowScaleUnits ,
1496
1511
selectColumn ,
1497
- inputColumnNames ,
1498
- inputColumnTypes ,
1499
- inputVectorTypes );
1512
+ inputKeyColumns ,
1513
+ inputKeyColumnTypes ,
1514
+ inputKeyComponentTypes ,
1515
+ inputNonKeyColumns ,
1516
+ inputNonKeyColumnTypes ,
1517
+ inputNonKeyVectorTypes );
1500
1518
}
1501
1519
}
1502
1520
}
0 commit comments