""" Graders for each OpenENV SQL task. All grader functions return a float in [0.0, 1.0]. Grader signatures: grade_easy(output, reference, error) -> float grade_medium(output, reference, error, query_str, step_errors) -> float grade_hard(output, reference, error, query_str) -> float """ from typing import Optional # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _normalize_rows(rows: list) -> list: """Normalize a list-of-tuples to lowercase stripped strings for comparison.""" return [tuple(str(v).strip().lower() if v is not None else "none" for v in row) for row in rows] def _rows_match(a: list, b: list) -> bool: """Return True if two result sets are equal (order-insensitive).""" return sorted(_normalize_rows(a)) == sorted(_normalize_rows(b)) def _partial_overlap(a: list, b: list) -> float: """ Return fraction of reference rows that appear in agent output. Used for partial credit scoring. """ if not b: return 0.9 if not a else 0.1 norm_a = set(map(tuple, _normalize_rows(a))) norm_b = set(map(tuple, _normalize_rows(b))) overlap = len(norm_a & norm_b) return overlap / len(norm_b) # --------------------------------------------------------------------------- # Task 1: fix-syntax (Easy) # --------------------------------------------------------------------------- def grade_easy( output: Optional[list], reference: list, error: Optional[str], ) -> float: if error is not None: return 0.1 if _rows_match(output or [], reference): return 0.9 return 0.1 # --------------------------------------------------------------------------- # Task 2: fix-logic (Medium) # --------------------------------------------------------------------------- def grade_medium( output: Optional[list], reference: list, error: Optional[str], query_str: str, cumulative_errors: int = 0, ) -> float: if error is not None: return 0.1 q = query_str.lower() has_left_join = "left join" in q has_gte_date = ">= '2024-01-01'" in q or ">='2024-01-01'" in q both_fixed = has_left_join and has_gte_date if both_fixed and _rows_match(output or [], reference): return 0.9 return 0.1 # --------------------------------------------------------------------------- # Task 3: fix-and-optimize (Hard) # --------------------------------------------------------------------------- def grade_hard( output: Optional[list], reference: list, error: Optional[str], query_str: str, ) -> float: if error is not None: return 0.1 q = query_str.lower() has_group_by = "group by" in q exact_match = _rows_match(output or [], reference) if exact_match and has_group_by: return 0.9 return 0.1