Skip to content

Commit

Permalink
fix: fix scenario with multiple filters
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterlukasse committed Jan 23, 2025
1 parent 0763d91 commit 96d3cd8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
2 changes: 2 additions & 0 deletions models/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func QueryFilterByConceptDefHelper(query *gorm.DB, sourceId int, filterConceptDe
if filterConceptDef.Transformation != "" {
// simple filterConceptDef with just the concept id
simpleFilterConceptDef := utils.CustomConceptVariableDef{ConceptId: filterConceptDef.ConceptId}
query.Select(fmt.Sprintf("%s.person_id, %s.observation_concept_id, %s.value_as_number ",
observationTableAlias+"_a", observationTableAlias+"_a", observationTableAlias+"_a"))
query := QueryFilterByConceptDefHelper2(query, sourceId, simpleFilterConceptDef,
omopDataSource, "", personIdFieldForObservationJoin, "observation_continuous", observationTableAlias+"_a")
tmpTransformedTable, err := TransformDataIntoTempTable(omopDataSource, query, filterConceptDef)
Expand Down
32 changes: 32 additions & 0 deletions tests/models_tests/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,38 @@ func TestRetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlusCohortPairs
if len(data) != 2 {
t.Errorf("expected 2 histogram data but got %d", len(data))
}

// now filter repeated twice:
filterConceptDefsPlusCohortPairs = []interface{}{
utils.CustomConceptVariableDef{
ConceptId: histogramConceptId,
Filters: []utils.Filter{
{
Type: ">=",
Value: utils.Float64Ptr(1.0),
},
},
Transformation: "z_score",
}, // ^ this filter will narrow down to 2 records
utils.CustomConceptVariableDef{
ConceptId: histogramConceptId,
Filters: []utils.Filter{
{
Type: ">=",
Value: utils.Float64Ptr(0.7),
},
},
Transformation: "z_score", // > a subsequent transformation of z_score on the remaining 2 records, will result
// in different values compared to the first filter above, as the scores are now
// calculated over two values only
},
}
data, _ = cohortDataModel.RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlusCohortPairs(testSourceId, largestCohort.Id, histogramConceptId, filterConceptDefsPlusCohortPairs)
// make sure the filter worked on transformed values:
if len(data) != 1 {
t.Errorf("expected 1 histogram data but got %d", len(data))
}

}

func TestRetrieveHistogramDataBySourceIdAndConceptId(t *testing.T) {
Expand Down
11 changes: 1 addition & 10 deletions utils/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (h DbAndSchema) GetViewDirective() string {
return ""
}
}
func ToSQL2(query *gorm.DB) string {
func ToSQL(query *gorm.DB) string {
// Use db.ToSQL to generate the SQL string for the existing query
sql := query.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Session(&gorm.Session{DryRun: true}).Find([]interface{}{})
Expand All @@ -90,15 +90,6 @@ func ToSQL2(query *gorm.DB) string {
return sql
}

func ToSQL(query *gorm.DB) string {
// Clone the query object to avoid altering the original
//tempQuery := query.Session(&gorm.Session{DryRun: true})
sql := query.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Find([]interface{}{})
})
return sql
}

func TableExists(tx *gorm.DB, tableName string) bool {
query := fmt.Sprintf("SELECT 1 FROM %s WHERE 1 = 2", tableName)
err := tx.Exec(query).Error
Expand Down

0 comments on commit 96d3cd8

Please sign in to comment.