Skip to content

Commit

Permalink
Introduce experimentalAnalyticsSqlEngineEnabled settings
Browse files Browse the repository at this point in the history
  • Loading branch information
luciano-fiandesio committed Jan 16, 2025
1 parent ab627c9 commit 76cdd53
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -745,4 +745,8 @@ default boolean isHideUnapprovedDataInAnalytics() {
// -1 means approval is disabled
return getIgnoreAnalyticsApprovalYearThreshold() >= 0;
}

default boolean getUseExperimentalAnalyticsQueryEngine() {
return asBoolean("experimentalAnalyticsSqlEngineEnabled", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,6 +168,8 @@ public abstract class AbstractJdbcEventAnalyticsManager {

protected final SqlBuilder sqlBuilder;

protected final SystemSettingsService settingsService;

/**
* Returns a SQL paging clause.
*
Expand Down Expand Up @@ -1445,8 +1448,11 @@ protected List<String> 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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -138,29 +139,34 @@ public JdbcEnrollmentAnalyticsManager(
ProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder,
EnrollmentTimeFieldSqlRenderer timeFieldSqlRenderer,
ExecutionPlanStore executionPlanStore,
SystemSettingsService settingsService,
SqlBuilder sqlBuilder) {
super(
jdbcTemplate,
programIndicatorService,
programIndicatorSubqueryBuilder,
executionPlanStore,
sqlBuilder);
sqlBuilder,
settingsService);
this.timeFieldSqlRenderer = timeFieldSqlRenderer;
}

@Override
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));
Expand Down Expand Up @@ -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.)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -139,6 +142,7 @@ public void setUp() {
programIndicatorSubqueryBuilder,
enrollmentTimeFieldSqlRenderer,
executionPlanStore,
systemSettingsService,
sqlBuilder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
Expand All @@ -138,6 +141,7 @@ public void setUp() {
programIndicatorSubqueryBuilder,
timeCoordinateSelector,
executionPlanStore,
systemSettingsService,
sqlBuilder);

when(jdbcTemplate.queryForRowSet(anyString())).thenReturn(this.rowSet);
Expand Down

0 comments on commit 76cdd53

Please sign in to comment.