diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettings.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettings.java index 63d79785850c..392e40b41050 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettings.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettings.java @@ -745,4 +745,8 @@ default boolean isHideUnapprovedDataInAnalytics() { // -1 means approval is disabled return getIgnoreAnalyticsApprovalYearThreshold() >= 0; } + + default boolean getUseExperimentalAnalyticsQueryEngine() { + return asBoolean("experimentalAnalyticsSqlEngineEnabled", false); + } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManager.java index 56166fd7ca10..fdcbe004bcd9 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManager.java @@ -122,6 +122,7 @@ import org.hisp.dhis.program.AnalyticsType; import org.hisp.dhis.program.ProgramIndicator; import org.hisp.dhis.program.ProgramIndicatorService; +import org.hisp.dhis.setting.SystemSettingsService; import org.hisp.dhis.system.util.MathUtils; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; @@ -167,6 +168,8 @@ public abstract class AbstractJdbcEventAnalyticsManager { protected final SqlBuilder sqlBuilder; + protected final SystemSettingsService settingsService; + /** * Returns a SQL paging clause. * @@ -1445,8 +1448,11 @@ protected List getSelectColumnsWithCTE(EventQueryParams params, CteConte } } // remove duplicates - var ded = columns.stream().distinct().toList(); - return ded; + return columns.stream().distinct().toList(); + } + + protected boolean useExperimentalAnalyticsQueryEngine() { + return this.settingsService.getCurrentSettings().getUseExperimentalAnalyticsQueryEngine(); } /** diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java index e9d8ed3356bd..e49a6f80932f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java @@ -100,6 +100,7 @@ import org.hisp.dhis.program.AnalyticsType; import org.hisp.dhis.program.ProgramIndicator; import org.hisp.dhis.program.ProgramIndicatorService; +import org.hisp.dhis.setting.SystemSettingsService; import org.hisp.dhis.system.util.ListBuilder; import org.locationtech.jts.util.Assert; import org.springframework.beans.factory.annotation.Qualifier; @@ -138,13 +139,15 @@ public JdbcEnrollmentAnalyticsManager( ProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder, EnrollmentTimeFieldSqlRenderer timeFieldSqlRenderer, ExecutionPlanStore executionPlanStore, + SystemSettingsService settingsService, SqlBuilder sqlBuilder) { super( jdbcTemplate, programIndicatorService, programIndicatorSubqueryBuilder, executionPlanStore, - sqlBuilder); + sqlBuilder, + settingsService); this.timeFieldSqlRenderer = timeFieldSqlRenderer; } @@ -152,15 +155,18 @@ public JdbcEnrollmentAnalyticsManager( public void getEnrollments(EventQueryParams params, Grid grid, int maxLimit) { String sql; if (params.isAggregatedEnrollments()) { - // LUCIANO // - sql = buildAggregatedEnrollmentQueryWithCte(grid.getHeaders(), params); - // sql = getAggregatedEnrollmentsSql(grid.getHeaders(), params); + sql = + useExperimentalAnalyticsQueryEngine() + ? buildAggregatedEnrollmentQueryWithCte(grid.getHeaders(), params) + : getAggregatedEnrollmentsSql(grid.getHeaders(), params); } else { - sql = buildEnrollmentQueryWithCte(params); + // getAggregatedEnrollmentsSql + sql = + useExperimentalAnalyticsQueryEngine() + ? buildEnrollmentQueryWithCte(params) + : getAggregatedEnrollmentsSql(params, maxLimit); } - System.out.println("SQL: " + sql); // FIXME: Remove debug line - if (params.analyzeOnly()) { withExceptionHandling( () -> executionPlanStore.addExecutionPlan(params.getExplainOrderId(), sql)); @@ -1414,7 +1420,6 @@ private String buildAggregatedEnrollmentQueryWithCte( } private String buildEnrollmentQueryWithCte(EventQueryParams params) { - // LUCIANO // // 1. Create the CTE context (collect all CTE definitions for program indicators, program // stages, etc.) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java index d28f27b029b3..1a2b084a5606 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java @@ -86,6 +86,7 @@ import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.program.AnalyticsType; import org.hisp.dhis.program.ProgramIndicatorService; +import org.hisp.dhis.setting.SystemSettingsService; import org.hisp.dhis.system.util.ListBuilder; import org.postgresql.util.PSQLException; import org.springframework.beans.factory.annotation.Qualifier; @@ -116,13 +117,15 @@ public JdbcEventAnalyticsManager( ProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder, EventTimeFieldSqlRenderer timeFieldSqlRenderer, ExecutionPlanStore executionPlanStore, + SystemSettingsService settingsService, SqlBuilder sqlBuilder) { super( jdbcTemplate, programIndicatorService, programIndicatorSubqueryBuilder, executionPlanStore, - sqlBuilder); + sqlBuilder, + settingsService); this.timeFieldSqlRenderer = timeFieldSqlRenderer; } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsManagerTest.java index a910ce147445..4417e627a89e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsManagerTest.java @@ -76,6 +76,7 @@ import org.hisp.dhis.relationship.RelationshipConstraint; import org.hisp.dhis.relationship.RelationshipEntity; import org.hisp.dhis.relationship.RelationshipType; +import org.hisp.dhis.setting.SystemSettingsService; import org.hisp.dhis.system.grid.ListGrid; import org.hisp.dhis.test.random.BeanRandomizer; import org.junit.jupiter.api.BeforeEach; @@ -110,6 +111,8 @@ class EnrollmentAnalyticsManagerTest extends EventAnalyticsTest { @Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder(); + @Mock private SystemSettingsService systemSettingsService; + @Spy private EnrollmentTimeFieldSqlRenderer enrollmentTimeFieldSqlRenderer = new EnrollmentTimeFieldSqlRenderer(sqlBuilder); @@ -139,6 +142,7 @@ public void setUp() { programIndicatorSubqueryBuilder, enrollmentTimeFieldSqlRenderer, executionPlanStore, + systemSettingsService, sqlBuilder); } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsManagerTest.java index c310aea85254..e24fc20f598c 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsManagerTest.java @@ -86,6 +86,7 @@ import org.hisp.dhis.program.ProgramIndicator; import org.hisp.dhis.program.ProgramIndicatorService; import org.hisp.dhis.program.ProgramType; +import org.hisp.dhis.setting.SystemSettingsService; import org.hisp.dhis.system.grid.ListGrid; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -116,6 +117,8 @@ class EventAnalyticsManagerTest extends EventAnalyticsTest { private static final String TABLE_NAME = "analytics_event"; + @Mock private SystemSettingsService systemSettingsService; + private static final String DEFAULT_COLUMNS_WITH_REGISTRATION = "event,ps,occurreddate,storedby," + "createdbydisplayname" @@ -138,6 +141,7 @@ public void setUp() { programIndicatorSubqueryBuilder, timeCoordinateSelector, executionPlanStore, + systemSettingsService, sqlBuilder); when(jdbcTemplate.queryForRowSet(anyString())).thenReturn(this.rowSet);