diff --git a/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramSqlGeneratorFunctionsTest.java b/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramSqlGeneratorFunctionsTest.java index 008625291277..303ce975fafb 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramSqlGeneratorFunctionsTest.java +++ b/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramSqlGeneratorFunctionsTest.java @@ -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")); } diff --git a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilder.java b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilder.java index 534fcb771494..d96e8dc97747 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilder.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilder.java @@ -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); diff --git a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java index ade5a4845a38..2fb726835628 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java @@ -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); }; } diff --git a/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilderTest.java b/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilderTest.java index c09d49f79383..8b77a3c0bd8d 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilderTest.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilderTest.java @@ -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 diff --git a/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/DorisSqlBuilderTest.java b/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/DorisSqlBuilderTest.java index 4ac1afc8921f..3715c3b7000e 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/DorisSqlBuilderTest.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/DorisSqlBuilderTest.java @@ -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 columns = @@ -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 diff --git a/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/PostgreSqlBuilderTest.java b/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/PostgreSqlBuilderTest.java index 7f3e4ff31ff1..1cc1841f202b 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/PostgreSqlBuilderTest.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/test/java/org/hisp/dhis/db/sql/PostgreSqlBuilderTest.java @@ -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 columns = @@ -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