Skip to content

Commit

Permalink
fix: Use toFloat64 vs toDecimal64 to avoid precision issues [DHIS2-18…
Browse files Browse the repository at this point in the history
…417] (#19525)
  • Loading branch information
larshelge authored Dec 18, 2024
1 parent 7a2235f commit c243461
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void testConditionWithBooleanAsBoolean() {
sql,
is(
"case when (coalesce("
+ "case when ax.\"ps\" = 'ProgrmStagA' then \"DataElmentE\" else null end::numeric!=0,false)) "
+ "case when ax.\"ps\" = 'ProgrmStagA' then \"DataElmentE\" else null end::numeric != 0,false)) "
+ "then 10 + 5 else 3 * 2 end"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public String jsonExtract(String json, String key, String property) {
@Override
public String cast(String column, DataType dataType) {
return switch (dataType) {
case NUMERIC -> String.format("toDecimal64(%s, 4)", column);
case NUMERIC -> String.format("toFloat64(%s)", column);
case BOOLEAN ->
String.format("toUInt8(%s) != 0", column); // ClickHouse uses UInt8 for boolean
case TEXT -> String.format("toString(%s)", column);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public String jsonExtract(String json, String key, String property) {
public String cast(String column, DataType dataType) {
return switch (dataType) {
case NUMERIC -> String.format("%s::numeric", column);
case BOOLEAN -> String.format("%s::numeric!=0", column);
case BOOLEAN -> String.format("%s::numeric != 0", column);
case TEXT -> String.format("%s::text", column);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,16 @@ void testJsonExtractObject() {
void testCast() {
assertEquals(
"""
toDecimal64(ax."qrur9Dvnyt5", 4)""",
toFloat64(ax."qrur9Dvnyt5")""",
sqlBuilder.cast("ax.\"qrur9Dvnyt5\"", org.hisp.dhis.analytics.DataType.NUMERIC));
assertEquals(
"""
toUInt8(ax."qrur9Dvnyt5") != 0""",
sqlBuilder.cast("ax.\"qrur9Dvnyt5\"", org.hisp.dhis.analytics.DataType.BOOLEAN));
assertEquals(
"""
toString(ax."qrur9Dvnyt5")""",
sqlBuilder.cast("ax.\"qrur9Dvnyt5\"", org.hisp.dhis.analytics.DataType.TEXT));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.junit.jupiter.api.Test;

class DorisSqlBuilderTest {
private final SqlBuilder sqlBuilder = new DorisSqlBuilder("pg_dhis", "postgresql.jar");
private final DorisSqlBuilder sqlBuilder = new DorisSqlBuilder("pg_dhis", "postgresql.jar");

private Table getTableA() {
List<Column> columns =
Expand Down Expand Up @@ -231,6 +231,14 @@ void testCast() {
"""
CAST(ax."qrur9Dvnyt5" AS DECIMAL)""",
sqlBuilder.cast("ax.\"qrur9Dvnyt5\"", org.hisp.dhis.analytics.DataType.NUMERIC));
assertEquals(
"""
CAST(ax."qrur9Dvnyt5" AS DECIMAL) != 0""",
sqlBuilder.cast("ax.\"qrur9Dvnyt5\"", org.hisp.dhis.analytics.DataType.BOOLEAN));
assertEquals(
"""
CAST(ax."qrur9Dvnyt5" AS CHAR)""",
sqlBuilder.cast("ax.\"qrur9Dvnyt5\"", org.hisp.dhis.analytics.DataType.TEXT));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import org.junit.jupiter.api.Test;

class PostgreSqlBuilderTest {
private final SqlBuilder sqlBuilder = new PostgreSqlBuilder();
private final PostgreSqlBuilder sqlBuilder = new PostgreSqlBuilder();

private Table getTableA() {
List<Column> columns =
Expand Down Expand Up @@ -257,6 +257,14 @@ void testCast() {
"""
ax."qrur9Dvnyt5"::numeric""",
sqlBuilder.cast("ax.\"qrur9Dvnyt5\"", org.hisp.dhis.analytics.DataType.NUMERIC));
assertEquals(
"""
ax."qrur9Dvnyt5"::numeric != 0""",
sqlBuilder.cast("ax.\"qrur9Dvnyt5\"", org.hisp.dhis.analytics.DataType.BOOLEAN));
assertEquals(
"""
ax."qrur9Dvnyt5"::text""",
sqlBuilder.cast("ax.\"qrur9Dvnyt5\"", org.hisp.dhis.analytics.DataType.TEXT));
}

@Test
Expand Down

0 comments on commit c243461

Please sign in to comment.