Please wait a minute...
Journal of ZheJiang University (Engineering Science)  2024, Vol. 58 Issue (5): 908-917    DOI: 10.3785/j.issn.1008-973X.2024.05.004
    
SQL generation method based on dependency relational graphattention network
Qing SHU1,2(),Xiping LIU1,*(),Zhao TAN1,Xi LI1,Changxuan WAN1,Dexi LIU1,Guoqiong LIAO1
1. School of Information Technology, Jiangxi University of Finance and Economics, Nanchang 330013, China
2. School of Software, Jiangxi Agricultural University, Nanchang 330013, China
Download: HTML     PDF(871KB) HTML
Export: BibTeX | EndNote (RIS)      

Abstract  

The problem of generating structured query language (SQL) from natural language questions (Text-to-SQL) was analyzed. A two-stage framework was proposed to decouple the processes of schema linking and SQL generation in order to reduce the complexity of SQL generation. Database tables, columns, and values mentioned in the question were identified by a schema linker based on relational graph attention network in the first stage. The syntactic structure of the question and the internal relationships between database schema items were used to guide the model in learning the alignment between the question and the database. The original syntactic dependency tree was modified by merging relationships irrelevant to schema linking and adding dependencies between subordinating conjunctions in parallel structures and other elements in the sentence in view of the characteristics of Text-to-SQL task when constructing the question graph, which helps the model capture long-distance dependencies. SQL generation was performed by injecting the alignment information into the T5 encoder and fine-tuning it in the second stage. Experiments were conducted on the Spider, Spider-DK and Spider-Syn datasets. Results showed that the method performed well, especially for Text-to-SQL problems of medium difficulty and above.



Key wordsText-to-SQL      natural language query      dependency parsing      relational graph attention network     
Received: 03 July 2023      Published: 26 April 2024
CLC:  TP 393  
Fund:  国家自然科学基金资助项目(62076112, 62272205, 62272206, 62272207);江西省自然科学基金资助项目(20232ACB202008);江西省教育厅科学技术研究项目(GJJ190255);江西省研究生创新专项资助项目(YC2023-B185).
Corresponding Authors: Xiping LIU     E-mail: shu_0124@163.com;lewislxp@gmail.com
Cite this article:

Qing SHU,Xiping LIU,Zhao TAN,Xi LI,Changxuan WAN,Dexi LIU,Guoqiong LIAO. SQL generation method based on dependency relational graphattention network. Journal of ZheJiang University (Engineering Science), 2024, 58(5): 908-917.

URL:

https://www.zjujournals.com/eng/10.3785/j.issn.1008-973X.2024.05.004     OR     https://www.zjujournals.com/eng/Y2024/V58/I5/908


基于依存关系图注意力网络的SQL生成方法

研究基于自然语言问题的结构化查询语言(SQL)生成问题(Text-to-SQL). 提出两阶段框架,旨在解耦模式链接和SQL生成过程,降低SQL生成的难度. 第1阶段通过基于关系图注意力网络的模式链接器识别问题中提及的数据库表、列和值,利用问题的语法结构和数据库模式项之间的内部关系,指导模型学习问题与数据库的对齐关系. 构建问题图时,针对Text-to-SQL任务的特点,在原始句法依存树的基础上,合并与模式链接无关的关系,添加并列结构中的从属词与句中其他成分间的依存关系,帮助模型捕获长距离依赖关系. 第2阶段进行SQL生成,将对齐信息注入T5的编码器,对T5进行微调. 在Spider、Spider-DK和Spider-Syn数据集上进行实验,实验结果显示,该方法具有良好的性能,尤其是对中等难度以上的Text-to-SQL问题具有良好的表现.


关键词: Text-to-SQL,  自然语言查询,  依存句法分析,  关系图注意力网络 
Fig.1 Two-stage SQL generation framework based on dependency graph attention network
Fig.2 Structure of schema linker
关系解释
dobj间接宾语
nmod名词修饰
compound复合词
conj连词
acl:relcl关系从句修饰
amod形容词修饰
nummod量词修饰
Tab.1 Collection of reserved dependencies
Fig.3 Example of dependency modification
yiyj关系解释
column-column-identityi = j
column-column-sametableyiyj同属一个表
column-column-fkyiyj的外键
column-column-fkryj$ {y}_{i} $的外键
table-table-identityi = j
table-table-fkryiyj中有一个外键列
table-table-fkbyjyi中有一个外键列
table-table-fkyiyj中均有对方的外键
table-column-pkyjyi的主键
table-column-hasyj属于yi,但不是主键
column-table-pkyiyj的主键
column-table-hasyi属于yj,但不是主键
Tab.2 Relation types of schema items
模型
PRF1PRF1PRF1
SLSQL0.8260.8200.8230.8060.8400.8220.7730.7410.757
本文方法0.8780.8530.8650.8380.8550.8460.8920.8830.888
Tab.3 Results of schema linking
问题Ground Truth本文方法SLSQL
1. What is the average $\underline{{\rm{rank}}}$ for winners
in all matches?
matches.winner_rank - columnmatches.winner_rank - columnrankings.ranking- column
2. How many languages are spoken in $\underline{{\rm{Aruba}}}$?country.Name - valuecountry.Name - valuecountrylanguage.Language - value
3. What are flight numbers of flights arriving
at Airport "$\underline{{\rm{APG}}}$"
flights.DestAirport - valueflights.DestAirport - valueairports.AirportName - value
Tab.4 Comparison of model prediction results for some samples
模型
PRF1PRF1PRF1
默认模型0.8780.8530.8650.8380.8550.8460.8920.8830.888
-依存增强0.8570.8280.8420.8490.8580.8540.8940.8800.887
-问题RGAT0.8420.8250.8330.8500.8360.8430.8900.8800.885
-模式RGAT0.8610.8240.8420.8500.8300.8400.8890.8740.881
Tab.5 Ablation study of schema linking
问题Ground Truth默认模型-问题RGAT
What are the names and $\underline{{\rm{ranks}}}$ of the three youngest winners across all matches?matches.winner_rank matches.winner_rankrankings.ranking
What is the total $\underline{{\rm{population}}}$ of Gelderland district?city.Populationcity.Populationcountry.Population
Return the first name, last name and email of the owners living in a state whose $\underline{{\rm{name}}}$ contains the substring 'North'.Owners.stateOwners.stateOwners.email_address
Tab.6 Comparison of column prediction
模型
PRF1PRF1PRF1
默认模型0.8780.8530.8650.8380.8550.8460.8920.8830.888
列+值0.8600.8420.8510.8520.8400.8460.8650.8570.861
问题词+值0.8660.8470.8560.8520.8790.8660.7990.7880.794
问题词+列序号0.8480.8410.8440.8580.8670.8630.8320.8150.823
Tab.7 Comparison of input sequence
模型EM/%EX/%
T5-Base[19]57.1
T5-3B[19]70.0
Unifiedskg(T5-Base)[22]58.160.1
Unifiedskg(T5-Large) [22]66.668.3
Unifiedskg(T5-3B)[22]71.874.4
T5-Base+PICARD[20]68.568.4
T5-Large+PICARD[20]69.172.9
T5-3B+PICARD[20]75.579.3
TKK(T5-Base)[21]61.564.2
TKK(T5-Large)[21]70.673.2
RASAT(T5-Base)[17]60.461.3
RASAT(T5-Large)[17]66.769.2
RASAT(T5-3B) [17]72.676.6
RASAT(T5-3B) +PICARD [17]75.380.5
本文方法-T5-Base68.571.6
本文方法-T5-Base+GT76.375.2
本文方法-T5-Large69.975.4
本文方法-T5-Large+GT79.280.9
Tab.8 Comparison of SQL generation results
模型EX/%
easymediumhardextra hard
TKK(T5-Large)[21]89.576.552.945.2
T5-3B+PICARD[20]95.285.467.250.6
RASAT(T5-3B)+PICARD[17]96.086.567.853.6
本文方法-T5-Large89.181.662.651.8
本文方法-T5-Large+GT92.787.271.356.6
Tab.9 Comparison of EX accuracy on different difficulty levels
模型Spider-DKSpider-Syn
EM/%EX/%EM/%EX/%
RAT-SQL+BERT[11]40.948.2
RAT-SQL+GRAPPA[29]38.549.1
T5-Base[21]40.843.8
T5-Large[21]53.157.4
TKK(T5-Base)[21]44.247.7
TKK(T5-Large)[21]55.160.5
本文方法-T5-Base39.346.649.854.6
本文方法-T5-Large48.255.356.060.9
Tab.10 SQL generation results on Spider-DK and Spider-Syn
[1]   刘喜平, 舒晴, 何佳壕, 等 基于自然语言的数据库查询生成研究综述[J]. 软件学报, 2022, 33 (11): 4107- 4136
LIU Xiping, SHU Qing, HE Jiahao, et al Survey on generating database queries based on natural language[J]. Journal of Software, 2022, 33 (11): 4107- 4136
[2]   GUO J, ZHAN Z, GAO Y, et al. Towards complex text-to-SQL in cross-domain database with intermediate representation [C]// Proceedings of ACL . Florence: ACL, 2019: 4524-4535.
[3]   WANG K, SHEN W, YANG Y, et al. Relational graph attention network for aspect-based sentiment analysis [C]// Proceedings of ACL . [S. l. ]: ACL, 2020: 3229-3238.
[4]   XU X, LIU C, SONG D X. SQLNet: generating structured queries from natural language without reinforcement learning [EB/OL]. (2017-11-13) [2023-10-27]. https://arxiv.org/abs/1711.04436.
[5]   YU T, LI Z, ZHANG Z, et al. TypeSQL: knowledge-based type-aware neural text-to-SQL generation [C]// Proceedings of NAACL-HLT . New Orleans: ACL, 2018: 588-594.
[6]   LEI W, WANG W, MA Z, et al. Re-examining the role of schema linking in text-to-SQL [C]// Proceedings of EMNLP . [S. l.]: ACL, 2020: 6943-6954.
[7]   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
[8]   BOGIN B, BERANT J, GARDNER M. Representing schema structure with graph neural networks for text-to-SQL parsing [C]// Proceedings of ACL . Florence: ACL, 2019: 4560-4565.
[9]   BOGIN B, GARDNER M, BERANT J. Global reasoning over database structures for text-to-SQL parsing [C]// Proceedings of EMNLP-IJCNLP 2019 . Hong Kong: ACL, 2019: 3659-3664.
[10]   ZHONG V, XIONG C, SOCHER R. Seq2SQL: generating structured queries from natural language using reinforcement learning [EB/OL]. (2017-11-09) [2023-10-27]. https://arxiv.org/abs/1709.00103.
[11]   WANG B, SHIN R, LIU X, et al. RAT-SQL: relation-aware schema encoding and linking for text-to-SQL parsers [C]// Proceedings of ACL . [S. l. ]: ACL, 2020: 7567-7578.
[12]   VASWANI A, SHAZEER N, PARMAR N, et al. Attention is all you need [C]// Advances in Neural Information Processing Systems . Long Beach: MIT Press, 2017: 5998–6008.
[13]   LIN X V, SOCHER R, XIONG C. Bridging textual and tabular data for cross-domain text-to-SQL semantic parsing [C]// Findings of the ACL: EMNLP 2020 . [S. l.]: ACL, 2020: 4870-4888.
[14]   CAO R, CHEN L, CHEN Z, et al. Line graph enhanced text-to-SQL model with mixed local and non-local relations [C]// Proceedings of ACL-IJCNLP 2021 . [S. l.]: ACL, 2021: 2541-2555.
[15]   CAI R, YUAN J, XU B, et al Sadga: Structure-aware dual graph aggregation network for text-to-sql[J]. Advances in Neural Information Processing Systems, 2021, 34: 7664- 7676
[16]   HUI B, GENG R, WANG L, et al. S2SQL: injecting syntax to question-schema interaction graph encoder for Text-to-SQL parsers [C]// Findings of the ACL: ACL 2022 . Dublin: ACL, 2022: 1254-1262.
[17]   QI J, TANG J, HE Z, et al. Rasat: Integrating relational structures into pretrained seq2seq model for text-to-sql [C]// Proceedings of the 2022 Conference on EMNLP . Abu Dhabi: ACL, 2022: 3215-3229.
[18]   RAFFEL C, SHAZEER N, ROBERTS A, et al Exploring the limits of transfer learning with a unified text-to-text transformer[J]. The Journal of Machine Learning Research, 2020, 21 (1): 5485- 5551
[19]   SHAW P, CHANG M W, PASUPAT P, et al. Compositional generalization and natural language variation: can a semantic parsing approach handle both? [C]// Proceedings of ACL-IJCNLP 2021 . [S. l. ]: ACL, 2021: 922–938.
[20]   SCHOLAK T, SCHUCHER N, BAHDANAU D. PICARD: parsing incrementally for constrained auto-regressive decoding from language models [C]// Proceedings of the 2021 Conference on EMNLP . Punta Cana: ACL, 2021: 9895-9901.
[21]   GAO C, LI B, ZHANG W, et al. Towards generalizable and robust Text-to-SQL parsing [C]// Findings of the ACL: EMNLP 2022 . Abu Dhabi: ACL, 2022: 2113-2125.
[22]   XIE T, WU C H, SHI P, et al. Unifiedskg: Unifying and multi-tasking structured knowledge grounding with text-to-text language models [C]// Proceedings of the 2022 Conference on EMNLP . Abu Dhabi: ACL, 2022: 602-631.
[23]   LI J, HUI B, CHENG R, et al. Graphix-T5: mixing pre-trained Transformers with graph-aware layers for text-to-SQL parsing [EB/OL]. (2023-01-18) [2023-10-27]. https://arxiv.org/abs/2301.07507.
[24]   DEVLIN J, CHANG M W, LEE K, et al. BERT: pre-training of deep bidirectional transformers for language understanding [C]// Proceedings of NAACL-HLT 2019 . Minneapolis: ACL, 2019: 4171–4186.
[25]   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 [C]// Proceedings of the 2018 Conference on EMNLP . Brussels: ACL, 2018: 3911–3921.
[26]   GAN Y, CHEN X, PURVER M. Exploring underexplored limitations of cross-domain Text-to-SQL generalization [C]// Proceedings of EMNLP . [S. l. ]: ACL, 2021: 8926–8931.
[27]   GAN Y, CHEN X, HUANG Q, et al. Towards robustness of Text-to-SQL models against synonym substitution [C]// Proceedings of ACL-IJCNLP. [S. l. ]: ACL , 2021: 2505-2515.
[28]   SHAZEER N, STERN M. Adafactor: adaptive learning rates with sublinear memory cost [C]// Proceedings of the 35th International Conference on Machine Learning , Stockholm: PMLR, 2018: 4596-4604.
[1] 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[J]. Journal of ZheJiang University (Engineering Science), 2023, 57(2): 277-286.