Skip to content
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

fix: Remove unnecessary subquery for event data value [DHIS2-18417] #19273

Merged
merged 177 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 165 commits
Commits
Show all changes
177 commits
Select commit Hold shift + click to select a range
b8a1966
fix: Update code
larshelge Nov 13, 2024
81e861b
fix: Update code
larshelge Nov 13, 2024
952657b
Merge branch 'master' into lars-dev
larshelge Nov 13, 2024
5eaeb3d
fix: Update code
larshelge Nov 13, 2024
2ac1d9d
fix: Update code
larshelge Nov 13, 2024
1a7ecbe
fix: Update code
larshelge Nov 13, 2024
3b1d516
fix: Update code
larshelge Nov 13, 2024
91e44ac
fix: Update code
larshelge Nov 13, 2024
1b46af6
fix: Update code
larshelge Nov 13, 2024
334379c
Merge branch 'master' into lars-dev
larshelge Nov 13, 2024
32de857
fix: Update code
larshelge Nov 13, 2024
23408ba
fix: Update code
larshelge Nov 13, 2024
10990c4
fix: Update code
larshelge Nov 13, 2024
99afbad
fix: Update code
larshelge Nov 13, 2024
f16ef96
fix: Update code
larshelge Nov 13, 2024
e292a23
fix: Update code
larshelge Nov 13, 2024
486776f
Merge branch 'master' into lars-dev
larshelge Nov 13, 2024
ca741d1
fix: Update code
larshelge Nov 14, 2024
8fdc695
fix: Update code
larshelge Nov 14, 2024
2c8abe8
fix: Update code
larshelge Nov 14, 2024
1da4c6a
fix: Update code
larshelge Nov 14, 2024
7dab403
fix: Update code
larshelge Nov 14, 2024
cf9e3b6
fix: Update code
larshelge Nov 14, 2024
a025ce6
fix: Update code
larshelge Nov 14, 2024
5a19093
Merge branch 'master' into lars-dev
larshelge Nov 14, 2024
b48fea5
fix: Update code
larshelge Nov 14, 2024
5910541
Merge branch 'master' into lars-dev
larshelge Nov 14, 2024
5c98cbe
Merge branch 'master' into lars-dev
larshelge Nov 15, 2024
9a8f4e4
fix: Update code
larshelge Nov 15, 2024
33dd76a
fix: Update code
larshelge Nov 15, 2024
f3847d1
fix: Update code
larshelge Nov 15, 2024
e3e2215
fix: Update code
larshelge Nov 15, 2024
4213d99
Merge branch 'master' into lars-dev
larshelge Nov 15, 2024
612c587
fix: Update code
larshelge Nov 15, 2024
c4ecc24
Merge branch 'master' into lars-dev
larshelge Nov 15, 2024
fd347f5
fix: Update code
larshelge Nov 15, 2024
3df3f70
fix: Update code
larshelge Nov 15, 2024
127f25a
fix: Update code
larshelge Nov 17, 2024
2af1fec
fix: Update code
larshelge Nov 17, 2024
7649246
Merge branch 'master' into lars-dev
larshelge Nov 17, 2024
221c724
Merge branch 'master' into lars-dev
larshelge Nov 17, 2024
06bffce
fix: Update code
larshelge Nov 17, 2024
655fee2
fix: Update code
larshelge Nov 18, 2024
8cbda99
fix: Update code
larshelge Nov 18, 2024
0eb8bcc
fix: Update code
larshelge Nov 18, 2024
d26bcb3
fix: Update code
larshelge Nov 18, 2024
c0722ec
Merge branch 'master' into lars-dev
larshelge Nov 18, 2024
e9423c3
fix: Update code
larshelge Nov 18, 2024
2c20571
fix: Update code
larshelge Nov 18, 2024
40d6c33
fix: Update code
larshelge Nov 18, 2024
6f5a1d6
Merge branch 'master' into lars-dev
larshelge Nov 18, 2024
f532475
fix: Update code
larshelge Nov 18, 2024
51e26bc
fix: Update code
larshelge Nov 18, 2024
6a2ace3
fix: Update code
larshelge Nov 18, 2024
3ceff8d
fix: Update code
larshelge Nov 18, 2024
fa4ffb9
Merge branch 'master' into lars-dev
larshelge Nov 18, 2024
13f8a01
fix: Update code
larshelge Nov 18, 2024
e27caad
fix: Update code
larshelge Nov 18, 2024
2ae8e58
Merge branch 'master' into lars-dev
larshelge Nov 18, 2024
5ed2410
fix: Update code
larshelge Nov 18, 2024
c828aa6
fix: Update code
larshelge Nov 18, 2024
8ce9a2d
fix: Update code
larshelge Nov 18, 2024
aaa7557
fix: Update code
larshelge Nov 18, 2024
b5a0cd2
Merge branch 'master' into lars-dev
larshelge Nov 18, 2024
fbcc596
fix: Update code
larshelge Nov 18, 2024
15dba5a
fix: Update code
larshelge Nov 18, 2024
b4ebd28
fix: Update code
larshelge Nov 18, 2024
e55866f
fix: Update code
larshelge Nov 18, 2024
001db10
Merge branch 'master' into lars-dev
larshelge Nov 18, 2024
387e0f6
fix: Update code
larshelge Nov 18, 2024
c9ac624
Merge branch 'master' into lars-dev
larshelge Nov 19, 2024
ff84d2a
fix: Update code
larshelge Nov 19, 2024
e13eabb
fix: Update code
larshelge Nov 19, 2024
5969b6a
fix: Update code
larshelge Nov 19, 2024
7c23c8b
fix: Update code
larshelge Nov 19, 2024
f892775
fix: Update code
larshelge Nov 19, 2024
c7dacee
Merge branch 'master' into lars-dev
larshelge Nov 19, 2024
7496a38
Merge branch 'master' into lars-dev
larshelge Nov 19, 2024
463c511
fix: Update code
larshelge Nov 19, 2024
7107ded
fix: Update code
larshelge Nov 19, 2024
d388a71
fix: Update code
larshelge Nov 19, 2024
30c7a56
fix: Update code
larshelge Nov 19, 2024
fcc344c
fix: Update code
larshelge Nov 19, 2024
ef53068
fix: Update code
larshelge Nov 19, 2024
d7aeca2
fix: Update code
larshelge Nov 19, 2024
8a3e048
fix: Update code
larshelge Nov 19, 2024
a1e925d
Merge branch 'master' into lars-dev
larshelge Nov 19, 2024
0e88ebb
fix: Update code
larshelge Nov 19, 2024
26805ce
fix: Update code
larshelge Nov 19, 2024
ade5f9e
fix: Update code
larshelge Nov 20, 2024
0d80d0d
Merge branch 'master' into lars-dev
larshelge Nov 20, 2024
36309e9
fix: Update code
larshelge Nov 20, 2024
0f53e47
Merge branch 'master' into lars-dev
larshelge Nov 20, 2024
403a868
Merge branch 'master' into lars-dev
larshelge Nov 20, 2024
36e3f11
fix: Update code
larshelge Nov 21, 2024
d2f49d5
fix: Update code
larshelge Nov 21, 2024
9f44151
Merge branch 'master' into lars-dev
larshelge Nov 21, 2024
d9ebb3c
Merge branch 'master' into lars-dev
larshelge Nov 22, 2024
c554378
fix: Update code
larshelge Nov 23, 2024
e2b4ccf
fix: Update code
larshelge Nov 23, 2024
1661309
fix: Update code
larshelge Nov 23, 2024
824df0a
fix: Update code
larshelge Nov 23, 2024
dbe3169
fix: Update code
larshelge Nov 23, 2024
b87bb9b
fix: Update code
larshelge Nov 23, 2024
f5d9436
fix: Update code
larshelge Nov 23, 2024
fa22cce
fix: Update code
larshelge Nov 23, 2024
84c1022
fix: Update code
larshelge Nov 23, 2024
38eabcc
fix: Update code
larshelge Nov 23, 2024
8e1ec27
fix: Update code
larshelge Nov 23, 2024
d947ea6
fix: Update code
larshelge Nov 23, 2024
710e4f5
Merge branch 'master' into lars-dev
larshelge Nov 23, 2024
a335311
fix: Update code
larshelge Nov 24, 2024
6d20921
fix: Update code
larshelge Nov 24, 2024
38c1df8
fix: Update code
larshelge Nov 24, 2024
49b58ea
fix: Update code
larshelge Nov 24, 2024
3b8c3ff
fix: Update code
larshelge Nov 24, 2024
3fec6c8
fix: Update code
larshelge Nov 24, 2024
87f0a72
fix: Update code
larshelge Nov 24, 2024
0dcaacf
fix: Update code
larshelge Nov 24, 2024
a397973
fix: Update code
larshelge Nov 24, 2024
74abf23
fix: Update code
larshelge Nov 24, 2024
a78d712
fix: Update code
larshelge Nov 24, 2024
4d6ce78
fix: Update code
larshelge Nov 24, 2024
7adfda5
fix: Update code
larshelge Nov 24, 2024
11f688f
fix: Update code
larshelge Nov 24, 2024
db7a54b
fix: Update code
larshelge Nov 24, 2024
243dc42
fix: Update code
larshelge Nov 24, 2024
5f6b566
fix: Update code
larshelge Nov 24, 2024
3e98d63
fix: Update code
larshelge Nov 24, 2024
089182d
fix: Update code
larshelge Nov 24, 2024
eb0bd0d
fix: Update code
larshelge Nov 24, 2024
6f72165
fix: Update code
larshelge Nov 24, 2024
719155b
fix: Update code
larshelge Nov 24, 2024
d811508
fix: Update code
larshelge Nov 24, 2024
5df4735
fix: Update code
larshelge Nov 24, 2024
49569a1
fix: Update code
larshelge Nov 24, 2024
ec6c906
fix: Update code
larshelge Nov 24, 2024
7db77c5
fix: Update code
larshelge Nov 24, 2024
4d2ad59
fix: Update code
larshelge Nov 24, 2024
16445cd
Merge branch 'master' into lars-dev
larshelge Nov 24, 2024
c4a882f
Merge branch 'master' into DHIS2-18417
larshelge Nov 24, 2024
9af7540
fix: Update code
larshelge Nov 24, 2024
a5ac41c
fix: Update code
larshelge Nov 24, 2024
952b0e8
fix: Update code
larshelge Nov 24, 2024
c7e5bf0
fix: Update code
larshelge Nov 24, 2024
4e2b03f
fix: Update code
larshelge Nov 24, 2024
f7aa4c5
fix: Update code
larshelge Nov 24, 2024
f767809
fix: Update code
larshelge Nov 24, 2024
4b2fea0
Merge branch 'master' into DHIS2-18417
larshelge Nov 24, 2024
3c7e37d
fix: Update code
larshelge Nov 24, 2024
fcecb69
fix: Update
larshelge Nov 25, 2024
576cc69
fix: Update code
larshelge Nov 25, 2024
879b90c
fix: Update code
larshelge Nov 25, 2024
8df4aad
Merge branch 'lars-dev' into DHIS2-18417
larshelge Nov 25, 2024
bba50e4
fix: Update code
larshelge Nov 25, 2024
17322b7
Merge branch 'master' into lars-dev
larshelge Nov 25, 2024
5840c47
Merge branch 'master' into lars-dev
larshelge Nov 25, 2024
b69948d
fix: Update code
larshelge Nov 25, 2024
c1ce2de
fix: Update code
larshelge Nov 25, 2024
8582429
fix: Update code
larshelge Nov 25, 2024
6ab60b0
fix: Update code
larshelge Nov 25, 2024
743aeb5
fix: Update code
larshelge Nov 25, 2024
8872a71
fix: Update code
larshelge Nov 25, 2024
67b10fe
fix: Update code
larshelge Nov 25, 2024
97f4851
fix: Update code
larshelge Nov 25, 2024
a177b59
fix: Update code
larshelge Nov 25, 2024
3ad9606
fix: Update code
larshelge Nov 25, 2024
fe13066
fix: Update code
larshelge Nov 25, 2024
0629269
fix: Update code
larshelge Nov 25, 2024
8be2a6d
fix: Update code
larshelge Nov 25, 2024
e78c0df
fix: Update code
larshelge Nov 25, 2024
151c811
fix: Update code
larshelge Nov 25, 2024
7f852b7
fix: Update code
larshelge Nov 25, 2024
36cc799
fix: Update code
larshelge Nov 25, 2024
8e24e08
fix: Update code
larshelge Nov 25, 2024
0106716
fix: Update code
larshelge Nov 25, 2024
b38684f
fix: Update code
larshelge Nov 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,18 @@ protected String getSelectExpressionForAttribute(ValueType valueType, String col
* @return a select expression appropriate for the given value type and context.
*/
private String getSelectExpression(ValueType valueType, String columnExpression, boolean isTea) {
String doubleType = sqlBuilder.dataTypeDouble();

if (valueType.isDecimal()) {
return "cast(" + columnExpression + " as " + doubleType + ")";
return getCastExpression(columnExpression, NUMERIC_REGEXP, sqlBuilder.dataTypeDouble());
} else if (valueType.isInteger()) {
return "cast(" + columnExpression + " as bigint)";
return getCastExpression(columnExpression, NUMERIC_REGEXP, sqlBuilder.dataTypeBigInt());
} else if (valueType.isBoolean()) {
return "case when "
+ columnExpression
+ " = 'true' then 1 when "
+ columnExpression
+ " = 'false' then 0 else null end";
} else if (valueType.isDate()) {
return "cast(" + columnExpression + " as " + sqlBuilder.dataTypeTimestamp() + ")";
return getCastExpression(columnExpression, DATE_REGEXP, sqlBuilder.dataTypeTimestamp());
} else if (valueType.isGeo() && isSpatialSupport()) {
return "ST_GeomFromGeoJSON('{\"type\":\"Point\", \"coordinates\":' || ("
+ columnExpression
Expand All @@ -179,6 +177,20 @@ private String getSelectExpression(ValueType valueType, String columnExpression,
}
}

/**
* Returns a cast expression which includes a value filter for the given value type.
*
* @param columnExpression the column expression.
* @param filterRegex the value type filter regular expression.
* @param dataType the SQL data type.
* @return a cast and validate expression.
*/
String getCastExpression(String columnExpression, String filterRegex, String dataType) {
String filter = sqlBuilder.regexpMatch(columnExpression, filterRegex);
return String.format(
"case when %s then cast(%s as %s) else null end", filter, columnExpression, dataType);
}

@Override
public boolean validState() {
return tableIsNotEmpty("event");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ private String getSelectForInsert(
String sqlTemplate =
dataElement.getValueType().isOrganisationUnit()
? "(select ${selectExpression} ${dataClause})${closingParentheses} as ${uid}"
: "(select ${selectExpression} from ${event} where eventid=ev.eventid ${dataClause})${closingParentheses} as ${uid}";
: "${selectExpression}${closingParentheses} as ${uid}";

return replaceQualify(
sqlTemplate,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.analytics.table;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.hisp.dhis.common.ValueType;
import org.hisp.dhis.db.sql.PostgreSqlBuilder;
import org.hisp.dhis.db.sql.SqlBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class AbstractEventJdbcTableManagerTest {

@Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder();

@InjectMocks private JdbcEventAnalyticsTableManager manager;

@Test
void testGetCastExpression() {
String expected =
"""
case when eventdatavalues #>> '{GieVkTxp4HH, value}' ~* '^(-?[0-9]+)(\\.[0-9]+)?$' \
then cast(eventdatavalues #>> '{GieVkTxp4HH, value}' as double precision) \
else null end""";

String actual =
manager.getCastExpression(
"eventdatavalues #>> '{GieVkTxp4HH, value}'",
"'^(-?[0-9]+)(\\.[0-9]+)?$'",
"double precision");

assertEquals(expected, actual);
}

@Test
void testGetSelectExpressionNumber() {
String expected =
"""
case when eventdatavalues #>> '{GieVkTxp4HH, value}' ~* '^(-?[0-9]+)(\\.[0-9]+)?$' \
then cast(eventdatavalues #>> '{GieVkTxp4HH, value}' as double precision) \
else null end""";

String actual =
manager.getSelectExpression(ValueType.NUMBER, "eventdatavalues #>> '{GieVkTxp4HH, value}'");

assertEquals(expected, actual);
}

@Test
void testGetSelectExpressionDate() {
String expected =
"""
case when eventdatavalues #>> '{AL04Wbutskk, value}' ~* '^\\d{4}-\\d{2}-\\d{2}(\\s|T)?((\\d{2}:)(\\d{2}:)?(\\d{2}))?(|.(\\d{3})|.(\\d{3})Z)?$' \
then cast(eventdatavalues #>> '{AL04Wbutskk, value}' as timestamp) \
else null end""";

String actual =
manager.getSelectExpression(ValueType.DATE, "eventdatavalues #>> '{AL04Wbutskk, value}'");

assertEquals(expected, actual);
}

@Test
void testGetSelectExpressionText() {
String expected =
"""
eventdatavalues #>> '{FwUzmc49Pcr, value}'""";

String actual =
manager.getSelectExpression(ValueType.TEXT, "eventdatavalues #>> '{FwUzmc49Pcr, value}'");

assertEquals(expected, actual);
}
}
Loading