处理数据库查询结果和比较的类 只对外暴露compare_results和calculate_tables_hash接口
| 1 | from .interaction import Database |
| 2 | |
| 3 | class DBResultProcessor: |
| 4 | """ |
| 5 | 处理数据库查询结果和比较的类 |
| 6 | 只对外暴露compare_results和calculate_tables_hash接口 |
| 7 | """ |
| 8 | |
| 9 | @staticmethod |
| 10 | def compare_results(answer, ground_truth, query_type): |
| 11 | """ |
| 12 | 比较答案和标准答案 |
| 13 | |
| 14 | 参数: |
| 15 | answer - 模型输出的答案 |
| 16 | ground_truth - 标准答案 |
| 17 | query_type - 查询类型 (SELECT/INSERT/UPDATE/DELETE) |
| 18 | |
| 19 | 返回: |
| 20 | bool - 答案是否匹配 |
| 21 | """ |
| 22 | try: |
| 23 | # 处理answer和ground_truth |
| 24 | processed_answer = DBResultProcessor._clean_answer(answer) |
| 25 | processed_ground_truth = DBResultProcessor._clean_answer(ground_truth) |
| 26 | |
| 27 | if query_type in ("INSERT", "DELETE", "UPDATE"): |
| 28 | return processed_answer == processed_ground_truth |
| 29 | |
| 30 | # 打印处理后的结果用于调试 |
| 31 | print("Processed answer:", processed_answer) |
| 32 | print("Processed ground_truth:", processed_ground_truth) |
| 33 | |
| 34 | # 比较逻辑 |
| 35 | if len(processed_answer) == 1 and len(processed_ground_truth) == 1: |
| 36 | # 获取处理后的值 |
| 37 | ans_val = processed_answer[0] |
| 38 | gt_val = processed_ground_truth[0] |
| 39 | |
| 40 | # 如果两个值都是特殊值(0、undefined等),认为它们相等 |
| 41 | if ans_val == "0" and gt_val == "0": |
| 42 | return True |
| 43 | |
| 44 | # 浮点数比较 |
| 45 | if DBResultProcessor._is_float(ans_val) and DBResultProcessor._is_float(gt_val): |
| 46 | return DBResultProcessor._float_equal(ans_val, gt_val) |
| 47 | |
| 48 | # 字符串比较 |
| 49 | return ans_val == gt_val |
| 50 | else: |
| 51 | # 如果都是浮点数,执行浮点比较 |
| 52 | if (all(DBResultProcessor._is_float(x) for x in processed_answer) and |
| 53 | all(DBResultProcessor._is_float(x) for x in processed_ground_truth)): |
| 54 | # 检查每个答案是否都有匹配的标准答案(考虑精度) |
| 55 | if len(processed_answer) != len(processed_ground_truth): |
| 56 | return False |
| 57 | |
| 58 | # 创建匹配标记 |
| 59 | matched_gt = [False] * len(processed_ground_truth) |
| 60 |
nothing calls this directly
no outgoing calls
no test coverage detected