text2sql-eval-toolkit 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- text2sql_eval_toolkit/__init__.py +107 -0
- text2sql_eval_toolkit/analysis/__init__.py +5 -0
- text2sql_eval_toolkit/analysis/error_analysis.py +335 -0
- text2sql_eval_toolkit/analysis/report_tools.py +719 -0
- text2sql_eval_toolkit/config_args.py +65 -0
- text2sql_eval_toolkit/data/__init__.py +4 -0
- text2sql_eval_toolkit/data/benchmarks.json +69 -0
- text2sql_eval_toolkit/data/test-benchmarks.json +69 -0
- text2sql_eval_toolkit/env_loader.py +55 -0
- text2sql_eval_toolkit/evaluation/__init__.py +26 -0
- text2sql_eval_toolkit/evaluation/evaluation_tools.py +759 -0
- text2sql_eval_toolkit/evaluation/llm_as_judge.py +90 -0
- text2sql_eval_toolkit/execution/__init__.py +5 -0
- text2sql_eval_toolkit/execution/execution_tools.py +1448 -0
- text2sql_eval_toolkit/execution/replace_select_tool.py +114 -0
- text2sql_eval_toolkit/inference/__init__.py +5 -0
- text2sql_eval_toolkit/inference/agentic_pipeline.py +2335 -0
- text2sql_eval_toolkit/inference/base_pipeline.py +11 -0
- text2sql_eval_toolkit/inference/baseline_llm_pipeline.py +372 -0
- text2sql_eval_toolkit/inference/inference_tools.py +769 -0
- text2sql_eval_toolkit/logging.py +54 -0
- text2sql_eval_toolkit/profiling/profiling_tools.py +185 -0
- text2sql_eval_toolkit/utils.py +302 -0
- text2sql_eval_toolkit-1.0.0.dist-info/METADATA +382 -0
- text2sql_eval_toolkit-1.0.0.dist-info/RECORD +28 -0
- text2sql_eval_toolkit-1.0.0.dist-info/WHEEL +5 -0
- text2sql_eval_toolkit-1.0.0.dist-info/licenses/LICENSE +201 -0
- text2sql_eval_toolkit-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright IBM Corp. 2025 - 2026
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
#
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Dict
|
|
11
|
+
from unitxt.text2sql_utils import replace_select_clause
|
|
12
|
+
from text2sql_eval_toolkit.utils import get_gt_sqls
|
|
13
|
+
from text2sql_eval_toolkit.logging import get_logger
|
|
14
|
+
|
|
15
|
+
logger = get_logger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def clean_sql(s: str | None) -> str | None:
|
|
19
|
+
"""Strip code fences and trailing semicolons to normalize for comparison."""
|
|
20
|
+
if s is None:
|
|
21
|
+
return None
|
|
22
|
+
t = s.strip()
|
|
23
|
+
# remove ```sql ... ``` or ``` ... ``` fences if present
|
|
24
|
+
if t.lower().startswith("```sql"):
|
|
25
|
+
t = t[6:].lstrip("`").strip() # drop the leading ```sql
|
|
26
|
+
if t.startswith("```") and t.endswith("```"):
|
|
27
|
+
t = t[3:-3].strip()
|
|
28
|
+
# strip trailing semicolons and whitespace
|
|
29
|
+
t = t.rstrip(";\n\r\t ")
|
|
30
|
+
return t
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def get_gt_sql(record: Dict[str, Any]) -> str | None:
|
|
34
|
+
"""Get the ground truth SQL from the record (first gt if multiple)"""
|
|
35
|
+
gt_sqls = get_gt_sqls(record)
|
|
36
|
+
return gt_sqls[0]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def replace_select_for_logic_ex(
|
|
40
|
+
predictions_path: str | Path, db_engine: Dict[str, Any]
|
|
41
|
+
) -> None:
|
|
42
|
+
"""
|
|
43
|
+
Iterate over predictions in a JSON file, and for each prediction record's
|
|
44
|
+
'predicted_sql', call the unitxt `replace_select_clause(gt_sql, predicted_sql, dialect)`
|
|
45
|
+
function. If the returned SQL differs, set 'predicted_sql_revised' on that prediction.
|
|
46
|
+
|
|
47
|
+
The file is updated in-place; a '.bak' backup is created alongside it.
|
|
48
|
+
|
|
49
|
+
Expected db_engine example:
|
|
50
|
+
{"db_type": "postgres" | "sqlite" | "db2" | "mysql" | "presto", ...}
|
|
51
|
+
"""
|
|
52
|
+
predictions_path = Path(predictions_path)
|
|
53
|
+
if not predictions_path.exists():
|
|
54
|
+
raise FileNotFoundError(f"No such file: {predictions_path}")
|
|
55
|
+
|
|
56
|
+
with predictions_path.open("r", encoding="utf-8") as f:
|
|
57
|
+
data = json.load(f)
|
|
58
|
+
|
|
59
|
+
dialect = db_engine.get("db_type")
|
|
60
|
+
if dialect not in {"postgres", "sqlite", "db2", "mysql", "presto"}:
|
|
61
|
+
raise NotImplementedError(f"Unsupported DB type '{dialect}'.")
|
|
62
|
+
if dialect == "db2":
|
|
63
|
+
dialect = "postgres"
|
|
64
|
+
|
|
65
|
+
modified_count = 0
|
|
66
|
+
total_predictions = 0
|
|
67
|
+
|
|
68
|
+
for record in data:
|
|
69
|
+
gt_sql_raw = get_gt_sql(record)
|
|
70
|
+
gt_sql = clean_sql(gt_sql_raw)
|
|
71
|
+
preds = record.get("predictions")
|
|
72
|
+
|
|
73
|
+
for _, pred in preds.items():
|
|
74
|
+
original_pred_sql_raw = pred.get("predicted_sql")
|
|
75
|
+
original_pred_sql = clean_sql(original_pred_sql_raw)
|
|
76
|
+
if not original_pred_sql:
|
|
77
|
+
continue
|
|
78
|
+
|
|
79
|
+
total_predictions += 1
|
|
80
|
+
|
|
81
|
+
try:
|
|
82
|
+
revised_sql_raw = replace_select_clause(
|
|
83
|
+
gt_sql, original_pred_sql, dialect
|
|
84
|
+
)
|
|
85
|
+
except Exception as e:
|
|
86
|
+
# pred.setdefault("errors", {})
|
|
87
|
+
# pred["errors"]["replace_select_clause"] = f"{type(e).__name__}: {e}"
|
|
88
|
+
logger.error(f"Error replacing select clause: {repr(e)}")
|
|
89
|
+
continue
|
|
90
|
+
|
|
91
|
+
revised_sql = clean_sql(revised_sql_raw)
|
|
92
|
+
if revised_sql and revised_sql != original_pred_sql:
|
|
93
|
+
# Only set if actually changed
|
|
94
|
+
pred["logic_sql"] = revised_sql
|
|
95
|
+
modified_count += 1
|
|
96
|
+
|
|
97
|
+
# Write back with a backup
|
|
98
|
+
# backup_path = predictions_path.with_suffix(predictions_path.suffix + ".bak")
|
|
99
|
+
# if not backup_path.exists():
|
|
100
|
+
# predictions_path.replace(backup_path)
|
|
101
|
+
# else:
|
|
102
|
+
# with (
|
|
103
|
+
# predictions_path.open("r", encoding="utf-8") as src,
|
|
104
|
+
# backup_path.open("w", encoding="utf-8") as dst,
|
|
105
|
+
# ):
|
|
106
|
+
# dst.write(src.read())
|
|
107
|
+
|
|
108
|
+
with predictions_path.open("w", encoding="utf-8") as f:
|
|
109
|
+
json.dump(data, f, ensure_ascii=False, indent=2)
|
|
110
|
+
|
|
111
|
+
logger.info(
|
|
112
|
+
f"[replace_select_clause] processed {total_predictions} predictions; "
|
|
113
|
+
f"updated {modified_count} with 'predicted_sql_revised'."
|
|
114
|
+
)
|