Skip to content

Commit bd144e7

Browse files
committed
feat: cleanup / review todos
1 parent 1473df6 commit bd144e7

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

models/cohortdata.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (h CohortData) RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlus
104104
omopDataSource := dataSourceModel.GetDataSource(sourceId, Omop)
105105
resultsDataSource := dataSourceModel.GetDataSource(sourceId, Results)
106106
finalSetAlias := "final_set_alias"
107-
histogramConcept, err := utils.GetLastCustomConceptVariableDef(filterConceptDefsAndCohortPairs)
107+
histogramConcept, err := utils.CheckAndGetLastCustomConceptVariableDef(filterConceptDefsAndCohortPairs)
108108
if err != nil {
109109
log.Fatalf("failed: %v", err)
110110
return nil, err

models/helper.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,24 @@ func QueryFilterByConceptDefHelper(query *gorm.DB, sourceId int, filterConceptDe
7070
simpleFilterConceptDef := utils.CustomConceptVariableDef{ConceptId: filterConceptDef.ConceptId}
7171
query.Select(fmt.Sprintf("%s.person_id, %s.observation_concept_id, %s.value_as_number ",
7272
observationTableAlias+"_a", observationTableAlias+"_a", observationTableAlias+"_a"))
73-
query := QueryFilterByConceptDefHelper2(query, sourceId, simpleFilterConceptDef,
74-
omopDataSource, "", personIdFieldForObservationJoin, "observation_continuous", observationTableAlias+"_a")
73+
query := queryJoinAndFilterByConceptDef(query, sourceId, simpleFilterConceptDef,
74+
omopDataSource, personIdFieldForObservationJoin, "observation_continuous", observationTableAlias+"_a")
7575
tmpTransformedTable, err := TransformDataIntoTempTable(omopDataSource, query, filterConceptDef)
76-
// TODO - the resulting query should actually be Select * from temptable.... as this collapses all underlying queries. TODO2 - ensure the transform method also filters....
77-
query = QueryFilterByConceptDefHelper2(query, sourceId, filterConceptDef, //TODO - turn around
78-
omopDataSource, "", personIdFieldForObservationJoin, tmpTransformedTable, observationTableAlias+"_b")
76+
// TODO - the resulting query should actually be Select * from temptable.... as this collapses all underlying queries.
77+
query = queryJoinAndFilterByConceptDef(query, sourceId, filterConceptDef,
78+
omopDataSource, personIdFieldForObservationJoin, tmpTransformedTable, observationTableAlias+"_b")
7979
return query, observationTableAlias + "_b", err
8080

8181
} else {
8282
// simple filterConceptDef with no transformation
83-
query := QueryFilterByConceptDefHelper2(query, sourceId, filterConceptDef,
84-
omopDataSource, "", personIdFieldForObservationJoin, "observation_continuous", observationTableAlias)
83+
query := queryJoinAndFilterByConceptDef(query, sourceId, filterConceptDef,
84+
omopDataSource, personIdFieldForObservationJoin, "observation_continuous", observationTableAlias)
8585
return query, "", nil
8686
}
8787
}
8888

89-
func QueryFilterByConceptDefHelper2(query *gorm.DB, sourceId int, filterConceptDef utils.CustomConceptVariableDef,
90-
omopDataSource *utils.DbAndSchema, resultSchemaName string, personIdFieldForObservationJoin string, observationDataSource string, observationTableAlias string) *gorm.DB {
89+
func queryJoinAndFilterByConceptDef(query *gorm.DB, sourceId int, filterConceptDef utils.CustomConceptVariableDef,
90+
omopDataSource *utils.DbAndSchema, personIdFieldForObservationJoin string, observationDataSource string, observationTableAlias string) *gorm.DB {
9191
log.Printf("Adding extra INNER JOIN with alias %s", observationTableAlias)
9292
aliasedObservationDataSource := omopDataSource.Schema + "." + observationDataSource + " as " + observationTableAlias + omopDataSource.GetViewDirective()
9393
// for temp table, the alias is slightly different:
@@ -131,7 +131,7 @@ func TransformDataIntoTempTable(omopDataSource *utils.DbAndSchema, query *gorm.D
131131
// Generate a unique hash key based on the query and transformation
132132
querySQL := utils.ToSQL(query)
133133
queryKey := fmt.Sprintf("%s|%s", querySQL, filterConceptDef.Transformation)
134-
cacheKey := utils.GenerateHash(queryKey) // TODO - review
134+
cacheKey := utils.GenerateHash(queryKey)
135135

136136
// Check if the temporary table already exists in the cache
137137
if cachedTableName, exists := utils.TempTableCache.Get(cacheKey); exists {
@@ -192,7 +192,7 @@ func CreateAndFillTempTable(omopDataSource *utils.DbAndSchema, query *gorm.DB, t
192192
case "box_cox":
193193
// Placeholder: implement Box-Cox transformation logic as per requirements
194194
log.Printf("box_cox transformation logic needs to be implemented")
195-
return ""
195+
panic("error")
196196
default:
197197
log.Fatalf("Unsupported transformation type: %s", filterConceptDef.Transformation)
198198
panic("error")
@@ -212,7 +212,7 @@ func TempTableSQLAndFinalName(omopDataSource *utils.DbAndSchema, tempTableName s
212212
tempTableName, selectStatement, fromSQL, extraWhereSQL,
213213
)
214214
if omopDataSource.Vendor == "sqlserver" {
215-
finalTempTableName = "##" + tempTableName // Global temp table for MSSQL
215+
finalTempTableName = "##" + tempTableName
216216
tempTableSQL = fmt.Sprintf(
217217
"SELECT %s INTO %s FROM (%s) T WHERE %s",
218218
selectStatement, finalTempTableName, fromSQL, extraWhereSQL,
@@ -231,6 +231,7 @@ func QueryFilterByConceptDefsHelper(query *gorm.DB, sourceId int, filterConceptD
231231
return query
232232
}
233233

234+
// DEPRECATED - use QueryFilterByConceptDefsPlusCohortPairsHelper instead
234235
// Helper function that adds extra filter clauses to the query, for the given filterCohortPairs, intersecting on the
235236
// right set of tables, excluding data where necessary, etc.
236237
// It basically iterates over the list of filterCohortPairs, adding relevant INTERSECT and EXCEPT clauses, so that the resulting set is the
@@ -247,7 +248,6 @@ func QueryFilterByCohortPairsHelper(filterCohortPairs []utils.CustomDichotomousV
247248
return query
248249
}
249250

250-
// TODO - remove code duplication above
251251
func QueryFilterByCohortPairHelper(query *gorm.DB, filterCohortPair utils.CustomDichotomousVariableDef, resultsDataSource *utils.DbAndSchema, cohortDefinitionId int, personIdFieldForObservationJoin string, unionAndIntersectSQLAlias string) *gorm.DB {
252252
unionAndIntersectSQL := "(" +
253253
"SELECT subject_id FROM " + resultsDataSource.Schema + ".cohort WHERE cohort_definition_id=? "

tests/models_tests/models_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func TestQueryFilterByCohortPairsHelper(t *testing.T) {
258258
var subjectIds []*SubjectId
259259
population := largestCohort
260260
query := models.QueryFilterByCohortPairsHelper(filterCohortPairs, resultsDataSource, population.Id, "unionAndIntersect").
261-
Select("*")
261+
Select("unionAndIntersect.subject_id")
262262
_ = query.Scan(&subjectIds)
263263
// ...so we expect overlap the size of the largestCohort:
264264
if len(subjectIds) != largestCohort.CohortSize {
@@ -280,7 +280,7 @@ func TestQueryFilterByCohortPairsHelper(t *testing.T) {
280280
population = largestCohort
281281
resultsDataSource = tests.GetResultsDataSource()
282282
query = models.QueryFilterByCohortPairsHelper(filterCohortPairs, resultsDataSource, population.Id, "unionAndIntersect").
283-
Select("*")
283+
Select("unionAndIntersect.subject_id")
284284
_ = query.Scan(&subjectIds)
285285
// in this case we expect overlap the size of the largestCohort-6 (where 6 is the size of the overlap between extendedCopyOfSecondLargestCohort and largestCohort):
286286
if len(subjectIds) != (largestCohort.CohortSize - 6) {
@@ -302,7 +302,7 @@ func TestQueryFilterByCohortPairsHelper(t *testing.T) {
302302
population = largestCohort
303303
resultsDataSource = tests.GetResultsDataSource()
304304
query = models.QueryFilterByCohortPairsHelper(filterCohortPairs, resultsDataSource, population.Id, "unionAndIntersect").
305-
Select("*")
305+
Select("unionAndIntersect.subject_id")
306306
_ = query.Scan(&subjectIds)
307307
// in this case we expect same as previous test above:
308308
if len(subjectIds) != (largestCohort.CohortSize - 6) {
@@ -320,7 +320,7 @@ func TestQueryFilterByCohortPairsHelper(t *testing.T) {
320320
population = extendedCopyOfSecondLargestCohort
321321
resultsDataSource = tests.GetResultsDataSource()
322322
query = models.QueryFilterByCohortPairsHelper(filterCohortPairs, resultsDataSource, population.Id, "unionAndIntersect").
323-
Select("*")
323+
Select("unionAndIntersect.subject_id")
324324
_ = query.Scan(&subjectIds)
325325
// in this case we expect overlap the size of the extendedCopyOfSecondLargestCohort.CohortSize - secondLargestCohort.CohortSize:
326326
if len(subjectIds) != (extendedCopyOfSecondLargestCohort.CohortSize - secondLargestCohort.CohortSize) {
@@ -342,7 +342,7 @@ func TestQueryFilterByCohortPairsHelper(t *testing.T) {
342342
population = extendedCopyOfSecondLargestCohort
343343
resultsDataSource = tests.GetResultsDataSource()
344344
query = models.QueryFilterByCohortPairsHelper(filterCohortPairs, resultsDataSource, population.Id, "unionAndIntersect").
345-
Select("*")
345+
Select("unionAndIntersect.subject_id")
346346
_ = query.Scan(&subjectIds)
347347
// in this case we expect overlap the size to be 0, since all items remaining from first pair happen to overlap with largestCohort and are therefore excluded (pair overlap is excluded):
348348
if len(subjectIds) != 0 {
@@ -354,7 +354,7 @@ func TestQueryFilterByCohortPairsHelper(t *testing.T) {
354354
population = largestCohort
355355
resultsDataSource = tests.GetResultsDataSource()
356356
query = models.QueryFilterByCohortPairsHelper(filterCohortPairs, resultsDataSource, population.Id, "unionAndIntersect").
357-
Select("*")
357+
Select("unionAndIntersect.subject_id")
358358
_ = query.Scan(&subjectIds)
359359
// in this case we expect overlap the size to be 0 as explained in comment above:
360360
if len(subjectIds) != 0 {
@@ -367,7 +367,7 @@ func TestQueryFilterByCohortPairsHelper(t *testing.T) {
367367
population = largestCohort
368368
resultsDataSource = tests.GetResultsDataSource()
369369
query = models.QueryFilterByCohortPairsHelper(filterCohortPairs, resultsDataSource, population.Id, "unionAndIntersect").
370-
Select("*")
370+
Select("unionAndIntersect.subject_id")
371371
_ = query.Scan(&subjectIds)
372372
// in this case we expect overlap the size to be the size of the cohort, since there are no filtering pairs:
373373
if len(subjectIds) != largestCohort.CohortSize {
@@ -385,7 +385,7 @@ func TestQueryFilterByCohortPairsHelper(t *testing.T) {
385385
population = largestCohort
386386
resultsDataSource = tests.GetResultsDataSource()
387387
query = models.QueryFilterByCohortPairsHelper(filterCohortPairs, resultsDataSource, population.Id, "unionAndIntersect").
388-
Select("*")
388+
Select("unionAndIntersect.subject_id")
389389
_ = query.Scan(&subjectIds)
390390
// in this case we expect overlap the size to be 0, since the pair is composed of the same cohort in CohortDefinitionId1 and CohortDefinitionId2 and their overlap is excluded:
391391
if len(subjectIds) != 0 {
@@ -403,7 +403,7 @@ func TestQueryFilterByCohortPairsHelper(t *testing.T) {
403403
population = smallestCohort
404404
resultsDataSource = tests.GetResultsDataSource()
405405
query = models.QueryFilterByCohortPairsHelper(filterCohortPairs, resultsDataSource, population.Id, "unionAndIntersect").
406-
Select("*")
406+
Select("unionAndIntersect.subject_id")
407407
_ = query.Scan(&subjectIds)
408408
// in this case we expect overlap the size to be 0, since the cohorts in the pair do not overlap with the population:
409409
if len(subjectIds) != 0 {

utils/parsing.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,8 @@ func GenerateSynchronizedTimestampID() string {
480480
return fmt.Sprintf("%x", time.Now().UnixNano())
481481
}
482482

483-
// GetLastCustomConceptVariableDef checks if the last item is of type CustomConceptVariableDef and returns it
484-
func GetLastCustomConceptVariableDef(filterConceptDefsAndCohortPairs []interface{}) (*CustomConceptVariableDef, error) {
483+
// checks if the last item is of type CustomConceptVariableDef and returns it
484+
func CheckAndGetLastCustomConceptVariableDef(filterConceptDefsAndCohortPairs []interface{}) (*CustomConceptVariableDef, error) {
485485
if len(filterConceptDefsAndCohortPairs) == 0 {
486486
return nil, fmt.Errorf("the slice is empty")
487487
}

0 commit comments

Comments
 (0)