Skip to content

Commit 116ee36

Browse files
committed
various fixes
1 parent 06b3ca7 commit 116ee36

10 files changed

Lines changed: 33 additions & 24 deletions

adapters/clickhouse_adapter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (a *ClickhouseDbqDataSourceAdapter) InterpretDataQualityCheck(check *dbqcor
233233
func (a *ClickhouseDbqDataSourceAdapter) ExecuteQuery(ctx context.Context, query string) (interface{}, error) {
234234
rows, err := a.cnn.Query(ctx, query)
235235
if err != nil {
236-
return "", fmt.Errorf("failed to execute query for check: %v", err)
236+
return "", fmt.Errorf("failed to execute query for check: %w", err)
237237
}
238238
defer rows.Close()
239239

@@ -255,7 +255,7 @@ func (a *ClickhouseDbqDataSourceAdapter) ExecuteQuery(ctx context.Context, query
255255
}
256256

257257
if err = rows.Err(); err != nil {
258-
return "", fmt.Errorf("failed to scan result for check: %v", err)
258+
return "", fmt.Errorf("failed to scan result for check: %w", err)
259259
}
260260

261261
return queryResult, nil

adapters/mysql_adapter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,19 @@ func (a *MysqlDbqDataSourceAdapter) InterpretDataQualityCheck(check *dbqcore.Dat
232232
func (a *MysqlDbqDataSourceAdapter) ExecuteQuery(ctx context.Context, query string) (interface{}, error) {
233233
rows, err := a.db.QueryContext(ctx, query)
234234
if err != nil {
235-
return "", fmt.Errorf("failed to execute query for check: %v", err)
235+
return "", fmt.Errorf("failed to execute query for check: %w", err)
236236
}
237237
defer rows.Close()
238238

239239
var queryResult interface{}
240240
for rows.Next() {
241241
if err := rows.Scan(&queryResult); err != nil {
242-
return "", fmt.Errorf("failed to scan result for check: %v", err)
242+
return "", fmt.Errorf("failed to scan result for check: %w", err)
243243
}
244244
}
245245

246246
if err = rows.Err(); err != nil {
247-
return "", fmt.Errorf("failed to scan result for check: %v", err)
247+
return "", fmt.Errorf("failed to scan result for check: %w", err)
248248
}
249249

250250
return queryResult, nil

adapters/postgresql_adapter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,19 @@ func (a *PostgresqlDbqDataSourceAdapter) InterpretDataQualityCheck(check *dbqcor
232232
func (a *PostgresqlDbqDataSourceAdapter) ExecuteQuery(ctx context.Context, query string) (interface{}, error) {
233233
rows, err := a.db.QueryContext(ctx, query)
234234
if err != nil {
235-
return "", fmt.Errorf("failed to execute query for check: %v", err)
235+
return "", fmt.Errorf("failed to execute query for check: %w", err)
236236
}
237237
defer rows.Close()
238238

239239
var queryResult interface{}
240240
for rows.Next() {
241241
if err := rows.Scan(&queryResult); err != nil {
242-
return "", fmt.Errorf("failed to scan result for check: %v", err)
242+
return "", fmt.Errorf("failed to scan result for check: %w", err)
243243
}
244244
}
245245

246246
if err = rows.Err(); err != nil {
247-
return "", fmt.Errorf("failed to scan result for check: %v", err)
247+
return "", fmt.Errorf("failed to scan result for check: %w", err)
248248
}
249249

250250
return queryResult, nil

check_parser.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ type CheckExpression struct {
4343
}
4444

4545
var (
46+
betweenRegex = regexp.MustCompile(`^(\w+)(?:\((.*?)\))?\s+between\s+(.+)\s+and\s+(.+)$`)
47+
operatorRegex = regexp.MustCompile(`^(\w+)(?:\((.*?)\))?\s*([<>=!]+)\s*(.+)$`)
48+
functionOnlyRegex = regexp.MustCompile(`^(\w+)(?:\((.*?)\))?$`)
49+
4650
tableScopeFunctions = map[string]bool{
4751
"row_count": true,
4852
"raw_query": true,
@@ -76,10 +80,6 @@ func ParseCheckExpression(expression string) (*CheckExpression, error) {
7680
FunctionParameters: []string{},
7781
}
7882

79-
betweenRegex := regexp.MustCompile(`^(\w+)(?:\((.*?)\))?\s+between\s+(.+)\s+and\s+(.+)$`)
80-
operatorRegex := regexp.MustCompile(`^(\w+)(?:\((.*?)\))?\s*([<>=!]+)\s*(.+)$`)
81-
functionOnlyRegex := regexp.MustCompile(`^(\w+)(?:\((.*?)\))?$`)
82-
8383
if matches := betweenRegex.FindStringSubmatch(expression); matches != nil {
8484
check.FunctionName = matches[1]
8585
check.Operator = "between"

checks_cfg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ func (c *DataQualityCheck) UnmarshalYAML(node *yaml.Node) error {
156156

157157
func LoadChecksFileConfig(fileName string) (*ChecksFileConfig, error) {
158158
file, err := os.Open(fileName)
159-
defer file.Close()
160159
if err != nil {
161160
return nil, err
162161
}
162+
defer file.Close()
163163

164164
var cfg ChecksFileConfig
165165
decoder := yaml.NewDecoder(file)

cnn/clickhouse_cnn.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package cnn
1616

1717
import (
1818
"crypto/tls"
19+
"fmt"
1920

2021
"github.com/ClickHouse/clickhouse-go/v2"
2122
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
@@ -26,7 +27,7 @@ func NewClickhouseConnection(connectionCfg dbqcore.ConnectionConfig, poolSize in
2627
proto, tlsCfg := clickhouseProtocol(connectionCfg.Protocol)
2728

2829
cnn, err := clickhouse.Open(&clickhouse.Options{
29-
Addr: []string{connectionCfg.Host},
30+
Addr: []string{fmt.Sprintf("%s:%d", connectionCfg.Host, connectionCfg.Port)},
3031
Auth: clickhouse.Auth{
3132
Database: connectionCfg.Database,
3233
Username: connectionCfg.Username,

connectors/clickhouse_connector.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ func NewClickhouseDbqConnector(cnn driver.Conn, logger *slog.Logger) dbqcore.Dbq
3434
}
3535

3636
func (c *ClickhouseDbqConnector) Ping(ctx context.Context) (string, error) {
37+
if err := c.cnn.Ping(ctx); err != nil {
38+
return "", fmt.Errorf("clickhouse ping failed: %w", err)
39+
}
40+
3741
serverVersion, err := c.cnn.ServerVersion()
3842
if err != nil {
39-
return "", err
43+
return "", fmt.Errorf("failed to get server version: %w", err)
4044
}
4145

4246
return serverVersion.String(), nil

dbq_validator.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ import (
1818
"context"
1919
"fmt"
2020
"log/slog"
21+
"math"
2122
"strconv"
2223
"time"
2324
)
2425

26+
const floatEqualityEpsilon = 1e-9
27+
2528
// DataQualityCheckType represents the type of data quality check.
2629
type DataQualityCheckType string
2730

@@ -138,8 +141,9 @@ func (d DbqDataValidatorImpl) RunCheck(ctx context.Context, adapter DbqDataSourc
138141
result.Pass = true
139142
}
140143
} else {
141-
// Unknown schema check type, default to pass
142-
result.Pass = true
144+
// Unknown schema check type, fail-safe
145+
result.Pass = false
146+
result.Error = fmt.Sprintf("unknown schema check type for expression: %s", check.Expression)
143147
}
144148
} else {
145149
// Regular checks use the existing validation logic
@@ -186,9 +190,9 @@ func (d DbqDataValidatorImpl) validateResult(queryResult interface{}, parsedChec
186190
case "!=", "<>":
187191
return d.validateNotEqual(actualValue, parsedCheck.ThresholdValue)
188192
default:
189-
d.logger.Warn("Unknown operator, defaulting to true",
193+
d.logger.Error("Unknown operator, failing check",
190194
"operator", parsedCheck.Operator)
191-
return true
195+
return false
192196
}
193197
}
194198

@@ -297,7 +301,7 @@ func (d DbqDataValidatorImpl) validateLessThanOrEqual(actualValue float64, thres
297301
return actualValue <= threshold
298302
}
299303

300-
// validateEqual checks if actual == threshold
304+
// validateEqual checks if actual == threshold (with epsilon tolerance for float comparison)
301305
func (d DbqDataValidatorImpl) validateEqual(actualValue float64, thresholdValue interface{}) bool {
302306
threshold, err := d.convertToFloat64(thresholdValue)
303307
if err != nil {
@@ -306,10 +310,10 @@ func (d DbqDataValidatorImpl) validateEqual(actualValue float64, thresholdValue
306310
"error", err)
307311
return false
308312
}
309-
return actualValue == threshold
313+
return math.Abs(actualValue-threshold) < floatEqualityEpsilon
310314
}
311315

312-
// validateNotEqual checks if actual != threshold
316+
// validateNotEqual checks if actual != threshold (with epsilon tolerance for float comparison)
313317
func (d DbqDataValidatorImpl) validateNotEqual(actualValue float64, thresholdValue interface{}) bool {
314318
threshold, err := d.convertToFloat64(thresholdValue)
315319
if err != nil {
@@ -318,7 +322,7 @@ func (d DbqDataValidatorImpl) validateNotEqual(actualValue float64, thresholdVal
318322
"error", err)
319323
return false
320324
}
321-
return actualValue != threshold
325+
return math.Abs(actualValue-threshold) >= floatEqualityEpsilon
322326
}
323327

324328
// convertToFloat64 converts various types to float64

dbq_validator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func TestDbqDataValidatorImpl_validateResult(t *testing.T) {
273273
Operator: "~=",
274274
ThresholdValue: 100,
275275
},
276-
expectedPass: true,
276+
expectedPass: false,
277277
},
278278
}
279279

File renamed without changes.

0 commit comments

Comments
 (0)