From 529d41ce3f58ae8c5da0fa0328a171e27109a06a Mon Sep 17 00:00:00 2001 From: Luciano Fiandesio Date: Thu, 14 Nov 2024 23:53:46 +0100 Subject: [PATCH] feat: add year column to allow Doris partitioning [DHIS2-16705] (#19158) --- .../table/AbstractJdbcTableManager.java | 20 +++++++++++++++++++ ...dbcTrackedEntityAnalyticsTableManager.java | 3 +++ ...ntityEnrollmentsAnalyticsTableManager.java | 4 +++- ...ckedEntityEventsAnalyticsTableManager.java | 5 +++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java index 5a5d59f0fe9b..c88fcd248943 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java @@ -32,7 +32,9 @@ import static org.hisp.dhis.analytics.table.util.PartitionUtils.getStartDate; import static org.hisp.dhis.commons.util.TextUtils.format; import static org.hisp.dhis.db.model.DataType.CHARACTER_11; +import static org.hisp.dhis.db.model.DataType.INTEGER; import static org.hisp.dhis.db.model.DataType.TEXT; +import static org.hisp.dhis.db.model.constraint.Nullable.NOT_NULL; import static org.hisp.dhis.util.DateUtils.toLongDate; import java.util.Collection; @@ -732,6 +734,24 @@ protected String replaceQualify(String template, Map variables) return TextUtils.replace(template, map); } + protected AnalyticsTableColumn getPartitionColumn() { + return AnalyticsTableColumn.builder() + .name("year") + .dataType(INTEGER) + .nullable(NOT_NULL) + // The expression should use sqlBuilder, but the concept of functions (like YEAR) + // is part of the previous PR (https://github.com/dhis2/dhis2-core/pull/19131/files) + .selectExpression( + """ + CASE + WHEN ev.status = 'SCHEDULE' THEN YEAR(ev.scheduleddate) + ELSE YEAR(ev.occurreddate) + END + """) + .skipIndex(Skip.SKIP) + .build(); + } + // ------------------------------------------------------------------------- // Private supportive methods // ------------------------------------------------------------------------- diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java index f6361f8aba1c..fc303bd6f90c 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java @@ -376,6 +376,9 @@ private List getColumns( .toList()); columns.addAll(getOrganisationUnitGroupSetColumns()); + if (sqlBuilder.supportsDeclarativePartitioning()) { + columns.add(getPartitionColumn()); + } return columns; } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java index 694c8e481bad..89502457e852 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java @@ -246,7 +246,9 @@ private List getColumns() { List columns = new ArrayList<>(); columns.addAll(FIXED_COLS); columns.add(getOrganisationUnitNameHierarchyColumn()); - + if (sqlBuilder.supportsDeclarativePartitioning()) { + columns.add(getPartitionColumn()); + } return columns; } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java index 549cd6d8fa4c..096e1f7e0a5e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java @@ -370,6 +370,11 @@ private List getDataYears(AnalyticsTableUpdateParams params, TrackedEnt private List getColumns() { List columns = new ArrayList<>(); columns.addAll(FIXED_COLS); + + if (sqlBuilder.supportsDeclarativePartitioning()) { + columns.add(getPartitionColumn()); + } + columns.add(getOrganisationUnitNameHierarchyColumn()); return columns;