Skip to content

Commit 5797ebd

Browse files
committed
fix freshness check parsing
1 parent 8ac5d1f commit 5797ebd

5 files changed

Lines changed: 46 additions & 18 deletions

File tree

adapters/clickhouse_adapter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func TestClickhouseAdapter_InterpretDataQualityCheck(t *testing.T) {
8181
{
8282
name: "freshness check",
8383
check: &dbqcore.DataQualityCheck{
84-
Expression: "freshness(last_updated) < 3600",
85-
ParsedCheck: createMockParsedCheck("freshness", []string{"last_updated"}, "<", 3600),
84+
Expression: "freshness(last_updated) < 1h",
85+
ParsedCheck: createMockParsedCheck("freshness", []string{"last_updated"}, "<", "1h"),
8686
},
8787
dataset: "products",
8888
whereClause: "",

adapters/mysql_adapter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func TestMySQLAdapter_InterpretDataQualityCheck(t *testing.T) {
8181
{
8282
name: "freshness check with TIMESTAMPDIFF",
8383
check: &dbqcore.DataQualityCheck{
84-
Expression: "freshness(last_updated) < 3600",
85-
ParsedCheck: createMockParsedCheck("freshness", []string{"last_updated"}, "<", 3600),
84+
Expression: "freshness(last_updated) < 1h",
85+
ParsedCheck: createMockParsedCheck("freshness", []string{"last_updated"}, "<", "1h"),
8686
},
8787
dataset: "products",
8888
whereClause: "",

adapters/postgresql_adapter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func TestPostgreSQLAdapter_InterpretDataQualityCheck(t *testing.T) {
8181
{
8282
name: "freshness check with EXTRACT and EPOCH",
8383
check: &dbqcore.DataQualityCheck{
84-
Expression: "freshness(last_updated) < 3600",
85-
ParsedCheck: createMockParsedCheck("freshness", []string{"last_updated"}, "<", 3600),
84+
Expression: "freshness(last_updated) < 1h",
85+
ParsedCheck: createMockParsedCheck("freshness", []string{"last_updated"}, "<", "1h"),
8686
},
8787
dataset: "products",
8888
whereClause: "",

dbq_validator.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,9 @@ func (d DbqDataValidatorImpl) convertToFloat64(value interface{}) (float64, erro
349349
case uint64:
350350
return float64(v), nil
351351
case string:
352-
return strconv.ParseFloat(v, 64)
352+
return d.tryParseTimeDurationOrFloat(v)
353353
case []byte:
354-
// Handle byte arrays from PostgreSQL/MySQL drivers
355-
return strconv.ParseFloat(string(v), 64)
354+
return d.tryParseTimeDurationOrFloat(string(v))
356355
default:
357356
return 0, fmt.Errorf("unsupported type: %T", value)
358357
}
@@ -388,9 +387,38 @@ func (d DbqDataValidatorImpl) convertToInt(value interface{}) (int, error) {
388387
case string:
389388
return strconv.Atoi(v)
390389
case []byte:
391-
// Handle byte arrays from PostgreSQL/MySQL drivers
392390
return strconv.Atoi(string(v))
393391
default:
394392
return 0, fmt.Errorf("unsupported type: %T", value)
395393
}
396394
}
395+
396+
// tryParseTimeDurationOrFloat parses time duration strings like "3d", "1h", "30m", "45s" into seconds or fallbacks to plain float parsing
397+
func (d DbqDataValidatorImpl) tryParseTimeDurationOrFloat(duration string) (float64, error) {
398+
if len(duration) < 2 {
399+
return strconv.ParseFloat(duration, 64) // Fallback to regular number parsing
400+
}
401+
402+
// number and unit
403+
numStr := duration[:len(duration)-1]
404+
unit := duration[len(duration)-1:]
405+
406+
num, err := strconv.ParseFloat(numStr, 64)
407+
if err != nil {
408+
return strconv.ParseFloat(duration, 64) // Fallback to regular number parsing
409+
}
410+
411+
switch unit {
412+
case "s": // seconds
413+
return num, nil
414+
case "m": // minutes
415+
return num * 60, nil
416+
case "h": // hours
417+
return num * 3600, nil
418+
case "d": // days
419+
return num * 86400, nil // 24 * 60 * 60
420+
default:
421+
// unit is not recognized, fallback to regular number parsing
422+
return strconv.ParseFloat(duration, 64)
423+
}
424+
}

dbq_validator_integration_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,29 @@ func TestDbqDataValidator_RunCheck_Integration(t *testing.T) {
9898
expectedPass: false,
9999
},
100100
{
101-
name: "freshness < 3600 - pass",
101+
name: "freshness < 1h - pass",
102102
check: &DataQualityCheck{
103-
Expression: "freshness(last_updated) < 3600",
103+
Expression: "freshness(last_updated) < 1h",
104104
ParsedCheck: &CheckExpression{
105105
FunctionName: "freshness",
106106
Operator: "<",
107-
ThresholdValue: 3600,
107+
ThresholdValue: "1h",
108108
},
109109
},
110-
queryResult: 1800,
110+
queryResult: 1800, // 30 minutes in seconds
111111
expectedPass: true,
112112
},
113113
{
114-
name: "freshness < 3600 - fail",
114+
name: "freshness < 1h - fail",
115115
check: &DataQualityCheck{
116-
Expression: "freshness(last_updated) < 3600",
116+
Expression: "freshness(last_updated) < 1h",
117117
ParsedCheck: &CheckExpression{
118118
FunctionName: "freshness",
119119
Operator: "<",
120-
ThresholdValue: 3600,
120+
ThresholdValue: "1h",
121121
},
122122
},
123-
queryResult: 7200,
123+
queryResult: 7200, // 2 hours in seconds
124124
expectedPass: false,
125125
},
126126
}

0 commit comments

Comments
 (0)