private void recreateState(Long lastData, TimePeriod.Duration recreateForDuration,
                               Table recreateFromTable, boolean isBeforeRoot) {
        OnDemandQuery onDemandQuery;
        if (lastData != null) {
            endOFLatestEventTimestamp = IncrementalTimeConverterUtil
                    .getNextEmitTime(lastData, recreateForDuration, timeZone);
        }
        if (recreateFromTable.getTableDefinition().getId().contains(TimePeriod.Duration.MONTHS.name())
                || recreateFromTable.getTableDefinition().getId().contains(TimePeriod.Duration.YEARS.name())
                || recreateFromTable.getTableDefinition().getId().contains(TimePeriod.Duration.DAYS.name())) {
            onDemandQuery = getOnDemandQuery(recreateFromTable, false, endOFLatestEventTimestamp, true,
                    OrderByAttribute.Order.DESC);
        } else {
            onDemandQuery = getOnDemandQuery(recreateFromTable, false, endOFLatestEventTimestamp, false,
                    OrderByAttribute.Order.DESC);
        }
        onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.FIND);
        OnDemandQueryRuntime onDemandQueryRuntime = OnDemandQueryParser.parse(onDemandQuery, null, siddhiAppContext,
                tableMap, windowMap, aggregationMap);
        Event[] events = onDemandQueryRuntime.execute();
        if (events != null) {
            long referenceToNextLatestEvent = (Long) events[events.length - 1].getData(0);
            if (!isStatePresentForAggregationDuration(recreateForDuration)) {
                ComplexEventChunk<StreamEvent> complexEventChunk = new ComplexEventChunk<>();
                for (Event event : events) {
                    StreamEvent streamEvent = streamEventFactory.newInstance();
                    streamEvent.setOutputData(event.getData());
                    complexEventChunk.add(streamEvent);
                }
                Executor incrementalExecutor = incrementalExecutorMap.get(recreateForDuration);
                incrementalExecutor.execute(complexEventChunk);
            }
            if (isBeforeRoot) {
                TimePeriod.Duration rootDuration = incrementalDurations.get(0);
                Executor rootIncrementalExecutor = incrementalExecutorMap.get(rootDuration);
                long emitTimeOfLatestEventInTable = IncrementalTimeConverterUtil.getNextEmitTime(
                        referenceToNextLatestEvent, rootDuration, timeZone);

                rootIncrementalExecutor.setEmitTime(emitTimeOfLatestEventInTable);

            }
        }
    }
This class is used to recreate in-memory data from the tables (Such as RDBMS) in incremental aggregation.
This ensures that the aggregation calculations are done correctly in case of server restart.
modules/siddhi-core/src/main/java/io/siddhi/core/aggregation/IncrementalExecutorsInitialiser.java