@@ -230,10 +230,10 @@ func (c *ClickhouseDbqConnector) RunCheck(check *Check, dataset string, defaultW
230230
231231 query , err := generateDataCheckQuery (check , dataset , defaultWhere )
232232 if err != nil {
233- return "" , fmt .Errorf ("failed to generate SQL for check %s (%s): %s" , check .ID , dataset , err .Error ())
233+ return "" , fmt .Errorf ("failed to generate SQL for check (%s)/ (%s): %s" , check .ID , dataset , err .Error ())
234234 }
235235
236- log .Printf ("Executing SQL for (%s) : %s" , check .ID , query )
236+ log .Printf ("Executing SQL for '%s' : %s" , check .ID , query )
237237
238238 startTime := time .Now ()
239239 rows , err := c .cnn .Query (context .Background (), query )
@@ -248,7 +248,8 @@ func (c *ClickhouseDbqConnector) RunCheck(check *Check, dataset string, defaultW
248248 if err := rows .Scan (& checkPassed ); err != nil {
249249 return "" , fmt .Errorf ("failed to scan row: %w" , err )
250250 }
251- log .Printf ("Check passed: %t (%d ms)" , checkPassed , elapsed )
251+ log .Printf ("Check passed: %t (in %d ms)" , checkPassed , elapsed )
252+ log .Printf ("---" )
252253 }
253254
254255 if err = rows .Err (); err != nil {
@@ -288,18 +289,18 @@ func fetchColumns(cnn driver.Conn, ctx context.Context, databaseName string, tab
288289 return cols , nil
289290}
290291
291- func generateDataCheckQuery (check * Check , dataSet string , whereClause string ) (string , error ) {
292+ func generateDataCheckQuery (check * Check , dataset string , whereClause string ) (string , error ) {
292293 var sqlQuery string
293294
294295 // handle raw_query first
295296 if check .ID == CheckTypeRawQuery {
296297 if check .Query == "" {
297298 return "" , fmt .Errorf ("check with id 'raw_query' requires a 'query' field" )
298299 }
299- sqlQuery = strings .ReplaceAll (check .Query , "{{table}}" , dataSet )
300300
301+ sqlQuery = strings .ReplaceAll (check .Query , "{{table}}" , dataset )
301302 if whereClause != "" {
302- // todo: more sophisticated check might be needed
303+ // todo: more sophisticated check is needed
303304 if strings .Contains (strings .ToLower (sqlQuery ), " where " ) {
304305 sqlQuery = fmt .Sprintf ("%s and (%s)" , sqlQuery , whereClause )
305306 } else {
@@ -312,48 +313,47 @@ func generateDataCheckQuery(check *Check, dataSet string, whereClause string) (s
312313
313314 isAggFunction := startWithAnyOf ([]string {
314315 "min" , "max" , "avg" , "stddevPop" , "sum" ,
315- }, check .ID )
316+ }, strings . ToLower ( check .ID ) )
316317
317318 var checkExpression string
319+ parts := strings .Fields (check .ID )
320+ if len (parts ) < 3 {
321+ return "" , fmt .Errorf ("invalid format for check: %s" , check .ID )
322+ }
323+
318324 switch {
319325 case strings .HasPrefix (check .ID , "row_count" ):
320- // format "row_count <operator> <value>"
321- parts := strings .Fields (check .ID )
322- if len (parts ) != 3 {
323- return "" , fmt .Errorf ("invalid format for row_count check: %s" , check .ID )
324- }
325- checkExpression = fmt .Sprintf ("count() %s %s" , parts [1 ], parts [2 ])
326+ checkExpression = strings .Replace (check .ID , "row_count" , "count()" , 1 )
326327
327328 case strings .HasPrefix (check .ID , "null_count" ):
328- // format "null_count(<column_name>) <operator> <value>"
329- re := regexp .MustCompile (`null_count\((.*?)\)\s*(==|!=|>|<|>=|<=)\s*(\d+)` )
329+ re := regexp .MustCompile (`^null_count\((.*?)\)(.*)` )
330330 matches := re .FindStringSubmatch (check .ID )
331- if len (matches ) != 4 {
331+ if len (matches ) < 3 {
332332 return "" , fmt .Errorf ("invalid format for null_count check: %s" , check .ID )
333333 }
334334
335335 column := matches [1 ]
336- operator := matches [2 ]
337- value := matches [3 ]
338- checkExpression = fmt .Sprintf ("countIf(%s IS NULL) %s %s" , column , operator , value )
336+ remainder := matches [2 ]
337+ checkExpression = fmt .Sprintf ("countIf(isNull(%s))%s" , column , remainder )
339338
340339 case isAggFunction :
341- // format: <func>(<column_name>) <operator> <value>
342- re := regexp .MustCompile (`^(min|max|avg|stddevPop|sum)\(([^)]+)\)\s+(==|>=|<=|>|<)\s+(.*)$` )
340+ re := regexp .MustCompile (`^(min|max|avg|stddevPop|sum)\((.*?)\)(.*)` )
343341 matches := re .FindStringSubmatch (check .ID )
344- if len (matches ) < 4 {
342+ if len (matches ) < 3 {
343+ fmt .Println (matches , " --- " , len (matches ))
345344 return "" , fmt .Errorf ("invalid format for aggregation function check: %s" , check .ID )
346345 }
347- checkExpression = fmt .Sprintf ("%s" , matches [0 ])
346+
347+ checkExpression = matches [0 ]
348348
349349 default :
350- // Assume the ID itself is a valid boolean expression if no specific pattern matches
351- // This is less robust but covers simple cases.
352- log .Printf ("Warning: Check ID '%s' did not match known patterns. Assuming it's a direct SQL boolean expression." , check .ID )
350+ // assume the ID itself is a valid boolean expression if no specific pattern matches
351+ // this is less robust but covers simple cases
352+ log .Printf ("Warning: Check ID '%s' did not match known check patterns. Assuming it's a direct SQL boolean expression." , check .ID )
353353 checkExpression = check .ID
354354 }
355355
356- sqlQuery = fmt .Sprintf ("select %s from %s" , checkExpression , dataSet )
356+ sqlQuery = fmt .Sprintf ("select %s from %s" , checkExpression , dataset )
357357 if whereClause != "" {
358358 sqlQuery = fmt .Sprintf ("%s where %s" , sqlQuery , whereClause )
359359 }
0 commit comments