Please wait a minute...
浙江大学学报(工学版)  2023, Vol. 57 Issue (2): 277-286    DOI: 10.3785/j.issn.1008-973X.2023.02.008
计算机技术     
带复杂计算的金融领域自然语言查询的SQL生成
何佳壕(),刘喜平*(),舒晴,万常选,刘德喜,廖国琼
江西财经大学 信息管理学院,江西 南昌 330013
SQL generation from natural language queries with complex calculations on financial data
Jia-hao HE(),Xi-ping LIU*(),Qing SHU,Chang-xuan WAN,De-xi LIU,Guo-qiong LIAO
School of Information Management, Jiangxi University of Finance and Economics, Nanchang 330013, China
 全文: PDF(739 KB)   HTML
摘要:

研究金融领域基于自然语言查询的结构化查询语言(SQL)生成问题(Text-to-SQL), 构建一个金融领域Text-to-SQL数据集,称为SOFT数据集. 该数据集覆盖了金融领域的常见查询,具有鲜明的特点,并对Text-to-SQL提出了挑战. 提出金融领域Text-to-SQL模型FinSQL,该模型优化了对金融领域复杂查询的支持. 通过分析一类复杂计算查询(行计算查询)的特点,提出一种基于分治的方法,即先将一个行计算查询分解为若干个子查询,分别针对每个子查询生成SQL语句,再将子查询的SQL语句组合在一起得到原始查询的SQL语句. 在SOFT数据集上进行验证,结果显示,本研究所提的方法在复杂查询上效果优于已有方法. 特别地,所提出的模型FinSQL能够较好地支持行计算查询.

关键词: Text-to-SQL自然语言查询金融领域行计算查询分治方法    
Abstract:

The problem of structured query language (SQL) generation from natural language queries (Text-to-SQL) in financial domain was investigated. First, SOFT, a Text-to-SQL dataset in the financial domain was constructed. The dataset covered common queries in the financial domain with distinctive features and presented challenges to Text-to-SQL research. Then, FinSQL, a Text-to-SQL model, which optimized the support for complex queries in the financial domain, was proposed. In particular, by analyzing the characteristics of row calculation queries, a class of queries with complex numerical calculations, a divide-and-conquer based method was proposed. A row calculation query was divided into several subqueries, the SQL statement for each subquery was generated, and the SQL statements were finally combined into together to get the SQL statement for the original query. Experimental results on SOFT dataset show that the proposed FinSQL model outperforms existing methods for the hard queries, and performs well for row calculation queries.

Key words: Text-to-SQL    natural language query    financial field    row calculation query    divide-and-conquer method
收稿日期: 2022-07-31 出版日期: 2022-12-02
CLC:  TP 391  
基金资助: 国家自然科学基金资助项目(62076112, 61972184); 江西省自然科学基金资助项目(20192BAB207017); 江西省教育厅科学技术研究资助项目(GJJ190255); 江西省研究生创新专项资金项目(YC2021-B130)
通讯作者: 刘喜平     E-mail: hejiahao810@126.com;liuxiping@jxufe.edu.cn
作者简介: 何佳壕(1994—),男,硕士生,从事数据挖掘研究. orcid.org/0000-0003-2264-6665. E-mail: hejiahao810@126.com
服务  
把本文推荐给朋友
加入引用管理器
E-mail Alert
作者相关文章  
何佳壕
刘喜平
舒晴
万常选
刘德喜
廖国琼

引用本文:

何佳壕,刘喜平,舒晴,万常选,刘德喜,廖国琼. 带复杂计算的金融领域自然语言查询的SQL生成[J]. 浙江大学学报(工学版), 2023, 57(2): 277-286.

Jia-hao HE,Xi-ping LIU,Qing SHU,Chang-xuan WAN,De-xi LIU,Guo-qiong LIAO. SQL generation from natural language queries with complex calculations on financial data. Journal of ZheJiang University (Engineering Science), 2023, 57(2): 277-286.

链接本文:

https://www.zjujournals.com/eng/CN/10.3785/j.issn.1008-973X.2023.02.008        https://www.zjujournals.com/eng/CN/Y2023/V57/I2/277

查询问题 SQL语句
查询2019年资本公积不少于100 亿的公司的员工人数,列出公司名称和员工人数. SELECT T1.公司名称 , T1.员工人数 FROM 公司基本信息 AS T1 JOIN 财务指标 AS T2 ON T1.
股票代码 = T2.股票代码 WHERE T2.资本公积 >= 10000000000 AND T2.年份 = "2019"
查询2018年每股净资产
不低于10的公司名称.
SELECT 公司名称 FROM 公司基本信息 WHERE 股票代码 IN (SELECT 股票代码 FROM 财务比率 WHERE 每股净资产 >= 10 AND 年份 = "2018")
国药一致2019年相比于2018年营业利润的同比增长率怎么样? SELECT (a.营业利润 ? b.营业利润) / b.营业利润 FROM (SELECT 营业利润 FROM 财务指标 WHERE 股票名称 = "国药一致" AND 年份 = "2019") a , (SELECT 营业利润 FROM 财务指标
WHERE 股票名称 = "国药一致" AND 年份 = "2018") b
表 1  SOFT数据集示例
数据集 p/%
排序 分组 嵌套 行计算 列计算
WikiSQL 0 0 0 0 0
TableQA 0 0 0 0 0
Spider/CSpider 13.1 14.6 8.3 0 0.3
SOFT 14.3 12.2 7.7 12.1 2.5
表 2  各数据集查询类型占比
模型 Pre
easy medium hard extra hard all
SyntaxSQLNet 0.514 0.383 0.296 0.235 0.340
SLSQL 0.825 0.677 0.536 0.495 0.613
RYANSQL 0.874 0.725 0.662 0.613 0.695
表 3  SOFT数据集模型的精确匹配率
模型 查询问题“国药一致2019年相比于2018年营业利润的同比增长率是多少?”对应的预测结果
SyntaxSQLNet SELECT 营业利润 FROM 财务比率 WHERE 股票名称 = "terminal" AND 年份 = "terminal"
SLSQL SELECT 营业利润 FROM 财务指标 WHERE 股票名称 = "国药一致" AND 年份 = "2019"
RYANSQL SELECT 营业利润 FROM 财务指标 WHERE 股票名称 = "国药一致" AND 年份 = "2019"
标准查询 SELECT (a.营业利润 ? b.营业利润) / b.营业利润 FROM (SELECT 营业利润 FROM 财务指标 WHERE 股票名称 = "国药一致"
AND 年份 = "2019") a , (SELECT 营业利润 FROM 财务指标 WHERE 股票名称 = "国药一致" AND 年份 = "2018") b
表 4  部分数据集模型预测结果对比
图 1  FinSQL总体结构图
组件 槽位
FROM ($TBL)+
SELECT $DIST( $AGG ( $DIST1 $AGG1 $COL1 $ARI
$DIST2 $AGG2 $COL2 ) )+
ORDER BY (($DIST1 $AGG1 $COL1 $ARI $DIST2
$AGG2 $COL2) $ORD )*
GROUP BY ($COL)*
LIMIT $NUM
WHERE、
HAVING
($CONJ ( $DIST1 $AGG1 $COL1 $ARI
$DIST2 $AGG2 $COL2 )
$NOT $COND $VAL1|$SEL1 $VAL2|$SEL2 )*
INTERSECT、
UNION、
EXPECT
$SEL
表 5  各个组件可能的槽位值
行计算问题类型 规则
变化问题 select a.col1 ? b.col2 from sql1 a, sql2 b
变化率问题 select (a.col1 ? b.col2) / b.col2 from sql1 a, sql2 b
比率问题 select a.col1 / b.col2 from sql1 a, sql2 b
求和问题 select a.col1+b.col2 from sql1 a, sql2 b
乘积问题 select a.col1 * b.col2 from sql1 a, sql2 b
表 6  问题类型与组合规则
模型 P Re F1
FastText 0.916 0.893 0.904
TextRNN 0.923 0.901 0.912
BERT 0.935 0.916 0.925
表 7  问题识别实验结果
模型 P Re F1
BiLSTM-CRF+Heu 0.816 0.753 0.783
BiLSTM-CRF+Model 0.875 0.837 0.856
BERT-CRF+Heu 0.887 0.852 0.869
BERT-CRF+Model 0.902 0.873 0.887
表 8  问题分解实验结果对比
模型 Pre
easy medium hard extra hard all
SLSQL 0.825 0.677 0.536 0.495 0.613
RYANSQL 0.874 0.725 0.662 0.613 0.695
FinSQL 0.874 0.804 0.785 0.682 0.781
表 9  Text-to-SQL模型实验结果对比
难度 Pre 难度 Pre
medium 0.806 extra hard 0.571
hard 0.742 all 0.713
表 10  FinSQL模型在SOFT-rc上的实验结果
模型 查询问题“给出平安银行2020年与2019年主营业务收入之和. ”对应的预测结果
SLSQL SELECT 主营业务收入 FROM 财务指标 WHERE 股票名称 = "平安银行" AND 年份 = "2020"
RYANSQL SELECT 主营业务收入 FROM 财务指标 WHERE 股票名称 = "平安银行" AND 年份 = "2020"
FinSQL SELECT a.主营业务收入+b.主营业务收入 FROM (SELECT 主营业务收入 FROM 财务指标 WHERE 股票名称 = "平安银行" AND 年份 = "2020") a, (SELECT 主营业务收入 FROM 财务指标 WHERE 股票名称 = "平安银行" AND 年份 = "2019") b
表 11  部分数据集模型预测结果对比
图 2  Text-to-SQL模型响应时间对比
1 曹金超, 黄滔, 陈刚, 等 自然语言生成多表SQL查询语句技术研究[J]. 计算机科学与探索, 2020, 14 (7): 1133- 1141
CAO Jin-chao, HUANG Tao, CHEN Gang, et al Research on technology of generating multi-table SQL query statement by natural language[J]. Journal of Frontiers of Computer Science and Technology, 2020, 14 (7): 1133- 1141
doi: 10.3778/j.issn.1673-9418.1908025
2 潘璇, 徐思涵, 蔡祥睿, 等 基于深度学习的数据库自然语言接口综述[J]. 计算机研究与发展, 2021, 58 (9): 1925- 1950
PAN Xuan, XU Si-han, CAI Xiang-rui, et al Survey on deep learning based natural language interface to database[J]. Journal of Computer Research and Development, 2021, 58 (9): 1925- 1950
doi: 10.7544/issn1000-1239.2021.20200209
3 ZHONG V, XIANG C, SOCHER R. Seq2SQL: generating structured queries from natural language using reinforcement learning [EB/OL]. (2017-11-20). http://arxiv.org/abs/1709.00103.
4 YU T, ZHANG R, YANG K, et al. Spider: a large-scale human-labeled dataset for complex and cross-domain semantic parsing and Text-To-SQL task [EB/OL]. (2019-02-02). http://arxiv.org/abs/1809.08887.
5 百度NLP. 语义解析(Text-to-SQL)技术研究及应用 [EB/OL]. [2022-07-01]. https://zhuanlan.zhihu.com/p/269478469.
6 BAIK C, JAGADISH H V, LI Y. Bridging the semantic gap with SQL query logs in natural language interfaces to databases [C]// Proceedings of ICDE 2019. Macao: IEEE, 2019: 374–385.
7 LI F, JAGADISH H V Constructing an interactive natural language interface for relational databases[J]. Proceedings of the VLDB Endowment, 2014, 8 (1): 73- 84
doi: 10.14778/2735461.2735468
8 SONG D, SCHILDER F, SMILEY C, et al. TR discover: a natural language interface for querying and analyzing interlinked datasets [C] // Proceedings of ISWC 2015. Bethlehem: Springer, 2015: 21–37.
9 YAGHMAZADEH N, WANG Y, DILLIG I, et al. SQLizer: query synthesis from natural language [C]// Proceedings of the ACM on Programming Languages. New York: ACM. 2017: 1-26.
10 BAHDANAU D, CHO K, BENGIO Y. Neural machine translation by jointly learning to align and translate [EB/OL]. (2016-05-19). http://arxiv.org/abs/1409.0473.
11 XU X, LIU C, SONG D. SQLNet: generating structured queries from natural language without reinforcement learning [EB/OL]. (2017-11-20). https://arxiv.org/abs/1711.04436.
12 YU T, YASUNAGA M, YANG K, et al. SyntaxSQLNet: syntax tree networks for complex and cross-domain Text-to-SQL task [C]// Proceedings of EMNLP 2018. Brussels: ACL, 2018: 1653–1663.
13 LEI W, WANG W, MA Z, et al. Re-examining the role of schema linking in Text-to-SQL [C]// Proceedings of EMNLP 2020. [s.l.]: ACL, 2020: 6943–6954.
14 CHOI D, SHIN M C, KIM E, et al RYANSQL: recursively applying sketch-based slot fillings for complex Text-to-SQL in cross-domain databases[J]. Computational Linguistics, 2021, 47 (2): 309- 332
15 HEMPHILL C T, GODFREY J J, DODDINGTON G R. The ATIS spoken language systems pilot corpus [EB/OL]. (2020-11-20). https://aclanthology.org/H90-1021.pdf.
16 ZELLE J M, MOONEY R J. Learning to parse database queries using inductive logic programming [C]// Proceedings of AAAI 1996. Portland Oregon: AAAI Press, 1996: 1050–1055.
17 MIN Q, SHI Y, ZHANG Y. A pilot study for chinese SQL semantic parsing [EB/OL]. (2019-10-16). http://arxiv.org/abs/1909.13293.
18 SUN N, YANG X, LIU Y. TableQA: a large-scale chinese Text-to-SQL dataset for table-aware SQL generation [EB/OL]. (2020-06-10). https://arxiv.org/abs/2006.06434.
19 LIANG C, YU Y, JIANG H, et al. BOND: BERT-assisted open-domain named entity recognition with distant supervision [C]// Proceedings of ACM SIGKDD 2020. [s.l.]: ACM, 2020: 1054–1064.
20 DEVLIN J, CHANG M W, LEE K, et al. BERT: pre-training of deep bidirectional transformers for language understanding [C]// Proceedings of NAACL 2019. Minneapolis: ACL, 2019: 4171–4186.
21 JOULIN A, GRAVE E, BOJANOWSKI P, et al. Bag of tricks for efficient text classification [C]// Proceedings of EACL 2017. Valencia: ACL, 2017: 427–431.
22 LIU P, QIU X, HUANG X. Recurrent neural network for text classification with multi-task learning [C]// Proceedings of IJCAI 2016. New York: AAAI Press, 2016: 2873–2879.
[1] 周天琪,杨艳,张继杰,殷少伟,郭增强. 基于无负样本损失和自适应增强的图对比学习[J]. 浙江大学学报(工学版), 2023, 57(2): 259-266.
[2] 杨帆,宁博,李怀清,周新,李冠宇. 基于语义增强特征融合的多模态图像检索模型[J]. 浙江大学学报(工学版), 2023, 57(2): 252-258.
[3] 苏丰龙,景宁. 基于关系聚合的时序知识图谱表示学习[J]. 浙江大学学报(工学版), 2023, 57(2): 235-242.
[4] 杨长春,叶赞挺,刘半藤,王柯,崔海东. 基于多源信息融合的医学图像分割方法[J]. 浙江大学学报(工学版), 2023, 57(2): 226-234.
[5] 黄华,赵秋舸,何再兴,李嘉然. 基于LSTM与牛顿迭代的两轴系统轮廓误差控制[J]. 浙江大学学报(工学版), 2023, 57(1): 10-20.
[6] 叶晨,战洪飞,林颖俊,余军合,王瑞,钟武昌. 基于推理-情境感知激活模型的设计知识推荐[J]. 浙江大学学报(工学版), 2023, 57(1): 32-46.
[7] 凤丽洲,杨阳,王友卫,杨贵军. 基于Transformer和知识图谱的新闻推荐新方法[J]. 浙江大学学报(工学版), 2023, 57(1): 133-143.
[8] 马骏驰,迪骁鑫,段宗涛,唐蕾. 程序表示学习综述[J]. 浙江大学学报(工学版), 2023, 57(1): 155-169.
[9] 袁天乐,袁巨龙,朱勇建,郑翰辰. 基于改进YOLOv5的推力球轴承表面缺陷检测算法[J]. 浙江大学学报(工学版), 2022, 56(12): 2349-2357.
[10] 国强,吴天昊,徐伟,KALIUZHNYMykola. 基于通道可靠性和异常抑制的目标跟踪算法[J]. 浙江大学学报(工学版), 2022, 56(12): 2379-2391.
[11] 于楠晶,范晓飚,邓天民,冒国韬. 基于多头自注意力的复杂背景船舶检测算法[J]. 浙江大学学报(工学版), 2022, 56(12): 2392-2402.
[12] 谢誉,包梓群,张娜,吴彪,涂小妹,包晓安. 基于特征优化与深层次融合的目标检测算法[J]. 浙江大学学报(工学版), 2022, 56(12): 2403-2415.
[13] 陆佳炜,郑嘉弘,李端倪,徐俊,肖刚. 面向服务聚类的短文本优化主题模型[J]. 浙江大学学报(工学版), 2022, 56(12): 2416-2425.
[14] 陈巧红,李妃玉,孙麒,贾宇波. 基于LSTM与衰减自注意力的答案选择模型[J]. 浙江大学学报(工学版), 2022, 56(12): 2436-2444.
[15] 李飞,胡坤,张勇,王文善,蒋浩. 基于混合域注意力YOLOv4的输送带纵向撕裂多维度检测[J]. 浙江大学学报(工学版), 2022, 56(11): 2156-2167.