Skip to content

Commit

Permalink
fix: Use String for JSON data type for ClickHouse [DHIS2-18417] (#19500)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge authored Dec 16, 2024
1 parent ae7bf7b commit dabc7b0
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class JdbcOwnershipAnalyticsTableManager extends AbstractEventJdbcTableMa
AnalyticsTableColumn.builder()
.name("teuid")
.dataType(CHARACTER_11)
.nullable(NOT_NULL)
.selectExpression("te.uid")
.build(),
AnalyticsTableColumn.builder()
Expand Down Expand Up @@ -250,16 +251,17 @@ private String getInputSql(Program program) {
\sfrom (\
select h.trackedentityid, '${historyTableId}' as startdate, h.enddate as enddate, h.organisationunitid \
from ${programownershiphistory} h \
where h.programid=${programId} \
where h.programid = ${programId} \
and h.organisationunitid is not null \
union \
select o.trackedentityid, '${trackedEntityOwnTableId}' as startdate, null as enddate, o.organisationunitid \
from ${trackedentityprogramowner} o \
where o.programid=${programId} \
where o.programid = ${programId} \
and exists (\
select 1 from ${programownershiphistory} p \
where o.trackedentityid = p.trackedentityid \
and p.programid=${programId} and p.organisationunitid is not null)) a \
and p.programid = ${programId} \
and p.organisationunitid is not null)) a \
inner join ${trackedentity} te on a.trackedentityid = te.trackedentityid \
inner join ${organisationunit} ou on a.organisationunitid = ou.organisationunitid \
left join analytics_rs_orgunitstructure ous on a.organisationunitid = ous.organisationunitid \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,16 @@ void testPopulateTable() throws SQLException {
select te.uid,a.startdate,a.enddate,ou.uid from (\
select h.trackedentityid, '1001-01-01' as startdate, h.enddate as enddate, h.organisationunitid \
from "programownershiphistory" h \
where h.programid=0 and h.organisationunitid is not null \
where h.programid = 0 \
and h.organisationunitid is not null \
union \
select o.trackedentityid, '2002-02-02' as startdate, null as enddate, o.organisationunitid \
from "trackedentityprogramowner" o \
where o.programid=0 \
where o.programid = 0 \
and exists (select 1 from "programownershiphistory" p \
where o.trackedentityid = p.trackedentityid \
and p.programid=0 and p.organisationunitid is not null)\
and p.programid = 0 \
and p.organisationunitid is not null)\
) a \
inner join "trackedentity" te on a.trackedentityid = te.trackedentityid \
inner join "organisationunit" ou on a.organisationunitid = ou.organisationunitid \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
<many-to-one name="program" class="org.hisp.dhis.program.Program"
column="programid" foreign-key="fk_programownershiphistory_programid" />

<many-to-one name="trackedEntity"
class="org.hisp.dhis.trackedentity.TrackedEntity" column="trackedentityid"
foreign-key="fk_programownershiphistory_trackedentityinstanceid" />
<many-to-one name="trackedEntity" class="org.hisp.dhis.trackedentity.TrackedEntity"
column="trackedentityid" foreign-key="fk_programownershiphistory_trackedentityinstanceid" />

<many-to-one name="organisationUnit"
class="org.hisp.dhis.organisationunit.OrganisationUnit" column="organisationunitid"
foreign-key="fk_programownershiphistory_organisationunitid" />
<many-to-one name="organisationUnit" class="org.hisp.dhis.organisationunit.OrganisationUnit"
column="organisationunitid" foreign-key="fk_programownershiphistory_organisationunitid" />

<property name="startDate" type="timestamp" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@
<generator class="native" />
</id>

<many-to-one name="trackedEntity"
class="org.hisp.dhis.trackedentity.TrackedEntity" column="trackedentityid"
foreign-key="fk_trackedentityprogramowner_trackedentityinstanceid"
lazy="false" unique-key="uk_tei_program" />
<many-to-one name="trackedEntity" class="org.hisp.dhis.trackedentity.TrackedEntity"
column="trackedentityid" foreign-key="fk_trackedentityprogramowner_trackedentityinstanceid"
lazy="false" unique-key="uk_tei_program" />

<many-to-one name="program" class="org.hisp.dhis.program.Program"
column="programid" not-null="true"
foreign-key="fk_trackedentityprogramowner_programid" lazy="false"
unique-key="uk_tei_program" />
column="programid" not-null="true" foreign-key="fk_trackedentityprogramowner_programid"
lazy="false" unique-key="uk_tei_program" />

<property name="created" type="timestamp" not-null="true" />

<property name="lastUpdated" type="timestamp" not-null="true" />

<many-to-one name="organisationUnit"
class="org.hisp.dhis.organisationunit.OrganisationUnit" column="organisationunitid"
foreign-key="fk_trackedentityprogramowner_organisationunitid" />
<many-to-one name="organisationUnit" class="org.hisp.dhis.organisationunit.OrganisationUnit"
column="organisationunitid" foreign-key="fk_trackedentityprogramowner_organisationunitid" />

<property name="createdBy" column="createdBy" not-null="true" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ public String dataTypeGeometryPoint() {
return "String";
}

/**
* ClickHouse JSON data type is in beta and is not yet production-ready. Check back and enable
* <code>JSON</code> when available.
*/
@Override
public String dataTypeJson() {
return "JSON";
return "String";
}

// Index functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private Table getTableD() {
void testDataTypes() {
assertEquals("Float64", sqlBuilder.dataTypeDouble());
assertEquals("DateTime64(3)", sqlBuilder.dataTypeTimestamp());
assertEquals("String", sqlBuilder.dataTypeJson());
}

// Index types
Expand Down Expand Up @@ -279,7 +280,7 @@ void testCreateTableA() {
String expected =
"""
create table "immunization" ("id" Int64 not null,"data" String not null,\
"period" String not null,"created" DateTime64(3) null,"user" JSON null,\
"period" String not null,"created" DateTime64(3) null,"user" String null,\
"value" Float64 null) \
engine = MergeTree() \
order by ("id");""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private Table getTableC() {
void testDataTypes() {
assertEquals("double", sqlBuilder.dataTypeDouble());
assertEquals("datetime", sqlBuilder.dataTypeTimestamp());
assertEquals("json", sqlBuilder.dataTypeJson());
}

// Index types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ private Table getTableC() {
void testDataTypes() {
assertEquals("double precision", sqlBuilder.dataTypeDouble());
assertEquals("geometry", sqlBuilder.dataTypeGeometry());
assertEquals("jsonb", sqlBuilder.dataTypeJson());
}

// Index types
Expand Down

0 comments on commit dabc7b0

Please sign in to comment.