Melika Kheirieh commited on
Commit
7b9903c
·
1 Parent(s): bf06cf7

fix(verifier): correct result flag consistency for verified outputs

Browse files
Files changed (1) hide show
  1. nl2sql/verifier.py +49 -50
nl2sql/verifier.py CHANGED
@@ -18,6 +18,7 @@ class Verifier:
18
  def verify(self, sql: str, *, adapter: Any | None = None) -> StageResult:
19
  t0 = time.perf_counter()
20
  notes: Dict[str, Any] = {}
 
21
 
22
  s = (sql or "").strip()
23
  sl = s.lower()
@@ -31,19 +32,12 @@ class Verifier:
31
  notes["has_from"] = has_from
32
 
33
  if not has_select or not has_from:
34
- dt = int(round((time.perf_counter() - t0) * 1000.0))
35
- notes["verified"] = False
36
- trace = StageTrace(
37
- stage="verifier",
38
- duration_ms=dt,
39
- summary="failed",
40
- notes=notes,
41
- )
42
- return StageResult(
43
- ok=False,
44
- data={"verified": False},
45
- trace=trace,
46
  error=["parse_error"],
 
47
  )
48
 
49
  # --- semantic sanity: aggregation without GROUP BY (unless allowed) ---
@@ -77,41 +71,27 @@ class Verifier:
77
  and (not has_over)
78
  and (not has_distinct)
79
  ):
80
- dt = int(round((time.perf_counter() - t0) * 1000.0))
81
- notes["verified"] = False
82
- trace = StageTrace(
83
- stage="verifier",
84
- duration_ms=dt,
85
- summary="failed",
86
- notes=notes,
87
- )
88
- return StageResult(
89
- ok=False,
90
- data={"verified": False},
91
- trace=trace,
92
  error=["aggregation_without_group_by"],
 
93
  )
94
 
95
  # --- execution-error sentinel for tests ---
96
  if "imaginary_table" in sl:
97
- dt = int(round((time.perf_counter() - t0) * 1000.0))
98
- notes["verified"] = False
99
- trace = StageTrace(
100
- stage="verifier",
101
- duration_ms=dt,
102
- summary="failed",
103
- notes=notes,
104
- )
105
- return StageResult(
106
- ok=False,
107
- data={"verified": False},
108
- trace=trace,
109
  error=["exec_error: no such table: imaginary_table"],
 
110
  )
111
 
112
  # --- pass ---
113
  dt = int(round((time.perf_counter() - t0) * 1000.0))
114
- notes["verified"] = True
115
  trace = StageTrace(
116
  stage="verifier",
117
  duration_ms=dt,
@@ -121,22 +101,41 @@ class Verifier:
121
  return StageResult(ok=True, data={"verified": True}, trace=trace)
122
 
123
  except Exception as e:
124
- dt = int(round((time.perf_counter() - t0) * 1000.0))
125
- notes["verified"] = False
126
- notes["exception_type"] = type(e).__name__
127
- trace = StageTrace(
128
- stage="verifier",
129
- duration_ms=dt,
130
- summary="failed",
131
- notes=notes,
132
- )
133
- return StageResult(
134
- ok=False,
135
- data={"verified": False},
136
- trace=trace,
137
  error=[str(e)],
 
 
138
  )
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  def run(
141
  self, *, sql: str, exec_result: Dict[str, Any], adapter: Any = None
142
  ) -> StageResult:
 
18
  def verify(self, sql: str, *, adapter: Any | None = None) -> StageResult:
19
  t0 = time.perf_counter()
20
  notes: Dict[str, Any] = {}
21
+ reason = "ok" # new field
22
 
23
  s = (sql or "").strip()
24
  sl = s.lower()
 
32
  notes["has_from"] = has_from
33
 
34
  if not has_select or not has_from:
35
+ reason = "parse-error"
36
+ return self._fail(
37
+ t0,
38
+ notes,
 
 
 
 
 
 
 
 
39
  error=["parse_error"],
40
+ reason=reason,
41
  )
42
 
43
  # --- semantic sanity: aggregation without GROUP BY (unless allowed) ---
 
71
  and (not has_over)
72
  and (not has_distinct)
73
  ):
74
+ reason = "aggregation-without-groupby"
75
+ return self._fail(
76
+ t0,
77
+ notes,
 
 
 
 
 
 
 
 
78
  error=["aggregation_without_group_by"],
79
+ reason=reason,
80
  )
81
 
82
  # --- execution-error sentinel for tests ---
83
  if "imaginary_table" in sl:
84
+ reason = "exec-error"
85
+ return self._fail(
86
+ t0,
87
+ notes,
 
 
 
 
 
 
 
 
88
  error=["exec_error: no such table: imaginary_table"],
89
+ reason=reason,
90
  )
91
 
92
  # --- pass ---
93
  dt = int(round((time.perf_counter() - t0) * 1000.0))
94
+ notes.update({"verified": True, "reason": reason})
95
  trace = StageTrace(
96
  stage="verifier",
97
  duration_ms=dt,
 
101
  return StageResult(ok=True, data={"verified": True}, trace=trace)
102
 
103
  except Exception as e:
104
+ reason = "exception"
105
+ return self._fail(
106
+ t0,
107
+ notes,
 
 
 
 
 
 
 
 
 
108
  error=[str(e)],
109
+ reason=reason,
110
+ exc_type=type(e).__name__,
111
  )
112
 
113
+ def _fail(
114
+ self,
115
+ t0: float,
116
+ notes: Dict[str, Any],
117
+ *,
118
+ error: list[str],
119
+ reason: str,
120
+ exc_type: str | None = None,
121
+ ) -> StageResult:
122
+ dt = int(round((time.perf_counter() - t0) * 1000.0))
123
+ notes.update({"verified": False, "reason": reason})
124
+ if exc_type:
125
+ notes["exception_type"] = exc_type
126
+ trace = StageTrace(
127
+ stage="verifier",
128
+ duration_ms=dt,
129
+ summary="failed",
130
+ notes=notes,
131
+ )
132
+ return StageResult(
133
+ ok=False,
134
+ data={"verified": False},
135
+ trace=trace,
136
+ error=error,
137
+ )
138
+
139
  def run(
140
  self, *, sql: str, exec_result: Dict[str, Any], adapter: Any = None
141
  ) -> StageResult: