-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DRAFT] fix: Use CTE in Enrollment analytics queries [DHIS-16705] #19519
base: master
Are you sure you want to change the base?
Conversation
|
||
void contributeCTE( | ||
ProgramIndicator programIndicator, | ||
RelationshipType relationshipType, |
Check notice
Code scanning / CodeQL
Useless parameter Note
void contributeCTE( | ||
ProgramIndicator programIndicator, | ||
RelationshipType relationshipType, | ||
AnalyticsType outerSqlEntity, |
Check notice
Code scanning / CodeQL
Useless parameter Note
...alytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java
Fixed
Show fixed
Hide fixed
...alytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java
Fixed
Show fixed
Hide fixed
return fromClause.toString(); | ||
} | ||
|
||
protected String getSortClause(EventQueryParams params) { |
Check notice
Code scanning / CodeQL
Missing Override annotation Note
AbstractJdbcEventAnalyticsManager.getSortClause
return ""; | ||
} | ||
|
||
protected String getSqlFilter(QueryFilter filter, QueryItem item) { |
Check notice
Code scanning / CodeQL
Missing Override annotation Note
AbstractJdbcEventAnalyticsManager.getSqlFilter
.../hisp/dhis/analytics/event/data/programindicator/DefaultProgramIndicatorSubqueryBuilder.java
Fixed
Show fixed
Hide fixed
8c33952
to
3e070ce
Compare
5170115
to
d8119c0
Compare
Quality Gate failedFailed conditions See analysis details on SonarQube Cloud Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE |
this.programStageUid = programStageUid; | ||
this.cteDefinition = cteDefinition; | ||
this.offset = offset; | ||
this.alias = new RandomStringGenerator.Builder().withinRange('a', 'z').build().generate(5); |
Check notice
Code scanning / CodeQL
Deprecated method or constructor invocation Note
Builder.build
this.programIndicatorUid = programIndicatorUid; | ||
this.programStageUid = null; | ||
this.offset = -999; | ||
this.alias = new RandomStringGenerator.Builder().withinRange('a', 'z').build().generate(5); |
Check notice
Code scanning / CodeQL
Deprecated method or constructor invocation Note
Builder.build
this.isProgramIndicator = true; | ||
} | ||
|
||
public CteDefinitionWithOffset(String cteFilterName, String cteDefinition, boolean isFilter) { |
Check notice
Code scanning / CodeQL
Useless parameter Note
this.programIndicatorUid = null; | ||
this.programStageUid = null; | ||
this.offset = -999; | ||
this.alias = new RandomStringGenerator.Builder().withinRange('a', 'z').build().generate(5); |
Check notice
Code scanning / CodeQL
Deprecated method or constructor invocation Note
Builder.build
/** | ||
* Returns a select SQL clause for the given query. | ||
* | ||
* @param params the {@link EventQueryParams}. | ||
*/ | ||
protected abstract String getSelectClause(EventQueryParams params); | ||
|
||
protected abstract String getColumnWithCte(QueryItem item, String suffix, CTEContext cteContext); |
Check notice
Code scanning / CodeQL
Useless parameter Note
@@ -579,6 +711,40 @@ | |||
return ColumnAndAlias.EMPTY; | |||
} | |||
|
|||
protected String getColumnWithCte(QueryItem item, String suffix, CTEContext cteContext) { |
Check notice
Code scanning / CodeQL
Missing Override annotation Note
AbstractJdbcEventAnalyticsManager.getColumnWithCte
|
||
for (QueryItem item : params.getItems()) { | ||
|
||
String itemId = item.getItem().getUid(); |
Check notice
Code scanning / CodeQL
Unread local variable Note
CTEContext cteContext) { | ||
|
||
// Generate a unique CTE name for this program indicator | ||
String cteName = "pi_" + programIndicator.getUid().toLowerCase(); |
Check notice
Code scanning / CodeQL
Unread local variable Note
WIP
Changes
This PR addresses the generation of SQL queries for Enrollment analytics
Problem
Currently, the Enrollment queries are structured so that sub-queries are used to fetch events values from the
analytics_event_*
tables.For instance:
The above query works in Postgres but does not work in Doris, because correlation with outer layers of the parent query is not supported.
Mitigation
The current approach is trying to refactor the Enrollment query so that the subqueries (both as select and as where conditions) are "moved" into CTE (Common Table Expressions).
Common Table Expressions are temporary result sets in SQL, defined within a
WITH
clause, that simplify complex queries by improving readability and modularizing logic. They are particularly useful for recursive queries and can be referenced multiple times within the main query.The above query can be rewritten like so:
The above query structure is compatible with Doris and it makes the execution of the query faster.
As a comparison:
Original query with sub-select
Refactored query with CTEs
Testing strategy
I am using the
e2e
project and aim at having 100% green tests on both Postgres and Doris.