|
| 1 | +// |
| 2 | +// Copyright (c) 2016-2025 Deephaven Data Labs and Patent Pending |
| 3 | +// |
| 4 | +package io.deephaven.engine.table.impl; |
| 5 | + |
| 6 | +import io.deephaven.api.ColumnName; |
| 7 | +import io.deephaven.api.agg.Aggregation; |
| 8 | +import io.deephaven.engine.context.ExecutionContext; |
| 9 | +import io.deephaven.engine.rowset.RowSetShiftData; |
| 10 | +import io.deephaven.engine.table.ColumnDefinition; |
| 11 | +import io.deephaven.engine.table.ModifiedColumnSet; |
| 12 | +import io.deephaven.engine.table.Table; |
| 13 | +import io.deephaven.engine.table.impl.by.AggregationProcessor; |
| 14 | +import io.deephaven.engine.testutil.ControlledUpdateGraph; |
| 15 | +import io.deephaven.engine.testutil.TstUtils; |
| 16 | +import io.deephaven.engine.testutil.testcase.RefreshingTableTestCase; |
| 17 | +import io.deephaven.engine.util.TableTools; |
| 18 | +import io.deephaven.test.types.OutOfBandTest; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.experimental.categories.Category; |
| 21 | + |
| 22 | +import java.util.*; |
| 23 | + |
| 24 | +import static io.deephaven.api.agg.Aggregation.*; |
| 25 | +import static io.deephaven.engine.table.impl.by.RollupConstants.ROLLUP_COLUMN_SUFFIX; |
| 26 | +import static io.deephaven.engine.testutil.TstUtils.assertTableEquals; |
| 27 | +import static io.deephaven.engine.testutil.TstUtils.i; |
| 28 | +import static io.deephaven.engine.util.TableTools.*; |
| 29 | + |
| 30 | +@Category(OutOfBandTest.class) |
| 31 | +public class TestAggGroup extends RefreshingTableTestCase { |
| 32 | + @Test |
| 33 | + public void testGroupModifications() { |
| 34 | + final QueryTable source = TstUtils.testRefreshingTable( |
| 35 | + stringCol("Key1", "Alpha", "Bravo", "Alpha", "Charlie", "Charlie", "Bravo", "Bravo"), |
| 36 | + stringCol("Key2", "Delta", "Delta", "Echo", "Echo", "Echo", "Echo", "Echo"), |
| 37 | + intCol("Sentinel", 1, 2, 3, 4, 5, 6, 7), |
| 38 | + intCol("Sentinel2", 101, 102, 103, 104, 105, 106, 107)); |
| 39 | + |
| 40 | + final List<Aggregation> aggs = |
| 41 | + List.of(AggGroup("Sentinel"), AggSum("Sum=Sentinel"), AggGroup("Sentinel2"), AggSum("Sum2=Sentinel2")); |
| 42 | + |
| 43 | + final QueryTable normal = source.aggNoMemo(AggregationProcessor.forAggregation(aggs), false, null, |
| 44 | + List.of(ColumnName.of("Key1"))); |
| 45 | + final ColumnName rollupColumn = ColumnName.of(ROLLUP_COLUMN_SUFFIX); |
| 46 | + final QueryTable base = source.aggNoMemo(AggregationProcessor.forRollupBase(aggs, false, rollupColumn), false, |
| 47 | + null, List.of(ColumnName.of("Key1"), ColumnName.of("Key2"))); |
| 48 | + final QueryTable reaggregated = base.aggNoMemo(AggregationProcessor.forRollupReaggregated(aggs, |
| 49 | + List.of(ColumnDefinition.ofString("Key2")), rollupColumn, source), false, null, |
| 50 | + List.of(ColumnName.of("Key1"))); |
| 51 | + |
| 52 | + TableTools.show(normal); |
| 53 | + TableTools.show(base); |
| 54 | + TableTools.show(reaggregated); |
| 55 | + |
| 56 | + doCheck(normal, base, reaggregated); |
| 57 | + |
| 58 | + final SimpleListener normalListener = new SimpleListener(normal); |
| 59 | + normal.addUpdateListener(normalListener); |
| 60 | + final SimpleListener baseListener = new SimpleListener(base); |
| 61 | + base.addUpdateListener(baseListener); |
| 62 | + final SimpleListener reaggListener = new SimpleListener(reaggregated); |
| 63 | + reaggregated.addUpdateListener(reaggListener); |
| 64 | + |
| 65 | + final ControlledUpdateGraph cug = ExecutionContext.getContext().getUpdateGraph().cast(); |
| 66 | + // modify the value of a Sentinel; check the updates |
| 67 | + cug.runWithinUnitTestCycle(() -> { |
| 68 | + TstUtils.addToTable(source, i(0), stringCol("Key1", "Alpha"), stringCol("Key2", "Delta"), |
| 69 | + intCol("Sentinel", 8), intCol("Sentinel2", 101)); |
| 70 | + final ModifiedColumnSet mcs = source.getModifiedColumnSetForUpdates(); |
| 71 | + mcs.clear(); |
| 72 | + mcs.setAll("Sentinel"); |
| 73 | + source.notifyListeners(new TableUpdateImpl(i(), i(), i(0), RowSetShiftData.EMPTY, mcs)); |
| 74 | + }); |
| 75 | + |
| 76 | + TableTools.show(normal); |
| 77 | + TableTools.show(base); |
| 78 | + TableTools.show(reaggregated); |
| 79 | + |
| 80 | + // make sure the aggregation is still consistent |
| 81 | + doCheck(normal, base, reaggregated); |
| 82 | + |
| 83 | + // we should have gotten an update from each of our listeners |
| 84 | + checkModified(normalListener, normal, "Sentinel", "Sentinel2"); |
| 85 | + checkModified(baseListener, base, "Sentinel", "Sentinel2"); |
| 86 | + checkModified(reaggListener, reaggregated, "Sentinel", "Sentinel2"); |
| 87 | + } |
| 88 | + |
| 89 | + private static void checkModified(SimpleListener listener, QueryTable table, final String modColumn, |
| 90 | + final String noModColumn) { |
| 91 | + System.out.println("update = " + listener.update); |
| 92 | + assertEquals(1, listener.count); |
| 93 | + assertTrue(listener.update.added().isEmpty()); |
| 94 | + assertTrue(listener.update.removed().isEmpty()); |
| 95 | + assertEquals(1, listener.update.modified().size()); |
| 96 | + assertTrue(listener.update.modifiedColumnSet().containsAll(table.newModifiedColumnSet(modColumn))); |
| 97 | + assertFalse(listener.update.modifiedColumnSet().containsAny(table.newModifiedColumnSet(noModColumn))); |
| 98 | + } |
| 99 | + |
| 100 | + private static void doCheck(Table normal, QueryTable base, QueryTable reaggregated) { |
| 101 | + assertEquals(0, normal.update("CheckSum=sum(Sentinel)", "CheckSum2=sum(Sentinel2)") |
| 102 | + .where("Sum != CheckSum || Sum2 != CheckSum2").size()); |
| 103 | + assertEquals(0, base.update("CheckSum=sum(Sentinel)", "CheckSum2=sum(Sentinel2)") |
| 104 | + .where("Sum != CheckSum || Sum2 != CheckSum2").size()); |
| 105 | + assertEquals(0, reaggregated.update("CheckSum=sum(Sentinel)", "CheckSum2=sum(Sentinel2)") |
| 106 | + .where("Sum != CheckSum || Sum2 != CheckSum2").size()); |
| 107 | + assertTableEquals(normal.view("Key1", "Sentinel", "Sum", "Sentinel2", "Sum2"), |
| 108 | + reaggregated.view("Key1", "Sentinel", "Sum", "Sentinel2", "Sum2")); |
| 109 | + } |
| 110 | +} |
0 commit comments