From 323d130ec412c4ec5a96f6e621b7bf621db302c0 Mon Sep 17 00:00:00 2001 From: liipx Date: Tue, 27 Nov 2018 11:55:29 +0800 Subject: [PATCH] update vendor --- ast/tidb.go | 1 + vendor/github.com/XiaoMi/soar/CHANGES.md | 82 - .../github.com/XiaoMi/soar/CODE_OF_CONDUCT.md | 46 - vendor/github.com/XiaoMi/soar/CONTRIBUTING.md | 3 - vendor/github.com/XiaoMi/soar/LICENSE | 202 - vendor/github.com/XiaoMi/soar/Makefile | 221 -- vendor/github.com/XiaoMi/soar/NOTICE.txt | 8 - vendor/github.com/XiaoMi/soar/README.md | 45 - vendor/github.com/XiaoMi/soar/README_EN.md | 40 - vendor/github.com/XiaoMi/soar/VERSION | 1 - vendor/github.com/XiaoMi/soar/advisor/doc.go | 18 - .../XiaoMi/soar/advisor/explainer.go | 285 -- .../XiaoMi/soar/advisor/explainer_test.go | 37 - .../XiaoMi/soar/advisor/heuristic.go | 3408 ----------------- .../XiaoMi/soar/advisor/heuristic_test.go | 3183 --------------- .../github.com/XiaoMi/soar/advisor/index.go | 1130 ------ .../XiaoMi/soar/advisor/index_test.go | 476 --- .../github.com/XiaoMi/soar/advisor/rules.go | 1426 ------- .../XiaoMi/soar/advisor/rules_test.go | 54 - vendor/github.com/XiaoMi/soar/ast/doc.go | 18 - vendor/github.com/XiaoMi/soar/ast/meta.go | 779 ---- .../github.com/XiaoMi/soar/ast/meta_test.go | 370 -- .../github.com/XiaoMi/soar/ast/node_array.go | 123 - vendor/github.com/XiaoMi/soar/ast/pretty.go | 350 -- .../github.com/XiaoMi/soar/ast/pretty_test.go | 201 - vendor/github.com/XiaoMi/soar/ast/rewrite.go | 1732 --------- .../XiaoMi/soar/ast/rewrite_test.go | 705 ---- vendor/github.com/XiaoMi/soar/ast/tidb.go | 61 - vendor/github.com/XiaoMi/soar/ast/token.go | 1028 ----- .../github.com/XiaoMi/soar/ast/token_test.go | 222 -- vendor/github.com/XiaoMi/soar/cmd/soar/doc.go | 18 - .../github.com/XiaoMi/soar/cmd/soar/soar.go | 429 --- .../XiaoMi/soar/cmd/soar/soar_test.go | 47 - .../github.com/XiaoMi/soar/cmd/soar/tool.go | 237 -- vendor/github.com/XiaoMi/soar/common/cases.go | 203 - .../github.com/XiaoMi/soar/common/chardet.go | 90 - .../XiaoMi/soar/common/chardet_test.go | 64 - .../github.com/XiaoMi/soar/common/config.go | 882 ----- .../XiaoMi/soar/common/config_test.go | 106 - vendor/github.com/XiaoMi/soar/common/doc.go | 18 - .../XiaoMi/soar/common/example_test.go | 63 - .../github.com/XiaoMi/soar/common/logger.go | 118 - .../XiaoMi/soar/common/logger_test.go | 59 - .../github.com/XiaoMi/soar/common/markdown.go | 156 - .../XiaoMi/soar/common/markdown_test.go | 90 - vendor/github.com/XiaoMi/soar/common/meta.go | 495 --- .../XiaoMi/soar/common/meta_test.go | 140 - .../github.com/XiaoMi/soar/common/signal.go | 39 - .../XiaoMi/soar/common/signal_test.go | 28 - .../github.com/XiaoMi/soar/common/tricks.go | 109 - .../github.com/XiaoMi/soar/common/version.go | 10 - vendor/github.com/XiaoMi/soar/database/doc.go | 18 - .../XiaoMi/soar/database/explain.go | 1095 ------ .../XiaoMi/soar/database/explain_test.go | 2463 ------------ .../github.com/XiaoMi/soar/database/mysql.go | 306 -- .../XiaoMi/soar/database/mysql_test.go | 111 - .../XiaoMi/soar/database/privilege.go | 104 - .../XiaoMi/soar/database/privilege_test.go | 31 - .../XiaoMi/soar/database/profiling.go | 135 - .../XiaoMi/soar/database/profiling_test.go | 53 - .../XiaoMi/soar/database/sampling.go | 230 -- .../XiaoMi/soar/database/sampling_test.go | 50 - .../github.com/XiaoMi/soar/database/show.go | 584 --- .../XiaoMi/soar/database/show_test.go | 94 - .../github.com/XiaoMi/soar/database/trace.go | 159 - .../XiaoMi/soar/database/trace_test.go | 58 - vendor/github.com/XiaoMi/soar/deps.sh | 12 - vendor/github.com/XiaoMi/soar/doc.go | 18 - vendor/github.com/XiaoMi/soar/env/doc.go | 18 - vendor/github.com/XiaoMi/soar/env/env.go | 546 --- vendor/github.com/XiaoMi/soar/env/env_test.go | 211 - vendor/github.com/XiaoMi/soar/genver.sh | 31 - .../github.com/XiaoMi/soar/retool-install.sh | 28 - vendor/github.com/XiaoMi/soar/revive.toml | 51 - vendor/github.com/XiaoMi/soar/tools.json | 45 - vendor/vendor.json | 42 - 76 files changed, 1 insertion(+), 26148 deletions(-) delete mode 100644 vendor/github.com/XiaoMi/soar/CHANGES.md delete mode 100644 vendor/github.com/XiaoMi/soar/CODE_OF_CONDUCT.md delete mode 100644 vendor/github.com/XiaoMi/soar/CONTRIBUTING.md delete mode 100644 vendor/github.com/XiaoMi/soar/LICENSE delete mode 100644 vendor/github.com/XiaoMi/soar/Makefile delete mode 100644 vendor/github.com/XiaoMi/soar/NOTICE.txt delete mode 100644 vendor/github.com/XiaoMi/soar/README.md delete mode 100644 vendor/github.com/XiaoMi/soar/README_EN.md delete mode 100644 vendor/github.com/XiaoMi/soar/VERSION delete mode 100644 vendor/github.com/XiaoMi/soar/advisor/doc.go delete mode 100644 vendor/github.com/XiaoMi/soar/advisor/explainer.go delete mode 100644 vendor/github.com/XiaoMi/soar/advisor/explainer_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/advisor/heuristic.go delete mode 100644 vendor/github.com/XiaoMi/soar/advisor/heuristic_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/advisor/index.go delete mode 100644 vendor/github.com/XiaoMi/soar/advisor/index_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/advisor/rules.go delete mode 100644 vendor/github.com/XiaoMi/soar/advisor/rules_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/ast/doc.go delete mode 100644 vendor/github.com/XiaoMi/soar/ast/meta.go delete mode 100644 vendor/github.com/XiaoMi/soar/ast/meta_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/ast/node_array.go delete mode 100644 vendor/github.com/XiaoMi/soar/ast/pretty.go delete mode 100644 vendor/github.com/XiaoMi/soar/ast/pretty_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/ast/rewrite.go delete mode 100644 vendor/github.com/XiaoMi/soar/ast/rewrite_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/ast/tidb.go delete mode 100644 vendor/github.com/XiaoMi/soar/ast/token.go delete mode 100644 vendor/github.com/XiaoMi/soar/ast/token_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/cmd/soar/doc.go delete mode 100644 vendor/github.com/XiaoMi/soar/cmd/soar/soar.go delete mode 100644 vendor/github.com/XiaoMi/soar/cmd/soar/soar_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/cmd/soar/tool.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/cases.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/chardet.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/chardet_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/config.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/config_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/doc.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/example_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/logger.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/logger_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/markdown.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/markdown_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/meta.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/meta_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/signal.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/signal_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/tricks.go delete mode 100644 vendor/github.com/XiaoMi/soar/common/version.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/doc.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/explain.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/explain_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/mysql.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/mysql_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/privilege.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/privilege_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/profiling.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/profiling_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/sampling.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/sampling_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/show.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/show_test.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/trace.go delete mode 100644 vendor/github.com/XiaoMi/soar/database/trace_test.go delete mode 100755 vendor/github.com/XiaoMi/soar/deps.sh delete mode 100644 vendor/github.com/XiaoMi/soar/doc.go delete mode 100644 vendor/github.com/XiaoMi/soar/env/doc.go delete mode 100644 vendor/github.com/XiaoMi/soar/env/env.go delete mode 100644 vendor/github.com/XiaoMi/soar/env/env_test.go delete mode 100755 vendor/github.com/XiaoMi/soar/genver.sh delete mode 100755 vendor/github.com/XiaoMi/soar/retool-install.sh delete mode 100644 vendor/github.com/XiaoMi/soar/revive.toml delete mode 100644 vendor/github.com/XiaoMi/soar/tools.json diff --git a/ast/tidb.go b/ast/tidb.go index 4fc27c1..8322d88 100644 --- a/ast/tidb.go +++ b/ast/tidb.go @@ -22,6 +22,7 @@ import ( "github.com/kr/pretty" "github.com/pingcap/parser" "github.com/pingcap/parser/ast" + // for pincap parser _ "github.com/pingcap/tidb/types/parser_driver" ) diff --git a/vendor/github.com/XiaoMi/soar/CHANGES.md b/vendor/github.com/XiaoMi/soar/CHANGES.md deleted file mode 100644 index b2ca818..0000000 --- a/vendor/github.com/XiaoMi/soar/CHANGES.md +++ /dev/null @@ -1,82 +0,0 @@ -# CHANGELOG - -## 2018-11 -- DOING: english translation -- add -cleanup-test-database command-line arg -- fix -config arg load file error -- fix #87 RuleImplicitConversion value type mistach check bug -- fix #38 always true where condition check -- abandon stdin terminal interactive mod, which may seems like hangup - -## 2018-10 -- Fix SplitStatement mulitstatement eof bug #66 -- Fix pretty func hangup issue #47 -- Fix some foolish code spell error -- Use travis for CI -- Fix Go 1.8 defapth GOPATH compatible issue BUG #5 -- 2018-10-20 开源先锋日(OSCAR)对外正式开源发布代码 - -## 2018-09 -- 修复多个启发式建议不准确BUG,优化部分建议文案使得建议更清晰 -- 基于TiDB Parser完善多个DDL类型语句的建议 -- 新增lint report-type类型,支持Vim Plugin优化建议输出 -- 更新整理项目文档,开源准备 -- 2018-09-21 Gdevops SOAR首次对外进行技术分享宣传 - -## 2018-08 -- 利用docker临时容器进行daily测试 -- 添加main_test全功能回归测试 -- 修复在测试中发现的问题 -- mymysql合并MySQL8.0相关PR,修改vendor依赖 -- 改善HeuristicRule中的文案 -- 持续集成Vitess Parser的改进 -- NewQuery4Audit结构体中引入TiDB Parser -- 通过TiAST完成大量与 DDL 相关的TODO -- 修改heuristic rules检查的返回值,提升拓展性 -- 建议中引入Position,用于表示建议产生于SQL的位置 -- 新增多个HeuristicRule -- Makefile中添加依赖检查,优化Makefile中逻辑,添加新功能 -- 优化gometalinter性能,引入新的代码质量检测工具,提升代码质量 -- 引入 retool 用于管理依赖的工具 -- 优化 doc 文档 - -## 2018-07 -- 补充文档,添加项目LOGO -- 改善代码质量提升测试覆盖度 -- mymysql升级,支持MySQL 8.0 -- 提供remove-comment小工具 -- 提供索引重复检查小工具 -- HeuristicRule新增RuleSpaceAfterDot -- 支持字符集和Collation不相同时的隐式数据类型转换的检查 - -## 2018-06 -- 支持更多的SQL Rewrite规则 -- 添加SQL执行超时限制 -- 索引优化建议支持对约束的检查 -- 修复数据采样中null值处理不正确的问题 -- Explain支持last_query_cost - -## 2018-05 -- 添加数据采样功能 -- 添加语句执行安全检查 -- 支持DDL语法检查 -- 支持DDL在测试环境的执行 -- 支持隐式数据类型转换检查 -- 支持索引去重 -- 索引优化建议支持前缀索引 -- 支持SQL Pretty输出 - -## 2018-04 -- 支持语法检查 -- 支持测试环境 -- 支持MySQL原数据的获取 -- 支持基于数据库环境信息给予索引优化建议 -- 支持不依赖数据库原信息的简单索引优化建议 -- 添加日志模块 -- 引入配置文件 - -## 2018-03 -- 基本架构设计 -- 添加大量底层函数用于处理AST -- 添加Insert、Delete、Update转写成Select的基本函数 -- 支持MySQL Explain信息输出 diff --git a/vendor/github.com/XiaoMi/soar/CODE_OF_CONDUCT.md b/vendor/github.com/XiaoMi/soar/CODE_OF_CONDUCT.md deleted file mode 100644 index 220407c..0000000 --- a/vendor/github.com/XiaoMi/soar/CODE_OF_CONDUCT.md +++ /dev/null @@ -1,46 +0,0 @@ -# Contributor Covenant Code of Conduct - -## Our Pledge - -In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. - -## Our Standards - -Examples of behavior that contributes to creating a positive environment include: - -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members - -Examples of unacceptable behavior by participants include: - -* The use of sexualized language or imagery and unwelcome sexual attention or advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a professional setting - -## Our Responsibilities - -Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. - -Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. - -## Scope - -This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. - -## Enforcement - -Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at zhangliang3@xiaomi.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. - -Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. - -## Attribution - -This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] - -[homepage]: http://contributor-covenant.org -[version]: http://contributor-covenant.org/version/1/4/ diff --git a/vendor/github.com/XiaoMi/soar/CONTRIBUTING.md b/vendor/github.com/XiaoMi/soar/CONTRIBUTING.md deleted file mode 100644 index 0346f4e..0000000 --- a/vendor/github.com/XiaoMi/soar/CONTRIBUTING.md +++ /dev/null @@ -1,3 +0,0 @@ -Ask questions at [Gitter](https://gitter.im/xiaomi-dba/soar). - -[Open an issue](https://github.com/xiaomi/soar/issues/new) to discuss your plans before doing any work on SOAR. diff --git a/vendor/github.com/XiaoMi/soar/LICENSE b/vendor/github.com/XiaoMi/soar/LICENSE deleted file mode 100644 index d645695..0000000 --- a/vendor/github.com/XiaoMi/soar/LICENSE +++ /dev/null @@ -1,202 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/vendor/github.com/XiaoMi/soar/Makefile b/vendor/github.com/XiaoMi/soar/Makefile deleted file mode 100644 index a89aabc..0000000 --- a/vendor/github.com/XiaoMi/soar/Makefile +++ /dev/null @@ -1,221 +0,0 @@ -# This how we want to name the binary output -# -# use checkmake linter https://github.com/mrtazz/checkmake -# $ checkmake Makefile -# -BINARY=soar -GOPATH ?= $(shell go env GOPATH) -# Ensure GOPATH is set before running build process. -ifeq "$(GOPATH)" "" - $(error Please set the environment variable GOPATH before running `make`) -endif -PATH := ${GOPATH}/bin:$(PATH) -GCFLAGS=-gcflags "all=-trimpath=${GOPATH}" -LDFLAGS=-ldflags="-s -w" - -# These are the values we want to pass for VERSION and BUILD -BUILD_TIME=`date +%Y%m%d%H%M` -COMMIT_VERSION=`git rev-parse HEAD` - -# Add mysql version for testing `MYSQL_RELEASE=percona MYSQL_VERSION=5.7 make docker` -# MYSQL_RELEASE: mysql, percona, mariadb ... -# MYSQL_VERSION: latest, 8.0, 5.7, 5.6, 5.5 ... -# use mysql:latest as default -MYSQL_RELEASE := $(or ${MYSQL_RELEASE}, ${MYSQL_RELEASE}, mysql) -MYSQL_VERSION := $(or ${MYSQL_VERSION}, ${MYSQL_VERSION}, latest) - -.PHONY: all -all: | fmt build - -.PHONY: go_version_check -GO_VERSION_MIN=1.10 -# Parse out the x.y or x.y.z version and output a single value x*10000+y*100+z (e.g., 1.9 is 10900) -# that allows the three components to be checked in a single comparison. -VER_TO_INT:=awk '{split(substr($$0, match ($$0, /[0-9\.]+/)), a, "."); print a[1]*10000+a[2]*100+a[3]}' -go_version_check: - @echo "\033[92mGo version check\033[0m" - @if test $(shell go version | $(VER_TO_INT) ) -lt \ - $(shell echo "$(GO_VERSION_MIN)" | $(VER_TO_INT)); \ - then printf "go version $(GO_VERSION_MIN)+ required, found: "; go version; exit 1; \ - else echo "go version check pass"; fi - -# Dependency check -.PHONY: deps -deps: - @echo "\033[92mDependency check\033[0m" - @bash ./deps.sh - # The retool tools.json is setup from retool-install.sh - retool sync - retool do gometalinter.v2 intall - -# Code format -.PHONY: fmt -fmt: go_version_check - @echo "\033[92mRun gofmt on all source files ...\033[0m" - @echo "gofmt -l -s -w ..." - @ret=0 && for d in $$(go list -f '{{.Dir}}' ./... | grep -v /vendor/); do \ - gofmt -l -s -w $$d/*.go || ret=$$? ; \ - done ; exit $$ret - -# Run golang test cases -.PHONY: test -test: - @echo "\033[92mRun all test cases ...\033[0m" - go test ./... - @echo "test Success!" - -# Code Coverage -# colorful coverage numerical >=90% GREEN, <80% RED, Other YELLOW -.PHONY: cover -cover: test - @echo "\033[92mRun test cover check ...\033[0m" - go test -coverpkg=./... -coverprofile=coverage.data ./... | column -t - go tool cover -html=coverage.data -o coverage.html - go tool cover -func=coverage.data -o coverage.txt - @tail -n 1 coverage.txt | awk '{sub(/%/, "", $$NF); \ - if($$NF < 80) \ - {print "\033[91m"$$0"%\033[0m"} \ - else if ($$NF >= 90) \ - {print "\033[92m"$$0"%\033[0m"} \ - else \ - {print "\033[93m"$$0"%\033[0m"}}' - -# Builds the project -build: fmt - @echo "\033[92mBuilding ...\033[0m" - @mkdir -p bin - @bash ./genver.sh - @ret=0 && for d in $$(go list -f '{{if (eq .Name "main")}}{{.ImportPath}}{{end}}' ./...); do \ - b=$$(basename $${d}) ; \ - go build ${GCFLAGS} -o bin/$${b} $$d || ret=$$? ; \ - done ; exit $$ret - @echo "build Success!" - -# Installs our project: copies binaries -install: build - @echo "\033[92mInstall ...\033[0m" - go install ./... - @echo "install Success!" - -# Generate doc use -list* command -.PHONY: doc -doc: build - @echo "\033[92mAuto generate doc ...\033[0m" - ./bin/soar -list-heuristic-rules > doc/heuristic.md - ./bin/soar -list-rewrite-rules > doc/rewrite.md - ./bin/soar -list-report-types > doc/report_type.md - -# Add or change a heuristic rule -.PHONY: heuristic -heuristic: doc docker - @echo "\033[92mUpdate Heuristic rule golden files ...\033[0m" - go test github.com/XiaoMi/soar/advisor -v -update -run TestListHeuristicRules - go test github.com/XiaoMi/soar/advisor -v -update -run TestMergeConflictHeuristicRules - docker stop soar-mysql 2>/dev/null || true - -# Update vitess vendor -.PHONY: vitess -vitess: - @echo "\033[92mUpdate vitess deps ...\033[0m" - govendor fetch -v vitess.io/vitess/... - -# Update tidb vendor -.PHONY: tidb -tidb: - @echo "\033[92mUpdate tidb deps ...\033[0m" - govendor fetch -v github.com/pingcap/tidb/... - -# make pingcap parser -.PHONY: pingcap-parser -pingcap-parser: tidb - @echo "\033[92mimporting pingcap parser ...\033[0m" - govendor fetch -v github.com/pingcap/parser/... - -# Update all vendor -.PHONY: vendor -vendor: vitess pingcap-parser -# gometalinter -# 如果有不想改的lint问题可以使用metalinter.sh加黑名单 -#@bash doc/example/metalinter.sh -.PHONY: lint -lint: build - @echo "\033[92mRun linter check ...\033[0m" - CGO_ENABLED=0 retool do gometalinter.v2 -j 1 --config doc/example/metalinter.json ./... - retool do revive -formatter friendly --exclude vendor/... -config doc/example/revive.toml ./... - retool do golangci-lint --tests=false run - @echo "gometalinter check your code is pretty good" - -.PHONY: release -release: deps build - @echo "\033[92mCross platform building for release ...\033[0m" - @mkdir -p release - @for GOOS in darwin linux windows; do \ - for GOARCH in amd64; do \ - for d in $$(go list -f '{{if (eq .Name "main")}}{{.ImportPath}}{{end}}' ./...); do \ - b=$$(basename $${d}) ; \ - echo "Building $${b}.$${GOOS}-$${GOARCH} ..."; \ - GOOS=$${GOOS} GOARCH=$${GOARCH} go build ${GCFLAGS} ${LDFLAGS} -v -o release/$${b}.$${GOOS}-$${GOARCH} $$d 2>/dev/null ; \ - done ; \ - done ;\ - done - -.PHONY: docker -docker: - @echo "\033[92mBuild mysql test enviorment\033[0m" - @docker stop soar-mysql 2>/dev/null || true - @echo "docker run --name soar-mysql $(MYSQL_RELEASE):$(MYSQL_VERSION)" - @docker run --name soar-mysql --rm -d \ - -e MYSQL_ROOT_PASSWORD=1tIsB1g3rt \ - -e MYSQL_DATABASE=sakila \ - -p 3306:3306 \ - -v `pwd`/doc/example/sakila.sql.gz:/docker-entrypoint-initdb.d/sakila.sql.gz \ - $(MYSQL_RELEASE):$(MYSQL_VERSION) - - @echo -n "waiting for sakila database initializing " - @while ! mysql -h 127.0.0.1 -u root sakila -p1tIsB1g3rt -NBe "do 1;" 2>/dev/null; do \ - printf '.' ; \ - sleep 1 ; \ - done ; \ - echo '.' - @echo "mysql test enviorment is ready!" - -.PHONY: connect -connect: - mysql -h 127.0.0.1 -u root -p1tIsB1g3rt -c - -.PHONY: main_test -main_test: install - @echo "\033[92mrunning main_test\033[0m" - @echo "soar -list-test-sqls | soar" - @./doc/example/main_test.sh - @echo "main_test Success!" - -.PHONY: daily -daily: | deps fmt vendor docker cover doc lint release install main_test clean logo - @echo "\033[92mdaily build finished\033[0m" - -# vendor, docker will cost long time, if all those are ready, daily-quick will much more fast. -.PHONY: daily-quick -daily-quick: | deps fmt cover doc lint logo - @echo "\033[92mdaily-quick build finished\033[0m" - -.PHONY: logo -logo: - @echo "\033[93m" - @cat doc/images/logo.ascii - @echo "\033[m" - -# Cleans our projects: deletes binaries -.PHONY: clean -clean: - @echo "\033[92mCleanup ...\033[0m" - go clean - @for GOOS in darwin linux windows; do \ - for GOARCH in 386 amd64; do \ - rm -f ${BINARY}.$${GOOS}-$${GOARCH} ;\ - done ;\ - done - rm -f ${BINARY} coverage.* - find . -name "*.log" -delete - git clean -fi - docker stop soar-mysql 2>/dev/null || true diff --git a/vendor/github.com/XiaoMi/soar/NOTICE.txt b/vendor/github.com/XiaoMi/soar/NOTICE.txt deleted file mode 100644 index 84fbfb3..0000000 --- a/vendor/github.com/XiaoMi/soar/NOTICE.txt +++ /dev/null @@ -1,8 +0,0 @@ - -Copyright 2018 Xiaomi, Inc. All Rights Reserved. -This product includes software developed by Xiaomi, Inc. -(http://www.mi.com/). -This product is licensed to you under the Apache License, Version 2.0 -(the "License"). You may not use this product except in compliance with -the License. - diff --git a/vendor/github.com/XiaoMi/soar/README.md b/vendor/github.com/XiaoMi/soar/README.md deleted file mode 100644 index 5f23326..0000000 --- a/vendor/github.com/XiaoMi/soar/README.md +++ /dev/null @@ -1,45 +0,0 @@ -# ![SOAR](https://raw.githubusercontent.com/XiaoMi/soar/master/doc/images/logo.png) - -[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/xiaomi-dba/soar) -[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](http://github.com/XiaoMi/soar/blob/master/LICENSE) -[![Go Report Card](https://goreportcard.com/badge/github.com/XiaoMi/soar)](https://goreportcard.com/report/github.com/XiaoMi/soar) -[![Build Status](https://travis-ci.org/XiaoMi/soar.svg?branch=master)](https://travis-ci.org/XiaoMi/soar) -[![GoDoc](https://godoc.org/github.com/XiaoMi/soar?status.svg)](https://godoc.org/github.com/XiaoMi/soar) - -[文档](http://github.com/XiaoMi/soar/tree/master/doc) | [FAQ](http://github.com/XiaoMi/soar/blob/master/doc/FAQ.md) | [变更记录](http://github.com/XiaoMi/soar/blob/master/CHANGES.md) | [路线图](http://github.com/XiaoMi/soar/blob/master/doc/roadmap.md) | [English](http://github.com/XiaoMi/soar/blob/master/README_EN.md) - -## SOAR - -SOAR(SQL Optimizer And Rewriter)是一个对SQL进行优化和改写的自动化工具。 由小米人工智能与云平台的数据库团队开发与维护。 - -## 功能特点 - -* 跨平台支持(支持Linux, Mac环境,Windows环境理论上也支持,不过未全面测试) -* 目前只支持 MySQL 语法族协议的SQL优化 -* 支持基于启发式算法的语句优化 -* 支持复杂查询的多列索引优化(UPDATE, INSERT, DELETE, SELECT) -* 支持EXPLAIN信息丰富解读 -* 支持SQL指纹、压缩和美化 -* 支持同一张表多条ALTER请求合并 -* 支持自定义规则的SQL改写 - -## 快速入门 - -* [安装使用](http://github.com/XiaoMi/soar/blob/master/doc/install.md) -* [体系架构](http://github.com/XiaoMi/soar/blob/master/doc/structure.md) -* [配置文件](http://github.com/XiaoMi/soar/blob/master/doc/config.md) -* [常用命令](http://github.com/XiaoMi/soar/blob/master/doc/cheatsheet.md) -* [产品对比](http://github.com/XiaoMi/soar/blob/master/doc/comparison.md) -* [路线图](http://github.com/XiaoMi/soar/blob/master/doc/roadmap.md) - -## 交流与反馈 - -* 欢迎通过Github Issues提交问题报告与建议 -* QQ群: 779359816(满) 758940447(新) -* [Gitter](https://gitter.im/xiaomi-dba/soar) 推荐 - - ![xiaomi_sa](https://raw.githubusercontent.com/XiaoMi/soar/master/doc/images/xiaomi_sa.png) - -## License - -[Apache License 2.0](https://github.com/XiaoMi/soar/blob/master/LICENSE). diff --git a/vendor/github.com/XiaoMi/soar/README_EN.md b/vendor/github.com/XiaoMi/soar/README_EN.md deleted file mode 100644 index fc7b7be..0000000 --- a/vendor/github.com/XiaoMi/soar/README_EN.md +++ /dev/null @@ -1,40 +0,0 @@ -# ![SOAR](https://raw.githubusercontent.com/XiaoMi/soar/master/doc/images/logo.png) - -[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/xiaomi-dba/soar) -[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](http://github.com/XiaoMi/soar/blob/master/LICENSE) -[![Go Report Card](https://goreportcard.com/badge/github.com/XiaoMi/soar)](https://goreportcard.com/report/github.com/XiaoMi/soar) -[![Build Status](https://travis-ci.org/XiaoMi/soar.svg?branch=master)](https://travis-ci.org/XiaoMi/soar) -[![GoDoc](https://godoc.org/github.com/XiaoMi/soar?status.svg)](https://godoc.org/github.com/XiaoMi/soar) - -[Docs](http://github.com/XiaoMi/soar/tree/master/doc) | [FAQ](http://github.com/XiaoMi/soar/blob/master/doc/FAQ_en.md) | [中文](http://github.com/XiaoMi/soar/blob/master/README.md) - -## SOAR - -SOAR (SQL Optimizer And Rewriter) is a tool, which can help SQL optimization and rewrite. It's developed and maintained by the DBA Team of Xiaomi AI&Cloud. - -## Features - -* Cross-platform support, such as Linux, Mac, and Windows -* Support Heuristic Rules Suggestion -* Support Complicate SQL Indexing Optimize -* Support EXPLAIN analyze for query plan -* Support SQL fingerprint, compress and built-in pretty print -* Support merge multi ALTER query into one SQL -* Support self-config rewrite rules from SQL Rewrite -* Suggestions were written in Chinese. But SOAR also gives many tools, which can be used without understanding Chinese. - -## QuickStart - -* [Install](http://github.com/XiaoMi/soar/blob/master/doc/install_en.md) -* [CheatSheet](http://github.com/XiaoMi/soar/blob/master/doc/cheatsheet_en.md) -* [Related works](http://github.com/XiaoMi/soar/blob/master/doc/comparison_en.md) - -## Communication - -* GitHub issues: bug reports, usage issues, feature requests -* [Gitter](https://gitter.im/xiaomi-dba/soar) -* IM QQ Group: 779359816 - -## License - -[Apache License 2.0](https://github.com/XiaoMi/soar/blob/master/LICENSE). diff --git a/vendor/github.com/XiaoMi/soar/VERSION b/vendor/github.com/XiaoMi/soar/VERSION deleted file mode 100644 index ac39a10..0000000 --- a/vendor/github.com/XiaoMi/soar/VERSION +++ /dev/null @@ -1 +0,0 @@ -0.9.0 diff --git a/vendor/github.com/XiaoMi/soar/advisor/doc.go b/vendor/github.com/XiaoMi/soar/advisor/doc.go deleted file mode 100644 index e435db2..0000000 --- a/vendor/github.com/XiaoMi/soar/advisor/doc.go +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Package advisor contain heuristic rules, index rules and explain translator. -package advisor diff --git a/vendor/github.com/XiaoMi/soar/advisor/explainer.go b/vendor/github.com/XiaoMi/soar/advisor/explainer.go deleted file mode 100644 index c8b7c5f..0000000 --- a/vendor/github.com/XiaoMi/soar/advisor/explainer.go +++ /dev/null @@ -1,285 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package advisor - -import ( - "fmt" - "strings" - - "github.com/XiaoMi/soar/common" - "github.com/XiaoMi/soar/database" -) - -var explainRuleID int - -// [EXP.XXX]Rule -var explainRules map[string]Rule - -// [table_name]"suggest text" -var tablesSuggests map[string][]string - -/* -var explainIgnoreTables = []string{ - "dual", - "", -} -*/ - -// explain建议的形式 -// Item: EXP.XXX -// Severity: L[0-8] -// Summary: full table scan, not use index, full index scan... -// Content: XX TABLE xxx - -// -func checkExplainSelectType(exp *database.ExplainInfo) { - // 判断是否跳过不检查 - if len(common.Config.ExplainWarnSelectType) == 1 { - if common.Config.ExplainWarnSelectType[0] == "" { - return - } - } else if len(common.Config.ExplainWarnSelectType) < 1 { - return - } - - if exp.ExplainFormat == database.JSONFormatExplain { - // TODO - // JSON 形式遍历分析不方便,转成 Row 格式也没有 SelectType 暂不处理 - return - } - for _, v := range common.Config.ExplainWarnSelectType { - for _, row := range exp.ExplainRows { - if row.SelectType == v && v != "" { - tablesSuggests[row.TableName] = append(tablesSuggests[row.TableName], fmt.Sprintf("SelectType:%s", row.SelectType)) - } - } - } -} - -// 用户可以设置AccessType的建议级别,匹配到的查询会给出建议 -func checkExplainAccessType(exp *database.ExplainInfo) { - // 判断是否跳过不检查 - if len(common.Config.ExplainWarnAccessType) == 1 { - if common.Config.ExplainWarnAccessType[0] == "" { - return - } - } else if len(common.Config.ExplainWarnAccessType) < 1 { - return - } - - rows := exp.ExplainRows - if exp.ExplainFormat == database.JSONFormatExplain { - // JSON形式遍历分析不方便,转成Row格式统一处理 - rows = database.ConvertExplainJSON2Row(exp.ExplainJSON) - } - for _, v := range common.Config.ExplainWarnAccessType { - for _, row := range rows { - if row.AccessType == v && v != "" { - tablesSuggests[row.TableName] = append(tablesSuggests[row.TableName], fmt.Sprintf("Scalability:%s", row.Scalability)) - } - } - } -} - -// TODO: -/* -func checkExplainPossibleKeys(exp *database.ExplainInfo) { - // 判断是否跳过不检查 - if common.Config.ExplainMinPossibleKeys == 0 { - return - } - - rows := exp.ExplainRows - if exp.ExplainFormat == database.JSONFormatExplain { - // JSON形式遍历分析不方便,转成Row格式统一处理 - rows = database.ConvertExplainJSON2Row(exp.ExplainJSON) - } - for _, row := range rows { - if len(row.PossibleKeys) < common.Config.ExplainMinPossibleKeys { - tablesSuggests[row.TableName] = append(tablesSuggests[row.TableName], fmt.Sprintf("PossibleKeys:%d < %d", - len(row.PossibleKeys), common.Config.ExplainMinPossibleKeys)) - } - } -} -*/ - -// TODO: -/* -func checkExplainKeyLen(exp *database.ExplainInfo) { -} -*/ - -// TODO: -/* -func checkExplainKey(exp *database.ExplainInfo) { - // 小于最小使用试用key数量 - //return intval($explainResult) < intval($userCond); - //explain-min-keys int -} -*/ - -func checkExplainRef(exp *database.ExplainInfo) { - rows := exp.ExplainRows - if exp.ExplainFormat == database.JSONFormatExplain { - // JSON形式遍历分析不方便,转成Row格式统一处理 - rows = database.ConvertExplainJSON2Row(exp.ExplainJSON) - } - for i, row := range rows { - if strings.Join(row.Ref, "") == "NULL" || strings.Join(row.Ref, "") == "" { - if i == 0 && len(rows) > 1 { - continue - } - tablesSuggests[row.TableName] = append(tablesSuggests[row.TableName], fmt.Sprintf("Ref:null")) - } - } -} - -func checkExplainRows(exp *database.ExplainInfo) { - // 判断是否跳过不检查 - if common.Config.ExplainMaxRows <= 0 { - return - } - - rows := exp.ExplainRows - if exp.ExplainFormat == database.JSONFormatExplain { - // JSON形式遍历分析不方便,转成Row格式统一处理 - rows = database.ConvertExplainJSON2Row(exp.ExplainJSON) - } - - for _, row := range rows { - if row.Rows >= common.Config.ExplainMaxRows { - tablesSuggests[row.TableName] = append(tablesSuggests[row.TableName], fmt.Sprintf("Rows:%d", row.Rows)) - } - } -} - -// TODO: -/* -func checkExplainExtra(exp *database.ExplainInfo) { - // 包含用户配置的逗号分隔关键词之一则提醒 - // return self::contains($explainResult, $userCond); - // explain-warn-extra []string -} -*/ - -func checkExplainFiltered(exp *database.ExplainInfo) { - // 判断是否跳过不检查 - if common.Config.ExplainMaxFiltered <= 0.001 { - return - } - - rows := exp.ExplainRows - if exp.ExplainFormat == database.JSONFormatExplain { - // JSON形式遍历分析不方便,转成Row格式统一处理 - rows = database.ConvertExplainJSON2Row(exp.ExplainJSON) - } - for i, row := range rows { - if i == 0 && len(rows) > 1 { - continue - } - if row.Filtered >= common.Config.ExplainMaxFiltered { - tablesSuggests[row.TableName] = append(tablesSuggests[row.TableName], fmt.Sprintf("Filtered:%.2f%s", row.Filtered, "%")) - } - } -} - -// ExplainAdvisor 基于explain信息给出建议 -func ExplainAdvisor(exp *database.ExplainInfo) map[string]Rule { - common.Log.Debug("ExplainAdvisor SQL: %v", exp.SQL) - explainRuleID = 0 - explainRules = make(map[string]Rule) - tablesSuggests = make(map[string][]string) - - checkExplainSelectType(exp) - checkExplainAccessType(exp) - checkExplainFiltered(exp) - checkExplainRef(exp) - checkExplainRows(exp) - - // 打印explain table - content := database.PrintMarkdownExplainTable(exp) - - if common.Config.ShowWarnings { - content += "\n" + database.MySQLExplainWarnings(exp) - } - - // 对explain table中各项难于理解的值做解释 - cases := database.ExplainInfoTranslator(exp) - - // 添加last_query_cost - if common.Config.ShowLastQueryCost { - content += "\n" + database.MySQLExplainQueryCost(exp) - } - - if content != "" { - explainRules["EXP.000"] = Rule{ - Item: "EXP.000", - Severity: "L0", - Summary: "Explain信息", - Content: content, - Case: cases, - Func: (*Query4Audit).RuleOK, - } - } - /* - for t, s := range tablesSuggests { - // 检查explain对应的表是否需要跳过,如dual,空表等 - ig := false - for _, ti := range explainIgnoreTables { - if ti == t { - ig = true - } - } - if ig { - continue - } - ruleId := fmt.Sprintf("EXP.%03d", explainRuleId+1) - explainRuleId = explainRuleId + 1 - explainRules[ruleId] = Rule{ - Item: ruleId, - Severity: "L0", - Summary: fmt.Sprintf("表 `%s` 查询效率不高", t), - Content: fmt.Sprint("原因:", strings.Join(s, ",")), - Case: "", - Func: (*Query4Audit).RuleOK, - } - } - */ - return explainRules -} - -// DigestExplainText 分析用户输入的EXPLAIN信息 -func DigestExplainText(text string) { - // explain信息就不要显示完美了,美不美自己看吧。 - common.Config.IgnoreRules = append(common.Config.IgnoreRules, "OK") - - if !IsIgnoreRule("EXP.") { - explainInfo, err := database.ParseExplainText(text) - if err != nil { - common.Log.Error("main ParseExplainText Error: %v", err) - return - } - expSuggest := ExplainAdvisor(explainInfo) - _, output := FormatSuggest("", common.Config.ReportType, expSuggest) - if common.Config.ReportType == "html" { - fmt.Println(common.MarkdownHTMLHeader()) - fmt.Println(common.Markdown2HTML(output)) - } else { - fmt.Println(output) - } - } -} diff --git a/vendor/github.com/XiaoMi/soar/advisor/explainer_test.go b/vendor/github.com/XiaoMi/soar/advisor/explainer_test.go deleted file mode 100644 index 0fc8a86..0000000 --- a/vendor/github.com/XiaoMi/soar/advisor/explainer_test.go +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package advisor - -import ( - "testing" - - "github.com/XiaoMi/soar/common" -) - -func TestDigestExplainText(t *testing.T) { - var text = `+----+-------------+---------+-------+---------------------------------------------------------+-------------------+---------+---------------------------+------+-------------+ -| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | -+----+-------------+---------+-------+---------------------------------------------------------+-------------------+---------+---------------------------+------+-------------+ -| 1 | SIMPLE | country | index | PRIMARY,country_id | country | 152 | NULL | 109 | Using index | -| 1 | SIMPLE | city | ref | idx_fk_country_id,idx_country_id_city,idx_all,idx_other | idx_fk_country_id | 2 | sakila.country.country_id | 2 | Using index | -+----+-------------+---------+-------+---------------------------------------------------------+-------------------+---------+---------------------------+------+-------------+` - common.Config.ReportType = "explain-digest" - err := common.GoldenDiff(func() { DigestExplainText(text) }, t.Name(), update) - if nil != err { - t.Fatal(err) - } -} diff --git a/vendor/github.com/XiaoMi/soar/advisor/heuristic.go b/vendor/github.com/XiaoMi/soar/advisor/heuristic.go deleted file mode 100644 index c35682f..0000000 --- a/vendor/github.com/XiaoMi/soar/advisor/heuristic.go +++ /dev/null @@ -1,3408 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package advisor - -import ( - "bytes" - "fmt" - "regexp" - "strconv" - "strings" - "unicode/utf8" - - "github.com/XiaoMi/soar/ast" - "github.com/XiaoMi/soar/common" - "github.com/XiaoMi/soar/database" - "github.com/gedex/inflector" - "github.com/percona/go-mysql/query" - tidb "github.com/pingcap/parser/ast" - "github.com/pingcap/parser/mysql" - "vitess.io/vitess/go/vt/sqlparser" -) - -// RuleOK OK -func (q *Query4Audit) RuleOK() Rule { - return HeuristicRules["OK"] -} - -// RuleImplicitAlias ALI.001 -func (q *Query4Audit) RuleImplicitAlias() Rule { - var rule = q.RuleOK() - tkns := ast.Tokenizer(q.Query) - if len(tkns) == 0 { - return rule - } - if tkns[0].Type != sqlparser.SELECT { - return rule - } - for i, tkn := range tkns { - if tkn.Type == sqlparser.ID && i+1 < len(tkns) && tkn.Type == tkns[i+1].Type { - rule = HeuristicRules["ALI.001"] - break - } - } - return rule -} - -// RuleStarAlias ALI.002 -func (q *Query4Audit) RuleStarAlias() Rule { - var rule = q.RuleOK() - re := regexp.MustCompile(`(?i)(\*\s+as\b)`) - if re.FindString(q.Query) != "" { - rule = HeuristicRules["ALI.002"] - } - return rule -} - -// RuleSameAlias ALI.003 -func (q *Query4Audit) RuleSameAlias() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case *sqlparser.AliasedExpr: - switch n := expr.Expr.(type) { - case *sqlparser.ColName: - if n.Name.String() == expr.As.String() { - rule = HeuristicRules["ALI.003"] - return false, nil - } - } - case *sqlparser.AliasedTableExpr: - switch n := expr.Expr.(type) { - case sqlparser.TableName: - if n.Name.String() == expr.As.String() { - rule = HeuristicRules["ALI.003"] - return false, nil - } - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RulePrefixLike ARG.001 -func (q *Query4Audit) RulePrefixLike() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case *sqlparser.ComparisonExpr: - if expr.Operator == "like" { - switch sqlval := expr.Right.(type) { - case *sqlparser.SQLVal: - // prefix like with '%', '_' - if sqlval.Type == 0 && (sqlval.Val[0] == 0x25 || sqlval.Val[0] == 0x5f) { - rule = HeuristicRules["ARG.001"] - return false, nil - } - } - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleEqualLike ARG.002 -func (q *Query4Audit) RuleEqualLike() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case *sqlparser.ComparisonExpr: - if expr.Operator == "like" { - switch sqlval := expr.Right.(type) { - case *sqlparser.SQLVal: - // not start with '%', '_' && not end with '%', '_' - if sqlval.Type == 0 { - if sqlval.Val[0] != 0x25 && - sqlval.Val[0] != 0x5f && - sqlval.Val[len(sqlval.Val)-1] != 0x5f && - sqlval.Val[len(sqlval.Val)-1] != 0x25 { - rule = HeuristicRules["ARG.002"] - return false, nil - } - } else { - rule = HeuristicRules["ARG.002"] - return false, nil - } - } - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleImplicitConversion ARG.003 -// 隐式类型转换检查:该项检查一定是在开启测试环境或线上环境情境下下进行的 -func (idxAdv *IndexAdvisor) RuleImplicitConversion() Rule { - /* - * 两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做类型转换 - * 两个参数都是字符串,会按照字符串来比较,不做类型转换 - * 两个参数都是整数,按照整数来比较,不做类型转换 - * 十六进制的值和非数字做比较时,会被当做二进制串 - * 有一个参数是 TIMESTAMP 或 DATETIME,并且另外一个参数是常量,常量会被转换为 timestamp - * 有一个参数是 decimal 类型,如果另外一个参数是 decimal 或者整数,会将整数转换为 decimal 后进行比较,如果另外一个参数是浮点数,则会把 decimal 转换为浮点数进行比较 - * 所有其他情况下,两个参数都会被转换为浮点数再进行比较 - */ - rule := HeuristicRules["OK"] - // 未开启测试环境不进行检查 - if common.Config.TestDSN.Disable { - return rule - } - - var content string - conditions := ast.FindAllCondition(idxAdv.Ast) - for _, cond := range conditions { - var colList []*common.Column - var values []*sqlparser.SQLVal - - // condition 左右两侧有且只有如下几种可能: - // 1. 列与列比较,如: col1 = col2 - // 2. 列与值比较,如: col = val - // 3. 值与值比较,如: val1 = val2 暂不处理 - // 如果列包含在一个函数中,认为这个条件为值,如: col = func(col) 认定为 列与值比较 - switch node := cond.(type) { - case *sqlparser.ComparisonExpr: - // 获取 condition 左侧的信息 - switch nLeft := node.Left.(type) { - case *sqlparser.SQLVal, sqlparser.ValTuple: - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch val := node.(type) { - case *sqlparser.SQLVal: - values = append(values, val) - } - return true, nil - }, nLeft) - common.LogIfError(err, "") - - case *sqlparser.ColName: - left := &common.Column{Name: nLeft.Name.String()} - if !nLeft.Qualifier.Name.IsEmpty() { - left.Table = nLeft.Qualifier.Name.String() - } - colList = append(colList, left) - } - - // 获取 condition 右侧的信息 - switch nRight := node.Right.(type) { - case *sqlparser.SQLVal, sqlparser.ValTuple: - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch val := node.(type) { - case *sqlparser.SQLVal: - values = append(values, val) - } - return true, nil - }, nRight) - common.LogIfError(err, "") - - case *sqlparser.ColName: - right := &common.Column{Name: nRight.Name.String()} - if !nRight.Qualifier.Name.IsEmpty() { - right.Table = nRight.Qualifier.Name.String() - } - colList = append(colList, right) - } - - if len(colList) == 0 { - continue - } - - // 补全列信息 - colList = CompleteColumnsInfo(idxAdv.Ast, colList, idxAdv.vEnv) - - // 列与列比较 - if len(colList) == 2 { - // 列信息补全后如果依然没有表信息,说明在该数据库中不存在该列 - // 如果列信息获取异常,可能会存在无法获取到数据类型的情况,对于这种情况将不会给予建议。 - needBreak := false - for _, col := range colList { - if col.Table == "" { - common.Log.Warning("Column %s not exists", col.Name) - needBreak = true - } - - if col.DataType == "" { - common.Log.Warning("Can't get column %s data type", col.Name) - needBreak = true - } - - } - - if needBreak { - break - } - - // 检查数据类型不一致导致的隐式数据转换 - type1 := common.GetDataTypeBase(colList[0].DataType) - type2 := common.GetDataTypeBase(colList[1].DataType) - common.Log.Debug("DataType: `%s`.`%s` (%s) VS `%s`.`%s` (%s)", - colList[0].Table, colList[0].Name, type1, - colList[1].Table, colList[1].Name, type2) - if strings.ToLower(type1) != strings.ToLower(type2) { - content += fmt.Sprintf("`%s`.`%s` (%s) VS `%s`.`%s` (%s) datatype not match", - colList[0].Table, colList[0].Name, type1, - colList[1].Table, colList[1].Name, type2) - continue - } - - // 检查字符集不一致导致的隐式数据转换 - common.Log.Debug("Charset: `%s`.`%s` (%s) VS `%s`.`%s` (%s)", - colList[0].Table, colList[0].Name, colList[0].Character, - colList[1].Table, colList[1].Name, colList[1].Character) - if colList[0].Character != colList[1].Character { - content += fmt.Sprintf("`%s`.`%s` (%s) VS `%s`.`%s` (%s) charset not match", - colList[0].Table, colList[0].Name, colList[0].Character, - colList[1].Table, colList[1].Name, colList[1].Character) - continue - } - - // 检查排序排序不一致导致的隐式数据转换 - common.Log.Debug("Collation: `%s`.`%s` (%s) VS `%s`.`%s` (%s)", - colList[0].Table, colList[0].Name, colList[0].Collation, - colList[1].Table, colList[1].Name, colList[1].Collation) - if colList[0].Collation != colList[1].Collation { - content += fmt.Sprintf("`%s`.`%s` (%s) VS `%s`.`%s` (%s) collation not match", - colList[0].Table, colList[0].Name, colList[0].Collation, - colList[1].Table, colList[1].Name, colList[1].Collation) - continue - } - } - - typMap := map[sqlparser.ValType][]string{ - // date, time, datetime, timestamp, year - sqlparser.StrVal: { - "char", "varchar", "tinytext", "text", "mediumtext", "longtext", - "date", "time", "datetime", "timestamp", "year", - }, - sqlparser.IntVal: { - "tinyint", "smallint", "mediumint", "int", "integer", "bigint", "timestamp", "year", - }, - sqlparser.FloatVal: { - "float", "double", "real", "decimal", - }, - } - - typNameMap := map[sqlparser.ValType]string{ - sqlparser.StrVal: "string", - sqlparser.IntVal: "int", - sqlparser.FloatVal: "float", - } - - // 列与值比较 - for _, val := range values { - if colList[0].DataType == "" { - common.Log.Debug("Can't get %s data type", colList[0].Name) - break - } - - isCovered := true - if tps, ok := typMap[val.Type]; ok { - for _, tp := range tps { - if strings.HasPrefix(colList[0].DataType, tp) { - isCovered = false - } - } - } - - if isCovered { - if colList[0].Table == "" { - common.Log.Warning("Column %s not exists", colList[0].Name) - continue - } - - c := fmt.Sprintf("%s.%s definition is %s not %s", - colList[0].Table, colList[0].Name, colList[0].DataType, typNameMap[val.Type]) - - common.Log.Debug("Implicit data type conversion: %s", c) - content += c - } - } - - case *sqlparser.RangeCond: - // TODO - case *sqlparser.IsExpr: - // TODO - } - } - if content != "" { - rule = HeuristicRules["ARG.003"] - rule.Content = content - } - return rule -} - -// RuleNoWhere CLA.001 & CLA.014 & CLA.015 -func (q *Query4Audit) RuleNoWhere() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.Select: - if n.Where == nil && sqlparser.String(n.From) != "dual" { - rule = HeuristicRules["CLA.001"] - return false, nil - } - case *sqlparser.Delete: - if n.Where == nil { - rule = HeuristicRules["CLA.014"] - return false, nil - } - case *sqlparser.Update: - if n.Where == nil { - rule = HeuristicRules["CLA.015"] - return false, nil - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleOrderByRand CLA.002 -func (q *Query4Audit) RuleOrderByRand() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case sqlparser.OrderBy: - for _, order := range n { - switch expr := order.Expr.(type) { - case *sqlparser.FuncExpr: - if expr.Name.String() == "rand" { - rule = HeuristicRules["CLA.002"] - return false, nil - } - } - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleOffsetLimit CLA.003 -func (q *Query4Audit) RuleOffsetLimit() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.Limit: - if n != nil && n.Offset != nil { - switch v := n.Offset.(type) { - case *sqlparser.SQLVal: - offset, err := strconv.Atoi(string(v.Val)) - // 检查一下Offset阈值,太小了给这个建议也没什么用,阈值写死了没加配置 - if err == nil && offset > 1000 { - rule = HeuristicRules["CLA.003"] - return false, nil - } - } - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleGroupByConst CLA.004 -func (q *Query4Audit) RuleGroupByConst() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case sqlparser.GroupBy: - for _, group := range n { - switch group.(type) { - case *sqlparser.SQLVal: - rule = HeuristicRules["CLA.004"] - return false, nil - } - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleGroupByConst GRP.001 -func (idxAdv *IndexAdvisor) RuleGroupByConst() Rule { - rule := HeuristicRules["OK"] - - // 非GroupBy语句 - if len(idxAdv.groupBy) == 0 || len(idxAdv.whereEQ) == 0 { - return rule - } - - for _, groupByCols := range idxAdv.groupBy { - for _, whereEQCols := range idxAdv.whereEQ { - if (groupByCols.Name == whereEQCols.Name) && - (groupByCols.DB == whereEQCols.DB) && - (groupByCols.Table == whereEQCols.Table) { - rule = HeuristicRules["GRP.001"] - break - } - } - } - return rule -} - -// RuleOrderByConst CLA.005 -func (q *Query4Audit) RuleOrderByConst() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case sqlparser.OrderBy: - for _, order := range n { - switch order.Expr.(type) { - case *sqlparser.SQLVal: - rule = HeuristicRules["CLA.005"] - return false, nil - } - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleOrderByConst CLA.005 -// TODO: SELECT col FROM tbl WHERE col IN('NEWS') ORDER BY col; -func (idxAdv *IndexAdvisor) RuleOrderByConst() Rule { - rule := HeuristicRules["OK"] - - // 非GroupBy语句 - if len(idxAdv.orderBy) == 0 || len(idxAdv.whereEQ) == 0 { - return rule - } - - for _, groupbyCols := range idxAdv.orderBy { - for _, whereEQCols := range idxAdv.whereEQ { - if (groupbyCols.Name == whereEQCols.Name) && - (groupbyCols.DB == whereEQCols.DB) && - (groupbyCols.Table == whereEQCols.Table) { - rule = HeuristicRules["CLA.005"] - break - } - } - } - return rule -} - -// RuleDiffGroupByOrderBy CLA.006 -func (q *Query4Audit) RuleDiffGroupByOrderBy() Rule { - var rule = q.RuleOK() - var groupbyTbls []sqlparser.TableIdent - var orderbyTbls []sqlparser.TableIdent - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case sqlparser.GroupBy: - // 检查group by涉及到表的个数 - for _, group := range n { - switch g := group.(type) { - case *sqlparser.ColName: - tblExist := false - for _, t := range groupbyTbls { - if t.String() == g.Qualifier.Name.String() { - tblExist = true - } - } - if !tblExist { - groupbyTbls = append(groupbyTbls, g.Qualifier.Name) - if len(groupbyTbls) > 1 { - rule = HeuristicRules["CLA.006"] - return false, nil - } - } - } - } - case sqlparser.OrderBy: - // 检查order by涉及到表的个数 - for _, order := range n { - switch o := order.Expr.(type) { - case *sqlparser.ColName: - tblExist := false - for _, t := range orderbyTbls { - if t.String() == o.Qualifier.Name.String() { - tblExist = true - } - } - if !tblExist { - orderbyTbls = append(orderbyTbls, o.Qualifier.Name) - if len(orderbyTbls) > 1 { - rule = HeuristicRules["CLA.006"] - return false, nil - } - } - } - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - - if rule.Item == "OK" { - // 检查group by, order by涉及到表的个数 - for _, g := range groupbyTbls { - tblExist := false - for _, o := range orderbyTbls { - if g.String() == o.String() { - tblExist = true - } - } - if !tblExist && len(orderbyTbls) > 0 { - rule = HeuristicRules["CLA.006"] - return rule - } - } - } - - return rule -} - -// RuleMixOrderBy CLA.007 -func (q *Query4Audit) RuleMixOrderBy() Rule { - var rule = q.RuleOK() - var direction string - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case sqlparser.OrderBy: - for _, order := range n { - // 比较相邻两个order by列的方向 - if direction != "" && order.Direction != direction { - rule = HeuristicRules["CLA.007"] - return false, nil - } - direction = order.Direction - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleExplicitOrderBy CLA.008 -func (q *Query4Audit) RuleExplicitOrderBy() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.Select: - // 有group by,但没有order by - if n.GroupBy != nil && n.OrderBy == nil { - rule = HeuristicRules["CLA.008"] - return false, nil - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleOrderByExpr CLA.009 -func (q *Query4Audit) RuleOrderByExpr() Rule { - var rule = q.RuleOK() - var orderByCols []string - var selectCols []string - funcExp := regexp.MustCompile(`(?i)[a-z0-9]\(`) - allowExp := regexp.MustCompile("(?i)[a-z0-9_,.` ()]") - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case sqlparser.OrderBy: - orderBy := sqlparser.String(n) - // 函数名方式,如:from_unixtime(col) - if funcExp.MatchString(orderBy) { - rule = HeuristicRules["CLA.009"] - return false, nil - } - - // 运算符方式,如:colA - colB - trim := allowExp.ReplaceAllFunc([]byte(orderBy), func(s []byte) []byte { - return []byte("") - }) - if string(trim) != "" { - rule = HeuristicRules["CLA.009"] - return false, nil - } - - for _, o := range strings.Split(strings.TrimPrefix(orderBy, " order by "), ",") { - orderByCols = append(orderByCols, strings.TrimSpace(strings.Split(o, " ")[0])) - } - case *sqlparser.Select: - for _, s := range n.SelectExprs { - selectCols = append(selectCols, sqlparser.String(s)) - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - - // AS情况,如:SELECT colA-colB a FROM tbl ORDER BY a; - for _, o := range orderByCols { - if o == "" { - continue - } - for _, s := range selectCols { - if strings.HasSuffix(s, " as "+o) { - buf := strings.TrimSuffix(s, " as "+o) - // 运算符 - trim := allowExp.ReplaceAllFunc([]byte(buf), func(s []byte) []byte { - return []byte("") - }) - if string(trim) != "" { - rule = HeuristicRules["CLA.009"] - } - // 函数 - if funcExp.MatchString(s) { - rule = HeuristicRules["CLA.009"] - } - } - } - } - return rule -} - -// RuleGroupByExpr CLA.010 -func (q *Query4Audit) RuleGroupByExpr() Rule { - var rule = q.RuleOK() - var groupByCols []string - var selectCols []string - funcExp := regexp.MustCompile(`(?i)[a-z0-9]\(`) - allowExp := regexp.MustCompile("(?i)[a-z0-9_,.` ()]") - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case sqlparser.GroupBy: - groupBy := sqlparser.String(n) - // 函数名方式,如:from_unixtime(col) - if funcExp.MatchString(groupBy) { - rule = HeuristicRules["CLA.010"] - return false, nil - } - - // 运算符方式,如:colA - colB - trim := allowExp.ReplaceAllFunc([]byte(groupBy), func(s []byte) []byte { - return []byte("") - }) - if string(trim) != "" { - rule = HeuristicRules["CLA.010"] - return false, nil - } - - for _, o := range strings.Split(strings.TrimPrefix(groupBy, " group by "), ",") { - groupByCols = append(groupByCols, strings.TrimSpace(strings.Split(o, " ")[0])) - } - case *sqlparser.Select: - for _, s := range n.SelectExprs { - selectCols = append(selectCols, sqlparser.String(s)) - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - - // AS情况,如:SELECT colA-colB a FROM tbl GROUP BY a; - for _, g := range groupByCols { - if g == "" { - continue - } - for _, s := range selectCols { - if strings.HasSuffix(s, " as "+g) { - buf := strings.TrimSuffix(s, " as "+g) - // 运算符 - trim := allowExp.ReplaceAllFunc([]byte(buf), func(s []byte) []byte { - return []byte("") - }) - if string(trim) != "" { - rule = HeuristicRules["CLA.010"] - } - // 函数 - if funcExp.MatchString(s) { - rule = HeuristicRules["CLA.010"] - } - } - } - } - return rule -} - -// RuleTblCommentCheck CLA.011 -func (q *Query4Audit) RuleTblCommentCheck() Rule { - var rule = q.RuleOK() - switch node := q.Stmt.(type) { - case *sqlparser.DDL: - if node.Action != "create" { - return rule - } - if node.TableSpec == nil { - return rule - } - if options := node.TableSpec.Options; options == "" { - rule = HeuristicRules["CLA.011"] - - } else { - reg := regexp.MustCompile("(?i)comment") - if !reg.MatchString(options) { - rule = HeuristicRules["CLA.011"] - } - } - } - return rule -} - -// RuleSelectStar COL.001 -func (q *Query4Audit) RuleSelectStar() Rule { - var rule = q.RuleOK() - // 先把count(*)替换为count(1) - re := regexp.MustCompile(`(?i)count\s*\(\s*\*\s*\)`) - sql := re.ReplaceAllString(q.Query, "count(1)") - stmt, err := sqlparser.Parse(sql) - if err != nil { - common.Log.Debug("RuleSelectStar sqlparser.Parse Error: %v", err) - return rule - } - err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node.(type) { - case *sqlparser.StarExpr: - rule = HeuristicRules["COL.001"] - return false, nil - } - return true, nil - }, stmt) - common.LogIfError(err, "") - return rule -} - -// RuleInsertColDef COL.002 -func (q *Query4Audit) RuleInsertColDef() Rule { - var rule = q.RuleOK() - switch node := q.Stmt.(type) { - case *sqlparser.Insert: - if node.Columns == nil { - rule = HeuristicRules["COL.002"] - return rule - } - } - return rule -} - -// RuleAddDefaultValue COL.004 -func (q *Query4Audit) RuleAddDefaultValue() Rule { - var rule = q.RuleOK() - for _, node := range q.TiStmt { - switch n := node.(type) { - case *tidb.CreateTableStmt: - for _, c := range n.Cols { - colDefault := false - for _, o := range c.Options { - // 忽略AutoIncrement类型的默认值检查 - if o.Tp == tidb.ColumnOptionDefaultValue || o.Tp == tidb.ColumnOptionAutoIncrement { - colDefault = true - } - } - if !colDefault { - rule = HeuristicRules["COL.004"] - break - } - } - case *tidb.AlterTableStmt: - for _, s := range n.Specs { - switch s.Tp { - case tidb.AlterTableAddColumns, tidb.AlterTableChangeColumn, tidb.AlterTableModifyColumn: - for _, c := range s.NewColumns { - colDefault := false - for _, o := range c.Options { - // 忽略AutoIncrement类型的默认值检查 - if o.Tp == tidb.ColumnOptionDefaultValue || o.Tp == tidb.ColumnOptionAutoIncrement { - colDefault = true - } - } - if !colDefault { - rule = HeuristicRules["COL.004"] - break - } - } - } - } - } - } - return rule -} - -// RuleColCommentCheck COL.005 -func (q *Query4Audit) RuleColCommentCheck() Rule { - var rule = q.RuleOK() - for _, node := range q.TiStmt { - switch n := node.(type) { - case *tidb.CreateTableStmt: - for _, c := range n.Cols { - colComment := false - for _, o := range c.Options { - if o.Tp == tidb.ColumnOptionComment { - colComment = true - } - } - if !colComment { - rule = HeuristicRules["COL.005"] - break - } - } - case *tidb.AlterTableStmt: - for _, s := range n.Specs { - switch s.Tp { - case tidb.AlterTableAddColumns, tidb.AlterTableChangeColumn, tidb.AlterTableModifyColumn: - for _, c := range s.NewColumns { - colComment := false - for _, o := range c.Options { - if o.Tp == tidb.ColumnOptionComment { - colComment = true - } - } - if !colComment { - rule = HeuristicRules["COL.005"] - break - } - } - } - } - } - } - return rule -} - -// RuleIPString LIT.001 -func (q *Query4Audit) RuleIPString() Rule { - var rule = q.RuleOK() - re := regexp.MustCompile(`['"]\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`) - if re.FindString(q.Query) != "" { - rule = HeuristicRules["LIT.001"] - if position := re.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - } - return rule -} - -// RuleDataNotQuote LIT.002 -func (q *Query4Audit) RuleDataNotQuote() Rule { - var rule = q.RuleOK() - // 2010-01-01 - re := regexp.MustCompile(`.\d{4}\s*-\s*\d{1,2}\s*-\s*\d{1,2}\b`) - sqls := re.FindAllString(q.Query, -1) - for _, sql := range sqls { - re = regexp.MustCompile(`^['"\w-].*`) - if re.FindString(sql) == "" { - rule = HeuristicRules["LIT.002"] - } - } - - // 10-01-01 - re = regexp.MustCompile(`.\d{2}\s*-\s*\d{1,2}\s*-\s*\d{1,2}\b`) - sqls = re.FindAllString(q.Query, -1) - for _, sql := range sqls { - re = regexp.MustCompile(`^['"\w-].*`) - if re.FindString(sql) == "" { - rule = HeuristicRules["LIT.002"] - } - } - - if position := re.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - return rule -} - -// RuleSQLCalcFoundRows KWR.001 -func (q *Query4Audit) RuleSQLCalcFoundRows() Rule { - var rule = q.RuleOK() - tkns := ast.Tokenizer(q.Query) - for _, tkn := range tkns { - if tkn.Val == "sql_calc_found_rows" { - rule = HeuristicRules["KWR.001"] - break - } - } - return rule -} - -// RuleCommaAnsiJoin JOI.001 -func (q *Query4Audit) RuleCommaAnsiJoin() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.Select: - ansiJoin := false - commaJoin := false - for _, f := range n.From { - switch f.(type) { - case *sqlparser.JoinTableExpr: - ansiJoin = true - case *sqlparser.AliasedTableExpr: - commaJoin = true - } - } - if ansiJoin && commaJoin { - rule = HeuristicRules["JOI.001"] - return false, nil - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleDupJoin JOI.002 -func (q *Query4Audit) RuleDupJoin() Rule { - var rule = q.RuleOK() - var tables []string - switch q.Stmt.(type) { - // TODO: 这里未检查UNION SELECT - case *sqlparser.Union: - return rule - default: - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.AliasedTableExpr: - switch table := n.Expr.(type) { - case sqlparser.TableName: - for _, t := range tables { - if t == table.Name.String() { - rule = HeuristicRules["JOI.002"] - return false, nil - } - } - tables = append(tables, table.Name.String()) - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - } - return rule -} - -// RuleImpossibleOuterJoin JOI.003 -// TODO: 未实现完 -func (idxAdv *IndexAdvisor) RuleImpossibleOuterJoin() Rule { - rule := HeuristicRules["OK"] - - var joinTables []string // JOIN相关表名 - var whereEQTables []string // WHERE等值判断条件表名 - var joinNotWhereTables []string // 是JOIN相关表,但未出现在WHERE等值判断条件中的表名 - - // 非JOIN语句 - if len(idxAdv.joinCond) == 0 || len(idxAdv.whereEQ) == 0 { - return rule - } - - for _, l1 := range idxAdv.joinCond { - for _, l2 := range l1 { - if l2.Table != "" && l2.Table != "dual" { - joinTables = append(joinTables, l2.Table) - } - } - } - - for _, w := range idxAdv.whereEQ { - whereEQTables = append(whereEQTables, w.Table) - } - - for _, j := range joinTables { - found := false - for _, w := range whereEQTables { - if j == w { - found = true - } - } - if !found { - joinNotWhereTables = append(joinNotWhereTables, j) - } - } - - // TODO: - fmt.Println(joinNotWhereTables) - /* - if len(joinNotWhereTables) == 0 { - rule = HeuristicRules["JOI.003"] - } - */ - rule = HeuristicRules["JOI.003"] - return rule -} - -// TODO: JOI.004 - -// RuleNoDeterministicGroupby RES.001 -func (q *Query4Audit) RuleNoDeterministicGroupby() Rule { - var rule = q.RuleOK() - var groupbyCols []*common.Column - var selectCols []*common.Column - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.Select: - // 过滤select列 - selectCols = ast.FindColumn(n.SelectExprs) - // 过滤group by列 - groupbyCols = ast.FindColumn(n.GroupBy) - // `select *`, but not `select count(*)` - if strings.Contains(sqlparser.String(n), " * ") && len(groupbyCols) > 0 { - rule = HeuristicRules["RES.001"] - return false, nil - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - - // TODO:暂时只检查了列名,未对库表名进行检查,也未处理AS - for _, s := range selectCols { - // 无group by退出 - if len(groupbyCols) == 0 { - break - } - found := false - for _, g := range groupbyCols { - if g.Name == s.Name { - found = true - } - } - if !found { - rule = HeuristicRules["RES.001"] - break - } - } - return rule -} - -// RuleNoDeterministicLimit RES.002 -func (q *Query4Audit) RuleNoDeterministicLimit() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.Select: - if n.Limit != nil && n.OrderBy == nil { - rule = HeuristicRules["RES.002"] - return false, nil - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleUpdateDeleteWithLimit RES.003 -func (q *Query4Audit) RuleUpdateDeleteWithLimit() Rule { - var rule = q.RuleOK() - switch s := q.Stmt.(type) { - case *sqlparser.Update: - if s.Limit != nil { - rule = HeuristicRules["RES.003"] - } - } - return rule -} - -// RuleUpdateDeleteWithOrderby RES.004 -func (q *Query4Audit) RuleUpdateDeleteWithOrderby() Rule { - var rule = q.RuleOK() - switch s := q.Stmt.(type) { - case *sqlparser.Update: - if s.OrderBy != nil { - rule = HeuristicRules["RES.004"] - } - } - return rule -} - -// RuleUpdateSetAnd RES.005 -func (q *Query4Audit) RuleUpdateSetAnd() Rule { - var rule = q.RuleOK() - switch s := q.Stmt.(type) { - case *sqlparser.Update: - if strings.Contains(sqlparser.String(s.Exprs), " and ") { - rule = HeuristicRules["RES.005"] - } - } - return rule -} - -// RuleImpossibleWhere RES.006 -func (q *Query4Audit) RuleImpossibleWhere() Rule { - var rule = q.RuleOK() - // BETWEEN 10 AND 5 - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.RangeCond: - if n.Operator == "between" { - from := 0 - to := 0 - switch s := n.From.(type) { - case *sqlparser.SQLVal: - from, _ = strconv.Atoi(string(s.Val)) - } - switch s := n.To.(type) { - case *sqlparser.SQLVal: - to, _ = strconv.Atoi(string(s.Val)) - } - if from > to { - rule = HeuristicRules["RES.006"] - return false, nil - } - } - case *sqlparser.ComparisonExpr: - factor := false - switch n.Operator { - case "!=", "<>": - case "=", "<=>": - factor = true - default: - return true, nil - } - - var left []byte - var right []byte - - // left - switch l := n.Left.(type) { - case *sqlparser.SQLVal: - left = l.Val - default: - return true, nil - } - - // right - switch r := n.Right.(type) { - case *sqlparser.SQLVal: - right = r.Val - default: - return true, nil - } - - // compare - if (!bytes.Equal(left, right) && factor) || (bytes.Equal(left, right) && !factor) { - rule = HeuristicRules["RES.006"] - } - return false, nil - } - - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleMeaninglessWhere RES.007 -func (q *Query4Audit) RuleMeaninglessWhere() Rule { - var rule = q.RuleOK() - // SELECT * FROM tb WHERE 1 - switch n := q.Stmt.(type) { - case *sqlparser.Select: - if n.Where != nil { - switch n.Where.Expr.(type) { - case *sqlparser.SQLVal: - rule = HeuristicRules["RES.007"] - return rule - } - } - } - // 1=1, 0=0 - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.ComparisonExpr: - factor := false - switch n.Operator { - case "!=", "<>": - factor = true - case "=", "<=>": - default: - return true, nil - } - - var left []byte - var right []byte - - // left - switch l := n.Left.(type) { - case *sqlparser.SQLVal: - left = l.Val - default: - return true, nil - } - - // right - switch r := n.Right.(type) { - case *sqlparser.SQLVal: - right = r.Val - default: - return true, nil - } - - // compare - if (bytes.Equal(left, right) && !factor) || (!bytes.Equal(left, right) && factor) { - rule = HeuristicRules["RES.007"] - } - return false, nil - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleLoadFile RES.008 -func (q *Query4Audit) RuleLoadFile() Rule { - var rule = q.RuleOK() - // 去除注释 - sql := database.RemoveSQLComments(q.Query) - // 去除多余的空格和回车 - sql = strings.Join(strings.Fields(sql), " ") - tks := ast.Tokenize(sql) - for i, tk := range tks { - // 注意:每个关键字token的结尾是带空格的,这里偷懒没trimspace直接加空格比较 - // LOAD DATA... - if strings.ToLower(tk.Val) == "load " && i+1 < len(tks) && - strings.ToLower(tks[i+1].Val) == "data " { - rule = HeuristicRules["RES.008"] - break - } - - // SELECT ... INTO OUTFILE - if strings.ToLower(tk.Val) == "into " && i+1 < len(tks) && - (strings.ToLower(tks[i+1].Val) == "outfile " || strings.ToLower(tks[i+1].Val) == "dumpfile ") { - rule = HeuristicRules["RES.008"] - break - } - } - return rule -} - -// RuleStandardINEQ STA.001 -func (q *Query4Audit) RuleStandardINEQ() Rule { - var rule = q.RuleOK() - re := regexp.MustCompile(`(!=)`) - if re.FindString(q.Query) != "" { - rule = HeuristicRules["STA.001"] - if position := re.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - } - return rule -} - -// RuleUseKeyWord KWR.002 -func (q *Query4Audit) RuleUseKeyWord() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - if q.TiStmt == nil { - common.Log.Error("TiStmt is nil, SQL: %s", q.Query) - return rule - } - - for _, tiStmtNode := range q.TiStmt { - switch stmt := tiStmtNode.(type) { - case *tidb.AlterTableStmt: - // alter - for _, spec := range stmt.Specs { - for _, column := range spec.NewColumns { - if ast.IsMysqlKeyword(column.Name.String()) { - return HeuristicRules["KWR.002"] - } - } - } - - case *tidb.CreateTableStmt: - // create - if ast.IsMysqlKeyword(stmt.Table.Name.String()) { - return HeuristicRules["KWR.002"] - } - - for _, col := range stmt.Cols { - if ast.IsMysqlKeyword(col.Name.String()) { - return HeuristicRules["KWR.002"] - } - } - } - - } - } - - return rule -} - -// RulePluralWord KWR.003 -// Reference: https://en.wikipedia.org/wiki/English_plurals -func (q *Query4Audit) RulePluralWord() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - if q.TiStmt == nil { - common.Log.Error("TiStmt is nil, SQL: %s", q.Query) - return rule - } - - for _, tiStmtNode := range q.TiStmt { - switch stmt := tiStmtNode.(type) { - case *tidb.AlterTableStmt: - // alter - for _, spec := range stmt.Specs { - for _, column := range spec.NewColumns { - if inflector.Singularize(column.Name.String()) != column.Name.String() { - return HeuristicRules["KWR.003"] - } - } - } - - case *tidb.CreateTableStmt: - // create - if inflector.Singularize(stmt.Table.Name.String()) != stmt.Table.Name.String() { - return HeuristicRules["KWR.003"] - } - - for _, col := range stmt.Cols { - if inflector.Singularize(col.Name.String()) != col.Name.String() { - return HeuristicRules["KWR.003"] - } - } - } - - } - - } - return rule -} - -// RuleMultiBytesWord KWR.004 -func (q *Query4Audit) RuleMultiBytesWord() Rule { - // TODO: 目前使用 utf8 字符集检查,其他字符集输入可能会有问题 - var rule = q.RuleOK() - for _, tk := range ast.Tokenize(q.Query) { - switch tk.Type { - case ast.TokenTypeBacktickQuote, ast.TokenTypeWord: - if utf8.RuneCountInString(tk.Val) != len(tk.Val) { - rule = HeuristicRules["KWR.004"] - } - default: - } - } - return rule -} - -// RuleInsertSelect LCK.001 -func (q *Query4Audit) RuleInsertSelect() Rule { - var rule = q.RuleOK() - switch n := q.Stmt.(type) { - case *sqlparser.Insert: - switch n.Rows.(type) { - case *sqlparser.Select: - rule = HeuristicRules["LCK.001"] - } - } - return rule -} - -// RuleInsertOnDup LCK.002 -func (q *Query4Audit) RuleInsertOnDup() Rule { - var rule = q.RuleOK() - switch n := q.Stmt.(type) { - case *sqlparser.Insert: - if n.OnDup != nil { - rule = HeuristicRules["LCK.002"] - return rule - } - } - return rule -} - -// RuleInSubquery SUB.001 -func (q *Query4Audit) RuleInSubquery() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node.(type) { - case *sqlparser.Subquery: - rule = HeuristicRules["SUB.001"] - return false, nil - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleSubqueryDepth SUB.004 -func (q *Query4Audit) RuleSubqueryDepth() Rule { - var rule = q.RuleOK() - if depth := ast.GetSubqueryDepth(q.Stmt); depth > common.Config.MaxSubqueryDepth { - rule = HeuristicRules["SUB.004"] - } - return rule -} - -// RuleSubQueryLimit SUB.005 -// 只有 IN 的 SUBQUERY 限制了 LIMIT, FROM 子句中的 SUBQUERY 并未限制 LIMIT -func (q *Query4Audit) RuleSubQueryLimit() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.ComparisonExpr: - if n.Operator == "in" { - switch r := n.Right.(type) { - case *sqlparser.Subquery: - switch s := r.Select.(type) { - case *sqlparser.Select: - if s.Limit != nil { - rule = HeuristicRules["SUB.005"] - return false, nil - } - } - } - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleSubQueryFunctions SUB.006 -func (q *Query4Audit) RuleSubQueryFunctions() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node.(type) { - case *sqlparser.Subquery: - err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node.(type) { - case *sqlparser.FuncExpr: - rule = HeuristicRules["SUB.006"] - return false, nil - } - return true, nil - }, node) - common.LogIfError(err, "") - } - - if rule.Item == "OK" { - return true, nil - } - return false, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleMultiValueAttribute LIT.003 -func (q *Query4Audit) RuleMultiValueAttribute() Rule { - var rule = q.RuleOK() - re := regexp.MustCompile(`(?i)(id\s+varchar)|(id\s+text)|(id\s+regexp)`) - if re.FindString(q.Query) != "" { - rule = HeuristicRules["LIT.003"] - if position := re.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - } - return rule -} - -// RuleAddDelimiter LIT.004 -func (q *Query4Audit) RuleAddDelimiter() Rule { - var rule = q.RuleOK() - re := regexp.MustCompile(`(?i)(^use\s+[0-9a-z_-]*)|(^show\s+databases)`) - if re.FindString(q.Query) != "" && !strings.HasSuffix(q.Query, common.Config.Delimiter) { - rule = HeuristicRules["LIT.004"] - if position := re.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - } - return rule -} - -// RuleRecursiveDependency KEY.003 -func (q *Query4Audit) RuleRecursiveDependency() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - // create statement - for _, ref := range node.Constraints { - if ref != nil && ref.Tp == tidb.ConstraintForeignKey { - rule = HeuristicRules["KEY.003"] - } - } - - case *tidb.AlterTableStmt: - // alter table statement - for _, spec := range node.Specs { - if spec.Constraint != nil && spec.Constraint.Tp == tidb.ConstraintForeignKey { - rule = HeuristicRules["KEY.003"] - } - } - } - } - } - - if rule.Item == "KEY.003" { - re := regexp.MustCompile(`(?i)(\s+references\s+)`) - if position := re.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - } - - return rule -} - -// RuleImpreciseDataType COL.009 -func (q *Query4Audit) RuleImpreciseDataType() Rule { - var rule = q.RuleOK() - if q.TiStmt != nil { - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - // Create table statement - for _, col := range node.Cols { - switch col.Tp.Tp { - case mysql.TypeFloat, mysql.TypeDouble, mysql.TypeDecimal, mysql.TypeNewDecimal: - rule = HeuristicRules["COL.009"] - } - } - - case *tidb.AlterTableStmt: - // Alter table statement - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableAddColumns, tidb.AlterTableChangeColumn, tidb.AlterTableModifyColumn: - for _, col := range spec.NewColumns { - switch col.Tp.Tp { - case mysql.TypeFloat, mysql.TypeDouble, - mysql.TypeDecimal, mysql.TypeNewDecimal: - rule = HeuristicRules["COL.009"] - } - } - } - } - - case *tidb.InsertStmt: - // Insert statement - for _, values := range node.Lists { - for _, value := range values { - switch value.GetType().Tp { - case mysql.TypeNewDecimal, mysql.TypeFloat: - rule = HeuristicRules["COL.009"] - } - } - } - - case *tidb.SelectStmt: - // Select statement - switch where := node.Where.(type) { - case *tidb.BinaryOperationExpr: - switch where.R.GetType().Tp { - case mysql.TypeNewDecimal, mysql.TypeFloat: - rule = HeuristicRules["COL.009"] - } - } - } - } - } - - return rule -} - -// RuleValuesInDefinition COL.010 -func (q *Query4Audit) RuleValuesInDefinition() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, col := range node.Cols { - switch col.Tp.Tp { - case mysql.TypeSet, mysql.TypeEnum, mysql.TypeBit: - rule = HeuristicRules["COL.010"] - } - } - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableAddColumns, tidb.AlterTableChangeColumn, tidb.AlterTableModifyColumn: - for _, col := range spec.NewColumns { - switch col.Tp.Tp { - case mysql.TypeSet, mysql.TypeEnum, mysql.TypeBit: - rule = HeuristicRules["COL.010"] - } - } - } - } - } - } - } - return rule -} - -// RuleIndexAttributeOrder KEY.004 -func (q *Query4Audit) RuleIndexAttributeOrder() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateIndexStmt: - if len(node.IndexColNames) > 1 { - rule = HeuristicRules["KEY.004"] - break - } - case *tidb.CreateTableStmt: - for _, constraint := range node.Constraints { - // 当一条索引中包含多个列的时候给予建议 - if len(constraint.Keys) > 1 { - rule = HeuristicRules["KEY.004"] - break - } - } - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - if spec.Tp == tidb.AlterTableAddConstraint && len(spec.Constraint.Keys) > 1 { - rule = HeuristicRules["KEY.004"] - break - } - } - } - } - } - return rule -} - -// RuleNullUsage COL.011 -func (q *Query4Audit) RuleNullUsage() Rule { - var rule = q.RuleOK() - re := regexp.MustCompile(`(?i)(\s+null\s+)`) - if re.FindString(q.Query) != "" { - rule = HeuristicRules["COL.011"] - if position := re.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - } - return rule -} - -// RuleStringConcatenation FUN.003 -func (q *Query4Audit) RuleStringConcatenation() Rule { - var rule = q.RuleOK() - re := regexp.MustCompile(`(?i)(\|\|)`) - if re.FindString(q.Query) != "" { - rule = HeuristicRules["FUN.003"] - if position := re.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - } - return rule -} - -// RuleSysdate FUN.004 -func (q *Query4Audit) RuleSysdate() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.FuncExpr: - if n.Name.String() == "sysdate" { - rule = HeuristicRules["FUN.004"] - return false, nil - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleCountConst FUN.005 -func (q *Query4Audit) RuleCountConst() Rule { - var rule = q.RuleOK() - fingerprint := query.Fingerprint(q.Query) - countReg := regexp.MustCompile(`(?i)count\(\s*[0-9a-z?]*\s*\)`) - if countReg.MatchString(fingerprint) { - rule = HeuristicRules["FUN.005"] - if position := countReg.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - } - return rule -} - -// RuleSumNPE FUN.006 -func (q *Query4Audit) RuleSumNPE() Rule { - var rule = q.RuleOK() - fingerprint := query.Fingerprint(q.Query) - sumReg := regexp.MustCompile(`(?i)sum\(\s*[0-9a-z?]*\s*\)`) - isnullReg := regexp.MustCompile(`(?i)isnull\(sum\(\s*[0-9a-z?]*\s*\)\)`) - if sumReg.MatchString(fingerprint) && !isnullReg.MatchString(fingerprint) { - rule = HeuristicRules["FUN.006"] - if position := isnullReg.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - } - return rule -} - -// RuleForbiddenTrigger FUN.007 -func (q *Query4Audit) RuleForbiddenTrigger() Rule { - var rule = q.RuleOK() - - // 由于vitess对某些语法的支持不完善,使得如创建临时表等语句无法通过语法检查 - // 所以这里使用正则对触发器、临时表、存储过程等进行匹配 - // 但是目前支持的也不是非常全面,有待完善匹配规则 - // TODO TiDB 目前还不支持触发器、存储过程、自定义函数、外键 - - forbidden := []*regexp.Regexp{ - regexp.MustCompile(`(?i)CREATE\s+TRIGGER\s+`), - } - - for _, reg := range forbidden { - if reg.MatchString(q.Query) { - rule = HeuristicRules["FUN.007"] - if position := reg.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - break - } - } - return rule -} - -// RuleForbiddenProcedure FUN.008 -func (q *Query4Audit) RuleForbiddenProcedure() Rule { - var rule = q.RuleOK() - - // 由于vitess对某些语法的支持不完善,使得如创建临时表等语句无法通过语法检查 - // 所以这里使用正则对触发器、临时表、存储过程等进行匹配 - // 但是目前支持的也不是非常全面,有待完善匹配规则 - // TODO TiDB 目前还不支持触发器、存储过程、自定义函数、外键 - - forbidden := []*regexp.Regexp{ - regexp.MustCompile(`(?i)CREATE\s+PROCEDURE\s+`), - } - - for _, reg := range forbidden { - if reg.MatchString(q.Query) { - rule = HeuristicRules["FUN.008"] - if position := reg.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - break - } - } - return rule -} - -// RuleForbiddenFunction FUN.009 -func (q *Query4Audit) RuleForbiddenFunction() Rule { - var rule = q.RuleOK() - - // 由于vitess对某些语法的支持不完善,使得如创建临时表等语句无法通过语法检查 - // 所以这里使用正则对触发器、临时表、存储过程等进行匹配 - // 但是目前支持的也不是非常全面,有待完善匹配规则 - // TODO TiDB 目前还不支持触发器、存储过程、自定义函数、外键 - - forbidden := []*regexp.Regexp{ - regexp.MustCompile(`(?i)CREATE\s+FUNCTION\s+`), - } - - for _, reg := range forbidden { - if reg.MatchString(q.Query) { - rule = HeuristicRules["FUN.009"] - if position := reg.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - break - } - } - return rule -} - -// RulePatternMatchingUsage ARG.007 -func (q *Query4Audit) RulePatternMatchingUsage() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.Select: - re := regexp.MustCompile(`(?i)(\bregexp\b)|(\bsimilar to\b)`) - if re.FindString(q.Query) != "" { - rule = HeuristicRules["ARG.007"] - } - } - return rule -} - -// RuleSpaghettiQueryAlert CLA.012 -func (q *Query4Audit) RuleSpaghettiQueryAlert() Rule { - var rule = q.RuleOK() - if len(query.Fingerprint(q.Query)) > common.Config.SpaghettiQueryLength { - rule = HeuristicRules["CLA.012"] - } - return rule -} - -// RuleReduceNumberOfJoin JOI.005 -func (q *Query4Audit) RuleReduceNumberOfJoin() Rule { - var rule = q.RuleOK() - var tables []string - switch q.Stmt.(type) { - // TODO: UNION有可能有多张表,这里未检查UNION SELECT - case *sqlparser.Union: - return rule - default: - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.AliasedTableExpr: - switch table := n.Expr.(type) { - case sqlparser.TableName: - exist := false - for _, t := range tables { - if t == table.Name.String() { - exist = true - break - } - } - if !exist { - tables = append(tables, table.Name.String()) - } - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - } - if len(tables) > common.Config.MaxJoinTableCount { - rule = HeuristicRules["JOI.005"] - } - return rule -} - -// RuleDistinctUsage DIS.001 -func (q *Query4Audit) RuleDistinctUsage() Rule { - // Distinct - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.Select: - re := regexp.MustCompile(`(?i)(\bdistinct\b)`) - if len(re.FindAllString(q.Query, -1)) > common.Config.MaxDistinctCount { - rule = HeuristicRules["DIS.001"] - } - } - return rule -} - -// RuleCountDistinctMultiCol DIS.002 -func (q *Query4Audit) RuleCountDistinctMultiCol() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.FuncExpr: - str := strings.ToLower(sqlparser.String(n)) - if strings.HasPrefix(str, "count") && strings.Contains(str, ",") { - rule = HeuristicRules["DIS.002"] - return false, nil - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleDistinctStar DIS.003 -func (q *Query4Audit) RuleDistinctStar() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.Select: - meta := ast.GetMeta(q.Stmt, nil) - for _, m := range meta { - if len(m.Table) == 1 { - // distinct tbl.* from tbl和 distinct * - re := regexp.MustCompile(`(?i)((\s+distinct\s*\*)|(\s+distinct\s+[0-9a-z_` + "`" + `]*\.\*))`) - if re.MatchString(q.Query) { - rule = HeuristicRules["DIS.003"] - } - } - break - } - } - return rule -} - -// RuleHavingClause CLA.013 -func (q *Query4Audit) RuleHavingClause() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case *sqlparser.Select: - if expr.Having != nil { - rule = HeuristicRules["CLA.013"] - return false, nil - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleUpdatePrimaryKey CLA.016 -func (idxAdv *IndexAdvisor) RuleUpdatePrimaryKey() Rule { - rule := HeuristicRules["OK"] - switch node := idxAdv.Ast.(type) { - case *sqlparser.Update: - var setColumns []*common.Column - - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node.(type) { - case *sqlparser.UpdateExpr: - // 获取 set 操作的全部 column - setColumns = append(setColumns, ast.FindAllCols(node)...) - } - return true, nil - }, node) - common.LogIfError(err, "") - setColumns = idxAdv.calcCardinality(CompleteColumnsInfo(idxAdv.Ast, setColumns, idxAdv.vEnv)) - for _, col := range setColumns { - idxMeta := idxAdv.IndexMeta[idxAdv.vEnv.DBHash(col.DB)][col.Table] - if idxMeta == nil { - return rule - } - for _, idx := range idxMeta.IdxRows { - if idx.KeyName == "PRIMARY" { - if col.Name == idx.ColumnName { - rule = HeuristicRules["CLA.016"] - return rule - } - continue - } - } - } - } - - return rule -} - -// RuleNestedSubQueries JOI.006 -func (q *Query4Audit) RuleNestedSubQueries() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node.(type) { - case *sqlparser.Subquery: - rule = HeuristicRules["JOI.006"] - return false, nil - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleMultiDeleteUpdate JOI.007 -func (q *Query4Audit) RuleMultiDeleteUpdate() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.Delete, *sqlparser.Update: - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node.(type) { - case *sqlparser.JoinTableExpr: - rule = HeuristicRules["JOI.007"] - return false, nil - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - } - return rule -} - -// RuleMultiDBJoin JOI.008 -func (q *Query4Audit) RuleMultiDBJoin() Rule { - var rule = q.RuleOK() - meta := ast.GetMeta(q.Stmt, nil) - dbCount := 0 - for range meta { - dbCount++ - } - - if dbCount > 1 { - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node.(type) { - case *sqlparser.JoinTableExpr: - rule = HeuristicRules["JOI.008"] - return false, nil - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - } - return rule -} - -// RuleORUsage ARG.008 -func (q *Query4Audit) RuleORUsage() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.Select: - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node.(type) { - case *sqlparser.OrExpr: - rule = HeuristicRules["ARG.008"] - return false, nil - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - } - return rule -} - -// RuleSpaceWithQuote ARG.009 -func (q *Query4Audit) RuleSpaceWithQuote() Rule { - var rule = q.RuleOK() - for _, tk := range ast.Tokenize(q.Query) { - if tk.Type == ast.TokenTypeQuote { - // 序列化的Val是带引号,所以要取第2个最倒数第二个,这样也就不用担心len<2了。 - switch tk.Val[1] { - case ' ': - rule = HeuristicRules["ARG.009"] - } - switch tk.Val[len(tk.Val)-2] { - case ' ': - rule = HeuristicRules["ARG.009"] - } - } - } - return rule -} - -// RuleHint ARG.010 -// TODO: sql_no_cache, straight join -func (q *Query4Audit) RuleHint() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.IndexHints: - if n != nil { - rule = HeuristicRules["ARG.010"] - } - return false, nil - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleNot ARG.011 -func (q *Query4Audit) RuleNot() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.ComparisonExpr: - if strings.HasPrefix(n.Operator, "not") { - rule = HeuristicRules["ARG.011"] - return false, nil - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleInsertValues ARG.012 -func (q *Query4Audit) RuleInsertValues() Rule { - var rule = q.RuleOK() - switch s := q.Stmt.(type) { - case *sqlparser.Insert: - switch val := s.Rows.(type) { - case sqlparser.Values: - if len(val) > common.Config.MaxValueCount { - rule = HeuristicRules["ARG.012"] - } - } - } - return rule -} - -// RuleUNIONUsage SUB.002 -func (q *Query4Audit) RuleUNIONUsage() Rule { - var rule = q.RuleOK() - switch s := q.Stmt.(type) { - case *sqlparser.Union: - if s.Type == "union" { - rule = HeuristicRules["SUB.002"] - } - } - return rule -} - -// RuleDistinctJoinUsage SUB.003 -func (q *Query4Audit) RuleDistinctJoinUsage() Rule { - var rule = q.RuleOK() - switch expr := q.Stmt.(type) { - case *sqlparser.Select: - if expr.Distinct != "" { - if expr.From != nil { - if len(expr.From) > 1 { - rule = HeuristicRules["SUB.003"] - } - } - } - } - return rule -} - -// RuleReadablePasswords SEC.002 -func (q *Query4Audit) RuleReadablePasswords() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - re := regexp.MustCompile(`(?i)(password)|(password)|(pwd)`) - for _, tiStmt := range q.TiStmt { - // create table stmt - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, col := range node.Cols { - switch col.Tp.Tp { - case mysql.TypeString, mysql.TypeVarchar, mysql.TypeVarString, - mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob: - if re.FindString(q.Query) != "" { - return HeuristicRules["SEC.002"] - } - } - } - - case *tidb.AlterTableStmt: - // alter table stmt - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableModifyColumn, tidb.AlterTableChangeColumn, tidb.AlterTableAddColumns: - for _, col := range spec.NewColumns { - switch col.Tp.Tp { - case mysql.TypeString, mysql.TypeVarchar, mysql.TypeVarString, - mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob: - if re.FindString(q.Query) != "" { - return HeuristicRules["SEC.002"] - } - } - } - } - } - } - } - } - return rule -} - -// RuleDataDrop SEC.003 -func (q *Query4Audit) RuleDataDrop() Rule { - var rule = q.RuleOK() - switch s := q.Stmt.(type) { - case *sqlparser.DBDDL: - if s.Action == "drop" { - rule = HeuristicRules["SEC.003"] - } - case *sqlparser.DDL: - if s.Action == "drop" || s.Action == "truncate" { - rule = HeuristicRules["SEC.003"] - } - case *sqlparser.Delete: - rule = HeuristicRules["SEC.003"] - } - return rule -} - -// RuleCompareWithFunction FUN.001 -func (q *Query4Audit) RuleCompareWithFunction() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - // Vitess 中有些函数进行了单独定义不在 FuncExpr 中,如: substring。所以不能直接用 FuncExpr 判断。 - switch n := node.(type) { - case *sqlparser.ComparisonExpr: - switch n.Left.(type) { - case *sqlparser.SQLVal, *sqlparser.ColName: - default: - rule = HeuristicRules["FUN.001"] - return false, nil - } - /* - // func always has bracket - if strings.HasSuffix(sqlparser.String(n.Left), ")") { - rule = HeuristicRules["FUN.001"] - return false, nil - } - */ - - case *sqlparser.RangeCond: - // func(a) between func(c) and func(d) - switch n.Left.(type) { - case *sqlparser.SQLVal, *sqlparser.ColName: - default: - rule = HeuristicRules["FUN.001"] - return false, nil - } - switch n.From.(type) { - case *sqlparser.SQLVal, *sqlparser.ColName: - default: - rule = HeuristicRules["FUN.001"] - return false, nil - } - switch n.To.(type) { - case *sqlparser.SQLVal, *sqlparser.ColName: - default: - rule = HeuristicRules["FUN.001"] - return false, nil - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleCountStar FUN.002 -func (q *Query4Audit) RuleCountStar() Rule { - var rule = q.RuleOK() - switch n := q.Stmt.(type) { - case *sqlparser.Select: - // count(N), count(col), count(*) - re := regexp.MustCompile(`(?i)(count\(\s*[*0-9a-z_` + "`" + `]*\s*\))`) - if re.FindString(q.Query) != "" && n.Where != nil { - rule = HeuristicRules["FUN.002"] - } - } - return rule -} - -// RuleTruncateTable SEC.001 -func (q *Query4Audit) RuleTruncateTable() Rule { - var rule = q.RuleOK() - switch s := q.Stmt.(type) { - case *sqlparser.DDL: - if s.Action == "truncate" { - rule = HeuristicRules["SEC.001"] - } - } - return rule -} - -// RuleIn ARG.005 && ARG.004 -func (q *Query4Audit) RuleIn() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case *sqlparser.ComparisonExpr: - switch n.Operator { - case "in": - switch r := n.Right.(type) { - case sqlparser.ValTuple: - // IN (NULL) - for _, v := range r { - switch v.(type) { - case *sqlparser.NullVal: - rule = HeuristicRules["ARG.004"] - return false, nil - } - } - if len(r) > common.Config.MaxInCount { - rule = HeuristicRules["ARG.005"] - return false, nil - } - } - case "not in": - switch r := n.Right.(type) { - case sqlparser.ValTuple: - // NOT IN (NULL) - for _, v := range r { - switch v.(type) { - case *sqlparser.NullVal: - rule = HeuristicRules["ARG.004"] - return false, nil - } - } - } - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleIsNullIsNotNull ARG.006 -func (q *Query4Audit) RuleIsNullIsNotNull() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.Select: - re := regexp.MustCompile(`(?i)is\s*(not)?\s+null\b`) - if re.FindString(q.Query) != "" { - rule = HeuristicRules["ARG.006"] - } - } - return rule -} - -// RuleVarcharVSChar COL.008 -func (q *Query4Audit) RuleVarcharVSChar() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, col := range node.Cols { - switch col.Tp.Tp { - // 在 TiDB 的 AST 中,char 和 binary 的 type 都是 mysql.TypeString - // 只是 binary 数据类型的 character 和 collate 是 binary - case mysql.TypeString: - rule = HeuristicRules["COL.008"] - } - } - - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableAddColumns, tidb.AlterTableChangeColumn, tidb.AlterTableModifyColumn: - for _, col := range spec.NewColumns { - switch col.Tp.Tp { - case mysql.TypeString: - rule = HeuristicRules["COL.008"] - } - } - } - } - } - } - } - return rule -} - -// RuleCreateDualTable TBL.003 -func (q *Query4Audit) RuleCreateDualTable() Rule { - var rule = q.RuleOK() - switch s := q.Stmt.(type) { - case *sqlparser.DDL: - if s.Table.Name.String() == "dual" { - rule = HeuristicRules["TBL.003"] - - } - } - return rule -} - -// RuleAlterCharset ALT.001 -func (q *Query4Audit) RuleAlterCharset() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableOption: - for _, option := range spec.Options { - if option.Tp == tidb.TableOptionCharset || - option.Tp == tidb.TableOptionCollate { - rule = HeuristicRules["ALT.001"] - break - } - } - } - - if rule.Item == "ALT.001" { - break - } - } - } - } - } - return rule -} - -// RuleAlterDropColumn ALT.003 -func (q *Query4Audit) RuleAlterDropColumn() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableDropColumn: - rule = HeuristicRules["ALT.003"] - } - } - } - } - - if rule.Item == "ALT.003" { - re := regexp.MustCompile(`(?i)(drop\s+column)`) - if position := re.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - } - } - return rule -} - -// RuleAlterDropKey ALT.004 -func (q *Query4Audit) RuleAlterDropKey() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableDropPrimaryKey, - tidb.AlterTableDropIndex, - tidb.AlterTableDropForeignKey: - rule = HeuristicRules["ALT.004"] - } - } - } - } - } - return rule -} - -// RuleCantBeNull COL.012 -func (q *Query4Audit) RuleCantBeNull() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, col := range node.Cols { - switch col.Tp.Tp { - case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob: - if !mysql.HasNotNullFlag(col.Tp.Flag) { - rule = HeuristicRules["COL.012"] - } - } - } - - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableAddColumns, tidb.AlterTableModifyColumn, tidb.AlterTableChangeColumn: - for _, col := range spec.NewColumns { - switch col.Tp.Tp { - case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob: - if !mysql.HasNotNullFlag(col.Tp.Flag) { - rule = HeuristicRules["COL.012"] - } - } - } - } - } - } - } - } - - return rule -} - -// RuleTooManyKeys KEY.005 -func (q *Query4Audit) RuleTooManyKeys() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - if len(node.Constraints) > common.Config.MaxIdxCount { - rule = HeuristicRules["KEY.005"] - } - } - } - } - return rule -} - -// RuleTooManyKeyParts KEY.006 -func (q *Query4Audit) RuleTooManyKeyParts() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, constraint := range node.Constraints { - if len(constraint.Keys) > common.Config.MaxIdxColsCount { - return HeuristicRules["KEY.006"] - } - - if constraint.Refer != nil && len(constraint.Refer.IndexColNames) > common.Config.MaxIdxColsCount { - return HeuristicRules["KEY.006"] - } - } - - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableAddConstraint: - if spec.Constraint != nil { - if len(spec.Constraint.Keys) > common.Config.MaxIdxColsCount { - return HeuristicRules["KEY.006"] - } - - if spec.Constraint.Refer != nil { - if len(spec.Constraint.Refer.IndexColNames) > common.Config.MaxIdxColsCount { - return HeuristicRules["KEY.006"] - } - } - } - } - } - } - } - } - - return rule -} - -// RulePKNotInt KEY.007 && KEY.001 -func (q *Query4Audit) RulePKNotInt() Rule { - var rule = q.RuleOK() - var pk sqlparser.ColIdent - switch s := q.Stmt.(type) { - case *sqlparser.DDL: - if s.Action == "create" { - if s.TableSpec == nil { - return rule - } - for _, idx := range s.TableSpec.Indexes { - if idx.Info.Type == "primary key" { - if len(idx.Columns) == 1 { - pk = idx.Columns[0].Column - break - } - } - } - - // 未指定主键 - if pk.String() == "" { - rule = HeuristicRules["KEY.007"] - return rule - } - - // 主键非int, bigint类型 - for _, col := range s.TableSpec.Columns { - if pk.String() == col.Name.String() { - switch col.Type.Type { - case "int", "bigint", "integer": - if !col.Type.Unsigned { - rule = HeuristicRules["KEY.007"] - } - if !col.Type.Autoincrement { - rule = HeuristicRules["KEY.001"] - } - default: - rule = HeuristicRules["KEY.007"] - } - } - } - } - } - return rule -} - -// RuleOrderByMultiDirection KEY.008 -func (q *Query4Audit) RuleOrderByMultiDirection() Rule { - var rule = q.RuleOK() - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch n := node.(type) { - case sqlparser.OrderBy: - order := "" - for _, col := range strings.Split(sqlparser.String(n), ",") { - orders := strings.Split(col, " ") - if order != "" && order != orders[len(orders)-1] { - rule = HeuristicRules["KEY.008"] - return false, nil - } - order = orders[len(orders)-1] - } - } - return true, nil - }, q.Stmt) - common.LogIfError(err, "") - return rule -} - -// RuleUniqueKeyDup KEY.009 -// TODO: 目前只是给建议,期望能够实现自动检查 -func (q *Query4Audit) RuleUniqueKeyDup() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateIndexStmt: - // create index - if node.Unique { - re := regexp.MustCompile(`(?i)(create\s+(unique)\s)`) - rule = HeuristicRules["KEY.009"] - if position := re.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - return rule - } - - case *tidb.AlterTableStmt: - // alter table add constraint - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableAddConstraint: - if spec.Constraint == nil { - continue - } - switch spec.Constraint.Tp { - case tidb.ConstraintPrimaryKey, tidb.ConstraintUniq, tidb.ConstraintUniqKey, tidb.ConstraintUniqIndex: - re := regexp.MustCompile(`(?i)(add\s+(unique)\s)`) - rule = HeuristicRules["KEY.009"] - if position := re.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - return rule - } - } - } - } - } - } - return rule -} - -// RuleTimestampDefault COL.013 -func (q *Query4Audit) RuleTimestampDefault() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, col := range node.Cols { - if col.Tp.Tp == mysql.TypeTimestamp { - hasDefault := false - for _, option := range col.Options { - if option.Tp == tidb.ColumnOptionDefaultValue { - hasDefault = true - } - } - if !hasDefault { - rule = HeuristicRules["COL.013"] - break - } - } - } - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableAddColumns, - tidb.AlterTableModifyColumn, - tidb.AlterTableChangeColumn, - tidb.AlterTableAlterColumn: - for _, col := range spec.NewColumns { - if col.Tp.Tp == mysql.TypeTimestamp { - hasDefault := false - for _, option := range col.Options { - if option.Tp == tidb.ColumnOptionDefaultValue { - hasDefault = true - } - } - if !hasDefault { - rule = HeuristicRules["COL.013"] - break - } - } - } - } - } - } - } - } - return rule -} - -// RuleAutoIncrementInitNotZero TBL.004 -func (q *Query4Audit) RuleAutoIncrementInitNotZero() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, opt := range node.Options { - if opt.Tp == tidb.TableOptionAutoIncrement && opt.UintValue > 1 { - rule = HeuristicRules["TBL.004"] - } - } - - } - } - } - return rule -} - -// RuleColumnWithCharset COL.014 -func (q *Query4Audit) RuleColumnWithCharset() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, col := range node.Cols { - if col.Tp.Charset != "" || col.Tp.Collate != "" { - rule = HeuristicRules["COL.014"] - break - } - } - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableAlterColumn, tidb.AlterTableChangeColumn, - tidb.AlterTableModifyColumn, tidb.AlterTableAddColumns: - for _, col := range spec.NewColumns { - if col.Tp.Charset != "" || col.Tp.Collate != "" { - rule = HeuristicRules["COL.014"] - break - } - } - } - - } - } - } - } - return rule -} - -// RuleTableCharsetCheck TBL.005 -func (q *Query4Audit) RuleTableCharsetCheck() Rule { - var rule = q.RuleOK() - - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - var allow bool - var hasCharset bool - for _, opt := range node.Options { - if opt.Tp == tidb.TableOptionCharset { - hasCharset = true - for _, ch := range common.Config.TableAllowCharsets { - if strings.TrimSpace(strings.ToLower(ch)) == strings.TrimSpace(strings.ToLower(opt.StrValue)) { - allow = true - break - } - } - } - } - - // 未指定字符集使用MySQL默认配置字符集,我们认为MySQL的配置是被优化过的。 - if hasCharset && !allow { - rule = HeuristicRules["TBL.005"] - break - } - - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - var allow bool - var hasCharset bool - switch spec.Tp { - case tidb.AlterTableOption: - for _, opt := range spec.Options { - if opt.Tp == tidb.TableOptionCharset { - hasCharset = true - for _, ch := range common.Config.TableAllowCharsets { - if strings.TrimSpace(strings.ToLower(ch)) == strings.TrimSpace(strings.ToLower(opt.StrValue)) { - allow = true - break - } - } - } - } - // 未指定字符集使用MySQL默认配置字符集,我们认为MySQL的配置是被优化过的。 - if hasCharset && !allow { - rule = HeuristicRules["TBL.005"] - break - } - } - } - } - } - } - return rule -} - -// RuleForbiddenView TBL.006 -func (q *Query4Audit) RuleForbiddenView() Rule { - var rule = q.RuleOK() - - // 由于vitess对某些语法的支持不完善,使得如创建临时表等语句无法通过语法检查 - // 所以这里使用正则对触发器、临时表、存储过程等进行匹配 - // 但是目前支持的也不是非常全面,有待完善匹配规则 - // TODO TiDB 目前还不支持触发器、存储过程、自定义函数、外键 - - forbidden := []*regexp.Regexp{ - regexp.MustCompile(`(?i)CREATE\s+VIEW\s+`), - regexp.MustCompile(`(?i)REPLACE\s+VIEW\s+`), - } - - for _, reg := range forbidden { - if reg.MatchString(q.Query) { - rule = HeuristicRules["TBL.006"] - if position := reg.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - break - } - } - return rule -} - -// RuleForbiddenTempTable TBL.007 -func (q *Query4Audit) RuleForbiddenTempTable() Rule { - var rule = q.RuleOK() - - // 由于vitess对某些语法的支持不完善,使得如创建临时表等语句无法通过语法检查 - // 所以这里使用正则对触发器、临时表、存储过程等进行匹配 - // 但是目前支持的也不是非常全面,有待完善匹配规则 - // TODO TiDB 目前还不支持触发器、存储过程、自定义函数、外键 - - forbidden := []*regexp.Regexp{ - regexp.MustCompile(`(?i)CREATE\s+TEMPORARY\s+TABLE\s+`), - } - - for _, reg := range forbidden { - if reg.MatchString(q.Query) { - rule = HeuristicRules["TBL.007"] - if position := reg.FindIndex([]byte(q.Query)); len(position) > 0 { - rule.Position = position[0] - } - break - } - } - return rule -} - -// RuleBlobDefaultValue COL.015 -func (q *Query4Audit) RuleBlobDefaultValue() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, col := range node.Cols { - switch col.Tp.Tp { - case mysql.TypeBlob, mysql.TypeMediumBlob, mysql.TypeTinyBlob, mysql.TypeLongBlob: - for _, opt := range col.Options { - if opt.Tp == tidb.ColumnOptionDefaultValue && opt.Expr.GetType().Tp != mysql.TypeNull { - rule = HeuristicRules["COL.015"] - break - } - } - } - } - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableModifyColumn, tidb.AlterTableAlterColumn, - tidb.AlterTableChangeColumn, tidb.AlterTableAddColumns: - for _, col := range spec.NewColumns { - switch col.Tp.Tp { - case mysql.TypeBlob, mysql.TypeMediumBlob, mysql.TypeTinyBlob, mysql.TypeLongBlob: - for _, opt := range col.Options { - if opt.Tp == tidb.ColumnOptionDefaultValue && opt.Expr.GetType().Tp != mysql.TypeNull { - rule = HeuristicRules["COL.015"] - break - } - } - } - } - } - } - } - } - } - return rule -} - -// RuleIntPrecision COL.016 -func (q *Query4Audit) RuleIntPrecision() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, col := range node.Cols { - switch col.Tp.Tp { - case mysql.TypeLong: - if (col.Tp.Flen < 10 || col.Tp.Flen > 11) && col.Tp.Flen > 0 { - // 有些语言 ORM 框架会生成 int(11),有些语言的框架生成 int(10) - rule = HeuristicRules["COL.016"] - break - } - case mysql.TypeLonglong: - if (col.Tp.Flen != 20) && col.Tp.Flen > 0 { - rule = HeuristicRules["COL.016"] - break - } - } - } - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableAddColumns, tidb.AlterTableChangeColumn, - tidb.AlterTableAlterColumn, tidb.AlterTableModifyColumn: - for _, col := range spec.NewColumns { - switch col.Tp.Tp { - case mysql.TypeLong: - if (col.Tp.Flen < 10 || col.Tp.Flen > 11) && col.Tp.Flen > 0 { - // 有些语言 ORM 框架会生成 int(11),有些语言的框架生成 int(10) - rule = HeuristicRules["COL.016"] - break - } - case mysql.TypeLonglong: - if col.Tp.Flen != 20 && col.Tp.Flen > 0 { - rule = HeuristicRules["COL.016"] - break - } - } - } - } - } - } - } - } - return rule -} - -// RuleVarcharLength COL.017 -func (q *Query4Audit) RuleVarcharLength() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, col := range node.Cols { - switch col.Tp.Tp { - case mysql.TypeVarchar, mysql.TypeVarString: - if col.Tp.Flen > common.Config.MaxVarcharLength { - rule = HeuristicRules["COL.017"] - break - } - } - } - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableAddColumns, tidb.AlterTableChangeColumn, - tidb.AlterTableAlterColumn, tidb.AlterTableModifyColumn: - for _, col := range spec.NewColumns { - switch col.Tp.Tp { - case mysql.TypeVarchar, mysql.TypeVarString: - if col.Tp.Flen > common.Config.MaxVarcharLength { - rule = HeuristicRules["COL.017"] - break - } - } - } - } - } - } - } - } - return rule -} - -// RuleNoOSCKey KEY.002 -func (q *Query4Audit) RuleNoOSCKey() Rule { - var rule = q.RuleOK() - switch s := q.Stmt.(type) { - case *sqlparser.DDL: - if s.Action == "create" { - pkReg := regexp.MustCompile(`(?i)(primary\s+key)`) - if !pkReg.MatchString(q.Query) { - ukReg := regexp.MustCompile(`(?i)(unique\s+((key)|(index)))`) - if !ukReg.MatchString(q.Query) { - rule = HeuristicRules["KEY.002"] - } - } - } - } - return rule -} - -// RuleTooManyFields COL.006 -func (q *Query4Audit) RuleTooManyFields() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - if len(node.Cols) > common.Config.MaxColCount { - rule = HeuristicRules["COL.006"] - } - } - } - } - return rule -} - -// RuleAllowEngine TBL.002 -func (q *Query4Audit) RuleAllowEngine() Rule { - var rule = q.RuleOK() - var hasDefaultEngine bool - var allowedEngine bool - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, opt := range node.Options { - if opt.Tp == tidb.TableOptionEngine { - hasDefaultEngine = true - // 使用了非推荐的存储引擎 - for _, engine := range common.Config.TableAllowEngines { - if strings.EqualFold(opt.StrValue, engine) { - allowedEngine = true - } - } - // common.Config.TableAllowEngines 为空时不给予建议 - if !allowedEngine && len(common.Config.TableAllowEngines) > 0 { - rule = HeuristicRules["TBL.002"] - break - } - } - } - // 建表语句未指定表的存储引擎 - if !hasDefaultEngine { - rule = HeuristicRules["TBL.002"] - break - } - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableOption: - for _, opt := range spec.Options { - if opt.Tp == tidb.TableOptionEngine { - // 使用了非推荐的存储引擎 - for _, engine := range common.Config.TableAllowEngines { - if strings.EqualFold(opt.StrValue, engine) { - allowedEngine = true - } - } - // common.Config.TableAllowEngines 为空时不给予建议 - if !allowedEngine && len(common.Config.TableAllowEngines) > 0 { - rule = HeuristicRules["TBL.002"] - break - } - } - } - } - } - } - } - } - return rule -} - -// RulePartitionNotAllowed TBL.001 -func (q *Query4Audit) RulePartitionNotAllowed() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - if node.Partition != nil { - rule = HeuristicRules["TBL.001"] - break - } - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - if len(spec.PartDefinitions) > 0 { - rule = HeuristicRules["TBL.001"] - break - } - } - } - } - } - return rule -} - -// RuleAutoIncUnsigned COL.003: -func (q *Query4Audit) RuleAutoIncUnsigned() Rule { - var rule = q.RuleOK() - switch q.Stmt.(type) { - case *sqlparser.DDL: - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.CreateTableStmt: - for _, col := range node.Cols { - for _, opt := range col.Options { - if opt.Tp == tidb.ColumnOptionAutoIncrement { - if !mysql.HasUnsignedFlag(col.Tp.Flag) { - rule = HeuristicRules["COL.003"] - break - } - } - - if rule.Item == "COL.003" { - break - } - } - } - case *tidb.AlterTableStmt: - for _, spec := range node.Specs { - switch spec.Tp { - case tidb.AlterTableChangeColumn, tidb.AlterTableAlterColumn, - tidb.AlterTableModifyColumn, tidb.AlterTableAddColumns: - for _, col := range spec.NewColumns { - for _, opt := range col.Options { - if opt.Tp == tidb.ColumnOptionAutoIncrement { - if !mysql.HasUnsignedFlag(col.Tp.Flag) { - rule = HeuristicRules["COL.003"] - break - } - } - - if rule.Item == "COL.003" { - break - } - } - } - } - } - } - } - } - return rule -} - -// RuleSpaceAfterDot STA.002 -func (q *Query4Audit) RuleSpaceAfterDot() Rule { - var rule = q.RuleOK() - tks := ast.Tokenize(q.Query) - for i, tk := range tks { - switch tk.Type { - - // SELECT * FROM db. tbl - // SELECT tbl. col FROM tbl - case ast.TokenTypeWord: - if len(tks) > i+1 && - tks[i+1].Type == ast.TokenTypeWhitespace && - strings.HasSuffix(tk.Val, ".") { - common.Log.Debug("RuleSpaceAfterDot: ", tk.Val, tks[i+1].Val) - rule = HeuristicRules["STA.002"] - return rule - } - default: - } - } - return rule -} - -// RuleIdxPrefix STA.003 -func (q *Query4Audit) RuleIdxPrefix() Rule { - var rule = q.RuleOK() - for _, node := range q.TiStmt { - switch n := node.(type) { - case *tidb.CreateTableStmt: - for _, c := range n.Constraints { - switch c.Tp { - case tidb.ConstraintIndex, tidb.ConstraintKey: - if !strings.HasPrefix(c.Name, common.Config.IdxPrefix) { - rule = HeuristicRules["STA.003"] - } - case tidb.ConstraintUniq, tidb.ConstraintUniqKey, tidb.ConstraintUniqIndex: - if !strings.HasPrefix(c.Name, common.Config.UkPrefix) { - rule = HeuristicRules["STA.003"] - } - } - } - case *tidb.AlterTableStmt: - for _, s := range n.Specs { - switch s.Tp { - case tidb.AlterTableAddConstraint: - switch s.Constraint.Tp { - case tidb.ConstraintIndex, tidb.ConstraintKey: - if !strings.HasPrefix(s.Constraint.Name, common.Config.IdxPrefix) { - rule = HeuristicRules["STA.003"] - } - case tidb.ConstraintUniq, tidb.ConstraintUniqKey, tidb.ConstraintUniqIndex: - if !strings.HasPrefix(s.Constraint.Name, common.Config.UkPrefix) { - rule = HeuristicRules["STA.003"] - } - } - } - } - } - } - return rule -} - -// RuleStandardName STA.004 -func (q *Query4Audit) RuleStandardName() Rule { - var rule = q.RuleOK() - allowReg := regexp.MustCompile(`(?i)[a-z0-9_` + "`" + `]`) - for _, tk := range ast.Tokenize(q.Query) { - if tk.Val == "``" { - rule = HeuristicRules["STA.004"] - } - - switch tk.Type { - // 反引号中可能有乱七八糟的东西 - case ast.TokenTypeBacktickQuote: - // 特殊字符,连续下划线 - if allowReg.ReplaceAllString(tk.Val, "") != "" || strings.Contains(tk.Val, "__") { - rule = HeuristicRules["STA.004"] - } - // 统一大小写 - if !(strings.ToLower(tk.Val) == tk.Val || strings.ToUpper(tk.Val) == tk.Val) { - rule = HeuristicRules["STA.004"] - } - case ast.TokenTypeWord: - // TOKEN_TYPE_WORD 中处理连续下划线的情况,其他情况容易误伤 - if strings.Contains(tk.Val, "__") { - rule = HeuristicRules["STA.004"] - } - default: - } - } - return rule -} - -// MergeConflictHeuristicRules merge conflict rules -func MergeConflictHeuristicRules(rules map[string]Rule) map[string]Rule { - // KWR.001 VS ERR.000 - // select sql_calc_found_rows * from film - if _, ok := rules["KWR.001"]; ok { - delete(rules, "ERR.000") - } - - // SUB.001 VS OWN.004 VS JOI.006 - if _, ok := rules["SUB.001"]; ok { - delete(rules, "ARG.005") - delete(rules, "JOI.006") - } - - // SUB.004 VS SUB.001 - if _, ok := rules["SUB.004"]; ok { - delete(rules, "SUB.001") - } - - // KEY.007 VS KEY.002 - if _, ok := rules["KEY.007"]; ok { - delete(rules, "KEY.002") - } - - // JOI.002 VS JOI.006 - if _, ok := rules["JOI.002"]; ok { - delete(rules, "JOI.006") - } - - // JOI.008 VS JOI.007 - if _, ok := rules["JOI.008"]; ok { - delete(rules, "JOI.007") - } - return rules -} - -// RuleMySQLError ERR.XXX -func RuleMySQLError(item string, err error) Rule { - - type MySQLError struct { - ErrCode string - ErrString string - } - - // tidb parser 语法检查出错返回的是ERR.000 - switch item { - case "ERR.000": - return Rule{ - Item: item, - Summary: "No available MySQL environment, build-in sql parse failed: " + err.Error(), - Severity: "L8", - Content: err.Error(), - } - } - - // Received #1146 error from MySQL server: "table xxx doesn't exist" - errReg := regexp.MustCompile(`(?i)Received #([0-9]+) error from MySQL server: ['"](.*)['"]`) - errStr := err.Error() - msg := errReg.FindStringSubmatch(errStr) - var mysqlError MySQLError - - if len(msg) == 3 { - if msg[1] != "" && msg[2] != "" { - mysqlError = MySQLError{ - ErrCode: msg[1], - ErrString: msg[2], - } - } - } else { - var errcode string - if strings.HasPrefix(err.Error(), "syntax error at position") { - errcode = "1064" - } - mysqlError = MySQLError{ - ErrCode: errcode, - ErrString: err.Error(), - } - } - switch mysqlError.ErrCode { - // 1146 ER_NO_SUCH_TABLE - case "", "1146": - return Rule{ - Item: item, - Summary: "MySQL execute failed: ", - Severity: "L0", - Content: "", - } - default: - return Rule{ - Item: item, - Summary: "MySQL execute failed: " + mysqlError.ErrString, - Severity: "L8", - Content: mysqlError.ErrString, - } - } -} diff --git a/vendor/github.com/XiaoMi/soar/advisor/heuristic_test.go b/vendor/github.com/XiaoMi/soar/advisor/heuristic_test.go deleted file mode 100644 index de57328..0000000 --- a/vendor/github.com/XiaoMi/soar/advisor/heuristic_test.go +++ /dev/null @@ -1,3183 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package advisor - -import ( - "errors" - "sort" - "testing" - - "github.com/XiaoMi/soar/common" - - "github.com/kr/pretty" -) - -// ALI.001 -func TestRuleImplicitAlias(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "select col c from tbl where id < 1000", - "select col from tbl tb where id < 1000", - }, - { - "do 1", - }, - } - for _, sql := range sqls[0] { - q, _ := NewQuery4Audit(sql) - rule := q.RuleImplicitAlias() - if rule.Item != "ALI.001" { - t.Error("Rule not match:", rule.Item, "Expect : ALI.001") - } - } - for _, sql := range sqls[1] { - q, _ := NewQuery4Audit(sql) - rule := q.RuleImplicitAlias() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ALI.002 -func TestRuleStarAlias(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select tbl.* as c1,c2,c3 from tbl where id < 1000", - } - for _, sql := range sqls { - q, _ := NewQuery4Audit(sql) - rule := q.RuleStarAlias() - if rule.Item != "ALI.002" { - t.Error("Rule not match:", rule.Item, "Expect : ALI.002") - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ALI.003 -func TestRuleSameAlias(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select col as col from tbl where id < 1000", - "select col from tbl as tbl where id < 1000", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSameAlias() - if rule.Item != "ALI.003" { - t.Error("Rule not match:", rule.Item, "Expect : ALI.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ARG.001 -func TestRulePrefixLike(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select col from tbl where id like '%abc'", - "select col from tbl where id like '_abc'", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RulePrefixLike() - if rule.Item != "ARG.001" { - t.Error("Rule not match:", rule.Item, "Expect : ARG.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ARG.002 -func TestRuleEqualLike(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select col from tbl where id like 'abc'", - "select col from tbl where id like 1", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleEqualLike() - if rule.Item != "ARG.002" { - t.Error("Rule not match:", rule.Item, "Expect : ARG.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.001 -func TestRuleNoWhere(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - {"select col from tbl", - "delete from tbl", - "update tbl set col=1", - "insert into city (country_id) select country_id from country", - }, - { - `select 1;`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleNoWhere() - if rule.Item != "CLA.001" && rule.Item != "CLA.014" && rule.Item != "CLA.015" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.001/CLA.014/CLA.015") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleNoWhere() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.002 -func TestRuleOrderByRand(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select col from tbl where id = 1 order by rand()", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleOrderByRand() - if rule.Item != "CLA.002" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.003 -func TestRuleOffsetLimit(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select c1,c2 from tbl where name=xx order by number limit 1 offset 2000", - "select c1,c2 from tbl where name=xx order by number limit 2000,1", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleOffsetLimit() - if rule.Item != "CLA.003" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.004 -func TestRuleGroupByConst(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select col1,col2 from tbl where col1='abc' group by 1", - "select col1,col2 from tbl group by 1", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleGroupByConst() - if rule.Item != "CLA.004" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.004") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.005 -func TestRuleOrderByConst(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - // "select id from test where id=1 order by id", - "select id from test where id=1 order by 1", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleOrderByConst() - if rule.Item != "CLA.005" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.005") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.006 -func TestRuleDiffGroupByOrderBy(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select tb1.col, tb2.col from tb1, tb2 where id=1 group by tb1.col, tb2.col", - "select tb1.col, tb2.col from tb1, tb2 where id=1 order by tb1.col, tb2.col", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleDiffGroupByOrderBy() - if rule.Item != "CLA.006" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.006") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.007 -func TestRuleMixOrderBy(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select c1,c2,c3 from t1 where c1='foo' order by c2 desc, c3 asc", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleMixOrderBy() - if rule.Item != "CLA.007" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.007") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.008 -func TestRuleExplicitOrderBy(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select c1,c2,c3 from t1 where c1='foo' group by c2", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleExplicitOrderBy() - if rule.Item != "CLA.008" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.008") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.009 -func TestRuleOrderByExpr(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "SELECT col FROM tbl order by cola - cl;", // order by 列运算 - "SELECT cola - cl col FROM tbl order by col;", // 别名为列运算 - "SELECT cola FROM tbl order by from_unixtime(col);", // order by 函数运算 - "SELECT from_unixtime(col) cola FROM tbl order by cola;", // 别名为函数运算 - }, - { - `SELECT tbl.col FROM tbl ORDER BY col`, - "SELECT sum(col) AS col FROM tbl ORDER BY dt", - "SELECT tbl.col FROM tb, tbl WHERE tbl.tag_id = tb.id ORDER BY tbl.col", - "SELECT col FROM tbl order by `timestamp`;", // 列名为关键字 - "select col from tb where cl = 1 order by APPLY_TIME", // issue #104 case sensitive - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleOrderByExpr() - if rule.Item != "CLA.009" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.009") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleOrderByExpr() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.010 -func TestRuleGroupByExpr(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "SELECT col FROM tbl GROUP by cola - col;", - "SELECT cola - col col FROM tbl GROUP by col;", - "SELECT cola FROM tbl GROUP by from_unixtime(col);", - "SELECT from_unixtime(col) cola FROM tbl GROUP by cola;", - - // 反面例子 - // `SELECT tbl.col FROM tbl GROUP BY col`, - // "SELECT dt, sum(col) AS col FROM tbl GROUP BY dt", - // "SELECT tbl.col FROM tb, tbl WHERE tbl.tag_id = tb.id GROUP BY tbl.col", - // "SELECT col FROM tbl GROUP by `timestamp`;", // 列名为关键字 - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleGroupByExpr() - if rule.Item != "CLA.010" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.010") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.011 -func TestRuleTblCommentCheck(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "CREATE TABLE `test1`( `ID` bigint(20) NOT NULL AUTO_INCREMENT," + - " `c1` varchar(128) DEFAULT NULL, `c2` varchar(300) DEFAULT NULL," + - " `c3` varchar(32) DEFAULT NULL, `c4` int(11) NOT NULL, `c5` double NOT NULL," + - " `c6` text NOT NULL, PRIMARY KEY (`ID`), KEY `idx_c3_c2_c4_c5_c6` " + - "(`c3`,`c2`(255),`c4`,`c5`,`c6`(255)), KEY `idx_c3_c2_c4` (`c3`,`c2`,`c4`)) " + - "ENGINE = InnoDB DEFAULT CHARSET=utf8", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleTblCommentCheck() - if rule.Item != "CLA.011" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.011") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.001 -func TestRuleSelectStar(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select * from tbl where id=1", - "select col, * from tbl where id=1", - // 反面例子 - // "select count(*) from film where id=1", - // `select count(* ) from film where id=1`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSelectStar() - if rule.Item != "COL.001" { - t.Error("Rule not match:", rule.Item, "Expect : COL.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.002 -func TestRuleInsertColDef(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "insert into tbl values(1,'name')", - "replace into tbl values(1,'name')", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleInsertColDef() - if rule.Item != "COL.002" { - t.Error("Rule not match:", rule.Item, "Expect : COL.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.004 -func TestRuleAddDefaultValue(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "create table test(id int)", - `ALTER TABLE test change id id varchar(10);`, - `ALTER TABLE test modify id varchar(10);`, - }, - { - `ALTER TABLE test modify id varchar(10) DEFAULT '';`, - `ALTER TABLE test CHANGE id id varchar(10) DEFAULT '';`, - "create table test(id int not null default 0 comment '用户id')", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAddDefaultValue() - if rule.Item != "COL.004" { - t.Error("Rule not match:", rule.Item, "Expect : COL.004") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAddDefaultValue() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.005 -func TestRuleColCommentCheck(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "create table test(id int not null default 0)", - `alter table test add column a int`, - `ALTER TABLE t1 CHANGE b b INT NOT NULL;`, - }, - { - "create table test(id int not null default 0 comment '用户id')", - `alter table test add column a int comment 'test'`, - `ALTER TABLE t1 AUTO_INCREMENT = 13;`, - `ALTER TABLE t1 CHANGE b b INT NOT NULL COMMENT 'test';`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleColCommentCheck() - if rule.Item != "COL.005" { - t.Error("Rule not match:", rule.Item, "Expect : COL.005") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleColCommentCheck() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// LIT.001 -func TestRuleIPString(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "insert into tbl (IP,name) values('10.20.306.122','test')", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleIPString() - if rule.Item != "LIT.001" { - t.Error("Rule not match:", rule.Item, "Expect : LIT.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// LIT.002 -func TestRuleDataNotQuote(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select col1,col2 from tbl where time < 2018-01-10", - "select col1,col2 from tbl where time < 18-01-10", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleDataNotQuote() - if rule.Item != "LIT.002" { - t.Error("Rule not match:", rule.Item, "Expect : LIT.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KWR.001 -func TestRuleSQLCalcFoundRows(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select SQL_CALC_FOUND_ROWS col from tbl where id>1000", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSQLCalcFoundRows() - if rule.Item != "KWR.001" { - t.Error("Rule not match:", rule.Item, "Expect : KWR.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// JOI.001 -func TestRuleCommaAnsiJoin(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select c1,c2,c3 from t1,t2 join t3 on t1.c1=t2.c1 and t1.c3=t3.c1 where id>1000;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleCommaAnsiJoin() - if rule.Item != "JOI.001" { - t.Error("Rule not match:", rule.Item, "Expect : JOI.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// JOI.002 -func TestRuleDupJoin(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select tb1.col from (tb1, tb2) join tb2 on tb1.id=tb.id where tb1.id=1;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleDupJoin() - if rule.Item != "JOI.002" { - t.Error("Rule not match:", rule.Item, "Expect : JOI.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// RES.001 -func TestRuleNoDeterministicGroupby(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - // 正面CASE - { - "select c1,c2,c3 from t1 where c2='foo' group by c2", - "select col, col2, sum(col1) from tb group by col", - "select col, col1 from tb group by col,sum(col1)", - "select * from tb group by col", - }, - - // 反面CASE - { - "select id from film", - "select col, sum(col1) from tb group by col", - "select * from file", - "SELECT COUNT(*) AS cnt, language_id FROM film GROUP BY language_id;", - "SELECT COUNT(*) AS cnt FROM film GROUP BY language_id;", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleNoDeterministicGroupby() - if rule.Item != "RES.001" { - t.Error("Rule not match:", rule.Item, "Expect : RES.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleNoDeterministicGroupby() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// RES.002 -func TestRuleNoDeterministicLimit(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select col1,col2 from tbl where name='tony' limit 10", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleNoDeterministicLimit() - if rule.Item != "RES.002" { - t.Error("Rule not match:", rule.Item, "Expect : RES.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// RES.003 -func TestRuleUpdateDeleteWithLimit(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "UPDATE film SET length = 120 WHERE title = 'abc' LIMIT 1;", - }, - { - "UPDATE film SET length = 120 WHERE title = 'abc';", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleUpdateDeleteWithLimit() - if rule.Item != "RES.003" { - t.Error("Rule not match:", rule.Item, "Expect : RES.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleUpdateDeleteWithLimit() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// RES.004 -func TestRuleUpdateDeleteWithOrderby(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "UPDATE film SET length = 120 WHERE title = 'abc' ORDER BY title;", - }, - { - "UPDATE film SET length = 120 WHERE title = 'abc';", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleUpdateDeleteWithOrderby() - if rule.Item != "RES.004" { - t.Error("Rule not match:", rule.Item, "Expect : RES.004") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleUpdateDeleteWithOrderby() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// RES.005 -func TestRuleUpdateSetAnd(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "update tbl set col = 1 and cl = 2 where col=3;", - }, - { - "update tbl set col = 1 ,cl = 2 where col=3;", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleUpdateSetAnd() - if rule.Item != "RES.005" { - t.Error("Rule not match:", rule.Item, "Expect : RES.005") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleUpdateSetAnd() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// RES.006 -func TestRuleImpossibleWhere(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "select * from tbl where 1 != 1;", - "select * from tbl where 'a' != 'a';", - "select * from tbl where col between 10 AND 5;", - }, - { - "select * from tbl where 1 = 1;", - "select * from tbl where 'a' != 1;", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleImpossibleWhere() - if rule.Item != "RES.006" { - t.Error("Rule not match:", rule.Item, "Expect : RES.006, SQL: ", sql) - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleImpossibleWhere() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK, SQL: ", sql) - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// RES.007 -func TestRuleMeaninglessWhere(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "select * from tbl where 1 = 1;", - "select * from tbl where 'a' = 'a';", - "select * from tbl where 'a' != 1;", - "select * from tbl where 'a';", - "select * from tbl where 'a' limit 1;", - "select * from tbl where 1;", - "select * from tbl where 1 limit 1;", - }, - { - "select * from tbl where 2 = 1;", - "select * from tbl where 'b' = 'a';", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleMeaninglessWhere() - if rule.Item != "RES.007" { - t.Error("Rule not match:", rule.Item, "Expect : RES.007, SQL: ", sql) - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleMeaninglessWhere() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK, SQL: ", sql) - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// RES.008 -func TestRuleLoadFile(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;", - "LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;", - "LOAD /*COMMENT*/DATA INFILE 'data.txt' INTO TABLE db2.my_table;", - `SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM test_table;`, - }, - { - "SELECT id, data INTO @x, @y FROM test.t1 LIMIT 1;", - }, - } - for _, sql := range sqls[0] { - q := &Query4Audit{Query: sql} - rule := q.RuleLoadFile() - if rule.Item != "RES.008" { - t.Error("Rule not match:", rule.Item, "Expect : RES.008, SQL: ", sql) - } - } - - for _, sql := range sqls[1] { - q := &Query4Audit{Query: sql} - rule := q.RuleLoadFile() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK, SQL: ", sql) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// STA.001 -func TestRuleStandardINEQ(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select col1,col2 from tbl where type!=0", - // "select col1,col2 from tbl where type<>0", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleStandardINEQ() - if rule.Item != "STA.001" { - t.Error("Rule not match:", rule.Item, "Expect : STA.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KWR.002 -func TestRuleUseKeyWord(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "CREATE TABLE tbl (`select` int)", - "CREATE TABLE `select` (a int)", - "ALTER TABLE tbl ADD COLUMN `select` varchar(10)", - }, - { - "CREATE TABLE tbl (a int)", - "ALTER TABLE tbl ADD COLUMN col varchar(10)", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleUseKeyWord() - if rule.Item != "KWR.002" { - t.Error("Rule not match:", rule.Item, "Expect : KWR.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleUseKeyWord() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KWR.003 -func TestRulePluralWord(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "CREATE TABLE tbl (`people` int)", - "CREATE TABLE people (a int)", - "ALTER TABLE tbl ADD COLUMN people varchar(10)", - }, - { - "CREATE TABLE tbl (`person` int)", - "ALTER TABLE tbl ADD COLUMN person varchar(10)", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RulePluralWord() - if rule.Item != "KWR.003" { - t.Error("Rule not match:", rule.Item, "Expect : KWR.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RulePluralWord() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KWR.004 -func TestRuleMultiBytesWord(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "select col as 列 from tb", - "select col as `列` from tb", - }, - { - "select col as c from tb", - "select '列'", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleMultiBytesWord() - if rule.Item != "KWR.004" { - t.Error("Rule not match:", rule.Item, "Expect : KWR.004") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleMultiBytesWord() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// LCK.001 -func TestRuleInsertSelect(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `INSERT INTO tbl SELECT * FROM tbl2;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleInsertSelect() - if rule.Item != "LCK.001" { - t.Error("Rule not match:", rule.Item, "Expect : LCK.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// LCK.002 -func TestRuleInsertOnDup(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `INSERT INTO t1(a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleInsertOnDup() - if rule.Item != "LCK.002" { - t.Error("Rule not match:", rule.Item, "Expect : LCK.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// SUB.001 -func TestRuleInSubquery(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select col1,col2,col3 from table1 where col2 in(select col from table2)", - "SELECT col1,col2,col3 from table1 where col2 =(SELECT col2 FROM `table1` limit 1)", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleInSubquery() - if rule.Item != "SUB.001" { - t.Error("Rule not match:", rule.Item, "Expect : SUB.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// LIT.003 -func TestRuleMultiValueAttribute(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select c1,c2,c3,c4 from tab1 where col_id REGEXP '[[:<:]]12[[:>:]]'", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleMultiValueAttribute() - if rule.Item != "LIT.003" { - t.Error("Rule not match:", rule.Item, "Expect : LIT.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// LIT.003 -func TestRuleAddDelimiter(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `use sakila - select * from film`, - `use sakila`, - `show databases`, - }, - { - `use sakila;`, - }, - } - for _, sql := range sqls[0] { - q, _ := NewQuery4Audit(sql) - - rule := q.RuleAddDelimiter() - if rule.Item != "LIT.004" { - t.Error("Rule not match:", rule.Item, "Expect : LIT.004") - } - } - for _, sql := range sqls[1] { - q, _ := NewQuery4Audit(sql) - - rule := q.RuleAddDelimiter() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KEY.003 -func TestRuleRecursiveDependency(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `CREATE TABLE tab2 ( - p_id BIGINT UNSIGNED NOT NULL, - a_id BIGINT UNSIGNED NOT NULL, - PRIMARY KEY (p_id, a_id), - FOREIGN KEY (p_id) REFERENCES tab1(p_id), - FOREIGN KEY (a_id) REFERENCES tab3(a_id) - );`, - `ALTER TABLE tbl2 add FOREIGN KEY (p_id) REFERENCES tab1(p_id);`, - }, - { - `ALTER TABLE tbl2 ADD KEY p_id (p_id);`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleRecursiveDependency() - if rule.Item != "KEY.003" { - t.Error("Rule not match:", rule.Item, "Expect : KEY.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleRecursiveDependency() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.009 -func TestRuleImpreciseDataType(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `CREATE TABLE tab2 ( - p_id BIGINT UNSIGNED NOT NULL, - a_id BIGINT UNSIGNED NOT NULL, - hours float NOT null, - PRIMARY KEY (p_id, a_id) - );`, - `alter table tbl add column c float not null;`, - `insert into tb (col) values (0.00001);`, - `select * from tb where col = 0.00001;`, - }, - { - "REPLACE INTO `storage` (`hostname`,`storagehost`, `filename`, `starttime`, `binlogstarttime`, `uploadname`, `binlogsize`, `filesize`, `md5`, `status`) VALUES (1, 1, 1, 1, 1, 1, ?, ?);", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleImpreciseDataType() - if rule.Item != "COL.009" { - t.Error("Rule not match:", rule.Item, "Expect : COL.009") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleImpreciseDataType() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.010 -func TestRuleValuesInDefinition(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `create table tab1(status ENUM('new', 'in progress', 'fixed'))`, - `alter table tab1 add column status ENUM('new', 'in progress', 'fixed')`, - } - - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleValuesInDefinition() - if rule.Item != "COL.010" { - t.Error("Rule not match:", rule.Item, "Expect : COL.010") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KEY.004 -func TestRuleIndexAttributeOrder(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `create index idx1 on tab(last_name,first_name);`, - `alter table tab add index idx1 (last_name,first_name);`, - `CREATE TABLE test (id int,blob_col BLOB, INDEX(blob_col(10),id));`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleIndexAttributeOrder() - if rule.Item != "KEY.004" { - t.Error("Rule not match:", rule.Item, "Expect : KEY.004") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.011 -func TestRuleNullUsage(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select c1,c2,c3 from tab where c4 is null or c4 <> 1;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleNullUsage() - if rule.Item != "COL.011" { - t.Error("Rule not match:", rule.Item, "Expect : COL.011") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// FUN.003 -func TestRuleStringConcatenation(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select c1 || coalesce(' ' || c2 || ' ', ' ') || c3 as c from tab;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleStringConcatenation() - if rule.Item != "FUN.003" { - t.Error("Rule not match:", rule.Item, "Expect : FUN.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// FUN.004 -func TestRuleSysdate(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select sysdate();`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSysdate() - if rule.Item != "FUN.004" { - t.Error("Rule not match:", rule.Item, "Expect : FUN.004") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// FUN.005 -func TestRuleCountConst(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `select count(1) from tbl;`, - `select count(col) from tbl;`, - }, - { - `select count(*) from tbl`, - `select count(DISTINCT col) from tbl`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleCountConst() - if rule.Item != "FUN.005" { - t.Error("Rule not match:", rule.Item, "Expect : FUN.005") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleCountConst() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// FUN.006 -func TestRuleSumNPE(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `select sum(1) from tbl;`, - `select sum(col) from tbl;`, - }, - { - `SELECT IF(ISNULL(SUM(COL)), 0, SUM(COL)) FROM tbl`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSumNPE() - if rule.Item != "FUN.006" { - t.Error("Rule not match:", rule.Item, "Expect : FUN.006") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSumNPE() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ARG.007 -func TestRulePatternMatchingUsage(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select c1,c2,c3,c4 from tab1 where col_id REGEXP '[[:<:]]12[[:>:]]';`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RulePatternMatchingUsage() - if rule.Item != "ARG.007" { - t.Error("Rule not match:", rule.Item, "Expect : ARG.007") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.012 -func TestRuleSpaghettiQueryAlert(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select 1`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - common.Config.SpaghettiQueryLength = 1 - rule := q.RuleSpaghettiQueryAlert() - if rule.Item != "CLA.012" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.012") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// JOI.005 -func TestRuleReduceNumberOfJoin(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select bp1.p_id, b1.d_d as l, b1.b_id from b1 join bp1 on (b1.b_id = bp1.b_id) left outer join (b1 as b2 join bp2 on (b2.b_id = bp2.b_id)) on (bp1.p_id = bp2.p_id ) join bp21 on (b1.b_id = bp1.b_id) join bp31 on (b1.b_id = bp1.b_id) join bp41 on (b1.b_id = bp1.b_id) where b2.b_id = 0; `, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleReduceNumberOfJoin() - if rule.Item != "JOI.005" { - t.Error("Rule not match:", rule.Item, "Expect : JOI.005") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// DIS.001 -func TestRuleDistinctUsage(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `SELECT DISTINCT c.c_id,count(DISTINCT c.c_name),count(DISTINCT c.c_e),count(DISTINCT c.c_n),count(DISTINCT c.c_me),c.c_d FROM (select distinct id, name from B) as e WHERE e.country_id = c.country_id;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleDistinctUsage() - if rule.Item != "DIS.001" { - t.Error("Rule not match:", rule.Item, "Expect : DIS.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// DIS.002 -func TestRuleCountDistinctMultiCol(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "SELECT COUNT(DISTINCT col, col2) FROM tbl;", - }, - { - "SELECT COUNT(DISTINCT col) FROM tbl;", - `SELECT JSON_OBJECT( "key", p.id, "title", p.name, "manufacturer", p.manufacturer, "price", p.price, "specifications", JSON_OBJECTAGG(a.name, v.value)) as product FROM product as p JOIN value as v ON p.id = v.prod_id JOIN attribute as a ON a.id = v.attribute_id GROUP BY v.prod_id`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleCountDistinctMultiCol() - if rule.Item != "DIS.002" { - t.Error("Rule not match:", rule.Item, "Expect : DIS.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleCountDistinctMultiCol() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// DIS.003 -// RuleDistinctStar -func TestRuleDistinctStar(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "SELECT DISTINCT * FROM film;", - "SELECT DISTINCT film.* FROM film;", - }, - { - "SELECT DISTINCT col FROM film;", - "SELECT DISTINCT film.* FROM film, tbl;", - "SELECT DISTINCT * FROM film, tbl;", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleDistinctStar() - if rule.Item != "DIS.003" { - t.Error("Rule not match:", rule.Item, "Expect : DIS.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleDistinctStar() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.013 -func TestRuleHavingClause(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `SELECT s.c_id,count(s.c_id) FROM s where c = test GROUP BY s.c_id HAVING s.c_id <> '1660' AND s.c_id <> '2' order by s.c_id;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleHavingClause() - if rule.Item != "CLA.013" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.013") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// FUN.007 -func TestRuleForbiddenTrigger(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `CREATE TRIGGER t1 AFTER INSERT ON work FOR EACH ROW INSERT INTO time VALUES(NOW());`, - } - for _, sql := range sqls { - q, _ := NewQuery4Audit(sql) - rule := q.RuleForbiddenTrigger() - if rule.Item != "FUN.007" { - t.Error("Rule not match:", rule.Item, "Expect : FUN.007") - } - - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// FUN.008 -func TestRuleForbiddenProcedure(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `CREATE PROCEDURE simpleproc (OUT param1 INT)`, - } - for _, sql := range sqls { - q, _ := NewQuery4Audit(sql) - rule := q.RuleForbiddenProcedure() - if rule.Item != "FUN.008" { - t.Error("Rule not match:", rule.Item, "Expect : FUN.008") - } - - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// FUN.009 -func TestRuleForbiddenFunction(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `CREATE FUNCTION hello (s CHAR(20));`, - } - for _, sql := range sqls { - q, _ := NewQuery4Audit(sql) - rule := q.RuleForbiddenFunction() - if rule.Item != "FUN.009" { - t.Error("Rule not match:", rule.Item, "Expect : FUN.009") - } - - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// TBL.006 -func TestRuleForbiddenView(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `create view v_today (today) AS SELECT CURRENT_DATE;`, - `CREATE VIEW v (col) AS SELECT 'abc';`, - } - for _, sql := range sqls { - q, _ := NewQuery4Audit(sql) - rule := q.RuleForbiddenView() - if rule.Item != "TBL.006" { - t.Error("Rule not match:", rule.Item, "Expect : TBL.006") - } - - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// TBL.007 -func TestRuleForbiddenTempTable(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "CREATE TEMPORARY TABLE `work` (`time` time DEFAULT NULL) ENGINE=InnoDB;", - } - for _, sql := range sqls { - q, _ := NewQuery4Audit(sql) - rule := q.RuleForbiddenTempTable() - if rule.Item != "TBL.007" { - t.Error("Rule not match:", rule.Item, "Expect : TBL.007") - } - - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// JOI.006 -func TestRuleNestedSubQueries(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `SELECT s,p,d FROM tab WHERE p.p_id = (SELECT s.p_id FROM tab WHERE s.c_id = 100996 AND s.q = 1 );`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleNestedSubQueries() - if rule.Item != "JOI.006" { - t.Error("Rule not match:", rule.Item, "Expect : JOI.006") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// JOI.007 -func TestRuleMultiDeleteUpdate(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `DELETE u FROM users u LEFT JOIN hobby tna ON u.id = tna.uid WHERE tna.hobby = 'piano'; `, - `UPDATE users u LEFT JOIN hobby h ON u.id = h.uid SET u.name = 'pianoboy' WHERE h.hobby = 'piano';`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleMultiDeleteUpdate() - if rule.Item != "JOI.007" { - t.Error("Rule not match:", rule.Item, "Expect : JOI.007") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// JOI.008 -func TestRuleMultiDBJoin(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `SELECT s,p,d FROM db1.tb1 join db2.tb2 on db1.tb1.a = db2.tb2.a where db1.tb1.a > 10;`, - `SELECT s,p,d FROM db1.tb1 join tb2 on db1.tb1.a = tb2.a where db1.tb1.a > 10;`, - // `SELECT s,p,d FROM db1.tb1 join db1.tb2 on db1.tb1.a = db1.tb2.a where db1.tb1.a > 10;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleMultiDBJoin() - if rule.Item != "JOI.008" { - t.Error("Rule not match:", rule.Item, "Expect : JOI.008") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ARG.008 -func TestRuleORUsage(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `SELECT c1,c2,c3 FROM tab WHERE c1 = 14 OR c2 = 17;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleORUsage() - if rule.Item != "ARG.008" { - t.Error("Rule not match:", rule.Item, "Expect : ARG.008") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ARG.009 -func TestRuleSpaceWithQuote(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `SELECT 'a ';`, - `SELECT ' a';`, - `SELECT "a ";`, - `SELECT " a";`, - }, - { - `select ''`, - `select 'a'`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSpaceWithQuote() - if rule.Item != "ARG.009" { - t.Error("Rule not match:", rule.Item, "Expect : ARG.009") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSpaceWithQuote() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ARG.010 -func TestRuleHint(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `SELECT * FROM t1 USE INDEX (i1) ORDER BY a;`, - `SELECT * FROM t1 IGNORE INDEX (i1) ORDER BY (i2);`, - // TODO: vitess syntax not support now - // `SELECT * FROM t1 USE INDEX (i1,i2) IGNORE INDEX (i2);`, - // `SELECT * FROM t1 USE INDEX (i1) IGNORE INDEX (i2) USE INDEX (i2);`, - }, - { - `select ''`, - `select 'a'`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleHint() - if rule.Item != "ARG.010" { - t.Error("Rule not match:", rule.Item, "Expect : ARG.010") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleHint() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ARG.011 -func TestRuleNot(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `select id from t where num not in(1,2,3);`, - `select id from t where num not like "a%"`, - }, - { - `select id from t where num in(1,2,3);`, - `select id from t where num like "a%"`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleNot() - if rule.Item != "ARG.011" { - t.Error("Rule not match:", rule.Item, "Expect : ARG.011") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleNot() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ARG.012 -func TestRuleInsertValues(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `INSERT INTO tb VALUES (1), (2)`, - `REPLACE INTO tb VALUES (1), (2)`, - }, - { - `INSERT INTO tb VALUES (1)`, - }, - } - oldMaxValueCount := common.Config.MaxValueCount - common.Config.MaxValueCount = 1 - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleInsertValues() - if rule.Item != "ARG.012" { - t.Error("Rule not match:", rule.Item, "Expect : ARG.012") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleInsertValues() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Config.MaxValueCount = oldMaxValueCount - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// SUB.002 -func TestRuleUNIONUsage(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select teacher_id as id,people_name as name from t1,t2 where t1.teacher_id=t2.people_id union select student_id as id,people_name as name from t1,t2 where t1.student_id=t2.people_id;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleUNIONUsage() - if rule.Item != "SUB.002" { - t.Error("Rule not match:", rule.Item, "Expect : SUB.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// SUB.003 -func TestRuleDistinctJoinUsage(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `SELECT DISTINCT c.c_id, c.c_name FROM c,e WHERE e.c_id = c.c_id;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleDistinctJoinUsage() - if rule.Item != "SUB.003" { - t.Error("Rule not match:", rule.Item, "Expect : SUB.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// SUB.005 -func TestRuleSubQueryLimit(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `SELECT * FROM staff WHERE name IN (SELECT NAME FROM customer ORDER BY name LIMIT 1)`, - }, - { - `select * from (select id from tbl limit 3) as foo`, - `select * from tbl where id in (select t.id from (select * from tbl limit 3)as t)`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSubQueryLimit() - if rule.Item != "SUB.005" { - t.Error("Rule not match:", rule.Item, "Expect : SUB.005") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSubQueryLimit() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// SUB.006 -func TestRuleSubQueryFunctions(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `SELECT * FROM staff WHERE name IN (SELECT max(NAME) FROM customer)`, - }, - { - `select * from (select id from tbl limit 3) as foo`, - `select * from tbl where id in (select t.id from (select * from tbl limit 3)as t)`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSubQueryFunctions() - if rule.Item != "SUB.006" { - t.Error("Rule not match:", rule.Item, "Expect : SUB.006") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSubQueryFunctions() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// SEC.002 -func TestRuleReadablePasswords(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `create table test(id int,name varchar(20) not null,password varchar(200)not null);`, - `alter table test add column password varchar(200) not null;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleReadablePasswords() - if rule.Item != "SEC.002" { - t.Error("Rule not match:", rule.Item, "Expect : SEC.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// SEC.003 -func TestRuleDataDrop(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `delete from tb where a = b;`, - `truncate table tb;`, - `drop table tb;`, - `drop database db;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleDataDrop() - if rule.Item != "SEC.003" { - t.Error("Rule not match:", rule.Item, "Expect : SEC.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// FUN.001 -func TestCompareWithFunction(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `select id from t where substring(name,1,3)='abc';`, - `SELECT * FROM tbl WHERE UNIX_TIMESTAMP(loginTime) BETWEEN UNIX_TIMESTAMP('2018-11-16 09:46:00 +0800 CST') AND UNIX_TIMESTAMP('2018-11-22 00:00:00 +0800 CST')`, - }, - // TODO: 右侧使用函数比较 - { - `select id from t where 'abc'=substring(name,1,3);`, - `select id from t where col = (select 1)`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleCompareWithFunction() - if rule.Item != "FUN.001" { - t.Error("Rule not match:", rule.Item, "Expect : FUN.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleCompareWithFunction() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// FUN.002 -func TestRuleCountStar(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `SELECT c3, COUNT(*) AS accounts FROM tab where c2 < 10000 GROUP BY c3 ORDER BY num;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleCountStar() - if rule.Item != "FUN.002" { - t.Error("Rule not match:", rule.Item, "Expect : FUN.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// SEC.001 -func TestRuleTruncateTable(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `TRUNCATE TABLE tbl_name;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleTruncateTable() - if rule.Item != "SEC.001" { - t.Error("Rule not match:", rule.Item, "Expect : SEC.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ARG.005 -func TestRuleIn(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select id from t where num in(1,2,3);`, - `SELECT * FROM tbl WHERE col IN (NULL)`, - `SELECT * FROM tbl WHERE col NOT IN (NULL)`, - } - common.Config.MaxInCount = 0 - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleIn() - if rule.Item != "ARG.005" && rule.Item != "ARG.004" { - t.Error("Rule not match:", rule.Item, "Expect : ARG.005 OR ARG.004") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ARG.006 -func TestRuleIsNullIsNotNull(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select id from t where num is null;`, - `select id from t where num is not null;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleIsNullIsNotNull() - if rule.Item != "ARG.006" { - t.Error("Rule not match:", rule.Item, "Expect : ARG.006") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.008 -func TestRuleVarcharVSChar(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `create table t1(id int,name char(20),last_time date);`, - `create table t1(id int,name binary(20),last_time date);`, - `alter table t1 add column id int, add column name binary(20), add column last_time date;`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleVarcharVSChar() - if rule.Item != "COL.008" { - t.Error("Rule not match:", rule.Item, "Expect : COL.008") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// TBL.003 -func TestRuleCreateDualTable(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "create table `dual`(id int, primary key (id));", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleCreateDualTable() - if rule.Item != "TBL.003" { - t.Error("Rule not match:", rule.Item, "Expect : TBL.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ALT.001 -func TestRuleAlterCharset(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `alter table tbl default character set 'utf8';`, - `alter table tbl default character set='utf8';`, - `ALTER TABLE t1 CHANGE a b BIGINT NOT NULL, default character set utf8`, - `ALTER TABLE t1 CHANGE a b BIGINT NOT NULL,default character set utf8`, - `ALTER TABLE tbl_name CHARACTER SET charset_name;`, - `ALTER TABLE t1 CHANGE a b BIGINT NOT NULL, character set utf8`, - `ALTER TABLE t1 CHANGE a b BIGINT NOT NULL,character set utf8`, - `alter table t1 convert to character set utf8 collate utf8_unicode_ci;`, - `alter table t1 default collate = utf8_unicode_ci;`, - }, - { - // 反面的例子 - `ALTER TABLE t MODIFY latin1_text_col TEXT CHARACTER SET utf8`, - `ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAlterCharset() - if rule.Item != "ALT.001" { - t.Error(sql, " Rule not match:", rule.Item, "Expect : ALT.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAlterCharset() - if rule.Item != "OK" { - t.Error(sql, " Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ALT.003 -func TestRuleAlterDropColumn(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `alter table film drop column title;`, - }, - { - // 反面的例子 - `ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAlterDropColumn() - if rule.Item != "ALT.003" { - t.Error(sql, " Rule not match:", rule.Item, "Expect : ALT.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAlterDropColumn() - if rule.Item != "OK" { - t.Error(sql, " Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// ALT.004 -func TestRuleAlterDropKey(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `alter table film drop primary key`, - `alter table film drop foreign key fk_film_language`, - }, - { - // 反面的例子 - `ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAlterDropKey() - if rule.Item != "ALT.004" { - t.Error(sql, " Rule not match:", rule.Item, "Expect : ALT.004") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAlterDropKey() - if rule.Item != "OK" { - t.Error(sql, " Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.012 -func TestRuleCantBeNull(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "CREATE TABLE `tbl` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `c` longblob, PRIMARY KEY (`id`));", - "alter TABLE `tbl` add column `c` longblob;", - "alter TABLE `tbl` add column `c` text;", - "alter TABLE `tbl` add column `c` blob;", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleCantBeNull() - if rule.Item != "COL.012" { - t.Error("Rule not match:", rule.Item, "Expect : COL.012") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KEY.006 -func TestRuleTooManyKeyParts(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "CREATE TABLE `tb` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `c` longblob NOT NULL DEFAULT '', PRIMARY KEY (`id`));", - "alter TABLE `tb` add index idx_idx (`id`);", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - common.Config.MaxIdxColsCount = 0 - rule := q.RuleTooManyKeyParts() - if rule.Item != "KEY.006" { - t.Error("Rule not match:", rule.Item, "Expect : KEY.006") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KEY.005 -func TestRuleTooManyKeys(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "create table tbl ( a char(10), b int, primary key (`a`)) engine=InnoDB;", - "create table tbl ( a varchar(64) not null, b int, PRIMARY KEY (`a`), key `idx_a_b` (`a`,`b`)) engine=InnoDB", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - common.Config.MaxIdxCount = 0 - rule := q.RuleTooManyKeys() - if rule.Item != "KEY.005" { - t.Error("Rule not match:", rule.Item, "Expect : KEY.005") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KEY.007 -func TestRulePKNotInt(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "create table tbl ( a char(10), b int, primary key (`a`)) engine=InnoDB;", - "create table tbl ( a int, b int, primary key (`a`)) engine=InnoDB;", - "create table tbl ( a bigint, b int, primary key (`a`)) engine=InnoDB;", - "create table tbl ( a int unsigned, b int, primary key (`a`)) engine=InnoDB;", - "create table tbl ( a bigint unsigned, b int, primary key (`a`)) engine=InnoDB;", - }, - { - "CREATE TABLE tbl (a int unsigned auto_increment, b int, primary key(`a`)) engine=InnoDB;", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RulePKNotInt() - if rule.Item != "KEY.007" && rule.Item != "KEY.001" { - t.Error("Rule not match:", rule.Item, "Expect : KEY.007 OR KEY.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RulePKNotInt() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KEY.008 -func TestRuleOrderByMultiDirection(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `SELECT col FROM tbl order by col desc, col2 asc`, - }, - { - `SELECT col FROM tbl order by col, col2`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleOrderByMultiDirection() - if rule.Item != "KEY.008" { - t.Error("Rule not match:", rule.Item, "Expect : KEY.008") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleOrderByMultiDirection() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KEY.009 -func TestRuleUniqueKeyDup(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `ALTER TABLE customer ADD UNIQUE INDEX part_of_name (name(10));`, - `CREATE UNIQUE INDEX part_of_name ON customer (name(10));`, - }, - { - `ALTER TABLE tbl add INDEX idx_col (col);`, - `CREATE INDEX part_of_name ON customer (name(10));`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleUniqueKeyDup() - if rule.Item != "KEY.009" { - t.Error("Rule not match:", rule.Item, "Expect : KEY.009") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleUniqueKeyDup() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.013 -func TestRuleTimestampDefault(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "CREATE TABLE tbl( `id` bigint not null, `create_time` timestamp) ENGINE=InnoDB DEFAULT CHARSET=utf8;", - "ALTER TABLE t1 MODIFY b timestamp NOT NULL;", - }, - { - "CREATE TABLE tbl (`id` bigint not null, `update_time` timestamp default current_timestamp)", - "ALTER TABLE t1 MODIFY b timestamp NOT NULL default current_timestamp;", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleTimestampDefault() - if rule.Item != "COL.013" { - t.Error("Rule not match:", rule.Item, "Expect : COL.013") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleTimestampDefault() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// TBL.004 -func TestRuleAutoIncrementInitNotZero(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - // 正面的例子 - { - "CREATE TABLE `tb` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `pad` char(60) NOT NULL DEFAULT '', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=13", - }, - // 反面的例子 - { - "CREATE TABLE `test1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `pad` char(60) NOT NULL DEFAULT '', PRIMARY KEY (`id`))", - "CREATE TABLE `test1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `pad` char(60) NOT NULL DEFAULT '', PRIMARY KEY (`id`)) auto_increment = 1", - "CREATE TABLE `test1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `pad` char(60) NOT NULL DEFAULT '', PRIMARY KEY (`id`)) auto_increment = 1 DEFAULT CHARSET=latin1", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAutoIncrementInitNotZero() - if rule.Item != "TBL.004" { - t.Error("Rule not match:", rule.Item, "Expect : TBL.004") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAutoIncrementInitNotZero() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.014 -func TestRuleColumnWithCharset(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - // 正面的例子 - { - "CREATE TABLE `tb2` ( `id` int(11) DEFAULT NULL, `col` char(10) CHARACTER SET utf8 DEFAULT NULL)", - "alter table tb2 change col col char(10) CHARACTER SET utf8 DEFAULT NULL;", - }, - // 反面的例子 - { - "CREATE TABLE `tb` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `c` char(120) NOT NULL DEFAULT '', PRIMARY KEY (`id`))", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleColumnWithCharset() - if rule.Item != "COL.014" { - t.Error("Rule not match:", rule.Item, "Expect : COL.014") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleColumnWithCharset() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// TBL.005 -func TestRuleTableCharsetCheck(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "create table tbl (a int) DEFAULT CHARSET=latin1;", - "ALTER TABLE tbl CONVERT TO CHARACTER SET latin1;", - }, - { - "create table tlb (a int);", - "ALTER TABLE `tbl` add column a int, add column b int ;", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleTableCharsetCheck() - if rule.Item != "TBL.005" { - t.Error("Rule not match:", rule.Item, "Expect : TBL.005") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleTableCharsetCheck() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.015 -func TestRuleBlobDefaultValue(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "CREATE TABLE `tb` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `c` blob NOT NULL DEFAULT '', PRIMARY KEY (`id`));", - "alter table `tb` add column `c` blob NOT NULL DEFAULT '';", - }, - { - "CREATE TABLE `tb` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `c` blob NOT NULL, PRIMARY KEY (`id`));", - "alter table `tb` add column `c` blob NOT NULL DEFAULT NULL;", - }, - } - - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleBlobDefaultValue() - if rule.Item != "COL.015" { - t.Error("Rule not match:", rule.Item, "Expect : COL.015") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleBlobDefaultValue() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.016 -func TestRuleIntPrecision(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "CREATE TABLE `tb` ( `id` int(1) );", - "CREATE TABLE `tb` ( `id` bigint(1) );", - "alter TABLE `tb` add column `id` bigint(1);", - "alter TABLE `tb` add column `id` int(1);", - }, - { - "CREATE TABLE `tb` ( `id` int(10));", - "CREATE TABLE `tb` ( `id` bigint(20));", - "alter TABLE `tb` add column `id` bigint(20);", - "alter TABLE `tb` add column `id` int(10);", - "CREATE TABLE `tb` ( `id` int);", - "alter TABLE `tb` add column `id` bigint;", - }, - } - - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleIntPrecision() - if rule.Item != "COL.016" { - t.Error("Rule not match:", rule.Item, "Expect : COL.016") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleIntPrecision() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.017 -func TestRuleVarcharLength(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "CREATE TABLE `tb` ( `id` varchar(4000) );", - "CREATE TABLE `tb` ( `id` varchar(3500) );", - "alter TABLE `tb` add column `id` varchar(3500);", - }, - { - "CREATE TABLE `tb` ( `id` varchar(1024));", - "CREATE TABLE `tb` ( `id` varchar(20));", - "alter TABLE `tb` add column `id` varchar(35);", - }, - } - - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleVarcharLength() - if rule.Item != "COL.017" { - t.Error("Rule not match:", rule.Item, "Expect : COL.017") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleVarcharLength() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// KEY.002 -func TestRuleNoOSCKey(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - // 正面的例子 - { - "CREATE TABLE tbl (a int, b int)", - }, - // 反面的例子 - { - "CREATE TABLE tbl (a int, primary key(`a`))", - "CREATE TABLE tbl (a int, unique key(`a`))", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleNoOSCKey() - if rule.Item != "KEY.002" { - t.Error("Rule not match:", rule.Item, "Expect : KEY.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleNoOSCKey() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.006 -func TestRuleTooManyFields(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "create table tbl (a int);", - } - - common.Config.MaxColCount = 0 - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleTooManyFields() - if rule.Item != "COL.006" { - t.Error("Rule not match:", rule.Item, "Expect : COL.006") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// TBL.002 -func TestRuleAllowEngine(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "CREATE TABLE tbl (a int) engine=MyISAM;", - "ALTER TABLE tbl engine=MyISAM;", - "CREATE TABLE tbl (a int);", - }, - { - "CREATE TABLE tbl (a int) engine = InnoDB;", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAllowEngine() - if rule.Item != "TBL.002" { - t.Error("Rule not match:", rule.Item, "Expect : TBL.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAllowEngine() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// TBL.001 -func TestRulePartitionNotAllowed(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE) PARTITION BY RANGE( YEAR(purchased) ) - ( - PARTITION p0 VALUES LESS THAN (1990), - PARTITION p1 VALUES LESS THAN (1995), - PARTITION p2 VALUES LESS THAN (2000), - PARTITION p3 VALUES LESS THAN (2005) - );`, - `ALTER TABLE t1 ADD PARTITION (PARTITION p3 VALUES LESS THAN (2002));`, - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RulePartitionNotAllowed() - if rule.Item != "TBL.001" { - t.Error("Rule not match:", rule.Item, "Expect : TBL.001") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// COL.003 -func TestRuleAutoIncUnsigned(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "CREATE TABLE `tb` ( `id` int(10) NOT NULL AUTO_INCREMENT, `c` longblob, PRIMARY KEY (`id`));", - "ALTER TABLE `tbl` ADD COLUMN `id` int(10) NOT NULL AUTO_INCREMENT;", - } - for _, sql := range sqls { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleAutoIncUnsigned() - if rule.Item != "COL.003" { - t.Error("Rule not match:", rule.Item, "Expect : COL.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// STA.003 -func TestRuleIdxPrefix(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "CREATE TABLE tbl (a int, unique key `xx_a` (`a`));", - "CREATE TABLE tbl (a int, key `xx_a` (`a`));", - `ALTER TABLE tbl ADD INDEX xx_a (a)`, - `ALTER TABLE tbl ADD UNIQUE INDEX xx_a (a)`, - }, - { - `ALTER TABLE tbl ADD INDEX idx_a (a)`, - `ALTER TABLE tbl ADD UNIQUE INDEX uk_a (a)`, - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleIdxPrefix() - if rule.Item != "STA.003" { - t.Error("Rule not match:", rule.Item, "Expect : STA.003") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleIdxPrefix() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// STA.004 -func TestRuleStandardName(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "CREATE TABLE `tbl-name` (a int);", - "CREATE TABLE `tbl `(a int)", - "CREATE TABLE t__bl (a int);", - }, - { - "CREATE TABLE tbl (a int)", - "CREATE TABLE `tbl`(a int)", - "CREATE TABLE `tbl` (a int) ENGINE=InnoDB DEFAULT CHARSET=utf8", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleStandardName() - if rule.Item != "STA.004" { - t.Error("Rule not match:", rule.Item, "Expect : STA.004") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleStandardName() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// STA.002 -func TestRuleSpaceAfterDot(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - "SELECT * FROM sakila. film", - "SELECT film. length FROM film", - }, - { - "SELECT * FROM sakila.film", - "SELECT film.length FROM film", - "SELECT * FROM t1, t2 WHERE t1.title = t2.title", - }, - } - for _, sql := range sqls[0] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSpaceAfterDot() - if rule.Item != "STA.002" { - t.Error("Rule not match:", rule.Item, "Expect : STA.002") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - - for _, sql := range sqls[1] { - q, err := NewQuery4Audit(sql) - if err == nil { - rule := q.RuleSpaceAfterDot() - if rule.Item != "OK" { - t.Error("Rule not match:", rule.Item, "Expect : OK") - } - } else { - t.Error("sqlparser.Parse Error:", err) - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -func TestRuleMySQLError(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - err := errors.New(`Received #1146 error from MySQL server: "can't xxxx"`) - if RuleMySQLError("ERR.002", err).Content != "" { - t.Error("Want: '', Bug get: ", err) - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -func TestMergeConflictHeuristicRules(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - tmpRules := make(map[string]Rule) - for item, val := range HeuristicRules { - tmpRules[item] = val - } - err := common.GoldenDiff(func() { - suggest := MergeConflictHeuristicRules(tmpRules) - var sortedSuggest []string - for item := range suggest { - sortedSuggest = append(sortedSuggest, item) - } - sort.Strings(sortedSuggest) - for _, item := range sortedSuggest { - pretty.Println(suggest[item]) - } - }, t.Name(), update) - if err != nil { - t.Error(err) - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} diff --git a/vendor/github.com/XiaoMi/soar/advisor/index.go b/vendor/github.com/XiaoMi/soar/advisor/index.go deleted file mode 100644 index 0c00818..0000000 --- a/vendor/github.com/XiaoMi/soar/advisor/index.go +++ /dev/null @@ -1,1130 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package advisor - -import ( - "fmt" - "strings" - - "github.com/XiaoMi/soar/ast" - "github.com/XiaoMi/soar/common" - "github.com/XiaoMi/soar/database" - "github.com/XiaoMi/soar/env" - - "github.com/dchest/uniuri" - "vitess.io/vitess/go/vt/sqlparser" -) - -const ( - // IndexNameMaxLength Ref. https://dev.mysql.com/doc/refman/8.0/en/identifiers.html - IndexNameMaxLength = 64 -) - -// IndexAdvisor 索引建议需要使用到的所有信息 -type IndexAdvisor struct { - vEnv *env.VirtualEnv // 线下虚拟测试环境(测试环境) - rEnv database.Connector // 线上真实环境 - Ast sqlparser.Statement // Vitess Parser生成的抽象语法树 - where []*common.Column // 所有where条件中用到的列 - whereEQ []*common.Column // where条件中可以加索引的等值条件列 - whereINEQ []*common.Column // where条件中可以加索引的非等值条件列 - groupBy []*common.Column // group by可以加索引列 - orderBy []*common.Column // order by可以加索引列 - joinCond [][]*common.Column // 由于join condition跨层级间索引不可共用,需要多一个维度用来维护层级关系 - IndexMeta map[string]map[string]*database.TableIndexInfo -} - -// IndexInfo 创建一条索引需要的信息 -type IndexInfo struct { - Name string `json:"name"` // 索引名称 - Database string `json:"database"` // 数据库名 - Table string `json:"table"` // 表名 - DDL string `json:"ddl"` // ALTER, CREATE 等类型的 DDL 语句 - ColumnDetails []*common.Column `json:"column_details"` // 列详情 -} - -// IndexAdvises IndexAdvises列表 -type IndexAdvises []IndexInfo - -// mergeAdvices 合并索引建议 -func mergeAdvices(dst []IndexInfo, src ...IndexInfo) IndexAdvises { - if len(src) == 0 { - return dst - } - - for _, newIdx := range src { - has := false - for _, idx := range dst { - if newIdx.DDL == idx.DDL { - common.Log.Debug("merge index %s and %s", idx.Name, newIdx.Name) - has = true - } - } - - if !has { - dst = append(dst, newIdx) - } - } - - return dst -} - -// NewAdvisor 构造一个 IndexAdvisor 的时候就会对其本身结构初始化 -// 获取 condition 中的等值条件、非等值条件,以及group by 、 order by信息 -func NewAdvisor(env *env.VirtualEnv, rEnv database.Connector, q Query4Audit) (*IndexAdvisor, error) { - common.Log.Debug("Enter: NewAdvisor(), Caller: %s", common.Caller()) - if common.Config.TestDSN.Disable { - return nil, fmt.Errorf("TestDSN is Disabled: %s", common.Config.TestDSN.Addr) - } - // DDL 检测 - switch stmt := q.Stmt.(type) { - case *sqlparser.DDL: - // 获取ast中用到的库表 - sqlMeta := ast.GetMeta(q.Stmt, nil) - for db := range sqlMeta { - dbRef := db - if db == "" { - dbRef = rEnv.Database - } - - // DDL 在 Env 初始化的时候已经执行过了 - if _, ok := env.TableMap[dbRef]; !ok { - env.TableMap[dbRef] = make(map[string]string) - } - - for _, tb := range sqlMeta[db].Table { - env.TableMap[dbRef][tb.TableName] = tb.TableName - } - } - - return nil, nil - - case *sqlparser.DBDDL: - // 忽略建库语句 - return nil, nil - - case *sqlparser.Use: - // 如果是use,切基础环境 - env.Database = env.DBHash(stmt.DBName.String()) - return nil, nil - } - - return &IndexAdvisor{ - vEnv: env, - rEnv: rEnv, - Ast: q.Stmt, - - // 所有的FindXXXXCols尽最大可能先排除不需要加索引的列,但由于元数据在此阶段尚未补齐,给出的列有可能也无法添加索引 - // 后续需要通过CompleteColumnsInfo + calcCardinality补全后再进一步判断 - joinCond: ast.FindJoinCols(q.Stmt), - whereEQ: ast.FindWhereEQ(q.Stmt), - whereINEQ: ast.FindWhereINEQ(q.Stmt), - groupBy: ast.FindGroupByCols(q.Stmt), - orderBy: ast.FindOrderByCols(q.Stmt), - where: ast.FindAllCols(q.Stmt, ast.WhereExpression), - IndexMeta: make(map[string]map[string]*database.TableIndexInfo), - }, nil -} - -/* - -关于如何添加索引: -在《Relational Database Index Design and the Optimizers》一书中,作者提出著名的的三星索引理论(Three-Star Index) - -To Qualify for the First Star: -Pick the columns from all equal predicates (WHERE COL = . . .). -Make these the first columns of the index—in any order. For CURSOR41, the three-star index will begin with -columns LNAME, CITY or CITY, LNAME. In both cases the index slice that must be scanned will be as thin as possible. - -To Qualify for the Second Star: -Add the ORDER BY columns. Do not change the order of these columns, but ignore columns that were already -picked in step 1. For example, if CURSOR41 had redundant columns in the ORDER BY, say ORDER BY LNAME, -FNAME or ORDER BY FNAME, CITY, only FNAME would be added in this step. When FNAME is the third index column, -the result table will be in the right order without sorting. The first FETCH call will return the row with -the smallest FNAME value. - -To Qualify for the Third Star: -Add all the remaining columns from the SELECT statement. The order of the columns added in this step -has no impact on the performance of the SELECT, but the cost of updates should be reduced by placing volatile -columns at the end. Now the index contains all the columns required for an index-only access path. - -索引添加算法正是以这个理想化索策略添为基础,尽可能的给予"三星"索引建议。 - -但又如《High Performance MySQL》一书中所说,索引并不总是最好的工具。只有当索引帮助存储引擎快速查找到记录带来的好处大于其 -带来的额外工作时,索引才是有效的。 - -因此,在三星索引理论的基础上引入启发式索引算法,在第二颗星的实现上做了部分改进,对于非等值条件只会添加散粒度最高的一列到索引中, -并基于总体列的使用情况作出判断,按需对order by、group by添加索引,由此来想`增强索引建议的通用性。 - -*/ - -// IndexAdvise 索引优化建议算法入口主函数 -// TODO 索引顺序该如何确定 -func (idxAdv *IndexAdvisor) IndexAdvise() IndexAdvises { - // 支持不依赖DB的索引建议分析 - if common.Config.TestDSN.Disable { - // 未开启Env原数据依赖,信息不全的情况下可能会给予错误的索引建议,请人工进行核查。 - common.Log.Warn("TestDSN.Disable = true") - } - - // 检查否是否含有子查询 - subQueries := ast.FindSubquery(0, idxAdv.Ast) - var subQueryAdvises []IndexInfo - // 含有子查询对子查询进行单独评审,子查询评审建议报错忽略 - if len(subQueries) > 0 { - for _, subSQL := range subQueries { - stmt, err := sqlparser.Parse(subSQL) - if err != nil { - continue - } - q := Query4Audit{ - Query: subSQL, - Stmt: stmt, - } - subIdxAdv, _ := NewAdvisor(idxAdv.vEnv, idxAdv.rEnv, q) - subQueryAdvises = append(subQueryAdvises, subIdxAdv.IndexAdvise()...) - } - } - - // 变量初始化,用于存放索引信息,按照db.tb.[cols]组织 - indexList := make(map[string]map[string][]*common.Column) - - // 为用到的每一列填充库名,表名等信息 - var joinCond [][]*common.Column - for _, joinCols := range idxAdv.joinCond { - joinCond = append(joinCond, CompleteColumnsInfo(idxAdv.Ast, joinCols, idxAdv.vEnv)) - } - idxAdv.joinCond = joinCond - - idxAdv.where = CompleteColumnsInfo(idxAdv.Ast, idxAdv.where, idxAdv.vEnv) - idxAdv.whereEQ = CompleteColumnsInfo(idxAdv.Ast, idxAdv.whereEQ, idxAdv.vEnv) - idxAdv.whereINEQ = CompleteColumnsInfo(idxAdv.Ast, idxAdv.whereINEQ, idxAdv.vEnv) - idxAdv.groupBy = CompleteColumnsInfo(idxAdv.Ast, idxAdv.groupBy, idxAdv.vEnv) - idxAdv.orderBy = CompleteColumnsInfo(idxAdv.Ast, idxAdv.orderBy, idxAdv.vEnv) - - // 只要在开启使用env元数据的时候才会计算散粒度 - if !common.Config.TestDSN.Disable { - // 计算joinCond, whereEQ, whereINEQ用到的每一列的散粒度,并排序,方便后续添加复合索引 - // groupBy, orderBy列按书写顺序给索引建议,不需要按散粒度排序 - idxAdv.calcCardinality(idxAdv.whereEQ) - idxAdv.calcCardinality(idxAdv.whereINEQ) - idxAdv.calcCardinality(idxAdv.orderBy) - idxAdv.calcCardinality(idxAdv.groupBy) - - for i, joinCols := range idxAdv.joinCond { - idxAdv.calcCardinality(joinCols) - joinCols = common.ColumnSort(joinCols) - idxAdv.joinCond[i] = joinCols - } - - // 根据散粒度进行排序 - // 对所有列进行排序,按散粒度由大到小排序 - idxAdv.whereEQ = common.ColumnSort(idxAdv.whereEQ) - idxAdv.whereINEQ = common.ColumnSort(idxAdv.whereINEQ) - idxAdv.orderBy = common.ColumnSort(idxAdv.orderBy) - idxAdv.groupBy = common.ColumnSort(idxAdv.groupBy) - - } - - // 是否指定Where条件,打标签 - hasWhere := false - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch where := node.(type) { - case *sqlparser.Subquery: - return false, nil - case *sqlparser.Where: - if where != nil { - hasWhere = true - } - } - return true, nil - }, idxAdv.Ast) - common.LogIfError(err, "") - // 获取哪些列被忽略 - var ignore []*common.Column - usedCols := append(idxAdv.whereINEQ, idxAdv.whereEQ...) - - for _, whereCol := range idxAdv.where { - isUsed := false - for _, used := range usedCols { - if whereCol.Equal(used) { - isUsed = true - } - } - - if !isUsed { - common.Log.Debug("column %s in `%s`.`%s` will ignore when adding index", whereCol.DB, whereCol.Table, whereCol.Name) - ignore = append(ignore, whereCol) - } - - } - - // 索引优化算法入口,从这里开始放大招 - if hasWhere { - // 有Where条件的先分析 等值条件 - for _, index := range idxAdv.whereEQ { - // 对应列在前面已经按散粒度由大到小排序好了 - mergeIndex(indexList, index) - } - // 若存在非等值查询条件,可以给第一个非等值条件添加索引 - if len(idxAdv.whereINEQ) > 0 { - mergeIndex(indexList, idxAdv.whereINEQ[0]) - } - // 有WHERE条件,但 WHERE 条件未能给出索引建议就不能再加 GROUP BY 和 ORDER BY 建议了 - if len(ignore) == 0 { - // 没有非等值查询条件时可以再为 GroupBy 和 OrderBy 添加索引 - for _, index := range idxAdv.groupBy { - mergeIndex(indexList, index) - } - - // OrderBy - // 没有 GroupBy 时可以为 OrderBy 加索引 - if len(idxAdv.groupBy) == 0 { - for _, index := range idxAdv.orderBy { - mergeIndex(indexList, index) - } - } - } - } else { - // 未指定 Where 条件的,只需要 GroupBy 和 OrderBy 的索引建议 - for _, index := range idxAdv.groupBy { - mergeIndex(indexList, index) - } - - // OrderBy - // 没有GroupBy 时可以为 OrderBy 加索引 - // 没有 where 条件时 OrderBy 的索引仅能够在索引覆盖的情况下被使用 - - // if len(idxAdv.groupBy) == 0 { - // for _, index := range idxAdv.orderBy { - // mergeIndex(indexList, index) - // } - // } - } - - // 开始整合索引信息,添加索引 - var indexes []IndexInfo - - // 为join添加索引 - // 获取 join condition 中需要加索引的表有哪些 - defaultDB := "" - if !common.Config.TestDSN.Disable { - defaultDB = idxAdv.vEnv.RealDB(idxAdv.vEnv.Database) - } - if !common.Config.OnlineDSN.Disable { - defaultDB = idxAdv.rEnv.Database - } - - // 根据join table的信息给予优化建议 - joinTableMeta := ast.FindJoinTable(idxAdv.Ast, nil).SetDefault(idxAdv.rEnv.Database).SetDefault(defaultDB) - indexes = mergeAdvices(indexes, idxAdv.buildJoinIndex(joinTableMeta)...) - - if common.Config.TestDSN.Disable || common.Config.OnlineDSN.Disable { - // 无 env 环境下只提供单列索引,无法确定 table 时不给予优化建议 - // 仅有 table 信息时给出的建议不包含 DB 信息 - indexes = mergeAdvices(indexes, idxAdv.buildIndexWithNoEnv(indexList)...) - } else { - // 给出尽可能详细的索引建议 - indexes = mergeAdvices(indexes, idxAdv.buildIndex(indexList)...) - } - - indexes = mergeAdvices(indexes, subQueryAdvises...) - - // 在开启 env 的情况下,检查数据库版本,字段类型,索引总长度 - indexes = idxAdv.idxColsTypeCheck(indexes) - - // 在开启 env 的情况下,会对索引进行检查,对全索引进行过滤 - // 在前几步都不会对 idx 生成 DDL 语句,DDL语句在这里生成 - return idxAdv.mergeIndexes(indexes) -} - -// idxColsTypeCheck 对超长的字段添加前缀索引,剔除无法添索引字段的列 -// TODO: 暂不支持 fulltext 索引, -func (idxAdv *IndexAdvisor) idxColsTypeCheck(idxList []IndexInfo) []IndexInfo { - if common.Config.TestDSN.Disable { - return rmSelfDupIndex(idxList) - } - - var indexes []IndexInfo - - for _, idx := range idxList { - var newCols []*common.Column - var newColInfo []string - // 索引总长度 - idxBytesTotal := 0 - isOverFlow := false - for _, col := range idx.ColumnDetails { - // 获取字段 bytes - bytes := col.GetDataBytes(common.Config.OnlineDSN.Version) - tmpCol := col.Name - overFlow := 0 - // 加上该列后是否索引长度过长 - if bytes < 0 { - // bytes < 0 说明字段的长度是无法计算的 - common.Log.Warning("%s.%s data type not support %s, can't add index", - col.Table, col.Name, col.DataType) - continue - } - - // idx bytes over flow - if total := idxBytesTotal + bytes; total > common.Config.MaxIdxBytes { - - common.Log.Debug("bytes: %d, idxBytesTotal: %d, total: %d, common.Config.MaxIdxBytes: %d", - bytes, idxBytesTotal, total, common.Config.MaxIdxBytes) - - overFlow = total - common.Config.MaxIdxBytes - isOverFlow = true - - } else { - idxBytesTotal = total - } - - // common.Config.MaxIdxColBytes 默认大小 767 - if bytes > common.Config.MaxIdxBytesPerColumn || isOverFlow { - // In 5.6, you may not include a column that equates to - // bigger than 767 bytes: VARCHAR(255) CHARACTER SET utf8 or VARCHAR(191) CHARACTER SET utf8mb4. - // In 5.7 you may not include a column that equates to - // bigger than 3072 bytes. - - // v : 在 col.Character 字符集下每个字符占用 v bytes - v, ok := common.CharSets[strings.ToLower(col.Character)] - if !ok { - // 找不到对应字符集,不添加索引 - // 如果出现不认识的字符集,认为每个字符占用4个字节 - common.Log.Warning("%s.%s(%s) charset not support yet %s, use default 4 bytes length", - col.Table, col.Name, col.DataType, col.Character) - v = 4 - } - - // 保留两个字节的安全余量 - length := (common.Config.MaxIdxBytesPerColumn - 2) / v - if isOverFlow { - // 在索引中添加该列会导致索引长度过长,建议根据需求转换为合理的前缀索引 - // _OPR_SPLIT_ 是自定的用于后续处理的特殊分隔符 - common.Log.Warning("adding index '%s(%s)' to table '%s' causes the index to be too long, overflow is %d", - col.Name, col.DataType, col.Table, overFlow) - tmpCol += fmt.Sprintf("_OPR_SPLIT_(N)") - } else { - // 索引没有过长,可以加一个最长的前缀索引 - common.Log.Warning("index column too large: %s.%s --> %s.%s(%d), data type: %s", - col.Table, col.Name, col.Table, tmpCol, length, col.DataType) - tmpCol += fmt.Sprintf("_OPR_SPLIT_(%d)", length) - } - - } - - newCols = append(newCols, col) - newColInfo = append(newColInfo, tmpCol) - } - - // 为新索引重建索引语句 - idxName := "idx_" - idxCols := "" - for i, newCol := range newColInfo { - // 对名称和可能存在的长度进行拼接 - // 用等号进行分割 - tmp := strings.Split(newCol, "_OPR_SPLIT_") - idxName += tmp[0] - if len(tmp) > 1 { - idxCols += tmp[0] + "`" + tmp[1] - } else { - idxCols += tmp[0] + "`" - } - - if i+1 < len(newColInfo) { - idxName += "_" - idxCols += ",`" - } - } - - // 索引名称最大长度64 - if len(idxName) > IndexNameMaxLength { - common.Log.Warn("index '%s' name large than IndexNameMaxLength", idxName) - idxName = strings.TrimRight(idxName[:IndexNameMaxLength], "_") - } - - // 新的alter语句 - newDDL := fmt.Sprintf("alter table `%s`.`%s` add index `%s` (`%s)", idxAdv.vEnv.RealDB(idx.Database), - idx.Table, idxName, idxCols) - - // 将筛选改造后的索引信息信息加入到新的索引列表中 - idx.ColumnDetails = newCols - idx.DDL = newDDL - indexes = append(indexes, idx) - } - - return indexes -} - -// mergeIndexes 与线上环境对比,将给出的索引建议进行去重 -func (idxAdv *IndexAdvisor) mergeIndexes(idxList []IndexInfo) []IndexInfo { - // TODO 暂不支持前缀索引去重 - if common.Config.TestDSN.Disable { - return rmSelfDupIndex(idxList) - } - - var indexes []IndexInfo - for _, idx := range idxList { - // 将 DB 替换成 vEnv 中的数据库名称 - dbInVEnv := idx.Database - if _, ok := idxAdv.vEnv.DBRef[idx.Database]; ok { - dbInVEnv = idxAdv.vEnv.DBRef[idx.Database] - } - - // 检测索引添加的表是否是视图 - if idxAdv.vEnv.IsView(idx.Table) { - common.Log.Info("%s.%s is a view. no need indexed", idx.Database, idx.Table) - continue - } - - // 检测是否存在重复索引 - indexMeta := idxAdv.IndexMeta[dbInVEnv][idx.Table] - isExisted := false - - // 检测无索引列的情况 - if len(idx.ColumnDetails) < 1 { - continue - } - - if existedIndexes := indexMeta.FindIndex(database.IndexColumnName, idx.ColumnDetails[0].Name); len(existedIndexes) > 0 { - for _, existedIdx := range existedIndexes { - // flag: 用于标记已存在的索引是否是约束条件 - isConstraint := false - - var cols []string - var colsDetail []*common.Column - - // 把已经存在的 key 摘出来遍历一遍对比是否是包含关系 - for _, col := range indexMeta.FindIndex(database.IndexKeyName, existedIdx.KeyName) { - cols = append(cols, col.ColumnName) - colsDetail = append(colsDetail, &common.Column{ - Name: col.ColumnName, - Table: idx.Table, - DB: idx.ColumnDetails[0].DB, - }) - } - - // 判断已存在的索引是否属于约束条件(唯一索引、主键) - // 这里可以忽略是否含有外键的情况,因为索引已经重复了,添加了新索引后原先重复的索引是可以删除的。 - if existedIdx.NonUnique == 0 { - common.Log.Debug("%s.%s表%s为约束条件", dbInVEnv, idx.Table, existedIdx.KeyName) - isConstraint = true - } - - // 如果已存在的索引与索引建议存在重叠,则说明无需添加新索引或可能需要给出删除索引的建议 - if common.IsColsPart(colsDetail, idx.ColumnDetails) { - idxName := existedIdx.KeyName - // 如果已经存在的索引包含需要添加的索引,则无需添加 - if len(colsDetail) >= len(idx.ColumnDetails) { - common.Log.Info(" `%s`.`%s` %s already had a index `%s`", - idx.Database, idx.Table, strings.Join(cols, ","), idxName) - isExisted = true - continue - } - - // 库、表、列名需要用反撇转义 - // TODO: 关于外键索引去重的优雅解决方案 - if !isConstraint { - if common.Config.AllowDropIndex { - alterSQL := fmt.Sprintf("alter table `%s`.`%s` drop index `%s`", idx.Database, idx.Table, idxName) - indexes = append(indexes, IndexInfo{ - Name: idxName, - Database: idx.Database, - Table: idx.Table, - DDL: alterSQL, - ColumnDetails: colsDetail, - }) - } else { - common.Log.Warning("In table `%s`, the new index of column `%s` contains index `%s`,"+ - " maybe you could drop one of them.", existedIdx.Table, - strings.Join(cols, ","), idxName) - } - } - } - } - } - - if !isExisted { - // 检测索引名称是否重复? - if existedIndexes := indexMeta.FindIndex(database.IndexKeyName, idx.Name); len(existedIndexes) > 0 { - var newName string - idxSuffix := getRandomIndexSuffix() - if len(idx.Name) < IndexNameMaxLength-len(idxSuffix) { - newName = idx.Name + idxSuffix - } else { - newName = idx.Name[:IndexNameMaxLength-len(idxSuffix)] + idxSuffix - } - - common.Log.Warning("duplicate index name '%s', new name is '%s'", idx.Name, newName) - idx.DDL = strings.Replace(idx.DDL, idx.Name, newName, -1) - idx.Name = newName - } - - // 添加合并 - indexes = mergeAdvices(indexes, idx) - } - - } - - // 对索引进行去重 - return rmSelfDupIndex(indexes) -} - -// getRandomIndexSuffix format: _xxxx, length: 5 -func getRandomIndexSuffix() string { - return fmt.Sprintf("_%s", uniuri.New()[:4]) -} - -// rmSelfDupIndex 去重传入的[]IndexInfo中重复的索引 -func rmSelfDupIndex(indexes []IndexInfo) []IndexInfo { - var resultIndex []IndexInfo - tmpIndexList := indexes - for _, a := range indexes { - tmp := a - for i, b := range tmpIndexList { - if common.IsColsPart(tmp.ColumnDetails, b.ColumnDetails) && tmp.Name != b.Name { - if len(b.ColumnDetails) > len(tmp.ColumnDetails) { - common.Log.Debug("remove duplicate index: %s", tmp.Name) - tmp = b - } - - if i < len(tmpIndexList) { - tmpIndexList = append(tmpIndexList[:i], tmpIndexList[i+1:]...) - } else { - tmpIndexList = tmpIndexList[:i] - } - - } - } - resultIndex = mergeAdvices(resultIndex, tmp) - } - - return resultIndex -} - -// buildJoinIndex 检查Join中使用的库表是否需要添加索引并给予索引建议 -func (idxAdv *IndexAdvisor) buildJoinIndex(meta common.Meta) []IndexInfo { - var indexes []IndexInfo - for _, IndexCols := range idxAdv.joinCond { - // 如果该列的库表为join condition中需要添加索引的库表 - indexColsList := make(map[string]map[string][]*common.Column) - for _, col := range IndexCols { - mergeIndex(indexColsList, col) - - } - - if common.Config.TestDSN.Disable || common.Config.OnlineDSN.Disable { - indexes = mergeAdvices(indexes, idxAdv.buildIndexWithNoEnv(indexColsList)...) - continue - } - - indexes = mergeAdvices(indexes, idxAdv.buildIndex(indexColsList)...) - } - return indexes -} - -// buildIndex 尽可能的将 map[string]map[string][]*common.Column 转换成 []IndexInfo -// 此处不判断索引是否重复 -func (idxAdv *IndexAdvisor) buildIndex(idxList map[string]map[string][]*common.Column) []IndexInfo { - var indexes []IndexInfo - for db, tbs := range idxList { - for tb, cols := range tbs { - - // 单个索引中含有的列收 config 中参数限制 - if len(cols) > common.Config.MaxIdxColsCount { - cols = cols[:common.Config.MaxIdxColsCount] - } - - var colNames []string - for _, col := range cols { - if col.DB == "" || col.Table == "" { - common.Log.Warn("can not get the meta info of column '%s'", col.Name) - continue - } - colNames = append(colNames, col.Name) - } - - if len(colNames) == 0 { - continue - } - - idxName := "idx_" + strings.Join(colNames, "_") - - // 索引名称最大长度64 - if len(idxName) > IndexNameMaxLength { - common.Log.Warn("index '%s' name large than IndexNameMaxLength", idxName) - idxName = strings.TrimRight(idxName[:IndexNameMaxLength], "_") - } - - alterSQL := fmt.Sprintf("alter table `%s`.`%s` add index `%s` (`%s`)", idxAdv.vEnv.RealDB(db), tb, - idxName, strings.Join(colNames, "`,`")) - - indexes = append(indexes, IndexInfo{ - Name: idxName, - Database: idxAdv.vEnv.RealDB(db), - Table: tb, - DDL: alterSQL, - ColumnDetails: cols, - }) - } - } - return indexes -} - -// buildIndexWithNoEnv 忽略原数据,给予最基础的索引 -func (idxAdv *IndexAdvisor) buildIndexWithNoEnv(indexList map[string]map[string][]*common.Column) []IndexInfo { - // 如果不获取数据库原信息,则不去判断索引是否重复,且只给单列加索引 - var indexes []IndexInfo - for _, tableIndex := range indexList { - for _, indexCols := range tableIndex { - for _, col := range indexCols { - if col.Table == "" { - common.Log.Warn("can not get the meta info of column '%s'", col.Name) - continue - } - idxName := "idx_" + col.Name - // 库、表、列名需要用反撇转义 - alterSQL := fmt.Sprintf("alter table `%s`.`%s` add index `%s` (`%s`)", idxAdv.vEnv.RealDB(col.DB), col.Table, idxName, col.Name) - if col.DB == "" { - alterSQL = fmt.Sprintf("alter table `%s` add index `%s` (`%s`)", col.Table, idxName, col.Name) - } - - indexes = append(indexes, IndexInfo{ - Name: idxName, - Database: idxAdv.vEnv.RealDB(col.DB), - Table: col.Table, - DDL: alterSQL, - ColumnDetails: []*common.Column{col}, - }) - } - - } - } - return indexes -} - -// mergeIndex 将索引用到的列去重后合并到一起 -func mergeIndex(idxList map[string]map[string][]*common.Column, column *common.Column) { - db := column.DB - tb := column.Table - if idxList[db] == nil { - idxList[db] = make(map[string][]*common.Column) - } - if idxList[db][tb] == nil { - idxList[db][tb] = make([]*common.Column, 0) - } - - // 去除重复列Append - exist := false - for _, cl := range idxList[db][tb] { - if cl.Name == column.Name { - exist = true - } - } - if !exist { - idxList[db][tb] = append(idxList[db][tb], column) - } -} - -// CompleteColumnsInfo 补全索引可能会用到列的所属库名、表名等信息 -func CompleteColumnsInfo(stmt sqlparser.Statement, cols []*common.Column, env *env.VirtualEnv) []*common.Column { - // 如果传过来的列是空的,没必要跑逻辑 - if len(cols) == 0 { - return cols - } - - // 从 Ast 中拿到 DBStructure,包含所有表的相关信息 - dbs := ast.GetMeta(stmt, nil) - - // 此处生成的 meta 信息中不应该含有""db的信息,若 DB 为空则认为是已传入的 db 为默认 db 并进行信息补全 - // BUG Fix: - // 修补 dbs 中空 DB 的导致后续补全列信息时无法获取正确 table 名称的问题 - if _, ok := dbs[""]; ok { - dbs[env.Database] = dbs[""] - delete(dbs, "") - } - - tableCount := 0 - for db := range dbs { - for tb := range dbs[db].Table { - if tb != "" { - tableCount++ - } - } - } - - var noEnvTmp []*common.Column - for _, col := range cols { - for db := range dbs { - // 对每一列进行比对,将别名转换为正确的名称 - find := false - for _, tb := range dbs[db].Table { - for _, tbAlias := range tb.TableAliases { - if col.Table != "" && col.Table == tbAlias { - common.Log.Debug("column '%s' prefix change: %s --> %s", col.Name, col.Table, tb.TableName) - find = true - col.Table = tb.TableName - col.DB = db - break - } - } - if find { - break - } - - } - - // 如果不依赖env环境,利用ast中包含的信息推理列的库表信息 - if common.Config.TestDSN.Disable { - if tableCount == 1 { - for _, tb := range dbs[db].Table { - col.Table = tb.TableName - - // 因为tableMeta是按照库表组织的树状结构,db变量贯穿全局 - // 只有在最终赋值前才能根据逻辑变更补全 - if db == "" { - db = env.Database - } - col.DB = db - } - } - - // 如果SQL中含有的表大于一个,则使用的列中必须含有前缀,不然无法判断该列属于哪个表 - // 如果某一列未含有前缀信息,则认为两张表中都含有该列,需要由人去判断 - if tableCount > 1 { - if col.Table == "" { - for _, tb := range dbs[db].Table { - if tb.TableName == "" { - common.Log.Warn("can not get the meta info of column '%s'", col.Name) - } - - if db == "" { - db = env.RealDB(env.Database) - } - col.Table = tb.TableName - col.DB = db - - tmp := *col - tmp.Table = tb.TableName - tmp.DB = db - - noEnvTmp = append(noEnvTmp, &tmp) - } - } - - if col.DB == "" { - if db == "" { - db = env.Database - } - col.DB = db - } - } - - break - } - - // 将已经获取到正确表信息的列信息带入到env中,利用show columns where table 获取库表信息 - // 此出会传入之前从ast中,该 db 下获取的所有表来作为where限定条件, - // 防止与SQL无关的库表信息干扰准确性 - // 此处传入的是测试环境,DB 是经过变换的,所以在寻找列名的时候需要将 DB 名称转换成测试环境中经过 hash 的 DB 名称 - // 不然会找不到col的信息 - realCols, err := env.FindColumn(col.Name, env.DBHash(db), dbs.Tables(db)...) - if err != nil { - common.Log.Warn("%v", err) - continue - } - - // 对比 column 信息中的表名与从 env 中获取的库表名的一致性 - for _, realCol := range realCols { - if col.Name == realCol.Name { - // 如果查询到了列名一致,但从 ast 中获取的列的前缀与 env 中的表信息不符 - // 1.存在一个同名列,但不同表,该情况下忽略 - // 2.存在一个未正确转换的别名(如表名为),该情况下修正,大概率是正确的 - if col.Table != "" && col.Table != realCol.Table { - has, _ := env.FindColumn(col.Name, env.DBHash(db), col.Table) - if len(has) > 0 { - realCol = has[0] - } - } - - col.DataType = realCol.DataType - col.Table = realCol.Table - col.DB = env.RealDB(realCol.DB) - col.Character = realCol.Character - col.Collation = realCol.Collation - - } - } - } - - } - - // 如果不依赖env环境,将可能存在的列也加入到索引预处理列表中 - if common.Config.TestDSN.Disable { - cols = append(cols, noEnvTmp...) - } - - return cols -} - -// calcCardinality 计算每一列的散粒度 -// 这个函数需要在补全列的库表信息之后再调用,否则无法确定要计算列的归属 -func (idxAdv *IndexAdvisor) calcCardinality(cols []*common.Column) []*common.Column { - common.Log.Debug("Enter: calcCardinality(), Caller: %s", common.Caller()) - tmpDB := *idxAdv.vEnv - for _, col := range cols { - // 补全对应列的库->表->索引信息到IndexMeta - // 这将在后面用于判断某一列是否为主键或单列唯一索引,快速返回散粒度 - if col.DB == "" { - col.DB = idxAdv.vEnv.Database - } - realDB := idxAdv.vEnv.DBHash(col.DB) - if idxAdv.IndexMeta[realDB] == nil { - idxAdv.IndexMeta[realDB] = make(map[string]*database.TableIndexInfo) - } - - if idxAdv.IndexMeta[realDB][col.Table] == nil { - tmpDB.Database = realDB - indexInfo, err := tmpDB.ShowIndex(col.Table) - if err != nil { - // 如果是不存在的表就会报错,报错的可能性有三个: - // 1.数据库错误 2.表不存在 3.临时表 - // 而这三种错误都是不需要在这一层关注的,直接跳过 - common.Log.Debug("calcCardinality error: %v", err) - continue - } - - // 将获取的索引信息以db.tb 维度组织到 IndexMeta 中 - idxAdv.IndexMeta[realDB][col.Table] = indexInfo - } - - // 检查对应列是否为主键或单列唯一索引,如果满足直接返回1,不再重复计算,提高效率 - // 多列复合唯一索引不能跳过计算,单列普通索引不能跳过计算 - for _, index := range idxAdv.IndexMeta[realDB][col.Table].IdxRows { - // 根据索引的名称判断该索引包含的列数,列数大于1即为复合索引 - columnCount := len(idxAdv.IndexMeta[realDB][col.Table].FindIndex(database.IndexKeyName, index.KeyName)) - if col.Name == index.ColumnName { - // 主键、唯一键 无需计算散粒度 - if (index.KeyName == "PRIMARY" || index.NonUnique == 0) && columnCount == 1 { - common.Log.Debug("column '%s' is PK or UK, no need to calculate cardinality.", col.Name) - col.Cardinality = 1 - break - } - } - - } - - // 给非 PRIMARY、UNIQUE 的列计算散粒度 - if col.Cardinality != 1 { - col.Cardinality = idxAdv.vEnv.ColumnCardinality(col.Table, col.Name) - } - } - - return cols -} - -// Format 用于格式化输出索引建议 -func (idxAdvs IndexAdvises) Format() map[string]Rule { - rulesMap := make(map[string]Rule) - number := 1 - rules := make(map[string]*Rule) - sqls := make(map[string][]string) - - for _, advise := range idxAdvs { - advKey := advise.Database + advise.Table - - if _, ok := sqls[advKey]; !ok { - sqls[advKey] = make([]string, 0) - } - - sqls[advKey] = append(sqls[advKey], advise.DDL) - - if _, ok := rules[advKey]; !ok { - summary := fmt.Sprintf("为%s库的%s表添加索引", advise.Database, advise.Table) - if advise.Database == "" { - summary = fmt.Sprintf("为%s表添加索引", advise.Table) - } - - rules[advKey] = &Rule{ - Summary: summary, - Content: "", - Severity: "L2", - } - } - - for _, col := range advise.ColumnDetails { - // 为了更好地显示效果 - if common.Config.Sampling { - cardinal := fmt.Sprintf("%0.2f", col.Cardinality*100) - if cardinal != "0.00" { - rules[advKey].Content += fmt.Sprintf("为列%s添加索引,散粒度为: %s%%; ", - col.Name, cardinal) - } - } else { - rules[advKey].Content += fmt.Sprintf("为列%s添加索引;", col.Name) - } - } - // 清理多余的标点 - rules[advKey].Content = strings.Trim(rules[advKey].Content, common.Config.Delimiter) - } - - for adv := range rules { - key := fmt.Sprintf("IDX.%03d", number) - ddl := ast.MergeAlterTables(sqls[adv]...) - // 由于传入合并的SQL都是一张表的,所以一定只会输出一条ddl语句 - for _, v := range ddl { - rules[adv].Case = v - } - - // set item - rules[adv].Item = key - - rulesMap[key] = *rules[adv] - - number++ - } - - return rulesMap -} - -// HeuristicCheck 依赖数据字典的启发式检查 -// IndexAdvisor会构建测试环境和数据字典,所以放在这里实现 -func (idxAdv *IndexAdvisor) HeuristicCheck(q Query4Audit) map[string]Rule { - var rule Rule - heuristicSuggest := make(map[string]Rule) - if common.Config.OnlineDSN.Disable && common.Config.TestDSN.Disable { - return heuristicSuggest - } - - ruleFuncs := []func(*IndexAdvisor) Rule{ - (*IndexAdvisor).RuleImplicitConversion, // ARG.003 - // (*IndexAdvisor).RuleImpossibleOuterJoin, // TODO: JOI.003, JOI.004 - (*IndexAdvisor).RuleGroupByConst, // CLA.004 - (*IndexAdvisor).RuleOrderByConst, // CLA.005 - (*IndexAdvisor).RuleUpdatePrimaryKey, // CLA.016 - } - - for _, f := range ruleFuncs { - rule = f(idxAdv) - if rule.Item != "OK" { - heuristicSuggest[rule.Item] = rule - } - } - return heuristicSuggest -} - -// DuplicateKeyChecker 对所有用到的库表检查是否存在重复索引 -func DuplicateKeyChecker(conn *database.Connector, databases ...string) map[string]Rule { - common.Log.Debug("Enter: DuplicateKeyChecker, Caller: %s", common.Caller()) - // 复制一份online connector,防止环境切换影响其他功能的使用 - tmpOnline := *conn - ruleMap := make(map[string]Rule) - number := 1 - - // 错误处理,用于汇总所有的错误 - funcErrCheck := func(err error) { - if err != nil { - if sug, ok := ruleMap["ERR.003"]; ok { - sug.Content += fmt.Sprintf("; %s", err.Error()) - } else { - ruleMap["ERR.003"] = Rule{ - Item: "ERR.003", - Severity: "L8", - Content: err.Error(), - } - } - } - } - - // 不指定 DB 的时候检查 online dsn 中的 DB - if len(databases) == 0 { - databases = append(databases, tmpOnline.Database) - } - - for _, db := range databases { - // 获取所有的表 - tmpOnline.Database = db - tables, err := tmpOnline.ShowTables() - - if err != nil { - funcErrCheck(err) - if !common.Config.DryRun { - return ruleMap - } - } - - for _, tb := range tables { - // 获取表中所有的索引 - idxMap := make(map[string][]*common.Column) - idxInfo, err := tmpOnline.ShowIndex(tb) - if err != nil { - funcErrCheck(err) - if !common.Config.DryRun { - return ruleMap - } - } - - // 枚举所有的索引信息,提取用到的列 - for _, idx := range idxInfo.IdxRows { - if _, ok := idxMap[idx.KeyName]; !ok { - idxMap[idx.KeyName] = make([]*common.Column, 0) - for _, col := range idxInfo.FindIndex(database.IndexKeyName, idx.KeyName) { - idxMap[idx.KeyName] = append(idxMap[idx.KeyName], &common.Column{ - Name: col.ColumnName, - Table: tb, - DB: db, - }) - } - } - } - - // 对索引进行重复检查 - hasDup := false - content := "" - - for k1, cl1 := range idxMap { - for k2, cl2 := range idxMap { - if k1 != k2 && common.IsColsPart(cl1, cl2) { - hasDup = true - col1Str := common.JoinColumnsName(cl1, ", ") - col2Str := common.JoinColumnsName(cl2, ", ") - content += fmt.Sprintf("索引%s(%s)与%s(%s)重复;", k1, col1Str, k2, col2Str) - common.Log.Debug(" %s.%s has duplicate index %s(%s) <--> %s(%s)", db, tb, k1, col1Str, k2, col2Str) - } - } - delete(idxMap, k1) - } - - // TODO 重复索引检查添加对约束及索引的判断,提供重复索引的删除功能 - if hasDup { - tmpOnline.Database = db - ddl, _ := tmpOnline.ShowCreateTable(tb) - key := fmt.Sprintf("IDX.%03d", number) - ruleMap[key] = Rule{ - Item: key, - Severity: "L2", - Summary: fmt.Sprintf("%s.%s存在重复的索引", db, tb), - Content: content, - Case: ddl, - } - number++ - } - } - } - - return ruleMap -} diff --git a/vendor/github.com/XiaoMi/soar/advisor/index_test.go b/vendor/github.com/XiaoMi/soar/advisor/index_test.go deleted file mode 100644 index 314655f..0000000 --- a/vendor/github.com/XiaoMi/soar/advisor/index_test.go +++ /dev/null @@ -1,476 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package advisor - -import ( - "fmt" - "os" - "strings" - "testing" - - "github.com/XiaoMi/soar/common" - "github.com/XiaoMi/soar/env" - - "github.com/kr/pretty" - "vitess.io/vitess/go/vt/sqlparser" -) - -func init() { - common.BaseDir = common.DevPath - err := common.ParseConfig("") - if err != nil { - fmt.Println(err.Error()) - } - vEnv, rEnv := env.BuildEnv() - if _, err = vEnv.Version(); err != nil { - fmt.Println(err.Error(), ", By pass all advisor test cases") - os.Exit(0) - } - - if _, err := rEnv.Version(); err != nil { - fmt.Println(err.Error(), ", By pass all advisor test cases") - os.Exit(0) - } -} - -// ARG.003 -func TestRuleImplicitConversion(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - dsn := common.Config.OnlineDSN - common.Config.OnlineDSN = common.Config.TestDSN - vEnv, rEnv := env.BuildEnv() - defer vEnv.CleanUp() - - initSQLs := []string{ - `CREATE TABLE t1 (id int, title varchar(255) CHARSET utf8 COLLATE utf8_general_ci);`, - `CREATE TABLE t2 (id int, title varchar(255) CHARSET utf8mb4);`, - `CREATE TABLE t3 (id int, title varchar(255) CHARSET utf8 COLLATE utf8_bin);`, - } - for _, sql := range initSQLs { - vEnv.BuildVirtualEnv(rEnv, sql) - } - - sqls := []string{ - "SELECT * FROM t1 WHERE title >= 60;", - "SELECT * FROM t1, t2 WHERE t1.title = t2.title;", - "SELECT * FROM t1, t3 WHERE t1.title = t3.title;", - "SELECT * FROM t1 WHERE title in (60, '60');", - "SELECT * FROM t1 WHERE title in (60);", - } - for _, sql := range sqls { - stmt, syntaxErr := sqlparser.Parse(sql) - if syntaxErr != nil { - common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) - } - - q := &Query4Audit{Query: sql, Stmt: stmt} - - idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) - if err != nil { - t.Error("NewAdvisor Error: ", err, "SQL: ", sql) - } - - if idxAdvisor != nil { - rule := idxAdvisor.RuleImplicitConversion() - if rule.Item != "ARG.003" { - t.Error("Rule not match:", rule, "Expect : ARG.003, SQL:", sql) - } - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) - common.Config.OnlineDSN = dsn -} - -// JOI.003 & JOI.004 -func TestRuleImpossibleOuterJoin(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select city_id, city, country from city left outer join country using(country_id) WHERE city.city_id=59 and country.country='Algeria'`, - `select city_id, city, country from city left outer join country using(country_id) WHERE country.country='Algeria'`, - `select city_id, city, country from city left outer join country on city.country_id=country.country_id WHERE city.city_id IS NULL`, - } - - vEnv, rEnv := env.BuildEnv() - defer vEnv.CleanUp() - - for _, sql := range sqls { - stmt, syntaxErr := sqlparser.Parse(sql) - if syntaxErr != nil { - common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) - } - - q := &Query4Audit{Query: sql, Stmt: stmt} - - if vEnv.BuildVirtualEnv(rEnv, q.Query) { - idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) - if err != nil { - t.Error("NewAdvisor Error: ", err, "SQL: ", sql) - } - - if idxAdvisor != nil { - rule := idxAdvisor.RuleImpossibleOuterJoin() - if rule.Item != "JOI.003" && rule.Item != "JOI.004" { - t.Error("Rule not match:", rule, "Expect : JOI.003 || JOI.004") - } - } - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// GRP.001 -func TestIndexAdvisorRuleGroupByConst(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `select film_id, title from film where release_year='2006' group by release_year`, - `select film_id, title from film where release_year in ('2006') group by release_year`, - }, - { - // 反面的例子 - `select film_id, title from film where release_year in ('2006', '2007') group by release_year`, - }, - } - - vEnv, rEnv := env.BuildEnv() - defer vEnv.CleanUp() - - for _, sql := range sqls[0] { - stmt, syntaxErr := sqlparser.Parse(sql) - if syntaxErr != nil { - common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) - } - - q := &Query4Audit{Query: sql, Stmt: stmt} - - if vEnv.BuildVirtualEnv(rEnv, q.Query) { - idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) - if err != nil { - t.Error("NewAdvisor Error: ", err, "SQL: ", sql) - } - - if idxAdvisor != nil { - rule := idxAdvisor.RuleGroupByConst() - if rule.Item != "GRP.001" { - t.Error("Rule not match:", rule, "Expect : GRP.001") - } - } - } - } - - for _, sql := range sqls[1] { - stmt, syntaxErr := sqlparser.Parse(sql) - if syntaxErr != nil { - common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) - } - - q := &Query4Audit{Query: sql, Stmt: stmt} - - if vEnv.BuildVirtualEnv(rEnv, q.Query) { - idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) - if err != nil { - t.Error("NewAdvisor Error: ", err, "SQL: ", sql) - } - - if idxAdvisor != nil { - rule := idxAdvisor.RuleGroupByConst() - if rule.Item != "OK" { - t.Error("Rule not match:", rule, "Expect : OK") - } - } - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.005 -func TestIndexAdvisorRuleOrderByConst(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `select film_id, title from film where release_year='2006' order by release_year;`, - `select film_id, title from film where release_year in ('2006') order by release_year;`, - }, - { - // 反面的例子 - `select film_id, title from film where release_year in ('2006', '2007') order by release_year;`, - }, - } - - vEnv, rEnv := env.BuildEnv() - defer vEnv.CleanUp() - - for _, sql := range sqls[0] { - stmt, syntaxErr := sqlparser.Parse(sql) - if syntaxErr != nil { - common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) - } - - q := &Query4Audit{Query: sql, Stmt: stmt} - - if vEnv.BuildVirtualEnv(rEnv, q.Query) { - idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) - if err != nil { - t.Error("NewAdvisor Error: ", err, "SQL: ", sql) - } - - if idxAdvisor != nil { - rule := idxAdvisor.RuleOrderByConst() - if rule.Item != "CLA.005" { - t.Error("Rule not match:", rule, "Expect : CLA.005") - } - } - } - } - - for _, sql := range sqls[1] { - stmt, syntaxErr := sqlparser.Parse(sql) - if syntaxErr != nil { - common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) - } - - q := &Query4Audit{Query: sql, Stmt: stmt} - - if vEnv.BuildVirtualEnv(rEnv, q.Query) { - idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) - if err != nil { - t.Error("NewAdvisor Error: ", err, "SQL: ", sql) - } - - if idxAdvisor != nil { - rule := idxAdvisor.RuleOrderByConst() - if rule.Item != "OK" { - t.Error("Rule not match:", rule, "Expect : OK") - } - } - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -// CLA.016 -func TestRuleUpdatePrimaryKey(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := [][]string{ - { - `update film set film_id = 1 where title='a';`, - }, - { - // 反面的例子 - `select film_id, title from film where release_year in ('2006', '2007') order by release_year;`, - }, - } - - vEnv, rEnv := env.BuildEnv() - defer vEnv.CleanUp() - - for _, sql := range sqls[0] { - stmt, syntaxErr := sqlparser.Parse(sql) - if syntaxErr != nil { - common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) - } - - q := &Query4Audit{Query: sql, Stmt: stmt} - - if vEnv.BuildVirtualEnv(rEnv, q.Query) { - idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) - if err != nil { - t.Error("NewAdvisor Error: ", err, "SQL: ", sql) - } - - if idxAdvisor != nil { - rule := idxAdvisor.RuleUpdatePrimaryKey() - if rule.Item != "CLA.016" { - t.Error("Rule not match:", rule.Item, "Expect : CLA.016") - } - } - } - } - - for _, sql := range sqls[1] { - stmt, syntaxErr := sqlparser.Parse(sql) - if syntaxErr != nil { - common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) - } - - q := &Query4Audit{Query: sql, Stmt: stmt} - - if vEnv.BuildVirtualEnv(rEnv, q.Query) { - idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) - if err != nil { - t.Error("NewAdvisor Error: ", err, "SQL: ", sql) - } - - if idxAdvisor != nil { - rule := idxAdvisor.RuleUpdatePrimaryKey() - if rule.Item != "OK" { - t.Error("Rule not match:", rule, "Expect : OK") - } - } - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -func TestIndexAdvise(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - vEnv, rEnv := env.BuildEnv() - defer vEnv.CleanUp() - - for _, sql := range common.TestSQLs { - stmt, syntaxErr := sqlparser.Parse(sql) - if syntaxErr != nil { - common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) - } - - q := &Query4Audit{Query: sql, Stmt: stmt} - - if vEnv.BuildVirtualEnv(rEnv, q.Query) { - idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) - if err != nil { - t.Error("NewAdvisor Error: ", err, "SQL: ", sql) - } - - if idxAdvisor != nil { - rule := idxAdvisor.IndexAdvise().Format() - if len(rule) > 0 { - pretty.Println(rule) - } - } - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -func TestIndexAdviseNoEnv(t *testing.T) { - common.Config.OnlineDSN.Disable = true - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - vEnv, rEnv := env.BuildEnv() - defer vEnv.CleanUp() - - for _, sql := range common.TestSQLs { - stmt, syntaxErr := sqlparser.Parse(sql) - if syntaxErr != nil { - common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) - } - - q := &Query4Audit{Query: sql, Stmt: stmt} - - if vEnv.BuildVirtualEnv(rEnv, q.Query) { - idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) - if err != nil { - t.Error("NewAdvisor Error: ", err, "SQL: ", sql) - } - - if idxAdvisor != nil { - rule := idxAdvisor.IndexAdvise().Format() - if len(rule) > 0 { - pretty.Println(rule) - } - } - } - } - common.Log.Debug("Exiting function: %s", common.GetFunctionName()) -} - -func TestDuplicateKeyChecker(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - _, rEnv := env.BuildEnv() - rule := DuplicateKeyChecker(rEnv, "sakila") - if len(rule) != 0 { - t.Errorf("got rules: %s", pretty.Sprint(rule)) - } -} - -func TestMergeAdvices(t *testing.T) { - dst := []IndexInfo{ - { - Name: "test", - Database: "db", - Table: "tb", - ColumnDetails: []*common.Column{ - { - Name: "test", - }, - }, - }, - } - - src := dst[0] - - advise := mergeAdvices(dst, src) - if len(advise) != 1 { - t.Error(pretty.Sprint(advise)) - } -} - -func TestIdxColsTypeCheck(t *testing.T) { - common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - `select city_id, city, country from city left outer join country using(country_id) WHERE city.city_id=59 and country.country='Algeria'`, - } - - vEnv, rEnv := env.BuildEnv() - defer vEnv.CleanUp() - - for _, sql := range sqls { - stmt, syntaxErr := sqlparser.Parse(sql) - if syntaxErr != nil { - common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) - } - - q := &Query4Audit{Query: sql, Stmt: stmt} - - if vEnv.BuildVirtualEnv(rEnv, q.Query) { - idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) - if err != nil { - t.Error("NewAdvisor Error: ", err, "SQL: ", sql) - } - - idxList := []IndexInfo{ - { - Name: "idx_fk_country_id", - Database: "sakila", - Table: "city", - ColumnDetails: []*common.Column{ - { - Name: "country_id", - Character: "utf8", - DataType: "varchar(3000)", - }, - }, - }, - } - - if idxAdvisor != nil { - rule := idxAdvisor.idxColsTypeCheck(idxList) - if !(len(rule) > 0 && rule[0].DDL == "alter table `sakila`.`city` add index `idx_country_id` (`country_id`(N))") { - t.Error(pretty.Sprint(rule)) - } - } - } - } -} - -func TestGetRandomIndexSuffix(t *testing.T) { - for i := 0; i < 5; i++ { - r := getRandomIndexSuffix() - if !(strings.HasPrefix(r, "_") && len(r) == 5) { - t.Errorf("getRandomIndexSuffix should return a string with prefix `_` and 5 length, but got:%s", r) - } - } -} diff --git a/vendor/github.com/XiaoMi/soar/advisor/rules.go b/vendor/github.com/XiaoMi/soar/advisor/rules.go deleted file mode 100644 index de92536..0000000 --- a/vendor/github.com/XiaoMi/soar/advisor/rules.go +++ /dev/null @@ -1,1426 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package advisor - -import ( - "encoding/json" - "fmt" - "regexp" - "sort" - "strconv" - "strings" - - "github.com/XiaoMi/soar/ast" - "github.com/XiaoMi/soar/common" - - "github.com/kr/pretty" - "github.com/percona/go-mysql/query" - tidb "github.com/pingcap/parser/ast" - "vitess.io/vitess/go/vt/sqlparser" -) - -// Query4Audit 待评审的SQL结构体,由原SQL和其对应的抽象语法树组成 -type Query4Audit struct { - Query string // 查询语句 - Stmt sqlparser.Statement // 通过Vitess解析出的抽象语法树 - TiStmt []tidb.StmtNode // 通过TiDB解析出的抽象语法树 -} - -// NewQuery4Audit return a struct for Query4Audit -func NewQuery4Audit(sql string, options ...string) (*Query4Audit, error) { - var err, vErr error - var charset string - var collation string - - if len(options) > 0 { - charset = options[0] - } - - if len(options) > 1 { - collation = options[1] - } - - q := &Query4Audit{Query: sql} - // vitess 语法解析不上报,以 tidb parser 为主 - q.Stmt, vErr = sqlparser.Parse(sql) - if vErr != nil { - common.Log.Warn("NewQuery4Audit vitess parse Error: %s", vErr.Error()) - } - - // TODO: charset, collation - // tdib parser 语法解析 - q.TiStmt, err = ast.TiParse(sql, charset, collation) - return q, err -} - -// Rule 评审规则元数据结构 -type Rule struct { - Item string `json:"Item"` // 规则代号 - Severity string `json:"Severity"` // 危险等级:L[0-8], 数字越大表示级别越高 - Summary string `json:"Summary"` // 规则摘要 - Content string `json:"Content"` // 规则解释 - Case string `json:"Case"` // SQL示例 - Position int `json:"Position"` // 建议所处SQL字符位置,默认0表示全局建议 - Func func(*Query4Audit) Rule `json:"-"` // 函数名 -} - -/* - -## Item单词缩写含义 - -* ALI Alias(AS) -* ALT Alter -* ARG Argument -* CLA Classic -* COL Column -* DIS Distinct -* ERR Error, 特指MySQL执行返回的报错信息, ERR.000为vitess语法错误,ERR.001为执行错误,ERR.002为EXPLAIN错误 -* EXP Explain, 由explain模块给 -* FUN Function -* IDX Index, 由index模块给 -* JOI Join -* KEY Key -* KWR Keyword -* LCK Lock -* LIT Literal -* PRO Profiling, 由profiling模块给 -* RES Result -* SEC Security -* STA Standard -* SUB Subquery -* TBL Table -* TRA Trace, 由trace模块给 - -*/ - -// HeuristicRules 启发式规则列表 -var HeuristicRules map[string]Rule - -func init() { - HeuristicRules = map[string]Rule{ - "OK": { - Item: "OK", - Severity: "L0", - Summary: "OK", - Content: `OK`, - Case: "OK", - Func: (*Query4Audit).RuleOK, - }, - "ALI.001": { - Item: "ALI.001", - Severity: "L0", - Summary: "建议使用 AS 关键字显示声明一个别名", - Content: `在列或表别名(如"tbl AS alias")中, 明确使用 AS 关键字比隐含别名(如"tbl alias")更易懂。`, - Case: "select name from tbl t1 where id < 1000", - Func: (*Query4Audit).RuleImplicitAlias, - }, - "ALI.002": { - Item: "ALI.002", - Severity: "L8", - Summary: "不建议给列通配符'*'设置别名", - Content: `例: "SELECT tbl.* col1, col2"上面这条 SQL 给列通配符设置了别名,这样的SQL可能存在逻辑错误。您可能意在查询 col1, 但是代替它的是重命名的是 tbl 的最后一列。`, - Case: "select tbl.* as c1,c2,c3 from tbl where id < 1000", - Func: (*Query4Audit).RuleStarAlias, - }, - "ALI.003": { - Item: "ALI.003", - Severity: "L1", - Summary: "别名不要与表或列的名字相同", - Content: `表或列的别名与其真实名称相同, 这样的别名会使得查询更难去分辨。`, - Case: "select name from tbl as tbl where id < 1000", - Func: (*Query4Audit).RuleSameAlias, - }, - "ALT.001": { - Item: "ALT.001", - Severity: "L4", - Summary: "修改表的默认字符集不会改表各个字段的字符集", - Content: `很多初学者会将 ALTER TABLE tbl_name [DEFAULT] CHARACTER SET 'UTF8' 误认为会修改所有字段的字符集,但实际上它只会影响后续新增的字段不会改表已有字段的字符集。如果想修改整张表所有字段的字符集建议使用 ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;`, - Case: "ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;", - Func: (*Query4Audit).RuleAlterCharset, - }, - "ALT.002": { - Item: "ALT.002", - Severity: "L2", - Summary: "同一张表的多条 ALTER 请求建议合为一条", - Content: `每次表结构变更对线上服务都会产生影响,即使是能够通过在线工具进行调整也请尽量通过合并 ALTER 请求的试减少操作次数。`, - Case: "ALTER TABLE tbl ADD COLUMN col int, ADD INDEX idx_col (`col`);", - Func: (*Query4Audit).RuleOK, // 该建议在indexAdvisor中给 - }, - "ALT.003": { - Item: "ALT.003", - Severity: "L0", - Summary: "删除列为高危操作,操作前请注意检查业务逻辑是否还有依赖", - Content: `如业务逻辑依赖未完全消除,列被删除后可能导致数据无法写入或无法查询到已删除列数据导致程序异常的情况。这种情况下即使通过备份数据回滚也会丢失用户请求写入的数据。`, - Case: "ALTER TABLE tbl DROP COLUMN col;", - Func: (*Query4Audit).RuleAlterDropColumn, - }, - "ALT.004": { - Item: "ALT.004", - Severity: "L0", - Summary: "删除主键和外键为高危操作,操作前请与 DBA 确认影响", - Content: `主键和外键为关系型数据库中两种重要约束,删除已有约束会打破已有业务逻辑,操作前请业务开发与 DBA 确认影响,三思而行。`, - Case: "ALTER TABLE tbl DROP PRIMARY KEY;", - Func: (*Query4Audit).RuleAlterDropKey, - }, - "ARG.001": { - Item: "ARG.001", - Severity: "L4", - Summary: "不建议使用前项通配符查找", - Content: `例如 "%foo",查询参数有一个前项通配符的情况无法使用已有索引。`, - Case: "select c1,c2,c3 from tbl where name like '%foo'", - Func: (*Query4Audit).RulePrefixLike, - }, - "ARG.002": { - Item: "ARG.002", - Severity: "L1", - Summary: "没有通配符的 LIKE 查询", - Content: `不包含通配符的 LIKE 查询可能存在逻辑错误,因为逻辑上它与等值查询相同。`, - Case: "select c1,c2,c3 from tbl where name like 'foo'", - Func: (*Query4Audit).RuleEqualLike, - }, - "ARG.003": { - Item: "ARG.003", - Severity: "L4", - Summary: "参数比较包含隐式转换,无法使用索引", - Content: "隐式类型转换有无法命中索引的风险,在高并发、大数据量的情况下,命不中索引带来的后果非常严重。", - Case: "SELECT * FROM sakila.film WHERE length >= '60';", - Func: (*Query4Audit).RuleOK, // 该建议在IndexAdvisor中给,RuleImplicitConversion - }, - "ARG.004": { - Item: "ARG.004", - Severity: "L4", - Summary: "IN (NULL)/NOT IN (NULL) 永远非真", - Content: "正确的作法是 col IN ('val1', 'val2', 'val3') OR col IS NULL", - Case: "SELECT * FROM tb WHERE col IN (NULL);", - Func: (*Query4Audit).RuleIn, - }, - "ARG.005": { - Item: "ARG.005", - Severity: "L1", - Summary: "IN 要慎用,元素过多会导致全表扫描", - Content: ` 如:select id from t where num in(1,2,3)对于连续的数值,能用 BETWEEN 就不要用 IN 了:select id from t where num between 1 and 3。而当 IN 值过多时 MySQL 也可能会进入全表扫描导致性能急剧下降。`, - Case: "select id from t where num in(1,2,3)", - Func: (*Query4Audit).RuleIn, - }, - "ARG.006": { - Item: "ARG.006", - Severity: "L1", - Summary: "应尽量避免在 WHERE 子句中对字段进行 NULL 值判断", - Content: `使用 IS NULL 或 IS NOT NULL 将可能导致引擎放弃使用索引而进行全表扫描,如:select id from t where num is null;可以在num上设置默认值0,确保表中 num 列没有 NULL 值,然后这样查询: select id from t where num=0;`, - Case: "select id from t where num is null", - Func: (*Query4Audit).RuleIsNullIsNotNull, - }, - "ARG.007": { - Item: "ARG.007", - Severity: "L3", - Summary: "避免使用模式匹配", - Content: `性能问题是使用模式匹配操作符的最大缺点。使用 LIKE 或正则表达式进行模式匹配进行查询的另一个问题,是可能会返回意料之外的结果。最好的方案就是使用特殊的搜索引擎技术来替代 SQL,比如 Apache Lucene。另一个可选方案是将结果保存起来从而减少重复的搜索开销。如果一定要使用SQL,请考虑在 MySQL 中使用像 FULLTEXT 索引这样的第三方扩展。但更广泛地说,您不一定要使用SQL来解决所有问题。`, - Case: "select c_id,c2,c3 from tbl where c2 like 'test%'", - Func: (*Query4Audit).RulePatternMatchingUsage, - }, - "ARG.008": { - Item: "ARG.008", - Severity: "L1", - Summary: "OR 查询索引列时请尽量使用 IN 谓词", - Content: `IN-list 谓词可以用于索引检索,并且优化器可以对 IN-list 进行排序,以匹配索引的排序序列,从而获得更有效的检索。请注意,IN-list 必须只包含常量,或在查询块执行期间保持常量的值,例如外引用。`, - Case: "SELECT c1,c2,c3 FROM tbl WHERE c1 = 14 OR c1 = 17", - Func: (*Query4Audit).RuleORUsage, - }, - "ARG.009": { - Item: "ARG.009", - Severity: "L1", - Summary: "引号中的字符串开头或结尾包含空格", - Content: `如果 VARCHAR 列的前后存在空格将可能引起逻辑问题,如在 MySQL 5.5中 'a' 和 'a ' 可能会在查询中被认为是相同的值。`, - Case: "SELECT 'abc '", - Func: (*Query4Audit).RuleSpaceWithQuote, - }, - "ARG.010": { - Item: "ARG.010", - Severity: "L1", - Summary: "不要使用 hint,如:sql_no_cache, force index, ignore key, straight join等", - Content: `hint 是用来强制 SQL 按照某个执行计划来执行,但随着数据量变化我们无法保证自己当初的预判是正确的。`, - Case: "SELECT * FROM t1 USE INDEX (i1) ORDER BY a;", - Func: (*Query4Audit).RuleHint, - }, - "ARG.011": { - Item: "ARG.011", - Severity: "L3", - Summary: "不要使用负向查询,如:NOT IN/NOT LIKE", - Content: `请尽量不要使用负向查询,这将导致全表扫描,对查询性能影响较大。`, - Case: "select id from t where num not in(1,2,3);", - Func: (*Query4Audit).RuleNot, - }, - "ARG.012": { - Item: "ARG.012", - Severity: "L2", - Summary: "一次性 INSERT/REPLACE 的数据过多", - Content: "单条 INSERT/REPLACE 语句批量插入大量数据性能较差,甚至可能导致从库同步延迟。为了提升性能,减少批量写入数据对从库同步延时的影响,建议采用分批次插入的方法。", - Case: "INSERT INTO tb (a) VALUES (1), (2)", - Func: (*Query4Audit).RuleInsertValues, - }, - "CLA.001": { - Item: "CLA.001", - Severity: "L4", - Summary: "最外层 SELECT 未指定 WHERE 条件", - Content: `SELECT 语句没有 WHERE 子句,可能检查比预期更多的行(全表扫描)。对于 SELECT COUNT(*) 类型的请求如果不要求精度,建议使用 SHOW TABLE STATUS 或 EXPLAIN 替代。`, - Case: "select id from tbl", - Func: (*Query4Audit).RuleNoWhere, - }, - "CLA.002": { - Item: "CLA.002", - Severity: "L3", - Summary: "不建议使用 ORDER BY RAND()", - Content: `ORDER BY RAND() 是从结果集中检索随机行的一种非常低效的方法,因为它会对整个结果进行排序并丢弃其大部分数据。`, - Case: "select name from tbl where id < 1000 order by rand(number)", - Func: (*Query4Audit).RuleOrderByRand, - }, - "CLA.003": { - Item: "CLA.003", - Severity: "L2", - Summary: "不建议使用带 OFFSET 的LIMIT 查询", - Content: `使用 LIMIT 和 OFFSET 对结果集分页的复杂度是 O(n^2),并且会随着数据增大而导致性能问题。采用“书签”扫描的方法实现分页效率更高。`, - Case: "select c1,c2 from tbl where name=xx order by number limit 1 offset 20", - Func: (*Query4Audit).RuleOffsetLimit, - }, - "CLA.004": { - Item: "CLA.004", - Severity: "L2", - Summary: "不建议对常量进行 GROUP BY", - Content: `GROUP BY 1 表示按第一列进行 GROUP BY。如果在 GROUP BY 子句中使用数字,而不是表达式或列名称,当查询列顺序改变时,可能会导致问题。`, - Case: "select col1,col2 from tbl group by 1", - Func: (*Query4Audit).RuleGroupByConst, - }, - "CLA.005": { - Item: "CLA.005", - Severity: "L2", - Summary: "ORDER BY 常数列没有任何意义", - Content: `SQL 逻辑上可能存在错误; 最多只是一个无用的操作,不会更改查询结果。`, - Case: "select id from test where id=1 order by id", - Func: (*Query4Audit).RuleOrderByConst, - }, - "CLA.006": { - Item: "CLA.006", - Severity: "L4", - Summary: "在不同的表中 GROUP BY 或 ORDER BY", - Content: `这将强制使用临时表和 filesort,可能产生巨大性能隐患,并且可能消耗大量内存和磁盘上的临时空间。`, - Case: "select tb1.col, tb2.col from tb1, tb2 where id=1 group by tb1.col, tb2.col", - Func: (*Query4Audit).RuleDiffGroupByOrderBy, - }, - "CLA.007": { - Item: "CLA.007", - Severity: "L2", - Summary: "ORDER BY 语句对多个不同条件使用不同方向的排序无法使用索引", - Content: `ORDER BY 子句中的所有表达式必须按统一的 ASC 或 DESC 方向排序,以便利用索引。`, - Case: "select c1,c2,c3 from t1 where c1='foo' order by c2 desc, c3 asc", - Func: (*Query4Audit).RuleMixOrderBy, - }, - "CLA.008": { - Item: "CLA.008", - Severity: "L2", - Summary: "请为 GROUP BY 显示添加 ORDER BY 条件", - Content: `默认 MySQL 会对 'GROUP BY col1, col2, ...' 请求按如下顺序排序 'ORDER BY col1, col2, ...'。如果 GROUP BY 语句不指定 ORDER BY 条件会导致无谓的排序产生,如果不需要排序建议添加 'ORDER BY NULL'。`, - Case: "select c1,c2,c3 from t1 where c1='foo' group by c2", - Func: (*Query4Audit).RuleExplicitOrderBy, - }, - "CLA.009": { - Item: "CLA.009", - Severity: "L2", - Summary: "ORDER BY 的条件为表达式", - Content: `当 ORDER BY 条件为表达式或函数时会使用到临时表,如果在未指定 WHERE 或 WHERE 条件返回的结果集较大时性能会很差。`, - Case: "select description from film where title ='ACADEMY DINOSAUR' order by length-language_id;", - Func: (*Query4Audit).RuleOrderByExpr, - }, - "CLA.010": { - Item: "CLA.010", - Severity: "L2", - Summary: "GROUP BY 的条件为表达式", - Content: `当 GROUP BY 条件为表达式或函数时会使用到临时表,如果在未指定 WHERE 或 WHERE 条件返回的结果集较大时性能会很差。`, - Case: "select description from film where title ='ACADEMY DINOSAUR' GROUP BY length-language_id;", - Func: (*Query4Audit).RuleGroupByExpr, - }, - "CLA.011": { - Item: "CLA.011", - Severity: "L1", - Summary: "建议为表添加注释", - Content: `为表添加注释能够使得表的意义更明确,从而为日后的维护带来极大的便利。`, - Case: "CREATE TABLE `test1` (`ID` bigint(20) NOT NULL AUTO_INCREMENT,`c1` varchar(128) DEFAULT NULL,PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=utf8", - Func: (*Query4Audit).RuleTblCommentCheck, - }, - "CLA.012": { - Item: "CLA.012", - Severity: "L2", - Summary: "将复杂的裹脚布式查询分解成几个简单的查询", - Content: `SQL是一门极具表现力的语言,您可以在单个SQL查询或者单条语句中完成很多事情。但这并不意味着必须强制只使用一行代码,或者认为使用一行代码就搞定每个任务是个好主意。通过一个查询来获得所有结果的常见后果是得到了一个笛卡儿积。当查询中的两张表之间没有条件限制它们的关系时,就会发生这种情况。没有对应的限制而直接使用两张表进行联结查询,就会得到第一张表中的每一行和第二张表中的每一行的一个组合。每一个这样的组合就会成为结果集中的一行,最终您就会得到一个行数很多的结果集。重要的是要考虑这些查询很难编写、难以修改和难以调试。数据库查询请求的日益增加应该是预料之中的事。经理们想要更复杂的报告以及在用户界面上添加更多的字段。如果您的设计很复杂,并且是一个单一查询,要扩展它们就会很费时费力。不论对您还是项目来说,时间花在这些事情上面不值得。将复杂的意大利面条式查询分解成几个简单的查询。当您拆分一个复杂的SQL查询时,得到的结果可能是很多类似的查询,可能仅仅在数据类型上有所不同。编写所有的这些查询是很乏味的,因此,最好能够有个程序自动生成这些代码。SQL代码生成是一个很好的应用。尽管SQL支持用一行代码解决复杂的问题,但也别做不切实际的事情。`, - Case: "这是一条很长很长的 SQL,案例略。", - Func: (*Query4Audit).RuleSpaghettiQueryAlert, - }, - /* - https://www.datacamp.com/community/tutorials/sql-tutorial-query - The HAVING Clause - The HAVING clause was originally added to SQL because the WHERE keyword could not be used with aggregate functions. HAVING is typically used with the GROUP BY clause to restrict the groups of returned rows to only those that meet certain conditions. However, if you use this clause in your query, the index is not used, which -as you already know- can result in a query that doesn't really perform all that well. - - If you’re looking for an alternative, consider using the WHERE clause. Consider the following queries: - - SELECT state, COUNT(*) - FROM Drivers - WHERE state IN ('GA', 'TX') - GROUP BY state - ORDER BY state - SELECT state, COUNT(*) - FROM Drivers - GROUP BY state - HAVING state IN ('GA', 'TX') - ORDER BY state - The first query uses the WHERE clause to restrict the number of rows that need to be summed, whereas the second query sums up all the rows in the table and then uses HAVING to throw away the sums it calculated. In these types of cases, the alternative with the WHERE clause is obviously the better one, as you don’t waste any resources. - - You see that this is not about limiting the result set, rather about limiting the intermediate number of records within a query. - - Note that the difference between these two clauses lies in the fact that the WHERE clause introduces a condition on individual rows, while the HAVING clause introduces a condition on aggregations or results of a selection where a single result, such as MIN, MAX, SUM,… has been produced from multiple rows. - */ - "CLA.013": { - Item: "CLA.013", - Severity: "L3", - Summary: "不建议使用 HAVING 子句", - Content: `将查询的 HAVING 子句改写为 WHERE 中的查询条件,可以在查询处理期间使用索引。`, - Case: "SELECT s.c_id,count(s.c_id) FROM s where c = test GROUP BY s.c_id HAVING s.c_id <> '1660' AND s.c_id <> '2' order by s.c_id", - Func: (*Query4Audit).RuleHavingClause, - }, - "CLA.014": { - Item: "CLA.014", - Severity: "L2", - Summary: "删除全表时建议使用 TRUNCATE 替代 DELETE", - Content: `删除全表时建议使用 TRUNCATE 替代 DELETE`, - Case: "delete from tbl", - Func: (*Query4Audit).RuleNoWhere, - }, - "CLA.015": { - Item: "CLA.015", - Severity: "L4", - Summary: "UPDATE 未指定 WHERE 条件", - Content: `UPDATE 不指定 WHERE 条件一般是致命的,请您三思后行`, - Case: "update tbl set col=1", - Func: (*Query4Audit).RuleNoWhere, - }, - "CLA.016": { - Item: "CLA.016", - Severity: "L2", - Summary: "不要 UPDATE 主键", - Content: `主键是数据表中记录的唯一标识符,不建议频繁更新主键列,这将影响元数据统计信息进而影响正常的查询。`, - Case: "update tbl set col=1", - Func: (*Query4Audit).RuleOK, // 该建议在indexAdvisor中给 - }, - "COL.001": { - Item: "COL.001", - Severity: "L1", - Summary: "不建议使用 SELECT * 类型查询", - Content: `当表结构变更时,使用 * 通配符选择所有列将导致查询的含义和行为会发生更改,可能导致查询返回更多的数据。`, - Case: "select * from tbl where id=1", - Func: (*Query4Audit).RuleSelectStar, - }, - "COL.002": { - Item: "COL.002", - Severity: "L2", - Summary: "INSERT/REPLACE 未指定列名", - Content: `当表结构发生变更,如果 INSERT 或 REPLACE 请求不明确指定列名,请求的结果将会与预想的不同; 建议使用 “INSERT INTO tbl(col1,col2)VALUES ...” 代替。`, - Case: "insert into tbl values(1,'name')", - Func: (*Query4Audit).RuleInsertColDef, - }, - "COL.003": { - Item: "COL.003", - Severity: "L2", - Summary: "建议修改自增 ID 为无符号类型", - Content: `建议修改自增 ID 为无符号类型`, - Case: "create table test(`id` int(11) NOT NULL AUTO_INCREMENT)", - Func: (*Query4Audit).RuleAutoIncUnsigned, - }, - "COL.004": { - Item: "COL.004", - Severity: "L1", - Summary: "请为列添加默认值", - Content: `请为列添加默认值,如果是 ALTER 操作,请不要忘记将原字段的默认值写上。字段无默认值,当表较大时无法在线变更表结构。`, - Case: "CREATE TABLE tbl (col int) ENGINE=InnoDB;", - Func: (*Query4Audit).RuleAddDefaultValue, - }, - "COL.005": { - Item: "COL.005", - Severity: "L1", - Summary: "列未添加注释", - Content: `建议对表中每个列添加注释,来明确每个列在表中的含义及作用。`, - Case: "CREATE TABLE tbl (col int) ENGINE=InnoDB;", - Func: (*Query4Audit).RuleColCommentCheck, - }, - "COL.006": { - Item: "COL.006", - Severity: "L3", - Summary: "表中包含有太多的列", - Content: `表中包含有太多的列`, - Case: "CREATE TABLE tbl ( cols ....);", - Func: (*Query4Audit).RuleTooManyFields, - }, - "COL.008": { - Item: "COL.008", - Severity: "L1", - Summary: "可使用 VARCHAR 代替 CHAR, VARBINARY 代替 BINARY", - Content: `为首先变长字段存储空间小,可以节省存储空间。其次对于查询来说,在一个相对较小的字段内搜索效率显然要高些。`, - Case: "create table t1(id int,name char(20),last_time date)", - Func: (*Query4Audit).RuleVarcharVSChar, - }, - "COL.009": { - Item: "COL.009", - Severity: "L2", - Summary: "建议使用精确的数据类型", - Content: `实际上,任何使用 FLOAT, REAL 或 DOUBLE PRECISION 数据类型的设计都有可能是反模式。大多数应用程序使用的浮点数的取值范围并不需要达到IEEE 754标准所定义的最大/最小区间。在计算总量时,非精确浮点数所积累的影响是严重的。使用 SQL 中的 NUMERIC 或 DECIMAL 类型来代替 FLOAT 及其类似的数据类型进行固定精度的小数存储。这些数据类型精确地根据您定义这一列时指定的精度来存储数据。尽可能不要使用浮点数。`, - Case: "CREATE TABLE tab2 (p_id BIGINT UNSIGNED NOT NULL,a_id BIGINT UNSIGNED NOT NULL,hours float not null,PRIMARY KEY (p_id, a_id))", - Func: (*Query4Audit).RuleImpreciseDataType, - }, - "COL.010": { - Item: "COL.010", - Severity: "L2", - Summary: "不建议使用 ENUM 数据类型", - Content: `ENUM 定义了列中值的类型,使用字符串表示 ENUM 里的值时,实际存储在列中的数据是这些值在定义时的序数。因此,这列的数据是字节对齐的,当您进行一次排序查询时,结果是按照实际存储的序数值排序的,而不是按字符串值的字母顺序排序的。这可能不是您所希望的。没有什么语法支持从 ENUM 或者 check 约束中添加或删除一个值;您只能使用一个新的集合重新定义这一列。如果您打算废弃一个选项,您可能会为历史数据而烦恼。作为一种策略,改变元数据——也就是说,改变表和列的定义——应该是不常见的,并且要注意测试和质量保证。有一个更好的解决方案来约束一列中的可选值:创建一张检查表,每一行包含一个允许在列中出现的候选值;然后在引用新表的旧表上声明一个外键约束。`, - Case: "create table tab1(status ENUM('new','in progress','fixed'))", - Func: (*Query4Audit).RuleValuesInDefinition, - }, - // 这个建议从sqlcheck迁移来的,实际生产环境每条建表SQL都会给这条建议,看多了会不开心。 - "COL.011": { - Item: "COL.011", - Severity: "L0", - Summary: "当需要唯一约束时才使用 NULL,仅当列不能有缺失值时才使用 NOT NULL", - Content: `NULL 和0是不同的,10乘以 NULL 还是 NULL。NULL 和空字符串是不一样的。将一个字符串和标准 SQL 中的 NULL 联合起来的结果还是 NULL。NULL 和 FALSE 也是不同的。AND、OR 和 NOT 这三个布尔操作如果涉及 NULL,其结果也让很多人感到困惑。当您将一列声明为 NOT NULL 时,也就是说这列中的每一个值都必须存在且是有意义的。使用 NULL 来表示任意类型不存在的空值。 当您将一列声明为 NOT NULL 时,也就是说这列中的每一个值都必须存在且是有意义的。`, - Case: "select c1,c2,c3 from tbl where c4 is null or c4 <> 1", - Func: (*Query4Audit).RuleNullUsage, - }, - "COL.012": { - Item: "COL.012", - Severity: "L5", - Summary: "BLOB 和 TEXT 类型的字段不可设置为 NULL", - Content: `BLOB 和 TEXT 类型的字段不可设置为 NULL`, - Case: "CREATE TABLE `tbl` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `c` longblob, PRIMARY KEY (`id`));", - Func: (*Query4Audit).RuleCantBeNull, - }, - "COL.013": { - Item: "COL.013", - Severity: "L4", - Summary: "TIMESTAMP 类型未设置默认值", - Content: `TIMESTAMP 类型未设置默认值`, - Case: "CREATE TABLE tbl( `id` bigint not null, `create_time` timestamp);", - Func: (*Query4Audit).RuleTimestampDefault, - }, - "COL.014": { - Item: "COL.014", - Severity: "L5", - Summary: "为列指定了字符集", - Content: `建议列与表使用同一个字符集,不要单独指定列的字符集。`, - Case: "CREATE TABLE `tb2` ( `id` int(11) DEFAULT NULL, `col` char(10) CHARACTER SET utf8 DEFAULT NULL)", - Func: (*Query4Audit).RuleColumnWithCharset, - }, - "COL.015": { - Item: "COL.015", - Severity: "L4", - Summary: "BLOB 类型的字段不可指定默认值", - Content: `BLOB 类型的字段不可指定默认值`, - Case: "CREATE TABLE `tbl` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `c` blob NOT NULL DEFAULT '', PRIMARY KEY (`id`));", - Func: (*Query4Audit).RuleBlobDefaultValue, - }, - "COL.016": { - Item: "COL.016", - Severity: "L1", - Summary: "整型定义建议采用 INT(10) 或 BIGINT(20)", - Content: `INT(M) 在 integer 数据类型中,M 表示最大显示宽度。 在 INT(M) 中,M 的值跟 INT(M) 所占多少存储空间并无任何关系。 INT(3)、INT(4)、INT(8) 在磁盘上都是占用 4 bytes 的存储空间。`, - Case: "CREATE TABLE tab (a INT(1));", - Func: (*Query4Audit).RuleIntPrecision, - }, - "COL.017": { - Item: "COL.017", - Severity: "L2", - Summary: "VARCHAR 定义长度过长", - Content: fmt.Sprintf(`varchar 是可变长字符串,不预先分配存储空间,长度不要超过%d,如果存储长度过长 MySQL 将定义字段类型为 text,独立出来一张表,用主键来对应,避免影响其它字段索引效率。`, common.Config.MaxVarcharLength), - Case: "CREATE TABLE tab (a varchar(3500));", - Func: (*Query4Audit).RuleVarcharLength, - }, - "DIS.001": { - Item: "DIS.001", - Severity: "L1", - Summary: "消除不必要的 DISTINCT 条件", - Content: `太多DISTINCT条件是复杂的裹脚布式查询的症状。考虑将复杂查询分解成许多简单的查询,并减少DISTINCT条件的数量。如果主键列是列的结果集的一部分,则DISTINCT条件可能没有影响。`, - Case: "SELECT DISTINCT c.c_id,count(DISTINCT c.c_name),count(DISTINCT c.c_e),count(DISTINCT c.c_n),count(DISTINCT c.c_me),c.c_d FROM (select distinct id, name from B) as e WHERE e.country_id = c.country_id", - Func: (*Query4Audit).RuleDistinctUsage, - }, - "DIS.002": { - Item: "DIS.002", - Severity: "L3", - Summary: "COUNT(DISTINCT) 多列时结果可能和你预想的不同", - Content: `COUNT(DISTINCT col) 计算该列除NULL之外的不重复行数,注意 COUNT(DISTINCT col, col2) 如果其中一列全为 NULL 那么即使另一列有不同的值,也返回0。`, - Case: "SELECT COUNT(DISTINCT col, col2) FROM tbl;", - Func: (*Query4Audit).RuleCountDistinctMultiCol, - }, - // DIS.003 灵感来源于如下链接 - // http://www.ijstr.org/final-print/oct2015/Query-Optimization-Techniques-Tips-For-Writing-Efficient-And-Faster-Sql-Queries.pdf - "DIS.003": { - Item: "DIS.003", - Severity: "L3", - Summary: "DISTINCT * 对有主键的表没有意义", - Content: `当表已经有主键时,对所有列进行 DISTINCT 的输出结果与不进行 DISTINCT 操作的结果相同,请不要画蛇添足。`, - Case: "SELECT DISTINCT * FROM film;", - Func: (*Query4Audit).RuleDistinctStar, - }, - "FUN.001": { - Item: "FUN.001", - Severity: "L2", - Summary: "避免在 WHERE 条件中使用函数或其他运算符", - Content: `虽然在 SQL 中使用函数可以简化很多复杂的查询,但使用了函数的查询无法利用表中已经建立的索引,该查询将会是全表扫描,性能较差。通常建议将列名写在比较运算符左侧,将查询过滤条件放在比较运算符右侧。也不建议在查询比较条件两侧书写多余的括号,这会对阅读产生比较大的困扰。`, - Case: "select id from t where substring(name,1,3)='abc'", - Func: (*Query4Audit).RuleCompareWithFunction, - }, - "FUN.002": { - Item: "FUN.002", - Severity: "L1", - Summary: "指定了 WHERE 条件或非 MyISAM 引擎时使用 COUNT(*) 操作性能不佳", - Content: `COUNT(*) 的作用是统计表行数,COUNT(COL) 的作用是统计指定列非 NULL 的行数。MyISAM 表对于 COUNT(*) 统计全表行数进行了特殊的优化,通常情况下非常快。但对于非 MyISAM 表或指定了某些 WHERE 条件,COUNT(*) 操作需要扫描大量的行才能获取精确的结果,性能也因此不佳。有时候某些业务场景并不需要完全精确的 COUNT 值,此时可以用近似值来代替。EXPLAIN 出来的优化器估算的行数就是一个不错的近似值,执行 EXPLAIN 并不需要真正去执行查询,所以成本很低。`, - Case: "SELECT c3, COUNT(*) AS accounts FROM tab where c2 < 10000 GROUP BY c3 ORDER BY num", - Func: (*Query4Audit).RuleCountStar, - }, - "FUN.003": { - Item: "FUN.003", - Severity: "L3", - Summary: "使用了合并为可空列的字符串连接", - Content: `在一些查询请求中,您需要强制让某一列或者某个表达式返回非 NULL 的值,从而让查询逻辑变得更简单,担忧不想将这个值存下来。使用 COALESCE() 函数来构造连接的表达式,这样即使是空值列也不会使整表达式变为 NULL。`, - Case: "select c1 || coalesce(' ' || c2 || ' ', ' ') || c3 as c from tbl", - Func: (*Query4Audit).RuleStringConcatenation, - }, - "FUN.004": { - Item: "FUN.004", - Severity: "L4", - Summary: "不建议使用 SYSDATE() 函数", - Content: `SYSDATE() 函数可能导致主从数据不一致,请使用 NOW() 函数替代 SYSDATE()。`, - Case: "SELECT SYSDATE();", - Func: (*Query4Audit).RuleSysdate, - }, - "FUN.005": { - Item: "FUN.005", - Severity: "L1", - Summary: "不建议使用 COUNT(col) 或 COUNT(常量)", - Content: `不要使用 COUNT(col) 或 COUNT(常量) 来替代 COUNT(*), COUNT(*) 是 SQL92 定义的标准统计行数的方法,跟数据无关,跟 NULL 和非 NULL 也无关。`, - Case: "SELECT COUNT(1) FROM tbl;", - Func: (*Query4Audit).RuleCountConst, - }, - "FUN.006": { - Item: "FUN.006", - Severity: "L1", - Summary: "使用 SUM(COL) 时需注意 NPE 问题", - Content: `当某一列的值全是 NULL 时,COUNT(COL) 的返回结果为0,但 SUM(COL) 的返回结果为 NULL,因此使用 SUM() 时需注意 NPE 问题。可以使用如下方式来避免 SUM 的 NPE 问题: SELECT IF(ISNULL(SUM(COL)), 0, SUM(COL)) FROM tbl`, - Case: "SELECT SUM(COL) FROM tbl;", - Func: (*Query4Audit).RuleSumNPE, - }, - "FUN.007": { - Item: "FUN.007", - Severity: "L1", - Summary: "不建议使用触发器", - Content: `触发器的执行没有反馈和日志,隐藏了实际的执行步骤,当数据库出现问题是,不能通过慢日志分析触发器的具体执行情况,不易发现问题。在MySQL中,触发器不能临时关闭或打开,在数据迁移或数据恢复等场景下,需要临时drop触发器,可能影响到生产环境。`, - Case: "CREATE TRIGGER t1 AFTER INSERT ON work FOR EACH ROW INSERT INTO time VALUES(NOW());", - Func: (*Query4Audit).RuleForbiddenTrigger, - }, - "FUN.008": { - Item: "FUN.008", - Severity: "L1", - Summary: "不建议使用存储过程", - Content: `存储过程无版本控制,配合业务的存储过程升级很难做到业务无感知。存储过程在拓展和移植上也存在问题。`, - Case: "CREATE PROCEDURE simpleproc (OUT param1 INT);", - Func: (*Query4Audit).RuleForbiddenProcedure, - }, - "FUN.009": { - Item: "FUN.009", - Severity: "L1", - Summary: "不建议使用自定义函数", - Content: `不建议使用自定义函数`, - Case: "CREATE FUNCTION hello (s CHAR(20));", - Func: (*Query4Audit).RuleForbiddenFunction, - }, - "GRP.001": { - Item: "GRP.001", - Severity: "L2", - Summary: "不建议对等值查询列使用 GROUP BY", - Content: `GROUP BY 中的列在前面的 WHERE 条件中使用了等值查询,对这样的列进行 GROUP BY 意义不大。`, - Case: "select film_id, title from film where release_year='2006' group by release_year", - Func: (*Query4Audit).RuleOK, // 该建议在indexAdvisor中给 - }, - "JOI.001": { - Item: "JOI.001", - Severity: "L2", - Summary: "JOIN 语句混用逗号和 ANSI 模式", - Content: `表连接的时候混用逗号和 ANSI JOIN 不便于人类理解,并且MySQL不同版本的表连接行为和优先级均有所不同,当 MySQL 版本变化后可能会引入错误。`, - Case: "select c1,c2,c3 from t1,t2 join t3 on t1.c1=t2.c1,t1.c3=t3,c1 where id>1000", - Func: (*Query4Audit).RuleCommaAnsiJoin, - }, - "JOI.002": { - Item: "JOI.002", - Severity: "L4", - Summary: "同一张表被连接两次", - Content: `相同的表在 FROM 子句中至少出现两次,可以简化为对该表的单次访问。`, - Case: "select tb1.col from (tb1, tb2) join tb2 on tb1.id=tb.id where tb1.id=1", - Func: (*Query4Audit).RuleDupJoin, - }, - "JOI.003": { - Item: "JOI.003", - Severity: "L4", - Summary: "OUTER JOIN 失效", - Content: `由于 WHERE 条件错误使得 OUTER JOIN 的外部表无数据返回,这会将查询隐式转换为 INNER JOIN 。如:select c from L left join R using(c) where L.a=5 and R.b=10。这种 SQL 逻辑上可能存在错误或程序员对 OUTER JOIN 如何工作存在误解,因为 LEFT/RIGHT JOIN 是 LEFT/RIGHT OUTER JOIN 的缩写。`, - Case: "select c1,c2,c3 from t1 left outer join t2 using(c1) where t1.c2=2 and t2.c3=4", - Func: (*Query4Audit).RuleOK, // TODO - }, - "JOI.004": { - Item: "JOI.004", - Severity: "L4", - Summary: "不建议使用排它 JOIN", - Content: `只在右侧表为 NULL 的带 WHERE 子句的 LEFT OUTER JOIN 语句,有可能是在WHERE子句中使用错误的列,如:“... FROM l LEFT OUTER JOIN r ON l.l = r.r WHERE r.z IS NULL”,这个查询正确的逻辑可能是 WHERE r.r IS NULL。`, - Case: "select c1,c2,c3 from t1 left outer join t2 on t1.c1=t2.c1 where t2.c2 is null", - Func: (*Query4Audit).RuleOK, // TODO - }, - "JOI.005": { - Item: "JOI.005", - Severity: "L2", - Summary: "减少 JOIN 的数量", - Content: `太多的 JOIN 是复杂的裹脚布式查询的症状。考虑将复杂查询分解成许多简单的查询,并减少 JOIN 的数量。`, - Case: "select bp1.p_id, b1.d_d as l, b1.b_id from b1 join bp1 on (b1.b_id = bp1.b_id) left outer join (b1 as b2 join bp2 on (b2.b_id = bp2.b_id)) on (bp1.p_id = bp2.p_id ) join bp21 on (b1.b_id = bp1.b_id) join bp31 on (b1.b_id = bp1.b_id) join bp41 on (b1.b_id = bp1.b_id) where b2.b_id = 0", - Func: (*Query4Audit).RuleReduceNumberOfJoin, - }, - "JOI.006": { - Item: "JOI.006", - Severity: "L4", - Summary: "将嵌套查询重写为 JOIN 通常会导致更高效的执行和更有效的优化", - Content: `一般来说,非嵌套子查询总是用于关联子查询,最多是来自FROM子句中的一个表,这些子查询用于 ANY, ALL 和 EXISTS 的谓词。如果可以根据查询语义决定子查询最多返回一个行,那么一个不相关的子查询或来自FROM子句中的多个表的子查询就被压平了。`, - Case: "SELECT s,p,d FROM tbl WHERE p.p_id = (SELECT s.p_id FROM tbl WHERE s.c_id = 100996 AND s.q = 1 )", - Func: (*Query4Audit).RuleNestedSubQueries, - }, - "JOI.007": { - Item: "JOI.007", - Severity: "L4", - Summary: "不建议使用联表删除或更新", - Content: `当需要同时删除或更新多张表时建议使用简单语句,一条 SQL 只删除或更新一张表,尽量不要将多张表的操作在同一条语句。`, - Case: "UPDATE users u LEFT JOIN hobby h ON u.id = h.uid SET u.name = 'pianoboy' WHERE h.hobby = 'piano';", - Func: (*Query4Audit).RuleMultiDeleteUpdate, - }, - "JOI.008": { - Item: "JOI.008", - Severity: "L4", - Summary: "不要使用跨数据库的 JOIN 查询", - Content: `一般来说,跨数据库的 JOIN 查询意味着查询语句跨越了两个不同的子系统,这可能意味着系统耦合度过高或库表结构设计不合理。`, - Case: "SELECT s,p,d FROM tbl WHERE p.p_id = (SELECT s.p_id FROM tbl WHERE s.c_id = 100996 AND s.q = 1 )", - Func: (*Query4Audit).RuleMultiDBJoin, - }, - // TODO: 跨库事务的检查,目前SOAR未对事务做处理 - "KEY.001": { - Item: "KEY.001", - Severity: "L2", - Summary: "建议使用自增列作为主键,如使用联合自增主键时请将自增键作为第一列", - Content: `建议使用自增列作为主键,如使用联合自增主键时请将自增键作为第一列`, - Case: "create table test(`id` int(11) NOT NULL PRIMARY KEY (`id`))", - Func: (*Query4Audit).RulePKNotInt, - }, - "KEY.002": { - Item: "KEY.002", - Severity: "L4", - Summary: "无主键或唯一键,无法在线变更表结构", - Content: `无主键或唯一键,无法在线变更表结构`, - Case: "create table test(col varchar(5000))", - Func: (*Query4Audit).RuleNoOSCKey, - }, - "KEY.003": { - Item: "KEY.003", - Severity: "L4", - Summary: "避免外键等递归关系", - Content: `存在递归关系的数据很常见,数据常会像树或者以层级方式组织。然而,创建一个外键约束来强制执行同一表中两列之间的关系,会导致笨拙的查询。树的每一层对应着另一个连接。您将需要发出递归查询,以获得节点的所有后代或所有祖先。解决方案是构造一个附加的闭包表。它记录了树中所有节点间的关系,而不仅仅是那些具有直接的父子关系。您也可以比较不同层次的数据设计:闭包表,路径枚举,嵌套集。然后根据应用程序的需要选择一个。`, - Case: "CREATE TABLE tab2 (p_id BIGINT UNSIGNED NOT NULL,a_id BIGINT UNSIGNED NOT NULL,PRIMARY KEY (p_id, a_id),FOREIGN KEY (p_id) REFERENCES tab1(p_id),FOREIGN KEY (a_id) REFERENCES tab3(a_id))", - Func: (*Query4Audit).RuleRecursiveDependency, - }, - // TODO: 新增复合索引,字段按散粒度是否由大到小排序,区分度最高的在最左边 - "KEY.004": { - Item: "KEY.004", - Severity: "L0", - Summary: "提醒:请将索引属性顺序与查询对齐", - Content: `如果为列创建复合索引,请确保查询属性与索引属性的顺序相同,以便DBMS在处理查询时使用索引。如果查询和索引属性订单没有对齐,那么DBMS可能无法在查询处理期间使用索引。`, - Case: "create index idx1 on tbl (last_name,first_name)", - Func: (*Query4Audit).RuleIndexAttributeOrder, - }, - "KEY.005": { - Item: "KEY.005", - Severity: "L2", - Summary: "表建的索引过多", - Content: `表建的索引过多`, - Case: "CREATE TABLE tbl ( a int, b int, c int, KEY idx_a (`a`),KEY idx_b(`b`),KEY idx_c(`c`));", - Func: (*Query4Audit).RuleTooManyKeys, - }, - "KEY.006": { - Item: "KEY.006", - Severity: "L4", - Summary: "主键中的列过多", - Content: `主键中的列过多`, - Case: "CREATE TABLE tbl ( a int, b int, c int, PRIMARY KEY(`a`,`b`,`c`));", - Func: (*Query4Audit).RuleTooManyKeyParts, - }, - "KEY.007": { - Item: "KEY.007", - Severity: "L4", - Summary: "未指定主键或主键非 int 或 bigint", - Content: `未指定主键或主键非 int 或 bigint,建议将主键设置为 int unsigned 或 bigint unsigned。`, - Case: "CREATE TABLE tbl (a int);", - Func: (*Query4Audit).RulePKNotInt, - }, - "KEY.008": { - Item: "KEY.008", - Severity: "L4", - Summary: "ORDER BY 多个列但排序方向不同时可能无法使用索引", - Content: `在 MySQL 8.0之前当 ORDER BY 多个列指定的排序方向不同时将无法使用已经建立的索引。`, - Case: "SELECT * FROM tbl ORDER BY a DESC, b ASC;", - Func: (*Query4Audit).RuleOrderByMultiDirection, - }, - "KEY.009": { - Item: "KEY.009", - Severity: "L0", - Summary: "添加唯一索引前请注意检查数据唯一性", - Content: `请提前检查添加唯一索引列的数据唯一性,如果数据不唯一在线表结构调整时将有可能自动将重复列删除,这有可能导致数据丢失。`, - Case: "CREATE UNIQUE INDEX part_of_name ON customer (name(10));", - Func: (*Query4Audit).RuleUniqueKeyDup, - }, - "KWR.001": { - Item: "KWR.001", - Severity: "L2", - Summary: "SQL_CALC_FOUND_ROWS 效率低下", - Content: `因为 SQL_CALC_FOUND_ROWS 不能很好地扩展,所以可能导致性能问题; 建议业务使用其他策略来替代 SQL_CALC_FOUND_ROWS 提供的计数功能,比如:分页结果展示等。`, - Case: "select SQL_CALC_FOUND_ROWS col from tbl where id>1000", - Func: (*Query4Audit).RuleSQLCalcFoundRows, - }, - "KWR.002": { - Item: "KWR.002", - Severity: "L2", - Summary: "不建议使用 MySQL 关键字做列名或表名", - Content: `当使用关键字做为列名或表名时程序需要对列名和表名进行转义,如果疏忽被将导致请求无法执行。`, - Case: "CREATE TABLE tbl ( `select` int )", - Func: (*Query4Audit).RuleUseKeyWord, - }, - "KWR.003": { - Item: "KWR.003", - Severity: "L1", - Summary: "不建议使用复数做列名或表名", - Content: `表名应该仅仅表示表里面的实体内容,不应该表示实体数量,对应于 DO 类名也是单数形式,符合表达习惯。`, - Case: "CREATE TABLE tbl ( `books` int )", - Func: (*Query4Audit).RulePluralWord, - }, - "KWR.004": { - Item: "KWR.004", - Severity: "L1", - Summary: "不建议使用使用多字节编码字符(中文)命名", - Content: `为库、表、列、别名命名时建议使用英文,数字,下划线等字符,不建议使用中文或其他多字节编码字符。`, - Case: "select col as 列 from tb", - Func: (*Query4Audit).RuleMultiBytesWord, - }, - "LCK.001": { - Item: "LCK.001", - Severity: "L3", - Summary: "INSERT INTO xx SELECT 加锁粒度较大请谨慎", - Content: `INSERT INTO xx SELECT 加锁粒度较大请谨慎`, - Case: "INSERT INTO tbl SELECT * FROM tbl2;", - Func: (*Query4Audit).RuleInsertSelect, - }, - "LCK.002": { - Item: "LCK.002", - Severity: "L3", - Summary: "请慎用 INSERT ON DUPLICATE KEY UPDATE", - Content: `当主键为自增键时使用 INSERT ON DUPLICATE KEY UPDATE 可能会导致主键出现大量不连续快速增长,导致主键快速溢出无法继续写入。极端情况下还有可能导致主从数据不一致。`, - Case: "INSERT INTO t1(a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;", - Func: (*Query4Audit).RuleInsertOnDup, - }, - "LIT.001": { - Item: "LIT.001", - Severity: "L2", - Summary: "用字符类型存储IP地址", - Content: `字符串字面上看起来像IP地址,但不是 INET_ATON() 的参数,表示数据被存储为字符而不是整数。将IP地址存储为整数更为有效。`, - Case: "insert into tbl (IP,name) values('10.20.306.122','test')", - Func: (*Query4Audit).RuleIPString, - }, - "LIT.002": { - Item: "LIT.002", - Severity: "L4", - Summary: "日期/时间未使用引号括起", - Content: `诸如“WHERE col <2010-02-12”之类的查询是有效的SQL,但可能是一个错误,因为它将被解释为“WHERE col <1996”; 日期/时间文字应该加引号。`, - Case: "select col1,col2 from tbl where time < 2018-01-10", - Func: (*Query4Audit).RuleDataNotQuote, - }, - "LIT.003": { - Item: "LIT.003", - Severity: "L3", - Summary: "一列中存储一系列相关数据的集合", - Content: `将 ID 存储为一个列表,作为 VARCHAR/TEXT 列,这样能导致性能和数据完整性问题。查询这样的列需要使用模式匹配的表达式。使用逗号分隔的列表来做多表联结查询定位一行数据是极不优雅和耗时的。这将使验证 ID 更加困难。考虑一下,列表最多支持存放多少数据呢?将 ID 存储在一张单独的表中,代替使用多值属性,从而每个单独的属性值都可以占据一行。这样交叉表实现了两张表之间的多对多关系。这将更好地简化查询,也更有效地验证ID。`, - Case: "select c1,c2,c3,c4 from tab1 where col_id REGEXP '[[:<:]]12[[:>:]]'", - Func: (*Query4Audit).RuleMultiValueAttribute, - }, - "LIT.004": { - Item: "LIT.004", - Severity: "L1", - Summary: "请使用分号或已设定的 DELIMITER 结尾", - Content: `USE database, SHOW DATABASES 等命令也需要使用使用分号或已设定的 DELIMITER 结尾。`, - Case: "USE db", - Func: (*Query4Audit).RuleOK, // TODO: RuleAddDelimiter - }, - "RES.001": { - Item: "RES.001", - Severity: "L4", - Summary: "非确定性的 GROUP BY", - Content: `SQL返回的列既不在聚合函数中也不是 GROUP BY 表达式的列中,因此这些值的结果将是非确定性的。如:select a, b, c from tbl where foo="bar" group by a,该 SQL 返回的结果就是不确定的。`, - Case: "select c1,c2,c3 from t1 where c2='foo' group by c2", - Func: (*Query4Audit).RuleNoDeterministicGroupby, - }, - "RES.002": { - Item: "RES.002", - Severity: "L4", - Summary: "未使用 ORDER BY 的 LIMIT 查询", - Content: `没有 ORDER BY 的 LIMIT 会导致非确定性的结果,这取决于查询执行计划。`, - Case: "select col1,col2 from tbl where name=xx limit 10", - Func: (*Query4Audit).RuleNoDeterministicLimit, - }, - "RES.003": { - Item: "RES.003", - Severity: "L4", - Summary: "UPDATE/DELETE 操作使用了 LIMIT 条件", - Content: `UPDATE/DELETE 操作使用 LIMIT 条件和不添加 WHERE 条件一样危险,它可将会导致主从数据不一致或从库同步中断。`, - Case: "UPDATE film SET length = 120 WHERE title = 'abc' LIMIT 1;", - Func: (*Query4Audit).RuleUpdateDeleteWithLimit, - }, - "RES.004": { - Item: "RES.004", - Severity: "L4", - Summary: "UPDATE/DELETE 操作指定了 ORDER BY 条件", - Content: `UPDATE/DELETE 操作不要指定 ORDER BY 条件。`, - Case: "UPDATE film SET length = 120 WHERE title = 'abc' ORDER BY title", - Func: (*Query4Audit).RuleUpdateDeleteWithOrderby, - }, - "RES.005": { - Item: "RES.005", - Severity: "L4", - Summary: "UPDATE 语句可能存在逻辑错误,导致数据损坏", - Content: "在一条 UPDATE 语句中,如果要更新多个字段,字段间不能使用 AND ,而应该用逗号分隔。", - Case: "update tbl set col = 1 and cl = 2 where col=3;", - Func: (*Query4Audit).RuleUpdateSetAnd, - }, - "RES.006": { - Item: "RES.006", - Severity: "L4", - Summary: "永远不真的比较条件", - Content: "查询条件永远非真,如果该条件出现在 where 中可能导致查询无匹配到的结果。", - Case: "select * from tbl where 1 != 1;", - Func: (*Query4Audit).RuleImpossibleWhere, - }, - "RES.007": { - Item: "RES.007", - Severity: "L4", - Summary: "永远为真的比较条件", - Content: "查询条件永远为真,可能导致 WHERE 条件失效进行全表查询。", - Case: "select * from tbl where 1 = 1;", - Func: (*Query4Audit).RuleMeaninglessWhere, - }, - "RES.008": { - Item: "RES.008", - Severity: "L2", - Summary: "不建议使用LOAD DATA/SELECT ... INTO OUTFILE", - Content: "SELECT INTO OUTFILE 需要授予 FILE 权限,这通过会引入安全问题。LOAD DATA 虽然可以提高数据导入速度,但同时也可能导致从库同步延迟过大。", - Case: "LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;", - Func: (*Query4Audit).RuleLoadFile, - }, - "SEC.001": { - Item: "SEC.001", - Severity: "L0", - Summary: "请谨慎使用TRUNCATE操作", - Content: `一般来说想清空一张表最快速的做法就是使用TRUNCATE TABLE tbl_name;语句。但TRUNCATE操作也并非是毫无代价的,TRUNCATE TABLE无法返回被删除的准确行数,如果需要返回被删除的行数建议使用DELETE语法。TRUNCATE 操作还会重置 AUTO_INCREMENT,如果不想重置该值建议使用 DELETE FROM tbl_name WHERE 1;替代。TRUNCATE 操作会对数据字典添加源数据锁(MDL),当一次需要 TRUNCATE 很多表时会影响整个实例的所有请求,因此如果要 TRUNCATE 多个表建议用 DROP+CREATE 的方式以减少锁时长。`, - Case: "TRUNCATE TABLE tbl_name", - Func: (*Query4Audit).RuleTruncateTable, - }, - "SEC.002": { - Item: "SEC.002", - Severity: "L0", - Summary: "不使用明文存储密码", - Content: `使用明文存储密码或者使用明文在网络上传递密码都是不安全的。如果攻击者能够截获您用来插入密码的SQL语句,他们就能直接读到密码。另外,将用户输入的字符串以明文的形式插入到纯SQL语句中,也会让攻击者发现它。如果您能够读取密码,黑客也可以。解决方案是使用单向哈希函数对原始密码进行加密编码。哈希是指将输入字符串转化成另一个新的、不可识别的字符串的函数。对密码加密表达式加点随机串来防御“字典攻击”。不要将明文密码输入到SQL查询语句中。在应用程序代码中计算哈希串,只在SQL查询中使用哈希串。`, - Case: "create table test(id int,name varchar(20) not null,password varchar(200)not null)", - Func: (*Query4Audit).RuleReadablePasswords, - }, - "SEC.003": { - Item: "SEC.003", - Severity: "L0", - Summary: "使用DELETE/DROP/TRUNCATE等操作时注意备份", - Content: `在执行高危操作之前对数据进行备份是十分有必要的。`, - Case: "delete from table where col = 'condition'", - Func: (*Query4Audit).RuleDataDrop, - }, - "STA.001": { - Item: "STA.001", - Severity: "L0", - Summary: "'!=' 运算符是非标准的", - Content: `"<>"才是标准SQL中的不等于运算符。`, - Case: "select col1,col2 from tbl where type!=0", - Func: (*Query4Audit).RuleStandardINEQ, - }, - "STA.002": { - Item: "STA.002", - Severity: "L1", - Summary: "库名或表名点后建议不要加空格", - Content: `当使用 db.table 或 table.column 格式访问表或字段时,请不要在点号后面添加空格,虽然这样语法正确。`, - Case: "select col from sakila. film", - Func: (*Query4Audit).RuleSpaceAfterDot, - }, - "STA.003": { - Item: "STA.003", - Severity: "L1", - Summary: "索引起名不规范", - Content: `建议普通二级索引以idx_为前缀,唯一索引以uk_为前缀。`, - Case: "select col from now where type!=0", - Func: (*Query4Audit).RuleIdxPrefix, - }, - "STA.004": { - Item: "STA.004", - Severity: "L1", - Summary: "起名时请不要使用字母、数字和下划线之外的字符", - Content: `以字母或下划线开头,名字只允许使用字母、数字和下划线。请统一大小写,不要使用驼峰命名法。不要在名字中出现连续下划线'__',这样很难辨认。`, - Case: "CREATE TABLE ` abc` (a int);", - Func: (*Query4Audit).RuleStandardName, - }, - "SUB.001": { - Item: "SUB.001", - Severity: "L4", - Summary: "MySQL 对子查询的优化效果不佳", - Content: `MySQL 将外部查询中的每一行作为依赖子查询执行子查询。 这是导致严重性能问题的常见原因。这可能会在 MySQL 5.6 版本中得到改善, 但对于5.1及更早版本, 建议将该类查询分别重写为 JOIN 或 LEFT OUTER JOIN。`, - Case: "select col1,col2,col3 from table1 where col2 in(select col from table2)", - Func: (*Query4Audit).RuleInSubquery, - }, - "SUB.002": { - Item: "SUB.002", - Severity: "L2", - Summary: "如果您不在乎重复的话,建议使用 UNION ALL 替代 UNION", - Content: `与去除重复的UNION不同,UNION ALL允许重复元组。如果您不关心重复元组,那么使用UNION ALL将是一个更快的选项。`, - Case: "select teacher_id as id,people_name as name from t1,t2 where t1.teacher_id=t2.people_id union select student_id as id,people_name as name from t1,t2 where t1.student_id=t2.people_id", - Func: (*Query4Audit).RuleUNIONUsage, - }, - "SUB.003": { - Item: "SUB.003", - Severity: "L3", - Summary: "考虑使用 EXISTS 而不是 DISTINCT 子查询", - Content: `DISTINCT 关键字在对元组排序后删除重复。相反,考虑使用一个带有 EXISTS 关键字的子查询,您可以避免返回整个表。`, - Case: "SELECT DISTINCT c.c_id, c.c_name FROM c,e WHERE e.c_id = c.c_id", - Func: (*Query4Audit).RuleDistinctJoinUsage, - }, - // TODO: 5.6有了semi join 还要把 in 转成 exists 么? - // Use EXISTS instead of IN to check existence of data. - // http://www.winwire.com/25-tips-to-improve-sql-query-performance/ - "SUB.004": { - Item: "SUB.004", - Severity: "L3", - Summary: "执行计划中嵌套连接深度过深", - Content: `MySQL对子查询的优化效果不佳,MySQL将外部查询中的每一行作为依赖子查询执行子查询。 这是导致严重性能问题的常见原因。`, - Case: "SELECT * from tb where id in (select id from (select id from tb))", - Func: (*Query4Audit).RuleSubqueryDepth, - }, - // SUB.005灵感来自 https://blog.csdn.net/zhuocr/article/details/61192418 - "SUB.005": { - Item: "SUB.005", - Severity: "L8", - Summary: "子查询不支持LIMIT", - Content: `当前 MySQL 版本不支持在子查询中进行 'LIMIT & IN/ALL/ANY/SOME'。`, - Case: "SELECT * FROM staff WHERE name IN (SELECT NAME FROM customer ORDER BY name LIMIT 1)", - Func: (*Query4Audit).RuleSubQueryLimit, - }, - "SUB.006": { - Item: "SUB.006", - Severity: "L2", - Summary: "不建议在子查询中使用函数", - Content: `MySQL将外部查询中的每一行作为依赖子查询执行子查询,如果在子查询中使用函数,即使是semi-join也很难进行高效的查询。可以将子查询重写为OUTER JOIN语句并用连接条件对数据进行过滤。`, - Case: "SELECT * FROM staff WHERE name IN (SELECT max(NAME) FROM customer)", - Func: (*Query4Audit).RuleSubQueryFunctions, - }, - "TBL.001": { - Item: "TBL.001", - Severity: "L4", - Summary: "不建议使用分区表", - Content: `不建议使用分区表`, - Case: "CREATE TABLE trb3(id INT, name VARCHAR(50), purchased DATE) PARTITION BY RANGE(YEAR(purchased)) (PARTITION p0 VALUES LESS THAN (1990), PARTITION p1 VALUES LESS THAN (1995), PARTITION p2 VALUES LESS THAN (2000), PARTITION p3 VALUES LESS THAN (2005) );", - Func: (*Query4Audit).RulePartitionNotAllowed, - }, - "TBL.002": { - Item: "TBL.002", - Severity: "L4", - Summary: "请为表选择合适的存储引擎", - Content: `建表或修改表的存储引擎时建议使用推荐的存储引擎,如:` + strings.Join(common.Config.TableAllowEngines, ","), - Case: "create table test(`id` int(11) NOT NULL AUTO_INCREMENT)", - Func: (*Query4Audit).RuleAllowEngine, - }, - "TBL.003": { - Item: "TBL.003", - Severity: "L8", - Summary: "以DUAL命名的表在数据库中有特殊含义", - Content: `DUAL表为虚拟表,不需要创建即可使用,也不建议服务以DUAL命名表。`, - Case: "create table dual(id int, primary key (id));", - Func: (*Query4Audit).RuleCreateDualTable, - }, - "TBL.004": { - Item: "TBL.004", - Severity: "L2", - Summary: "表的初始AUTO_INCREMENT值不为0", - Content: `AUTO_INCREMENT不为0会导致数据空洞。`, - Case: "CREATE TABLE tbl (a int) AUTO_INCREMENT = 10;", - Func: (*Query4Audit).RuleAutoIncrementInitNotZero, - }, - "TBL.005": { - Item: "TBL.005", - Severity: "L4", - Summary: "请使用推荐的字符集", - Content: `表字符集只允许设置为` + strings.Join(common.Config.TableAllowCharsets, ","), - Case: "CREATE TABLE tbl (a int) DEFAULT CHARSET = latin1;", - Func: (*Query4Audit).RuleTableCharsetCheck, - }, - "TBL.006": { - Item: "TBL.006", - Severity: "L1", - Summary: "不建议使用视图", - Content: `不建议使用视图`, - Case: "create view v_today (today) AS SELECT CURRENT_DATE;", - Func: (*Query4Audit).RuleForbiddenView, - }, - "TBL.007": { - Item: "TBL.007", - Severity: "L1", - Summary: "不建议使用临时表", - Content: `不建议使用临时表`, - Case: "CREATE TEMPORARY TABLE `work` (`time` time DEFAULT NULL) ENGINE=InnoDB;", - Func: (*Query4Audit).RuleForbiddenTempTable, - }, - } -} - -// IsIgnoreRule 判断是否是过滤规则 -// 支持XXX*前缀匹配,OK规则不可设置过滤 -func IsIgnoreRule(item string) bool { - - for _, ir := range common.Config.IgnoreRules { - ir = strings.Trim(ir, "*") - if strings.HasPrefix(item, ir) && ir != "OK" && ir != "" { - common.Log.Debug("IsIgnoreRule: %s", item) - return true - } - } - return false -} - -// InBlackList 判断一条请求是否在黑名单列表中 -// 如果在返回true,表示不需要评审 -// 注意这里没有做指纹判断,是否用指纹在这个函数的外面处理 -func InBlackList(sql string) bool { - in := false - for _, r := range common.BlackList { - if sql == r { - in = true - break - } - re, err := regexp.Compile("(?i)" + r) - if err == nil { - if re.FindString(sql) != "" { - common.Log.Debug("InBlackList: true, regexp: %s, sql: %s", "(?i)"+r, sql) - in = true - break - } - common.Log.Debug("InBlackList: false, regexp: %s, sql: %s", "(?i)"+r, sql) - } - } - return in -} - -// FormatSuggest 格式化输出优化建议 -func FormatSuggest(sql string, format string, suggests ...map[string]Rule) (map[string]Rule, string) { - common.Log.Debug("FormatSuggest, Query: %s", sql) - var fingerprint, id string - var buf []string - var score = 100 - type Result struct { - ID string - Fingerprint string - Sample string - Suggest map[string]Rule - } - - // 生成指纹和ID - if sql != "" { - fingerprint = query.Fingerprint(sql) - id = query.Id(fingerprint) - } - - // 合并重复的建议 - suggest := make(map[string]Rule) - for _, s := range suggests { - for item, rule := range s { - suggest[item] = rule - } - } - suggest = MergeConflictHeuristicRules(suggest) - - // 是否忽略显示OK建议,测试的时候大家都喜欢看OK,线上跑起来的时候OK太多反而容易看花眼 - ignoreOK := false - for _, r := range common.Config.IgnoreRules { - if "OK" == r { - ignoreOK = true - } - } - - // 先保证suggest中有元素,然后再根据ignore配置删除不需要的项 - if len(suggest) < 1 { - suggest = map[string]Rule{"OK": HeuristicRules["OK"]} - } - if ignoreOK || len(suggest) > 1 { - delete(suggest, "OK") - } - for k := range suggest { - if IsIgnoreRule(k) { - delete(suggest, k) - } - } - common.Log.Debug("FormatSuggest, format: %s", format) - switch format { - case "json": - js, err := json.MarshalIndent(Result{ - ID: id, - Fingerprint: fingerprint, - Sample: sql, - Suggest: suggest, - }, "", " ") - if err == nil { - buf = append(buf, fmt.Sprintln(string(js))) - } else { - common.Log.Error("FormatSuggest json.Marshal Error: %v", err) - } - - case "text": - for item, rule := range suggest { - buf = append(buf, fmt.Sprintln("Query: ", sql)) - buf = append(buf, fmt.Sprintln("ID: ", id)) - buf = append(buf, fmt.Sprintln("Item: ", item)) - buf = append(buf, fmt.Sprintln("Severity: ", rule.Severity)) - buf = append(buf, fmt.Sprintln("Summary: ", rule.Summary)) - buf = append(buf, fmt.Sprintln("Content: ", rule.Content)) - } - case "lint": - for item, rule := range suggest { - // lint 中无需关注 OK 和 EXP - if item != "OK" && !strings.HasPrefix(item, "EXP") { - buf = append(buf, fmt.Sprintf("%s %s", item, rule.Summary)) - } - } - - case "markdown", "html", "explain-digest", "duplicate-key-checker": - if sql != "" && len(suggest) > 0 { - switch common.Config.ExplainSQLReportType { - case "fingerprint": - buf = append(buf, fmt.Sprintf("# Query: %s\n", id)) - buf = append(buf, fmt.Sprintf("```sql\n%s\n```\n", fingerprint)) - case "sample": - buf = append(buf, fmt.Sprintf("# Query: %s\n", id)) - buf = append(buf, fmt.Sprintf("```sql\n%s\n```\n", sql)) - default: - buf = append(buf, fmt.Sprintf("# Query: %s\n", id)) - buf = append(buf, fmt.Sprintf("```sql\n%s\n```\n", ast.Pretty(sql, format))) - } - } - // MySQL - common.Log.Debug("FormatSuggest, start of sortedMySQLSuggest") - var sortedMySQLSuggest []string - for item := range suggest { - if strings.HasPrefix(item, "ERR") { - if suggest[item].Content == "" { - delete(suggest, item) - } else { - sortedMySQLSuggest = append(sortedMySQLSuggest, item) - } - } - } - sort.Strings(sortedMySQLSuggest) - if len(sortedMySQLSuggest) > 0 { - buf = append(buf, "## MySQL execute failed\n") - } - for _, item := range sortedMySQLSuggest { - buf = append(buf, fmt.Sprintln(suggest[item].Content)) - score = 0 - delete(suggest, item) - } - - // Explain - common.Log.Debug("FormatSuggest, start of sortedExplainSuggest") - if suggest["EXP.000"].Item != "" { - buf = append(buf, fmt.Sprintln("## ", suggest["EXP.000"].Summary)) - buf = append(buf, fmt.Sprintln(suggest["EXP.000"].Content)) - buf = append(buf, fmt.Sprint(suggest["EXP.000"].Case, "\n")) - delete(suggest, "EXP.000") - } - var sortedExplainSuggest []string - for item := range suggest { - if strings.HasPrefix(item, "EXP") { - sortedExplainSuggest = append(sortedExplainSuggest, item) - } - } - sort.Strings(sortedExplainSuggest) - for _, item := range sortedExplainSuggest { - buf = append(buf, fmt.Sprintln("### ", suggest[item].Summary)) - buf = append(buf, fmt.Sprintln(suggest[item].Content)) - buf = append(buf, fmt.Sprint(suggest[item].Case, "\n")) - } - - // Profiling - common.Log.Debug("FormatSuggest, start of sortedProfilingSuggest") - var sortedProfilingSuggest []string - for item := range suggest { - if strings.HasPrefix(item, "PRO") { - sortedProfilingSuggest = append(sortedProfilingSuggest, item) - } - } - sort.Strings(sortedProfilingSuggest) - if len(sortedProfilingSuggest) > 0 { - buf = append(buf, "## Profiling信息\n") - } - for _, item := range sortedProfilingSuggest { - buf = append(buf, fmt.Sprintln(suggest[item].Content)) - delete(suggest, item) - } - - // Trace - common.Log.Debug("FormatSuggest, start of sortedTraceSuggest") - var sortedTraceSuggest []string - for item := range suggest { - if strings.HasPrefix(item, "TRA") { - sortedTraceSuggest = append(sortedTraceSuggest, item) - } - } - sort.Strings(sortedTraceSuggest) - if len(sortedTraceSuggest) > 0 { - buf = append(buf, "## Trace信息\n") - } - for _, item := range sortedTraceSuggest { - buf = append(buf, fmt.Sprintln(suggest[item].Content)) - delete(suggest, item) - } - - // Index - common.Log.Debug("FormatSuggest, start of sortedIdxSuggest") - var sortedIdxSuggest []string - for item := range suggest { - if strings.HasPrefix(item, "IDX") { - sortedIdxSuggest = append(sortedIdxSuggest, item) - } - } - sort.Strings(sortedIdxSuggest) - for _, item := range sortedIdxSuggest { - buf = append(buf, fmt.Sprintln("## ", common.MarkdownEscape(suggest[item].Summary))) - buf = append(buf, fmt.Sprintln("* **Item:** ", item)) - buf = append(buf, fmt.Sprintln("* **Severity:** ", suggest[item].Severity)) - minus, err := strconv.Atoi(strings.Trim(suggest[item].Severity, "L")) - if err == nil { - score = score - minus*5 - } else { - common.Log.Debug("FormatSuggest, sortedIdxSuggest, strconv.Atoi, Error: ", err) - score = 0 - } - buf = append(buf, fmt.Sprintln("* **Content:** ", common.MarkdownEscape(suggest[item].Content))) - - if format == "duplicate-key-checker" { - buf = append(buf, fmt.Sprintf("* **原建表语句:** \n```sql\n%s\n```\n", suggest[item].Case), "\n\n") - } else { - buf = append(buf, fmt.Sprint("* **Case:** ", common.MarkdownEscape(suggest[item].Case), "\n\n")) - } - } - - // Heuristic - common.Log.Debug("FormatSuggest, start of sortedHeuristicSuggest") - var sortedHeuristicSuggest []string - for item := range suggest { - if !strings.HasPrefix(item, "EXP") && - !strings.HasPrefix(item, "IDX") && - !strings.HasPrefix(item, "PRO") { - sortedHeuristicSuggest = append(sortedHeuristicSuggest, item) - } - } - sort.Strings(sortedHeuristicSuggest) - for _, item := range sortedHeuristicSuggest { - buf = append(buf, fmt.Sprintln("## ", suggest[item].Summary)) - if item == "OK" { - continue - } - buf = append(buf, fmt.Sprintln("* **Item:** ", item)) - buf = append(buf, fmt.Sprintln("* **Severity:** ", suggest[item].Severity)) - minus, err := strconv.Atoi(strings.Trim(suggest[item].Severity, "L")) - if err == nil { - score = score - minus*5 - } else { - common.Log.Debug("FormatSuggest, sortedHeuristicSuggest, strconv.Atoi, Error: ", err) - score = 0 - } - buf = append(buf, fmt.Sprintln("* **Content:** ", common.MarkdownEscape(suggest[item].Content))) - // buf = append(buf, fmt.Sprint("* **Case:** ", common.MarkdownEscape(suggest[item].Case), "\n\n")) - } - - default: - common.Log.Debug("report-type: %s", format) - buf = append(buf, fmt.Sprintln("Query: ", sql)) - for _, rule := range suggest { - buf = append(buf, pretty.Sprint(rule)) - } - } - - // 打分 - var str string - switch common.Config.ReportType { - case "markdown", "html": - if len(buf) > 1 { - str = buf[0] + "\n" + common.Score(score) + "\n\n" + strings.Join(buf[1:], "\n") - } - default: - str = strings.Join(buf, "\n") - } - - return suggest, str -} - -// ListHeuristicRules 打印支持的启发式规则,对应命令行参数-list-heuristic-rules -func ListHeuristicRules(rules ...map[string]Rule) { - switch common.Config.ReportType { - case "json": - js, err := json.MarshalIndent(rules, "", " ") - if err == nil { - fmt.Println(string(js)) - } - default: - fmt.Print("# 启发式规则建议\n\n[toc]\n\n") - for _, r := range rules { - delete(r, "OK") - for _, item := range common.SortedKey(r) { - fmt.Print("## ", common.MarkdownEscape(r[item].Summary), - "\n\n* **Item**:", r[item].Item, - "\n* **Severity**:", r[item].Severity, - "\n* **Content**:", common.MarkdownEscape(r[item].Content), - "\n* **Case**:\n\n```sql\n", r[item].Case, "\n```\n") - } - } - } -} - -// ListTestSQLs 打印测试用的SQL,方便测试,对应命令行参数-list-test-sqls -func ListTestSQLs() { - for _, sql := range common.TestSQLs { - fmt.Println(sql) - } -} diff --git a/vendor/github.com/XiaoMi/soar/advisor/rules_test.go b/vendor/github.com/XiaoMi/soar/advisor/rules_test.go deleted file mode 100644 index 938a546..0000000 --- a/vendor/github.com/XiaoMi/soar/advisor/rules_test.go +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package advisor - -import ( - "flag" - "testing" - - "github.com/XiaoMi/soar/common" -) - -var update = flag.Bool("update", false, "update .golden files") - -func TestListTestSQLs(t *testing.T) { - err := common.GoldenDiff(func() { ListTestSQLs() }, t.Name(), update) - if nil != err { - t.Fatal(err) - } -} - -func TestListHeuristicRules(t *testing.T) { - err := common.GoldenDiff(func() { ListHeuristicRules(HeuristicRules) }, t.Name(), update) - if nil != err { - t.Fatal(err) - } -} - -func TestInBlackList(t *testing.T) { - common.BlackList = []string{"select"} - if !InBlackList("select 1") { - t.Error("should be true") - } -} - -func TestIsIgnoreRule(t *testing.T) { - common.Config.IgnoreRules = []string{"test"} - if !IsIgnoreRule("test") { - t.Error("should be true") - } -} diff --git a/vendor/github.com/XiaoMi/soar/ast/doc.go b/vendor/github.com/XiaoMi/soar/ast/doc.go deleted file mode 100644 index 43bbb74..0000000 --- a/vendor/github.com/XiaoMi/soar/ast/doc.go +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Package ast is an interface for Abstract Syntax Tree parser -package ast diff --git a/vendor/github.com/XiaoMi/soar/ast/meta.go b/vendor/github.com/XiaoMi/soar/ast/meta.go deleted file mode 100644 index 48323ff..0000000 --- a/vendor/github.com/XiaoMi/soar/ast/meta.go +++ /dev/null @@ -1,779 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ast - -import ( - "fmt" - "strings" - - "github.com/XiaoMi/soar/common" - "vitess.io/vitess/go/vt/sqlparser" -) - -// GetTableFromExprs 从sqlparser.Exprs中获取所有的库表 -func GetTableFromExprs(exprs sqlparser.TableExprs, metas ...common.Meta) common.Meta { - meta := make(map[string]*common.DB) - if len(metas) >= 1 { - meta = metas[0] - } - - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case *sqlparser.AliasedTableExpr: - - switch table := expr.Expr.(type) { - case sqlparser.TableName: - db := table.Qualifier.String() - tb := table.Name.String() - - if meta[db] == nil { - meta[db] = common.NewDB(db) - } - - meta[db].Table[tb] = common.NewTable(tb) - - // alias去重 - aliasExist := false - for _, existedAlias := range meta[db].Table[tb].TableAliases { - if existedAlias == expr.As.String() { - aliasExist = true - } - } - - if !aliasExist { - meta[db].Table[tb].TableAliases = append(meta[db].Table[tb].TableAliases, expr.As.String()) - } - } - } - return true, nil - }, exprs) - common.LogIfWarn(err, "") - return meta -} - -// GetMeta 获取元数据信息,构建到db->table层级。 -// 从 SQL 或 Statement 中获取表信息,并返回。当 meta 不为 nil 时,返回值会将新老 meta 合并去重 -func GetMeta(stmt sqlparser.Statement, meta common.Meta) common.Meta { - // 初始化meta - if meta == nil { - meta = make(map[string]*common.DB) - } - - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case *sqlparser.DDL: - // 如果 SQL 是一个 DDL,则不需要继续遍历语法树了 - for _, tb := range expr.FromTables { - appendTable(tb, "", meta) - } - - for _, tb := range expr.ToTables { - appendTable(tb, "", meta) - } - - appendTable(expr.Table, "", meta) - return false, nil - case *sqlparser.AliasedTableExpr: - // 非 DDL 情况下处理 TableExpr - // 在 sqlparser 中存在三种 TableExpr: AliasedTableExpr,ParenTableExpr 以及 JoinTableExpr。 - // 其中 AliasedTableExpr 是其他两种 TableExpr 的基础组成,SQL中的 表信息(别名、前缀)在这个结构体中。 - - switch table := expr.Expr.(type) { - - // 获取表名、别名与前缀名(数据库名) - // 表名存放在 AST 中 TableName 里,包含表名与表前缀名。 - // 当与 As 相对应的 Expr 为 TableName 的时候,别名才是一张实体表的别名,否则为结果集的别名。 - case sqlparser.TableName: - appendTable(table, expr.As.String(), meta) - default: - // 如果 AliasedTableExpr 中的 Expr 不是 TableName 结构体,则表示该表为一个查询结果集(子查询或临时表)。 - // 在这里记录一下别名,但将列名制空,用来保证在其他环节中判断列前缀的时候不会有遗漏 - // 最终结果为所有的子查询别名都会归于 ""(空) 数据库 ""(空) 表下,对于空数据库,空表后续在索引优化时直接PASS - if meta == nil { - meta = make(map[string]*common.DB) - } - - if meta[""] == nil { - meta[""] = common.NewDB("") - } - - meta[""].Table[""] = common.NewTable("") - meta[""].Table[""].TableAliases = append(meta[""].Table[""].TableAliases, expr.As.String()) - } - } - return true, nil - }, stmt) - common.LogIfWarn(err, "") - return meta -} - -// appendTable 将 sqlparser.TableName 中的库表信息提取后放到 meta 中 -// @tb 为 sqlparser.TableName 对象 -// @as 为该表的别名,无别名时为空 -// @meta 为信息集合 -func appendTable(tb sqlparser.TableName, as string, meta map[string]*common.DB) map[string]*common.DB { - if meta == nil { - return meta - } - - dbName := tb.Qualifier.String() - tbName := tb.Name.String() - if tbName == "" { - return meta - } - - if meta[dbName] == nil { - meta[dbName] = common.NewDB(dbName) - } - - meta[dbName].Table[tbName] = common.NewTable(tbName) - mergeAlias(dbName, tbName, as, meta) - - return meta -} - -// mergeAlias 将所有的表别名归并到一个表下 -func mergeAlias(db, tb, as string, meta map[string]*common.DB) { - if meta == nil || as == "" { - return - } - - aliasExist := false - for _, existedAlias := range meta[db].Table[tb].TableAliases { - if existedAlias == as { - aliasExist = true - } - } - - if !aliasExist { - meta[db].Table[tb].TableAliases = append(meta[db].Table[tb].TableAliases, as) - } -} - -// eqOperators 等值条件判断关键字 -var eqOperators = map[string]string{ - "=": "eq", - "<=>": "eq", - "is true": "eq", - "is false": "eq", - "is not true": "eq", - "is not false": "eq", - "is null": "eq", - "in": "eq", // 单值的in属于等值条件 -} - -// inEqOperators 非等值条件判断关键字 -var inEqOperators = map[string]string{ - "<": "inEq", - ">": "inEq", - "<=": "inEq", - ">=": "inEq", - "!=": "inEq", - "is not null": "inEq", - "like": "inEq", - "not like": "inEq", - "->": "inEq", - "->>": "inEq", - "between": "inEq", - "not between": "inEq", - "in": "inEq", // 多值in属于非等值条件 - - // 某些非等值条件无需添加索引,所以忽略即可 - // 比如"not in",比如"exists"、 "not exists"等 -} - -// FindColumn 从传入的 node 中获取所有可能加索引的的 column 信息 -func FindColumn(node sqlparser.SQLNode) []*common.Column { - common.Log.Debug("Enter: FindColumn, Caller: %s", common.Caller()) - var result []*common.Column - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch col := node.(type) { - case *sqlparser.FuncExpr: - // 忽略function - return false, nil - case *sqlparser.ColName: - result = common.MergeColumn(result, &common.Column{ - Name: col.Name.String(), - Table: col.Qualifier.Name.String(), - DB: col.Qualifier.Qualifier.String(), - Alias: make([]string, 0), - }) - } - - return true, nil - }, node) - common.LogIfWarn(err, "") - return result -} - -// inEqIndexAble 判断非等值查询条件是否可以复用到索引 -// Output: true 可以考虑添加索引, false 不需要添加索引 -func inEqIndexAble(node sqlparser.SQLNode) bool { - common.Log.Debug("Enter: inEqIndexAble(), Caller: %s", common.Caller()) - var indexAble bool - switch expr := node.(type) { - case *sqlparser.ComparisonExpr: - // like前百分号查询无法使用索引 - // TODO: date 类型的 like 属于隐式数据类型转换,会导致无法使用索引 - if expr.Operator == "like" || expr.Operator == "not like" { - switch right := expr.Right.(type) { - case *sqlparser.SQLVal: - return !(strings.HasPrefix(string(right.Val), "%")) - } - } - - // 如果是in查询,则需要判断in查询是否是多值查询 - if expr.Operator == "in" { - switch right := expr.Right.(type) { - case sqlparser.ValTuple: - // 若是单值查询则应该属于等值条件而非非等值条件 - return len(right) > 1 - } - } - - _, indexAble = inEqOperators[expr.Operator] - - case *sqlparser.IsExpr: - _, indexAble = inEqOperators[expr.Operator] - - case *sqlparser.RangeCond: - _, indexAble = inEqOperators[expr.Operator] - - default: - indexAble = false - } - return indexAble -} - -// FindWhereEQ 找到Where中的等值条件 -func FindWhereEQ(node sqlparser.SQLNode) []*common.Column { - common.Log.Debug("Enter: FindWhereEQ(), Caller: %s", common.Caller()) - return append(FindEQColsInWhere(node), FindEQColsInJoinCond(node)...) -} - -// FindWhereINEQ 找到Where条件中的非等值条件 -func FindWhereINEQ(node sqlparser.SQLNode) []*common.Column { - common.Log.Debug("Enter: FindWhereINEQ(), Caller: %s", common.Caller()) - return append(FindINEQColsInWhere(node), FindINEQColsInJoinCond(node)...) -} - -// FindEQColsInWhere 获取等值条件信息 -// 将所有值得加索引的condition条件信息进行过滤 -func FindEQColsInWhere(node sqlparser.SQLNode) []*common.Column { - common.Log.Debug("Enter: FindEQColsInWhere(), Caller: %s", common.Caller()) - var columns []*common.Column - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node := node.(type) { - // 对AST中所有节点进行扫描 - case *sqlparser.Subquery, *sqlparser.JoinTableExpr, *sqlparser.BinaryExpr, *sqlparser.OrExpr: - // 忽略子查询,join condition,数值计算,or condition - return false, nil - - case *sqlparser.ComparisonExpr: - var newCols []*common.Column - // ComparisonExpr中可能含有等值查询列条件 - switch node.Operator { - case "in": - // 对in进行特别判断,只有单值的in条件才算做是等值查询 - switch right := node.Right.(type) { - case sqlparser.ValTuple: - if len(right) == 1 { - newCols = FindColumn(node) - } - } - - default: - if _, ok := eqOperators[node.Operator]; ok { - newCols = FindColumn(node) - } - } - - // operator两边都为列的情况不提供索引建议 - // 如果该列位于function中则不予提供索引建议 - if len(newCols) == 1 { - columns = common.MergeColumn(columns, newCols[0]) - } - - case *sqlparser.IsExpr: - // IsExpr中可能含有等值查询列条件 - if _, ok := eqOperators[node.Operator]; ok { - newCols := FindColumn(node) - if len(newCols) == 1 { - columns = common.MergeColumn(columns, newCols[0]) - } - } - } - return true, nil - - }, node) - common.LogIfWarn(err, "") - return columns -} - -// FindINEQColsInWhere 获取非等值条件中可能需要加索引的列 -// 将所有值得加索引的condition条件信息进行过滤 -// TODO: 将 where 条件中隐含的 join 条件合并到 join condition中 -func FindINEQColsInWhere(node sqlparser.SQLNode) []*common.Column { - common.Log.Debug("Enter: FindINEQColsInWhere(), Caller: %s", common.Caller()) - var columns []*common.Column - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node := node.(type) { - // 对AST中所有节点进行扫描 - case *sqlparser.Subquery, *sqlparser.JoinTableExpr, *sqlparser.BinaryExpr, *sqlparser.OrExpr: - // 忽略子查询,join condition,数值计算,or condition - return false, nil - - case *sqlparser.ComparisonExpr: - // ComparisonExpr中可能含有非等值查询列条件 - if inEqIndexAble(node) { - newCols := FindColumn(node) - // operator两边都为列的情况不提供索引建议 - if len(newCols) == 1 { - columns = common.MergeColumn(columns, newCols[0]) - } - } - case *sqlparser.IsExpr: - // IsExpr中可能含有非等值查询列条件 - if inEqIndexAble(node) { - newCols := FindColumn(node) - if len(newCols) == 1 { - columns = common.MergeColumn(columns, newCols[0]) - } - } - - case *sqlparser.RangeCond: - // RangeCond中只存在非等值条件查询 - if inEqIndexAble(node) { - columns = common.MergeColumn(columns, FindColumn(node)...) - } - } - - return true, nil - - }, node) - common.LogIfWarn(err, "") - return columns -} - -// FindGroupByCols 获取groupBy中可能需要加索引的列信息 -func FindGroupByCols(node sqlparser.SQLNode) []*common.Column { - common.Log.Debug("Enter: FindGroupByCols(), Caller: %s", common.Caller()) - isIgnore := false - var columns []*common.Column - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case sqlparser.GroupBy: - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node := node.(type) { - case *sqlparser.BinaryExpr, *sqlparser.FuncExpr: - // 如果group by中出现数值计算、函数等 - isIgnore = true - return false, nil - default: - columns = common.MergeColumn(columns, FindColumn(node)...) - } - return true, nil - }, expr) - common.LogIfWarn(err, "") - case *sqlparser.Subquery, *sqlparser.JoinTableExpr, *sqlparser.BinaryExpr: - // 忽略子查询,join condition以及数值计算 - return false, nil - } - return true, nil - }, node) - common.LogIfWarn(err, "") - if isIgnore { - return []*common.Column{} - } - - return columns -} - -// FindOrderByCols 为索引优化获取orderBy中可能添加索引的列信息 -func FindOrderByCols(node sqlparser.SQLNode) []*common.Column { - common.Log.Debug("Enter: FindOrderByCols(), Caller: %s", common.Caller()) - var columns []*common.Column - lastDirection := "" - directionNotEq := false - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case *sqlparser.Order: - // MySQL对于排序顺序不同的查询无法使用索引(8.0后支持) - if lastDirection != "" && expr.Direction != lastDirection { - directionNotEq = true - return false, nil - } - lastDirection = expr.Direction - columns = common.MergeColumn(columns, FindColumn(expr)...) - case *sqlparser.Subquery, *sqlparser.JoinTableExpr, *sqlparser.BinaryExpr: - // 忽略子查询,join condition以及数值计算 - return false, nil - } - return true, nil - }, node) - common.LogIfWarn(err, "") - if directionNotEq { - // 当发现Order by中排序顺序不同时,即放弃Oder by条件中的字段 - return []*common.Column{} - } - - return columns -} - -// FindJoinTable 获取 Join 中需要添加索引的表 -// join 优化添加索引分为三种类型:1. inner join, 2. left join, 3.right join -// 针对三种优化类型,需要三种不同的索引添加方案: -// 1. inner join 需要对 join 左右的表添加索引 -// 2. left join 由于左表为全表扫描,需要对右表的关联列添加索引。 -// 3. right join 与 left join 相反,需要对左表的关联列添加索引。 -// 以上添加索引的策略前提为join的表为实体表而非临时表。 -func FindJoinTable(node sqlparser.SQLNode, meta common.Meta) common.Meta { - common.Log.Debug("Enter: FindJoinTable(), Caller: %s", common.Caller()) - if meta == nil { - meta = make(common.Meta) - } - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case *sqlparser.JoinTableExpr: - switch expr.Join { - case sqlparser.JoinStr, sqlparser.NaturalJoinStr: - // 两边表都需要 - findJoinTable(expr.LeftExpr, meta) - findJoinTable(expr.RightExpr, meta) - case sqlparser.LeftJoinStr, sqlparser.NaturalLeftJoinStr, sqlparser.StraightJoinStr: - // 只需要右表 - findJoinTable(expr.RightExpr, meta) - - case sqlparser.RightJoinStr, sqlparser.NaturalRightJoinStr: - // 只需要左表 - findJoinTable(expr.LeftExpr, meta) - } - } - return true, nil - }, node) - common.LogIfWarn(err, "") - return meta -} - -// findJoinTable 获取join table -func findJoinTable(expr sqlparser.TableExpr, meta common.Meta) { - common.Log.Debug("Enter: findJoinTable(), Caller: %s", common.Caller()) - if meta == nil { - meta = make(common.Meta) - } - switch tableExpr := expr.(type) { - case *sqlparser.AliasedTableExpr: - switch table := tableExpr.Expr.(type) { - // 获取表名、别名与前缀名(数据库名) - // 表名存放在 AST 中 TableName 里,包含表名与表前缀名。 - // 当与 As 相对应的 Expr 为 TableName 的时候,别名才是一张实体表的别名,否则为结果集的别名。 - case sqlparser.TableName: - db := table.Qualifier.String() - tb := table.Name.String() - - if meta == nil { - meta = make(map[string]*common.DB) - } - - if meta[db] == nil { - meta[db] = common.NewDB(db) - } - - meta[db].Table[tb] = common.NewTable(tb) - - // alias去重 - aliasExist := false - for _, existedAlias := range meta[db].Table[tb].TableAliases { - if existedAlias == tableExpr.As.String() { - aliasExist = true - } - } - if !aliasExist { - meta[db].Table[tb].TableAliases = append(meta[db].Table[tb].TableAliases, tableExpr.As.String()) - } - } - case *sqlparser.ParenTableExpr: - // join 时可能会同时 join 多张表 - for _, tbExpr := range tableExpr.Exprs { - findJoinTable(tbExpr, meta) - } - default: - // 如果是如上两种类型都没有命中,说明join的表为临时表,递归调用 FindJoinTable 继续下探查找。 - // NOTE: 这里需要注意的是,如果不递归寻找,如果存在子查询结果集的join表,subquery也会把这个查询提取出。 - // 所以针对default这一段理论上可以忽略处理(待测试) - FindJoinTable(tableExpr, meta) - } -} - -// FindJoinCols 获取 join condition 中使用到的列(必须是 `列 operator 列` 的情况。 -// 如果列对应的值或是 function,则应该移到where condition中) -// 某些 where 条件隐含在 join 条件中(INNER JOIN) -func FindJoinCols(node sqlparser.SQLNode) [][]*common.Column { - common.Log.Debug("Enter: FindJoinCols(), Caller: %s", common.Caller()) - var columns [][]*common.Column - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case *sqlparser.JoinTableExpr: - // on - if on := expr.Condition.On; on != nil { - cols := FindColumn(expr.Condition.On) - if len(cols) > 1 { - columns = append(columns, cols) - } - } - - // using - if using := expr.Condition.Using; using != nil { - left := "" - right := "" - - switch tableExpr := expr.LeftExpr.(type) { - case *sqlparser.AliasedTableExpr: - switch table := tableExpr.Expr.(type) { - case sqlparser.TableName: - left = table.Name.String() - } - } - - switch tableExpr := expr.RightExpr.(type) { - case *sqlparser.AliasedTableExpr: - switch table := tableExpr.Expr.(type) { - case sqlparser.TableName: - right = table.Name.String() - } - } - - var cols []*common.Column - for _, col := range using { - if left != "" { - cols = append(cols, &common.Column{ - Name: col.String(), - Table: left, - Alias: make([]string, 0), - }) - } - - if right != "" { - cols = append(cols, &common.Column{ - Name: col.String(), - Table: right, - Alias: make([]string, 0), - }) - } - - } - columns = append(columns, cols) - - } - - } - return true, nil - }, node) - common.LogIfWarn(err, "") - return columns -} - -// FindEQColsInJoinCond 获取 join condition 中应转为whereEQ条件的列 -func FindEQColsInJoinCond(node sqlparser.SQLNode) []*common.Column { - common.Log.Debug("Enter: FindEQColsInJoinCond(), Caller: %s", common.Caller()) - var columns []*common.Column - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case sqlparser.JoinCondition: - columns = append(columns, FindEQColsInWhere(expr)...) - } - return true, nil - }, node) - common.LogIfWarn(err, "") - return columns -} - -// FindINEQColsInJoinCond 获取 join condition 中应转为whereINEQ条件的列 -func FindINEQColsInJoinCond(node sqlparser.SQLNode) []*common.Column { - common.Log.Debug("Enter: FindINEQColsInJoinCond(), Caller: %s", common.Caller()) - var columns []*common.Column - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case sqlparser.JoinCondition: - columns = append(columns, FindINEQColsInWhere(expr)...) - } - return true, nil - }, node) - common.LogIfWarn(err, "") - return columns -} - -// FindSubquery 拆分subquery,获取最深层的subquery -// 为索引优化获取subquery中包含的列信息 -func FindSubquery(depth int, node sqlparser.SQLNode, queries ...string) []string { - common.Log.Debug("Enter: FindSubquery(), Caller: %s", common.Caller()) - if queries == nil { - queries = make([]string, 0) - } - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - // 查找SQL中的子查询 - case *sqlparser.Subquery: - noSub := true - // 查看子查询中是否还包含子查询,如果包含,递归找到最深层的子查询 - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch sub := node.(type) { - case *sqlparser.Subquery: - noSub = false - // 查找深度depth,超过最大深度后不再向下查找 - depth = depth + 1 - if depth < common.Config.MaxSubqueryDepth { - queries = append(queries, FindSubquery(depth, sub.Select)...) - } - } - return true, nil - }, expr.Select) - common.LogIfWarn(err, "") - - // 如果没有嵌套的子查询了,返回子查询的SQL - if noSub { - sql := sqlparser.String(expr) - // 去除SQL前后的括号 - queries = append(queries, sql[1:len(sql)-1]) - } - - } - return true, nil - }, node) - common.LogIfWarn(err, "") - return queries -} - -// FindAllCondition 获取 AST 中所有的 condition 条件 -func FindAllCondition(node sqlparser.SQLNode) []interface{} { - common.Log.Debug("Enter: FindAllCondition(), Caller: %s", common.Caller()) - var conditions []interface{} - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node := node.(type) { - case *sqlparser.ComparisonExpr, *sqlparser.RangeCond, *sqlparser.IsExpr: - conditions = append(conditions, node) - } - return true, nil - }, node) - common.LogIfWarn(err, "") - return conditions -} - -// Expression describe sql expression type -type Expression string - -const ( - // WhereExpression 用于标记 where - WhereExpression Expression = "where" - // JoinExpression 用于标记 join - JoinExpression Expression = "join" - // GroupByExpression 用于标记 group by - GroupByExpression Expression = "group by" - // OrderByExpression 用于标记 order by - OrderByExpression Expression = "order by" -) - -// FindAllCols 获取 AST 中某个节点下所有的 columns -func FindAllCols(node sqlparser.SQLNode, targets ...Expression) []*common.Column { - var result []*common.Column - // 获取节点内所有的列 - f := func(node sqlparser.SQLNode) { - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch col := node.(type) { - case *sqlparser.ColName: - result = common.MergeColumn(result, &common.Column{ - Name: col.Name.String(), - Table: col.Qualifier.Name.String(), - DB: col.Qualifier.Qualifier.String(), - Alias: make([]string, 0), - }) - } - return true, nil - }, node) - common.LogIfWarn(err, "") - } - - if len(targets) == 0 { - // 如果不指定具体节点类型,则获取全部的column - f(node) - } else { - // 根据target获取所有的节点 - for _, target := range targets { - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node := node.(type) { - case *sqlparser.Subquery: - // 忽略子查询 - case *sqlparser.JoinTableExpr: - if target == JoinExpression { - f(node) - } - case *sqlparser.Where: - if target == WhereExpression { - f(node) - } - case sqlparser.GroupBy: - if target == GroupByExpression { - f(node) - } - case sqlparser.OrderBy: - if target == OrderByExpression { - f(node) - } - } - return true, nil - }, node) - common.LogIfWarn(err, "") - } - } - - return result -} - -// GetSubqueryDepth 获取一条SQL的嵌套深度 -func GetSubqueryDepth(node sqlparser.SQLNode) int { - depth := 1 - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node.(type) { - case *sqlparser.Subquery: - depth++ - } - return true, nil - }, node) - common.LogIfWarn(err, "") - return depth -} - -// getColumnName 获取 node 中 column 具体的定义以及名称 -func getColumnName(node sqlparser.SQLNode) (*sqlparser.ColName, string) { - var colName *sqlparser.ColName - str := "" - switch c := node.(type) { - case *sqlparser.ColName: - if c.Qualifier.Name.IsEmpty() { - str = fmt.Sprintf("`%s`", c.Name.String()) - } else { - if c.Qualifier.Qualifier.IsEmpty() { - str = fmt.Sprintf("`%s`.`%s`", c.Qualifier.Name.String(), c.Name.String()) - } else { - str = fmt.Sprintf("`%s`.`%s`.`%s`", - c.Qualifier.Qualifier.String(), c.Qualifier.Name.String(), c.Name.String()) - } - } - colName = c - } - return colName, str -} diff --git a/vendor/github.com/XiaoMi/soar/ast/meta_test.go b/vendor/github.com/XiaoMi/soar/ast/meta_test.go deleted file mode 100644 index f072978..0000000 --- a/vendor/github.com/XiaoMi/soar/ast/meta_test.go +++ /dev/null @@ -1,370 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ast - -import ( - "fmt" - "testing" - - "github.com/XiaoMi/soar/common" - - "github.com/kr/pretty" - "vitess.io/vitess/go/vt/sqlparser" -) - -func TestGetTableFromExprs(t *testing.T) { - tbExprs := sqlparser.TableExprs{ - &sqlparser.AliasedTableExpr{ - Expr: sqlparser.TableName{ - Name: sqlparser.NewTableIdent("table"), - Qualifier: sqlparser.NewTableIdent("db"), - }, - As: sqlparser.NewTableIdent("as"), - }, - } - meta := GetTableFromExprs(tbExprs) - if tb, ok := meta["db"]; !ok { - t.Errorf("no table qualifier, meta: %s", pretty.Sprint(tb)) - } -} - -func TestGetParseTableWithStmt(t *testing.T) { - for _, sql := range common.TestSQLs { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - if err != nil { - t.Errorf("SQL Parsed error: %v", err) - } - meta := GetMeta(stmt, nil) - pretty.Println(meta) - fmt.Println() - } -} - -func TestFindCondition(t *testing.T) { - for _, sql := range common.TestSQLs { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - // pretty.Println(stmt) - if err != nil { - panic(err) - } - eq := FindEQColsInWhere(stmt) - inEq := FindINEQColsInWhere(stmt) - fmt.Println("WhereEQ:") - pretty.Println(eq) - fmt.Println("WhereINEQ:") - pretty.Println(inEq) - fmt.Println() - } -} - -func TestFindGroupBy(t *testing.T) { - sqlList := []string{ - "select a from t group by c", - } - - for _, sql := range sqlList { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - if err != nil { - panic(err) - } - res := FindGroupByCols(stmt) - pretty.Println(res) - fmt.Println() - } -} - -func TestFindOrderBy(t *testing.T) { - sqlList := []string{ - "select a from t group by c order by d, c desc", - "select a from t group by c order by d desc", - } - - for _, sql := range sqlList { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - if err != nil { - panic(err) - } - res := FindOrderByCols(stmt) - pretty.Println(res) - fmt.Println() - } -} - -func TestFindSubquery(t *testing.T) { - sqlList := []string{ - "SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM (SELECT column1 FROM t2) a);", - "select column1 from t2", - "SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);", - "select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;", - } - - for _, sql := range sqlList { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - if err != nil { - panic(err) - } - - subquery := FindSubquery(0, stmt) - fmt.Println(len(subquery)) - pretty.Println(subquery) - } - -} - -func TestFindJoinTable(t *testing.T) { - sqlList := []string{ - "SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;", - "SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "SELECT * FROM t1 RIGHT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - "SELECT left_tbl.* FROM left_tbl RIGHT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - } - - for _, sql := range sqlList { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - // pretty.Println(stmt) - if err != nil { - panic(err) - } - - joinMeta := FindJoinTable(stmt, nil) - pretty.Println(joinMeta) - } -} - -func TestFindJoinCols(t *testing.T) { - sqlList := []string{ - "SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "select t from a LEFT JOIN b USING (c1, c2, c3)", - "select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;", - "SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "SELECT * FROM t1 RIGHT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - "SELECT left_tbl.* FROM left_tbl RIGHT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - } - - for _, sql := range sqlList { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - // pretty.Println(stmt) - if err != nil { - panic(err) - } - - columns := FindJoinCols(stmt) - pretty.Println(columns) - } -} - -func TestFindJoinColBeWhereEQ(t *testing.T) { - sqlList := []string{ - "select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;", - "SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "SELECT * FROM t1 RIGHT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - "SELECT left_tbl.* FROM left_tbl RIGHT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - } - - for _, sql := range sqlList { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - // pretty.Println(stmt) - if err != nil { - panic(err) - } - - columns := FindEQColsInJoinCond(stmt) - pretty.Println(columns) - } -} - -func TestFindJoinColBeWhereINEQ(t *testing.T) { - sqlList := []string{ - "select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;", - "SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "SELECT * FROM t1 RIGHT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b > 'b' AND t4.c = t1.c)", - "SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - "SELECT left_tbl.* FROM left_tbl RIGHT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - } - - for _, sql := range sqlList { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - // pretty.Println(stmt) - if err != nil { - panic(err) - } - - columns := FindINEQColsInJoinCond(stmt) - pretty.Println(columns) - } -} - -func TestFindAllCondition(t *testing.T) { - sqlList := []string{ - "SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "select t from a LEFT JOIN b USING (c1, c2, c3)", - "select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;", - "SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "SELECT * FROM t1 RIGHT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - "SELECT left_tbl.* FROM left_tbl RIGHT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - "SELECT * FROM t1 where a in ('a','b')", - "SELECT * FROM t1 where a BETWEEN 'bar' AND 'foo'", - "SELECT * FROM t1 where a = sum(a,b)", - "SELECT distinct a FROM t1 where a = '2001-01-01 01:01:01'", - } - - for _, sql := range sqlList { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - // pretty.Println(stmt) - if err != nil { - panic(err) - } - - columns := FindAllCondition(stmt) - pretty.Println(columns) - } -} - -func TestFindColumn(t *testing.T) { - sqlList := []string{ - "select col, col2, sum(col1) from tb group by col", - "select col from tb group by col,sum(col1)", - "select col, sum(col1) from tb group by col", - } - for _, sql := range sqlList { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - // pretty.Println(stmt) - if err != nil { - panic(err) - } - - columns := FindColumn(stmt) - pretty.Println(columns) - } -} - -func TestFindAllCols(t *testing.T) { - sqlList := []string{ - "select * from tb where a = '1' order by c", - "select * from tb where a = '1' group by c", - "select * from tb where c = '1' group by a", - "select * from tb join tb2 on c = c where c = '1' group by a", - } - - targets := []Expression{ - OrderByExpression, - GroupByExpression, - WhereExpression, - JoinExpression, - } - - for i, sql := range sqlList { - stmt, err := sqlparser.Parse(sql) - if err != nil { - t.Error(err) - return - } - - columns := FindAllCols(stmt, targets[i]) - if columns[0].Name != "c" { - fmt.Println(sql) - t.Error(fmt.Errorf("want 'c' got %v", columns)) - } - } -} - -func TestGetSubqueryDepth(t *testing.T) { - sqlList := []string{ - "SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "select t from a LEFT JOIN b USING (c1, c2, c3)", - "select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;", - "SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "SELECT * FROM t1 RIGHT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)", - "SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - "SELECT left_tbl.* FROM left_tbl RIGHT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;", - "SELECT * FROM t1 where a in ('a','b')", - "SELECT * FROM t1 where a BETWEEN 'bar' AND 'foo'", - "SELECT * FROM t1 where a = sum(a,b)", - "SELECT distinct a FROM t1 where a = '2001-01-01 01:01:01'", - } - - for _, sql := range sqlList { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - if err != nil { - t.Error("syntax check error.") - } - - dep := GetSubqueryDepth(stmt) - fmt.Println(dep) - } -} - -func TestAppendTable(t *testing.T) { - sqlList := []string{ - "select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;", - } - - meta := make(map[string]*common.DB) - for _, sql := range sqlList { - fmt.Println(sql) - stmt, err := sqlparser.Parse(sql) - if err != nil { - t.Error("syntax check error.") - } - - err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch expr := node.(type) { - case *sqlparser.AliasedTableExpr: - switch table := expr.Expr.(type) { - case sqlparser.TableName: - appendTable(table, expr.As.String(), meta) - default: - if meta == nil { - meta = make(map[string]*common.DB) - } - if meta[""] == nil { - meta[""] = common.NewDB("") - } - meta[""].Table[""] = common.NewTable("") - meta[""].Table[""].TableAliases = append(meta[""].Table[""].TableAliases, expr.As.String()) - } - } - return true, nil - }, stmt) - - if err != nil { - t.Error(err) - } - } - - // 仅对第一条测试SQL进行测试,验证别名正确性 - if meta[""].Table["customer_list"].TableAliases[0] != "l" || meta[""].Table["city"].TableAliases[0] != "c" { - t.Error("alias filed\n", pretty.Sprint(meta)) - } -} diff --git a/vendor/github.com/XiaoMi/soar/ast/node_array.go b/vendor/github.com/XiaoMi/soar/ast/node_array.go deleted file mode 100644 index e8f7952..0000000 --- a/vendor/github.com/XiaoMi/soar/ast/node_array.go +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ast - -import ( - "errors" - - "github.com/XiaoMi/soar/common" - "vitess.io/vitess/go/vt/sqlparser" -) - -// 该文件用于构造一个存储AST生成节点的链表 -// 以能够更好的对AST中的每个节点进行查询、跳转、重建等 - -// NodeItem 链表节点 -type NodeItem struct { - ID int // NodeItem 在 List 中的编号,与顺序有关 - Prev *NodeItem // 前一个节点 - Self sqlparser.SQLNode // 自身指向的AST Node - Next *NodeItem // 后一个节点 - Array *NodeList // 指针指向所在的链表,用于快速跳转node -} - -// NodeList 链表结构体 -type NodeList struct { - Length int - Head *NodeItem - NodeMap map[int]*NodeItem -} - -// NewNodeList 从抽象语法树中构造一个链表 -func NewNodeList(statement sqlparser.Statement) *NodeList { - // 将AST构造成链表 - l := &NodeList{NodeMap: make(map[int]*NodeItem)} - err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - l.Add(node) - return true, nil - }, statement) - common.LogIfWarn(err, "") - return l -} - -// Add 将会把一个sqlparser.SQLNode添加到节点中 -func (l *NodeList) Add(node sqlparser.SQLNode) *NodeItem { - if l.Length == 0 { - l.Head = &NodeItem{ - ID: 0, - Self: node, - Next: nil, - Prev: nil, - Array: l, - } - l.NodeMap[l.Length] = l.Head - } else { - if n, ok := l.NodeMap[l.Length-1]; ok { - n.Next = &NodeItem{ - ID: l.Length - 1, - Prev: n, - Self: node, - Next: nil, - Array: l, - } - l.NodeMap[l.Length] = n.Next - } - } - l.Length++ - - return l.NodeMap[l.Length-1] -} - -// Remove 从链表中移除一个节点 -func (l *NodeList) Remove(node *NodeItem) error { - var err error - defer func() { - err := recover() - if err != nil { - common.Log.Error("func (l *NodeList) Remove recovered: %v", err) - } - }() - - if node.Array != l { - return errors.New("node not belong to this array") - } - - if node.Prev == nil { - // 如果是头结点 - node.Next.Prev = nil - } else if node.Next == nil { - // 如果是尾节点 - node.Prev.Next = nil - } else { - // 删除节点,连接断开的链表 - node.Prev.Next = node.Next - node.Next.Prev = node.Prev - delete(l.NodeMap, node.ID) - } - - return err -} - -// First 返回链表头结点 -func (l *NodeList) First() *NodeItem { - return l.Head -} - -// Last 返回链表末尾节点 -func (l *NodeList) Last() *NodeItem { - return l.NodeMap[l.Length-1] -} diff --git a/vendor/github.com/XiaoMi/soar/ast/pretty.go b/vendor/github.com/XiaoMi/soar/ast/pretty.go deleted file mode 100644 index 6e4862f..0000000 --- a/vendor/github.com/XiaoMi/soar/ast/pretty.go +++ /dev/null @@ -1,350 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ast - -import ( - "container/list" - "regexp" - "strings" - - "github.com/XiaoMi/soar/common" - - "github.com/percona/go-mysql/query" -) - -// Pretty 格式化输出SQL -func Pretty(sql string, method string) (output string) { - common.Log.Debug("Pretty, Query: %s, method: %s", sql, method) - // 超出 Config.MaxPrettySQLLength 长度的 SQL 会对其指纹进行 pretty - if len(sql) > common.Config.MaxPrettySQLLength { - fingerprint := query.Fingerprint(sql) - // 超出 Config.MaxFpPrettySqlLength 长度的指纹不会进行pretty - if len(fingerprint) > common.Config.MaxPrettySQLLength { - return sql - } - sql = fingerprint - } - - switch method { - case "builtin", "markdown": - return format(sql) - default: - return sql - } -} - -// format the whitespace in a SQL string to make it easier to read. -// @param string $query The SQL string -// @return String The SQL string with HTML styles and formatting wrapped in a
 tag
-func format(query string) string {
-	// This variable will be populated with formatted html
-	result := ""
-	// Use an actual tab while formatting and then switch out with self::$tab at the end
-	tab := "  "
-	indentLevel := 0
-	var newline bool
-	var inlineParentheses bool
-	var increaseSpecialIndent bool
-	var increaseBlockIndent bool
-	var addedNewline bool
-	var inlineCount int
-	var inlineIndented bool
-	var clauseLimit bool
-	indentTypes := list.New()
-
-	// Tokenize String
-	originalTokens := Tokenize(query)
-
-	// Remove existing whitespace//
-	var tokens []Token
-	for i, token := range originalTokens {
-		if token.Type != TokenTypeWhitespace {
-			token.i = i
-			tokens = append(tokens, token)
-		}
-	}
-
-	for i, token := range tokens {
-		highlighted := token.Val
-
-		// If we are increasing the special indent level now
-		if increaseSpecialIndent {
-			indentLevel++
-			increaseSpecialIndent = false
-			indentTypes.PushFront("special")
-		}
-
-		// If we are increasing the block indent level now
-		if increaseBlockIndent {
-			indentLevel++
-			increaseBlockIndent = false
-			indentTypes.PushFront("block")
-		}
-
-		// If we need a new line before the token
-		if newline {
-			result += "\n" + strings.Repeat(tab, indentLevel)
-			newline = false
-			addedNewline = true
-		} else {
-			addedNewline = false
-		}
-
-		// Display comments directly where they appear in the source
-		if token.Type == TokenTypeComment || token.Type == TokenTypeBlockComment {
-			if token.Type == TokenTypeBlockComment {
-				indent := strings.Repeat(tab, indentLevel)
-				result += "\n" + indent
-				highlighted = strings.Replace(highlighted, "\n", "\n"+indent, -1)
-			}
-
-			result += highlighted
-			newline = true
-			continue
-		}
-
-		if inlineParentheses {
-			// End of inline parentheses
-			if token.Val == ")" {
-				result = strings.TrimRight(result, " ")
-
-				if inlineIndented {
-					indentTypes.Remove(indentTypes.Front())
-					if indentLevel > 0 {
-						indentLevel--
-					}
-					result += strings.Repeat(tab, indentLevel)
-				}
-
-				inlineParentheses = false
-
-				result += highlighted + " "
-				continue
-			}
-
-			if token.Val == "," {
-				if inlineCount >= 30 {
-					inlineCount = 0
-					newline = true
-				}
-			}
-
-			inlineCount += len(token.Val)
-		}
-
-		// Opening parentheses increase the block indent level and start a new line
-		if token.Val == "(" {
-			// First check if this should be an inline parentheses block
-			// Examples are "NOW()", "COUNT(*)", "int(10)", key(`somecolumn`), DECIMAL(7,2)
-			// Allow up to 3 non-whitespace tokens inside inline parentheses
-			length := 0
-			for j := 1; j <= 250; j++ {
-				// Reached end of string
-				if i+j >= len(tokens) {
-					break
-				}
-
-				next := tokens[i+j]
-
-				// Reached closing parentheses, able to inline it
-				if next.Val == ")" {
-					inlineParentheses = true
-					inlineCount = 0
-					inlineIndented = false
-					break
-				}
-
-				// Reached an invalid token for inline parentheses
-				if next.Val == ";" || next.Val == "(" {
-					break
-				}
-
-				// Reached an invalid token type for inline parentheses
-				if next.Type == TokenTypeReservedToplevel ||
-					next.Type == TokenTypeReservedNewline ||
-					next.Type == TokenTypeComment ||
-					next.Type == TokenTypeBlockComment {
-					break
-				}
-
-				length += len(next.Val)
-			}
-
-			if inlineParentheses && length > 30 {
-				increaseBlockIndent = true
-				inlineIndented = true
-				newline = true
-			}
-
-			// Take out the preceding space unless there was whitespace there in the original query
-			if token.i != 0 && (token.i-1) > len(originalTokens)-1 &&
-				originalTokens[token.i-1].Type != TokenTypeWhitespace {
-
-				result = strings.TrimRight(result, " ")
-			}
-
-			if inlineParentheses {
-				increaseBlockIndent = true
-				// Add a newline after the parentheses
-				newline = true
-			}
-
-		} else if token.Val == ")" {
-			// Closing parentheses decrease the block indent level
-			// Remove whitespace before the closing parentheses
-			result = strings.TrimRight(result, " ")
-
-			if indentLevel > 0 {
-				indentLevel--
-			}
-
-			// Reset indent level
-			for j := indentTypes.Front(); indentTypes.Len() > 0; indentTypes.Remove(j) {
-				if j.Value.(string) == "special" {
-					if indentLevel > 0 {
-						indentLevel--
-					} else {
-						break
-					}
-				} else {
-					break
-				}
-			}
-
-			if indentLevel < 0 {
-				// This is an error
-				indentLevel = 0
-			}
-
-			// Add a newline before the closing parentheses (if not already added)
-			if !addedNewline {
-				result += "\n" + strings.Repeat(tab, indentLevel)
-			}
-
-		} else if token.Type == TokenTypeReservedToplevel {
-			// Top level reserved words start a new line and increase the special indent level
-			increaseSpecialIndent = true
-
-			// If the last indent type was 'special', decrease the special indent for this round
-			if indentTypes.Len() > 0 && indentTypes.Front().Value.(string) == "special" {
-				if indentLevel > 0 {
-					indentLevel--
-				}
-				indentTypes.Remove(indentTypes.Front())
-			}
-
-			// Add a newline after the top level reserved word
-			newline = true
-			// Add a newline before the top level reserved word (if not already added)
-			if !addedNewline {
-				result += "\n" + strings.Repeat(tab, indentLevel)
-			} else {
-				// If we already added a newline, redo the indentation since it may be different now
-				result = strings.TrimSuffix(result, tab) + strings.Repeat(tab, indentLevel)
-			}
-
-			// If the token may have extra whitespace
-			if strings.Index(token.Val, " ") != 0 ||
-				strings.Index(token.Val, "\n") != 0 ||
-				strings.Index(token.Val, "\t") != 0 {
-
-				re, _ := regexp.Compile(`\s+`)
-				highlighted = re.ReplaceAllString(highlighted, " ")
-
-			}
-
-			//if SQL 'LIMIT' clause, start variable to reset newline
-			if token.Val == "LIMIT" && inlineParentheses {
-				clauseLimit = true
-			}
-
-		} else if clauseLimit && token.Val != "," &&
-			token.Type != TokenTypeNumber &&
-			token.Type != TokenTypeWhitespace {
-			// Checks if we are out of the limit clause
-
-			clauseLimit = false
-
-		} else if token.Val == "," && !inlineParentheses {
-			// Commas start a new line (unless within inline parentheses or SQL 'LIMIT' clause)
-			if clauseLimit {
-				newline = false
-				clauseLimit = false
-			} else {
-				// All other cases of commas
-				newline = true
-			}
-
-		} else if token.Type == TokenTypeReservedNewline {
-			// Newline reserved words start a new line
-			// Add a newline before the reserved word (if not already added)
-			if !addedNewline {
-				result += "\n" + strings.Repeat(tab, indentLevel)
-			}
-
-			// If the token may have extra whitespace
-			if strings.Index(token.Val, " ") != 0 ||
-				strings.Index(token.Val, "\n") != 0 ||
-				strings.Index(token.Val, "\t") != 0 {
-
-				re, _ := regexp.Compile(`\s+`)
-				highlighted = re.ReplaceAllString(highlighted, " ")
-			}
-
-		} else if token.Type == TokenTypeBoundary {
-			// Multiple boundary characters in a row should not have spaces between them (not including parentheses)
-			if i != 0 && i < len(tokens) &&
-				tokens[i-1].Type == TokenTypeBoundary {
-
-				if token.i != 0 && token.i < len(originalTokens) &&
-					originalTokens[token.i-1].Type != TokenTypeWhitespace {
-
-					result = strings.TrimRight(result, " ")
-				}
-			}
-		}
-
-		// If the token shouldn't have a space before it
-		if token.Val == "." || token.Val == "," || token.Val == ";" {
-			result = strings.TrimRight(result, " ")
-		}
-
-		result += highlighted + " "
-
-		// If the token shouldn't have a space after it
-		if token.Val == "(" || token.Val == "." {
-			result = strings.TrimRight(result, " ")
-		}
-
-		// If this is the "-" of a negative number, it shouldn't have a space after it
-		if token.Val == "-" && i+1 < len(tokens) && tokens[i+1].Type == TokenTypeNumber && i != 0 {
-			prev := tokens[i-1].Type
-			if prev != TokenTypeQuote &&
-				prev != TokenTypeBacktickQuote &&
-				prev != TokenTypeWord &&
-				prev != TokenTypeNumber {
-
-				result = strings.TrimRight(result, " ")
-			}
-		}
-	}
-
-	// Replace tab characters with the configuration tab character
-	result = strings.TrimRight(strings.Replace(result, "\t", tab, -1), " ")
-
-	return result
-}
diff --git a/vendor/github.com/XiaoMi/soar/ast/pretty_test.go b/vendor/github.com/XiaoMi/soar/ast/pretty_test.go
deleted file mode 100644
index 2046cad..0000000
--- a/vendor/github.com/XiaoMi/soar/ast/pretty_test.go
+++ /dev/null
@@ -1,201 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package ast
-
-import (
-	"flag"
-	"fmt"
-	"testing"
-
-	"github.com/XiaoMi/soar/common"
-
-	"vitess.io/vitess/go/vt/sqlparser"
-)
-
-var update = flag.Bool("update", false, "update .golden files")
-
-var TestSqlsPretty = []string{
-	"select sourcetable, if(f.lastcontent = ?, f.lastupdate, f.lastcontent) as lastactivity, f.totalcount as activity, type.class as type, (f.nodeoptions & ?) as nounsubscribe from node as f inner join contenttype as type on type.contenttypeid = f.contenttypeid inner join subscribed as sd on sd.did = f.nodeid and sd.userid = ? union all select f.name as title, f.userid as keyval, ? as sourcetable, ifnull(f.lastpost, f.joindate) as lastactivity, f.posts as activity, ? as type, ? as nounsubscribe from user as f inner join userlist as ul on ul.relationid = f.userid and ul.userid = ? where ul.type = ? and ul.aq = ? order by title limit ?",
-	"administrator command: Init DB",
-	"CALL foo(1, 2, 3)",
-	"### Channels ###\n\u0009\u0009\u0009\u0009\u0009SELECT sourcetable, IF(f.lastcontent = 0, f.lastupdate, f.lastcontent) AS lastactivity,\n\u0009\u0009\u0009\u0009\u0009f.totalcount AS activity, type.class AS type,\n\u0009\u0009\u0009\u0009\u0009(f.nodeoptions \u0026 512) AS noUnsubscribe\n\u0009\u0009\u0009\u0009\u0009FROM node AS f\n\u0009\u0009\u0009\u0009\u0009INNER JOIN contenttype AS type ON type.contenttypeid = f.contenttypeid \n\n\u0009\u0009\u0009\u0009\u0009INNER JOIN subscribed AS sd ON sd.did = f.nodeid AND sd.userid = 15965\n UNION  ALL \n\n\u0009\u0009\u0009\u0009\u0009### Users ###\n\u0009\u0009\u0009\u0009\u0009SELECT f.name AS title, f.userid AS keyval, 'user' AS sourcetable, IFNULL(f.lastpost, f.joindate) AS lastactivity,\n\u0009\u0009\u0009\u0009\u0009f.posts as activity, 'Member' AS type,\n\u0009\u0009\u0009\u0009\u00090 AS noUnsubscribe\n\u0009\u0009\u0009\u0009\u0009FROM user AS f\n\u0009\u0009\u0009\u0009\u0009INNER JOIN userlist AS ul ON ul.relationid = f.userid AND ul.userid = 15965\n\u0009\u0009\u0009\u0009\u0009WHERE ul.type = 'f' AND ul.aq = 'yes'\n ORDER BY title ASC LIMIT 100",
-	"CREATE DATABASE org235_percona345 COLLATE 'utf8_general_ci'",
-	"insert into abtemp.coxed select foo.bar from foo",
-	"insert into foo(a, b, c) value(2, 4, 5)",
-	"insert into foo(a, b, c) values(2, 4, 5)",
-	"insert into foo(a, b, c) values(2, 4, 5) , (2,4,5)",
-	"insert into foo values (1, '(2)', 'This is a trick: ). More values.', 4)",
-	"insert into tb values (1)",
-	"INSERT INTO t (ts) VALUES ('()', '\\(', '\\)')",
-	"INSERT INTO t (ts) VALUES (NOW())",
-	"INSERT INTO t () VALUES ()",
-	"insert into t values (1), (2), (3)\n\n\ton duplicate key update query_count=1",
-	"insert into t values (1) on duplicate key update query_count=COALESCE(query_count, 0) + VALUES(query_count)",
-	"LOAD DATA INFILE '/tmp/foo.txt' INTO db.tbl",
-	"select 0e0, +6e-30, -6.00 from foo where a = 5.5 or b=0.5 or c=.5",
-	"select 0x0, x'123', 0b1010, b'10101' from foo",
-	"select 123_foo from 123_foo",
-	"select 123foo from 123foo",
-	`SELECT 	1 AS one FROM calls USE INDEX(index_name)`,
-	"SELECT /*!40001 SQL_NO_CACHE */ * FROM `film`",
-	"SELECT 'a' 'b' 'c' 'd' FROM kamil",
-	"SELECT BENCHMARK(100000000, pow(rand(), rand())), 1 FROM `-hj-7d6-shdj5-7jd-kf-g988h-`.`-aaahj-7d6-shdj5-7&^%$jd-kf-g988h-9+4-5*6ab-`",
-	"SELECT c FROM org235.t WHERE id=0xdeadbeaf",
-	"select c from t where i=1 order by c asc",
-	"SELECT c FROM t WHERE id=0xdeadbeaf",
-	"SELECT c FROM t WHERE id=1",
-	"select `col` from `table-1` where `id` = 5",
-	"SELECT `db`.*, (CASE WHEN (`date_start` <=  '2014-09-10 09:17:59' AND `date_end` >=  '2014-09-10 09:17:59') THEN 'open' WHEN (`date_start` >  '2014-09-10 09:17:59' AND `date_end` >  '2014-09-10 09:17:59') THEN 'tbd' ELSE 'none' END) AS `status` FROM `foo` AS `db` WHERE (a_b in ('1', '10101'))",
-	"select field from `-master-db-1`.`-table-1-` order by id, ?;",
-	"select   foo",
-	"select foo_1 from foo_2_3",
-	"select foo -- bar\n",
-	"select foo-- bar\n,foo",
-	"select '\\\\' from foo",
-	"select * from foo limit 5",
-	"select * from foo limit 5, 10",
-	"select * from foo limit 5 offset 10",
-	"SELECT * from foo where a = 5",
-	"select * from foo where a in (5) and b in (5, 8,9 ,9 , 10)",
-	"SELECT '' '' '' FROM kamil",
-	" select  * from\nfoo where a = 5",
-	"SELECT * FROM prices.rt_5min where id=1",
-	"SELECT * FROM table WHERE field = 'value' /*arbitrary/31*/ ",
-	"SELECT * FROM table WHERE field = 'value' /*arbitrary31*/ ",
-	"SELECT *    FROM t WHERE 1=1 AND id=1",
-	"select * from t where (base.nid IN  ('1412', '1410', '1411'))",
-	`select * from t where i=1      order            by
-             a,  b          ASC, d    DESC,
-
-                                    e asc`,
-	"select * from t where i=1 order by a, b ASC, d DESC, e asc",
-	"select 'hello'\n",
-	"select 'hello', '\nhello\n', \"hello\", '\\'' from foo",
-	"SELECT ID, name, parent, type FROM posts WHERE _name IN ('perf','caching') AND (type = 'page' OR type = 'attachment')",
-	"SELECT name, value FROM variable",
-	"select \n-- bar\n foo",
-	"select null, 5.001, 5001. from foo",
-	"select sleep(2) from test.n",
-	"SELECT t FROM field WHERE  (entity_type = 'node') AND (entity_id IN  ('609')) AND (language IN  ('und')) AND (deleted = '0') ORDER BY delta ASC",
-	"select  t.table_schema,t.table_name,engine  from information_schema.tables t  inner join information_schema.columns c  on t.table_schema=c.table_schema and t.table_name=c.table_name group by t.table_schema,t.table_name having  sum(if(column_key in ('PRI','UNI'),1,0))=0",
-	"/* -- S++ SU ABORTABLE -- spd_user: rspadim */SELECT SQL_SMALL_RESULT SQL_CACHE DISTINCT centro_atividade FROM est_dia WHERE unidade_id=1001 AND item_id=67 AND item_id_red=573",
-	`UPDATE groups_search SET  charter = '   -------3\'\' XXXXXXXXX.\n    \n    -----------------------------------------------------', show_in_list = 'Y' WHERE group_id='aaaaaaaa'`,
-	"use `foo`",
-	"select sourcetable, if(f.lastcontent = ?, f.lastupdate, f.lastcontent) as lastactivity, f.totalcount as activity, type.class as type, (f.nodeoptions & ?) as nounsubscribe from node as f inner join contenttype as type on type.contenttypeid = f.contenttypeid inner join subscribed as sd on sd.did = f.nodeid and sd.userid = ? union all select f.name as title, f.userid as keyval, ? as sourcetable, ifnull(f.lastpost, f.joindate) as lastactivity, f.posts as activity, ? as type, ? as nounsubscribe from user as f inner join userlist as ul on ul.relationid = f.userid and ul.userid = ? where ul.type = ? and ul.aq = ? order by title limit ?",
-	"CREATE INDEX part_of_name ON customer (name(10));",
-	"alter table `sakila`.`t1` add index `idx_col`(`col`)",
-	"alter table `sakila`.`t1` add UNIQUE index `idx_col`(`col`)",
-	"alter table `sakila`.`t1` add index `idx_ID`(`ID`)",
-
-	// ADD|DROP COLUMN
-	"ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;",
-	"ALTER TABLE T2 ADD COLUMN C int;",
-	"ALTER TABLE T2 ADD COLUMN D int FIRST;",
-	"ALTER TABLE T2 ADD COLUMN E int AFTER D;",
-
-	// RENAME COLUMN
-	"ALTER TABLE t1 RENAME COLUMN a TO b",
-
-	// RENAME INDEX
-	"ALTER TABLE t1 RENAME INDEX idx_a TO idx_b",
-	"ALTER TABLE t1 RENAME KEY idx_a TO idx_b",
-
-	// RENAME TABLE
-	"ALTER TABLE db.old_table RENAME new_table;",
-	"ALTER TABLE old_table RENAME TO new_table;",
-	"ALTER TABLE old_table RENAME AS new_table;",
-
-	// MODIFY & CHANGE
-	"ALTER TABLE t1 MODIFY col1 BIGINT UNSIGNED DEFAULT 1 COMMENT 'my column';",
-	"ALTER TABLE t1 CHANGE b a INT NOT NULL;",
-
-	// COMMENT
-	"/*!40000 select 1*/;",
-}
-
-func TestPretty(t *testing.T) {
-	err := common.GoldenDiff(func() {
-		for _, sql := range append(TestSqlsPretty, common.TestSQLs...) {
-			fmt.Println(sql)
-			fmt.Println(Pretty(sql, "builtin"))
-		}
-	}, t.Name(), update)
-	if nil != err {
-		t.Fatal(err)
-	}
-}
-
-func TestIsKeyword(t *testing.T) {
-	tks := map[string]bool{
-		"AGAINST":        true,
-		"AUTO_INCREMENT": true,
-		"ADD":            true,
-		"BETWEEN":        true,
-		".":              false,
-		"actions":        false,
-		`"`:              false,
-		":":              false,
-	}
-	for tk, v := range tks {
-		if IsMysqlKeyword(tk) != v {
-			t.Error("isKeyword:", tk)
-		}
-	}
-}
-
-func TestRemoveComments(t *testing.T) {
-	for _, sql := range TestSqlsPretty {
-		stmt, _ := sqlparser.Parse(sql)
-		newSQL := sqlparser.String(stmt)
-		if newSQL != sql {
-			fmt.Print(newSQL)
-		}
-	}
-}
-
-func TestMysqlEscapeString(t *testing.T) {
-	var strs = []map[string]string{
-		{
-			"input":  "abc",
-			"output": "abc",
-		},
-		{
-			"input":  "'abc",
-			"output": "\\'abc",
-		},
-		{
-			"input": `
-abc`,
-			"output": `\
-abc`,
-		},
-		{
-			"input":  "\"abc",
-			"output": "\\\"abc",
-		},
-	}
-	for _, str := range strs {
-		output, err := MysqlEscapeString(str["input"])
-		if err != nil {
-			t.Error("TestMysqlEscapeString", err)
-		} else {
-			if output != str["output"] {
-				t.Error("TestMysqlEscapeString", output, str["output"])
-			}
-		}
-	}
-}
diff --git a/vendor/github.com/XiaoMi/soar/ast/rewrite.go b/vendor/github.com/XiaoMi/soar/ast/rewrite.go
deleted file mode 100644
index cf4bab2..0000000
--- a/vendor/github.com/XiaoMi/soar/ast/rewrite.go
+++ /dev/null
@@ -1,1732 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package ast
-
-import (
-	"bytes"
-	"encoding/json"
-	"fmt"
-	"reflect"
-	"regexp"
-	"strings"
-
-	"github.com/XiaoMi/soar/common"
-
-	"github.com/kr/pretty"
-	"vitess.io/vitess/go/vt/sqlparser"
-)
-
-// Rule SQL重写规则
-type Rule struct {
-	Name        string                  `json:"Name"`
-	Description string                  `json:"Description"`
-	Original    string                  `json:"Original"` // 错误示范。为空或"暂不支持"不会出现在list-rewrite-rules中
-	Suggest     string                  `json:"Suggest"`  // 正确示范。
-	Func        func(*Rewrite) *Rewrite `json:"-"`        // 如果不定义 Func 需要多条 SQL 联动改写
-}
-
-// RewriteRules SQL重写规则,注意这个规则是有序的,先后顺序不能乱
-var RewriteRules = []Rule{
-	{
-		Name:        "dml2select",
-		Description: "将数据库更新请求转换为只读查询请求,便于执行EXPLAIN",
-		Original:    "DELETE FROM film WHERE length > 100",
-		Suggest:     "select * from film where length > 100",
-		Func:        (*Rewrite).RewriteDML2Select,
-	},
-	{
-		Name:        "star2columns",
-		Description: "为SELECT *补全表的列信息",
-		Original:    "SELECT * FROM film",
-		Suggest:     "select film.film_id, film.title from film",
-		Func:        (*Rewrite).RewriteStar2Columns,
-	},
-	{
-		Name:        "insertcolumns",
-		Description: "为INSERT补全表的列信息",
-		Original:    "insert into film values(1,2,3,4,5)",
-		Suggest:     "insert into film(film_id, title, description, release_year, language_id) values (1, 2, 3, 4, 5)",
-		Func:        (*Rewrite).RewriteInsertColumns,
-	},
-	{
-		Name:        "having",
-		Description: "将查询的 HAVING 子句改写为 WHERE 中的查询条件",
-		Original:    "SELECT state, COUNT(*) FROM Drivers GROUP BY state HAVING state IN ('GA', 'TX') ORDER BY state",
-		Suggest:     "select state, COUNT(*) from Drivers where state in ('GA', 'TX') group by state order by state asc",
-		Func:        (*Rewrite).RewriteHaving,
-	},
-	{
-		Name:        "orderbynull",
-		Description: "如果 GROUP BY 语句不指定 ORDER BY 条件会导致无谓的排序产生,如果不需要排序建议添加 ORDER BY NULL",
-		Original:    "SELECT sum(col1) FROM tbl GROUP BY col",
-		Suggest:     "select sum(col1) from tbl group by col order by null",
-		Func:        (*Rewrite).RewriteAddOrderByNull,
-	},
-	{
-		Name:        "unionall",
-		Description: "可以接受重复的时间,使用 UNION ALL 替代 UNION 以提高查询效率",
-		Original:    "select country_id from city union select country_id from country",
-		Suggest:     "select country_id from city union all select country_id from country",
-		Func:        (*Rewrite).RewriteUnionAll,
-	},
-	{
-		Name:        "or2in",
-		Description: "将同一列不同条件的 OR 查询转写为 IN 查询",
-		Original:    "select country_id from city where col1 = 1 or (col2 = 1 or col2 = 2 ) or col1 = 3;",
-		Suggest:     "select country_id from city where (col2 in (1, 2)) or col1 in (1, 3);",
-		Func:        (*Rewrite).RewriteOr2In,
-	},
-	{
-		Name:        "innull",
-		Description: "如果 IN 条件中可能有 NULL 值而又想匹配 NULL 值时,建议添加OR col IS NULL",
-		Original:    "暂不支持",
-		Suggest:     "暂不支持",
-		Func:        (*Rewrite).RewriteInNull,
-	},
-	// 把所有跟 or 相关的重写完之后才进行 or 转 union 的重写
-	{
-		Name:        "or2union",
-		Description: "将不同列的 OR 查询转为 UNION 查询,建议结合 unionall 重写策略一起使用",
-		Original:    "暂不支持",
-		Suggest:     "暂不支持",
-		Func:        (*Rewrite).RewriteOr2Union,
-	},
-	{
-		Name:        "dmlorderby",
-		Description: "删除 DML 更新操作中无意义的 ORDER BY",
-		Original:    "DELETE FROM tbl WHERE col1=1 ORDER BY col",
-		Suggest:     "delete from tbl where col1 = 1",
-		Func:        (*Rewrite).RewriteRemoveDMLOrderBy,
-	},
-	/*
-		{
-			Name:        "groupbyconst",
-			Description: "删除无意义的GROUP BY常量",
-			Original:    "SELECT sum(col1) FROM tbl GROUP BY 1;",
-			Suggest:     "select sum(col1) from tbl",
-			Func:        (*Rewrite).RewriteGroupByConst,
-		},
-	*/
-	{
-		Name:        "sub2join",
-		Description: "将子查询转换为JOIN查询",
-		Original:    "暂不支持",
-		Suggest:     "暂不支持",
-		Func:        (*Rewrite).RewriteSubQuery2Join,
-	},
-	{
-		Name:        "join2sub",
-		Description: "将JOIN查询转换为子查询",
-		Original:    "暂不支持",
-		Suggest:     "暂不支持",
-		Func:        (*Rewrite).RewriteJoin2SubQuery,
-	},
-	{
-		Name:        "distinctstar",
-		Description: "DISTINCT *对有主键的表没有意义,可以将DISTINCT删掉",
-		Original:    "SELECT DISTINCT * FROM film;",
-		Suggest:     "SELECT * FROM film",
-		Func:        (*Rewrite).RewriteDistinctStar,
-	},
-	{
-		Name:        "standard",
-		Description: "SQL标准化,如:关键字转换为小写",
-		Original:    "SELECT sum(col1) FROM tbl GROUP BY 1;",
-		Suggest:     "select sum(col1) from tbl group by 1",
-		Func:        (*Rewrite).RewriteStandard,
-	},
-	{
-		Name:        "mergealter",
-		Description: "合并同一张表的多条ALTER语句",
-		Original:    "ALTER TABLE t2 DROP COLUMN c;ALTER TABLE t2 DROP COLUMN d;",
-		Suggest:     "ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;",
-	},
-	{
-		Name:        "alwaystrue",
-		Description: "删除无用的恒真判断条件",
-		Original:    "SELECT count(col) FROM tbl where 'a'= 'a' or ('b' = 'b' and a = 'b');",
-		Suggest:     "select count(col) from tbl where (a = 'b');",
-		Func:        (*Rewrite).RewriteAlwaysTrue,
-	},
-	{
-		Name:        "countstar",
-		Description: "不建议使用COUNT(col)或COUNT(常量),建议改写为COUNT(*)",
-		Original:    "SELECT count(col) FROM tbl GROUP BY 1;",
-		Suggest:     "SELECT count(*) FROM tbl GROUP BY 1;",
-		Func:        (*Rewrite).RewriteCountStar,
-	},
-	{
-		Name:        "innodb",
-		Description: "建表时建议使用InnoDB引擎,非 InnoDB 引擎表自动转 InnoDB",
-		Original:    "CREATE TABLE t1(id bigint(20) NOT NULL AUTO_INCREMENT);",
-		Suggest:     "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB;",
-		Func:        (*Rewrite).RewriteInnoDB,
-	},
-	{
-		Name:        "autoincrement",
-		Description: "将autoincrement初始化为1",
-		Original:    "CREATE TABLE t1(id bigint(20) NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123802;",
-		Suggest:     "create table t1(id bigint(20) not null auto_increment) ENGINE=InnoDB auto_increment=1;",
-		Func:        (*Rewrite).RewriteAutoIncrement,
-	},
-	{
-		Name:        "intwidth",
-		Description: "整型数据类型修改默认显示宽度",
-		Original:    "create table t1 (id int(20) not null auto_increment) ENGINE=InnoDB;",
-		Suggest:     "create table t1 (id int(10) not null auto_increment) ENGINE=InnoDB;",
-		Func:        (*Rewrite).RewriteIntWidth,
-	},
-	{
-		Name:        "truncate",
-		Description: "不带 WHERE 条件的 DELETE 操作建议修改为 TRUNCATE",
-		Original:    "DELETE FROM tbl",
-		Suggest:     "truncate table tbl",
-		Func:        (*Rewrite).RewriteTruncate,
-	},
-	{
-		Name:        "rmparenthesis",
-		Description: "去除没有意义的括号",
-		Original:    "select col from table where (col = 1);",
-		Suggest:     "select col from table where col = 1;",
-		Func:        (*Rewrite).RewriteRmParenthesis,
-	},
-	// delimiter要放在最后,不然补不上
-	{
-		Name:        "delimiter",
-		Description: "补全DELIMITER",
-		Original:    "use sakila",
-		Suggest:     "use sakila;",
-		Func:        (*Rewrite).RewriteDelimiter,
-	},
-	// TODO in to exists
-	// TODO exists to in
-}
-
-// ListRewriteRules 打印SQL重写规则
-func ListRewriteRules(rules []Rule) {
-	switch common.Config.ReportType {
-	case "json":
-		js, err := json.MarshalIndent(rules, "", "  ")
-		if err == nil {
-			fmt.Println(string(js))
-		}
-	default:
-
-		fmt.Print("# 重写规则\n\n[toc]\n\n")
-		for _, r := range rules {
-			if !common.Config.Verbose && (r.Original == "" || r.Original == "暂不支持") {
-				continue
-			}
-
-			fmt.Print("## ", common.MarkdownEscape(r.Name),
-				"\n* **Description**:", r.Description+"\n",
-				"\n* **Original**:\n\n```sql\n", r.Original, "\n```\n",
-				"\n* **Suggest**:\n\n```sql\n", r.Suggest, "\n```\n")
-
-		}
-	}
-}
-
-// Rewrite 用于重写SQL
-type Rewrite struct {
-	SQL     string
-	NewSQL  string
-	Stmt    sqlparser.Statement
-	Columns common.TableColumns
-}
-
-// NewRewrite 返回一个*Rewrite对象,如果SQL无法被正常解析,将错误输出到日志中,返回一个nil
-func NewRewrite(sql string) *Rewrite {
-	stmt, err := sqlparser.Parse(sql)
-	if err != nil {
-		common.Log.Error(err.Error(), sql)
-		return nil
-	}
-
-	return &Rewrite{
-		SQL:  sql,
-		Stmt: stmt,
-	}
-}
-
-// Rewrite 入口函数
-func (rw *Rewrite) Rewrite() *Rewrite {
-	defer func() {
-		if err := recover(); err != nil {
-			common.Log.Error("Query rewrite Error: %s, maybe hit a bug.\nQuery: %s \nAST: %s",
-				err, rw.SQL, pretty.Sprint(rw.Stmt))
-			return
-		}
-	}()
-
-	for _, rule := range RewriteRules {
-		if RewriteRuleMatch(rule.Name) && rule.Func != nil {
-			rule.Func(rw)
-			common.Log.Debug("Rewrite Rule:%s Output NewSQL: %s", rule.Name, rw.NewSQL)
-		}
-	}
-	if rw.NewSQL == "" {
-		rw.NewSQL = rw.SQL
-	}
-	rw.Stmt, _ = sqlparser.Parse(rw.NewSQL)
-
-	// TODO: 重新前后返回结果一致性对比
-
-	// TODO: 前后SQL性能对比
-	return rw
-}
-
-// RewriteDelimiter delimiter: 补分号,可以指定不同的DELIMITER
-func (rw *Rewrite) RewriteDelimiter() *Rewrite {
-	if rw.NewSQL != "" {
-		rw.NewSQL = strings.TrimSuffix(rw.NewSQL, common.Config.Delimiter) + common.Config.Delimiter
-	} else {
-		rw.NewSQL = strings.TrimSuffix(rw.SQL, common.Config.Delimiter) + common.Config.Delimiter
-	}
-	return rw
-}
-
-// RewriteStandard standard: 使用 vitess 提供的 String 功能将抽象语法树转写回 SQL,注意:这可能转写失败。
-func (rw *Rewrite) RewriteStandard() *Rewrite {
-	if _, err := sqlparser.Parse(rw.SQL); err == nil {
-		rw.NewSQL = sqlparser.String(rw.Stmt)
-	}
-	return rw
-}
-
-// RewriteAlwaysTrue alwaystrue: 删除恒真条件
-func (rw *Rewrite) RewriteAlwaysTrue() (reWriter *Rewrite) {
-	array := NewNodeList(rw.Stmt)
-	tNode := array.Head
-	for {
-		omitAlwaysTrue(tNode)
-		tNode = tNode.Next
-		if tNode == nil {
-			break
-		}
-	}
-
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// isAlwaysTrue 用于判断ComparisonExpr是否是恒真
-func isAlwaysTrue(expr *sqlparser.ComparisonExpr) bool {
-	if expr == nil {
-		return true
-	}
-
-	var result bool
-	switch expr.Operator {
-	case "<>":
-		expr.Operator = "!="
-	case "<=>":
-		expr.Operator = "="
-	case ">=", "<=", "!=", "=":
-	default:
-		return false
-	}
-
-	var left []byte
-	var right []byte
-
-	// left
-	switch l := expr.Left.(type) {
-	case *sqlparser.SQLVal:
-		left = l.Val
-	default:
-		return false
-	}
-
-	// right
-	switch r := expr.Right.(type) {
-	case *sqlparser.SQLVal:
-		right = r.Val
-	default:
-		return false
-	}
-
-	switch expr.Operator {
-	case "=":
-		result = bytes.Equal(left, right)
-	case "!=":
-		result = !bytes.Equal(left, right)
-	case ">":
-		result = bytes.Compare(left, right) > 0
-	case ">=":
-		result = bytes.Compare(left, right) >= 0
-	case "<":
-		result = bytes.Compare(left, right) < 0
-	case "<=":
-		result = bytes.Compare(left, right) <= 0
-	default:
-		result = false
-	}
-
-	return result
-}
-
-// omitAlwaysTrue 移除AST中的恒真条件
-func omitAlwaysTrue(node *NodeItem) {
-	if node == nil {
-		return
-	}
-
-	switch self := node.Self.(type) {
-	case *sqlparser.Where:
-		if self != nil {
-			switch cond := self.Expr.(type) {
-			case *sqlparser.ComparisonExpr:
-				if isAlwaysTrue(cond) {
-					self.Expr = nil
-				}
-			case *sqlparser.ParenExpr:
-				if cond.Expr == nil {
-					self.Expr = nil
-				}
-			}
-		}
-	case *sqlparser.ParenExpr:
-		if self != nil {
-			switch cond := self.Expr.(type) {
-			case *sqlparser.ComparisonExpr:
-				if isAlwaysTrue(cond) {
-					self.Expr = nil
-				}
-			}
-		}
-	case *sqlparser.AndExpr:
-		if self != nil {
-			var tmp sqlparser.Expr
-			isRightTrue := false
-			isLeftTrue := false
-			tmp = nil
-
-			// 查看左树的情况
-			switch l := self.Left.(type) {
-			case *sqlparser.ComparisonExpr:
-				if isAlwaysTrue(l) {
-					self.Left = nil
-					isLeftTrue = true
-					tmp = self.Right
-				}
-			case *sqlparser.ParenExpr:
-				if l.Expr == nil {
-					self.Left = nil
-					isLeftTrue = true
-					tmp = self.Right
-				}
-			default:
-				if l == nil {
-					isLeftTrue = true
-					tmp = self.Right
-				}
-			}
-
-			// 查看右树的情况
-			switch r := self.Right.(type) {
-			case *sqlparser.ComparisonExpr:
-				if isAlwaysTrue(r) {
-					self.Right = nil
-					isRightTrue = true
-					tmp = self.Left
-				}
-			case *sqlparser.ParenExpr:
-				if r.Expr == nil {
-					self.Right = nil
-					isRightTrue = true
-					tmp = self.Left
-				}
-			default:
-				if r == nil {
-					isRightTrue = true
-					tmp = self.Left
-				}
-			}
-
-			if isRightTrue && isLeftTrue {
-				tmp = nil
-			} else if !isLeftTrue && !isRightTrue {
-				return
-			}
-
-			// 根据类型开始替换节点
-			switch l := node.Prev.Self.(type) {
-			case *sqlparser.Where:
-				l.Expr = tmp
-			case *sqlparser.ParenExpr:
-				l.Expr = tmp
-			case *sqlparser.AndExpr:
-				if l.Left == self {
-					l.Left = tmp
-				} else if l.Right == self {
-					l.Right = tmp
-				}
-			case *sqlparser.OrExpr:
-				if l.Left == self {
-					l.Left = tmp
-				} else if l.Right == self {
-					l.Right = tmp
-				}
-			default:
-				// 未匹配到对应数据类型则从链表中移除该节点
-				err := node.Array.Remove(node.Prev)
-				common.LogIfError(err, "")
-			}
-
-		}
-
-	case *sqlparser.OrExpr:
-		// 与AndExpr相同
-		if self != nil {
-			var tmp sqlparser.Expr
-			isRightTrue := false
-			isLeftTrue := false
-			tmp = nil
-
-			switch l := self.Left.(type) {
-			case *sqlparser.ComparisonExpr:
-				if isAlwaysTrue(l) {
-					self.Left = nil
-					isLeftTrue = true
-					tmp = self.Right
-				}
-			case *sqlparser.ParenExpr:
-				if l.Expr == nil {
-					self.Left = nil
-					isLeftTrue = true
-					tmp = self.Right
-				}
-			default:
-				if l == nil {
-					isLeftTrue = true
-					tmp = self.Right
-				}
-			}
-
-			switch r := self.Right.(type) {
-			case *sqlparser.ComparisonExpr:
-				if isAlwaysTrue(r) {
-					self.Right = nil
-					isRightTrue = true
-					tmp = self.Left
-				}
-			case *sqlparser.ParenExpr:
-				if r.Expr == nil {
-					self.Right = nil
-					isRightTrue = true
-					tmp = self.Left
-				}
-			default:
-				if r == nil {
-					isRightTrue = true
-					tmp = self.Left
-				}
-			}
-
-			if isRightTrue && isLeftTrue {
-				tmp = nil
-			} else if !isLeftTrue && !isRightTrue {
-				return
-			}
-
-			switch l := node.Prev.Self.(type) {
-			case *sqlparser.Where:
-				l.Expr = tmp
-			case *sqlparser.ParenExpr:
-				l.Expr = tmp
-			case *sqlparser.AndExpr:
-				if l.Left == self {
-					l.Left = tmp
-				} else if l.Right == self {
-					l.Right = tmp
-				}
-			case *sqlparser.OrExpr:
-				if l.Left == self {
-					l.Left = tmp
-				} else if l.Right == self {
-					l.Right = tmp
-				}
-			default:
-				err := node.Array.Remove(node.Prev)
-				common.LogIfError(err, "")
-			}
-		}
-	}
-
-	omitAlwaysTrue(node.Prev)
-}
-
-// RewriteCountStar countstar: 将COUNT(col)改写为COUNT(*)
-// COUNT(DISTINCT col)不能替换为COUNT(*)
-func (rw *Rewrite) RewriteCountStar() *Rewrite {
-	err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-		switch f := node.(type) {
-		case *sqlparser.FuncExpr:
-			if strings.ToLower(f.Name.String()) == "count" && len(f.Exprs) > 0 {
-				switch colExpr := f.Exprs[0].(type) {
-				case *sqlparser.AliasedExpr:
-					switch col := colExpr.Expr.(type) {
-					case *sqlparser.ColName:
-						f.Exprs[0] = &sqlparser.StarExpr{TableName: col.Qualifier}
-					}
-				}
-			}
-		}
-		return true, nil
-	}, rw.Stmt)
-	common.LogIfError(err, "")
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// RewriteInnoDB InnoDB: 为未指定 Engine 的表默认添加 InnoDB 引擎,将其他存储引擎转为 InnoDB
-func (rw *Rewrite) RewriteInnoDB() *Rewrite {
-	switch create := rw.Stmt.(type) {
-	case *sqlparser.DDL:
-		if create.Action != "create" {
-			return rw
-		}
-
-		if strings.Contains(strings.ToLower(create.TableSpec.Options), "engine=") {
-			reg := regexp.MustCompile(`(?i)engine=[a-z]+`)
-			create.TableSpec.Options = reg.ReplaceAllString(create.TableSpec.Options, "ENGINE=InnoDB ")
-		} else {
-			create.TableSpec.Options = " ENGINE=InnoDB " + create.TableSpec.Options
-		}
-
-	}
-
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// RewriteAutoIncrement autoincrement: 将auto_increment设置为1
-func (rw *Rewrite) RewriteAutoIncrement() *Rewrite {
-	switch create := rw.Stmt.(type) {
-	case *sqlparser.DDL:
-		if create.Action != "create" || create.TableSpec == nil {
-			return rw
-		}
-		if strings.Contains(strings.ToLower(create.TableSpec.Options), "auto_increment=") {
-			reg := regexp.MustCompile(`(?i)auto_increment=[0-9]+`)
-			create.TableSpec.Options = reg.ReplaceAllString(create.TableSpec.Options, "auto_increment=1 ")
-		}
-	}
-
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// RewriteIntWidth intwidth: int 类型转为 int(10),bigint 类型转为 bigint(20)
-func (rw *Rewrite) RewriteIntWidth() *Rewrite {
-	switch create := rw.Stmt.(type) {
-	case *sqlparser.DDL:
-		if create.Action != "create" || create.TableSpec == nil {
-			return rw
-		}
-		for _, col := range create.TableSpec.Columns {
-			switch col.Type.Type {
-			case "int", "integer":
-				if col.Type.Length != nil &&
-					(string(col.Type.Length.Val) != "10" && string(col.Type.Length.Val) != "11") {
-					col.Type.Length = sqlparser.NewIntVal([]byte("10"))
-				}
-			case "bigint":
-				if col.Type.Length != nil && string(col.Type.Length.Val) != "20" || col.Type.Length == nil {
-					col.Type.Length = sqlparser.NewIntVal([]byte("20"))
-				}
-			default:
-			}
-		}
-	}
-
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// RewriteStar2Columns star2columns: 对应COL.001,SELECT补全*指代的列名
-func (rw *Rewrite) RewriteStar2Columns() *Rewrite {
-	// 如果未配置mysql环境或从环境中获取失败,*不进行替换
-	if common.Config.TestDSN.Disable || len(rw.Columns) == 0 {
-		common.Log.Debug("(rw *Rewrite) RewriteStar2Columns(): Rewrite failed. TestDSN.Disable: %v, len(rw.Columns):%d",
-			common.Config.TestDSN.Disable, len(rw.Columns))
-		return rw
-	}
-
-	err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-		switch n := node.(type) {
-		case *sqlparser.Select:
-
-			// select * 可能出现的情况:
-			// 1. select * from tb;
-			// 2. select * from tb1,tb2;
-			// 3. select tb1.* from tb1;
-			// 4. select tb1.*,tb2.col from tb1,tb2;
-			// 5. select db.tb1.* from tb1;
-			// 6. select db.tb1.*,db.tb2.col from db.tb1,db.tb2;
-
-			newSelectExprs := make(sqlparser.SelectExprs, 0)
-			for _, expr := range n.SelectExprs {
-				switch e := expr.(type) {
-				case *sqlparser.StarExpr:
-					// 一般情况下最外层循环不会超过两层
-					for _, tables := range rw.Columns {
-						for _, cols := range tables {
-							for _, col := range cols {
-								newExpr := &sqlparser.AliasedExpr{
-									Expr: &sqlparser.ColName{
-										Metadata: nil,
-										Name:     sqlparser.NewColIdent(col.Name),
-										Qualifier: sqlparser.TableName{
-											Name: sqlparser.NewTableIdent(col.Table),
-											// 因为不建议跨DB的查询,所以这里的db前缀将不进行补齐
-											Qualifier: sqlparser.TableIdent{},
-										},
-									},
-									As: sqlparser.ColIdent{},
-								}
-
-								if e.TableName.Name.IsEmpty() {
-									// 情况1,2
-									newSelectExprs = append(newSelectExprs, newExpr)
-								} else {
-									// 其他情况下只有在匹配表名的时候才会进行替换
-									if e.TableName.Name.String() == col.Table {
-										newSelectExprs = append(newSelectExprs, newExpr)
-									}
-								}
-							}
-						}
-					}
-				default:
-					newSelectExprs = append(newSelectExprs, e)
-				}
-			}
-
-			n.SelectExprs = newSelectExprs
-		}
-		return true, nil
-	}, rw.Stmt)
-	common.LogIfError(err, "")
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// RewriteInsertColumns insertcolumns: 对应COL.002,INSERT补全列名
-func (rw *Rewrite) RewriteInsertColumns() *Rewrite {
-
-	switch insert := rw.Stmt.(type) {
-	case *sqlparser.Insert:
-		switch insert.Action {
-		case "insert", "replace":
-			if insert.Columns != nil {
-				return rw
-			}
-
-			newColumns := make(sqlparser.Columns, 0)
-			db := insert.Table.Qualifier.String()
-			table := insert.Table.Name.String()
-			// 支持INSERT/REPLACE INTO VALUES形式,支持INSERT/REPLACE INTO SELECT
-			colCount := 0
-			switch v := insert.Rows.(type) {
-			case sqlparser.Values:
-				if len(v) > 0 {
-					colCount = len(v[0])
-				}
-
-			case *sqlparser.Select:
-				if l := len(v.SelectExprs); l > 0 {
-					colCount = l
-				}
-			}
-
-			// 开始对ast进行替换,补全前N列
-			counter := 0
-			for dbName, tb := range rw.Columns {
-				for tbName, cols := range tb {
-					for _, col := range cols {
-						// 只有全部列补全完成的时候才会替换ast
-						if counter == colCount {
-							insert.Columns = newColumns
-							rw.NewSQL = sqlparser.String(rw.Stmt)
-							return rw
-						}
-
-						if db != "" {
-							// 指定了DB的时候,只能怼指定DB的列
-							if db == dbName && table == tbName {
-								newColumns = append(newColumns, sqlparser.NewColIdent(col.Name))
-								counter++
-							}
-						} else {
-							// 没有指定DB的时候,将column中的列按顺序往里怼
-							if table == tbName {
-								newColumns = append(newColumns, sqlparser.NewColIdent(col.Name))
-								counter++
-							}
-						}
-					}
-				}
-			}
-		}
-	}
-	return rw
-}
-
-// RewriteHaving having: 对应CLA.013,使用 WHERE 过滤条件替代 HAVING
-func (rw *Rewrite) RewriteHaving() *Rewrite {
-	err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-		switch n := node.(type) {
-		case *sqlparser.Select:
-			if n.Having != nil {
-				if n.Where == nil {
-					// WHERE 条件为空直接用 HAVING 替代 WHERE 即可
-					n.Where = n.Having
-				} else {
-					// WHERE 条件不为空,需要对已有的条件进行括号保护,然后再 AND+HAVING
-					n.Where = &sqlparser.Where{
-						Expr: &sqlparser.AndExpr{
-							Left: &sqlparser.ParenExpr{
-								Expr: n.Where.Expr,
-							},
-							Right: n.Having.Expr,
-						},
-					}
-				}
-				// 别忘了重置 HAVING 和 Where.Type
-				n.Where.Type = "where"
-				n.Having = nil
-			}
-		}
-		return true, nil
-	}, rw.Stmt)
-	common.LogIfError(err, "")
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// RewriteAddOrderByNull orderbynull: 对应 CLA.008,GROUP BY 无排序要求时添加 ORDER BY NULL
-func (rw *Rewrite) RewriteAddOrderByNull() *Rewrite {
-	err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-		switch n := node.(type) {
-		case *sqlparser.Select:
-			if n.GroupBy != nil && n.OrderBy == nil {
-				n.OrderBy = sqlparser.OrderBy{
-					&sqlparser.Order{
-						Expr:      &sqlparser.NullVal{},
-						Direction: "asc",
-					},
-				}
-			}
-		}
-		return true, nil
-	}, rw.Stmt)
-	common.LogIfError(err, "")
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// RewriteOr2Union or2union: 将 OR 查询转写为 UNION ALL TODO: 暂无对应 HeuristicRules
-// https://sqlperformance.com/2014/09/sql-plan/rewriting-queries-improve-performance
-func (rw *Rewrite) RewriteOr2Union() *Rewrite {
-	return rw
-}
-
-// RewriteUnionAll unionall: 不介意重复数据的情况下使用 union all 替换 union
-func (rw *Rewrite) RewriteUnionAll() *Rewrite {
-	err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-		switch n := node.(type) {
-		case *sqlparser.Union:
-			n.Type = "union all"
-		}
-		return true, nil
-	}, rw.Stmt)
-	common.LogIfError(err, "")
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// RewriteOr2In or2in: 同一列的 OR 过滤条件使用 IN() 替代,如果值有相等的会进行合并
-func (rw *Rewrite) RewriteOr2In() *Rewrite {
-	// 通过 AST 生成 node 的双向链表,链表顺序为书写顺序
-	nodeList := NewNodeList(rw.Stmt)
-	tNode := nodeList.First()
-
-	for {
-		tNode.or2in()
-		if tNode.Next == nil {
-			break
-		}
-		tNode = tNode.Next
-	}
-
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// or2in 用于将 or 转换成 in
-func (node *NodeItem) or2in() {
-	if node == nil || node.Self == nil {
-		return
-	}
-
-	switch selfNode := node.Self.(type) {
-	case *sqlparser.OrExpr:
-		newExpr := mergeExprs(selfNode.Left, selfNode.Right)
-		if newExpr != nil {
-			// or 自身两个节点可以合并的情况下,将父节点中的 expr 替换成新的
-			switch pre := node.Prev.Self.(type) {
-			case *sqlparser.OrExpr:
-				if pre.Left == node.Self {
-					node.Self = newExpr
-					pre.Left = newExpr
-				} else if pre.Right == node.Self {
-					node.Self = newExpr
-					pre.Right = newExpr
-				}
-			case *sqlparser.AndExpr:
-				if pre.Left == node.Self {
-					node.Self = newExpr
-					pre.Left = newExpr
-				} else if pre.Right == node.Self {
-					node.Self = newExpr
-					pre.Right = newExpr
-				}
-			case *sqlparser.Where:
-				node.Self = newExpr
-				pre.Expr = newExpr
-			case *sqlparser.ParenExpr:
-				// 如果 SQL 书写中带了括号,暂不会进行跨括号的合并
-				// TODO: 无意义括号打平,加个 rewrite rule
-				node.Self = newExpr
-				pre.Expr = newExpr
-			}
-		} else {
-			// or 自身两个节点如不可以合并,则检测是否可以与父节点合并
-			// 与父节点的合并不能跨越and、括号等,可能会改变语义
-			// 检查自身左右节点是否能与上层节点中合并,or 只能与 or 合并
-			switch pre := node.Prev.Self.(type) {
-			case *sqlparser.OrExpr:
-				// AST 中如果出现复合条件,则一定在左树,所以只需要判断左边就可以
-				if pre.Left == selfNode {
-					switch n := pre.Right.(type) {
-					case *sqlparser.ComparisonExpr:
-						newLeftExpr := mergeExprs(selfNode.Left, n)
-						newRightExpr := mergeExprs(selfNode.Right, n)
-
-						// newLeftExpr 与 newRightExpr 一定有一个是 nil,
-						// 否则说明该 orExpr 下的两个节点可合并,可以通过最后的向前递归合并 pre 节点中的 expr
-						if newLeftExpr == nil || newRightExpr == nil {
-							if newLeftExpr != nil {
-								pre.Right = newLeftExpr
-								pre.Left = selfNode.Right
-								err := node.Array.Remove(node)
-								common.LogIfError(err, "")
-							}
-
-							if newRightExpr != nil {
-								pre.Right = newRightExpr
-								pre.Left = selfNode.Left
-								err := node.Array.Remove(node)
-								common.LogIfError(err, "")
-							}
-						}
-					}
-				}
-			}
-		}
-	}
-
-	// 逆向合并由更改 AST 后产生的新的可合并节点
-	node.Prev.or2in()
-}
-
-// mergeExprs 将两个属于同一个列的 ComparisonExpr 合并成一个,如果不能合并则返回 nil
-func mergeExprs(left, right sqlparser.Expr) *sqlparser.ComparisonExpr {
-	// 用于对比两个列是否相同
-	colInLeft := ""
-	colInRight := ""
-	lOperator := ""
-	rOperator := ""
-
-	// 用于存放 expr 左右子树中的值
-	var values []sqlparser.SQLNode
-
-	// SQL 中使用到的列
-	var colName *sqlparser.ColName
-
-	// 左子树
-	switch l := left.(type) {
-	case *sqlparser.ComparisonExpr:
-		// 获取列名
-		colName, colInLeft = getColumnName(l.Left)
-		// 获取值
-		if colInLeft != "" {
-			switch v := l.Right.(type) {
-			case *sqlparser.SQLVal, sqlparser.ValTuple, *sqlparser.BoolVal, *sqlparser.NullVal:
-				values = append(values, v)
-			}
-		}
-		// 获取 operator
-		lOperator = l.Operator
-	default:
-		return nil
-	}
-
-	// 右子树
-	switch r := right.(type) {
-	case *sqlparser.ComparisonExpr:
-		// 获取列名
-		if colName.Name.String() != "" {
-			common.Log.Warn("colName shouldn't has value, but now it's %s", colName.Name.String())
-		}
-		colName, colInRight = getColumnName(r.Left)
-		// 获取值
-		if colInRight != "" {
-			switch v := r.Right.(type) {
-			case *sqlparser.SQLVal, sqlparser.ValTuple, *sqlparser.BoolVal, *sqlparser.NullVal:
-				values = append(values, v)
-			}
-		}
-		// 获取 operator
-		rOperator = r.Operator
-	default:
-		return nil
-	}
-
-	// operator 替换,用于在之后判断是否可以合并
-	switch lOperator {
-	case "in", "=":
-		lOperator = "="
-	default:
-		return nil
-	}
-
-	switch rOperator {
-	case "in", "=":
-		rOperator = "="
-	default:
-		return nil
-	}
-
-	// 不匹配则返回
-	if colInLeft == "" || colInLeft != colInRight ||
-		lOperator == "" || lOperator != rOperator {
-		return nil
-	}
-
-	// 合并左右子树的值
-	newValTuple := make(sqlparser.ValTuple, 0)
-	for _, v := range values {
-		switch v := v.(type) {
-		case *sqlparser.SQLVal:
-			newValTuple = append(newValTuple, v)
-		case *sqlparser.BoolVal:
-			newValTuple = append(newValTuple, v)
-		case *sqlparser.NullVal:
-			newValTuple = append(newValTuple, v)
-		case sqlparser.ValTuple:
-			newValTuple = append(newValTuple, v...)
-		}
-	}
-
-	// 去 expr 中除重复的 value,
-	newValTuple = removeDup(newValTuple...)
-	newExpr := &sqlparser.ComparisonExpr{
-		Operator: "in",
-		Left:     colName,
-		Right:    newValTuple,
-	}
-	// 如果只有一个值则是一个等式,没有必要转写成 in
-	if len(newValTuple) == 1 {
-		newExpr = &sqlparser.ComparisonExpr{
-			Operator: lOperator,
-			Left:     colName,
-			Right:    newValTuple[0],
-		}
-	}
-
-	return newExpr
-}
-
-// removeDup 清除 sqlparser.ValTuple 中重复的值
-func removeDup(vt ...sqlparser.Expr) sqlparser.ValTuple {
-	uni := make(sqlparser.ValTuple, 0)
-	m := make(map[string]sqlparser.SQLNode)
-
-	for _, value := range vt {
-		switch v := value.(type) {
-		case *sqlparser.SQLVal:
-			// Type:Val, 冒号用于分隔 Type 和 Val,防止两种不同类型拼接后出现同一个值
-			if _, ok := m[string(v.Type)+":"+sqlparser.String(v)]; !ok {
-				uni = append(uni, v)
-				m[string(v.Type)+":"+sqlparser.String(v)] = v
-			}
-		case *sqlparser.BoolVal:
-			if _, ok := m[sqlparser.String(v)]; !ok {
-				uni = append(uni, v)
-				m[sqlparser.String(v)] = v
-			}
-		case *sqlparser.NullVal:
-			if _, ok := m[sqlparser.String(v)]; !ok {
-				uni = append(uni, v)
-				m[sqlparser.String(v)] = v
-			}
-		case sqlparser.ValTuple:
-			for _, val := range removeDup(v...) {
-				switch v := val.(type) {
-				case *sqlparser.SQLVal:
-					if _, ok := m[string(v.Type)+":"+sqlparser.String(v)]; !ok {
-						uni = append(uni, v)
-						m[string(v.Type)+":"+sqlparser.String(v)] = v
-					}
-				case *sqlparser.BoolVal:
-					if _, ok := m[sqlparser.String(v)]; !ok {
-						uni = append(uni, v)
-						m[sqlparser.String(v)] = v
-					}
-				case *sqlparser.NullVal:
-					if _, ok := m[sqlparser.String(v)]; !ok {
-						uni = append(uni, v)
-						m[sqlparser.String(v)] = v
-					}
-				}
-			}
-		}
-	}
-
-	return uni
-}
-
-// RewriteInNull innull: TODO: 对应ARG.004
-func (rw *Rewrite) RewriteInNull() *Rewrite {
-	return rw
-}
-
-// RewriteRmParenthesis rmparenthesis: 去除无意义的括号
-func (rw *Rewrite) RewriteRmParenthesis() *Rewrite {
-	rw.rmParenthesis()
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// rmParenthesis 用于语出无用的括号
-func (rw *Rewrite) rmParenthesis() {
-	continueFlag := false
-	err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-		switch node := node.(type) {
-		case *sqlparser.Where:
-			if node == nil {
-				return true, nil
-			}
-			switch paren := node.Expr.(type) {
-			case *sqlparser.ParenExpr:
-				switch paren.Expr.(type) {
-				case *sqlparser.ComparisonExpr:
-					node.Expr = paren.Expr
-					continueFlag = true
-				}
-			}
-
-		case *sqlparser.ParenExpr:
-			switch paren := node.Expr.(type) {
-			case *sqlparser.ParenExpr:
-				switch paren.Expr.(type) {
-				case *sqlparser.ComparisonExpr:
-					node.Expr = paren.Expr
-					continueFlag = true
-				}
-			}
-
-		case *sqlparser.AndExpr:
-			switch left := node.Left.(type) {
-			case *sqlparser.ParenExpr:
-				switch inner := left.Expr.(type) {
-				case *sqlparser.ComparisonExpr:
-					node.Left = inner
-					continueFlag = true
-				}
-			}
-
-			switch right := node.Right.(type) {
-			case *sqlparser.ParenExpr:
-				switch inner := right.Expr.(type) {
-				case *sqlparser.ComparisonExpr:
-					node.Right = inner
-					continueFlag = true
-				}
-			}
-
-		case *sqlparser.OrExpr:
-			switch left := node.Left.(type) {
-			case *sqlparser.ParenExpr:
-				switch inner := left.Expr.(type) {
-				case *sqlparser.ComparisonExpr:
-					node.Left = inner
-					continueFlag = true
-				}
-			}
-
-			switch right := node.Right.(type) {
-			case *sqlparser.ParenExpr:
-				switch inner := right.Expr.(type) {
-				case *sqlparser.ComparisonExpr:
-					node.Right = inner
-					continueFlag = true
-				}
-			}
-		}
-		return true, nil
-	}, rw.Stmt)
-	common.LogIfError(err, "")
-	// 本层的修改可能使得原本不符合条件的括号变为无意义括号
-	// 每次修改都需要再过滤一遍语法树
-	if continueFlag {
-		rw.rmParenthesis()
-	} else {
-		return
-	}
-}
-
-// RewriteRemoveDMLOrderBy dmlorderby: 对应 RES.004,删除无 LIMIT 条件时 UPDATE, DELETE 中包含的 ORDER BY
-func (rw *Rewrite) RewriteRemoveDMLOrderBy() *Rewrite {
-	switch st := rw.Stmt.(type) {
-	case *sqlparser.Update:
-		err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-			switch n := node.(type) {
-			case *sqlparser.Select:
-				if n.OrderBy != nil && n.Limit == nil {
-					n.OrderBy = nil
-				}
-				return false, nil
-			}
-			return true, nil
-		}, rw.Stmt)
-		common.LogIfError(err, "")
-		if st.OrderBy != nil && st.Limit == nil {
-			st.OrderBy = nil
-		}
-	case *sqlparser.Delete:
-		err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-			switch n := node.(type) {
-			case *sqlparser.Select:
-				if n.OrderBy != nil && n.Limit == nil {
-					n.OrderBy = nil
-				}
-				return false, nil
-			}
-			return true, nil
-		}, rw.Stmt)
-		common.LogIfError(err, "")
-		if st.OrderBy != nil && st.Limit == nil {
-			st.OrderBy = nil
-		}
-	}
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// RewriteGroupByConst 对应CLA.004,将GROUP BY CONST替换为列名
-// TODO:
-func (rw *Rewrite) RewriteGroupByConst() *Rewrite {
-	err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-		switch n := node.(type) {
-		case *sqlparser.Select:
-			groupByCol := false
-			if n.GroupBy != nil {
-				for _, group := range n.GroupBy {
-					switch group.(type) {
-					case *sqlparser.SQLVal:
-					default:
-						groupByCol = true
-					}
-				}
-				if !groupByCol {
-					// TODO: 这里只是去掉了GROUP BY并没解决问题
-					n.GroupBy = nil
-				}
-			}
-		}
-		return true, nil
-	}, rw.Stmt)
-	common.LogIfError(err, "")
-	rw.NewSQL = sqlparser.String(rw.Stmt)
-	return rw
-}
-
-// RewriteSubQuery2Join 将 subquery 转写成 join
-func (rw *Rewrite) RewriteSubQuery2Join() *Rewrite {
-	var err error
-	// 如果未配置 mysql 环境或从环境中获取失败
-	if common.Config.TestDSN.Disable || len(rw.Columns) == 0 {
-		common.Log.Debug("(rw *Rewrite) RewriteSubQuery2Join(): Rewrite failed. TestDSN.Disable: %v, len(rw.Columns):%d",
-			common.Config.TestDSN.Disable, len(rw.Columns))
-		return rw
-	}
-
-	if rw.NewSQL == "" {
-		rw.NewSQL = sqlparser.String(rw.Stmt)
-	}
-
-	// query backup
-	backup := rw.NewSQL
-	var subQueryList []string
-	err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-		switch sub := node.(type) {
-		case sqlparser.SelectStatement:
-			subStr := sqlparser.String(sub)
-			if strings.HasPrefix(subStr, "(") {
-				subStr = subStr[1 : len(subStr)-1]
-			}
-			subQueryList = append(subQueryList, subStr)
-		}
-		return true, nil
-	}, rw.Stmt)
-	common.LogIfError(err, "")
-	if length := len(subQueryList); length > 1 {
-		lastResult := ""
-		for i := length - 1; i > 0; i-- {
-			if lastResult == "" {
-				lastResult, err = rw.sub2Join(subQueryList[i-1], subQueryList[i])
-			} else {
-				// 将subquery的部分替换成上次合并的结果
-				subQueryList[i-1] = strings.Replace(subQueryList[i-1], subQueryList[i], lastResult, -1)
-				lastResult, err = rw.sub2Join(subQueryList[i-1], lastResult)
-			}
-
-			if err != nil {
-				common.Log.Error("RewriteSubQuery2Join Error: %v", err)
-				return rw
-			}
-		}
-		rw.NewSQL = lastResult
-	} else if length == 1 {
-		var newSQL string
-		newSQL, err = rw.sub2Join(rw.NewSQL, subQueryList[0])
-		if err == nil {
-			rw.NewSQL = newSQL
-		}
-	}
-
-	// 因为这个修改不会直接修改rw.stmt,所以需要将rw.stmt也更新一下
-	newStmt, err := sqlparser.Parse(rw.NewSQL)
-	if err != nil {
-		rw.NewSQL = backup
-		rw.Stmt, _ = sqlparser.Parse(backup)
-	} else {
-		rw.Stmt = newStmt
-	}
-
-	return rw
-}
-
-// sub2Join 将 subquery 转写成 join
-func (rw *Rewrite) sub2Join(parent, sub string) (string, error) {
-	// 只处理SelectStatement
-	if sqlparser.Preview(parent) != sqlparser.StmtSelect || sqlparser.Preview(sub) != sqlparser.StmtSelect {
-		return "", nil
-	}
-
-	// 如果子查询不属于parent,则不处理
-	if !strings.Contains(parent, sub) {
-		return "", nil
-	}
-
-	// 解析外层SQL语法树
-	stmt, err := sqlparser.Parse(parent)
-	if err != nil {
-		common.Log.Warn("(rw *Rewrite) RewriteSubQuery2Join() sub2Join sql `%s` parsed error: %v", parent, err)
-		return "", err
-	}
-
-	switch stmt.(type) {
-	case sqlparser.SelectStatement:
-	default:
-		common.Log.Debug("Query `%s` not select statement.", parent)
-		return "", nil
-	}
-
-	// 解析子查询语法树
-	subStmt, err := sqlparser.Parse(sub)
-	if err != nil {
-		common.Log.Warn("(rw *Rewrite) RewriteSubQuery2Join() sub2Join sql `%s` parsed error: %v", sub, err)
-		return "", err
-	}
-
-	// 获取外部SQL用到的表
-	stmtMeta := GetTableFromExprs(stmt.(*sqlparser.Select).From)
-	// 获取内部SQL用到的表
-	subMeta := GetTableFromExprs(subStmt.(*sqlparser.Select).From)
-
-	// 处理关联条件
-	err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-		switch p := node.(type) {
-		case *sqlparser.ComparisonExpr:
-			// a in (select * from tb)
-			switch subquery := p.Right.(type) {
-			case *sqlparser.Subquery:
-
-				// 获取左边的列
-				var leftColumn *sqlparser.ColName
-
-				switch l := p.Left.(type) {
-				case *sqlparser.ColName:
-					leftColumn = l
-				default:
-					return false, nil
-				}
-
-				// 用于存放获取的subquery中的列,有且只有一个
-				var rightColumn sqlparser.SQLNode
-
-				// 对subquery中的列进行替换
-				switch subSelectStmt := subquery.Select.(type) {
-				case *sqlparser.Select:
-					cachingOperator := p.Operator
-
-					rightColumn = subSelectStmt.SelectExprs[0]
-
-					rightCol, _ := getColumnName(rightColumn.(*sqlparser.AliasedExpr).Expr)
-					if rightCol != nil {
-						// 将subquery替换为等值条件
-						p.Operator = "="
-
-						// selectExpr 信息补齐
-						var newExprs []sqlparser.SelectExpr
-						err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-							switch col := node.(type) {
-							case *sqlparser.StarExpr:
-								if col.TableName.Name.IsEmpty() {
-									for dbName, db := range stmtMeta {
-										for tbName := range db.Table {
-
-											col.TableName.Name = sqlparser.NewTableIdent(tbName)
-											if dbName != "" {
-												col.TableName.Qualifier = sqlparser.NewTableIdent(dbName)
-											}
-
-											newExprs = append(newExprs, col)
-										}
-									}
-								}
-							case *sqlparser.AliasedExpr:
-								switch n := col.Expr.(type) {
-								case *sqlparser.ColName:
-									col.Expr = columnFromWhere(n, stmtMeta, rw.Columns)
-								}
-							}
-							return true, nil
-						}, stmt.(*sqlparser.Select).SelectExprs)
-						common.LogIfError(err, "")
-
-						// 原节点列信息补齐
-						p.Left = columnFromWhere(leftColumn, stmtMeta, rw.Columns)
-
-						// 将子查询中的节点上提,补充前缀信息
-						p.Right = columnFromWhere(rightCol, subMeta, rw.Columns)
-
-						// subquery Where条件中的列信息补齐
-						subWhereExpr := subStmt.(*sqlparser.Select).Where
-						err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
-							switch n := node.(type) {
-							case *sqlparser.ComparisonExpr:
-								switch left := n.Left.(type) {
-								case *sqlparser.ColName:
-									n.Left = columnFromWhere(left, subMeta, rw.Columns)
-								}
-
-								switch right := n.Right.(type) {
-								case *sqlparser.ColName:
-									n.Right = columnFromWhere(right, subMeta, rw.Columns)
-								}
-							}
-							return true, nil
-						}, subWhereExpr)
-						common.LogIfError(err, "")
-						// 如果 subquery 中存在 Where 条件,怼在 parent 的 where 中后面
-						if subWhereExpr != nil {
-							if stmt.(*sqlparser.Select).Where != nil {
-								stmt.(*sqlparser.Select).Where.Expr = &sqlparser.AndExpr{
-									Left:  stmt.(*sqlparser.Select).Where.Expr,
-									Right: subWhereExpr.Expr,
-								}
-							} else {
-								stmt.(*sqlparser.Select).Where = subWhereExpr
-							}
-						}
-
-						switch cachingOperator {
-						case "in":
-							// 将表以 inner join 的形式追加到 parent 的 from 中
-							var newTables []sqlparser.TableExpr
-							for _, subExpr := range subStmt.(*sqlparser.Select).From {
-								has := false
-								for _, expr := range stmt.(*sqlparser.Select).From {
-									if reflect.DeepEqual(expr, subExpr) {
-										has = true
-									}
-								}
-								if !has {
-									newTables = append(newTables, subExpr)
-								}
-							}
-							stmt.(*sqlparser.Select).From = append(stmt.(*sqlparser.Select).From, newTables...)
-						case "not in":
-							// 将表以left join 的形式 追加到 parent 的 from 中
-							// TODO:
-						}
-					}
-
-				}
-			}
-		}
-		return true, nil
-	}, stmt)
-	common.LogIfError(err, "")
-	newSQL := sqlparser.String(stmt)
-	return newSQL, nil
-}
-
-// columnFromWhere 获取列是来自哪个表,并补充前缀
-func columnFromWhere(col *sqlparser.ColName, meta common.Meta, columns common.TableColumns) *sqlparser.ColName {
-
-	for dbName, db := range meta {
-		for tbName := range db.Table {
-			for _, tables := range columns {
-				for _, columns := range tables {
-					for _, column := range columns {
-						if strings.EqualFold(col.Name.String(), column.Name) {
-							if col.Qualifier.Name.IsEmpty() && tbName == column.Table {
-								col.Qualifier.Name = sqlparser.NewTableIdent(column.Table)
-								return col
-							}
-							if (dbName == "" && tbName == column.Table) || (tbName == column.Table && dbName == column.DB) {
-								col.Qualifier.Name = sqlparser.NewTableIdent(column.Table)
-								if dbName != "" {
-									col.Qualifier.Qualifier = sqlparser.NewTableIdent(column.DB)
-								}
-								return col
-							}
-						}
-					}
-				}
-			}
-
-		}
-	}
-	return col
-}
-
-// RewriteJoin2SubQuery join2sub: TODO:
-// https://mariadb.com/kb/en/library/subqueries-and-joins/
-func (rw *Rewrite) RewriteJoin2SubQuery() *Rewrite {
-	return rw
-}
-
-// RewriteDistinctStar distinctstar: 对应DIS.003,将多余的`DISTINCT *`删除
-func (rw *Rewrite) RewriteDistinctStar() *Rewrite {
-	// 注意:这里并未对表是否有主键做检查,按照我们的SQL编程规范,一张表必须有主键
-	switch rw.Stmt.(type) {
-	case *sqlparser.Select:
-		meta := GetMeta(rw.Stmt, nil)
-		for _, m := range meta {
-			if len(m.Table) == 1 {
-				// distinct tbl.*, distinct *, count(distinct *)
-				re := regexp.MustCompile(`(?i)((distinct\s*\*)|(distinct\s+[0-9a-z_` + "`" + `]*\.\*))`)
-				if re.MatchString(rw.SQL) {
-					rw.NewSQL = re.ReplaceAllString(rw.SQL, "*")
-				}
-			}
-			break
-		}
-	}
-	if rw.NewSQL == "" {
-		rw.NewSQL = rw.SQL
-	}
-	rw.Stmt, _ = sqlparser.Parse(rw.NewSQL)
-	return rw
-}
-
-// RewriteTruncate truncate: DELETE 全表修改为 TRUNCATE TABLE
-func (rw *Rewrite) RewriteTruncate() *Rewrite {
-	switch n := rw.Stmt.(type) {
-	case *sqlparser.Delete:
-		meta := GetMeta(rw.Stmt, nil)
-		if len(meta) == 1 && n.Where == nil {
-			for _, db := range meta {
-				for _, tbl := range db.Table {
-					rw.NewSQL = "truncate table " + tbl.TableName
-				}
-			}
-		}
-	}
-	return rw
-}
-
-// RewriteDML2Select dml2select: DML 转成 SELECT,兼容低版本的 EXPLAIN
-func (rw *Rewrite) RewriteDML2Select() *Rewrite {
-	if rw.Stmt == nil {
-		return rw
-	}
-
-	switch stmt := rw.Stmt.(type) {
-	case *sqlparser.Select:
-		rw.NewSQL = rw.SQL
-	case *sqlparser.Delete: // Multi DELETE not support yet.
-		rw.NewSQL = delete2Select(stmt)
-	case *sqlparser.Insert:
-		rw.NewSQL = insert2Select(stmt)
-	case *sqlparser.Update: // Multi UPDATE not support yet.
-		rw.NewSQL = update2Select(stmt)
-	}
-	rw.Stmt, _ = sqlparser.Parse(rw.NewSQL)
-	return rw
-}
-
-// delete2Select 将 Delete 语句改写成 Select
-func delete2Select(stmt *sqlparser.Delete) string {
-	newSQL := &sqlparser.Select{
-		SelectExprs: []sqlparser.SelectExpr{
-			new(sqlparser.StarExpr),
-		},
-		From:    stmt.TableExprs,
-		Where:   stmt.Where,
-		OrderBy: stmt.OrderBy,
-	}
-	return sqlparser.String(newSQL)
-}
-
-// update2Select 将 Update 语句改写成 Select
-func update2Select(stmt *sqlparser.Update) string {
-	newSQL := &sqlparser.Select{
-		SelectExprs: []sqlparser.SelectExpr{
-			new(sqlparser.StarExpr),
-		},
-		From:    stmt.TableExprs,
-		Where:   stmt.Where,
-		OrderBy: stmt.OrderBy,
-		Limit:   stmt.Limit,
-	}
-	return sqlparser.String(newSQL)
-}
-
-// insert2Select 将 Insert 语句改写成 Select
-func insert2Select(stmt *sqlparser.Insert) string {
-	switch row := stmt.Rows.(type) {
-	// 如果insert包含子查询,只需要explain该子树
-	case *sqlparser.Select, *sqlparser.Union, *sqlparser.ParenSelect:
-		return sqlparser.String(row)
-	}
-
-	return "select 1 from DUAL"
-}
-
-// AlterAffectTable 获取ALTER影响的库表名,返回:`db`.`table`
-func AlterAffectTable(stmt sqlparser.Statement) string {
-	switch n := stmt.(type) {
-	case *sqlparser.DDL:
-		tableName := strings.ToLower(n.Table.Name.String())
-		dbName := strings.ToLower(n.Table.Qualifier.String())
-		if tableName != "" && tableName != "dual" {
-			if dbName == "" {
-				return "`" + tableName + "`"
-			}
-
-			return "`" + dbName + "`.`" + tableName + "`"
-		}
-	}
-	return ""
-}
-
-// MergeAlterTables mergealter: 将同一张表的多条 ALTER 语句合成一条 ALTER 语句
-// @input: sql, alter string
-// @output: [[db.]table]sql, 如果找不到 DB,key 为表名;如果找得到 DB,key 为 db.table
-func MergeAlterTables(sqls ...string) map[string]string {
-	alterSQLs := make(map[string][]string)
-	mergedAlterStr := make(map[string]string)
-
-	// table/column/index name can be quoted in back ticks
-	backTicks := "(`[^\\s]*`)"
-
-	alterExp := regexp.MustCompile(`(?i)alter\s*table\s*(` + backTicks + `|([^\s]*))\s*`)   // ALTER TABLE
-	renameExp := regexp.MustCompile(`(?i)rename\s*table\s*(` + backTicks + `|([^\s]*))\s*`) // RENAME TABLE
-	// CREATE [UNIQUE|FULLTEXT|SPATIAL|PRIMARY] [KEY|INDEX] idx_name ON tbl_name
-	createIndexExp := regexp.MustCompile(`(?i)create((unique)|(fulltext)|(spatial)|(primary)|(\s*)\s*)((index)|(key))\s*`)
-	indexNameExp := regexp.MustCompile(`(?i)(` + backTicks + `|([^\s]*))\s*`)
-	indexColsExp := regexp.MustCompile(`(?i)(` + backTicks + `|([^\s]*))\s*on\s*(` + backTicks + `|([^\s]*))\s*`)
-
-	for _, sql := range sqls {
-		sql = strings.Trim(sql, common.Config.Delimiter)
-		stmt, _ := sqlparser.Parse(sql)
-		alterSQL := ""
-		dbName := ""
-		tableName := ""
-		switch n := stmt.(type) {
-		case *sqlparser.DDL:
-			// 注意: 表名和库名不区分大小写
-			tableName = strings.ToLower(n.Table.Name.String())
-			dbName = strings.ToLower(n.Table.Qualifier.String())
-			switch n.Action {
-			case "rename":
-				if alterExp.MatchString(sql) {
-					common.Log.Debug("rename alterExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, ""))
-					alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, ""))
-				} else if renameExp.MatchString(sql) {
-					common.Log.Debug("rename renameExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, ""))
-					alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, ""))
-				} else {
-					common.Log.Warn("rename not match: ALTER %v %v", tableName, sql)
-				}
-			case "alter":
-				if alterExp.MatchString(sql) {
-					common.Log.Debug("rename alterExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, ""))
-					alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, ""))
-				} else if createIndexExp.MatchString(sql) {
-					buf := createIndexExp.ReplaceAllString(sql, "")
-					idxName := strings.TrimSpace(indexNameExp.FindString(buf))
-					buf = indexColsExp.ReplaceAllString(buf, "")
-					common.Log.Debug("alter createIndexExp: ALTER %v ADD INDEX %v %v", tableName, "ADD INDEX", idxName, buf)
-					alterSQL = fmt.Sprint("ADD INDEX", " "+idxName+" ", buf)
-				}
-			default:
-
-			}
-		}
-		if alterSQL != "" && tableName != "" && tableName != "dual" {
-			if dbName == "" {
-				alterSQLs["`"+tableName+"`"] = append(alterSQLs["`"+tableName+"`"], alterSQL)
-			} else {
-				alterSQLs["`"+dbName+"`.`"+tableName+"`"] = append(alterSQLs["`"+dbName+"`.`"+tableName+"`"], alterSQL)
-			}
-		}
-	}
-	for k, v := range alterSQLs {
-		mergedAlterStr[k] = fmt.Sprintln("ALTER TABLE", k, strings.Join(v, ", "), common.Config.Delimiter)
-	}
-	return mergedAlterStr
-}
-
-// RewriteRuleMatch 检查重写规则是否生效
-func RewriteRuleMatch(name string) bool {
-	for _, r := range common.Config.RewriteRules {
-		if r == name {
-			return true
-		}
-	}
-	return false
-}
diff --git a/vendor/github.com/XiaoMi/soar/ast/rewrite_test.go b/vendor/github.com/XiaoMi/soar/ast/rewrite_test.go
deleted file mode 100644
index 043acb9..0000000
--- a/vendor/github.com/XiaoMi/soar/ast/rewrite_test.go
+++ /dev/null
@@ -1,705 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package ast
-
-import (
-	"fmt"
-	"sort"
-	"testing"
-
-	"github.com/XiaoMi/soar/common"
-)
-
-func TestRewrite(t *testing.T) {
-	common.Config.TestDSN.Disable = false
-	testSQL := []map[string]string{
-		{
-			"input":  `SELECT * FROM film`,
-			"output": `select film.film_id, film.title, film.description, film.release_year, film.language_id, film.original_language_id, film.rental_duration from film;`,
-		},
-		{
-			"input":  `SELECT film.*, actor.actor_id FROM film,actor`,
-			"output": `select film.film_id, film.title, film.description, film.release_year, film.language_id, film.original_language_id, film.rental_duration, actor.actor_id from film, actor;`,
-		},
-		{
-			"input":  `insert into film values(1,2,3,4,5)`,
-			"output": `insert into film(film_id, title, description, release_year, language_id) values (1, 2, 3, 4, 5);`,
-		},
-		{
-			"input":  `insert into sakila.film values(1,2)`,
-			"output": `insert into sakila.film(film_id, title) values (1, 2);`,
-		},
-		{
-			"input":  `replace into sakila.film select id from tb`,
-			"output": `replace into sakila.film(film_id) select id from tb;`,
-		},
-		{
-			"input":  `replace into sakila.film select id, title, description from tb`,
-			"output": `replace into sakila.film(film_id, title, description) select id, title, description from tb;`,
-		},
-		{
-			"input":  `insert into film values(1,2,3,4,5)`,
-			"output": `insert into film(film_id, title, description, release_year, language_id) values (1, 2, 3, 4, 5);`,
-		},
-		{
-			"input":  `insert into sakila.film values(1,2)`,
-			"output": `insert into sakila.film(film_id, title) values (1, 2);`,
-		},
-		{
-			"input":  `replace into sakila.film select id from tb`,
-			"output": `replace into sakila.film(film_id) select id from tb;`,
-		},
-		{
-			"input":  `replace into sakila.film select id, title, description from tb`,
-			"output": `replace into sakila.film(film_id, title, description) select id, title, description from tb;`,
-		},
-		{
-			"input":  "DELETE FROM tbl WHERE col1=1 ORDER BY col",
-			"output": "delete from tbl where col1 = 1;",
-		},
-		{
-			"input":  "UPDATE tbl SET col =1 WHERE col1=1 ORDER BY col",
-			"output": "update tbl set col = 1 where col1 = 1;",
-		},
-	}
-
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"])
-		rw.Columns = map[string]map[string][]*common.Column{
-			"sakila": {
-				"film": {
-					{Name: "film_id", Table: "film"},
-					{Name: "title", Table: "film"},
-					{Name: "description", Table: "film"},
-					{Name: "release_year", Table: "film"},
-					{Name: "language_id", Table: "film"},
-					{Name: "original_language_id", Table: "film"},
-					{Name: "rental_duration", Table: "film"},
-				},
-			},
-		}
-		rw.Rewrite()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteStar2Columns(t *testing.T) {
-	common.Config.TestDSN.Disable = false
-	testSQL := []map[string]string{
-		{
-			"input":  `SELECT * FROM film`,
-			"output": `select film.film_id, film.title from film`,
-		},
-		{
-			"input":  `SELECT film.*, actor.actor_id FROM film,actor`,
-			"output": `select film.film_id, film.title, actor.actor_id from film, actor`,
-		},
-	}
-
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"])
-		rw.Columns = map[string]map[string][]*common.Column{
-			"sakila": {
-				"film": {
-					{Name: "film_id", Table: "film"},
-					{Name: "title", Table: "film"},
-				},
-			},
-		}
-		rw.RewriteStar2Columns()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteInsertColumns(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  `insert into film values(1,2,3,4,5)`,
-			"output": `insert into film(film_id, title, description, release_year, language_id) values (1, 2, 3, 4, 5)`,
-		},
-		{
-			"input":  `insert into sakila.film values(1,2)`,
-			"output": `insert into sakila.film(film_id, title) values (1, 2)`,
-		},
-		{
-			"input":  `replace into sakila.film select id from tb`,
-			"output": `replace into sakila.film(film_id) select id from tb`,
-		},
-		{
-			"input":  `replace into sakila.film select id, title, description from tb`,
-			"output": `replace into sakila.film(film_id, title, description) select id, title, description from tb`,
-		},
-	}
-
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"])
-		rw.Columns = map[string]map[string][]*common.Column{
-			"sakila": {
-				"film": {
-					{Name: "film_id", Table: "film"},
-					{Name: "title", Table: "film"},
-					{Name: "description", Table: "film"},
-					{Name: "release_year", Table: "film"},
-					{Name: "language_id", Table: "film"},
-					{Name: "original_language_id", Table: "film"},
-					{Name: "rental_duration", Table: "film"},
-				},
-			},
-		}
-		rw.RewriteInsertColumns()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteHaving(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  `SELECT state, COUNT(*) FROM Drivers GROUP BY state HAVING state IN ('GA', 'TX') ORDER BY state`,
-			"output": "select state, COUNT(*) from Drivers where state in ('GA', 'TX') group by state order by state asc",
-		},
-		{
-			"input":  `SELECT state, COUNT(*) FROM Drivers WHERE col =1 GROUP BY state HAVING state IN ('GA', 'TX') ORDER BY state`,
-			"output": "select state, COUNT(*) from Drivers where (col = 1) and state in ('GA', 'TX') group by state order by state asc",
-		},
-		{
-			"input":  `SELECT state, COUNT(*) FROM Drivers WHERE col =1 or col1 =2 GROUP BY state HAVING state IN ('GA', 'TX') ORDER BY state`,
-			"output": "select state, COUNT(*) from Drivers where (col = 1 or col1 = 2) and state in ('GA', 'TX') group by state order by state asc",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteHaving()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteAddOrderByNull(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  "SELECT sum(col1) FROM tbl GROUP BY col",
-			"output": "select sum(col1) from tbl group by col order by null",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteAddOrderByNull()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteRemoveDMLOrderBy(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  "DELETE FROM tbl WHERE col1=1 ORDER BY col",
-			"output": "delete from tbl where col1 = 1",
-		},
-		{
-			"input":  "UPDATE tbl SET col =1 WHERE col1=1 ORDER BY col",
-			"output": "update tbl set col = 1 where col1 = 1",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteRemoveDMLOrderBy()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteGroupByConst(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  "select 1;",
-			"output": "select 1 from dual",
-		},
-		/*
-				{
-					"input":  "SELECT col1 FROM tbl GROUP BY 1;",
-					"output": "select col1 from tbl GROUP BY col1",
-				},
-			    {
-					"input":  "SELECT col1, col2 FROM tbl GROUP BY 1, 2;",
-					"output": "select col1, col2 from tbl GROUP BY col1, col2",
-				},
-			    {
-					"input":  "SELECT col1, col2, col3 FROM tbl GROUP BY 1, 3;",
-					"output": "select col1, col2, col3 from tbl GROUP BY col1, col3",
-				},
-		*/
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteGroupByConst()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteStandard(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  "SELECT sum(col1) FROM tbl GROUP BY 1;",
-			"output": "select sum(col1) from tbl group by 1",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteStandard()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteCountStar(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  "SELECT count(col) FROM tbl GROUP BY 1;",
-			"output": "select count(*) from tbl group by 1",
-		},
-		{
-			"input":  "SELECT COUNT(tb.col) FROM tbl GROUP BY 1;",
-			"output": "select COUNT(tb.*) from tbl group by 1",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteCountStar()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteInnoDB(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  "CREATE TABLE t1(id bigint(20) NOT NULL AUTO_INCREMENT);",
-			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB ",
-		},
-		{
-			"input":  "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=memory ",
-			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB ",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteInnoDB()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteAutoIncrement(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  "CREATE TABLE t1(id bigint(20) NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123802;",
-			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB auto_increment=1 ",
-		},
-		{
-			"input":  "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB",
-			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteAutoIncrement()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteIntWidth(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  "CREATE TABLE t1(id bigint(10) NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123802;",
-			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB auto_increment=123802",
-		},
-		{
-			"input":  "CREATE TABLE t1(id bigint NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123802;",
-			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB auto_increment=123802",
-		},
-		{
-			"input":  "create table t1(id int(20) not null auto_increment) ENGINE=InnoDB;",
-			"output": "create table t1 (\n\tid int(10) not null auto_increment\n) ENGINE=InnoDB",
-		},
-		{
-			"input":  "create table t1(id int not null auto_increment) ENGINE=InnoDB;",
-			"output": "create table t1 (\n\tid int not null auto_increment\n) ENGINE=InnoDB",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteIntWidth()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteAlwaysTrue(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  "SELECT count(col) FROM tbl where 1=1;",
-			"output": "select count(col) from tbl",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where col=col;",
-			"output": "select count(col) from tbl where col = col",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where col=col2;",
-			"output": "select count(col) from tbl where col = col2",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where 1>=1;",
-			"output": "select count(col) from tbl",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where 1<=1;",
-			"output": "select count(col) from tbl",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where 1=1 and 2=2;",
-			"output": "select count(col) from tbl",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where 1=1 or 2=3;",
-			"output": "select count(col) from tbl where 2 = 3",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where 1=1 and 3=3 or 2=3;",
-			"output": "select count(col) from tbl where 2 = 3",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where 1=1 and 3=3 or 2!=3;",
-			"output": "select count(col) from tbl",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where 1=1 or 2=3 and 3=3 ;",
-			"output": "select count(col) from tbl where 2 = 3",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where (1=1);",
-			"output": "select count(col) from tbl",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where ('a'= 'a' or 'b' = 'b') and a = 'b';",
-			"output": "select count(col) from tbl where a = 'b'",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where (('a'= 'a' or 'b' = 'b') and a = 'b');",
-			"output": "select count(col) from tbl where (a = 'b')",
-		},
-		{
-			"input":  "SELECT count(col) FROM tbl where 'a'= 'a' or ('b' = 'b' and a = 'b');",
-			"output": "select count(col) from tbl where (a = 'b')",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteAlwaysTrue()
-		if rw == nil {
-			t.Errorf("NoRw")
-		} else if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-// TODO:
-func TestRewriteSubQuery2Join(t *testing.T) {
-	common.Config.TestDSN.Disable = true
-	testSQL := []map[string]string{
-		{
-			// 这个case是官方文档给的,但不一定正确,需要视表结构的定义来进行判断
-			"input":  `SELECT * FROM t1 WHERE id IN (SELECT id FROM t2);`,
-			"output": "",
-			//"output": `SELECT DISTINCT t1.* FROM t1, t2 WHERE t1.id=t2.id;`,
-		},
-		{
-			"input":  `SELECT * FROM t1 WHERE id NOT IN (SELECT id FROM t2);`,
-			"output": "",
-			//"output": `SELECT table1.* FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;`,
-		},
-		{
-			"input":  `SELECT * FROM t1 WHERE NOT EXISTS (SELECT id FROM t2 WHERE t1.id=t2.id);`,
-			"output": "",
-			//"output": `SELECT table1.* FROM table1 LEFT JOIN table2 ON table1.id=table2.id WHERE table2.id IS NULL;`,
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteSubQuery2Join()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteDML2Select(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  "DELETE city, country FROM city INNER JOIN country using (country_id) WHERE city.city_id = 1;",
-			"output": "select * from city join country using (country_id) where city.city_id = 1",
-		}, {
-			"input":  "DELETE city FROM city LEFT JOIN country ON city.country_id = country.country_id WHERE country.country IS NULL;",
-			"output": "select * from city left join country on city.country_id = country.country_id where country.country is null",
-		}, {
-			"input":  "DELETE a1, a2 FROM city AS a1 INNER JOIN country AS a2 WHERE a1.country_id=a2.country_id",
-			"output": "select * from city as a1 join country as a2 where a1.country_id = a2.country_id",
-		}, {
-			"input":  "DELETE FROM a1, a2 USING city AS a1 INNER JOIN country AS a2 WHERE a1.country_id=a2.country_id",
-			"output": "select * from city as a1 join country as a2 where a1.country_id = a2.country_id",
-		}, {
-			"input":  "DELETE FROM film WHERE length > 100;",
-			"output": "select * from film where length > 100",
-		}, {
-			"input":  "UPDATE city INNER JOIN country USING(country_id) SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.city_id=10;",
-			"output": "select * from city join country using (country_id) where city.city_id = 10",
-		}, {
-			"input":  "UPDATE city INNER JOIN country ON city.country_id = country.country_id INNER JOIN address ON city.city_id = address.city_id SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.city_id=10;",
-			"output": "select * from city join country on city.country_id = country.country_id join address on city.city_id = address.city_id where city.city_id = 10",
-		}, {
-			"input":  "UPDATE city, country SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.country_id = country.country_id AND city.city_id=10;",
-			"output": "select * from city, country where city.country_id = country.country_id and city.city_id = 10",
-		}, {
-			"input":  "UPDATE film SET length = 10 WHERE language_id = 20;",
-			"output": "select * from film where language_id = 20",
-		}, {
-			"input":  "INSERT INTO city (country_id) SELECT country_id FROM country;",
-			"output": "select country_id from country",
-		}, {
-			"input":  "INSERT INTO city (country_id) VALUES (1),(2),(3);",
-			"output": "select 1 from DUAL",
-		}, {
-			"input":  "INSERT INTO city (country_id) VALUES (10);",
-			"output": "select 1 from DUAL",
-		}, {
-			"input":  "INSERT INTO city (country_id) SELECT 10 FROM DUAL;",
-			"output": "select 10 from dual",
-		}, {
-			"input":  "replace INTO city (country_id) SELECT 10 FROM DUAL;",
-			"output": "select 10 from dual",
-		},
-	}
-
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteDML2Select()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteDistinctStar(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  `SELECT DISTINCT * FROM film;`,
-			"output": "SELECT * FROM film;",
-		},
-		{
-			"input":  `SELECT COUNT(DISTINCT *) FROM film;`,
-			"output": "SELECT COUNT(*) FROM film;",
-		},
-		{
-			"input":  `SELECT DISTINCT film.* FROM film;`,
-			"output": "SELECT * FROM film;",
-		},
-		{
-			"input":  "SELECT DISTINCT col FROM film;",
-			"output": "SELECT DISTINCT col FROM film;",
-		},
-		{
-			"input":  "SELECT DISTINCT film.* FROM film, tbl;",
-			"output": "SELECT DISTINCT film.* FROM film, tbl;",
-		},
-		{
-
-			"input":  "SELECT DISTINCT * FROM film, tbl;",
-			"output": "SELECT DISTINCT * FROM film, tbl;",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteDistinctStar()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestMergeAlterTables(t *testing.T) {
-	sqls := []string{
-		// ADD|DROP INDEX
-		// TODO: PRIMARY KEY, [UNIQUE|FULLTEXT|SPATIAL] INDEX
-		"CREATE INDEX part_of_name ON customer (name(10));",
-		"alter table `sakila`.`t1` add index `idx_col`(`col`)",
-		"alter table `sakila`.`t1` add UNIQUE index `idx_col`(`col`)",
-		"alter table `sakila`.`t1` add index `idx_ID`(`ID`)",
-
-		// ADD|DROP COLUMN
-		"ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;",
-		"ALTER TABLE T2 ADD COLUMN C int;",
-		"ALTER TABLE T2 ADD COLUMN D int FIRST;",
-		"ALTER TABLE T2 ADD COLUMN E int AFTER D;",
-
-		// RENAME COLUMN
-		"ALTER TABLE t1 RENAME COLUMN a TO b",
-
-		// RENAME INDEX
-		"ALTER TABLE t1 RENAME INDEX idx_a TO idx_b",
-		"ALTER TABLE t1 RENAME KEY idx_a TO idx_b",
-
-		// RENAME TABLE
-		"ALTER TABLE db.old_table RENAME new_table;",
-		"ALTER TABLE old_table RENAME TO new_table;",
-		"ALTER TABLE old_table RENAME AS new_table;",
-
-		// MODIFY & CHANGE
-		"ALTER TABLE t1 MODIFY col1 BIGINT UNSIGNED DEFAULT 1 COMMENT 'my column';",
-		"ALTER TABLE t1 CHANGE b a INT NOT NULL;",
-
-		// table name quote in back ticks
-		"alter table `t3`add index `idx_a`(a)",
-		"alter table`t3`drop index`idx_b`(b)",
-	}
-
-	alterSQLs := MergeAlterTables(sqls...)
-	var sortedAlterSQLs []string
-	for item := range alterSQLs {
-		sortedAlterSQLs = append(sortedAlterSQLs, item)
-	}
-	sort.Strings(sortedAlterSQLs)
-
-	err := common.GoldenDiff(func() {
-		for _, tb := range sortedAlterSQLs {
-			fmt.Println(tb, ":", alterSQLs[tb])
-		}
-	}, t.Name(), update)
-	if err != nil {
-		t.Error(err)
-	}
-}
-
-func TestRewriteUnionAll(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  `select country_id from city union select country_id from country;`,
-			"output": "select country_id from city union all select country_id from country",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteUnionAll()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-func TestRewriteTruncate(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  `delete from tbl;`,
-			"output": "truncate table tbl",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteTruncate()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRewriteOr2In(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  `select country_id from city where country_id = 1 or country_id = 2 or country_id = 3;`,
-			"output": "select country_id from city where country_id in (1, 2, 3)",
-		},
-		// TODO or中的恒真条件
-		{
-			"input":  `select country_id from city where country_id != 1 or country_id != 2 or country_id = 3;`,
-			"output": "select country_id from city where country_id != 1 or country_id != 2 or country_id = 3",
-		},
-		// col = 1 or col is null不可转为IN
-		{
-			"input":  `select country_id from city where col = 1 or col is null;`,
-			"output": "select country_id from city where col = 1 or col is null",
-		},
-		{
-			"input":  `select country_id from city where col1 = 1 or col2 = 1 or col2 = 2;`,
-			"output": "select country_id from city where col1 = 1 or col2 in (1, 2)",
-		},
-		{
-			"input":  `select country_id from city where col1 = 1 or col2 = 1 or col2 = 2 or col1 = 3;`,
-			"output": "select country_id from city where col2 in (1, 2) or col1 in (1, 3)",
-		},
-		{
-			"input":  `select country_id from city where (col1 = 1 or col2 = 1 or col2 = 2 ) or col1 = 3;`,
-			"output": "select country_id from city where (col1 = 1 or col2 in (1, 2)) or col1 = 3",
-		},
-		{
-			"input":  `select country_id from city where col1 = 1 or (col2 = 1 or col2 = 2 ) or col1 = 3;`,
-			"output": "select country_id from city where (col2 in (1, 2)) or col1 in (1, 3)",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteOr2In()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestRmParenthesis(t *testing.T) {
-	testSQL := []map[string]string{
-		{
-			"input":  `select country_id from city where (country_id = 1);`,
-			"output": "select country_id from city where country_id = 1",
-		},
-		{
-			"input":  `select * from city where a = 1 and (country_id = 1);`,
-			"output": "select * from city where a = 1 and country_id = 1",
-		},
-		{
-			"input":  `select country_id from city where (country_id = 1) or country_id = 1 ;`,
-			"output": "select country_id from city where country_id = 1 or country_id = 1",
-		},
-		{
-			"input":  `select country_id from city where col = 1 or (country_id = 1) or country_id = 1 ;`,
-			"output": "select country_id from city where col = 1 or country_id = 1 or country_id = 1",
-		},
-	}
-	for _, sql := range testSQL {
-		rw := NewRewrite(sql["input"]).RewriteRmParenthesis()
-		if rw.NewSQL != sql["output"] {
-			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
-		}
-	}
-}
-
-func TestListRewriteRules(t *testing.T) {
-	err := common.GoldenDiff(func() {
-		ListRewriteRules(RewriteRules)
-	}, t.Name(), update)
-	if err != nil {
-		t.Error(err)
-	}
-}
diff --git a/vendor/github.com/XiaoMi/soar/ast/tidb.go b/vendor/github.com/XiaoMi/soar/ast/tidb.go
deleted file mode 100644
index 4fc27c1..0000000
--- a/vendor/github.com/XiaoMi/soar/ast/tidb.go
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package ast
-
-import (
-	"github.com/XiaoMi/soar/common"
-
-	"github.com/kr/pretty"
-	"github.com/pingcap/parser"
-	"github.com/pingcap/parser/ast"
-	_ "github.com/pingcap/tidb/types/parser_driver"
-)
-
-// TiParse TiDB 语法解析
-func TiParse(sql, charset, collation string) ([]ast.StmtNode, error) {
-	p := parser.New()
-	return p.Parse(sql, charset, collation)
-}
-
-// PrintPrettyStmtNode 打印TiParse语法树
-func PrintPrettyStmtNode(sql, charset, collation string) {
-	tree, err := TiParse(sql, charset, collation)
-	if err != nil {
-		common.Log.Warning(err.Error())
-	} else {
-		_, err = pretty.Println(tree)
-		common.LogIfWarn(err, "")
-	}
-}
-
-// TiVisitor TODO:
-type TiVisitor struct {
-	EnterFunc func(node ast.Node) bool
-	LeaveFunc func(node ast.Node) bool
-}
-
-// Enter TODO:
-func (visitor *TiVisitor) Enter(n ast.Node) (node ast.Node, skip bool) {
-	skip = visitor.EnterFunc(n)
-	return
-}
-
-// Leave TODO:
-func (visitor *TiVisitor) Leave(n ast.Node) (node ast.Node, ok bool) {
-	ok = visitor.LeaveFunc(n)
-	return
-}
diff --git a/vendor/github.com/XiaoMi/soar/ast/token.go b/vendor/github.com/XiaoMi/soar/ast/token.go
deleted file mode 100644
index ea64ee9..0000000
--- a/vendor/github.com/XiaoMi/soar/ast/token.go
+++ /dev/null
@@ -1,1028 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package ast
-
-import (
-	"errors"
-	"fmt"
-	"regexp"
-	"strings"
-	"unicode"
-
-	"vitess.io/vitess/go/vt/sqlparser"
-)
-
-// TokenType
-const (
-	TokenTypeWhitespace       = 0
-	TokenTypeWord             = 1
-	TokenTypeQuote            = 2
-	TokenTypeBacktickQuote    = 3
-	TokenTypeReserved         = 4
-	TokenTypeReservedToplevel = 5
-	TokenTypeReservedNewline  = 6
-	TokenTypeBoundary         = 7
-	TokenTypeComment          = 8
-	TokenTypeBlockComment     = 9
-	TokenTypeNumber           = 10
-	TokenTypeError            = 11
-	TokenTypeVariable         = 12
-)
-
-var maxCachekeySize = 15
-var cacheHits int
-var cacheMisses int
-var tokenCache map[string]Token
-
-var tokenBoundaries = []string{",", ";", ":", ")", "(", ".", "=", "<", ">", "+", "-", "*", "/", "!", "^", "%", "|", "&", "#"}
-
-var tokenReserved = []string{
-	"ACCESSIBLE", "ACTION", "AGAINST", "AGGREGATE", "ALGORITHM", "ALL", "ALTER", "ANALYSE", "ANALYZE", "AS", "ASC",
-	"AUTOCOMMIT", "AUTO_INCREMENT", "BACKUP", "BEGIN", "BETWEEN", "BINLOG", "BOTH", "CASCADE", "CASE", "CHANGE", "CHANGED", "CHARACTER SET",
-	"CHARSET", "CHECK", "CHECKSUM", "COLLATE", "COLLATION", "COLUMN", "COLUMNS", "COMMENT", "COMMIT", "COMMITTED", "COMPRESSED", "CONCURRENT",
-	"CONSTRAINT", "CONTAINS", "CONVERT", "CREATE", "CROSS", "CURRENT_TIMESTAMP", "DATABASE", "DATABASES", "DAY", "DAY_HOUR", "DAY_MINUTE",
-	"DAY_SECOND", "DEFAULT", "DEFINER", "DELAYED", "DELETE", "DESC", "DESCRIBE", "DETERMINISTIC", "DISTINCT", "DISTINCTROW", "DIV",
-	"DO", "DUMPFILE", "DUPLICATE", "DYNAMIC", "ELSE", "ENCLOSED", "END", "ENGINE", "ENGINE_TYPE", "ENGINES", "ESCAPE", "ESCAPED", "EVENTS", "EXEC",
-	"EXECUTE", "EXISTS", "EXPLAIN", "EXTENDED", "FAST", "FIELDS", "FILE", "FIRST", "FIXED", "FLUSH", "FOR", "FORCE", "FOREIGN", "FULL", "FULLTEXT",
-	"FUNCTION", "GLOBAL", "GRANT", "GRANTS", "GROUP_CONCAT", "HEAP", "HIGH_PRIORITY", "HOSTS", "HOUR", "HOUR_MINUTE",
-	"HOUR_SECOND", "IDENTIFIED", "IF", "IFNULL", "IGNORE", "IN", "INDEX", "INDEXES", "INFILE", "INSERT", "INSERT_ID", "INSERT_METHOD", "INTERVAL",
-	"INTO", "INVOKER", "IS", "ISOLATION", "KEY", "KEYS", "KILL", "LAST_INSERT_ID", "LEADING", "LEVEL", "LIKE", "LINEAR",
-	"LINES", "LOAD", "LOCAL", "LOCK", "LOCKS", "LOGS", "LOW_PRIORITY", "MARIA", "MASTER", "MASTER_CONNECT_RETRY", "MASTER_HOST", "MASTER_LOG_FILE",
-	"MATCH", "MAX_CONNECTIONS_PER_HOUR", "MAX_QUERIES_PER_HOUR", "MAX_ROWS", "MAX_UPDATES_PER_HOUR", "MAX_USER_CONNECTIONS",
-	"MEDIUM", "MERGE", "MINUTE", "MINUTE_SECOND", "MIN_ROWS", "MODE", "MODIFY",
-	"MONTH", "MRG_MYISAM", "MYISAM", "NAMES", "NATURAL", "NOT", "NOW()", "NULL", "OFFSET", "ON", "OPEN", "OPTIMIZE", "OPTION", "OPTIONALLY",
-	"ON UPDATE", "ON DELETE", "OUTFILE", "PACK_KEYS", "PAGE", "PARTIAL", "PARTITION", "PARTITIONS", "PASSWORD", "PRIMARY", "PRIVILEGES", "PROCEDURE",
-	"PROCESS", "PROCESSLIST", "PURGE", "QUICK", "RANGE", "RAID0", "RAID_CHUNKS", "RAID_CHUNKSIZE", "RAID_TYPE", "READ", "READ_ONLY",
-	"READ_WRITE", "REFERENCES", "REGEXP", "RELOAD", "RENAME", "REPAIR", "REPEATABLE", "REPLACE", "REPLICATION", "RESET", "RESTORE", "RESTRICT",
-	"RETURN", "RETURNS", "REVOKE", "RLIKE", "ROLLBACK", "ROW", "ROWS", "ROW_FORMAT", "SECOND", "SECURITY", "SEPARATOR",
-	"SERIALIZABLE", "SESSION", "SHARE", "SHOW", "SHUTDOWN", "SLAVE", "SONAME", "SOUNDS", "SQL", "SQL_AUTO_IS_NULL", "SQL_BIG_RESULT",
-	"SQL_BIG_SELECTS", "SQL_BIG_TABLES", "SQL_BUFFER_RESULT", "SQL_CALC_FOUND_ROWS", "SQL_LOG_BIN", "SQL_LOG_OFF", "SQL_LOG_UPDATE",
-	"SQL_LOW_PRIORITY_UPDATES", "SQL_MAX_JOIN_SIZE", "SQL_QUOTE_SHOW_CREATE", "SQL_SAFE_UPDATES", "SQL_SELECT_LIMIT", "SQL_SLAVE_SKIP_COUNTER",
-	"SQL_SMALL_RESULT", "SQL_WARNINGS", "SQL_CACHE", "SQL_NO_CACHE", "START", "STARTING", "STATUS", "STOP", "STORAGE",
-	"STRAIGHT_JOIN", "STRING", "STRIPED", "SUPER", "TABLE", "TABLES", "TEMPORARY", "TERMINATED", "THEN", "TO", "TRAILING", "TRANSACTIONAL", "TRUE",
-	"TRUNCATE", "TYPE", "TYPES", "UNCOMMITTED", "UNIQUE", "UNLOCK", "UNSIGNED", "USAGE", "USE", "USING", "VARIABLES",
-	"VIEW", "WHEN", "WITH", "WORK", "WRITE", "YEAR_MONTH",
-}
-
-var tokenReservedTopLevel = []string{
-	"SELECT", "FROM", "WHERE", "SET", "ORDER BY", "GROUP BY", "LIMIT", "DROP",
-	"VALUES", "UPDATE", "HAVING", "ADD", "AFTER", "ALTER TABLE", "DELETE FROM", "UNION ALL", "UNION", "EXCEPT", "INTERSECT",
-}
-
-var tokenFunction = []string{
-	"ABS", "ACOS", "ADDDATE", "ADDTIME", "AES_DECRYPT", "AES_ENCRYPT", "AREA", "ASBINARY", "ASCII", "ASIN", "ASTEXT", "ATAN", "ATAN2",
-	"AVG", "BDMPOLYFROMTEXT", "BDMPOLYFROMWKB", "BDPOLYFROMTEXT", "BDPOLYFROMWKB", "BENCHMARK", "BIN", "BIT_AND", "BIT_COUNT", "BIT_LENGTH",
-	"BIT_OR", "BIT_XOR", "BOUNDARY", "BUFFER", "CAST", "CEIL", "CEILING", "CENTROID", "CHAR", "CHARACTER_LENGTH", "CHARSET", "CHAR_LENGTH",
-	"COALESCE", "COERCIBILITY", "COLLATION", "COMPRESS", "CONCAT", "CONCAT_WS", "CONNECTION_ID", "CONTAINS", "CONV", "CONVERT", "CONVERT_TZ",
-	"CONVEXHULL", "COS", "COT", "COUNT", "CRC32", "CROSSES", "CURDATE", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "CURRENT_USER",
-	"CURTIME", "DATABASE", "DATE", "DATEDIFF", "DATE_ADD", "DATE_DIFF", "DATE_FORMAT", "DATE_SUB", "DAY", "DAYNAME", "DAYOFMONTH", "DAYOFWEEK",
-	"DAYOFYEAR", "DECODE", "DEFAULT", "DEGREES", "DES_DECRYPT", "DES_ENCRYPT", "DIFFERENCE", "DIMENSION", "DISJOINT", "DISTANCE", "ELT", "ENCODE",
-	"ENCRYPT", "ENDPOINT", "ENVELOPE", "EQUALS", "EXP", "EXPORT_SET", "EXTERIORRING", "EXTRACT", "EXTRACTVALUE", "FIELD", "FIND_IN_SET", "FLOOR",
-	"FORMAT", "FOUND_ROWS", "FROM_DAYS", "FROM_UNIXTIME", "GEOMCOLLFROMTEXT", "GEOMCOLLFROMWKB", "GEOMETRYCOLLECTION", "GEOMETRYCOLLECTIONFROMTEXT",
-	"GEOMETRYCOLLECTIONFROMWKB", "GEOMETRYFROMTEXT", "GEOMETRYFROMWKB", "GEOMETRYN", "GEOMETRYTYPE", "GEOMFROMTEXT", "GEOMFROMWKB", "GET_FORMAT",
-	"GET_LOCK", "GLENGTH", "GREATEST", "GROUP_CONCAT", "GROUP_UNIQUE_USERS", "HEX", "HOUR", "IF", "IFNULL", "INET_ATON", "INET_NTOA", "INSERT", "INSTR",
-	"INTERIORRINGN", "INTERSECTION", "INTERSECTS", "INTERVAL", "ISCLOSED", "ISEMPTY", "ISNULL", "ISRING", "ISSIMPLE", "IS_FREE_LOCK", "IS_USED_LOCK",
-	"LAST_DAY", "LAST_INSERT_ID", "LCASE", "LEAST", "LEFT", "LENGTH", "LINEFROMTEXT", "LINEFROMWKB", "LINESTRING", "LINESTRINGFROMTEXT", "LINESTRINGFROMWKB",
-	"LN", "LOAD_FILE", "LOCALTIME", "LOCALTIMESTAMP", "LOCATE", "LOG", "LOG10", "LOG2", "LOWER", "LPAD", "LTRIM", "MAKEDATE", "MAKETIME", "MAKE_SET",
-	"MASTER_POS_WAIT", "MAX", "MBRCONTAINS", "MBRDISJOINT", "MBREQUAL", "MBRINTERSECTS", "MBROVERLAPS", "MBRTOUCHES", "MBRWITHIN", "MD5", "MICROSECOND",
-	"MID", "MIN", "MINUTE", "MLINEFROMTEXT", "MLINEFROMWKB", "MOD", "MONTH", "MONTHNAME", "MPOINTFROMTEXT", "MPOINTFROMWKB", "MPOLYFROMTEXT", "MPOLYFROMWKB",
-	"MULTILINESTRING", "MULTILINESTRINGFROMTEXT", "MULTILINESTRINGFROMWKB", "MULTIPOINT", "MULTIPOINTFROMTEXT", "MULTIPOINTFROMWKB", "MULTIPOLYGON",
-	"MULTIPOLYGONFROMTEXT", "MULTIPOLYGONFROMWKB", "NAME_CONST", "NULLIF", "NUMGEOMETRIES", "NUMINTERIORRINGS", "NUMPOINTS", "OCT", "OCTET_LENGTH",
-	"OLD_PASSWORD", "ORD", "OVERLAPS", "PASSWORD", "PERIOD_ADD", "PERIOD_DIFF", "PI", "POINT", "POINTFROMTEXT", "POINTFROMWKB", "POINTN", "POINTONSURFACE",
-	"POLYFROMTEXT", "POLYFROMWKB", "POLYGON", "POLYGONFROMTEXT", "POLYGONFROMWKB", "POSITION", "POW", "POWER", "QUARTER", "QUOTE", "RADIANS", "RAND",
-	"RELATED", "RELEASE_LOCK", "REPEAT", "REPLACE", "REVERSE", "RIGHT", "ROUND", "ROW_COUNT", "RPAD", "RTRIM", "SCHEMA", "SECOND", "SEC_TO_TIME",
-	"SESSION_USER", "SHA", "SHA1", "SIGN", "SIN", "SLEEP", "SOUNDEX", "SPACE", "SQRT", "SRID", "STARTPOINT", "STD", "STDDEV", "STDDEV_POP", "STDDEV_SAMP",
-	"STRCMP", "STR_TO_DATE", "SUBDATE", "SUBSTR", "SUBSTRING", "SUBSTRING_INDEX", "SUBTIME", "SUM", "SYMDIFFERENCE", "SYSDATE", "SYSTEM_USER", "TAN",
-	"TIME", "TIMEDIFF", "TIMESTAMP", "TIMESTAMPADD", "TIMESTAMPDIFF", "TIME_FORMAT", "TIME_TO_SEC", "TOUCHES", "TO_DAYS", "TRIM", "TRUNCATE", "UCASE",
-	"UNCOMPRESS", "UNCOMPRESSED_LENGTH", "UNHEX", "UNIQUE_USERS", "UNIX_TIMESTAMP", "UPDATEXML", "UPPER", "USER", "UTC_DATE", "UTC_TIME", "UTC_TIMESTAMP",
-	"UUID", "VARIANCE", "VAR_POP", "VAR_SAMP", "VERSION", "WEEK", "WEEKDAY", "WEEKOFYEAR", "WITHIN", "X", "Y", "YEAR", "YEARWEEK",
-}
-
-var tokenReservedNewLine = []string{
-	"LEFT OUTER JOIN", "RIGHT OUTER JOIN", "LEFT JOIN", "RIGHT JOIN", "OUTER JOIN", "INNER JOIN", "JOIN", "XOR", "OR", "AND",
-}
-
-var regBoundariesString string
-var regReservedToplevelString string
-var regReservedNewlineString string
-var regReservedString string
-var regFunctionString string
-
-func init() {
-	var regs []string
-	for _, reg := range tokenBoundaries {
-		regs = append(regs, regexp.QuoteMeta(reg))
-	}
-	regBoundariesString = "(" + strings.Join(regs, "|") + ")"
-
-	regs = make([]string, 0)
-	for _, reg := range tokenReservedTopLevel {
-		regs = append(regs, regexp.QuoteMeta(reg))
-	}
-	regReservedToplevelString = "(" + strings.Join(regs, "|") + ")"
-
-	regs = make([]string, 0)
-	for _, reg := range tokenReservedNewLine {
-		regs = append(regs, regexp.QuoteMeta(reg))
-	}
-	regReservedNewlineString = "(" + strings.Join(regs, "|") + ")"
-
-	regs = make([]string, 0)
-	for _, reg := range tokenReserved {
-		regs = append(regs, regexp.QuoteMeta(reg))
-	}
-	regReservedString = "(" + strings.Join(regs, "|") + ")"
-
-	regs = make([]string, 0)
-	for _, reg := range tokenFunction {
-		regs = append(regs, regexp.QuoteMeta(reg))
-	}
-	regFunctionString = "(" + strings.Join(regs, "|") + ")"
-}
-
-// TokenString sqlparser tokens
-var TokenString = map[int]string{
-	sqlparser.LEX_ERROR:               "",
-	sqlparser.UNION:                   "union",
-	sqlparser.SELECT:                  "select",
-	sqlparser.STREAM:                  "stream",
-	sqlparser.INSERT:                  "insert",
-	sqlparser.UPDATE:                  "update",
-	sqlparser.DELETE:                  "delete",
-	sqlparser.FROM:                    "from",
-	sqlparser.WHERE:                   "where",
-	sqlparser.GROUP:                   "group",
-	sqlparser.HAVING:                  "having",
-	sqlparser.ORDER:                   "order",
-	sqlparser.BY:                      "by",
-	sqlparser.LIMIT:                   "limit",
-	sqlparser.OFFSET:                  "offset",
-	sqlparser.FOR:                     "for",
-	sqlparser.ALL:                     "all",
-	sqlparser.DISTINCT:                "distinct",
-	sqlparser.AS:                      "as",
-	sqlparser.EXISTS:                  "exists",
-	sqlparser.ASC:                     "asc",
-	sqlparser.DESC:                    "desc",
-	sqlparser.INTO:                    "into",
-	sqlparser.DUPLICATE:               "duplicate",
-	sqlparser.KEY:                     "key",
-	sqlparser.DEFAULT:                 "default",
-	sqlparser.SET:                     "set",
-	sqlparser.LOCK:                    "lock",
-	sqlparser.KEYS:                    "keys",
-	sqlparser.VALUES:                  "values",
-	sqlparser.LAST_INSERT_ID:          "last_insert_id",
-	sqlparser.NEXT:                    "next",
-	sqlparser.VALUE:                   "value",
-	sqlparser.SHARE:                   "share",
-	sqlparser.MODE:                    "mode",
-	sqlparser.SQL_NO_CACHE:            "sql_no_cache",
-	sqlparser.SQL_CACHE:               "sql_cache",
-	sqlparser.JOIN:                    "join",
-	sqlparser.STRAIGHT_JOIN:           "straight_join",
-	sqlparser.LEFT:                    "left",
-	sqlparser.RIGHT:                   "right",
-	sqlparser.INNER:                   "inner",
-	sqlparser.OUTER:                   "outer",
-	sqlparser.CROSS:                   "cross",
-	sqlparser.NATURAL:                 "natural",
-	sqlparser.USE:                     "use",
-	sqlparser.FORCE:                   "force",
-	sqlparser.ON:                      "on",
-	sqlparser.USING:                   "using",
-	sqlparser.ID:                      "id",
-	sqlparser.HEX:                     "hex",
-	sqlparser.STRING:                  "string",
-	sqlparser.INTEGRAL:                "integral",
-	sqlparser.FLOAT:                   "float",
-	sqlparser.HEXNUM:                  "hexnum",
-	sqlparser.VALUE_ARG:               "?",
-	sqlparser.LIST_ARG:                ":",
-	sqlparser.COMMENT:                 "",
-	sqlparser.COMMENT_KEYWORD:         "comment",
-	sqlparser.BIT_LITERAL:             "bit_literal",
-	sqlparser.NULL:                    "null",
-	sqlparser.TRUE:                    "true",
-	sqlparser.FALSE:                   "false",
-	sqlparser.OR:                      "||",
-	sqlparser.AND:                     "&&",
-	sqlparser.NOT:                     "not",
-	sqlparser.BETWEEN:                 "between",
-	sqlparser.CASE:                    "case",
-	sqlparser.WHEN:                    "when",
-	sqlparser.THEN:                    "then",
-	sqlparser.ELSE:                    "else",
-	sqlparser.END:                     "end",
-	sqlparser.LE:                      "<",
-	sqlparser.GE:                      ">=",
-	sqlparser.NE:                      "<>",
-	sqlparser.NULL_SAFE_EQUAL:         "<=>",
-	sqlparser.IS:                      "is",
-	sqlparser.LIKE:                    "like",
-	sqlparser.REGEXP:                  "regexp",
-	sqlparser.IN:                      "in",
-	sqlparser.SHIFT_LEFT:              "<<",
-	sqlparser.SHIFT_RIGHT:             ">>",
-	sqlparser.DIV:                     "div",
-	sqlparser.MOD:                     "mod",
-	sqlparser.UNARY:                   "unary",
-	sqlparser.COLLATE:                 "collate",
-	sqlparser.BINARY:                  "binary",
-	sqlparser.UNDERSCORE_BINARY:       "_binary",
-	sqlparser.INTERVAL:                "interval",
-	sqlparser.JSON_EXTRACT_OP:         "->>",
-	sqlparser.JSON_UNQUOTE_EXTRACT_OP: "->",
-	sqlparser.CREATE:                  "create",
-	sqlparser.ALTER:                   "alter",
-	sqlparser.DROP:                    "drop",
-	sqlparser.RENAME:                  "rename",
-	sqlparser.ANALYZE:                 "analyze",
-	sqlparser.ADD:                     "add",
-	sqlparser.SCHEMA:                  "schema",
-	sqlparser.TABLE:                   "table",
-	sqlparser.INDEX:                   "index",
-	sqlparser.VIEW:                    "view",
-	sqlparser.TO:                      "to",
-	sqlparser.IGNORE:                  "ignore",
-	sqlparser.IF:                      "if",
-	sqlparser.UNIQUE:                  "unique",
-	sqlparser.PRIMARY:                 "primary",
-	sqlparser.COLUMN:                  "column",
-	sqlparser.CONSTRAINT:              "constraint",
-	sqlparser.SPATIAL:                 "spatial",
-	sqlparser.FULLTEXT:                "fulltext",
-	sqlparser.FOREIGN:                 "foreign",
-	sqlparser.SHOW:                    "show",
-	sqlparser.DESCRIBE:                "describe",
-	sqlparser.EXPLAIN:                 "explain",
-	sqlparser.DATE:                    "date",
-	sqlparser.ESCAPE:                  "escape",
-	sqlparser.REPAIR:                  "repair",
-	sqlparser.OPTIMIZE:                "optimize",
-	sqlparser.TRUNCATE:                "truncate",
-	sqlparser.MAXVALUE:                "maxvalue",
-	sqlparser.PARTITION:               "partition",
-	sqlparser.REORGANIZE:              "reorganize",
-	sqlparser.LESS:                    "less",
-	sqlparser.THAN:                    "than",
-	sqlparser.PROCEDURE:               "procedure",
-	sqlparser.TRIGGER:                 "trigger",
-	sqlparser.VINDEX:                  "vindex",
-	sqlparser.VINDEXES:                "vindexes",
-	sqlparser.STATUS:                  "status",
-	sqlparser.VARIABLES:               "variables",
-	sqlparser.BEGIN:                   "begin",
-	sqlparser.START:                   "start",
-	sqlparser.TRANSACTION:             "transaction",
-	sqlparser.COMMIT:                  "commit",
-	sqlparser.ROLLBACK:                "rollback",
-	sqlparser.BIT:                     "bit",
-	sqlparser.TINYINT:                 "tinyint",
-	sqlparser.SMALLINT:                "smallint",
-	sqlparser.MEDIUMINT:               "mediumint",
-	sqlparser.INT:                     "int",
-	sqlparser.INTEGER:                 "integer",
-	sqlparser.BIGINT:                  "bigint",
-	sqlparser.INTNUM:                  "intnum",
-	sqlparser.REAL:                    "real",
-	sqlparser.DOUBLE:                  "double",
-	sqlparser.FLOAT_TYPE:              "float_type",
-	sqlparser.DECIMAL:                 "decimal",
-	sqlparser.NUMERIC:                 "numeric",
-	sqlparser.TIME:                    "time",
-	sqlparser.TIMESTAMP:               "timestamp",
-	sqlparser.DATETIME:                "datetime",
-	sqlparser.YEAR:                    "year",
-	sqlparser.CHAR:                    "char",
-	sqlparser.VARCHAR:                 "varchar",
-	sqlparser.BOOL:                    "bool",
-	sqlparser.CHARACTER:               "character",
-	sqlparser.VARBINARY:               "varbinary",
-	sqlparser.NCHAR:                   "nchar",
-	sqlparser.TEXT:                    "text",
-	sqlparser.TINYTEXT:                "tinytext",
-	sqlparser.MEDIUMTEXT:              "mediumtext",
-	sqlparser.LONGTEXT:                "longtext",
-	sqlparser.BLOB:                    "blob",
-	sqlparser.TINYBLOB:                "tinyblob",
-	sqlparser.MEDIUMBLOB:              "mediumblob",
-	sqlparser.LONGBLOB:                "longblob",
-	sqlparser.JSON:                    "json",
-	sqlparser.ENUM:                    "enum",
-	sqlparser.GEOMETRY:                "geometry",
-	sqlparser.POINT:                   "point",
-	sqlparser.LINESTRING:              "linestring",
-	sqlparser.POLYGON:                 "polygon",
-	sqlparser.GEOMETRYCOLLECTION:      "geometrycollection",
-	sqlparser.MULTIPOINT:              "multipoint",
-	sqlparser.MULTILINESTRING:         "multilinestring",
-	sqlparser.MULTIPOLYGON:            "multipolygon",
-	sqlparser.NULLX:                   "nullx",
-	sqlparser.AUTO_INCREMENT:          "auto_increment",
-	sqlparser.APPROXNUM:               "approxnum",
-	sqlparser.SIGNED:                  "signed",
-	sqlparser.UNSIGNED:                "unsigned",
-	sqlparser.ZEROFILL:                "zerofill",
-	sqlparser.DATABASES:               "databases",
-	sqlparser.TABLES:                  "tables",
-	sqlparser.VITESS_KEYSPACES:        "vitess_keyspaces",
-	sqlparser.VITESS_SHARDS:           "vitess_shards",
-	sqlparser.VITESS_TABLETS:          "vitess_tablets",
-	sqlparser.VSCHEMA_TABLES:          "vschema_tables",
-	sqlparser.NAMES:                   "names",
-	sqlparser.CHARSET:                 "charset",
-	sqlparser.GLOBAL:                  "global",
-	sqlparser.SESSION:                 "session",
-	sqlparser.CURRENT_TIMESTAMP:       "current_timestamp",
-	sqlparser.DATABASE:                "database",
-	sqlparser.CURRENT_DATE:            "current_date",
-	sqlparser.CURRENT_TIME:            "current_time",
-	sqlparser.LOCALTIME:               "localtime",
-	sqlparser.LOCALTIMESTAMP:          "localtimestamp",
-	sqlparser.UTC_DATE:                "utc_date",
-	sqlparser.UTC_TIME:                "utc_time",
-	sqlparser.UTC_TIMESTAMP:           "utc_timestamp",
-	sqlparser.REPLACE:                 "replace",
-	sqlparser.CONVERT:                 "convert",
-	sqlparser.CAST:                    "cast",
-	sqlparser.SUBSTR:                  "substr",
-	sqlparser.SUBSTRING:               "substring",
-	sqlparser.GROUP_CONCAT:            "group_concat",
-	sqlparser.SEPARATOR:               "separator",
-	sqlparser.MATCH:                   "match",
-	sqlparser.AGAINST:                 "against",
-	sqlparser.BOOLEAN:                 "boolean",
-	sqlparser.LANGUAGE:                "language",
-	sqlparser.WITH:                    "with",
-	sqlparser.QUERY:                   "query",
-	sqlparser.EXPANSION:               "expansion",
-	sqlparser.UNUSED:                  "",
-}
-
-// 这个变更从vitess更新过来,如果vitess新开了一个关键字这里也要同步开
-var mySQLKeywords = map[string]string{
-	"add":                "ADD",
-	"against":            "AGAINST",
-	"all":                "ALL",
-	"alter":              "ALTER",
-	"analyze":            "ANALYZE",
-	"and":                "AND",
-	"as":                 "AS",
-	"asc":                "ASC",
-	"auto_increment":     "AUTO_INCREMENT",
-	"begin":              "BEGIN",
-	"between":            "BETWEEN",
-	"bigint":             "BIGINT",
-	"binary":             "BINARY",
-	"_binary":            "UNDERSCORE_BINARY",
-	"bit":                "BIT",
-	"blob":               "BLOB",
-	"bool":               "BOOL",
-	"boolean":            "BOOLEAN",
-	"by":                 "BY",
-	"case":               "CASE",
-	"cast":               "CAST",
-	"char":               "CHAR",
-	"character":          "CHARACTER",
-	"charset":            "CHARSET",
-	"collate":            "COLLATE",
-	"column":             "COLUMN",
-	"comment":            "COMMENT_KEYWORD",
-	"commit":             "COMMIT",
-	"constraint":         "CONSTRAINT",
-	"convert":            "CONVERT",
-	"substr":             "SUBSTR",
-	"substring":          "SUBSTRING",
-	"create":             "CREATE",
-	"cross":              "CROSS",
-	"current_date":       "CURRENT_DATE",
-	"current_time":       "CURRENT_TIME",
-	"current_timestamp":  "CURRENT_TIMESTAMP",
-	"database":           "DATABASE",
-	"databases":          "DATABASES",
-	"date":               "DATE",
-	"datetime":           "DATETIME",
-	"decimal":            "DECIMAL",
-	"default":            "DEFAULT",
-	"delete":             "DELETE",
-	"desc":               "DESC",
-	"describe":           "DESCRIBE",
-	"distinct":           "DISTINCT",
-	"div":                "DIV",
-	"double":             "DOUBLE",
-	"drop":               "DROP",
-	"duplicate":          "DUPLICATE",
-	"else":               "ELSE",
-	"end":                "END",
-	"enum":               "ENUM",
-	"escape":             "ESCAPE",
-	"exists":             "EXISTS",
-	"explain":            "EXPLAIN",
-	"expansion":          "EXPANSION",
-	"false":              "FALSE",
-	"float":              "FLOAT_TYPE",
-	"for":                "FOR",
-	"force":              "FORCE",
-	"foreign":            "FOREIGN",
-	"from":               "FROM",
-	"fulltext":           "FULLTEXT",
-	"geometry":           "GEOMETRY",
-	"geometrycollection": "GEOMETRYCOLLECTION",
-	"global":             "GLOBAL",
-	"group":              "GROUP",
-	"group_concat":       "GROUP_CONCAT",
-	"having":             "HAVING",
-	"if":                 "IF",
-	"ignore":             "IGNORE",
-	"in":                 "IN",
-	"index":              "INDEX",
-	"inner":              "INNER",
-	"insert":             "INSERT",
-	"int":                "INT",
-	"integer":            "INTEGER",
-	"interval":           "INTERVAL",
-	"into":               "INTO",
-	"is":                 "IS",
-	"join":               "JOIN",
-	"json":               "JSON",
-	"key":                "KEY",
-	"keys":               "KEYS",
-	"key_block_size":     "KEY_BLOCK_SIZE",
-	"language":           "LANGUAGE",
-	"last_insert_id":     "LAST_INSERT_ID",
-	"left":               "LEFT",
-	"less":               "LESS",
-	"like":               "LIKE",
-	"limit":              "LIMIT",
-	"linestring":         "LINESTRING",
-	"localtime":          "LOCALTIME",
-	"localtimestamp":     "LOCALTIMESTAMP",
-	"lock":               "LOCK",
-	"longblob":           "LONGBLOB",
-	"longtext":           "LONGTEXT",
-	"match":              "MATCH",
-	"maxvalue":           "MAXVALUE",
-	"mediumblob":         "MEDIUMBLOB",
-	"mediumint":          "MEDIUMINT",
-	"mediumtext":         "MEDIUMTEXT",
-	"mod":                "MOD",
-	"mode":               "MODE",
-	"multilinestring":    "MULTILINESTRING",
-	"multipoint":         "MULTIPOINT",
-	"multipolygon":       "MULTIPOLYGON",
-	"names":              "NAMES",
-	"natural":            "NATURAL",
-	"nchar":              "NCHAR",
-	"next":               "NEXT",
-	"not":                "NOT",
-	"null":               "NULL",
-	"numeric":            "NUMERIC",
-	"offset":             "OFFSET",
-	"on":                 "ON",
-	"optimize":           "OPTIMIZE",
-	"or":                 "OR",
-	"order":              "ORDER",
-	"outer":              "OUTER",
-	"partition":          "PARTITION",
-	"point":              "POINT",
-	"polygon":            "POLYGON",
-	"primary":            "PRIMARY",
-	"procedure":          "PROCEDURE",
-	"query":              "QUERY",
-	"real":               "REAL",
-	"regexp":             "REGEXP",
-	"rename":             "RENAME",
-	"reorganize":         "REORGANIZE",
-	"repair":             "REPAIR",
-	"replace":            "REPLACE",
-	"right":              "RIGHT",
-	"rlike":              "REGEXP",
-	"rollback":           "ROLLBACK",
-	"schema":             "SCHEMA",
-	"select":             "SELECT",
-	"separator":          "SEPARATOR",
-	"session":            "SESSION",
-	"set":                "SET",
-	"share":              "SHARE",
-	"show":               "SHOW",
-	"signed":             "SIGNED",
-	"smallint":           "SMALLINT",
-	"spatial":            "SPATIAL",
-	"sql_cache":          "SQL_CACHE",
-	"sql_no_cache":       "SQL_NO_CACHE",
-	"start":              "START",
-	"status":             "STATUS",
-	"straight_join":      "STRAIGHT_JOIN",
-	"stream":             "STREAM",
-	"table":              "TABLE",
-	"tables":             "TABLES",
-	"text":               "TEXT",
-	"than":               "THAN",
-	"then":               "THEN",
-	"time":               "TIME",
-	"timestamp":          "TIMESTAMP",
-	"tinyblob":           "TINYBLOB",
-	"tinyint":            "TINYINT",
-	"tinytext":           "TINYTEXT",
-	"to":                 "TO",
-	"transaction":        "TRANSACTION",
-	"trigger":            "TRIGGER",
-	"true":               "TRUE",
-	"truncate":           "TRUNCATE",
-	"union":              "UNION",
-	"unique":             "UNIQUE",
-	"unsigned":           "UNSIGNED",
-	"update":             "UPDATE",
-	"use":                "USE",
-	"using":              "USING",
-	"utc_date":           "UTC_DATE",
-	"utc_time":           "UTC_TIME",
-	"utc_timestamp":      "UTC_TIMESTAMP",
-	"values":             "VALUES",
-	"variables":          "VARIABLES",
-	"varbinary":          "VARBINARY",
-	"varchar":            "VARCHAR",
-	"vindex":             "VINDEX",
-	"vindexes":           "VINDEXES",
-	"view":               "VIEW",
-	"vitess_keyspaces":   "VITESS_KEYSPACES",
-	"vitess_shards":      "VITESS_SHARDS",
-	"vitess_tablets":     "VITESS_TABLETS",
-	"vschema_tables":     "VSCHEMA_TABLES",
-	"when":               "WHEN",
-	"where":              "WHERE",
-	"with":               "WITH",
-	"year":               "YEAR",
-	"zerofill":           "ZEROFILL",
-}
-
-// Token 基本定义
-type Token struct {
-	Type int
-	Val  string
-	i    int
-}
-
-// Tokenizer 用于初始化token
-func Tokenizer(sql string) []Token {
-	var tokens []Token
-	tkn := sqlparser.NewStringTokenizer(sql)
-	typ, val := tkn.Scan()
-	for typ != 0 {
-		if val != nil {
-			tokens = append(tokens, Token{Type: typ, Val: string(val)})
-		} else {
-			if typ > 255 {
-				if v, ok := TokenString[typ]; ok {
-					tokens = append(tokens, Token{
-						Type: typ,
-						Val:  v,
-					})
-				} else {
-					tokens = append(tokens, Token{
-						Type: typ,
-						Val:  "",
-					})
-				}
-			} else {
-				tokens = append(tokens, Token{
-					Type: typ,
-					Val:  fmt.Sprintf("%c", typ),
-				})
-			}
-		}
-		typ, val = tkn.Scan()
-	}
-	return tokens
-}
-
-// MysqlEscapeString mysql_real_escape_string
-// https://github.com/liule/golang_escape
-func MysqlEscapeString(source string) (string, error) {
-	var j = 0
-	if len(source) == 0 {
-		return "", errors.New("source is null")
-	}
-	tempStr := source[:]
-	desc := make([]byte, len(tempStr)*2)
-	for i := 0; i < len(tempStr); i++ {
-		flag := false
-		var escape byte
-		switch tempStr[i] {
-		case '\r':
-			flag = true
-			escape = '\r'
-		case '\n':
-			flag = true
-			escape = '\n'
-		case '\\':
-			flag = true
-			escape = '\\'
-		case '\'':
-			flag = true
-			escape = '\''
-		case '"':
-			flag = true
-			escape = '"'
-		case '\032':
-			flag = true
-			escape = 'Z'
-		default:
-		}
-		if flag {
-			desc[j] = '\\'
-			desc[j+1] = escape
-			j = j + 2
-		} else {
-			desc[j] = tempStr[i]
-			j = j + 1
-		}
-	}
-	return string(desc[0:j]), nil
-}
-
-// IsMysqlKeyword 判断是否是关键字
-func IsMysqlKeyword(name string) bool {
-	_, ok := mySQLKeywords[strings.ToLower(strings.TrimSpace(name))]
-	return ok
-}
-
-// getNextToken 从 buf 中获取 token
-func getNextToken(buf string, previous Token) Token {
-	var typ int // TOKEN_TYPE
-
-	// Whitespace
-	whiteSpaceReg := regexp.MustCompile(`^\s+`)
-	if whiteSpaceReg.MatchString(buf) {
-		return Token{
-			Type: TokenTypeWhitespace,
-			Val:  " ",
-		}
-	}
-
-	// Comment (#, --, /**/)
-	if buf[0] == '#' || (len(buf) > 1 && (buf[:2] == "--" || buf[:2] == "/*")) {
-		var last int
-		if buf[0] == '-' || buf[0] == '#' {
-			// Comment until end of line
-			last = strings.Index(buf, "\n")
-			typ = TokenTypeComment
-		} else {
-			// Comment until closing comment tag
-			last = strings.Index(buf[2:], "*/") + 2
-			typ = TokenTypeBlockComment
-		}
-		if last == 0 {
-			last = len(buf)
-		}
-		return Token{
-			Type: typ,
-			Val:  buf[:last],
-		}
-	}
-
-	// Quoted String
-	if buf[0] == '"' || buf[0] == '\'' || buf[0] == '`' || buf[0] == '[' {
-		var typ int
-		switch buf[0] {
-		case '`', '[':
-			typ = TokenTypeBacktickQuote
-		default:
-			typ = TokenTypeQuote
-		}
-		return Token{
-			Type: typ,
-			Val:  getQuotedString(buf),
-		}
-	}
-
-	// User-defined Variable
-	if (buf[0] == '@' || buf[0] == ':') && len(buf) > 1 {
-		ret := Token{
-			Type: TokenTypeVariable,
-			Val:  "",
-		}
-
-		if buf[1] == '"' || buf[1] == '\'' || buf[1] == '`' {
-			// If the variable name is quoted
-			ret.Val = string(buf[0]) + getQuotedString(buf[1:])
-		} else {
-			// Non-quoted variable name
-			varReg := regexp.MustCompile(`^(` + string(buf[0]) + `[a-zA-Z0-9\._\$]+)`)
-			if varReg.MatchString(buf) {
-				ret.Val = varReg.FindString(buf)
-			}
-		}
-
-		if ret.Val != "" {
-			return ret
-		}
-	}
-
-	// Number(decimal, binary, hex...)
-	numReg := regexp.MustCompile(`^([0-9]+(\.[0-9]+)?|0x[0-9a-fA-F]+|0b[01]+)($|\s|"'` + "`|" + regBoundariesString + ")")
-	if numReg.MatchString(buf) {
-		return Token{
-			Type: TokenTypeNumber,
-			Val:  numReg.FindString(buf),
-		}
-	}
-
-	// Boundary Character(punctuation and symbols)
-	boundaryReg := regexp.MustCompile(`^(` + regBoundariesString + `)`)
-	if boundaryReg.MatchString(buf) {
-		return Token{
-			Type: TokenTypeBoundary,
-			Val:  boundaryReg.FindString(buf),
-		}
-	}
-	sqlUpper := strings.ToUpper(buf)
-	// A reserved word cannot be preceded by a '.'
-	// this makes it so in "mytable.from", "from" is not considered a reserved word
-	if previous.Val != "." {
-		// Top Level Reserved Word
-		reservedToplevelReg := regexp.MustCompile(`^(` + regReservedToplevelString + `)($|\s|` + regBoundariesString + `)`)
-		if reservedToplevelReg.MatchString(sqlUpper) {
-			return Token{
-				Type: TokenTypeReservedToplevel,
-				Val:  reservedToplevelReg.FindString(sqlUpper),
-			}
-		}
-
-		// Newline Reserved Word
-		reservedNewlineReg := regexp.MustCompile(`^(` + regReservedNewlineString + `)($|\s|` + regBoundariesString + `)`)
-		if reservedNewlineReg.MatchString(sqlUpper) {
-			return Token{
-				Type: TokenTypeReservedNewline,
-				Val:  reservedNewlineReg.FindString(sqlUpper),
-			}
-		}
-
-		// Other Reserved Word
-		reservedReg := regexp.MustCompile(`^(` + regReservedString + `)($|\s|` + regBoundariesString + `)`)
-		if reservedNewlineReg.MatchString(sqlUpper) {
-			return Token{
-				Type: TokenTypeReserved,
-				Val:  reservedReg.FindString(sqlUpper),
-			}
-		}
-
-	}
-
-	// function
-	// A function must be succeeded by '('
-	// this makes it so "count(" is considered a function, but "count" alone is not
-	functionReg := regexp.MustCompile(`^(` + regFunctionString + `)($|\s|` + regBoundariesString + `)`)
-	if functionReg.MatchString(sqlUpper) {
-		return Token{
-			Type: TokenTypeReserved,
-			Val:  functionReg.FindString(sqlUpper),
-		}
-	}
-
-	// Non reserved word
-	noReservedReg := regexp.MustCompile(`(.*?)($|\s|["'` + "`]|" + regBoundariesString + `)`)
-	if noReservedReg.MatchString(buf) {
-		return Token{
-			Type: TokenTypeWord,
-			Val:  noReservedReg.FindString(buf),
-		}
-	}
-	return Token{}
-}
-
-// getQuotedString 获取quote
-func getQuotedString(buf string) string {
-	// This checks for the following patterns:
-	// 1. backtick quoted string using `` to escape
-	// 2. double quoted string using "" or \" to escape
-	// 3. single quoted string using '' or \' to escape
-	start := string(buf[0])
-	switch start {
-	case "\"", "`", "'":
-		reg := fmt.Sprintf(`(^%s[^%s\\]*(?:\\.[^%s\\]*)*(%s|$))+`, start, start, start, start)
-		quotedReg := regexp.MustCompile(reg)
-		if quotedReg.MatchString(buf) {
-			buf = quotedReg.FindString(buf)
-		} else {
-			buf = ""
-		}
-	default:
-		buf = ""
-	}
-	return buf
-}
-
-// Tokenize 序列化token
-func Tokenize(sql string) []Token {
-	var token Token
-	var tokenLength int
-	var tokens []Token
-	tokenCache = make(map[string]Token)
-
-	// Used to make sure the string keeps shrinking on each iteration
-	oldStringLen := len(sql) + 1
-
-	currentLength := len(sql)
-	for currentLength > 0 {
-		// If the string stopped shrinking, there was a problem
-		if oldStringLen <= currentLength {
-			tokens = []Token{
-				{
-					Type: TokenTypeError,
-					Val:  sql,
-				},
-			}
-			return tokens
-		}
-
-		oldStringLen = currentLength
-		cacheKey := ""
-		// Determine if we can use caching
-		if currentLength >= maxCachekeySize {
-			cacheKey = sql[:maxCachekeySize]
-		}
-
-		// See if the token is already cached
-		if _, ok := tokenCache[cacheKey]; ok {
-			// Retrieve from cache
-			token = tokenCache[cacheKey]
-			tokenLength = len(token.Val)
-			cacheHits = cacheHits + 1
-		} else {
-			// Get the next token and the token type
-			token = getNextToken(sql, token)
-			tokenLength = len(token.Val)
-			cacheMisses = cacheMisses + 1
-			// If the token is shorter than the max length, store it in cache
-			if cacheKey != "" && tokenLength < maxCachekeySize {
-				tokenCache[cacheKey] = token
-			}
-		}
-
-		tokens = append(tokens, token)
-
-		// Advance the string
-		sql = sql[tokenLength:]
-		currentLength = currentLength - tokenLength
-	}
-	return tokens
-}
-
-// Compress compress sql
-// this method is inspired by eversql.com
-func Compress(sql string) string {
-	regLineTab := regexp.MustCompile(`(?i)([\n\t])`)
-	regSpace := regexp.MustCompile(`\s\s+`)
-	sql = regSpace.ReplaceAllString(regLineTab.ReplaceAllString(sql, " "), " ")
-	return sql
-}
-
-// SplitStatement SQL切分
-// return original sql, remove comment sql, left over buf
-func SplitStatement(buf []byte, delimiter []byte) (string, string, []byte) {
-	var singleLineComment bool
-	var multiLineComment bool
-	var quoted bool
-	var quoteRune byte
-	var sql string
-
-	for i := 0; i < len(buf); i++ {
-		b := buf[i]
-		// single line comment
-		if b == '-' {
-			if i+2 < len(buf) && buf[i+1] == '-' && buf[i+2] == ' ' {
-				singleLineComment = true
-				i = i + 2
-				continue
-			}
-			if i+2 < len(buf) && i == 0 && buf[i+1] == '-' && (buf[i+2] == '\n' || buf[i+2] == '\r') {
-				sql = "--\n"
-				break
-			}
-		}
-
-		if b == '#' {
-			if !multiLineComment && !quoted && !singleLineComment {
-				singleLineComment = true
-				continue
-			}
-		}
-
-		// new line end single line comment
-		if b == '\r' || b == '\n' {
-			if singleLineComment {
-				sql = string(buf[:i])
-				singleLineComment = false
-				if strings.HasPrefix(strings.TrimSpace(sql), "--") ||
-					strings.HasPrefix(strings.TrimSpace(sql), "#") {
-					// just comment, query start with '--', '#'
-					break
-				}
-				// comment in multi-line sql
-				continue
-			}
-			continue
-		}
-
-		// multi line comment
-		if b == '/' && i+1 < len(buf) && buf[i+1] == '*' {
-			if !multiLineComment && !singleLineComment && !quoted && buf[i+2] != '!' {
-				i = i + 2
-				multiLineComment = true
-				continue
-			}
-		}
-
-		if b == '*' && i+1 < len(buf) && buf[i+1] == '/' {
-			if multiLineComment && !quoted && !singleLineComment {
-				i = i + 2
-				multiLineComment = false
-				// '/*comment*/'
-				if i == len(buf) {
-					sql = string(buf[:i])
-				}
-				// '/*comment*/;', 'select 1/*comment*/;'
-				if string(buf[i:]) == string(delimiter) {
-					sql = string(buf)
-				}
-				continue
-			}
-		}
-
-		// quoted string
-		if b == '`' || b == '\'' || b == '"' {
-			if i > 1 && buf[i-1] != '\\' {
-				if quoted && b == quoteRune {
-					quoted = false
-				} else {
-					quoted = true
-					quoteRune = b
-				}
-			}
-		}
-
-		// delimiter
-		if !quoted && !singleLineComment && !multiLineComment {
-			eof := true
-			for k, c := range delimiter {
-				if len(buf) > i+k && buf[i+k] != c {
-					eof = false
-				}
-			}
-			if eof {
-				i = i + len(delimiter)
-				sql = string(buf[:i])
-				break
-			}
-		}
-
-		// ended of buf
-		if i == len(buf)-1 {
-			sql = string(buf)
-		}
-	}
-	orgSQL := string(buf[:len(sql)])
-	buf = buf[len(sql):]
-	return orgSQL, strings.TrimSuffix(sql, string(delimiter)), buf
-}
-
-// LeftNewLines cal left new lines in space
-func LeftNewLines(buf []byte) int {
-	newLines := 0
-	for _, b := range buf {
-		if !unicode.IsSpace(rune(b)) {
-			break
-		}
-		if b == byte('\n') {
-			newLines++
-		}
-	}
-	return newLines
-}
-
-// NewLines cal all new lines
-func NewLines(buf []byte) int {
-	newLines := 0
-	for _, b := range buf {
-		if b == byte('\n') {
-			newLines++
-		}
-	}
-	return newLines
-}
diff --git a/vendor/github.com/XiaoMi/soar/ast/token_test.go b/vendor/github.com/XiaoMi/soar/ast/token_test.go
deleted file mode 100644
index 97d6254..0000000
--- a/vendor/github.com/XiaoMi/soar/ast/token_test.go
+++ /dev/null
@@ -1,222 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package ast
-
-import (
-	"fmt"
-	"testing"
-
-	"github.com/XiaoMi/soar/common"
-
-	"github.com/kr/pretty"
-)
-
-func TestTokenize(t *testing.T) {
-	err := common.GoldenDiff(func() {
-		for _, sql := range common.TestSQLs {
-			fmt.Println(sql)
-			fmt.Println(Tokenize(sql))
-		}
-	}, t.Name(), update)
-	if nil != err {
-		t.Fatal(err)
-	}
-}
-
-func TestTokenizer(t *testing.T) {
-	sqls := []string{
-		"select c1,c2,c3 from t1,t2 join t3 on t1.c1=t2.c1 and t1.c3=t3.c1 where id>1000",
-		"select sourcetable, if(f.lastcontent = ?, f.lastupdate, f.lastcontent) as lastactivity, f.totalcount as activity, type.class as type, (f.nodeoptions & ?) as nounsubscribe from node as f inner join contenttype as type on type.contenttypeid = f.contenttypeid inner join subscribed as sd on sd.did = f.nodeid and sd.userid = ? union all select f.name as title, f.userid as keyval, ? as sourcetable, ifnull(f.lastpost, f.joindate) as lastactivity, f.posts as activity, ? as type, ? as nounsubscribe from user as f inner join userlist as ul on ul.relationid = f.userid and ul.userid = ? where ul.type = ? and ul.aq = ? order by title limit ?",
-		"select c1 from t1 where id>=1000", // test ">="
-		"select SQL_CALC_FOUND_ROWS col from tbl where id>1000",
-		"SELECT * FROM tb WHERE id=?;",
-		"SELECT * FROM tb WHERE id is null;",
-		"SELECT * FROM tb WHERE id is not null;",
-		"SELECT * FROM tb WHERE id between 1 and 3;",
-		"alter table inventory add index idx_store_film` (`store_id`,`film_id`);",
-	}
-	err := common.GoldenDiff(func() {
-		for _, sql := range sqls {
-			pretty.Println(Tokenizer(sql))
-		}
-	}, t.Name(), update)
-	if nil != err {
-		t.Fatal(err)
-	}
-}
-
-func TestGetQuotedString(t *testing.T) {
-	var str = []string{
-		`"hello world"`,
-		"`hello world`",
-		`'hello world'`,
-		"hello world",
-		`'hello \'world'`,
-		`"hello \"wor\"ld"`,
-		`"hello \"world"`,
-		`""`,
-		`''`,
-		"``",
-		`'hello 'world'`,
-		`"hello "world"`,
-	}
-	err := common.GoldenDiff(func() {
-		for _, s := range str {
-			fmt.Printf("orignal: %s\nquoted: %s\n", s, getQuotedString(s))
-		}
-	}, t.Name(), update)
-	if nil != err {
-		t.Fatal(err)
-	}
-}
-
-func TestCompress(t *testing.T) {
-	err := common.GoldenDiff(func() {
-		for _, sql := range common.TestSQLs {
-			fmt.Println(sql)
-			fmt.Println(Compress(sql))
-		}
-	}, t.Name(), update)
-	if nil != err {
-		t.Fatal(err)
-	}
-
-}
-
-func TestFormat(t *testing.T) {
-	err := common.GoldenDiff(func() {
-		for _, sql := range common.TestSQLs {
-			fmt.Println(sql)
-			fmt.Println(format(sql))
-		}
-	}, t.Name(), update)
-	if nil != err {
-		t.Fatal(err)
-	}
-}
-
-func TestSplitStatement(t *testing.T) {
-	bufs := [][]byte{
-		[]byte("select * from test;hello"),
-		[]byte("select 'asd;fas', col from test;hello"),
-		[]byte("-- select * from test;hello"),
-		[]byte("#select * from test;hello"),
-		[]byte("select * /*comment*/from test;hello"),
-		[]byte("select * /*comment;*/from test;hello"),
-		[]byte(`select * /*comment
-		;*/
-		from test;hello`),
-		[]byte(`select * from test`),
-		// https://github.com/XiaoMi/soar/issues/66
-		[]byte(`/*comment*/`),
-		[]byte(`/*comment*/;`),
-		[]byte(`--`),
-		[]byte(`-- comment`),
-		[]byte(`# comment`),
-		// https://github.com/XiaoMi/soar/issues/116
-		[]byte(`select
-*
--- comment
-from tb
-where col = 1`),
-		[]byte(`select
-* --
-from tb
-where col = 1`),
-		[]byte(`select
-* #
-from tb
-where col = 1`),
-		[]byte(`select
-*
---
-from tb
-where col = 1`),
-		[]byte(`select * from
--- comment
-tb;
-select col from tb where col = 1;`),
-		// https://github.com/XiaoMi/soar/issues/120
-		[]byte(`
--- comment
-select col from tb;
-select col from tb;
-`),
-	}
-	buf2s := [][]byte{
-		[]byte("select * from test\\Ghello"),
-		[]byte("select 'hello\\Gworld', col from test\\Ghello"),
-		[]byte("-- select * from test\\Ghello"),
-		[]byte("#select * from test\\Ghello"),
-		[]byte("select * /*comment*/from test\\Ghello"),
-		[]byte("select * /*comment;*/from test\\Ghello"),
-		[]byte(`select * /*comment
-        \\G*/
-        from test\\Ghello`),
-	}
-	err := common.GoldenDiff(func() {
-		for i, buf := range bufs {
-			sql, _, _ := SplitStatement(buf, []byte(common.Config.Delimiter))
-			fmt.Println(i, sql)
-		}
-		for i, buf := range buf2s {
-			sql, _, _ := SplitStatement(buf, []byte(common.Config.Delimiter))
-			fmt.Println(i, sql)
-		}
-	}, t.Name(), update)
-	if nil != err {
-		t.Fatal(err)
-	}
-}
-
-func TestLeftNewLines(t *testing.T) {
-	bufs := [][]byte{
-		[]byte(`
-		select * from test;hello`),
-		[]byte(`select * /*comment
-        ;*/
-        from test;hello`),
-		[]byte(`select * from test`),
-	}
-	err := common.GoldenDiff(func() {
-		for _, buf := range bufs {
-			fmt.Println(LeftNewLines(buf))
-		}
-	}, t.Name(), update)
-	if nil != err {
-		t.Fatal(err)
-	}
-}
-
-func TestNewLines(t *testing.T) {
-	bufs := [][]byte{
-		[]byte(`
-		select * from test;hello`),
-		[]byte(`select * /*comment
-        ;*/
-        from test;hello`),
-		[]byte(`select * from test`),
-	}
-	err := common.GoldenDiff(func() {
-		for _, buf := range bufs {
-			fmt.Println(NewLines(buf))
-		}
-	}, t.Name(), update)
-	if nil != err {
-		t.Fatal(err)
-	}
-}
diff --git a/vendor/github.com/XiaoMi/soar/cmd/soar/doc.go b/vendor/github.com/XiaoMi/soar/cmd/soar/doc.go
deleted file mode 100644
index 7f50ee8..0000000
--- a/vendor/github.com/XiaoMi/soar/cmd/soar/doc.go
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Package soar is the main program of SOAR
-package main
diff --git a/vendor/github.com/XiaoMi/soar/cmd/soar/soar.go b/vendor/github.com/XiaoMi/soar/cmd/soar/soar.go
deleted file mode 100644
index cb7172c..0000000
--- a/vendor/github.com/XiaoMi/soar/cmd/soar/soar.go
+++ /dev/null
@@ -1,429 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package main
-
-import (
-	"encoding/json"
-	"fmt"
-	"os"
-	"strings"
-
-	"github.com/XiaoMi/soar/advisor"
-	"github.com/XiaoMi/soar/ast"
-	"github.com/XiaoMi/soar/common"
-	"github.com/XiaoMi/soar/database"
-	"github.com/XiaoMi/soar/env"
-
-	"github.com/kr/pretty"
-	"github.com/percona/go-mysql/query"
-	"github.com/ziutek/mymysql/mysql"
-	"vitess.io/vitess/go/vt/sqlparser"
-)
-
-func main() {
-	// 全局变量
-	var err error
-	var sql string                                            // 单条评审指定的 sql 或 explain
-	sqlCounter := 1                                           // SQL 计数器
-	lineCounter := 1                                          // 行计数器
-	var alterSQLs []string                                    // 待评审的 SQL 中所有 ALTER 请求
-	alterTableTimes := make(map[string]int)                   // 待评审的 SQL 中同一经表 ALTER 请求计数器
-	suggestMerged := make(map[string]map[string]advisor.Rule) // 优化建议去重, key 为 sql 的 fingerprint.ID
-
-	// 配置文件&命令行参数解析
-	initConfig()
-
-	// 命令行帮助工具,如 -list-report-types, -check-config等。
-	if isContinue, exitCode := helpTools(); !isContinue {
-		os.Exit(exitCode)
-	}
-
-	// 环境初始化,连接检查线上环境+构建测试环境
-	vEnv, rEnv := env.BuildEnv()
-
-	// 使用 -cleanup-test-database 命令手动清理残余的 optimizer_xxx 数据库
-	if common.Config.CleanupTestDatabase {
-		vEnv.CleanupTestDatabase()
-		return
-	}
-
-	// 如果使用到测试环境,在这里环境清理
-	if common.Config.DropTestTemporary {
-		defer vEnv.CleanUp()
-	}
-
-	// 当程序卡死的时候,或者由于某些原因程序没有退出,可以通过捕获信号量的形式让程序优雅退出并且清理测试环境
-	common.HandleSignal(func() {
-		shutdown(vEnv)
-	})
-
-	// 对指定的库表进行索引重复检查
-	if common.Config.ReportType == "duplicate-key-checker" {
-		dupKeySuggest := advisor.DuplicateKeyChecker(rEnv)
-		_, str := advisor.FormatSuggest("", common.Config.ReportType, dupKeySuggest)
-		if str == "" {
-			fmt.Printf("%s/%s 未发现重复索引\n", common.Config.OnlineDSN.Addr, common.Config.OnlineDSN.Schema)
-		} else {
-			fmt.Println(str)
-		}
-		return
-	}
-
-	// 读入待优化 SQL ,当配置文件或命令行参数未指定 SQL 时从管道读取
-	buf := initQuery(common.Config.Query)
-	lineCounter += ast.LeftNewLines([]byte(buf))
-	buf = strings.TrimSpace(buf)
-
-	// remove bom from file header
-	var bom []byte
-	buf, bom = common.RemoveBOM([]byte(buf))
-
-	if isContinue, exitCode := reportTool(buf, bom); !isContinue {
-		os.Exit(exitCode)
-	}
-
-	// 逐条SQL给出优化建议
-	for ; ; sqlCounter++ {
-		var id string                                     // fingerprint.ID
-		heuristicSuggest := make(map[string]advisor.Rule) // 启发式建议
-		expSuggest := make(map[string]advisor.Rule)       // EXPLAIN 解读
-		idxSuggest := make(map[string]advisor.Rule)       // 索引建议
-		proSuggest := make(map[string]advisor.Rule)       // Profiling 信息
-		traceSuggest := make(map[string]advisor.Rule)     // Trace 信息
-		mysqlSuggest := make(map[string]advisor.Rule)     // MySQL 返回的 ERROR 信息
-
-		if buf == "" {
-			common.Log.Debug("buf: %s, sql: %s empty", buf, sql)
-			break
-		}
-		// 查询请求切分
-		orgSQL, sql, bufBytes := ast.SplitStatement([]byte(buf), []byte(common.Config.Delimiter))
-		// lineCounter
-		lc := ast.NewLines([]byte(orgSQL))
-		// leftLineCounter
-		llc := ast.LeftNewLines([]byte(orgSQL))
-		lineCounter += llc
-		buf = string(bufBytes)
-
-		// 去除无用的备注和空格
-		sql = database.RemoveSQLComments(sql)
-		if sql == "" {
-			common.Log.Debug("empty query or comment, buf: %s", buf)
-			continue
-		}
-		common.Log.Debug("main loop SQL: %s", sql)
-
-		// +++++++++++++++++++++小工具集[开始]+++++++++++++++++++++++{
-		fingerprint := strings.TrimSpace(query.Fingerprint(sql))
-		switch common.Config.ReportType {
-		case "fingerprint":
-			// SQL 指纹
-			fmt.Println(fingerprint)
-			continue
-		case "pretty":
-			// SQL 美化
-			fmt.Println(ast.Pretty(sql, "builtin") + common.Config.Delimiter)
-			continue
-		case "compress":
-			// SQL 压缩
-			fmt.Println(ast.Compress(sql) + common.Config.Delimiter)
-			continue
-		case "ast":
-			// SQL 抽象语法树
-			var tree sqlparser.Statement
-			tree, err = sqlparser.Parse(sql)
-			if err != nil {
-				fmt.Println(err)
-			} else {
-				_, err = pretty.Println(tree)
-				common.LogIfWarn(err, "")
-			}
-			continue
-		case "tiast":
-			// TiDB SQL 抽象语法树
-			ast.PrintPrettyStmtNode(sql, "", "")
-			continue
-		case "tokenize":
-			// SQL 切词
-			_, err = pretty.Println(ast.Tokenize(sql))
-			common.LogIfWarn(err, "")
-			continue
-		default:
-			// SQL 签名
-			id = query.Id(fingerprint)
-			// 建议去重,减少评审整个文件耗时
-			// TODO: 由于 a = 11 和 a = '11' 的 fingerprint 相同,这里一旦跳过即无法检查有些建议了,如: ARG.003
-			if _, ok := suggestMerged[id]; ok {
-				continue
-			}
-			// 黑名单中的SQL不给建议
-			if advisor.InBlackList(fingerprint) {
-				continue
-			}
-		}
-		// +++++++++++++++++++++小工具集[结束]+++++++++++++++++++++++}
-
-		// +++++++++++++++++++++语法检查[开始]+++++++++++++++++++++++{
-		q, syntaxErr := advisor.NewQuery4Audit(sql)
-		stmt := q.Stmt
-
-		// 如果语法检查出错则不需要给优化建议
-		if syntaxErr != nil {
-			errContent := fmt.Sprintf("At SQL %d : %v", sqlCounter, syntaxErr)
-			common.Log.Warning(errContent)
-			if common.Config.OnlySyntaxCheck {
-				fmt.Println(errContent)
-			}
-			if !common.Config.DryRun {
-				os.Exit(1)
-			}
-			// tidb parser 语法检查给出的建议 ERR.000
-			if common.Config.TestDSN.Disable {
-				mysqlSuggest["ERR.000"] = advisor.RuleMySQLError("ERR.000", syntaxErr)
-			}
-		}
-		// 如果只想检查语法直接跳过后面的步骤
-		if common.Config.OnlySyntaxCheck {
-			continue
-		}
-
-		// +++++++++++++++++++++语法检查[结束]+++++++++++++++++++++++}
-
-		// +++++++++++++++++++++启发式规则建议[开始]+++++++++++++++++++++++{
-		common.Log.Debug("start of heuristic advisor Query: %s", q.Query)
-		for item, rule := range advisor.HeuristicRules {
-			// 去除忽略的建议检查
-			okFunc := (*advisor.Query4Audit).RuleOK
-			if !advisor.IsIgnoreRule(item) && &rule.Func != &okFunc {
-				r := rule.Func(q)
-				if r.Item == item {
-					heuristicSuggest[item] = r
-				}
-			}
-		}
-		common.Log.Debug("end of heuristic advisor Query: %s", q.Query)
-		// +++++++++++++++++++++启发式规则建议[结束]+++++++++++++++++++++++}
-
-		// +++++++++++++++++++++索引优化建议[开始]+++++++++++++++++++++++{
-		// 如果配置了索引建议过滤规则,不进行索引优化建议
-		// 在配置文件 ignore-rules 中添加 'IDX.*' 即可屏蔽索引优化建议
-		common.Log.Debug("start of index advisor Query: %s", q.Query)
-		if !advisor.IsIgnoreRule("IDX.") {
-			if vEnv.BuildVirtualEnv(rEnv, q.Query) {
-				idxAdvisor, err := advisor.NewAdvisor(vEnv, *rEnv, *q)
-				if err != nil || (idxAdvisor == nil && vEnv.Error == nil) {
-					if idxAdvisor == nil {
-						// 如果 SQL 是 DDL 语句,则返回的 idxAdvisor 为 nil,可以忽略不处理
-						// TODO alter table add index 语句检查索引是否已经存在
-						common.Log.Debug("idxAdvisor by pass Query: %s", q.Query)
-					} else {
-						common.Log.Warning("advisor.NewAdvisor Error: %v", err)
-					}
-				} else {
-					// 创建环境时没有出现错误,生成索引建议
-					if vEnv.Error == nil {
-						idxSuggest = idxAdvisor.IndexAdvise().Format()
-
-						// 依赖数据字典的启发式建议
-						for i, r := range idxAdvisor.HeuristicCheck(*q) {
-							heuristicSuggest[i] = r
-						}
-					} else {
-						// 根据错误号输出建议
-						switch vEnv.Error.(*mysql.Error).Code {
-						case 1061:
-							idxSuggest["IDX.001"] = advisor.Rule{
-								Item:     "IDX.001",
-								Severity: "L2",
-								Summary:  "索引名称已存在",
-								Content:  strings.Trim(strings.Split(vEnv.Error.Error(), ":")[1], " "),
-								Case:     sql,
-							}
-						default:
-							// vEnv.VEnvBuild 阶段给出的 ERROR 是 ERR.001
-							mysqlSuggest["ERR.001"] = advisor.RuleMySQLError("ERR.001", vEnv.Error)
-							common.Log.Error("BuildVirtualEnv DDL Execute Error : %v", vEnv.Error)
-						}
-					}
-				}
-			} else {
-				common.Log.Error("vEnv.BuildVirtualEnv Error: prepare SQL '%s' in vEnv failed.", q.Query)
-			}
-		}
-		common.Log.Debug("end of index advisor Query: %s", q.Query)
-		// +++++++++++++++++++++索引优化建议[结束]+++++++++++++++++++++++}
-
-		// +++++++++++++++++++++EXPLAIN 建议[开始]+++++++++++++++++++++++{
-		// 如果未配置 Online 或 Test 无法给 Explain 建议
-		common.Log.Debug("start of explain Query: %s", q.Query)
-		if !common.Config.OnlineDSN.Disable && !common.Config.TestDSN.Disable {
-			// 因为 EXPLAIN 依赖数据库环境,所以把这段逻辑放在启发式建议和索引建议后面
-			if common.Config.Explain {
-				// 执行 EXPLAIN
-				explainInfo, err := rEnv.Explain(q.Query,
-					database.ExplainType[common.Config.ExplainType],
-					database.ExplainFormatType[common.Config.ExplainFormat])
-				if err != nil && strings.HasPrefix(vEnv.Database, "optimizer_") {
-					// 线上环境执行失败才到测试环境 EXPLAIN,比如在用户提供建表语句及查询语句的场景
-					common.Log.Warn("rEnv.Explain Warn: %v", err)
-					explainInfo, err = vEnv.Explain(q.Query,
-						database.ExplainType[common.Config.ExplainType],
-						database.ExplainFormatType[common.Config.ExplainFormat])
-					if err != nil {
-						// EXPLAIN 阶段给出的 ERROR 是 ERR.002
-						mysqlSuggest["ERR.002"] = advisor.RuleMySQLError("ERR.002", err)
-						common.Log.Error("vEnv.Explain Error: %v", err)
-						continue
-					}
-				}
-				// 分析 EXPLAIN 结果
-				if explainInfo != nil {
-					expSuggest = advisor.ExplainAdvisor(explainInfo)
-				} else {
-					common.Log.Warn("rEnv&vEnv.Explain explainInfo nil, SQL: %s", q.Query)
-				}
-			}
-		}
-		common.Log.Debug("end of explain Query: %s", q.Query)
-		// +++++++++++++++++++++ EXPLAIN 建议[结束]+++++++++++++++++++++++}
-
-		// +++++++++++++++++++++ Profiling [开始]+++++++++++++++++++++++++{
-		common.Log.Debug("start of profiling Query: %s", q.Query)
-		if common.Config.Profiling {
-			res, err := vEnv.Profiling(q.Query)
-			if err == nil {
-				proSuggest["PRO.001"] = advisor.Rule{
-					Item:     "PRO.001",
-					Severity: "L0",
-					Content:  database.FormatProfiling(res),
-				}
-			} else {
-				common.Log.Error("Profiling Error: %v", err)
-			}
-		}
-		common.Log.Debug("end of profiling Query: %s", q.Query)
-		// +++++++++++++++++++++ Profiling [结束]++++++++++++++++++++++++++}
-
-		// +++++++++++++++++++++ Trace [开始]+++++++++++++++++++++++++{
-		common.Log.Debug("start of trace Query: %s", q.Query)
-		if common.Config.Trace {
-			res, err := vEnv.Trace(q.Query)
-			if err == nil {
-				traceSuggest["TRA.001"] = advisor.Rule{
-					Item:     "TRA.001",
-					Severity: "L0",
-					Content:  database.FormatTrace(res),
-				}
-			} else {
-				common.Log.Error("Trace Error: %v", err)
-			}
-		}
-		common.Log.Debug("end of trace Query: %s", q.Query)
-		// +++++++++++++++++++++Trace [结束]++++++++++++++++++++++++++}
-
-		// +++++++++++++++++++++SQL 重写[开始]+++++++++++++++++++++++++{
-		common.Log.Debug("start of rewrite Query: %s", q.Query)
-		if common.Config.ReportType == "rewrite" {
-			if strings.HasPrefix(strings.TrimSpace(strings.ToLower(sql)), "create") ||
-				strings.HasPrefix(strings.TrimSpace(strings.ToLower(sql)), "alter") ||
-				strings.HasPrefix(strings.TrimSpace(strings.ToLower(sql)), "rename") {
-				// 依赖上下文件的 SQL 重写,如:多条 ALTER SQL 合并
-				// vitess 对 DDL 语法的支持不好,大部分 DDL 会语法解析出错,但即使出错了还是会生成一个 stmt 而且里面的 db.table 还是准确的。
-
-				alterSQLs = append(alterSQLs, sql)
-				alterTbl := ast.AlterAffectTable(stmt)
-				if alterTbl != "" && alterTbl != "dual" {
-					if _, ok := alterTableTimes[alterTbl]; ok {
-						heuristicSuggest["ALT.002"] = advisor.HeuristicRules["ALT.002"]
-						alterTableTimes[alterTbl] = alterTableTimes[alterTbl] + 1
-					} else {
-						alterTableTimes[alterTbl] = 1
-					}
-				}
-			} else {
-				// 其他不依赖上下文件的 SQL 重写
-				rw := ast.NewRewrite(sql)
-				if rw == nil {
-					// 都到这一步了 sql 不会语法不正确,因此 rw 一般不会为 nil
-					common.Log.Critical("NewRewrite nil point error, SQL: %s", sql)
-					os.Exit(1)
-				}
-				// SQL 转写需要的源信息采集,如果没有配置环境则只做有限改写
-				meta := ast.GetMeta(rw.Stmt, nil)
-				rw.Columns = vEnv.GenTableColumns(meta)
-				// 执行定义好的 SQL 重写规则
-				rw.Rewrite()
-				fmt.Println(strings.TrimSpace(rw.NewSQL))
-			}
-		}
-		common.Log.Debug("end of rewrite Query: %s", q.Query)
-		// +++++++++++++++++++++ SQL 重写[结束]++++++++++++++++++++++++++}
-
-		// +++++++++++++++++++++打印单条 SQL 优化建议[开始]++++++++++++++++++++++++++{
-		common.Log.Debug("start of print suggestions, Query: %s", q.Query)
-		sug, str := advisor.FormatSuggest(q.Query, common.Config.ReportType, heuristicSuggest, idxSuggest, expSuggest, proSuggest, traceSuggest, mysqlSuggest)
-		suggestMerged[id] = sug
-		switch common.Config.ReportType {
-		case "json":
-		case "duplicate-key-checker":
-		case "rewrite":
-		case "lint":
-			for _, s := range strings.Split(str, "\n") {
-				// ignore empty output
-				if strings.TrimSpace(s) == "" {
-					continue
-				}
-
-				if common.Config.Query != "" {
-					if _, err = os.Stat(common.Config.Query); err == nil {
-						fmt.Printf("%s:%d:%s\n", common.Config.Query, lineCounter, s)
-					} else {
-						fmt.Printf("null:%d:%s\n", lineCounter, s)
-					}
-				} else {
-					fmt.Printf("stdin:%d:%s\n", lineCounter, s)
-				}
-			}
-			lineCounter += lc - llc
-		case "html":
-			fmt.Println(common.Markdown2HTML(str))
-		default:
-			fmt.Println(str)
-		}
-		common.Log.Debug("end of print suggestions, Query: %s", q.Query)
-		// +++++++++++++++++++++打印单条 SQL 优化建议[结束]++++++++++++++++++++++++++}
-	}
-
-	// 同一张表的多条 ALTER 语句合并为一条
-	if ast.RewriteRuleMatch("mergealter") {
-		for _, v := range ast.MergeAlterTables(alterSQLs...) {
-			fmt.Println(strings.TrimSpace(v))
-		}
-		return
-	}
-
-	// 以 JSON 格式化输出
-	if common.Config.ReportType == "json" {
-		js, err := json.MarshalIndent(suggestMerged, "", "  ")
-		if err == nil {
-			fmt.Println(string(js))
-		} else {
-			common.Log.Error("FormatSuggest json.Marshal Error: %v", err)
-		}
-		return
-	}
-}
diff --git a/vendor/github.com/XiaoMi/soar/cmd/soar/soar_test.go b/vendor/github.com/XiaoMi/soar/cmd/soar/soar_test.go
deleted file mode 100644
index 82be382..0000000
--- a/vendor/github.com/XiaoMi/soar/cmd/soar/soar_test.go
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package main
-
-import (
-	"testing"
-
-	"github.com/XiaoMi/soar/common"
-)
-
-func init() {
-	common.Config.OnlineDSN.Schema = "sakila"
-}
-
-func Test_Main(_ *testing.T) {
-	common.Config.OnlineDSN.Disable = true
-	common.Config.LogLevel = 0
-	common.Config.Query = "select * from film;alter table city add index idx_country_id(country_id);"
-	main()
-}
-
-func Test_Main_More(_ *testing.T) {
-	common.Config.LogLevel = 0
-	common.Config.Profiling = true
-	common.Config.Explain = true
-	common.Config.Query = "select * from film where country_id = 1;use sakila;alter table city add index idx_country_id(country_id);"
-	for _, typ := range []string{
-		"json", "html", "markdown", "fingerprint", "compress", "pretty", "rewrite",
-	} {
-		common.Config.ReportType = typ
-		main()
-	}
-}
diff --git a/vendor/github.com/XiaoMi/soar/cmd/soar/tool.go b/vendor/github.com/XiaoMi/soar/cmd/soar/tool.go
deleted file mode 100644
index dd977d6..0000000
--- a/vendor/github.com/XiaoMi/soar/cmd/soar/tool.go
+++ /dev/null
@@ -1,237 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package main
-
-import (
-	"fmt"
-	"io/ioutil"
-	"os"
-	"path/filepath"
-	"strings"
-
-	"github.com/XiaoMi/soar/advisor"
-	"github.com/XiaoMi/soar/ast"
-	"github.com/XiaoMi/soar/common"
-	"github.com/XiaoMi/soar/database"
-	"github.com/XiaoMi/soar/env"
-)
-
-// initConfig load config from default->file->cmdFlag
-func initConfig() {
-	// 更新 binary 文件所在路径为 BaseDir
-	ex, err := os.Executable()
-	if err != nil {
-		panic(err)
-	}
-	common.BaseDir = filepath.Dir(ex)
-
-	for i, c := range os.Args {
-		// 如果指定了 -config, 它必须是第一个参数
-		if strings.HasPrefix(c, "-config") && i != 1 {
-			fmt.Println("-config must be the first arg")
-			os.Exit(1)
-		}
-		// 等号两边请不要加空格
-		if c == "=" {
-			// -config = soar.yaml not support
-			fmt.Println("wrong format, no space between '=', eg: -config=soar.yaml")
-			os.Exit(1)
-		}
-	}
-
-	// 加载配置文件,处理命令行参数
-	err = common.ParseConfig(common.ArgConfig())
-	// 检查配置文件及命令行参数是否正确
-	if common.CheckConfig && err != nil {
-		fmt.Println(err.Error())
-		os.Exit(1)
-	}
-	common.LogIfWarn(err, "")
-}
-
-// checkConfig for `-check-config` flag
-// if error found return non-zero, no error return zero
-func checkConfig() int {
-	// TestDSN connection check
-	testConn := &database.Connector{
-		Addr:     common.Config.TestDSN.Addr,
-		User:     common.Config.TestDSN.User,
-		Pass:     common.Config.TestDSN.Password,
-		Database: common.Config.TestDSN.Schema,
-		Charset:  common.Config.TestDSN.Charset,
-	}
-	testVersion, err := testConn.Version()
-	if err != nil && !common.Config.TestDSN.Disable {
-		fmt.Println("test-dsn:", testConn, err.Error())
-		return 1
-	}
-	if common.Config.Verbose {
-		if err == nil {
-			fmt.Println("test-dsn", testConn, "Version:", testVersion)
-		} else {
-			fmt.Println("test-dsn", common.Config.TestDSN)
-		}
-	}
-
-	if !testConn.HasAllPrivilege() {
-		fmt.Printf("test-dsn: %s, need all privileges", common.FormatDSN(common.Config.TestDSN))
-		return 1
-	}
-	// OnlineDSN connection check
-	onlineConn := &database.Connector{
-		Addr:     common.Config.OnlineDSN.Addr,
-		User:     common.Config.OnlineDSN.User,
-		Pass:     common.Config.OnlineDSN.Password,
-		Database: common.Config.OnlineDSN.Schema,
-		Charset:  common.Config.OnlineDSN.Charset,
-	}
-	onlineVersion, err := onlineConn.Version()
-	if err != nil && !common.Config.OnlineDSN.Disable {
-		fmt.Println("online-dsn:", onlineConn, err.Error())
-		return 1
-	}
-	if common.Config.Verbose {
-		if err == nil {
-			fmt.Println("online-dsn", onlineConn, "Version:", onlineVersion)
-		} else {
-			fmt.Println("online-dsn", common.Config.OnlineDSN)
-		}
-	}
-
-	if !onlineConn.HasSelectPrivilege() {
-		fmt.Printf("online-dsn: %s, need all privileges", common.FormatDSN(common.Config.OnlineDSN))
-		return 1
-	}
-	return 0
-}
-
-// helpTools help tools in cmd flags
-func helpTools() (isContinue bool, exitCode int) {
-	// environment error check, eg. MySQL password error
-	if common.CheckConfig {
-		return false, checkConfig()
-	}
-	// 打印 SOAR 版本信息
-	if common.PrintVersion {
-		common.SoarVersion()
-		return false, 0
-	}
-	// 打印已加载配置的各配置项,检查配置是否生效
-	if common.PrintConfig {
-		common.PrintConfiguration()
-		return false, 0
-	}
-	// 打印支持启发式建议
-	if common.Config.ListHeuristicRules {
-		advisor.ListHeuristicRules(advisor.HeuristicRules)
-		return false, 0
-	}
-	// 打印支持的 SQL 重写规则
-	if common.Config.ListRewriteRules {
-		ast.ListRewriteRules(ast.RewriteRules)
-		return false, 0
-	}
-	// 打印所有的测试 SQL
-	if common.Config.ListTestSqls {
-		advisor.ListTestSQLs()
-		return false, 0
-	}
-	// 打印支持的 report-type
-	if common.Config.ListReportTypes {
-		common.ListReportTypes()
-		return false, 0
-	}
-
-	return true, 0
-}
-
-// reportTool tools in report type
-func reportTool(sql string, bom []byte) (isContinue bool, exitCode int) {
-	switch common.Config.ReportType {
-	case "html":
-		// HTML 格式输入 CSS 加载
-		fmt.Println(common.MarkdownHTMLHeader())
-		return true, 0
-	case "md2html":
-		// markdown2html 转换小工具
-		fmt.Println(common.MarkdownHTMLHeader())
-		fmt.Println(common.Markdown2HTML(sql))
-		return false, 0
-	case "explain-digest":
-		// 当用户输入为 EXPLAIN 信息,只对 Explain 信息进行分析
-		// 注意: 这里只能处理一条 SQL 的 EXPLAIN 信息,用户一次反馈多条 SQL 的 EXPLAIN 信息无法处理
-		advisor.DigestExplainText(sql)
-		return false, 0
-	case "chardet":
-		// Get charset of input
-		charset := common.CheckCharsetByBOM(bom)
-		if charset == "" {
-			charset = common.Chardet([]byte(sql))
-		}
-		fmt.Println(charset)
-		return false, 0
-	case "remove-comment":
-		fmt.Println(database.RemoveSQLComments(sql))
-		return false, 0
-	default:
-		return true, 0
-	}
-}
-
-// initQuery
-func initQuery(query string) string {
-	// 读入待优化 SQL ,当配置文件或命令行参数未指定 SQL 时从管道读取
-	if query == "" {
-		// check stdin is pipe or terminal
-		// https://stackoverflow.com/questions/22744443/check-if-there-is-something-to-read-on-stdin-in-golang
-		stat, err := os.Stdin.Stat()
-		if stat == nil {
-			common.Log.Critical("os.Stdin.Stat Error: %v", err)
-			os.Exit(1)
-		}
-		if (stat.Mode() & os.ModeCharDevice) != 0 {
-			// stdin is from a terminal
-			fmt.Println("Args format error, use --help see how to use it!")
-			os.Exit(1)
-		}
-		// read from pipe
-		var data []byte
-		data, err = ioutil.ReadAll(os.Stdin)
-		if err != nil {
-			common.Log.Critical("ioutil.ReadAll Error: %v", err)
-		}
-		return string(data)
-	}
-
-	if _, err := os.Stat(query); err == nil {
-		var data []byte
-		data, err = ioutil.ReadFile(query)
-		if err != nil {
-			common.Log.Critical("ioutil.ReadFile Error: %v", err)
-		}
-		return string(data)
-	}
-
-	return query
-}
-
-func shutdown(vEnv *env.VirtualEnv) {
-	if common.Config.DropTestTemporary {
-		vEnv.CleanUp()
-	}
-	os.Exit(0)
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/cases.go b/vendor/github.com/XiaoMi/soar/common/cases.go
deleted file mode 100644
index 25763f7..0000000
--- a/vendor/github.com/XiaoMi/soar/common/cases.go
+++ /dev/null
@@ -1,203 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-// TestSQLs 测试SQL大集合
-var TestSQLs []string
-
-func init() {
-	// 所有的SQL都要以分号结尾,-list-test-sqls 参数会打印这个 list,以分号结尾可方便测试
-	// 如:./soar -list-test-sql | ./soar
-	TestSQLs = []string{
-		// single equality
-		"SELECT * FROM film WHERE length = 86;",    // index(length)
-		"SELECT * FROM film WHERE length IS NULL;", // index(length)
-		"SELECT * FROM film HAVING title = 'abc';", // 无法使用索引
-
-		// single inequality
-		"SELECT * FROM sakila.film WHERE length >= 60;",   // any of <, <=, >=, >; but not <>, !=, IS NOT NULL"
-		"SELECT * FROM sakila.film WHERE length >= '60';", // Implicit Conversion
-		"SELECT * FROM film WHERE length BETWEEN 60 AND 84;",
-		"SELECT * FROM film WHERE title LIKE 'AIR%';", // but not LIKE '%blah'",
-		"SELECT * FROM film WHERE title IS NOT NULL;",
-
-		// multiple equalities
-		"SELECT * FROM film WHERE length = 114 and title = 'ALABAMA DEVIL';", // index(title,length) or index(length,title)",
-
-		// equality and inequality
-		"SELECT * FROM film WHERE length > 100 and title = 'ALABAMA DEVIL';", // index(title, length)",
-
-		// multiple inequality
-		"SELECT * FROM film WHERE length > 100 and language_id < 10 and title = 'xyz';", // index(d, b) or index(d, c) 依赖数据",
-		"SELECT * FROM film WHERE length > 100 and language_id < 10;",                   // index(b) or index(c)",
-
-		// GROUP BY
-		"SELECT release_year, sum(length) FROM film WHERE length = 123 AND language_id = 1 GROUP BY release_year;",  // INDEX(length, language_id, release_year) or INDEX(language_id, length, release_year)",
-		"SELECT release_year, sum(length) FROM film WHERE length >= 123 GROUP BY release_year;",                     // INDEX(length)",
-		"SELECT release_year, language_id, sum(length) FROM film GROUP BY release_year, language_id;",               // INDEX(release_year, language_id) (no WHERE)",
-		"SELECT release_year, sum(length) FROM film WHERE length = 123 GROUP BY release_year,(length+language_id);", // INDEX(length) expression in GROUP BY, so no use including even release_year.",
-		"SELECT release_year, sum(film_id) FROM film GROUP BY release_year;",                                        // INDEX(`release_year`)
-		"SELECT * FROM address GROUP BY address,district;",                                                          // INDEX(address, district)
-		"SELECT title FROM film WHERE ABS(language_id) = 3 GROUP BY title;",                                         // 无法使用索引
-
-		// ORDER BY
-		"SELECT language_id FROM film WHERE length = 123 GROUP BY release_year ORDER BY language_id;",            //  INDEX(length, release_year) should have stopped with Step 2b",
-		"SELECT release_year FROM film WHERE length = 123 GROUP BY release_year ORDER BY release_year;",          //  INDEX(length, release_year) the release_year will be used for both GROUP BY and ORDER BY",
-		"SELECT * FROM film WHERE length = 123 ORDER BY release_year ASC, language_id DESC;",                     //  INDEX(length) mixture of ASC and DESC.",
-		"SELECT release_year FROM film WHERE length = 123 GROUP BY release_year ORDER BY release_year LIMIT 10;", //  INDEX(length, release_year)",
-		"SELECT * FROM film WHERE length = 123 ORDER BY release_year LIMIT 10;",                                  //  INDEX(length, release_year)",
-		"SELECT * FROM film ORDER BY release_year LIMIT 10;",                                                     //  不能单独给release_year加索引
-		"SELECT film_id FROM film ORDER BY release_year LIMIT 10;",                                               //  TODO: INDEX(release_year),film_id 是主键查询列满足索引覆盖的情况才会使用到 release_year 索引
-		"SELECT * FROM film WHERE length > 100 ORDER BY length LIMIT 10;",                                        //  INDEX(length) This "range" is compatible with ORDER BY
-		"SELECT * FROM film WHERE length < 100 ORDER BY length LIMIT 10;",                                        //  INDEX(length) also works
-		"SELECT * FROM customer WHERE address_id in (224,510) ORDER BY last_name;",                               //  INDEX(address_id)
-		"SELECT * FROM film WHERE release_year = 2016 AND length != 1 ORDER BY title;",                           //  INDEX(`release_year`, `length`, `title`)
-
-		// "Covering" IdxRows
-		"SELECT title FROM film WHERE release_year = 1995;",                               //  INDEX(release_year, title)",
-		"SELECT title, replacement_cost FROM film WHERE language_id = 5 AND length = 70;", //  INDEX(language_id, length, title, replacement_cos film ), title, replacement_cost顺序无关,language_id, length顺序视散粒度情况.
-		"SELECT title FROM film WHERE language_id > 5 AND length > 70;",                   //  INDEX(language_id, length, title) language_id or length first (that's as far as the Algorithm goes), then the other two fields afterwards.
-
-		// equalities and sort
-		"SELECT * FROM film WHERE length = 100 and title = 'xyz' ORDER BY release_year;", // 依赖数据特征,index(length, title, release_year) or index(title, length, release_year)需要评估
-
-		// inequality and sort
-		"SELECT * FROM film WHERE length > 100 and title = 'xyz' ORDER BY release_year;", // 依赖数据特征, index(title, release_year),index(title, length)需要评估
-		"SELECT * FROM film WHERE length > 100 ORDER BY release_year;",                   // 依赖数据特征, index(length),index(release_year)需要评估
-
-		// Join
-		// 内连接 INNER JOIN
-		// 在mysql中,inner join...on , join...on , 逗号...WHERE ,cross join...on是一样的含义。
-		// 但是在标准SQL中,它们并不等价,标准 SQL 中 INNER JOIN 与 ON共同使用, CROSS JOIN用于其他情况。
-		// 逗号不支持 on 和 using 语法, 逗号的优先级要低于INNER JOIN, CROSS JOIN, LEFT JOIN
-		// ON子句的语法格式为:tb1.col1 = tb2.col2列名可以不同,筛选连接后的结果,两表的对应列值相同才在结果集中。
-		// 当模式设计对联接表的列采用了相同的命名样式时,就可以使用 USING 语法来简化 ON 语法
-
-		// join, inner join, cross join等价,优先选择小结果集条件表为驱动表
-		// left [outer] join左表为驱动表
-		// right [outer] join右表为驱动表
-		// 驱动表连接列如果没其他条件可以不考虑加索引,反正是需要foreach
-		// 被驱动表连接列需要加索引。即:left [outer] join的右表连接列需要加索引,right [outer] join的左表连接列需要加索引,inner join结果集较大表的连接列需要加索引
-		// 其他索引添加算法与单表索引优化算法相同
-		// 总结:被驱动表列需要添加索引
-		// 建议:将无索引的表通常作为驱动表
-
-		"SELECT * FROM city a INNER JOIN country b ON a.country_id=b.country_id;",
-
-		// 左外连接 LEFT [OUTER] JOIN
-		"SELECT * FROM city a LEFT JOIN country b ON a.country_id=b.country_id;",
-
-		// 右外连接 RIGHT [OUTER] JOIN
-		"SELECT * FROM city a RIGHT JOIN country b ON a.country_id=b.country_id;",
-
-		// 左连接
-		"SELECT * FROM city a LEFT JOIN country b ON a.country_id=b.country_id WHERE b.last_update IS NULL;",
-
-		// 右连接
-		"SELECT * FROM city a RIGHT JOIN country b ON a.country_id=b.country_id WHERE a.last_update IS NULL;",
-
-		// 全连接 FULL JOIN 因为在mysql中并不支持,所以我们用union实现
-		"SELECT * FROM city a LEFT JOIN country b ON a.country_id=b.country_id " +
-			"UNION " +
-			"SELECT * FROM city a RIGHT JOIN country b ON a.country_id=b.country_id;",
-
-		// 两张表中不共同满足的数据集
-		"SELECT * FROM city a RIGHT JOIN country b ON a.country_id=b.country_id WHERE a.last_update IS NULL " +
-			"UNION " +
-			"SELECT * FROM city a LEFT JOIN country b ON a.country_id=b.country_id WHERE b.last_update IS NULL;",
-
-		// NATURAL JOIN 默认是同名字段完全匹配的INNER JOIN
-		"SELECT country_id, last_update FROM city NATURAL JOIN country;",
-
-		// NATURAL LEFT JOIN
-		"SELECT country_id, last_update FROM city NATURAL LEFT JOIN country;",
-
-		// NATURAL RIGHT JOIN
-		"SELECT country_id, last_update FROM city NATURAL RIGHT JOIN country;",
-
-		// STRAIGHT_JOIN 实际上与内连接 INNER JOIN 表现完全一致,
-		// 不同的是使用了 STRAIGHT_JOIN 后指定表载入的顺序,city 先于 country 载入
-		"SELECT a.country_id, a.last_update FROM city a STRAIGHT_JOIN country b ON a.country_id=b.country_id;",
-
-		// SEMI JOIN
-		// 半连接: 当一张表在另一张表找到匹配的记录之后,半连接(semi-join)返回第一张表中的记录。
-		// 与条件连接相反,即使在右节点中找到几条匹配的记录,左节点的表也只会返回一条记录。
-		// 另外,右节点的表一条记录也不会返回。半连接通常使用IN  或 EXISTS 作为连接条件
-		"SELECT d.deptno,d.dname,d.loc FROM scott.dept d WHERE d.deptno IN  (SELECT e.deptno FROM scott.emp e);",
-
-		// Delayed Join
-		// https://www.percona.com/blog/2007/04/06/using-delayed-join-to-optimize-count-and-limit-queries/
-		`SELECT visitor_id, url FROM (SELECT id FROM log WHERE ip="123.45.67.89" order by ts desc limit 50, 10) I JOIN log ON (I.id=log.id) JOIN url ON (url.id=log.url_id) order by TS desc;`,
-
-		// DELETE
-		"DELETE city, country FROM city INNER JOIN country using (country_id) WHERE city.city_id = 1;",
-		"DELETE city FROM city LEFT JOIN country ON city.country_id = country.country_id WHERE country.country IS NULL;",
-		"DELETE a1, a2 FROM city AS a1 INNER JOIN country AS a2 WHERE a1.country_id=a2.country_id;",
-		"DELETE FROM a1, a2 USING city AS a1 INNER JOIN country AS a2 WHERE a1.country_id=a2.country_id;",
-		"DELETE FROM film WHERE length > 100;",
-
-		// UPDATE
-		"UPDATE city INNER JOIN country USING(country_id) SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.city_id=10;",
-		"UPDATE city INNER JOIN country ON city.country_id = country.country_id INNER JOIN address ON city.city_id = address.city_id SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.city_id=10;",
-		"UPDATE city, country SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.country_id = country.country_id AND city.city_id=10;",
-		"UPDATE film SET length = 10 WHERE language_id = 20;",
-
-		// INSERT
-		"INSERT INTO city (country_id) SELECT country_id FROM country;",
-		"INSERT INTO city (country_id) VALUES (1),(2),(3);",
-		"INSERT INTO city (country_id) VALUES (10);",
-		"INSERT INTO city (country_id) SELECT 10 FROM DUAL;",
-
-		// REPLACE
-		"REPLACE INTO city (country_id) SELECT country_id FROM country;",
-		"REPLACE INTO city (country_id) VALUES (1),(2),(3);",
-		"REPLACE INTO city (country_id) VALUES (10);",
-		"REPLACE INTO city (country_id) SELECT 10 FROM DUAL;",
-
-		// DEPTH
-		"SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM ( SELECT film_id FROM  film ) film ) film ) film ) film ) film ) film ) film ) film ) film ) film ) film ) film ) film ) film ) film ) film;",
-
-		// SUBQUERY
-		"SELECT * FROM film WHERE language_id = (SELECT language_id FROM language LIMIT 1);",
-		// "SELECT COUNT(*) /* no hint */ FROM t2 WHERE NOT EXISTS (SELECT * FROM t3 WHERE ROW(5 * t2.s1, 77) = (SELECT 50, 11 * s1 FROM t4 UNION SELECT 50, 77 FROM (SELECT * FROM t5) AS t5 ) ) ;",
-		"SELECT * FROM city i left JOIN country o ON i.city_id=o.country_id union SELECT * FROM city i right JOIN country o ON i.city_id=o.country_id;",
-		"SELECT * FROM (SELECT * FROM actor WHERE last_update='2006-02-15 04:34:33' and last_name='CHASE') t WHERE last_update='2006-02-15 04:34:33' and last_name='CHASE' GROUP BY first_name;",
-		"SELECT * FROM city i left JOIN country o ON i.city_id=o.country_id union SELECT * FROM city i right JOIN country o ON i.city_id=o.country_id;",
-		"SELECT * FROM city i left JOIN country o ON i.city_id=o.country_id WHERE o.country_id is null union SELECT * FROM city i right JOIN country o ON i.city_id=o.country_id WHERE i.city_id is null;",
-		"SELECT first_name,last_name,email FROM customer STRAIGHT_JOIN address ON customer.address_id=address.address_id;",
-		"SELECT ID,name FROM (SELECT address FROM customer_list WHERE SID=1 order by phone limit 50,10) a JOIN customer_list l ON (a.address=l.address) JOIN city c ON (c.city=l.city) order by phone desc;",
-
-		// function in conditions
-		"SELECT * FROM film WHERE date(last_update)='2006-02-15';",
-		"SELECT last_update FROM film GROUP BY date(last_update);",
-		"SELECT last_update FROM film order by date(last_update);",
-
-		// CLA.004
-		"SELECT description FROM film WHERE description IN('NEWS','asd') GROUP BY description;",
-
-		// ALTER TABLE ADD INDEX
-		// 已经存在索引的列应该提醒索引已存在
-		"alter table address add index idx_city_id(city_id);",
-		"alter table inventory add index `idx_store_film` (`store_id`,`film_id`);",
-		"alter table inventory add index `idx_store_film` (`store_id`,`film_id`),add index `idx_store_film` (`store_id`,`film_id`),add index `idx_store_film` (`store_id`,`film_id`);",
-
-		// https://github.com/XiaoMi/soar/issues/47
-		`SELECT	DATE_FORMAT(t.atm, '%Y-%m-%d'),	COUNT(DISTINCT (t.usr))	FROM usr_terminal t WHERE t.atm > '2018-10-22 00:00:00'	AND t.agent LIKE '%Chrome%'	AND t.system = 'eip' GROUP BY DATE_FORMAT(t.atm, '%Y-%m-%d')	ORDER BY DATE_FORMAT(t.atm, '%Y-%m-%d')`,
-		// https://github.com/XiaoMi/soar/issues/17
-		"create table hello.t (id int unsigned);",
-	}
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/chardet.go b/vendor/github.com/XiaoMi/soar/common/chardet.go
deleted file mode 100644
index 223b330..0000000
--- a/vendor/github.com/XiaoMi/soar/common/chardet.go
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"github.com/saintfish/chardet"
-)
-
-// Chardet get best match charset
-func Chardet(buf []byte) string {
-	// check character set by file BOM
-	charset := CheckCharsetByBOM(buf)
-	if charset != "" {
-		return charset
-	}
-
-	// use chardet pkg check file charset
-	charset = "unknown"
-	var confidence int
-	detector := chardet.NewTextDetector()
-
-	// detector.DetectBest is unstable
-	// when the confidence value are equally, the best detect charset will be random
-	result, err := detector.DetectAll(buf)
-	if err != nil {
-		return charset
-	}
-
-	// SOAR's main user speak Chinese, GB-18030, UTF-8 are higher suggested
-	for _, r := range result {
-		if confidence > r.Confidence && r.Confidence != 0 {
-			return charset
-		}
-		confidence = r.Confidence
-		if r.Charset == "GB-18030" || r.Charset == "UTF-8" {
-			return r.Charset
-		}
-		charset = r.Charset
-	}
-	return charset
-}
-
-// CheckCharsetByBOM ref: https://en.wikipedia.org/wiki/Byte_order_mark
-func CheckCharsetByBOM(buf []byte) string {
-	// TODO: There are many kind of BOM
-	// UTF-8	EF BB BF
-	if len(buf) >= 3 {
-		if buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf {
-			return "UTF-8"
-		}
-	}
-	// GB-18030	84 31 95 33
-	if len(buf) >= 4 {
-		if buf[0] == 0x84 && buf[1] == 0x31 && buf[2] == 0x95 && buf[3] == 0x33 {
-			return "GB-18030"
-		}
-	}
-	return ""
-}
-
-// RemoveBOM remove bom from file
-func RemoveBOM(buf []byte) (string, []byte) {
-	// ef bb bf, UTF-8 BOM
-	if len(buf) > 3 {
-		if buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf {
-			return string(buf[3:]), buf[:3]
-		}
-	}
-	// ff fe, UTF-16 (LE) BOM
-	if len(buf) > 2 {
-		if buf[0] == 0xff && buf[1] == 0xfe {
-			return string(buf[2:]), buf[:2]
-		}
-	}
-	return string(buf), []byte{}
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/chardet_test.go b/vendor/github.com/XiaoMi/soar/common/chardet_test.go
deleted file mode 100644
index 0d6e411..0000000
--- a/vendor/github.com/XiaoMi/soar/common/chardet_test.go
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"fmt"
-	"io/ioutil"
-	"testing"
-)
-
-func TestChardet(t *testing.T) {
-	charsets := []string{
-		"GB-18030",
-		"UTF-8",
-	}
-	for _, c := range charsets {
-		fileName := DevPath + "/common/testdata/chardet_" + c + ".txt"
-		buf, err := ioutil.ReadFile(fileName)
-		if err != nil {
-			t.Errorf("ioutil.ReadFile %s, Error: %s", fileName, err.Error())
-		}
-		name := Chardet(buf)
-		if name != c {
-			t.Errorf("file: %s, Want: %s, Get: %s", fileName, c, name)
-		}
-	}
-}
-
-func TestRemoveBOM(t *testing.T) {
-	fileName := DevPath + "/common/testdata/UTF-8.bom.sql"
-	buf, err := ioutil.ReadFile(fileName)
-	if err != nil {
-		t.Errorf("ioutil.ReadFile %s, Error: %s", fileName, err.Error())
-	}
-	GoldenDiff(func() {
-		fmt.Println(RemoveBOM(buf))
-	}, t.Name(), update)
-}
-
-func TestCheckCharsetByBOM(t *testing.T) {
-	fileName := DevPath + "/common/testdata/UTF-8.bom.sql"
-	buf, err := ioutil.ReadFile(fileName)
-	if err != nil {
-		t.Errorf("ioutil.ReadFile %s, Error: %s", fileName, err.Error())
-	}
-
-	if CheckCharsetByBOM(buf) != "UTF-8" {
-		t.Errorf("checkCharsetByBOM Want: UTF-8, Get: %s", CheckCharsetByBOM(buf))
-	}
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/config.go b/vendor/github.com/XiaoMi/soar/common/config.go
deleted file mode 100644
index 4f84116..0000000
--- a/vendor/github.com/XiaoMi/soar/common/config.go
+++ /dev/null
@@ -1,882 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"bufio"
-	"bytes"
-	"encoding/json"
-	"flag"
-	"fmt"
-	"io"
-	"io/ioutil"
-	"os"
-	"regexp"
-	"runtime"
-	"strings"
-
-	"gopkg.in/yaml.v2"
-)
-
-var (
-	// BlackList 黑名单中的SQL不会被评审
-	BlackList []string
-	// PrintConfig -print-config
-	PrintConfig bool
-	// PrintVersion -print-config
-	PrintVersion bool
-	// CheckConfig -check-config
-	CheckConfig bool
-	// 防止 readCmdFlags 函数重入
-	hasParsed bool
-)
-
-// Configuration 配置文件定义结构体
-type Configuration struct {
-	// +++++++++++++++测试环境+++++++++++++++++
-	OnlineDSN               *dsn   `yaml:"online-dsn"`                // 线上环境数据库配置
-	TestDSN                 *dsn   `yaml:"test-dsn"`                  // 测试环境数据库配置
-	AllowOnlineAsTest       bool   `yaml:"allow-online-as-test"`      // 允许 Online 环境也可以当作 Test 环境
-	DropTestTemporary       bool   `yaml:"drop-test-temporary"`       // 是否清理Test环境产生的临时库表
-	CleanupTestDatabase     bool   `yaml:"cleanup-test-database"`     // 清理残余的测试数据库(程序异常退出或未开启drop-test-temporary)  issue #48
-	OnlySyntaxCheck         bool   `yaml:"only-syntax-check"`         // 只做语法检查不输出优化建议
-	SamplingStatisticTarget int    `yaml:"sampling-statistic-target"` // 数据采样因子,对应 PostgreSQL 的 default_statistics_target
-	Sampling                bool   `yaml:"sampling"`                  // 数据采样开关
-	Profiling               bool   `yaml:"profiling"`                 // 在开启数据采样的情况下,在测试环境执行进行profile
-	Trace                   bool   `yaml:"trace"`                     // 在开启数据采样的情况下,在测试环境执行进行Trace
-	Explain                 bool   `yaml:"explain"`                   // Explain开关
-	ConnTimeOut             int    `yaml:"conn-time-out"`             // 数据库连接超时时间,单位秒
-	QueryTimeOut            int    `yaml:"query-time-out"`            // 数据库SQL执行超时时间,单位秒
-	Delimiter               string `yaml:"delimiter"`                 // SQL分隔符
-
-	// +++++++++++++++日志相关+++++++++++++++++
-	// 日志级别,这里使用了 beego 的 log 包
-	// [0:Emergency, 1:Alert, 2:Critical, 3:Error, 4:Warning, 5:Notice, 6:Informational, 7:Debug]
-	LogLevel int `yaml:"log-level"`
-	// 日志输出位置,默认日志输出到控制台
-	// 目前只支持['console', 'file']两种形式,如非console形式这里需要指定文件的路径,可以是相对路径
-	LogOutput string `yaml:"log-output"`
-	// 优化建议输出格式,目前支持: json, text, markdown格式,如指定其他格式会给 pretty.Println 的输出
-	ReportType string `yaml:"report-type"`
-	// 当 ReportType 为 html 格式时使用的 css 风格,如不指定会提供一个默认风格。CSS可 以是本地文件,也可以是一个URL
-	ReportCSS string `yaml:"report-css"`
-	// 当 ReportType 为 html 格式时使用的 javascript 脚本,如不指定默认会加载SQL pretty 使用的 javascript。像CSS一样可以是本地文件,也可以是一个URL
-	ReportJavascript string `yaml:"report-javascript"`
-	// 当ReportType 为 html 格式时,HTML 的 title
-	ReportTitle string `yaml:"report-title"`
-	// blackfriday markdown2html config
-	MarkdownExtensions int `yaml:"markdown-extensions"` // markdown 转 html 支持的扩展包, 参考blackfriday
-	MarkdownHTMLFlags  int `yaml:"markdown-html-flags"` // markdown 转 html 支持的 flag, 参考blackfriday, default 0
-
-	// ++++++++++++++优化建议相关++++++++++++++
-	IgnoreRules          []string `yaml:"ignore-rules"`              // 忽略的优化建议规则
-	RewriteRules         []string `yaml:"rewrite-rules"`             // 生效的重写规则
-	BlackList            string   `yaml:"blacklist"`                 // blacklist 中的 SQL 不会被评审,可以是指纹,也可以是正则
-	MaxJoinTableCount    int      `yaml:"max-join-table-count"`      // 单条 SQL 中 JOIN 表的最大数量
-	MaxGroupByColsCount  int      `yaml:"max-group-by-cols-count"`   // 单条 SQL 中 GroupBy 包含列的最大数量
-	MaxDistinctCount     int      `yaml:"max-distinct-count"`        // 单条 SQL 中 Distinct 的最大数量
-	MaxIdxColsCount      int      `yaml:"max-index-cols-count"`      // 复合索引中包含列的最大数量
-	MaxTotalRows         int64    `yaml:"max-total-rows"`            // 计算散粒度时,当数据行数大于 MaxTotalRows 即开启数据库保护模式,散粒度返回结果可信度下降
-	MaxQueryCost         int64    `yaml:"max-query-cost"`            // last_query_cost 超过该值时将给予警告
-	SpaghettiQueryLength int      `yaml:"spaghetti-query-length"`    // SQL最大长度警告,超过该长度会给警告
-	AllowDropIndex       bool     `yaml:"allow-drop-index"`          // 允许输出删除重复索引的建议
-	MaxInCount           int      `yaml:"max-in-count"`              // IN()最大数量
-	MaxIdxBytesPerColumn int      `yaml:"max-index-bytes-percolumn"` // 索引中单列最大字节数,默认767
-	MaxIdxBytes          int      `yaml:"max-index-bytes"`           // 索引总长度限制,默认3072
-	TableAllowCharsets   []string `yaml:"table-allow-charsets"`      // Table 允许使用的 DEFAULT CHARSET
-	TableAllowEngines    []string `yaml:"table-allow-engines"`       // Table 允许使用的 Engine
-	MaxIdxCount          int      `yaml:"max-index-count"`           // 单张表允许最多索引数
-	MaxColCount          int      `yaml:"max-column-count"`          // 单张表允许最大列数
-	MaxValueCount        int      `yaml:"max-value-count"`           // INSERT/REPLACE 单次允许批量写入的行数
-	IdxPrefix            string   `yaml:"index-prefix"`              // 普通索引建议使用的前缀
-	UkPrefix             string   `yaml:"unique-key-prefix"`         // 唯一键建议使用的前缀
-	MaxSubqueryDepth     int      `yaml:"max-subquery-depth"`        // 子查询最大尝试
-	MaxVarcharLength     int      `yaml:"max-varchar-length"`        // varchar最大长度
-
-	// ++++++++++++++EXPLAIN检查项+++++++++++++
-	ExplainSQLReportType   string   `yaml:"explain-sql-report-type"`  // EXPLAIN markdown 格式输出 SQL 样式,支持 sample, fingerprint, pretty 等
-	ExplainType            string   `yaml:"explain-type"`             // EXPLAIN方式 [traditional, extended, partitions]
-	ExplainFormat          string   `yaml:"explain-format"`           // FORMAT=[json, traditional]
-	ExplainWarnSelectType  []string `yaml:"explain-warn-select-type"` // 哪些 select_type 不建议使用
-	ExplainWarnAccessType  []string `yaml:"explain-warn-access-type"` // 哪些 access type 不建议使用
-	ExplainMaxKeyLength    int      `yaml:"explain-max-keys"`         // 最大 key_len
-	ExplainMinPossibleKeys int      `yaml:"explain-min-keys"`         // 最小 possible_keys 警告
-	ExplainMaxRows         int      `yaml:"explain-max-rows"`         // 最大扫描行数警告
-	ExplainWarnExtra       []string `yaml:"explain-warn-extra"`       // 哪些 extra 信息会给警告
-	ExplainMaxFiltered     float64  `yaml:"explain-max-filtered"`     // filtered 大于该配置给出警告
-	ExplainWarnScalability []string `yaml:"explain-warn-scalability"` // 复杂度警告名单
-	ShowWarnings           bool     `yaml:"show-warnings"`            // explain extended with show warnings
-	ShowLastQueryCost      bool     `yaml:"show-last-query-cost"`     // switch with show status like 'last_query_cost'
-	// ++++++++++++++其他配置项+++++++++++++++
-	Query              string `yaml:"query"`                 // 需要进行调优的SQL
-	ListHeuristicRules bool   `yaml:"list-heuristic-rules"`  // 打印支持的评审规则列表
-	ListRewriteRules   bool   `yaml:"list-rewrite-rules"`    // 打印重写规则
-	ListTestSqls       bool   `yaml:"list-test-sqls"`        // 打印测试case用于测试
-	ListReportTypes    bool   `yaml:"list-report-types"`     // 打印支持的报告输出类型
-	Verbose            bool   `yaml:"verbose"`               // verbose模式,会多输出一些信息
-	DryRun             bool   `yaml:"dry-run"`               // 是否在预演环境执行
-	MaxPrettySQLLength int    `yaml:"max-pretty-sql-length"` // 超出该长度的SQL会转换成指纹输出
-}
-
-// Config 默认设置
-var Config = &Configuration{
-	OnlineDSN: &dsn{
-		Schema:  "information_schema",
-		Charset: "utf8mb4",
-		Disable: true,
-		Version: 99999,
-	},
-	TestDSN: &dsn{
-		Schema:  "information_schema",
-		Charset: "utf8mb4",
-		Disable: true,
-		Version: 99999,
-	},
-	AllowOnlineAsTest:       false,
-	DropTestTemporary:       true,
-	CleanupTestDatabase:     false,
-	DryRun:                  true,
-	OnlySyntaxCheck:         false,
-	SamplingStatisticTarget: 100,
-	Sampling:                false,
-	Profiling:               false,
-	Trace:                   false,
-	Explain:                 true,
-	ConnTimeOut:             3,
-	QueryTimeOut:            30,
-	Delimiter:               ";",
-
-	MaxJoinTableCount:    5,
-	MaxGroupByColsCount:  5,
-	MaxDistinctCount:     5,
-	MaxIdxColsCount:      5,
-	MaxIdxBytesPerColumn: 767,
-	MaxIdxBytes:          3072,
-	MaxTotalRows:         9999999,
-	MaxQueryCost:         9999,
-	SpaghettiQueryLength: 2048,
-	AllowDropIndex:       false,
-	LogLevel:             3,
-	LogOutput:            "soar.log",
-	ReportType:           "markdown",
-	ReportCSS:            "",
-	ReportJavascript:     "",
-	ReportTitle:          "SQL优化分析报告",
-	BlackList:            "",
-	TableAllowCharsets:   []string{"utf8", "utf8mb4"},
-	TableAllowEngines:    []string{"innodb"},
-	MaxIdxCount:          10,
-	MaxColCount:          40,
-	MaxValueCount:        100,
-	MaxInCount:           10,
-	IdxPrefix:            "idx_",
-	UkPrefix:             "uk_",
-	MaxSubqueryDepth:     5,
-	MaxVarcharLength:     1024,
-
-	MarkdownExtensions: 94,
-	MarkdownHTMLFlags:  0,
-
-	ExplainSQLReportType:   "pretty",
-	ExplainType:            "extended",
-	ExplainFormat:          "traditional",
-	ExplainWarnSelectType:  []string{""},
-	ExplainWarnAccessType:  []string{"ALL"},
-	ExplainMaxKeyLength:    3,
-	ExplainMinPossibleKeys: 0,
-	ExplainMaxRows:         10000,
-	ExplainWarnExtra:       []string{"Using temporary", "Using filesort"},
-	ExplainMaxFiltered:     100.0,
-	ExplainWarnScalability: []string{"O(n)"},
-	ShowWarnings:           false,
-	ShowLastQueryCost:      false,
-
-	IgnoreRules: []string{
-		"COL.011",
-	},
-	RewriteRules: []string{
-		"delimiter",
-		"orderbynull",
-		"groupbyconst",
-		"dmlorderby",
-		"having",
-		"star2columns",
-		"insertcolumns",
-		"distinctstar",
-	},
-
-	ListHeuristicRules: false,
-	ListRewriteRules:   false,
-	ListTestSqls:       false,
-	ListReportTypes:    false,
-	MaxPrettySQLLength: 1024,
-}
-
-type dsn struct {
-	Addr   string `yaml:"addr"`
-	Schema string `yaml:"schema"`
-
-	// 数据库用户名和密码可以通过系统环境变量的形式赋值
-	User     string `yaml:"user"`
-	Password string `yaml:"password"`
-	Charset  string `yaml:"charset"`
-	Disable  bool   `yaml:"disable"`
-
-	Version int `yaml:"-"` // 版本自动检查,不可配置
-}
-
-// 解析命令行DSN输入
-func parseDSN(odbc string, d *dsn) *dsn {
-	var addr, user, password, schema, charset string
-	if odbc == FormatDSN(d) {
-		return d
-	}
-
-	if d != nil {
-		addr = d.Addr
-		user = d.User
-		password = d.Password
-		schema = d.Schema
-		charset = d.Charset
-	}
-
-	// 设置为空表示禁用环境
-	odbc = strings.TrimSpace(odbc)
-	if odbc == "" {
-		return &dsn{Disable: true}
-	}
-
-	// username:password@ip:port/database
-	l1 := strings.Split(odbc, "@")
-	if len(l1) < 2 {
-		if strings.HasPrefix(l1[0], ":") {
-			// ":port/database"
-			l2 := strings.Split(strings.TrimLeft(l1[0], ":"), "/")
-			if l2[0] == "" {
-				addr = strings.Split(addr, ":")[0] + ":3306"
-				if len(l2) > 1 {
-					schema = strings.Split(l2[1], "?")[0]
-				}
-			} else {
-				addr = strings.Split(addr, ":")[0] + ":" + l2[0]
-				if len(l2) > 1 {
-					schema = strings.Split(l2[1], "?")[0]
-				}
-			}
-		} else if strings.HasPrefix(l1[0], "/") {
-			// "/database"
-			l2 := strings.TrimLeft(l1[0], "/")
-			schema = l2
-		} else {
-			// ip:port/database
-			l2 := strings.Split(l1[0], "/")
-			if len(l2) == 2 {
-				addr = l2[0]
-				schema = strings.Split(l2[1], "?")[0]
-			} else {
-				addr = l2[0]
-			}
-		}
-	} else {
-		// user:password
-		l2 := strings.Split(l1[0], ":")
-		if len(l2) == 2 {
-			user = l2[0]
-			password = l2[1]
-		} else {
-			user = l2[0]
-		}
-		// ip:port/database
-		l3 := strings.Split(l1[1], "/")
-		if len(l3) == 2 {
-			addr = l3[0]
-			schema = strings.Split(l3[1], "?")[0]
-		} else {
-			addr = l3[0]
-		}
-	}
-
-	// 其他flag参数,目前只支持charset :(
-	if len(strings.Split(odbc, "?")) > 1 {
-		flags := strings.Split(strings.Split(odbc, "?")[1], "&")
-		for _, f := range flags {
-			attr := strings.Split(f, "=")
-			if len(attr) > 1 {
-				arg := strings.TrimSpace(attr[0])
-				val := strings.TrimSpace(attr[1])
-				switch arg {
-				case "charset":
-					charset = val
-				default:
-				}
-			}
-		}
-	}
-
-	// 自动补端口
-	if !strings.Contains(addr, ":") {
-		addr = addr + ":3306"
-	} else {
-		if strings.HasSuffix(addr, ":") {
-			addr = addr + "3306"
-		}
-	}
-
-	// 默认走127.0.0.1
-	if strings.HasPrefix(addr, ":") {
-		addr = "127.0.0.1" + addr
-	}
-
-	// 默认用information_schema库
-	if schema == "" {
-		schema = "information_schema"
-	}
-
-	// 默认utf8mb4使用字符集
-	if charset == "" {
-		charset = "utf8mb4"
-	}
-
-	dsn := &dsn{
-		Addr:     addr,
-		User:     user,
-		Password: password,
-		Schema:   schema,
-		Charset:  charset,
-		Disable:  false,
-		Version:  999,
-	}
-	return dsn
-}
-
-// FormatDSN 格式化打印DSN
-func FormatDSN(env *dsn) string {
-	if env == nil || env.Disable {
-		return ""
-	}
-	// username:password@ip:port/schema?charset=xxx
-	return fmt.Sprintf("%s:%s@%s/%s?charset=%s", env.User, env.Password, env.Addr, env.Schema, env.Charset)
-}
-
-// SoarVersion soar version information
-func SoarVersion() {
-	fmt.Println("Version:", Version)
-	fmt.Println("Branch:", Branch)
-	fmt.Println("Compile:", Compile)
-	fmt.Println("GitDirty:", GitDirty)
-}
-
-// 因为vitess sqlparser 使用了 glog 中也会使用 flag,为了不让用户困扰我们单独写一个 usage
-func usage() {
-	regPwd := regexp.MustCompile(`:.*@`)
-	vitessHelp := []string{
-		"-alsologtostderr",
-		"log to standard error as well as files",
-		"-log_backtrace_at value",
-		"when logging hits line file:N, emit a stack trace",
-		"-log_dir string",
-		"If non-empty, write log files in this directory",
-		"-logtostderr",
-		"log to standard error instead of files",
-		"-sql-max-length-errors int",
-		"truncate queries in error logs to the given length (default unlimited)",
-		"-sql-max-length-ui int",
-		"truncate queries in debug UIs to the given length (default 512) (default 512)",
-		"-stderrthreshold value",
-		"logs at or above this threshold go to stderr",
-		"-v value",
-		"log level for V logs",
-		"-vmodule value",
-		"comma-separated list of pattern=N settings for file-filtered logging",
-	}
-
-	// io redirect
-	restoreStdout := os.Stdout
-	restoreStderr := os.Stderr
-	stdin, stdout, _ := os.Pipe()
-	os.Stderr = stdout
-	os.Stdout = stdout
-
-	flag.PrintDefaults()
-
-	// copy the output in a separate goroutine so printing can't block indefinitely
-	outC := make(chan string)
-	go func() {
-		var buf bytes.Buffer
-		_, err := io.Copy(&buf, stdin)
-		if err != nil {
-			fmt.Println(err.Error())
-		}
-		outC <- buf.String()
-	}()
-
-	// back to normal state
-	stdout.Close()
-	os.Stdout = restoreStdout // restoring the real stderr
-	os.Stderr = restoreStderr
-
-	fmt.Printf("Usage of %s:\n", os.Args[0])
-	// reading our temp stdout
-	out := <-outC
-	for _, line := range strings.Split(out, "\n") {
-		found := false
-		for _, ignore := range vitessHelp {
-			if strings.TrimSpace(line) == strings.TrimSpace(ignore) {
-				found = true
-			}
-			if regPwd.MatchString(line) && !Config.Verbose {
-				line = regPwd.ReplaceAllString(line, ":********@")
-			}
-		}
-		if !found {
-			fmt.Println(line)
-		}
-	}
-}
-
-// PrintConfiguration for `-print-config` flag
-func PrintConfiguration() {
-	// 打印配置的时候密码不显示
-	if !Config.Verbose {
-		Config.OnlineDSN.Password = "********"
-		Config.TestDSN.Password = "********"
-	}
-	data, _ := yaml.Marshal(Config)
-	fmt.Print(string(data))
-}
-
-// 加载配置文件
-func (conf *Configuration) readConfigFile(path string) error {
-	configFile, err := os.Open(path)
-	if err != nil {
-		Log.Warning("readConfigFile(%s) os.Open failed: %v", path, err)
-		return err
-	}
-	defer configFile.Close()
-
-	content, err := ioutil.ReadAll(configFile)
-	if err != nil {
-		Log.Warning("readConfigFile(%s) ioutil.ReadAll failed: %v", path, err)
-		return err
-	}
-
-	err = yaml.Unmarshal(content, Config)
-	if err != nil {
-		Log.Warning("readConfigFile(%s) yaml.Unmarshal failed: %v", path, err)
-		return err
-	}
-	return nil
-}
-
-// 从命令行参数读配置
-func readCmdFlags() error {
-	if hasParsed {
-		Log.Debug("Skip read cmd flags.")
-		return nil
-	}
-
-	_ = flag.String("config", "", "Config file path")
-	// +++++++++++++++测试环境+++++++++++++++++
-	onlineDSN := flag.String("online-dsn", FormatDSN(Config.OnlineDSN), "OnlineDSN, 线上环境数据库配置, username:password@ip:port/schema")
-	testDSN := flag.String("test-dsn", FormatDSN(Config.TestDSN), "TestDSN, 测试环境数据库配置, username:password@ip:port/schema")
-	allowOnlineAsTest := flag.Bool("allow-online-as-test", Config.AllowOnlineAsTest, "AllowOnlineAsTest, 允许线上环境也可以当作测试环境")
-	dropTestTemporary := flag.Bool("drop-test-temporary", Config.DropTestTemporary, "DropTestTemporary, 是否清理测试环境产生的临时库表")
-	cleanupTestDatabase := flag.Bool("cleanup-test-database", Config.CleanupTestDatabase, "单次运行清理历史1小时前残余的测试库。")
-	onlySyntaxCheck := flag.Bool("only-syntax-check", Config.OnlySyntaxCheck, "OnlySyntaxCheck, 只做语法检查不输出优化建议")
-	profiling := flag.Bool("profiling", Config.Profiling, "Profiling, 开启数据采样的情况下在测试环境执行Profile")
-	trace := flag.Bool("trace", Config.Trace, "Trace, 开启数据采样的情况下在测试环境执行Trace")
-	explain := flag.Bool("explain", Config.Explain, "Explain, 是否开启Explain执行计划分析")
-	sampling := flag.Bool("sampling", Config.Sampling, "Sampling, 数据采样开关")
-	samplingStatisticTarget := flag.Int("sampling-statistic-target", Config.SamplingStatisticTarget, "SamplingStatisticTarget, 数据采样因子,对应 PostgreSQL 的 default_statistics_target")
-	connTimeOut := flag.Int("conn-time-out", Config.ConnTimeOut, "ConnTimeOut, 数据库连接超时时间,单位秒")
-	queryTimeOut := flag.Int("query-time-out", Config.QueryTimeOut, "QueryTimeOut, 数据库SQL执行超时时间,单位秒")
-	delimiter := flag.String("delimiter", Config.Delimiter, "Delimiter, SQL分隔符")
-	// +++++++++++++++日志相关+++++++++++++++++
-	logLevel := flag.Int("log-level", Config.LogLevel, "LogLevel, 日志级别, [0:Emergency, 1:Alert, 2:Critical, 3:Error, 4:Warning, 5:Notice, 6:Informational, 7:Debug]")
-	logOutput := flag.String("log-output", Config.LogOutput, "LogOutput, 日志输出位置")
-	reportType := flag.String("report-type", Config.ReportType, "ReportType, 化建议输出格式,目前支持: json, text, markdown, html等")
-	reportCSS := flag.String("report-css", Config.ReportCSS, "ReportCSS, 当 ReportType 为 html 格式时使用的 css 风格,如不指定会提供一个默认风格。CSS可以是本地文件,也可以是一个URL")
-	reportJavascript := flag.String("report-javascript", Config.ReportJavascript, "ReportJavascript, 当 ReportType 为 html 格式时使用的javascript脚本,如不指定默认会加载SQL pretty 使用的 javascript。像CSS一样可以是本地文件,也可以是一个URL")
-	reportTitle := flag.String("report-title", Config.ReportTitle, "ReportTitle, 当 ReportType 为 html 格式时,HTML 的 title")
-	// +++++++++++++++markdown+++++++++++++++++
-	markdownExtensions := flag.Int("markdown-extensions", Config.MarkdownExtensions, "MarkdownExtensions, markdown 转 html支持的扩展包, 参考blackfriday")
-	markdownHTMLFlags := flag.Int("markdown-html-flags", Config.MarkdownHTMLFlags, "MarkdownHTMLFlags, markdown 转 html 支持的 flag, 参考blackfriday")
-	// ++++++++++++++优化建议相关++++++++++++++
-	ignoreRules := flag.String("ignore-rules", strings.Join(Config.IgnoreRules, ","), "IgnoreRules, 忽略的优化建议规则")
-	rewriteRules := flag.String("rewrite-rules", strings.Join(Config.RewriteRules, ","), "RewriteRules, 生效的重写规则")
-	blackList := flag.String("blacklist", Config.BlackList, "指定 blacklist 配置文件的位置,文件中的 SQL 不会被评审。一行一条SQL,可以是指纹,也可以是正则")
-	maxJoinTableCount := flag.Int("max-join-table-count", Config.MaxJoinTableCount, "MaxJoinTableCount, 单条 SQL 中 JOIN 表的最大数量")
-	maxGroupByColsCount := flag.Int("max-group-by-cols-count", Config.MaxGroupByColsCount, "MaxGroupByColsCount, 单条 SQL 中 GroupBy 包含列的最大数量")
-	maxDistinctCount := flag.Int("max-distinct-count", Config.MaxDistinctCount, "MaxDistinctCount, 单条 SQL 中 Distinct 的最大数量")
-	maxIdxColsCount := flag.Int("max-index-cols-count", Config.MaxIdxColsCount, "MaxIdxColsCount, 复合索引中包含列的最大数量")
-	maxTotalRows := flag.Int64("max-total-rows", Config.MaxTotalRows, "MaxTotalRows, 计算散粒度时,当数据行数大于MaxTotalRows即开启数据库保护模式,不计算散粒度")
-	maxQueryCost := flag.Int64("max-query-cost", Config.MaxQueryCost, "MaxQueryCost, last_query_cost 超过该值时将给予警告")
-	spaghettiQueryLength := flag.Int("spaghetti-query-length", Config.SpaghettiQueryLength, "SpaghettiQueryLength, SQL最大长度警告,超过该长度会给警告")
-	allowDropIdx := flag.Bool("allow-drop-index", Config.AllowDropIndex, "AllowDropIndex, 允许输出删除重复索引的建议")
-	maxInCount := flag.Int("max-in-count", Config.MaxInCount, "MaxInCount, IN()最大数量")
-	maxIdxBytesPerColumn := flag.Int("max-index-bytes-percolumn", Config.MaxIdxBytesPerColumn, "MaxIdxBytesPerColumn, 索引中单列最大字节数")
-	maxIdxBytes := flag.Int("max-index-bytes", Config.MaxIdxBytes, "MaxIdxBytes, 索引总长度限制")
-	tableAllowCharsets := flag.String("table-allow-charsets", strings.ToLower(strings.Join(Config.TableAllowCharsets, ",")), "TableAllowCharsets")
-	tableAllowEngines := flag.String("table-allow-engines", strings.ToLower(strings.Join(Config.TableAllowEngines, ",")), "TableAllowEngines")
-	maxIdxCount := flag.Int("max-index-count", Config.MaxIdxCount, "MaxIdxCount, 单表最大索引个数")
-	maxColCount := flag.Int("max-column-count", Config.MaxColCount, "MaxColCount, 单表允许的最大列数")
-	maxValueCount := flag.Int("max-value-count", Config.MaxValueCount, "MaxValueCount, INSERT/REPLACE 单次批量写入允许的行数")
-	idxPrefix := flag.String("index-prefix", Config.IdxPrefix, "IdxPrefix")
-	ukPrefix := flag.String("unique-key-prefix", Config.UkPrefix, "UkPrefix")
-	maxSubqueryDepth := flag.Int("max-subquery-depth", Config.MaxSubqueryDepth, "MaxSubqueryDepth")
-	maxVarcharLength := flag.Int("max-varchar-length", Config.MaxVarcharLength, "MaxVarcharLength")
-	// ++++++++++++++EXPLAIN检查项+++++++++++++
-	explainSQLReportType := flag.String("explain-sql-report-type", strings.ToLower(Config.ExplainSQLReportType), "ExplainSQLReportType [pretty, sample, fingerprint]")
-	explainType := flag.String("explain-type", strings.ToLower(Config.ExplainType), "ExplainType [extended, partitions, traditional]")
-	explainFormat := flag.String("explain-format", strings.ToLower(Config.ExplainFormat), "ExplainFormat [json, traditional]")
-	explainWarnSelectType := flag.String("explain-warn-select-type", strings.Join(Config.ExplainWarnSelectType, ","), "ExplainWarnSelectType, 哪些select_type不建议使用")
-	explainWarnAccessType := flag.String("explain-warn-access-type", strings.Join(Config.ExplainWarnAccessType, ","), "ExplainWarnAccessType, 哪些access type不建议使用")
-	explainMaxKeyLength := flag.Int("explain-max-keys", Config.ExplainMaxKeyLength, "ExplainMaxKeyLength, 最大key_len")
-	explainMinPossibleKeys := flag.Int("explain-min-keys", Config.ExplainMinPossibleKeys, "ExplainMinPossibleKeys, 最小possible_keys警告")
-	explainMaxRows := flag.Int("explain-max-rows", Config.ExplainMaxRows, "ExplainMaxRows, 最大扫描行数警告")
-	explainWarnExtra := flag.String("explain-warn-extra", strings.Join(Config.ExplainWarnExtra, ","), "ExplainWarnExtra, 哪些extra信息会给警告")
-	explainMaxFiltered := flag.Float64("explain-max-filtered", Config.ExplainMaxFiltered, "ExplainMaxFiltered, filtered大于该配置给出警告")
-	explainWarnScalability := flag.String("explain-warn-scalability", strings.Join(Config.ExplainWarnScalability, ","), "ExplainWarnScalability, 复杂度警告名单, 支持O(n),O(log n),O(1),O(?)")
-	showWarnings := flag.Bool("show-warnings", Config.ShowWarnings, "ShowWarnings")
-	showLastQueryCost := flag.Bool("show-last-query-cost", Config.ShowLastQueryCost, "ShowLastQueryCost")
-	// +++++++++++++++++其他+++++++++++++++++++
-	printConfig := flag.Bool("print-config", false, "Print configs")
-	checkConfig := flag.Bool("check-config", false, "Check configs")
-	printVersion := flag.Bool("version", false, "Print version info")
-	query := flag.String("query", Config.Query, "待评审的 SQL 或 SQL 文件,如 SQL 中包含特殊字符建议使用文件名。")
-	listHeuristicRules := flag.Bool("list-heuristic-rules", Config.ListHeuristicRules, "ListHeuristicRules, 打印支持的评审规则列表")
-	listRewriteRules := flag.Bool("list-rewrite-rules", Config.ListRewriteRules, "ListRewriteRules, 打印支持的重写规则列表")
-	listTestSQLs := flag.Bool("list-test-sqls", Config.ListTestSqls, "ListTestSqls, 打印测试case用于测试")
-	listReportTypes := flag.Bool("list-report-types", Config.ListReportTypes, "ListReportTypes, 打印支持的报告输出类型")
-	verbose := flag.Bool("verbose", Config.Verbose, "Verbose")
-	dryrun := flag.Bool("dry-run", Config.DryRun, "是否在预演环境执行")
-	maxPrettySQLLength := flag.Int("max-pretty-sql-length", Config.MaxPrettySQLLength, "MaxPrettySQLLength, 超出该长度的SQL会转换成指纹输出")
-	// 一个不存在 log-level,用于更新 usage。
-	// 因为 vitess 里面也用了 flag,这些 vitess 的参数我们不需要关注
-	if !Config.Verbose && runtime.GOOS != "windows" {
-		flag.Usage = usage
-	}
-	flag.Parse()
-
-	Config.OnlineDSN = parseDSN(*onlineDSN, Config.OnlineDSN)
-	Config.TestDSN = parseDSN(*testDSN, Config.TestDSN)
-	Config.AllowOnlineAsTest = *allowOnlineAsTest
-	Config.DropTestTemporary = *dropTestTemporary
-	Config.CleanupTestDatabase = *cleanupTestDatabase
-	Config.OnlySyntaxCheck = *onlySyntaxCheck
-	Config.Profiling = *profiling
-	Config.Trace = *trace
-	Config.Explain = *explain
-	Config.Sampling = *sampling
-	Config.SamplingStatisticTarget = *samplingStatisticTarget
-	Config.ConnTimeOut = *connTimeOut
-	Config.QueryTimeOut = *queryTimeOut
-
-	Config.LogLevel = *logLevel
-	if strings.HasPrefix(*logOutput, "/") {
-		Config.LogOutput = *logOutput
-	} else {
-		if BaseDir == "" {
-			Config.LogOutput = *logOutput
-		} else {
-			if runtime.GOOS == "windows" {
-				Config.LogOutput = *logOutput
-			} else {
-				Config.LogOutput = BaseDir + "/" + *logOutput
-			}
-		}
-	}
-	Config.ReportType = strings.ToLower(*reportType)
-	Config.ReportCSS = *reportCSS
-	Config.ReportJavascript = *reportJavascript
-	Config.ReportTitle = *reportTitle
-	Config.MarkdownExtensions = *markdownExtensions
-	Config.MarkdownHTMLFlags = *markdownHTMLFlags
-	Config.IgnoreRules = strings.Split(*ignoreRules, ",")
-	Config.RewriteRules = strings.Split(*rewriteRules, ",")
-	*blackList = strings.TrimSpace(*blackList)
-	if strings.HasPrefix(*blackList, "/") || *blackList == "" {
-		Config.BlackList = *blackList
-	} else {
-		pwd, _ := os.Getwd()
-		Config.BlackList = pwd + "/" + *blackList
-	}
-	Config.MaxJoinTableCount = *maxJoinTableCount
-	Config.MaxGroupByColsCount = *maxGroupByColsCount
-	Config.MaxDistinctCount = *maxDistinctCount
-
-	if *maxIdxColsCount < 16 {
-		Config.MaxIdxColsCount = *maxIdxColsCount
-	} else {
-		Config.MaxIdxColsCount = 16
-	}
-
-	Config.MaxIdxBytesPerColumn = *maxIdxBytesPerColumn
-	Config.MaxIdxBytes = *maxIdxBytes
-	Config.TableAllowCharsets = strings.Split(strings.ToLower(*tableAllowCharsets), ",")
-	Config.TableAllowEngines = strings.Split(strings.ToLower(*tableAllowEngines), ",")
-	Config.MaxIdxCount = *maxIdxCount
-	Config.MaxColCount = *maxColCount
-	Config.MaxValueCount = *maxValueCount
-	Config.IdxPrefix = *idxPrefix
-	Config.UkPrefix = *ukPrefix
-	Config.MaxSubqueryDepth = *maxSubqueryDepth
-	Config.MaxTotalRows = *maxTotalRows
-	Config.MaxQueryCost = *maxQueryCost
-	Config.AllowDropIndex = *allowDropIdx
-	Config.MaxInCount = *maxInCount
-	Config.SpaghettiQueryLength = *spaghettiQueryLength
-	Config.Query = *query
-	Config.Delimiter = *delimiter
-
-	Config.ExplainSQLReportType = strings.ToLower(*explainSQLReportType)
-	Config.ExplainType = strings.ToLower(*explainType)
-	Config.ExplainFormat = strings.ToLower(*explainFormat)
-	Config.ExplainWarnSelectType = strings.Split(*explainWarnSelectType, ",")
-	Config.ExplainWarnAccessType = strings.Split(*explainWarnAccessType, ",")
-	Config.ExplainMaxKeyLength = *explainMaxKeyLength
-	Config.ExplainMinPossibleKeys = *explainMinPossibleKeys
-	Config.ExplainMaxRows = *explainMaxRows
-	Config.ExplainWarnExtra = strings.Split(*explainWarnExtra, ",")
-	Config.ExplainMaxFiltered = *explainMaxFiltered
-	Config.ExplainWarnScalability = strings.Split(*explainWarnScalability, ",")
-	Config.ShowWarnings = *showWarnings
-	Config.ShowLastQueryCost = *showLastQueryCost
-	Config.ListHeuristicRules = *listHeuristicRules
-	Config.ListRewriteRules = *listRewriteRules
-	Config.ListTestSqls = *listTestSQLs
-	Config.ListReportTypes = *listReportTypes
-	Config.Verbose = *verbose
-	Config.DryRun = *dryrun
-	Config.MaxPrettySQLLength = *maxPrettySQLLength
-	Config.MaxVarcharLength = *maxVarcharLength
-
-	PrintVersion = *printVersion
-	PrintConfig = *printConfig
-	CheckConfig = *checkConfig
-
-	hasParsed = true
-	return nil
-}
-
-// ParseConfig 加载配置文件和命令行参数
-func ParseConfig(configFile string) error {
-	var err error
-	var configs []string
-	// 指定了配置文件优先读配置文件,未指定配置文件按如下顺序加载,先找到哪个加载哪个
-	if configFile == "" {
-		configs = []string{
-			"/etc/soar.yaml",
-			BaseDir + "/etc/soar.yaml",
-			BaseDir + "/soar.yaml",
-		}
-	} else {
-		configs = []string{
-			configFile,
-		}
-	}
-
-	for _, config := range configs {
-		if _, err = os.Stat(config); err == nil {
-			err = Config.readConfigFile(config)
-			if err != nil {
-				Log.Error("ParseConfig Config.readConfigFile Error: %v", err)
-			}
-			// LogOutput now is "console", if add Log.Debug here will print into stdout anyway.
-			// Log.Debug("ParseConfig use config file: %s", config)
-			break
-		}
-	}
-
-	err = readCmdFlags()
-	if err != nil {
-		Log.Error("ParseConfig readCmdFlags Error: %v", err)
-		return err
-	}
-
-	// parse blacklist & ignore blacklist file parse error
-	if _, e := os.Stat(Config.BlackList); e == nil {
-		var blFd *os.File
-		blFd, err = os.Open(Config.BlackList)
-		if err == nil {
-			bl := bufio.NewReader(blFd)
-			for {
-				rule, e := bl.ReadString('\n')
-				if e != nil {
-					break
-				}
-				rule = strings.TrimSpace(rule)
-				if strings.HasPrefix(rule, "#") || rule == "" {
-					continue
-				}
-				BlackList = append(BlackList, rule)
-			}
-		}
-		defer blFd.Close()
-	}
-	LoggerInit()
-	return err
-}
-
-// ReportType 元数据结构定义
-type ReportType struct {
-	Name        string `json:"Name"`
-	Description string `json:"Description"`
-	Example     string `json:"Example"`
-}
-
-// ReportTypes 命令行-report-type支持的形式
-var ReportTypes = []ReportType{
-	{
-		Name:        "lint",
-		Description: "参考sqlint格式,以插件形式集成到代码编辑器,显示输出更加友好",
-		Example:     `soar -report-type lint -query test.sql`,
-	},
-	{
-		Name:        "markdown",
-		Description: "该格式为默认输出格式,以markdown格式展现,可以用网页浏览器插件直接打开,也可以用markdown编辑器打开",
-		Example:     `echo "select * from film" | soar`,
-	},
-	{
-		Name:        "rewrite",
-		Description: "SQL重写功能,配合-rewrite-rules参数一起使用,可以通过-list-rewrite-rules 查看所有支持的 SQL 重写规则",
-		Example:     `echo "select * from film" | soar -rewrite-rules star2columns,delimiter -report-type rewrite`,
-	},
-	{
-		Name:        "ast",
-		Description: "输出 SQL 的抽象语法树,主要用于测试",
-		Example:     `echo "select * from film" | soar -report-type ast`,
-	},
-	{
-		Name:        "tiast",
-		Description: "输出 SQL 的 TiDB抽象语法树,主要用于测试",
-		Example:     `echo "select * from film" | soar -report-type tiast`,
-	},
-	{
-		Name:        "fingerprint",
-		Description: "输出SQL的指纹",
-		Example:     `echo "select * from film where language_id=1" | soar -report-type fingerprint`,
-	},
-	{
-		Name:        "md2html",
-		Description: "markdown 格式转 html 格式小工具",
-		Example:     `soar -list-heuristic-rules | soar -report-type md2html > heuristic_rules.html`,
-	},
-	{
-		Name:        "explain-digest",
-		Description: "输入为EXPLAIN的表格,JSON 或 Vertical格式,对其进行分析,给出分析结果",
-		Example: `soar -report-type explain-digest << EOF
-+----+-------------+-------+------+---------------+------+---------+------+------+-------+
-| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra |
-+----+-------------+-------+------+---------------+------+---------+------+------+-------+
-|  1 | SIMPLE      | film  | ALL  | NULL          | NULL | NULL    | NULL | 1131 |       |
-+----+-------------+-------+------+---------------+------+---------+------+------+-------+
-EOF`,
-	},
-	{
-		Name:        "duplicate-key-checker",
-		Description: "对 OnlineDsn 中指定的 database 进行索引重复检查",
-		Example:     `soar -report-type duplicate-key-checker -online-dsn user:password@127.0.0.1:3306/db`,
-	},
-	{
-		Name:        "html",
-		Description: "以HTML格式输出报表",
-		Example:     `echo "select * from film" | soar -report-type html`,
-	},
-	{
-		Name:        "json",
-		Description: "输出JSON格式报表,方便应用程序处理",
-		Example:     `echo "select * from film" | soar -report-type json`,
-	},
-	{
-		Name:        "tokenize",
-		Description: "对SQL进行切词,主要用于测试",
-		Example:     `echo "select * from film" | soar -report-type tokenize`,
-	},
-	{
-		Name:        "compress",
-		Description: "SQL压缩小工具,使用内置SQL压缩逻辑,测试中的功能",
-		Example: `echo "select
-*
-from
-  film" | soar -report-type compress`,
-	},
-	{
-		Name:        "pretty",
-		Description: "使用kr/pretty打印报告,主要用于测试",
-		Example:     `echo "select * from film" | soar -report-type pretty`,
-	},
-	{
-		Name:        "remove-comment",
-		Description: "去除SQL语句中的注释,支持单行多行注释的去除",
-		Example:     `echo "select/*comment*/ * from film" | soar -report-type remove-comment`,
-	},
-	{
-		Name:        "chardet",
-		Description: "猜测输入的 SQL 使用的字符集",
-		Example:     "echo '中文' | soar -report-type chardet",
-	},
-}
-
-// ListReportTypes 查看所有支持的report-type
-func ListReportTypes() {
-	switch Config.ReportType {
-	case "json":
-		js, err := json.MarshalIndent(ReportTypes, "", "  ")
-		if err == nil {
-			fmt.Println(string(js))
-		}
-	default:
-		fmt.Print("# 支持的报告类型\n\n[toc]\n\n")
-		for _, r := range ReportTypes {
-			fmt.Print("## ", MarkdownEscape(r.Name),
-				"\n* **Description**:", r.Description+"\n",
-				"\n* **Example**:\n\n```bash\n", r.Example, "\n```\n")
-		}
-	}
-}
-
-// ArgConfig get -config arg value from cli
-func ArgConfig() string {
-	var configFile string
-	if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "-config") {
-		if os.Args[1] == "-config" && len(os.Args) > 2 {
-			if os.Args[2] == "=" && len(os.Args) > 3 {
-				// -config = soar.yaml not support
-				fmt.Println("wrong format, no space between '=', eg: -config=soar.yaml")
-			} else {
-				// -config soar.yaml
-				configFile = os.Args[2]
-			}
-			if strings.HasPrefix(configFile, "=") {
-				// -config =soar.yaml
-				configFile = strings.Split(configFile, "=")[1]
-			}
-		}
-		if strings.Contains(os.Args[1], "=") {
-			// -config=soar.yaml
-			configFile = strings.Split(os.Args[1], "=")[1]
-		}
-	} else {
-		for i, c := range os.Args {
-			if strings.HasPrefix(c, "-config") && i != 1 {
-				fmt.Println("-config must be the first arg")
-			}
-		}
-	}
-	return configFile
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/config_test.go b/vendor/github.com/XiaoMi/soar/common/config_test.go
deleted file mode 100644
index 48f5a7a..0000000
--- a/vendor/github.com/XiaoMi/soar/common/config_test.go
+++ /dev/null
@@ -1,106 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"flag"
-	"os"
-	"testing"
-
-	"github.com/kr/pretty"
-)
-
-var update = flag.Bool("update", false, "update .golden files")
-
-func TestParseConfig(t *testing.T) {
-	err := ParseConfig("")
-	if err != nil {
-		t.Error("sqlparser.Parse Error:", err)
-	}
-}
-
-func TestReadConfigFile(t *testing.T) {
-	if Config == nil {
-		Config = new(Configuration)
-	}
-	Config.readConfigFile("../soar.yaml")
-}
-
-func TestParseDSN(t *testing.T) {
-	var dsns = []string{
-		"",
-		"user:password@hostname:3307/database",
-		"user:password@hostname:3307",
-		"user:password@hostname:/database",
-		"user:password@:3307/database",
-		"user:password@",
-		"hostname:3307/database",
-		"@hostname:3307/database",
-		"@hostname",
-		"hostname",
-		"@/database",
-		"@hostname:3307",
-		"@:3307/database",
-		":3307/database",
-		"/database",
-	}
-
-	GoldenDiff(func() {
-		for _, dsn := range dsns {
-			pretty.Println(parseDSN(dsn, nil))
-		}
-	}, t.Name(), update)
-}
-
-func TestListReportTypes(t *testing.T) {
-	err := GoldenDiff(func() { ListReportTypes() }, t.Name(), update)
-	if nil != err {
-		t.Fatal(err)
-	}
-}
-
-func TestArgConfig(t *testing.T) {
-	testArgs1 := [][]string{
-		{"soar", "-config", "=", "soar.yaml"},
-		{"soar", "-print-config", "-config", "soar.yaml"},
-	}
-	testArgs2 := [][]string{
-		{"soar", "-config", "soar.yaml"},
-		{"soar", "-config", "=soar.yaml"},
-		{"soar", "-config=soar.yaml"},
-	}
-	for _, args := range testArgs1 {
-		os.Args = args
-		configFile := ArgConfig()
-		if configFile != "" {
-			t.Errorf("should return '', but got %s", configFile)
-		}
-	}
-	for _, args := range testArgs2 {
-		os.Args = args
-		configFile := ArgConfig()
-		if configFile != "soar.yaml" {
-			t.Errorf("should return soar.yaml, but got %s", configFile)
-		}
-	}
-}
-
-func TestPrintConfiguration(t *testing.T) {
-	Config.Verbose = true
-	PrintConfiguration()
-
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/doc.go b/vendor/github.com/XiaoMi/soar/common/doc.go
deleted file mode 100644
index 95b0973..0000000
--- a/vendor/github.com/XiaoMi/soar/common/doc.go
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Package common contain many useful functions for logging, formatting and so on.
-package common
diff --git a/vendor/github.com/XiaoMi/soar/common/example_test.go b/vendor/github.com/XiaoMi/soar/common/example_test.go
deleted file mode 100644
index 8baeb71..0000000
--- a/vendor/github.com/XiaoMi/soar/common/example_test.go
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import "fmt"
-
-func ExampleFormatDSN() {
-	dsxExp := &dsn{
-		Addr:     "127.0.0.1:3306",
-		Schema:   "mysql",
-		User:     "root",
-		Password: "1t'sB1g3rt",
-		Charset:  "utf8mb4",
-		Disable:  false,
-	}
-
-	// 根据 &dsn 生成 dsnStr
-	fmt.Println(FormatDSN(dsxExp))
-
-	// Output: root:1t'sB1g3rt@127.0.0.1:3306/mysql?charset=utf8mb4
-}
-
-func ExampleIsColsPart() {
-	// IsColsPart() 会 按照顺序 检查两个Column队列是否是包含(或相等)关系。
-	a := []*Column{{Name: "1"}, {Name: "2"}, {Name: "3"}}
-	b := []*Column{{Name: "1"}, {Name: "2"}}
-	c := []*Column{{Name: "1"}, {Name: "3"}}
-	d := []*Column{{Name: "1"}, {Name: "2"}, {Name: "3"}, {Name: "4"}}
-
-	ab := IsColsPart(a, b)
-	ac := IsColsPart(a, c)
-	ad := IsColsPart(a, d)
-
-	fmt.Println(ab, ac, ad)
-	// Output: true false true
-}
-
-func ExampleSortedKey() {
-	ages := map[string]int{
-		"a": 1,
-		"c": 3,
-		"d": 4,
-		"b": 2,
-	}
-	for _, name := range SortedKey(ages) {
-		fmt.Print(ages[name])
-	}
-	// Output: 1234
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/logger.go b/vendor/github.com/XiaoMi/soar/common/logger.go
deleted file mode 100644
index 6323fca..0000000
--- a/vendor/github.com/XiaoMi/soar/common/logger.go
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"fmt"
-	"regexp"
-	"runtime"
-	"strings"
-
-	"github.com/astaxie/beego/logs"
-)
-
-// Log 使用 beego 的 log 库
-var Log *logs.BeeLogger
-
-// BaseDir 日志打印在binary的根路径
-var BaseDir string
-
-func init() {
-	Log = logs.NewLogger(0)
-	Log.EnableFuncCallDepth(true)
-}
-
-// LoggerInit Log配置初始化
-func LoggerInit() {
-	Log.SetLevel(Config.LogLevel)
-	func() { _ = Log.DelLogger(logs.AdapterFile) }()
-	err := Log.SetLogger(logs.AdapterFile, fmt.Sprintf(`{"filename":"%s","level":7,"maxlines":0,"maxsize":0,"daily":false,"maxdays":0}`, Config.LogOutput))
-	if err != nil {
-		fmt.Println(err.Error())
-	}
-}
-
-// Caller returns the caller of the function that called it :)
-// https://stackoverflow.com/questions/35212985/is-it-possible-get-information-about-caller-function-in-golang
-func Caller() string {
-	// we get the callers as uintptrs - but we just need 1
-	fpcs := make([]uintptr, 1)
-
-	// skip 3 levels to get to the caller of whoever called Caller()
-	n := runtime.Callers(3, fpcs)
-	if n == 0 {
-		return "n/a" // proper error her would be better
-	}
-
-	// get the info of the actual function that's in the pointer
-	fun := runtime.FuncForPC(fpcs[0] - 1)
-	if fun == nil {
-		return "n/a"
-	}
-
-	// return its name
-	return fun.Name()
-}
-
-// GetFunctionName 获取调当前函数名
-func GetFunctionName() string {
-	// Skip this function, and fetch the PC and file for its parent
-	pc, _, _, _ := runtime.Caller(1)
-	// Retrieve a Function object this functions parent
-	functionObject := runtime.FuncForPC(pc)
-	// Regex to extract just the function name (and not the module path)
-	extractFnName := regexp.MustCompile(`^.*\.(.*)$`)
-	fnName := extractFnName.ReplaceAllString(functionObject.Name(), "$1")
-	return fnName
-}
-
-// fileName get filename from path
-func fileName(original string) string {
-	i := strings.LastIndex(original, "/")
-	if i == -1 {
-		return original
-	}
-	return original[i+1:]
-}
-
-// LogIfError 简化if err != nil 打 Error 日志代码长度
-func LogIfError(err error, format string, v ...interface{}) {
-	if err != nil {
-		_, fn, line, _ := runtime.Caller(1)
-		if format == "" {
-			format = "[%s:%d] %s"
-			Log.Error(format, fileName(fn), line, err.Error())
-		} else {
-			format = "[%s:%d] " + format + " Error: %s"
-			Log.Error(format, fileName(fn), line, v, err.Error())
-		}
-	}
-}
-
-// LogIfWarn 简化if err != nil 打 Warn 日志代码长度
-func LogIfWarn(err error, format string, v ...interface{}) {
-	if err != nil {
-		_, fn, line, _ := runtime.Caller(1)
-		if format == "" {
-			format = "[%s:%d] %s"
-			Log.Warn(format, fileName(fn), line, err.Error())
-		} else {
-			format = "[%s:%d] " + format + " Error: %s"
-			Log.Warn(format, fileName(fn), line, v, err.Error())
-		}
-	}
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/logger_test.go b/vendor/github.com/XiaoMi/soar/common/logger_test.go
deleted file mode 100644
index 66e2706..0000000
--- a/vendor/github.com/XiaoMi/soar/common/logger_test.go
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"errors"
-	"testing"
-)
-
-func init() {
-	BaseDir = DevPath
-}
-
-func TestLogger(t *testing.T) {
-	Log.Info("info")
-	Log.Debug("debug")
-	Log.Warning("warning")
-	Log.Error("error")
-}
-
-func TestCaller(t *testing.T) {
-	caller := Caller()
-	if caller != "testing.tRunner" {
-		t.Error("get caller failed")
-	}
-}
-
-func TestGetFunctionName(t *testing.T) {
-	f := GetFunctionName()
-	if f != "TestGetFunctionName" {
-		t.Error("get functionname failed")
-	}
-}
-
-func TestIfError(t *testing.T) {
-	err := errors.New("test")
-	LogIfError(err, "")
-	LogIfError(err, "func %s", "func_test")
-}
-
-func TestIfWarn(t *testing.T) {
-	err := errors.New("test")
-	LogIfWarn(err, "")
-	LogIfWarn(err, "func %s", "func_test")
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/markdown.go b/vendor/github.com/XiaoMi/soar/common/markdown.go
deleted file mode 100644
index 55d91c2..0000000
--- a/vendor/github.com/XiaoMi/soar/common/markdown.go
+++ /dev/null
@@ -1,156 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"encoding/base64"
-	"fmt"
-	"io/ioutil"
-	"net/http"
-	"os"
-	"strings"
-
-	"github.com/russross/blackfriday"
-)
-
-// BuiltinCSS 内置HTML风格
-var BuiltinCSS = `
-a:link,a:visited{text-decoration:none}h3,h4{margin-top:2em}h5,h6{margin-top:20px}h3,h4,h5,h6{margin-bottom:.5em;color:#000}body,h1,h2,h3,h4,h5,h6{color:#000}ol,ul{margin:0 0 0 30px;padding:0 0 12px 6px}ol,ol ol{list-style-position:outside}table td p,table th p{margin-bottom:0}input,select{vertical-align:middle;padding:0}h5,h6,input,select{padding:0}hr,table,textarea{width:100%}body{margin:20px auto;width:800px;background-color:#fff;font:13px "Myriad Pro","Lucida Grande",Lucida,Verdana,sans-serif}h1,table th p{font-weight:700}a:link{color:#00f}a:visited{color:#00a}a:active,a:hover{color:#f60;text-decoration:underline}* html code,* html pre{font-size:101%}code,pre{font-size:11px;font-family:monaco,courier,consolas,monospace}pre{border:1px solid #c7cfd5;background:#f1f5f9;margin:20px 0;padding:8px;text-align:left}hr{color:#919699;size:1;noshade:"noshade"}h1,h2,h3,h4,h5,h6{font-family:"Myriad Pro","Lucida Grande",Lucida,Verdana,sans-serif;font-weight:700}h1{margin-top:1em;margin-bottom:25px;font-size:30px}h2{margin-top:2.5em;font-size:24px;padding-bottom:2px;border-bottom:1px solid #919699}h3{font-size:17px}h4{font-size:15px}h5{font-size:13px}h6{font-size:11px}table td,table th{font-size:12px;border-bottom:1px solid #919699;border-right:1px solid #919699}p{margin-top:0;margin-bottom:10px}ul{list-style:square}li{margin-top:7px}ol{list-style-type:decimal}ol ol{list-style-type:lower-alpha;margin:7px 0 0 30px;padding:0 0 0 10px}ul ul{margin-left:40px;padding:0 0 0 6px}li>p{display:inline}li>a+p,li>p+p{display:block}table{border-top:1px solid #919699;border-left:1px solid #919699;border-spacing:0}table th{padding:4px 8px;background:#E2E2E2}table td{padding:8px;vertical-align:top}table td p+p,table td p+p+p{margin-top:5px}form{margin:0}button{margin:3px 0 10px}input{margin:0 0 5px}select{margin:0 0 3px}textarea{margin:0 0 10px}
-`
-
-// BuiltinJavascript 内置 SQL 美化 Javascript 脚本
-var BuiltinJavascript = `IWZ1bmN0aW9uKGUsRSl7Im9iamVjdCI9PXR5cGVvZiBleHBvcnRzJiYib2JqZWN0Ij09dHlwZW9mIG1vZHVsZT9tb2R1bGUuZXhwb3J0cz1FKCk6ImZ1bmN0aW9uIj09dHlwZW9mIGRlZmluZSYmZGVmaW5lLmFtZD9kZWZpbmUoW10sRSk6Im9iamVjdCI9PXR5cGVvZiBleHBvcnRzP2V4cG9ydHMuc3FsRm9ybWF0dGVyPUUoKTplLnNxbEZvcm1hdHRlcj1FKCl9KHRoaXMsZnVuY3Rpb24oKXtyZXR1cm4gZnVuY3Rpb24oZSl7ZnVuY3Rpb24gRShuKXtpZih0W25dKXJldHVybiB0W25dLmV4cG9ydHM7dmFyIHI9dFtuXT17ZXhwb3J0czp7fSxpZDpuLGxvYWRlZDohMX07cmV0dXJuIGVbbl0uY2FsbChyLmV4cG9ydHMscixyLmV4cG9ydHMsRSksci5sb2FkZWQ9ITAsci5leHBvcnRzfXZhciB0PXt9O3JldHVybiBFLm09ZSxFLmM9dCxFLnA9IiIsRSgwKX0oW2Z1bmN0aW9uKGUsRSx0KXsidXNlIHN0cmljdCI7ZnVuY3Rpb24gbihlKXtyZXR1cm4gZSYmZS5fX2VzTW9kdWxlP2U6eyJkZWZhdWx0IjplfX1FLl9fZXNNb2R1bGU9ITA7dmFyIHI9dCgxOCksVD1uKHIpLFI9dCgxOSksbz1uKFIpLE49dCgyMCksQT1uKE4pLEk9dCgyMSksTz1uKEkpO0VbImRlZmF1bHQiXT17Zm9ybWF0O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gICAJICAgIGZvciAobGV0IGkgPSAwIDtpPGNvZGVMaXN0Lmxlbmd0aDtpKyspIHsKICAgIAkgICAgICAgIGNvZGVMaXN0W2ldLmlubmVySFRNTCA9IHdpbmRvdy5zcWxGb3JtYXR0ZXIuZm9ybWF0KGVzY2FwZTJIdG1sKGNvZGVMaXN0W2ldLmlubmVySFRNTCkpCiAgICAJICAgIH0KICAgIAl9Owo=
-`
-
-// MarkdownEscape markdown格式转义,原样输出
-func MarkdownEscape(str string) string {
-	for _, b := range "_`*" {
-		str = strings.Replace(str, string(b), "\\"+string(b), -1)
-	}
-	return str
-}
-
-//
-func loadExternalResource(resource string) string {
-	var content string
-	var body []byte
-	if strings.HasPrefix(resource, "http") {
-		resp, err := http.Get(resource)
-		if err == nil {
-			body, err = ioutil.ReadAll(resp.Body)
-			if err == nil {
-				content = string(body)
-			} else {
-				Log.Debug("ioutil.ReadAll %s Error: %v", resource, err)
-			}
-		} else {
-			Log.Debug("http.Get %s Error: %v", resource, err)
-		}
-		defer resp.Body.Close()
-	} else {
-		fd, err := os.Open(resource)
-		defer func() {
-			err = fd.Close()
-			if err != nil {
-				Log.Error("loadExternalResource(%s) fd.Close failed: %s", resource, err.Error())
-			}
-		}()
-		if err == nil {
-			body, err = ioutil.ReadAll(fd)
-			if err != nil {
-				Log.Debug("ioutil.ReadAll %s Error: %v", resource, err)
-			} else {
-				content = string(body)
-			}
-		} else {
-			Log.Debug("os.Open %s Error: %v", resource, err)
-		}
-	}
-	return content
-}
-
-// MarkdownHTMLHeader markdown 转 HTML 输出时添加 HTML 头
-func MarkdownHTMLHeader() string {
-	// load css
-	var css string
-	if Config.ReportCSS == "" {
-		css = BuiltinCSS
-	} else {
-		css = loadExternalResource(Config.ReportCSS)
-	}
-
-	// load javascript
-	var js string
-	if Config.ReportJavascript == "" {
-		decode, _ := base64.StdEncoding.DecodeString(BuiltinJavascript)
-		js = string(decode)
-	} else {
-		js = loadExternalResource(Config.ReportJavascript)
-	}
-
-	header := `
-
-` + Config.ReportTitle + `
-
-
-
-
-`
-	return header
-}
-
-// Markdown2HTML markdown 转 HTML 输出
-func Markdown2HTML(buf string) string {
-	// extensions default: 94
-	// extensions |= blackfriday.EXTENSION_TABLES
-	// extensions |= blackfriday.EXTENSION_FENCED_CODE
-	// extensions |= blackfriday.EXTENSION_AUTOLINK
-	// extensions |= blackfriday.EXTENSION_STRIKETHROUGH
-	// extensions |= blackfriday.EXTENSION_SPACE_HEADERS
-	extensions := Config.MarkdownExtensions
-
-	// htmlFlags
-	htmlFlags := Config.MarkdownHTMLFlags
-
-	renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")
-	buf = string(blackfriday.Markdown([]byte(buf), renderer, extensions))
-	return buf
-}
-
-// Score SQL评审打分
-func Score(score int) string {
-	// 不需要打分的功能
-	switch Config.ReportType {
-	case "duplicate-key-checker", "explain-digest":
-		return ""
-	}
-	s1, s2 := "★ ", "☆ "
-	if score > 100 {
-		score = 100
-		Log.Debug("Score Error: score larger than 100, %d", score)
-	}
-	if score < 0 {
-		score = 0
-		Log.Debug("Score Warn: score less than 0, %d", score)
-	}
-	s1Count := score / 20
-	s2Count := 5 - s1Count
-	str := fmt.Sprintf("%s %d分", strings.TrimSpace(strings.Repeat(s1, s1Count)+strings.Repeat(s2, s2Count)), score)
-	return str
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/markdown_test.go b/vendor/github.com/XiaoMi/soar/common/markdown_test.go
deleted file mode 100644
index 8788e0d..0000000
--- a/vendor/github.com/XiaoMi/soar/common/markdown_test.go
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"fmt"
-	"io"
-	"io/ioutil"
-	"os"
-	"path/filepath"
-	"testing"
-)
-
-func TestMarkdownEscape(_ *testing.T) {
-	var strs = []string{
-		"a`bc",
-		"abc",
-		"a'bc",
-		"a\"bc",
-	}
-	for _, str := range strs {
-		fmt.Println(MarkdownEscape(str))
-	}
-}
-
-func TestMarkdown2Html(t *testing.T) {
-	md := filepath.Join("testdata", t.Name()+".md")
-	buf, err := ioutil.ReadFile(md)
-	if err != nil {
-		t.Error(err.Error())
-	}
-	err = GoldenDiff(func() {
-		fmt.Println(Markdown2HTML(string(buf)))
-	}, t.Name(), update)
-	if nil != err {
-		t.Fatal(err)
-	}
-
-	// golden 文件拷贝成 html 文件,这步是给人看的
-	gd, err := os.OpenFile("testdata/"+t.Name()+".golden", os.O_RDONLY, 0666)
-	if nil != err {
-		t.Fatal(err)
-	}
-	html, err := os.OpenFile("testdata/"+t.Name()+".html", os.O_CREATE|os.O_RDWR, 0666)
-	if nil != err {
-		t.Fatal(err)
-	}
-	io.Copy(html, gd)
-}
-
-func TestScore(t *testing.T) {
-	score := Score(50)
-	if score != "★ ★ ☆ ☆ ☆ 50分" {
-		t.Error(score)
-	}
-}
-
-func TestLoadExternalResource(t *testing.T) {
-	buf := loadExternalResource("../doc/themes/github.css")
-	if buf == "" {
-		t.Error("loadExternalResource local error")
-	}
-	buf = loadExternalResource("http://www.baidu.com")
-	if buf == "" {
-		t.Error("loadExternalResource http error")
-	}
-}
-
-func TestMarkdownHTMLHeader(t *testing.T) {
-	err := GoldenDiff(func() {
-		MarkdownHTMLHeader()
-	}, t.Name(), update)
-	if err != nil {
-		t.Error(err)
-	}
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/meta.go b/vendor/github.com/XiaoMi/soar/common/meta.go
deleted file mode 100644
index 433704e..0000000
--- a/vendor/github.com/XiaoMi/soar/common/meta.go
+++ /dev/null
@@ -1,495 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"strconv"
-	"strings"
-)
-
-// Meta 以 'database' 为 key, DB 的 map, 按 db->table->column 组织的元数据
-type Meta map[string]*DB
-
-// DB 数据库相关的结构体
-type DB struct {
-	Name  string
-	Table map[string]*Table // ['table_name']*Table
-}
-
-// NewDB 用于初始化*DB
-func NewDB(db string) *DB {
-	return &DB{
-		Name:  db,
-		Table: make(map[string]*Table),
-	}
-}
-
-// Table 含有表的属性
-type Table struct {
-	TableName    string
-	TableAliases []string
-	Column       map[string]*Column
-}
-
-// NewTable 初始化*Table
-func NewTable(tb string) *Table {
-	return &Table{
-		TableName:    tb,
-		TableAliases: make([]string, 0),
-		Column:       make(map[string]*Column),
-	}
-}
-
-// KeyType 用于标志每个Key的类别
-type KeyType int
-
-// Column 含有列的定义属性
-type Column struct {
-	Name        string   `json:"col_name"`    // 列名
-	Alias       []string `json:"alias"`       // 别名
-	Table       string   `json:"tb_name"`     // 表名
-	DB          string   `json:"db_name"`     // 数据库名称
-	DataType    string   `json:"data_type"`   // 数据类型
-	Character   string   `json:"character"`   // 字符集
-	Collation   string   `json:"collation"`   // collation
-	Cardinality float64  `json:"cardinality"` // 散粒度
-	Null        string   `json:"null"`        // 是否为空: YES/NO
-	Key         string   `json:"key"`         // 键类型
-	Default     string   `json:"default"`     // 默认值
-	Extra       string   `json:"extra"`       // 其他
-	Comment     string   `json:"comment"`     // 备注
-	Privileges  string   `json:"privileges"`  // 权限
-}
-
-// TableColumns 这个结构体中的元素是有序的  map[db]map[table][]columns
-type TableColumns map[string]map[string][]*Column
-
-// Equal 判断两个column是否相等
-func (col *Column) Equal(column *Column) bool {
-	return col.Name == column.Name &&
-		col.Table == column.Table &&
-		col.DB == column.DB
-}
-
-// IsColsPart 判断两个column队列是否是包含关系(包括相等)
-func IsColsPart(a, b []*Column) bool {
-	times := len(a)
-	if len(b) < times {
-		times = len(b)
-	}
-
-	for i := 0; i < times; i++ {
-		if a[i].DB != b[i].DB || a[i].Table != b[i].Table || a[i].Name != b[i].Name {
-			return false
-		}
-	}
-
-	return true
-}
-
-// JoinColumnsName 将所有的列合并
-func JoinColumnsName(cols []*Column, sep string) string {
-	name := ""
-	for _, col := range cols {
-		name += col.Name + sep
-	}
-	return strings.Trim(name, sep)
-}
-
-// Tables 获取 Meta 中指定 db 的所有表名
-// Input:数据库名
-// Output:表名组成的list
-func (b Meta) Tables(db string) []string {
-	var result []string
-	if b[db] != nil {
-		for tb := range b[db].Table {
-			result = append(result, tb)
-		}
-
-	}
-	return result
-}
-
-// SetDefault 设置默认值
-func (b Meta) SetDefault(defaultDB string) Meta {
-	if defaultDB == "" {
-		return b
-	}
-
-	for db := range b {
-		if db == "" {
-			// 当获取到的 join 中的 DB 为空的时候,说明 SQL 未显示的指定 DB,即使用的是 rEnv 默认 DB, 需要将表合并到原 DB 中
-			if _, ok := b[defaultDB]; ok {
-				for tbName, table := range b[""].Table {
-					if _, ok := b[defaultDB].Table[tbName]; ok {
-						b[defaultDB].Table[tbName].TableAliases = append(
-							b[defaultDB].Table[tbName].TableAliases,
-							table.TableAliases...,
-						)
-						continue
-					}
-					b[defaultDB].Table[tbName] = table
-				}
-				delete(b, "")
-			}
-
-			// 如果没有出现DB指定不一致的情况,直接进行合并
-			b[defaultDB] = b[""]
-			delete(b, "")
-		}
-	}
-
-	return b
-}
-
-// MergeColumn 将使用到的列按 db->table 组织去重
-// 注意:Column 中的 db, table 信息可能为空,需要提前通过env环境补齐再调用该函数。
-// @input: 目标列list, 源列list(可以将多个源合并到一个目标列list)
-// @output: 合并后的列list
-func MergeColumn(dst []*Column, src ...*Column) []*Column {
-	var tmp []*Column
-	for _, newCol := range src {
-		if len(dst) == 0 {
-			tmp = append(tmp, newCol)
-			continue
-		}
-
-		has := false
-		for _, oldCol := range dst {
-			if (newCol.Name == oldCol.Name) && (newCol.Table == oldCol.Table) && (newCol.DB == oldCol.DB) {
-				has = true
-			}
-		}
-
-		if !has {
-			tmp = append(tmp, newCol)
-		}
-
-	}
-	return append(dst, tmp...)
-}
-
-// ColumnSort 通过散粒度对 colList 进行排序, 散粒度排序由大到小
-func ColumnSort(colList []*Column) []*Column {
-	// 使用冒泡排序保持相等情况下左右两边顺序不变
-	if len(colList) < 2 {
-		return colList
-	}
-
-	for i := 0; i < len(colList)-1; i++ {
-		for j := i + 1; j < len(colList); j++ {
-			if colList[i].Cardinality < colList[j].Cardinality {
-				colList[i], colList[j] = colList[j], colList[i]
-			}
-		}
-	}
-
-	return colList
-}
-
-// GetDataTypeBase 获取dataType中的数据类型,忽略长度
-func GetDataTypeBase(dataType string) string {
-	if i := strings.Index(dataType, "("); i > 0 {
-		return dataType[0:i]
-	}
-
-	return dataType
-}
-
-// GetDataTypeLength 获取dataType中的数据类型长度
-func GetDataTypeLength(dataType string) []int {
-	var length []int
-	if si := strings.Index(dataType, "("); si > 0 {
-		dataLength := dataType[si+1:]
-		if ei := strings.Index(dataLength, ")"); ei > 0 {
-			dataLength = dataLength[:ei]
-			for _, l := range strings.Split(dataLength, ",") {
-				v, err := strconv.Atoi(l)
-				if err != nil {
-					Log.Debug("GetDataTypeLength() Error: %v", err)
-					return []int{-1}
-				}
-				length = append(length, v)
-			}
-		}
-	}
-
-	if len(length) == 0 {
-		length = []int{-1}
-	}
-
-	return length
-}
-
-// GetDataBytes 计算数据类型字节数
-// https://dev.mysql.com/doc/refman/8.0/en/storage-requirements.html
-// return -1 表示该列无法计算数据大小
-func (col *Column) GetDataBytes(dbVersion int) int {
-	if col.DataType == "" {
-		Log.Warning("Can't get %s.%s data type", col.Table, col.Name)
-		return -1
-	}
-	switch strings.ToLower(GetDataTypeBase(col.DataType)) {
-	case "tinyint", "smallint", "mediumint",
-		"int", "integer", "bigint",
-		"double", "real", "float", "decimal",
-		"numeric", "bit":
-		// numeric
-		return numericStorageReq(col.DataType)
-
-	case "year", "date", "time", "datetime", "timestamp":
-		// date & time
-		return timeStorageReq(col.DataType, dbVersion)
-
-	case "char", "binary", "varchar", "varbinary", "enum", "set":
-		// string
-		return StringStorageReq(col.DataType, col.Character)
-	case "tinyblob", "tinytext", "blob", "text", "mediumblob", "mediumtext",
-		"longblob", "longtext":
-		// strings length depend on it's values
-		// 这些字段为不定长字段,添加索引时必须指定前缀,索引前缀与字符集相关
-		return Config.MaxIdxBytesPerColumn + 1
-	default:
-		Log.Warning("Type %s not support:", col.DataType)
-		return -1
-	}
-}
-
-// Numeric Type Storage Requirements
-// return bytes count
-func numericStorageReq(dataType string) int {
-	typeLength := GetDataTypeLength(dataType)
-	baseType := strings.ToLower(GetDataTypeBase(dataType))
-
-	switch baseType {
-	case "tinyint":
-		return 1
-	case "smallint":
-		return 2
-	case "mediumint":
-		return 3
-	case "int", "integer":
-		return 4
-	case "bigint", "double", "real":
-		return 8
-	case "float":
-		if typeLength[0] == -1 || typeLength[0] >= 0 && typeLength[0] <= 24 {
-			// 4 bytes if 0 <= p <= 24
-			return 4
-		}
-		// 8 bytes if no p || 25 <= p <= 53
-		return 8
-	case "decimal", "numeric":
-		// Values for DECIMAL (and NUMERIC) columns are represented using a binary format
-		// that packs nine decimal (base 10) digits into four bytes. Storage for the integer
-		// and fractional parts of each value are determined separately. Each multiple of nine
-		// digits requires four bytes, and the “leftover” digits require some fraction of four bytes.
-
-		if typeLength[0] == -1 {
-			return 4
-		}
-
-		leftover := func(leftover int) int {
-			if leftover > 0 && leftover <= 2 {
-				return 1
-			} else if leftover > 2 && leftover <= 4 {
-				return 2
-			} else if leftover > 4 && leftover <= 6 {
-				return 3
-			} else if leftover > 6 && leftover <= 8 {
-				return 4
-			} else {
-				return 4
-			}
-		}
-
-		integer := typeLength[0]/9*4 + leftover(typeLength[0]%9)
-		fractional := typeLength[1]/9*4 + leftover(typeLength[1]%9)
-
-		return integer + fractional
-
-	case "bit":
-		// approximately (M+7)/8 bytes
-		if typeLength[0] == -1 {
-			return 1
-		}
-		return (typeLength[0] + 7) / 8
-
-	default:
-		Log.Error("No such numeric type: %s", baseType)
-		return 8
-	}
-}
-
-// Date and Time Type Storage Requirements
-// return bytes count
-func timeStorageReq(dataType string, version int) int {
-	/*
-			https://dev.mysql.com/doc/refman/8.0/en/storage-requirements.html
-			*   ============================================================================================
-			*   |	Data Type |	Storage Required Before MySQL 5.6.4	| Storage Required as of MySQL 5.6.4   |
-			*   | ---------------------------------------------------------------------------------------- |
-			*   |	YEAR	  |	1 byte	                            | 1 byte                               |
-			*   |	DATE	  | 3 bytes	                            | 3 bytes                              |
-			*   |	TIME	  | 3 bytes	                            | 3 bytes + fractional seconds storage |
-			*   |	DATETIME  | 8 bytes	                            | 5 bytes + fractional seconds storage |
-			*   |	TIMESTAMP |	4 bytes	                            | 4 bytes + fractional seconds storage |
-			*   ============================================================================================
-			*	|  Fractional Seconds Precision |Storage Required  |
-			*   | ------------------------------------------------ |
-			*	|  0	    					|0 bytes		   |
-			*	|  1, 2						    |1 byte            |
-			*	|  3, 4						    |2 bytes           |
-			*	|  5, 6						    |3 bytes           |
-		    *   ====================================================
-	*/
-
-	typeLength := GetDataTypeLength(dataType)
-
-	extr := func(length int) int {
-		if length > 0 && length <= 2 {
-			return 1
-		} else if length > 2 && length <= 4 {
-			return 2
-		} else if length > 4 && length <= 6 || length > 6 {
-			return 3
-		}
-		return 0
-	}
-
-	switch strings.ToLower(GetDataTypeBase(dataType)) {
-	case "year":
-		return 1
-	case "date":
-		return 3
-	case "time":
-		if version < 50604 {
-			return 3
-		}
-		// 3 bytes + fractional seconds storage
-		return 3 + extr(typeLength[0])
-	case "datetime":
-		if version < 50604 {
-			return 8
-		}
-		// 5 bytes + fractional seconds storage
-		return 5 + extr(typeLength[0])
-	case "timestamp":
-		if version < 50604 {
-			return 4
-		}
-		// 4 bytes + fractional seconds storage
-		return 4 + extr(typeLength[0])
-	default:
-		return 8
-	}
-}
-
-// SHOW CHARACTER SET
-
-// CharSets character bytes per charcharacter bytes per char
-var CharSets = map[string]int{
-	"armscii8": 1,
-	"ascii":    1,
-	"big5":     2,
-	"binary":   1,
-	"cp1250":   1,
-	"cp1251":   1,
-	"cp1256":   1,
-	"cp1257":   1,
-	"cp850":    1,
-	"cp852":    1,
-	"cp866":    1,
-	"cp932":    2,
-	"dec8":     1,
-	"eucjpms":  3,
-	"euckr":    2,
-	"gb18030":  4,
-	"gb2312":   2,
-	"gbk":      2,
-	"geostd8":  1,
-	"greek":    1,
-	"hebrew":   1,
-	"hp8":      1,
-	"keybcs2":  1,
-	"koi8r":    1,
-	"koi8u":    1,
-	"latin1":   1,
-	"latin2":   1,
-	"latin5":   1,
-	"latin7":   1,
-	"macce":    1,
-	"macroman": 1,
-	"sjis":     2,
-	"swe7":     1,
-	"tis620":   1,
-	"ucs2":     2,
-	"ujis":     3,
-	"utf16":    4,
-	"utf16le":  4,
-	"utf32":    4,
-	"utf8":     3,
-	"utf8mb4":  4,
-}
-
-// StringStorageReq String Type Storage Requirements return bytes count
-func StringStorageReq(dataType string, charset string) int {
-	// get bytes per character, default 1
-	bysPerChar := 1
-	if _, ok := CharSets[strings.ToLower(charset)]; ok {
-		bysPerChar = CharSets[strings.ToLower(charset)]
-	}
-
-	// get length
-	typeLength := GetDataTypeLength(dataType)
-	if typeLength[0] == -1 {
-		return 0
-	}
-
-	// get type
-	baseType := strings.ToLower(GetDataTypeBase(dataType))
-
-	switch baseType {
-	case "char":
-		// Otherwise, M × w bytes, <= M <= 255,
-		// where w is the number of bytes required for the maximum-length character in the character set.
-		if typeLength[0] > 255 {
-			typeLength[0] = 255
-		}
-		return typeLength[0] * bysPerChar
-	case "binary":
-		// M bytes, 0 <= M <= 255
-		if typeLength[0] > 255 {
-			typeLength[0] = 255
-		}
-		return typeLength[0]
-	case "varchar", "varbinary":
-		if typeLength[0] < 255 {
-			return typeLength[0]*bysPerChar + 1
-		}
-		return typeLength[0]*bysPerChar + 2
-
-	case "enum":
-		// 1 or 2 bytes, depending on the number of enumeration values (65,535 values maximum)
-		return 2
-	case "set":
-		// 1, 2, 3, 4, or 8 bytes, depending on the number of set members (64 members maximum)
-		return 8
-	default:
-		return 0
-	}
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/meta_test.go b/vendor/github.com/XiaoMi/soar/common/meta_test.go
deleted file mode 100644
index c6e8080..0000000
--- a/vendor/github.com/XiaoMi/soar/common/meta_test.go
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"testing"
-)
-
-func TestGetDataTypeLength(t *testing.T) {
-	typeList := map[string][]int{
-		"varchar(20)":  {20},
-		"int(2)":       {2},
-		"int(2000000)": {2000000},
-		"DECIMAL(1,2)": {1, 2},
-		"int":          {-1},
-	}
-
-	for typ, want := range typeList {
-		got := GetDataTypeLength(typ)
-		for i := 0; i < len(want); i++ {
-			if want[i] != got[i] {
-				t.Errorf("Not match, want %v, got %v", want, got)
-			}
-		}
-	}
-
-}
-
-func TestGetDataTypeBase(t *testing.T) {
-	typeList := map[string]string{
-		"varchar(20)":  "varchar",
-		"int(2)":       "int",
-		"int(2000000)": "int",
-	}
-
-	for typ := range typeList {
-		if got := GetDataTypeBase(typ); got != typeList[typ] {
-			t.Errorf("Not match, want %s, got %s", typeList[typ], got)
-		}
-	}
-
-}
-
-func TestGetDataBytes(t *testing.T) {
-	cols50604 := map[*Column]int{
-		// numeric type
-		{Name: "col000", DataType: "tinyint", Character: "utf8"}:        1,
-		{Name: "col001", DataType: "SMALLINT", Character: "utf8"}:       2,
-		{Name: "col002", DataType: "MEDIUMINT", Character: "utf8"}:      3,
-		{Name: "col003", DataType: "int(32)", Character: "utf8"}:        4,
-		{Name: "col004", DataType: "integer(32)", Character: "utf8"}:    4,
-		{Name: "col005", DataType: "bigint(10)", Character: "utf8"}:     8,
-		{Name: "col006", DataType: "float(12)", Character: "utf8"}:      4,
-		{Name: "col007", DataType: "float(50)", Character: "utf8"}:      8,
-		{Name: "col008", DataType: "float(100)", Character: "utf8"}:     8,
-		{Name: "col009", DataType: "float", Character: "utf8"}:          4,
-		{Name: "col010", DataType: "double", Character: "utf8"}:         8,
-		{Name: "col011", DataType: "real", Character: "utf8"}:           8,
-		{Name: "col012", DataType: "BIT(32)", Character: "utf8"}:        4,
-		{Name: "col013", DataType: "numeric(32,32)", Character: "utf8"}: 30,
-		{Name: "col013", DataType: "decimal(2,32)", Character: "utf8"}:  16,
-		{Name: "col014", DataType: "BIT(32)", Character: "utf8"}:        4,
-
-		// date & time
-		{Name: "col015", DataType: "year(32)", Character: "utf8mb4"}:      1,
-		{Name: "col016", DataType: "date", Character: "utf8mb4"}:          3,
-		{Name: "col017", DataType: "time", Character: "utf8mb4"}:          3,
-		{Name: "col018", DataType: "time(0)", Character: "utf8mb4"}:       3,
-		{Name: "col019", DataType: "time(2)", Character: "utf8mb4"}:       4,
-		{Name: "col020", DataType: "time(4)", Character: "utf8mb4"}:       5,
-		{Name: "col021", DataType: "time(6)", Character: "utf8mb4"}:       6,
-		{Name: "col022", DataType: "datetime", Character: "utf8mb4"}:      5,
-		{Name: "col023", DataType: "timestamp(32)", Character: "utf8mb4"}: 7,
-
-		// string
-		{Name: "col024", DataType: "varchar(255)", Character: "utf8"}:    767,
-		{Name: "col025", DataType: "varchar(191)", Character: "utf8mb4"}: 765,
-	}
-
-	for col, bytes := range cols50604 {
-		if got := col.GetDataBytes(50604); got != bytes {
-			t.Errorf("Version 5.6.4, %s Not match, want %d, got %d", col.Name, bytes, got)
-		}
-	}
-
-	cols50500 := map[*Column]int{
-		// numeric type
-		{Name: "col000", DataType: "tinyint", Character: "utf8"}:        1,
-		{Name: "col001", DataType: "SMALLINT", Character: "utf8"}:       2,
-		{Name: "col002", DataType: "MEDIUMINT", Character: "utf8"}:      3,
-		{Name: "col003", DataType: "int(32)", Character: "utf8"}:        4,
-		{Name: "col004", DataType: "integer(32)", Character: "utf8"}:    4,
-		{Name: "col005", DataType: "bigint(10)", Character: "utf8"}:     8,
-		{Name: "col006", DataType: "float(12)", Character: "utf8"}:      4,
-		{Name: "col007", DataType: "float(50)", Character: "utf8"}:      8,
-		{Name: "col008", DataType: "float(100)", Character: "utf8"}:     8,
-		{Name: "col009", DataType: "float", Character: "utf8"}:          4,
-		{Name: "col010", DataType: "double", Character: "utf8"}:         8,
-		{Name: "col011", DataType: "real", Character: "utf8"}:           8,
-		{Name: "col012", DataType: "BIT(32)", Character: "utf8"}:        4,
-		{Name: "col013", DataType: "numeric(32,32)", Character: "utf8"}: 30,
-		{Name: "col013", DataType: "decimal(2,32)", Character: "utf8"}:  16,
-		{Name: "col014", DataType: "BIT(32)", Character: "utf8"}:        4,
-
-		// date & time
-		{Name: "col015", DataType: "year(32)", Character: "utf8mb4"}:      1,
-		{Name: "col016", DataType: "date", Character: "utf8mb4"}:          3,
-		{Name: "col017", DataType: "time", Character: "utf8mb4"}:          3,
-		{Name: "col018", DataType: "time(0)", Character: "utf8mb4"}:       3,
-		{Name: "col019", DataType: "time(2)", Character: "utf8mb4"}:       3,
-		{Name: "col020", DataType: "time(4)", Character: "utf8mb4"}:       3,
-		{Name: "col021", DataType: "time(6)", Character: "utf8mb4"}:       3,
-		{Name: "col022", DataType: "datetime", Character: "utf8mb4"}:      8,
-		{Name: "col023", DataType: "timestamp(32)", Character: "utf8mb4"}: 4,
-
-		// string
-		{Name: "col024", DataType: "varchar(255)", Character: "utf8"}:    767,
-		{Name: "col025", DataType: "varchar(191)", Character: "utf8mb4"}: 765,
-	}
-
-	for col, bytes := range cols50500 {
-		if got := col.GetDataBytes(50500); got != bytes {
-			t.Errorf("Version: 5.5.0, %s Not match, want %d, got %d", col.Name, bytes, got)
-		}
-	}
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/signal.go b/vendor/github.com/XiaoMi/soar/common/signal.go
deleted file mode 100644
index 79ae5ca..0000000
--- a/vendor/github.com/XiaoMi/soar/common/signal.go
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"os"
-	"os/signal"
-	"syscall"
-)
-
-// HandleSignal 当程序卡死的时候,或者由于某些原因程序没有退出,可以通过捕获信号量的形式让程序优雅退出并且清理测试环境
-func HandleSignal(f func()) {
-	sc := make(chan os.Signal, 1)
-	signal.Notify(sc,
-		syscall.SIGHUP,
-		syscall.SIGINT,
-		syscall.SIGTERM,
-		syscall.SIGQUIT)
-
-	go func() {
-		n := <-sc
-		Log.Info("receive signal %v, closing", n)
-		f()
-	}()
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/signal_test.go b/vendor/github.com/XiaoMi/soar/common/signal_test.go
deleted file mode 100644
index 853a2fa..0000000
--- a/vendor/github.com/XiaoMi/soar/common/signal_test.go
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"fmt"
-	"testing"
-)
-
-func TestHandleSignal(t *testing.T) {
-	HandleSignal(func() {
-		fmt.Println("done")
-	})
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/tricks.go b/vendor/github.com/XiaoMi/soar/common/tricks.go
deleted file mode 100644
index 9e89a57..0000000
--- a/vendor/github.com/XiaoMi/soar/common/tricks.go
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package common
-
-import (
-	"bufio"
-	"bytes"
-	"fmt"
-	"io"
-	"io/ioutil"
-	"os"
-	"path/filepath"
-	"reflect"
-	"sort"
-)
-
-// GoldenDiff 从 gofmt 学来的测试方法
-// https://medium.com/soon-london/testing-with-golden-files-in-go-7fccc71c43d3
-func GoldenDiff(f func(), name string, update *bool) error {
-	var b bytes.Buffer
-	w := bufio.NewWriter(&b)
-	str := captureOutput(f)
-	_, err := w.WriteString(str)
-	if err != nil {
-		Log.Warning(err.Error())
-	}
-	err = w.Flush()
-	if err != nil {
-		Log.Warning(err.Error())
-	}
-
-	gp := filepath.Join("testdata", name+".golden")
-	if *update {
-		if err = ioutil.WriteFile(gp, b.Bytes(), 0644); err != nil {
-			err = fmt.Errorf("%s failed to update golden file: %s", name, err)
-			return err
-		}
-	}
-	g, err := ioutil.ReadFile(gp)
-	if err != nil {
-		err = fmt.Errorf("%s failed reading .golden: %s", name, err)
-	}
-	if !bytes.Equal(b.Bytes(), g) {
-		err = fmt.Errorf("%s does not match .golden file", name)
-	}
-	return err
-}
-
-// captureOutput 获取函数标准输出
-func captureOutput(f func()) string {
-	// keep backup of the real stdout
-	oldStdout := os.Stdout
-	r, w, _ := os.Pipe()
-	os.Stdout = w
-
-	// execute function
-	f()
-
-	outC := make(chan string)
-	// copy the output in a separate goroutine so printing can't block indefinitely
-	go func() {
-		var buf bytes.Buffer
-		_, err := io.Copy(&buf, r)
-		if err != nil {
-			Log.Warning(err.Error())
-		}
-		outC <- buf.String()
-	}()
-
-	// back to normal state
-	err := w.Close()
-	if err != nil {
-		Log.Warning(err.Error())
-	}
-	os.Stdout = oldStdout // restoring the real stdout
-	out := <-outC
-	os.Stdout = oldStdout
-	return out
-}
-
-// SortedKey sort map[string]interface{}, use in range clause
-func SortedKey(m interface{}) []string {
-	var keys []string
-	switch reflect.TypeOf(m).Kind() {
-	case reflect.Map:
-		switch reflect.TypeOf(m).Key().Kind() {
-		case reflect.String:
-			for _, k := range reflect.ValueOf(m).MapKeys() {
-				keys = append(keys, k.String())
-			}
-		}
-	}
-	sort.Strings(keys)
-	return keys
-}
diff --git a/vendor/github.com/XiaoMi/soar/common/version.go b/vendor/github.com/XiaoMi/soar/common/version.go
deleted file mode 100644
index d26a3b8..0000000
--- a/vendor/github.com/XiaoMi/soar/common/version.go
+++ /dev/null
@@ -1,10 +0,0 @@
-package common
-
-// -version输出信息
-const (
-	Version  = "2018-11-27 09:12:27 +0800 v0.8.1-112-gbbf012d"
-	Compile  = "2018-11-27 11:33:40 +0800 by go version go1.10.4 darwin/amd64"
-	Branch   = "master"
-	GitDirty = 35486
-	DevPath  = "/Users/lpx/code/gopath/src/github.com/XiaoMi/soar"
-)
diff --git a/vendor/github.com/XiaoMi/soar/database/doc.go b/vendor/github.com/XiaoMi/soar/database/doc.go
deleted file mode 100644
index 9deb6a0..0000000
--- a/vendor/github.com/XiaoMi/soar/database/doc.go
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Package database will take cover of communicate with mysql database.
-package database
diff --git a/vendor/github.com/XiaoMi/soar/database/explain.go b/vendor/github.com/XiaoMi/soar/database/explain.go
deleted file mode 100644
index 0e17e64..0000000
--- a/vendor/github.com/XiaoMi/soar/database/explain.go
+++ /dev/null
@@ -1,1095 +0,0 @@
-/*
- * Copyright 2018 Xiaomi, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package database
-
-import (
-	"encoding/json"
-	"errors"
-	"fmt"
-	"regexp"
-	"runtime"
-	"strconv"
-	"strings"
-
-	"github.com/XiaoMi/soar/ast"
-	"github.com/XiaoMi/soar/common"
-
-	tidb "github.com/pingcap/parser/ast"
-	"github.com/tidwall/gjson"
-	"vitess.io/vitess/go/vt/sqlparser"
-)
-
-// format_type 支持的输出格式
-// https://dev.mysql.com/doc/refman/5.7/en/explain-output.html
-const (
-	TraditionalFormatExplain = iota // 默认输出
-	JSONFormatExplain               // JSON格式输出
-)
-
-// ExplainFormatType EXPLAIN 支持的 FORMAT_TYPE
-var ExplainFormatType = map[string]int{
-	"traditional": 0,
-	"json":        1,
-}
-
-// explain_type
-const (
-	TraditionalExplainType = iota // 默认转出
-	ExtendedExplainType           // EXTENDED输出
-	PartitionsExplainType         // PARTITIONS输出
-)
-
-// ExplainType EXPLAIN命令支持的参数
-var ExplainType = map[string]int{
-	"traditional": 0,
-	"extended":    1,
-	"partitions":  2,
-}
-
-// 为TraditionalFormatExplain准备的结构体 { start
-
-// ExplainInfo 用于存放Explain信息
-type ExplainInfo struct {
-	SQL           string
-	ExplainFormat int
-	ExplainRows   []*ExplainRow
-	ExplainJSON   *ExplainJSON
-	Warnings      []*ExplainWarning
-	QueryCost     float64
-}
-
-// ExplainRow 单行Explain
-type ExplainRow struct {
-	ID           int
-	SelectType   string
-	TableName    string
-	Partitions   string // explain partitions
-	AccessType   string
-	PossibleKeys []string
-	Key          string
-	KeyLen       string // 索引长度,如果发生了index_merge, KeyLen 格式为 N,N,所以不能定义为整型
-	Ref          []string
-	Rows         int
-	Filtered     float64 // 5.6 JSON, 5.7+, 5.5 EXTENDED
-	Scalability  string  // O(1), O(n), O(log n), O(log n)+
-	Extra        string
-}
-
-// ExplainWarning explain extended 后 SHOW WARNINGS 输出的结果
-type ExplainWarning struct {
-	Level   string
-	Code    int
-	Message string
-}
-
-// 为TraditionalFormatExplain准备的结构体 end }
-
-// 为JSONFormatExplain准备的结构体 { start
-
-// ExplainJSONCostInfo JSON
-type ExplainJSONCostInfo struct {
-	ReadCost        string `json:"read_cost"`
-	EvalCost        string `json:"eval_cost"`
-	PrefixCost      string `json:"prefix_cost"`
-	DataReadPerJoin string `json:"data_read_per_join"`
-	QueryCost       string `json:"query_cost"`
-	SortCost        string `json:"sort_cost"`
-}
-
-// ExplainJSONMaterializedFromSubquery JSON
-type ExplainJSONMaterializedFromSubquery struct {
-	UsingTemporaryTable bool                   `json:"using_temporary_table"`
-	Dependent           bool                   `json:"dependent"`
-	Cacheable           bool                   `json:"cacheable"`
-	QueryBlock          *ExplainJSONQueryBlock `json:"query_block"`
-}
-
-// 该变量用于存放 JSON 到 Traditional 模式的所有 ExplainJSONTable
-var explainJSONTables []*ExplainJSONTable
-
-// ExplainJSONTable JSON
-type ExplainJSONTable struct {
-	TableName                string                              `json:"table_name"`
-	AccessType               string                              `json:"access_type"`
-	PossibleKeys             []string                            `json:"possible_keys"`
-	Key                      string                              `json:"key"`
-	UsedKeyParts             []string                            `json:"used_key_parts"`
-	KeyLength                string                              `json:"key_length"`
-	Ref                      []string                            `json:"ref"`
-	RowsExaminedPerScan      int                                 `json:"rows_examined_per_scan"`
-	RowsProducedPerJoin      int                                 `json:"rows_produced_per_join"`
-	Filtered                 string                              `json:"filtered"`
-	UsingIndex               bool                                `json:"using_index"`
-	UsingIndexForGroupBy     bool                                `json:"using_index_for_group_by"`
-	CostInfo                 ExplainJSONCostInfo                 `json:"cost_info"`
-	UsedColumns              []string                            `json:"used_columns"`
-	AttachedCondition        string                              `json:"attached_condition"`
-	AttachedSubqueries       []ExplainJSONSubqueries             `json:"attached_subqueries"`
-	MaterializedFromSubquery ExplainJSONMaterializedFromSubquery `json:"materialized_from_subquery"`
-}
-
-// ExplainJSONNestedLoop JSON
-type ExplainJSONNestedLoop struct {
-	Table ExplainJSONTable `json:"table"`
-}
-
-// ExplainJSONBufferResult JSON
-type ExplainJSONBufferResult struct {
-	UsingTemporaryTable bool                    `json:"using_temporary_table"`
-	NestedLoop          []ExplainJSONNestedLoop `json:"nested_loop"`
-}
-
-// ExplainJSONSubqueries JSON
-type ExplainJSONSubqueries struct {
-	Dependent  bool                  `json:"dependent"`
-	Cacheable  bool                  `json:"cacheable"`
-	QueryBlock ExplainJSONQueryBlock `json:"query_block"`
-}
-
-// ExplainJSONGroupingOperation JSON
-type ExplainJSONGroupingOperation struct {
-	UsingTemporaryTable bool                    `json:"using_temporary_table"`
-	UsingFilesort       bool                    `json:"using_filesort"`
-	Table               ExplainJSONTable        `json:"table"`
-	CostInfo            ExplainJSONCostInfo     `json:"cost_info"`
-	NestedLoop          []ExplainJSONNestedLoop `json:"nested_loop"`
-	GroupBySubqueries   []ExplainJSONSubqueries `json:"group_by_subqueries"`
-}
-
-// ExplainJSONDuplicatesRemoval JSON
-type ExplainJSONDuplicatesRemoval struct {
-	UsingTemporaryTable bool                         `json:"using_temporary_table"`
-	UsingFilesort       bool                         `json:"using_filesort"`
-	BufferResult        ExplainJSONBufferResult      `json:"buffer_result"`
-	GroupingOperation   ExplainJSONGroupingOperation `json:"grouping_operation"`
-}
-
-// ExplainJSONOrderingOperation JSON
-type ExplainJSONOrderingOperation struct {
-	UsingFilesort     bool                         `json:"using_filesort"`
-	Table             ExplainJSONTable             `json:"table"`
-	DuplicatesRemoval ExplainJSONDuplicatesRemoval `json:"duplicates_removal"`
-	GroupingOperation ExplainJSONGroupingOperation `json:"grouping_operation"`
-	OrderbySubqueries []ExplainJSONSubqueries      `json:"order_by_subqueries"`
-}
-
-// ExplainJSONQueryBlock JSON
-type ExplainJSONQueryBlock struct {
-	SelectID                int                          `json:"select_id"`
-	CostInfo                ExplainJSONCostInfo          `json:"cost_info"`
-	Table                   ExplainJSONTable             `json:"table"`
-	NestedLoop              []ExplainJSONNestedLoop      `json:"nested_loop"`
-	OrderingOperation       ExplainJSONOrderingOperation `json:"ordering_operation"`
-	GroupingOperation       ExplainJSONGroupingOperation `json:"grouping_operation"`
-	OptimizedAwaySubqueries []ExplainJSONSubqueries      `json:"optimized_away_subqueries"`
-	HavingSubqueries        []ExplainJSONSubqueries      `json:"having_subqueries"`
-	SelectListSubqueries    []ExplainJSONSubqueries      `json:"select_list_subqueries"`
-	UpdateValueSubqueries   []ExplainJSONSubqueries      `json:"update_value_subqueries"`
-	QuerySpecifications     []ExplainJSONSubqueries      `json:"query_specifications"`
-	UnionResult             ExplainJSONUnionResult       `json:"union_result"`
-	Message                 string                       `json:"message"`
-}
-
-// ExplainJSONUnionResult JSON
-type ExplainJSONUnionResult struct {
-	UsingTemporaryTable bool                    `json:"using_temporary_table"`
-	TableName           string                  `json:"table_name"`
-	AccessType          string                  `json:"access_type"`
-	QuerySpecifications []ExplainJSONSubqueries `json:"query_specifications"`
-}
-
-// ExplainJSON 根结点
-type ExplainJSON struct {
-	QueryBlock ExplainJSONQueryBlock `json:"query_block"`
-}
-
-// 为JSONFormatExplain准备的结构体 end }
-
-// ExplainKeyWords 需要解释的关键字
-var ExplainKeyWords = []string{
-	"access_type",
-	"attached_condition",
-	"attached_subqueries",
-	"buffer_result",
-	"cacheable",
-	"cost_info",
-	"data_read_per_join",
-	"dependent",
-	"duplicates_removal",
-	"eval_cost",
-	"filtered",
-	"group_by_subqueries",
-	"grouping_operation",
-	"having_subqueries",
-	"key",
-	"key_length",
-	"materialized_from_subquery",
-	"message",
-	"nested_loop",
-	"optimized_away_subqueries",
-	"order_by_subqueries",
-	"ordering_operation",
-	"possible_keys",
-	"prefix_cost",
-	"query_block",
-	"query_cost",
-	"query_specifications",
-	"read_cost",
-	"ref",
-	"rows_examined_per_scan",
-	"rows_produced_per_join",
-	"select_id",
-	"select_list_subqueries",
-	"sort_cost",
-	"table",
-	"table_name",
-	"union_result",
-	"update_value_subqueries",
-	"used_columns",
-	"used_key_parts",
-	"using_filesort",
-	"using_index",
-	"using_index_for_group_by",
-	"using_temporary_table",
-}
-
-// ExplainColumnIndent EXPLAIN表头
-var ExplainColumnIndent = map[string]string{
-	"id":            "id为SELECT的标识符. 它是在SELECT查询中的顺序编号. 如果这一行表示其他行的union结果, 这个值可以为空. 在这种情况下, table列会显示为形如, 表示它是id为M和N的查询行的联合结果.",
-	"select_type":   "表示查询的类型. ",
-	"table":         "输出行所引用的表.",
-	"type":          "type显示连接使用的类型, 有关不同类型的描述, 请参见解释连接类型.",
-	"possible_keys": "指出MySQL能在该表中使用哪些索引有助于查询. 如果为空, 说明没有可用的索引.",
-	"key":           "MySQL实际从possible_keys选择使用的索引. 如果为NULL, 则没有使用索引. 很少情况下, MySQL会选择优化不足的索引. 这种情况下, 可以在select语句中使用USE INDEX (indexname)来强制使用一个索引或者用IGNORE INDEX (indexname)来强制MySQL忽略索引.",
-	"key_len":       "显示MySQL使用索引键的长度. 如果key是NULL, 则key_len为NULL. 使用的索引的长度. 在不损失精确性的情况下, 长度越短越好.",
-	"ref":           "显示索引的哪一列被使用了.",
-	"rows":          "表示MySQL认为必须检查的用来返回请求数据的行数.",
-	"filtered":      "表示返回结果的行占需要读到的行(rows列的值)的百分比.",
-	"Extra":         "该列显示MySQL在查询过程中的一些详细信息, MySQL查询优化器执行查询的过程中对查询计划的重要补充信息.",
-}
-
-// ExplainSelectType EXPLAIN中SELECT TYPE会出现的类型
-var ExplainSelectType = map[string]string{
-	"SIMPLE":               "简单SELECT(不使用UNION或子查询等).",
-	"PRIMARY":              "最外层的select.",
-	"UNION":                "UNION中的第二个或后面的SELECT查询, 不依赖于外部查询的结果集.",
-	"DEPENDENT":            "UNION中的第二个或后面的SELECT查询, 依赖于外部查询的结果集.",
-	"UNION RESULT":         "UNION查询的结果集.",
-	"SUBQUERY":             "子查询中的第一个SELECT查询, 不依赖于外部查询的结果集.",
-	"DEPENDENT SUBQUERY":   "子查询中的第一个SELECT查询, 依赖于外部查询的结果集.",
-	"DERIVED":              "用于from子句里有子查询的情况. MySQL会递归执行这些子查询, 把结果放在临时表里.",
-	"MATERIALIZED":         "Materialized subquery.",
-	"UNCACHEABLE SUBQUERY": "结果集不能被缓存的子查询, 必须重新为外层查询的每一行进行评估.",
-	"UNCACHEABLE UNION":    "UNION中的第二个或后面的select查询, 属于不可缓存的子查询(类似于UNCACHEABLE SUBQUERY).",
-}
-
-// ExplainAccessType EXPLAIN中ACCESS TYPE会出现的类型
-var ExplainAccessType = map[string]string{
-	"system":          "这是const连接类型的一种特例, 该表仅有一行数据(=系统表).",
-	"const":           `const用于使用常数值比较PRIMARY KEY时, 当查询的表仅有一行时, 使用system. 例:SELECT * FROM tbl WHERE col =1.`,
-	"eq_ref":          `除const类型外最好的可能实现的连接类型. 它用在一个索引的所有部分被连接使用并且索引是UNIQUE或PRIMARY KEY, 对于每个索引键, 表中只有一条记录与之匹配. 例:'SELECT * FROM ref_table,tbl WHERE ref_table.key_column=tbl.column;'.`,
-	"ref":             `连接不能基于关键字选择单个行, 可能查找到多个符合条件的行. 叫做ref是因为索引要跟某个参考值相比较. 这个参考值或者是一个数, 或者是来自一个表里的多表查询的结果值. 例:'SELECT * FROM tbl WHERE idx_col=expr;'.`,
-	"fulltext":        "查询时使用 FULLTEXT 索引.",
-	"ref_or_null":     "如同ref, 但是MySQL必须在初次查找的结果里找出null条目, 然后进行二次查找.",
-	"index_merge":     `表示使用了索引合并优化方法. 在这种情况下. key列包含了使用的索引的清单, key_len包含了使用的索引的最长的关键元素. 详情请见 8.2.1.4, “Index Merge Optimization”.`,
-	"unique_subquery": `在某些IN查询中使用此种类型,而不是常规的ref:'value IN (SELECT primary_key FROM single_table WHERE some_expr)'.`,
-	"index_subquery":  "在某些IN查询中使用此种类型, 与unique_subquery类似, 但是查询的是非唯一索引性索引.",
-	"range":           `只检索给定范围的行, 使用一个索引来选择行. key列显示使用了哪个索引. key_len包含所使用索引的最长关键元素.`,
-	"index":           "全表扫描, 只是扫描表的时候按照索引次序进行而不是行. 主要优点就是避免了排序, 但是开销仍然非常大.",
-	"ALL":             `最坏的情况, 从头到尾全表扫描.`,
-}
-
-// ExplainScalability ACCESS TYPE对应的运算复杂度 [AccessType]scalability map
-var ExplainScalability = map[string]string{
-	"ALL":             "O(n)",
-	"index":           "O(n)",
-	"range":           "O(log n)+",
-	"index_subquery":  "O(log n)+",
-	"unique_subquery": "O(log n)+",
-	"index_merge":     "O(log n)+",
-	"ref_or_null":     "O(log n)+",
-	"fulltext":        "O(log n)+",
-	"ref":             "O(log n)",
-	"eq_ref":          "O(log n)",
-	"const":           "O(1)",
-	"system":          "O(1)",
-}
-
-// ExplainExtra Extra信息解读
-// https://dev.mysql.com/doc/refman/8.0/en/explain-output.html
-// sql/opt_explain_traditional.cc:traditional_extra_tags
-var ExplainExtra = map[string]string{
-	"Using temporary":                   "表示MySQL在对查询结果排序时使用临时表. 常见于排序order by和分组查询group by.",
-	"Using filesort":                    "MySQL会对结果使用一个外部索引排序,而不是从表里按照索引次序读到相关内容. 可能在内存或者磁盘上进行排序. MySQL中无法利用索引完成的排序操作称为'文件排序'.",
-	"Using index condition":             "在5.6版本后加入的新特性(Index Condition Pushdown)。Using index condition 会先条件过滤索引,过滤完索引后找到所有符合索引条件的数据行,随后用 WHERE 子句中的其他条件去过滤这些数据行。",
-	"Range checked for each record":     "MySQL没有发现好的可以使用的索引,但发现如果来自前面的表的列值已知,可能部分索引可以使用。",
-	"Using where with pushed condition": "这是一个仅仅在NDBCluster存储引擎中才会出现的信息,打开condition pushdown优化功能才可能被使用。",
-	"Using MRR":                         "使用了 MRR Optimization IO 层面进行了优化,减少 IO 方面的开销。",
-	"Skip_open_table":                   "Tables are read using the Multi-Range Read optimization strategy.",
-	"Open_frm_only":                     "Table files do not need to be opened. The information is already available from the data dictionary.",
-	"Open_full_table":                   "Unoptimized information lookup. Table information must be read from the data dictionary and by reading table files.",
-	"Scanned":                           "This indicates how many directory scans the server performs when processing a query for INFORMATION_SCHEMA tables.",
-	"Using index for group-by":          "Similar to the Using index table access method, Using index for group-by indicates that MySQL found an index that can be used to retrieve all columns of a GROUP BY or DISTINCT query without any extra disk access to the actual table. Additionally, the index is used in the most efficient way so that for each group, only a few index entries are read.",
-	"Start temporary":                   "This indicates temporary table use for the semi-join Duplicate Weedout strategy.Start",
-	"End temporary":                     "This indicates temporary table use for the semi-join Duplicate Weedout strategy.End",
-	"FirstMatch":                        "The semi-join FirstMatch join shortcutting strategy is used for tbl_name.",
-	"Materialize":                       "Materialized subquery",
-	"Start materialize":                 "Materialized subquery Start",
-	"End materialize":                   "Materialized subquery End",
-	"unique row not found":              "For a query such as SELECT ... FROM tbl_name, no rows satisfy the condition for a UNIQUE index or PRIMARY KEY on the table.",
-	// "Scan":                                                "",
-	// "Impossible ON condition":                             "",
-	// "Ft_hints:":                                           "",
-	// "Backward index scan":                                 "",
-	// "Recursive":                                           "",
-	// "Table function:":                                     "",
-	"Index dive skipped due to FORCE":                     "This item applies to NDB tables only. It means that MySQL Cluster is using the Condition Pushdown optimization to improve the efficiency of a direct comparison between a nonindexed column and a constant. In such cases, the condition is “pushed down” to the cluster's data nodes and is evaluated on all data nodes simultaneously. This eliminates the need to send nonmatching rows over the network, and can speed up such queries by a factor of 5 to 10 times over cases where Condition Pushdown could be but is not used.",
-	"Impossible WHERE noticed after reading const tables": "查询了所有const(和system)表, 但发现WHERE查询条件不起作用.",
-	"Using where":                              "WHERE条件用于筛选出与下一个表匹配的数据然后返回给客户端. 除非故意做的全表扫描, 否则连接类型是ALL或者是index, 且在Extra列的值中没有Using Where, 则该查询可能是有问题的.",
-	"Using join buffer":                        "从已有连接中找被读入缓存的数据, 并且通过缓存来完成与当前表的连接.",
-	"Using index":                              "只需通过索引就可以从表中获取列的信息, 无需额外去读取真实的行数据. 如果查询使用的列值仅仅是一个简单索引的部分值, 则会使用这种策略来优化查询.",
-	"const row not found":                      "空表做类似 SELECT ... FROM tbl_name 的查询操作.",
-	"Distinct":                                 "MySQL is looking for distinct values, so it stops searching for more rows for the current row combination after it has found the first matching row.",
-	"Full scan on NULL key":                    "子查询中的一种优化方式, 常见于无法通过索引访问null值.",
-	"Impossible HAVING":                        "HAVING条件过滤没有效果, 返回已有查询的结果集.",
-	"Impossible WHERE":                         "WHERE条件过滤没有效果, 最终是全表扫描.",
-	"LooseScan":                                "使用半连接LooseScan策略.",
-	"No matching min/max row":                  "没有行满足查询的条件, 如 SELECT MIN(...) FROM ... WHERE condition.",
-	"no matching row in const table":           "对于连接查询, 列未满足唯一索引的条件或表为空.",
-	"No matching rows after partition pruning": "对于DELETE 或 UPDATE, 优化器在分区之后, 未发现任何要删除或更新的内容. 类似查询 Impossible WHERE.",
-	"No tables used":                           "查询没有FROM子句, 或者有一个 FROM DUAL子句.",
-	"Not exists":                               "MySQL能够对LEFT JOIN查询进行优化, 并且在查找到符合LEFT JOIN条件的行后, 则不再查找更多的行.",
-	"Plan isn't ready yet":                     "This value occurs with EXPLAIN FOR CONNECTION when the optimizer has not finished creating the execution plan for the statement executing in the named connection. If execution plan output comprises multiple lines, any or all of them could have this Extra value, depending on the progress of the optimizer in determining the full execution plan.",
-	"Using intersect":                          "开启了index merge,即:对多个索引分别进行条件扫描,然后将它们各自的结果进行合并,使用的算法为:index_merge_intersection",
-	"Using union":                              "开启了index merge,即:对多个索引分别进行条件扫描,然后将它们各自的结果进行合并,使用的算法为:index_merge_union",
-	"Using sort_union":                         "开启了index merge,即:对多个索引分别进行条件扫描,然后将它们各自的结果进行合并,使用的算法为:index_merge_sort_union",
-}
-
-// 提取ExplainJSON中所有的ExplainJSONTable, 将其写入全局变量explainJSONTables
-// depth只是用于debug,逻辑上并未使用
-func findTablesInJSON(explainJSON string, depth int) {
-	common.Log.Debug("findTablesInJSON Enter: depth(%d), json(%s)", depth, explainJSON)
-	// 去除注释,语法检查
-	explainJSON = RemoveSQLComments(explainJSON)
-	if !gjson.Valid(explainJSON) {
-		return
-	}
-	// 提取所有ExplainJSONTable struct
-	for _, key := range ExplainKeyWords {
-		result := gjson.Get(explainJSON, key)
-		if result.String() == "" {
-			continue
-		}
-
-		if key == "table" {
-			table := new(ExplainJSONTable)
-			common.Log.Debug("findTablesInJSON FindTable: depth(%d), table(%s)", depth, result.String)
-			err := json.Unmarshal([]byte(result.Raw), table)
-			common.LogIfError(err, "")
-			if table.TableName != "" {
-				explainJSONTables = append(explainJSONTables, table)
-			}
-			findTablesInJSON(result.String(), depth+1)
-		} else {
-			common.Log.Debug("findTablesInJSON ScanOther: depth(%d), key(%s), array_len(%d), json(%s)", depth, key, len(result.Array()), result.String)
-			for _, val := range result.Array() {
-				if val.String() != "" {
-					findTablesInJSON(val.String(), depth+1)
-				}
-			}
-			findTablesInJSON(result.String(), depth+1)
-		}
-	}
-}
-
-// FormatJSONIntoTraditional 将JSON形式转换为TRADITIONAL形式,方便前端展现
-func FormatJSONIntoTraditional(explainJSON string) []*ExplainRow {
-	// 查找JSON中的所有ExplainJSONTable
-	explainJSONTables = []*ExplainJSONTable{}
-	findTablesInJSON(explainJSON, 0)
-
-	var explainRows []*ExplainRow
-	id := -1
-	for _, table := range explainJSONTables {
-		keyLen := table.KeyLength
-		filtered, err := strconv.ParseFloat(table.Filtered, 64)
-		if err != nil {
-			filtered = 0.00
-		}
-		if filtered > 100.00 {
-			filtered = 100.00
-		}
-		explainRows = append(explainRows, &ExplainRow{
-			ID:           id + 1,
-			SelectType:   "",
-			TableName:    table.TableName,
-			Partitions:   "NULL",
-			AccessType:   table.AccessType,
-			PossibleKeys: table.PossibleKeys,
-			Key:          table.Key,
-			KeyLen:       keyLen,
-			Ref:          table.Ref,
-			Rows:         table.RowsExaminedPerScan,
-			Filtered:     filtered,
-			Scalability:  ExplainScalability[table.AccessType],
-			Extra:        "",
-		})
-	}
-	return explainRows
-}
-
-// ConvertExplainJSON2Row 将 JSON 格式转成 ROW 格式,为方便统一做优化建议
-// 但是会损失一些 JSON 特有的分析结果
-func ConvertExplainJSON2Row(explainJSON *ExplainJSON) []*ExplainRow {
-	buf, err := json.Marshal(explainJSON)
-	if err != nil {
-		return nil
-	}
-	return FormatJSONIntoTraditional(string(buf))
-}
-
-// 用于检测 MySQL 版本是否低于 MySQL5.6
-// 低于5.6 返回 true, 表示需要改写非 SELECT 的 SQL --> SELECT
-func (db *Connector) supportExplainWrite() (bool, error) {
-	defer func() {
-		err := recover()
-		if err != nil {
-			common.Log.Error("Recover supportExplainWrite() Error:", err)
-		}
-	}()
-
-	// 5.6以上版本支持 EXPLAIN UPDATE/DELETE 等语句,但需要开启写入
-	// 如开启了 read_only, EXPLAIN UPDATE/DELETE 也会受限制
-	if common.Config.TestDSN.Version >= 50600 {
-		readOnly, err := db.SingleIntValue("read_only")
-		if err != nil {
-			return false, err
-		}
-		superReadOnly, err := db.SingleIntValue("super_read_only")
-		// Percona, MariaDB 5.6就已经有super_read_only了,但社区版5.6还没有这个参数
-		if err != nil {
-			if !strings.Contains(err.Error(), "Unknown system variable") {
-				return false, err
-			}
-			superReadOnly = readOnly
-		}
-
-		if readOnly == 1 || superReadOnly == 1 {
-			return true, nil
-		}
-
-		return false, nil
-	}
-
-	return true, nil
-}
-
-// 将SQL语句转换为可以被Explain的语句,如:写转读
-// 当输出为空时,表示语法错误或不支持EXPLAIN
-func (db *Connector) explainAbleSQL(sql string) (string, error) {
-	stmt, err := sqlparser.Parse(sql)
-	if err != nil {
-		// TODO: charset, collation
-		tiStmt, tiErr := ast.TiParse(sql, "", "")
-		if tiErr != nil {
-			common.Log.Error("explainAbleSQL ast.TiParse Error: %v", tiErr)
-			return "", tiErr
-		}
-
-		var isSelect bool
-		for _, st := range tiStmt {
-			switch st.(type) {
-			case *tidb.SelectStmt, *tidb.UnionStmt:
-				isSelect = true
-			default:
-				isSelect = false
-			}
-			if !isSelect {
-				break
-			}
-		}
-
-		if isSelect {
-			return sql, nil
-		}
-
-		common.Log.Error("explainAbleSQL sqlparser.Parse Error: %v", err)
-		return "", err
-	}
-
-	switch stmt.(type) {
-	case *sqlparser.Insert, *sqlparser.Update, *sqlparser.Delete: // REPLACE 和 INSERT 的 AST 基本相同,只是 Action 不同
-		// 判断 Explain 的 SQL 是否需要被改写
-		need, err := db.supportExplainWrite()
-		if err != nil {
-			common.Log.Error("explainAbleSQL db.supportExplainWrite Error: %v", err)
-			return "", err
-		}
-		if need {
-			rw := ast.NewRewrite(sql)
-			if rw != nil {
-				return rw.RewriteDML2Select().NewSQL, nil
-			}
-		}
-		return sql, nil
-
-	case *sqlparser.Union, *sqlparser.ParenSelect, *sqlparser.Select, sqlparser.SelectStatement:
-		return sql, nil
-	default:
-	}
-	return "", nil
-}
-
-// 执行explain请求,返回mysql.Result执行结果
-func (db *Connector) executeExplain(sql string, explainType int, formatType int) (*QueryResult, error) {
-	var err error
-	sql, err = db.explainAbleSQL(sql)
-	if sql == "" {
-		return nil, err
-	}
-
-	// 5.6以上支持FORMAT=JSON
-	explainFormat := ""
-	switch formatType {
-	case JSONFormatExplain:
-		if common.Config.TestDSN.Version >= 50600 {
-			explainFormat = "FORMAT=JSON"
-		}
-	}
-	// 执行explain
-	var res *QueryResult
-	switch explainType {
-	case ExtendedExplainType:
-		// 5.6以上extended关键字已经不推荐使用,8.0废弃了这个关键字
-		if common.Config.TestDSN.Version >= 50600 {
-			res, err = db.Query("explain %s", sql)
-		} else {
-			res, err = db.Query("explain extended %s", sql)
-		}
-	case PartitionsExplainType:
-		res, err = db.Query("explain partitions %s", sql)
-
-	default:
-		res, err = db.Query("explain %s %s", explainFormat, sql)
-	}
-	return res, err
-}
-
-// MySQLExplainWarnings WARNINGS信息中包含的优化器信息
-func MySQLExplainWarnings(exp *ExplainInfo) string {
-	content := "## MySQL优化器调优结果\n\n```sql\n"
-	for _, row := range exp.Warnings {
-		content += "\n" + row.Message + "\n"
-	}
-	content += "\n```"
-	return content
-}
-
-// MySQLExplainQueryCost 将last_query_cost信息补充到评审结果中
-func MySQLExplainQueryCost(exp *ExplainInfo) string {
-	var content string
-	if exp.QueryCost > 0 {
-
-		tmp := fmt.Sprintf("%.3f\n", exp.QueryCost)
-
-		content = "Query cost: "
-		if exp.QueryCost > float64(common.Config.MaxQueryCost) {
-			content += fmt.Sprintf("☠️ **%s**", tmp)
-		} else {
-			content += tmp
-		}
-
-	}
-	return content
-}
-
-// ExplainInfoTranslator 将explain信息翻译成人能读懂的
-func ExplainInfoTranslator(exp *ExplainInfo) string {
-	var buf []string
-	var selectTypeBuf []string
-	var accessTypeBuf []string
-	var extraTypeBuf []string
-	buf = append(buf, fmt.Sprint("### Explain信息解读\n"))
-	rows := exp.ExplainRows
-	if exp.ExplainFormat == JSONFormatExplain {
-		// JSON形式遍历分析不方便,转成Row格式统一处理
-		rows = ConvertExplainJSON2Row(exp.ExplainJSON)
-	}
-	if len(rows) == 0 {
-		return ""
-	}
-
-	// SelectType信息解读
-	explainSelectType := make(map[string]string)
-	for k, v := range ExplainSelectType {
-		explainSelectType[k] = v
-	}
-	for _, row := range rows {
-		if _, ok := explainSelectType[row.SelectType]; ok {
-			desc := fmt.Sprintf("* **%s**: %s\n", row.SelectType, explainSelectType[row.SelectType])
-			selectTypeBuf = append(selectTypeBuf, desc)
-			delete(explainSelectType, row.SelectType)
-		}
-	}
-	if len(selectTypeBuf) > 0 {
-		buf = append(buf, fmt.Sprint("#### SelectType信息解读\n"))
-		buf = append(buf, strings.Join(selectTypeBuf, "\n"))
-	}
-
-	// #### Type信息解读
-	explainAccessType := make(map[string]string)
-	for k, v := range ExplainAccessType {
-		explainAccessType[k] = v
-	}
-	for _, row := range rows {
-		if _, ok := explainAccessType[row.AccessType]; ok {
-			var warn bool
-			var desc string
-			for _, t := range common.Config.ExplainWarnAccessType {
-				if row.AccessType == t {
-					warn = true
-				}
-			}
-			if warn {
-				desc = fmt.Sprintf("* ☠️ **%s**: %s\n", row.AccessType, explainAccessType[row.AccessType])
-			} else {
-				desc = fmt.Sprintf("* **%s**: %s\n", row.AccessType, explainAccessType[row.AccessType])
-			}
-
-			accessTypeBuf = append(accessTypeBuf, desc)
-			delete(explainAccessType, row.AccessType)
-		}
-	}
-	if len(accessTypeBuf) > 0 {
-		buf = append(buf, fmt.Sprint("#### Type信息解读\n"))
-		buf = append(buf, strings.Join(accessTypeBuf, "\n"))
-	}
-
-	// #### Extra信息解读
-	if exp.ExplainFormat != JSONFormatExplain {
-		explainExtra := make(map[string]string)
-		for k, v := range ExplainExtra {
-			explainExtra[k] = v
-		}
-		for _, row := range rows {
-			for k, c := range explainExtra {
-				if strings.Contains(row.Extra, k) {
-					if k == "Impossible WHERE" {
-						if strings.Contains(row.Extra, "Impossible WHERE noticed after reading const tables") {
-							continue
-						}
-					}
-					warn := false
-					for _, w := range common.Config.ExplainWarnExtra {
-						if k == w {
-							warn = true
-						}
-					}
-					if warn {
-						extraTypeBuf = append(extraTypeBuf, fmt.Sprintf("* ☠️ **%s**: %s\n", k, c))
-					} else {
-						extraTypeBuf = append(extraTypeBuf, fmt.Sprintf("* **%s**: %s\n", k, c))
-					}
-					delete(explainExtra, k)
-				}
-			}
-		}
-	}
-	if len(extraTypeBuf) > 0 {
-		buf = append(buf, fmt.Sprint("#### Extra信息解读\n"))
-		buf = append(buf, strings.Join(extraTypeBuf, "\n"))
-	}
-
-	return strings.Join(buf, "\n")
-}
-
-// ParseExplainText 解析explain文本信息(很可能是用户复制粘贴得到),返回格式化数据
-func ParseExplainText(content string) (exp *ExplainInfo, err error) {
-	exp = &ExplainInfo{ExplainFormat: TraditionalFormatExplain}
-
-	content = strings.TrimSpace(content)
-	verticalFormat := strings.HasPrefix(content, "*")
-	jsonFormat := strings.HasPrefix(content, "{")
-	traditionalFormat := strings.HasPrefix(content, "+")
-
-	if verticalFormat && traditionalFormat && jsonFormat {
-		return nil, errors.New("not supported explain type")
-	}
-
-	if verticalFormat {
-		exp.ExplainRows, err = parseVerticalExplainText(content)
-	}
-
-	if jsonFormat {
-		exp.ExplainFormat = JSONFormatExplain
-		exp.ExplainJSON, err = parseJSONExplainText(content)
-	}
-
-	if traditionalFormat {
-		exp.ExplainRows, err = parseTraditionalExplainText(content)
-	}
-	return exp, err
-}
-
-// 解析文本形式传统形式Explain信息
-func parseTraditionalExplainText(content string) (explainRows []*ExplainRow, err error) {
-	LS := regexp.MustCompile(`^\+`) // 华丽的分隔线:)
-
-	// 格式正确性检查
-	lines := strings.Split(content, "\n")
-	if len(lines) < 3 {
-		return nil, errors.New("explain Rows less than 3")
-	}
-
-	// 提取头部,用于后续list到map的转换
-	var header []string
-	for _, h := range strings.Split(strings.Trim(lines[1], "|"), "|") {
-		header = append(header, strings.TrimSpace(h))
-	}
-	colIdx := make(map[string]int)
-	for i, item := range header {
-		colIdx[strings.ToLower(item)] = i
-	}
-
-	// explain format=json未把外面的框去了
-	if strings.ToLower(header[0]) == "explain" {
-		return nil, errors.New("json format explain need remove")
-	}
-
-	// 将每一列填充至ExplainRow结构体
-	colsMap := make(map[string]string)
-	for _, l := range lines[3:] {
-		var keylen string
-		var rows int
-		var filtered float64
-		var partitions string
-		// 跳过分割线
-		if LS.MatchString(l) || strings.TrimSpace(l) == "" {
-			continue
-		}
-
-		// list到map的转换
-		var cols []string
-		for _, c := range strings.Split(strings.Trim(l, "|"), "|") {
-			cols = append(cols, strings.TrimSpace(c))
-		}
-		for item, i := range colIdx {
-			colsMap[item] = cols[i]
-		}
-
-		// 值类型转换
-		id, err := strconv.Atoi(colsMap["id"])
-		if err != nil {
-			return nil, err
-		}
-
-		// 不存在字段给默认值
-		if colsMap["partitions"] == "" {
-			partitions = "NULL"
-		} else {
-			partitions = colsMap["partitions"]
-		}
-
-		keylen = colsMap["key_len"]
-
-		rows, err = strconv.Atoi(colsMap["Rows"])
-		if err != nil {
-			rows = 0
-		}
-
-		filtered, err = strconv.ParseFloat(colsMap["filtered"], 64)
-		if err != nil {
-			filtered = 0.00
-		}
-		// filtered may larger than 100.00
-		// https://bugs.mysql.com/bug.php?id=34124
-		if filtered > 100.00 {
-			filtered = 100.00
-		}
-
-		// 拼接结构体
-		explainRows = append(explainRows, &ExplainRow{
-			ID:           id,
-			SelectType:   colsMap["select_type"],
-			TableName:    colsMap["table"],
-			Partitions:   partitions,
-			AccessType:   colsMap["type"],
-			PossibleKeys: strings.Split(colsMap["possible_keys"], ","),
-			Key:          colsMap["key"],
-			KeyLen:       keylen,
-			Ref:          strings.Split(colsMap["ref"], ","),
-			Rows:         rows,
-			Filtered:     filtered,
-			Scalability:  ExplainScalability[colsMap["type"]],
-			Extra:        colsMap["extra"],
-		})
-	}
-	return explainRows, nil
-}
-
-// 解析文本形式竖排版 Explain信息
-func parseVerticalExplainText(content string) (explainRows []*ExplainRow, err error) {
-	var lines []string
-	explainRow := &ExplainRow{
-		Partitions: "NULL",
-		Filtered:   0.00,
-	}
-	LS := regexp.MustCompile(`^\*.*\*$`) // 华丽的分隔线:)
-
-	// 格式正确性检查
-	for _, l := range strings.Split(content, "\n") {
-		lines = append(lines, strings.TrimSpace(l))
-	}
-	if len(lines) < 11 {
-		return nil, errors.New("explain rows less than 11")
-	}
-
-	// 将每一行填充至ExplainRow结构体
-	for _, l := range lines {
-		if LS.MatchString(l) || strings.TrimSpace(l) == "" {
-			continue
-		}
-		if strings.HasPrefix(l, "id:") {
-			id := strings.TrimPrefix(l, "id: ")
-			explainRow.ID, err = strconv.Atoi(id)
-			if err != nil {
-				return nil, err
-			}
-		}
-		if strings.HasPrefix(l, "select_type:") {
-			explainRow.SelectType = strings.TrimPrefix(l, "select_type: ")
-		}
-		if strings.HasPrefix(l, "table:") {
-			explainRow.TableName = strings.TrimPrefix(l, "table: ")
-		}
-		if strings.HasPrefix(l, "partitions:") {
-			explainRow.AccessType = strings.TrimPrefix(l, "partitions: ")
-		}
-		if strings.HasPrefix(l, "type:") {
-			explainRow.AccessType = strings.TrimPrefix(l, "type: ")
-			explainRow.Scalability = ExplainScalability[explainRow.AccessType]
-		}
-		if strings.HasPrefix(l, "possible_keys:") {
-			explainRow.PossibleKeys = strings.Split(strings.TrimPrefix(l, "possible_keys: "), ",")
-		}
-		if strings.HasPrefix(l, "key:") {
-			explainRow.Key = strings.TrimPrefix(l, "key: ")
-		}
-		if strings.HasPrefix(l, "key_len:") {
-			keyLen := strings.TrimPrefix(l, "key_len: ")
-			explainRow.KeyLen = keyLen
-		}
-		if strings.HasPrefix(l, "ref:") {
-			explainRow.Ref = strings.Split(strings.TrimPrefix(l, "ref: "), ",")
-		}
-		if strings.HasPrefix(l, "Rows:") {
-			rows := strings.TrimPrefix(l, "Rows: ")
-			explainRow.Rows, err = strconv.Atoi(rows)
-			if err != nil {
-				explainRow.Rows = 0
-			}
-		}
-		if strings.HasPrefix(l, "filtered:") {
-			filtered := strings.TrimPrefix(l, "filtered: ")
-			explainRow.Filtered, err = strconv.ParseFloat(filtered, 64)
-			if err != nil {
-				return nil, err
-			} else if explainRow.Filtered > 100.00 {
-				explainRow.Filtered = 100.00
-			}
-		}
-		if strings.HasPrefix(l, "Extra:") {
-			explainRow.Extra = strings.TrimPrefix(l, "Extra: ")
-			explainRows = append(explainRows, explainRow)
-		}
-	}
-	return explainRows, err
-}
-
-// 解析文本形式JSON Explain信息
-func parseJSONExplainText(content string) (*ExplainJSON, error) {
-	explainJSON := new(ExplainJSON)
-	err := json.Unmarshal([]byte(RemoveSQLComments(content)), explainJSON)
-	return explainJSON, err
-}
-
-// ParseExplainResult 分析 mysql 执行 explain 的结果,返回 ExplainInfo 结构化数据
-func ParseExplainResult(res *QueryResult, formatType int) (exp *ExplainInfo, err error) {
-	exp = &ExplainInfo{
-		ExplainFormat: formatType,
-	}
-	// JSON 格式直接调用文本方式解析
-	if formatType == JSONFormatExplain {
-		exp.ExplainJSON, err = parseJSONExplainText(res.Rows[0].Str(0))
-		return exp, err
-	}
-
-	// 生成表头
-	colIdx := make(map[int]string)
-	for i, f := range res.Result.Fields() {
-		colIdx[i] = strings.ToLower(f.Name)
-	}
-	// 补全 ExplainRows
-	var explainrows []*ExplainRow
-	for _, row := range res.Rows {
-		expRow := &ExplainRow{Partitions: "NULL", Filtered: 0.00}
-		// list 到 map 的转换
-		for i := range row {
-			switch colIdx[i] {
-			case "id":
-				expRow.ID = row.ForceInt(i)
-			case "select_type":
-				expRow.SelectType = row.Str(i)
-			case "table":
-				expRow.TableName = row.Str(i)
-				if expRow.TableName == "" {
-					expRow.TableName = "NULL"
-				}
-			case "type":
-				expRow.AccessType = row.Str(i)
-				if expRow.AccessType == "" {
-					expRow.AccessType = "NULL"
-				}
-				expRow.Scalability = ExplainScalability[expRow.AccessType]
-			case "possible_keys":
-				expRow.PossibleKeys = strings.Split(row.Str(i), ",")
-			case "key":
-				expRow.Key = row.Str(i)
-				if expRow.Key == "" {
-					expRow.Key = "NULL"
-				}
-			case "key_len":
-				expRow.KeyLen = row.Str(i)
-			case "ref":
-				expRow.Ref = strings.Split(row.Str(i), ",")
-			case "rows":
-				expRow.Rows = row.ForceInt(i)
-			case "extra":
-				expRow.Extra = row.Str(i)
-				if expRow.Extra == "" {
-					expRow.Extra = "NULL"
-				}
-			case "filtered":
-				expRow.Filtered = row.ForceFloat(i)
-				// MySQL bug: https://bugs.mysql.com/bug.php?id=34124
-				if expRow.Filtered > 100.00 {
-					expRow.Filtered = 100.00
-				}
-			}
-		}
-		explainrows = append(explainrows, expRow)
-	}
-	exp.ExplainRows = explainrows
-	for _, w := range res.Warning {
-		// 'EXTENDED' is deprecated and will be removed in a future release.
-		if w.Int(1) != 1681 {
-			exp.Warnings = append(exp.Warnings, &ExplainWarning{Level: w.Str(0), Code: w.Int(1), Message: w.Str(2)})
-		}
-	}
-
-	// 添加 last_query_cost
-	exp.QueryCost = res.QueryCost
-
-	return exp, err
-}
-
-// Explain 获取 SQL 的 explain 信息
-func (db *Connector) Explain(sql string, explainType int, formatType int) (exp *ExplainInfo, err error) {
-	exp = &ExplainInfo{SQL: sql}
-	if explainType != TraditionalExplainType {
-		formatType = TraditionalFormatExplain
-	}
-	defer func() {
-		if e := recover(); e != nil {
-			const size = 4096
-			buf := make([]byte, size)
-			buf = buf[:runtime.Stack(buf, false)]
-			common.Log.Error("Recover Explain() Error: %v\n%v", e, string(buf))
-			err = errors.New(fmt.Sprint(e))
-		}
-	}()
-
-	// 执行EXPLAIN请求
-	res, err := db.executeExplain(sql, explainType, formatType)
-	if err != nil || res == nil {
-		return exp, err
-	}
-
-	// 解析mysql结果,输出ExplainInfo
-	exp, err = ParseExplainResult(res, formatType)
-
-	return exp, err
-}
-
-// PrintMarkdownExplainTable 打印 markdown 格式的 explain table
-func PrintMarkdownExplainTable(exp *ExplainInfo) string {
-	var buf []string
-	rows := exp.ExplainRows
-	// JSON 转换为 TRADITIONAL 格式
-	if exp.ExplainFormat == JSONFormatExplain {
-		buf = append(buf, fmt.Sprint("以下为 JSON 格式转为传统格式 EXPLAIN 表格", "\n\n"))
-		rows = ConvertExplainJSON2Row(exp.ExplainJSON)
-	}
-
-	// explain出错
-	if len(rows) == 0 {
-		return ""
-	}
-	if exp.ExplainFormat == JSONFormatExplain {
-		buf = append(buf, fmt.Sprintln("| table | partitions | type | possible\\_keys | key | key\\_len | ref | rows | filtered | scalability | Extra |"))
-		buf = append(buf, fmt.Sprintln("|---|---|---|---|---|---|---|---|---|---|---|"))
-		for _, row := range rows {
-			buf = append(buf, fmt.Sprintln("|", row.TableName, "|", row.Partitions, "|", row.AccessType,
-				"|", strings.Join(row.PossibleKeys, ","), "|", row.Key, "|", row.KeyLen, "|",
-				strings.Join(row.Ref, ","), "|", row.Rows, "|", fmt.Sprintf("%.2f%s", row.Filtered, "%"),
-				"|", row.Scalability, "|", row.Extra, "|"))
-		}
-	} else {
-		buf = append(buf, fmt.Sprintln("| id | select\\_type | table | partitions | type | possible_keys | key | key\\_len | ref | rows | filtered | scalability | Extra |"))
-		buf = append(buf, fmt.Sprintln("|---|---|---|---|---|---|---|---|---|---|---|---|---|"))
-		for _, row := range rows {
-			// 加粗
-			rows := fmt.Sprint(row.Rows)
-			if row.Rows >= common.Config.ExplainMaxRows {
-				rows = "☠️ **" + rows + "**"
-			}
-			filtered := fmt.Sprintf("%.2f%s", row.Filtered, "%")
-			if row.Filtered >= common.Config.ExplainMaxFiltered {
-				filtered = "☠️ **" + filtered + "**"
-			}
-			scalability := row.Scalability
-			for _, s := range common.Config.ExplainWarnScalability {
-				scalability = "☠️ **" + s + "**"
-			}
-			buf = append(buf, fmt.Sprintln("|", row.ID, " |",
-				common.MarkdownEscape(row.SelectType),
-				"| *"+common.MarkdownEscape(row.TableName)+"* |",
-				common.MarkdownEscape(row.Partitions), "|",
-				common.MarkdownEscape(row.AccessType), "|",
-				common.MarkdownEscape(strings.Join(row.PossibleKeys, ",
")), "|", - common.MarkdownEscape(row.Key), "|", - row.KeyLen, "|", - common.MarkdownEscape(strings.Join(row.Ref, ",
")), - "|", rows, "|", - filtered, "|", scalability, "|", - strings.Replace(common.MarkdownEscape(row.Extra), ",", ",
", -1), - "|")) - } - } - buf = append(buf, "\n") - return strings.Join(buf, "") -} diff --git a/vendor/github.com/XiaoMi/soar/database/explain_test.go b/vendor/github.com/XiaoMi/soar/database/explain_test.go deleted file mode 100644 index 01ef50f..0000000 --- a/vendor/github.com/XiaoMi/soar/database/explain_test.go +++ /dev/null @@ -1,2463 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "fmt" - "os" - "testing" - - "github.com/XiaoMi/soar/common" - - "github.com/kr/pretty" -) - -var connTest *Connector - -func init() { - common.BaseDir = common.DevPath - common.ParseConfig("") - connTest = &Connector{ - Addr: common.Config.OnlineDSN.Addr, - User: common.Config.OnlineDSN.User, - Pass: common.Config.OnlineDSN.Password, - Database: common.Config.OnlineDSN.Schema, - } - if _, err := connTest.Version(); err != nil { - common.Log.Critical("Test env Error: %v", err) - os.Exit(0) - } -} - -var sqls = []string{ - `select * from city where country_id = 44;`, - `select * from address where address2 is not null;`, - `select * from address where address2 is null;`, - `select * from address where address2 >= 44;`, - `select * from city where country_id between 44 and 107;`, - `select * from city where city like 'Ad%';`, - `select * from city where city = 'Aden' and country_id = 107;`, - `select * from city where country_id > 31 and city = 'Aden';`, - `select * from address where address_id > 8 and city_id < 400 and district = 'Nantou';`, - `select * from address where address_id > 8 and city_id < 400;`, - `select * from actor where last_update='2006-02-15 04:34:33' and last_name='CHASE' group by first_name;`, - `select * from address where last_update >='2014-09-25 22:33:47' group by district;`, - `select * from address group by address,district;`, - `select * from address where last_update='2014-09-25 22:30:27' group by district,(address_id+city_id);`, - `select * from customer where active=1 order by last_name limit 10;`, - `select * from customer order by last_name limit 10;`, - `select * from customer where address_id > 224 order by address_id limit 10;`, - `select * from customer where address_id < 224 order by address_id limit 10;`, - `select * from customer where active=1 order by last_name;`, - `select * from customer where address_id > 224 order by address_id;`, - `select * from customer where address_id in (224,510) order by last_name;`, - `select city from city where country_id = 44;`, - `select city,city_id from city where country_id = 44 and last_update='2006-02-15 04:45:25';`, - `select city from city where country_id > 44 and last_update > '2006-02-15 04:45:25';`, - `select * from city where country_id=1 and city='Kabul' order by last_update;`, - `select * from city where country_id>1 and city='Kabul' order by last_update;`, - `select * from city where city_id>251 order by last_update;`, - `select * from city i inner join country o on i.country_id=o.country_id;`, - `select * from city i left join country o on i.city_id=o.country_id;`, - `select * from city i right join country o on i.city_id=o.country_id;`, - `select * from city i left join country o on i.city_id=o.country_id where o.country_id is null;`, - `select * from city i right join country o on i.city_id=o.country_id where i.city_id is null;`, - `select * from city i left join country o on i.city_id=o.country_id union select * from city i right join country o on i.city_id=o.country_id;`, - `select * from city i left join country o on i.city_id=o.country_id where o.country_id is null union select * from city i right join country o on i.city_id=o.country_id where i.city_id is null;`, - `select first_name,last_name,email from customer natural left join address;`, - `select first_name,last_name,email from customer natural left join address;`, - `select first_name,last_name,email from customer natural right join address;`, - `select first_name,last_name,email from customer STRAIGHT_JOIN address on customer.address_id=address.address_id;`, - `select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;`, - `SELECT a.table_name 表名, a.table_comment 表说明, b.COLUMN_NAME 字段名, b.column_comment 字段说明, b.column_type 字段类型, b.column_key 约束 FROM information_schema.TABLES a LEFT JOIN information_schema. COLUMNS b ON a.table_name = b.TABLE_NAME WHERE a.table_schema IN ('a') AND b.column_comment LIKE '%一%' ORDER BY a.table_name`, -} - -var exp = []string{ - `+----+-------------+---------+-------+---------------------------------------------------------+-------------------+---------+---------------------------+------+-------------+ -| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | -+----+-------------+---------+-------+---------------------------------------------------------+-------------------+---------+---------------------------+------+-------------+ -| 1 | SIMPLE | country | index | PRIMARY,country_id | country | 152 | NULL | 109 | Using index | -| 1 | SIMPLE | city | ref | idx_fk_country_id,idx_country_id_city,idx_all,idx_other | idx_fk_country_id | 2 | sakila.country.country_id | 2 | Using index | -+----+-------------+---------+-------+---------------------------------------------------------+-------------------+---------+---------------------------+------+-------------+`, - `+----+-------------+---------+------------+-------+-------------------+-------------------+---------+---------------------------+------+----------+-------------+ -| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | -+----+-------------+---------+------------+-------+-------------------+-------------------+---------+---------------------------+------+----------+-------------+ -| 1 | SIMPLE | country | NULL | index | PRIMARY | PRIMARY | 2 | NULL | 109 | 100.00 | Using index | -| 1 | SIMPLE | city | NULL | ref | idx_fk_country_id | idx_fk_country_id | 2 | sakila.country.country_id | 5 | 100.00 | Using index | -+----+-------------+---------+------------+-------+-------------------+-------------------+---------+---------------------------+------+----------+-------------+`, - `*************************** 1. row *************************** - id: 1 - select_type: SIMPLE - table: country - type: index -possible_keys: PRIMARY,country_id - key: country - key_len: 152 - ref: NULL - rows: 109 - Extra: Using index -*************************** 2. row *************************** - id: 1 - select_type: SIMPLE - table: city - type: ref -possible_keys: idx_fk_country_id,idx_country_id_city,idx_all,idx_other - key: idx_fk_country_id - key_len: 2 - ref: sakila.country.country_id - rows: 2 - Extra: Using index`, - `+----+-------------+---------+------------+-------+---------------------------------------------------------+-------------------+---------+---------------------------+------+-------------+ -| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra | -+----+-------------+---------+------------+-------+---------------------------------------------------------+-------------------+---------+---------------------------+------+-------------+ -| 1 | SIMPLE | country | NULL | index | PRIMARY,country_id | country | 152 | NULL | 109 | Using index | -| 1 | SIMPLE | city | NULL | ref | idx_fk_country_id,idx_country_id_city,idx_all,idx_other | idx_fk_country_id | 2 | sakila.country.country_id | 2 | Using index | -+----+-------------+---------+------------+-------+---------------------------------------------------------+-------------------+---------+---------------------------+------+-------------+`, - `{ - "query_block": { - "select_id": 1, - "message": "No tables used" - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "message": "no matching row in const table" - } -}`, - `{ - "query_block": { - "select_id": 1, - "table": { - "insert": true, - "table_name": "t1", - "access_type": "ALL" - } /* table */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "message": "no matching row in const table" - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "message": "no matching row in const table" - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "13.50" - } /* cost_info */, - "table": { - "table_name": "a4", - "access_type": "ALL", - "rows_examined_per_scan": 14, - "rows_produced_per_join": 14, - "filtered": "100.00", - "cost_info": { - "read_cost": "10.70", - "eval_cost": "2.80", - "prefix_cost": "13.50", - "data_read_per_join": "224" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */, - "materialized_from_subquery": { - "using_temporary_table": true, - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "13.50" - } /* cost_info */, - "table": { - "table_name": "a3", - "access_type": "ALL", - "rows_examined_per_scan": 14, - "rows_produced_per_join": 14, - "filtered": "100.00", - "cost_info": { - "read_cost": "10.70", - "eval_cost": "2.80", - "prefix_cost": "13.50", - "data_read_per_join": "224" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */, - "materialized_from_subquery": { - "using_temporary_table": true, - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 3, - "cost_info": { - "query_cost": "13.50" - } /* cost_info */, - "table": { - "table_name": "a2", - "access_type": "ALL", - "rows_examined_per_scan": 14, - "rows_produced_per_join": 14, - "filtered": "100.00", - "cost_info": { - "read_cost": "10.70", - "eval_cost": "2.80", - "prefix_cost": "13.50", - "data_read_per_join": "224" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */, - "materialized_from_subquery": { - "using_temporary_table": true, - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 4, - "cost_info": { - "query_cost": "15.55" - } /* cost_info */, - "nested_loop": [ - { - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */ - } /* table */ - }, - { - "table": { - "table_name": "a1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 14, - "filtered": "100.00", - "using_join_buffer": "Block Nested Loop", - "cost_info": { - "read_cost": "10.35", - "eval_cost": "2.80", - "prefix_cost": "15.55", - "data_read_per_join": "224" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */, - "materialized_from_subquery": { - "using_temporary_table": true, - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 5, - "cost_info": { - "query_cost": "3.41" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 7, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "1.40", - "prefix_cost": "3.41", - "data_read_per_join": "56" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - } /* materialized_from_subquery */ - } /* table */ - } - ] /* nested_loop */ - } /* query_block */ - } /* materialized_from_subquery */ - } /* table */ - } /* query_block */ - } /* materialized_from_subquery */ - } /* table */ - } /* query_block */ - } /* materialized_from_subquery */ - } /* table */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "5.81" - } /* cost_info */, - "nested_loop": [ - { - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 0, - "filtered": "14.29", - "cost_info": { - "read_cost": "3.21", - "eval_cost": "0.20", - "prefix_cost": "3.41", - "data_read_per_join": "7" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */, - "attached_condition": "(test.t1.i = 10)" - } /* table */ - }, - { - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 0, - "filtered": "50.00", - "first_match": "t1", - "using_join_buffer": "Block Nested Loop", - "cost_info": { - "read_cost": "2.20", - "eval_cost": "0.20", - "prefix_cost": "5.82", - "data_read_per_join": "7" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */, - "attached_condition": "(test.t2.i = 10)" - } /* table */ - } - ] /* nested_loop */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "3.41" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 7, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "1.40", - "prefix_cost": "3.41", - "data_read_per_join": "56" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */, - "attached_condition": "((test.t1.i ,(/* select#2 */ select 1 from test.t2 where ((test.t1.i = 10) and ((test.t1.i) = test.t2.i)))) or (test.t1.i.test.t1.i in ( (/* select#3 */ select NULL from test.t4 where 1 ), (test.t1.i in on where ((test.t1.i = materialized-subquery.i))))))", - "attached_subqueries": [ - { - "table": { - "table_name": "", - "access_type": "eq_ref", - "key": "", - "key_length": "5", - "rows_examined_per_scan": 1, - "materialized_from_subquery": { - "using_temporary_table": true, - "dependent": true, - "cacheable": false, - "query_block": { - "select_id": 3, - "message": "no matching row in const table" - } /* query_block */ - } /* materialized_from_subquery */ - } /* table */ - }, - { - "dependent": true, - "cacheable": false, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 1, - "filtered": "50.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.20", - "prefix_cost": "2.40", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */, - "attached_condition": "((test.t1.i = 10) and ((test.t1.i) = test.t2.i))" - } /* table */ - } /* query_block */ - } - ] /* attached_subqueries */ - } /* table */ - } /* query_block */ -}`, - `{ - "query_block": { - "union_result": { - "using_temporary_table": true, - "table_name": "", - "access_type": "ALL", - "query_specifications": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "3.41" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 7, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "1.40", - "prefix_cost": "3.41", - "data_read_per_join": "56" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - }, - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - }, - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 3, - "message": "no matching row in const table" - } /* query_block */ - } - ] /* query_specifications */ - } /* union_result */ - } /* query_block */ -}`, - `{ - "query_block": { - "union_result": { - "using_temporary_table": false, - "query_specifications": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "7.21" - } /* cost_info */, - "nested_loop": [ - { - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */ - } /* table */ - }, - { - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 14, - "filtered": "100.00", - "using_join_buffer": "Block Nested Loop", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "2.80", - "prefix_cost": "7.22", - "data_read_per_join": "112" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } - ] /* nested_loop */ - } /* query_block */ - }, - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "message": "no matching row in const table" - } /* query_block */ - } - ] /* query_specifications */ - } /* union_result */ - } /* query_block */ -}`, - `{ - "query_block": { - "ordering_operation": { - "using_filesort": true, - "union_result": { - "using_temporary_table": true, - "table_name": "", - "access_type": "ALL", - "query_specifications": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "3.41" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 7, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "1.40", - "prefix_cost": "3.41", - "data_read_per_join": "56" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - }, - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - } - ] /* query_specifications */ - } /* union_result */, - "order_by_subqueries": [ - { - "dependent": true, - "cacheable": false, - "query_block": { - "select_id": 3, - "message": "No tables used" - } /* query_block */ - } - ] /* order_by_subqueries */ - } /* ordering_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "3.41" - } /* cost_info */, - "ordering_operation": { - "using_filesort": false, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 7, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "1.40", - "prefix_cost": "3.41", - "data_read_per_join": "56" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */, - "optimized_away_subqueries": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */ - } /* table */ - } /* query_block */ - } - ] /* optimized_away_subqueries */ - } /* ordering_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "3.41" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 7, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "1.40", - "prefix_cost": "3.41", - "data_read_per_join": "56" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */, - "having_subqueries": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 3, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - }, - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - } - ] /* having_subqueries */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "10.41" - } /* cost_info */, - "grouping_operation": { - "using_temporary_table": true, - "using_filesort": true, - "cost_info": { - "sort_cost": "7.00" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 7, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "1.40", - "prefix_cost": "3.41", - "data_read_per_join": "56" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */, - "group_by_subqueries": [ - { - "dependent": true, - "cacheable": false, - "query_block": { - "select_id": 3, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */, - "attached_condition": "(outer_field_is_not_null, (((test.t1.i) >= test.t2.i) or isnull(test.t2.i)), true)" - } /* table */ - } /* query_block */ - }, - { - "dependent": true, - "cacheable": false, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */, - "attached_condition": "(outer_field_is_not_null, (((test.t1.i) <= test.t2.i) or isnull(test.t2.i)), true)" - } /* table */ - } /* query_block */ - } - ] /* group_by_subqueries */ - } /* grouping_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "3.41" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 7, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "1.40", - "prefix_cost": "3.41", - "data_read_per_join": "56" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */, - "select_list_subqueries": [ - { - "dependent": false, - "cacheable": false, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "3.41" - } /* cost_info */, - "ordering_operation": { - "using_temporary_table": true, - "using_filesort": true, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 7, - "rows_produced_per_join": 7, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "1.40", - "prefix_cost": "3.41", - "data_read_per_join": "56" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* ordering_operation */ - } /* query_block */ - } - ] /* select_list_subqueries */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "message": "no matching row in const table" - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "5.21" - } /* cost_info */, - "nested_loop": [ - { - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "32" - } /* cost_info */, - "used_columns": [ - "a", - "b" - ] /* used_columns */, - "attached_condition": "(((/* select#3 */ select test.t3.e from test.t3),(/* select#4 */ select 1 from test.t3 where (test.t1.b and (outer_field_is_not_null, ((((/* select#3 */ select test.t3.e from test.t3)) < test.t3.e) or isnull(test.t3.e)), true)) having (outer_field_is_not_null, (test.t3.e), true))))", - "attached_subqueries": [ - { - "dependent": true, - "cacheable": false, - "query_block": { - "select_id": 4, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "table": { - "table_name": "t3", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "e" - ] /* used_columns */, - "attached_condition": "(test.t1.b and (outer_field_is_not_null, ((((/* select#3 */ select test.t3.e from test.t3)) < test.t3.e) or isnull(test.t3.e)), true))" - } /* table */ - } /* query_block */ - }, - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 3, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "table": { - "table_name": "t3", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "e" - ] /* used_columns */ - } /* table */ - } /* query_block */ - } - ] /* attached_subqueries */ - } /* table */ - }, - { - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "50.00", - "first_match": "t1", - "using_join_buffer": "Block Nested Loop", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "5.21", - "data_read_per_join": "32" - } /* cost_info */, - "used_columns": [ - "c" - ] /* used_columns */, - "attached_condition": "(test.t2.c = test.t1.a)" - } /* table */ - } - ] /* nested_loop */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "35.44" - } /* cost_info */, - "nested_loop": [ - { - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 12, - "rows_produced_per_join": 12, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.02", - "eval_cost": "2.40", - "prefix_cost": "4.42", - "data_read_per_join": "96" - } /* cost_info */, - "used_columns": [ - "a" - ] /* used_columns */, - "attached_condition": "((test.t1.a is not null) and (test.t1.a is not null))" - } /* table */ - }, - { - "table": { - "table_name": "", - "access_type": "eq_ref", - "key": "", - "key_length": "5", - "ref": [ - "test.t1.a" - ] /* ref */, - "rows_examined_per_scan": 1, - "materialized_from_subquery": { - "using_temporary_table": true, - "query_block": { - "nested_loop": [ - { - "table": { - "table_name": "t4", - "access_type": "ALL", - "rows_examined_per_scan": 12, - "rows_produced_per_join": 3, - "filtered": "33.33", - "cost_info": { - "read_cost": "3.62", - "eval_cost": "0.80", - "prefix_cost": "4.42", - "data_read_per_join": "31" - } /* cost_info */, - "used_columns": [ - "a" - ] /* used_columns */, - "attached_condition": "(test.t4.a > 0)" - } /* table */ - }, - { - "table": { - "table_name": "t3", - "access_type": "ALL", - "rows_examined_per_scan": 12, - "rows_produced_per_join": 4, - "filtered": "10.00", - "using_join_buffer": "Block Nested Loop", - "cost_info": { - "read_cost": "2.02", - "eval_cost": "0.96", - "prefix_cost": "16.04", - "data_read_per_join": "38" - } /* cost_info */, - "used_columns": [ - "a" - ] /* used_columns */, - "attached_condition": "(test.t3.a = test.t4.a)" - } /* table */ - } - ] /* nested_loop */ - } /* query_block */ - } /* materialized_from_subquery */ - } /* table */ - }, - { - "table": { - "table_name": "", - "access_type": "eq_ref", - "key": "", - "key_length": "5", - "ref": [ - "test.t1.a" - ] /* ref */, - "rows_examined_per_scan": 1, - "materialized_from_subquery": { - "using_temporary_table": true, - "query_block": { - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 12, - "rows_produced_per_join": 3, - "filtered": "33.33", - "cost_info": { - "read_cost": "3.62", - "eval_cost": "0.80", - "prefix_cost": "4.42", - "data_read_per_join": "31" - } /* cost_info */, - "used_columns": [ - "a" - ] /* used_columns */, - "attached_condition": "(test.t2.a > 0)" - } /* table */ - } /* query_block */ - } /* materialized_from_subquery */ - } /* table */ - } - ] /* nested_loop */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "1.20" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "1.00", - "eval_cost": "0.20", - "prefix_cost": "1.20", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "i1", - "c1" - ] /* used_columns */, - "attached_condition": "exists(/* select#2 */ select test.t2.c1 from test.t2 join test.t3 where ((test.t2.c1 = test.t3.c1) and (test.t2.c2 = (/* select#3 */ select min(test.t3.c1) from test.t3)) and ((/* select#3 */ select min(test.t3.c1) from test.t3) <> test.t1.c1)))", - "attached_subqueries": [ - { - "dependent": true, - "cacheable": false, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "nested_loop": [ - { - "table": { - "table_name": "t3", - "access_type": "ALL", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "1.00", - "eval_cost": "0.20", - "prefix_cost": "1.20", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "c1" - ] /* used_columns */, - "attached_condition": "((/* select#3 */ select min(test.t3.c1) from test.t3) <> test.t1.c1)", - "attached_subqueries": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 3, - "cost_info": { - "query_cost": "1.20" - } /* cost_info */, - "table": { - "table_name": "t3", - "access_type": "ALL", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "1.00", - "eval_cost": "0.20", - "prefix_cost": "1.20", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "c1" - ] /* used_columns */ - } /* table */ - } /* query_block */ - } - ] /* attached_subqueries */ - } /* table */ - }, - { - "table": { - "table_name": "t2", - "access_type": "ref", - "possible_keys": [ - "c1" - ] /* possible_keys */, - "key": "c1", - "used_key_parts": [ - "c1" - ] /* used_key_parts */, - "key_length": "3", - "ref": [ - "test.t3.c1" - ] /* ref */, - "rows_examined_per_scan": 1, - "rows_produced_per_join": 0, - "filtered": "50.00", - "cost_info": { - "read_cost": "1.00", - "eval_cost": "0.10", - "prefix_cost": "2.40", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "c1", - "c2" - ] /* used_columns */, - "attached_condition": "(test.t2.c2 = (/* select#3 */ select min(test.t3.c1) from test.t3))", - "attached_subqueries": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 3, - "cost_info": { - "query_cost": "1.20" - } /* cost_info */, - "table": { - "table_name": "t3", - "access_type": "ALL", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "1.00", - "eval_cost": "0.20", - "prefix_cost": "1.20", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "c1" - ] /* used_columns */ - } /* table */ - } /* query_block */ - } - ] /* attached_subqueries */ - } /* table */ - } - ] /* nested_loop */ - } /* query_block */ - } - ] /* attached_subqueries */ - } /* table */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "20.82" - } /* cost_info */, - "duplicates_removal": { - "using_temporary_table": true, - "nested_loop": [ - { - "table": { - "table_name": "t5", - "access_type": "ALL", - "rows_examined_per_scan": 3, - "rows_produced_per_join": 3, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "0.60", - "prefix_cost": "2.61", - "data_read_per_join": "24" - } /* cost_info */, - "used_columns": [ - "c" - ] /* used_columns */ - } /* table */ - }, - { - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 3, - "rows_produced_per_join": 3, - "filtered": "33.33", - "using_join_buffer": "Block Nested Loop", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "0.60", - "prefix_cost": "6.41", - "data_read_per_join": "48" - } /* cost_info */, - "used_columns": [ - "c", - "c_key" - ] /* used_columns */, - "attached_condition": "(test.t2.c = test.t5.c)" - } /* table */ - }, - { - "table": { - "table_name": "t1", - "access_type": "index", - "possible_keys": [ - "c_key" - ] /* possible_keys */, - "key": "c_key", - "used_key_parts": [ - "c_key" - ] /* used_key_parts */, - "key_length": "5", - "rows_examined_per_scan": 3, - "rows_produced_per_join": 3, - "filtered": "33.33", - "using_index": true, - "using_join_buffer": "Block Nested Loop", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "0.60", - "prefix_cost": "12.22", - "data_read_per_join": "24" - } /* cost_info */, - "used_columns": [ - "c_key" - ] /* used_columns */, - "attached_condition": "(test.t1.c_key = test.t2.c_key)" - } /* table */ - }, - { - "table": { - "table_name": "t4", - "access_type": "ALL", - "rows_examined_per_scan": 3, - "rows_produced_per_join": 3, - "filtered": "33.33", - "using_join_buffer": "Block Nested Loop", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "0.60", - "prefix_cost": "16.02", - "data_read_per_join": "48" - } /* cost_info */, - "used_columns": [ - "c", - "c_key" - ] /* used_columns */, - "attached_condition": "((test.t4.c = test.t5.c) and (test.t4.c_key is not null))" - } /* table */ - }, - { - "table": { - "table_name": "t3", - "access_type": "ref", - "possible_keys": [ - "c_key" - ] /* possible_keys */, - "key": "c_key", - "used_key_parts": [ - "c_key" - ] /* used_key_parts */, - "key_length": "5", - "ref": [ - "test.t4.c_key" - ] /* ref */, - "rows_examined_per_scan": 1, - "rows_produced_per_join": 3, - "filtered": "100.00", - "using_index": true, - "cost_info": { - "read_cost": "3.00", - "eval_cost": "0.60", - "prefix_cost": "20.82", - "data_read_per_join": "24" - } /* cost_info */, - "used_columns": [ - "c_key" - ] /* used_columns */ - } /* table */ - } - ] /* nested_loop */ - } /* duplicates_removal */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "table": { - "update": true, - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 1, - "filtered": "100.00" - } /* table */, - "update_value_subqueries": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "1.00" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "system", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "0.00", - "eval_cost": "0.20", - "prefix_cost": "0.00", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - } - ] /* update_value_subqueries */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "1.00" - } /* cost_info */, - "nested_loop": [ - { - "table": { - "update": true, - "table_name": "t1", - "access_type": "system", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "0.00", - "eval_cost": "0.20", - "prefix_cost": "0.00", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - }, - { - "table": { - "table_name": "t2", - "access_type": "system", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "0.00", - "eval_cost": "0.20", - "prefix_cost": "0.00", - "data_read_per_join": "8" - } /* cost_info */ - } /* table */ - } - ] /* nested_loop */, - "update_value_subqueries": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "1.00" - } /* cost_info */, - "table": { - "table_name": "t3", - "access_type": "system", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "0.00", - "eval_cost": "0.20", - "prefix_cost": "0.00", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - } - ] /* update_value_subqueries */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "1.00" - } /* cost_info */, - "table": { - "insert": true, - "table_name": "t1", - "access_type": "ALL" - } /* table */, - "insert_from": { - "table": { - "table_name": "t2", - "access_type": "system", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "0.00", - "eval_cost": "0.20", - "prefix_cost": "0.00", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* insert_from */, - "update_value_subqueries": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "1.00" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "system", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "0.00", - "eval_cost": "0.20", - "prefix_cost": "0.00", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - } - ] /* update_value_subqueries */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "table": { - "insert": true, - "table_name": "t1", - "access_type": "ALL" - } /* table */, - "update_value_subqueries": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "1.00" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "system", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "0.00", - "eval_cost": "0.20", - "prefix_cost": "0.00", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - } - ] /* update_value_subqueries */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "table": { - "insert": true, - "table_name": "t3", - "access_type": "ALL" - } /* table */, - "optimized_away_subqueries": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 3, - "cost_info": { - "query_cost": "1.00" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "system", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "0.00", - "eval_cost": "0.20", - "prefix_cost": "0.00", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - }, - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "1.00" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "system", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "0.00", - "eval_cost": "0.20", - "prefix_cost": "0.00", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */ - } /* query_block */ - } - ] /* optimized_away_subqueries */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "10.50" - } /* cost_info */, - "ordering_operation": { - "using_filesort": true, - "grouping_operation": { - "using_temporary_table": true, - "using_filesort": false, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "10.10", - "eval_cost": "0.40", - "prefix_cost": "10.50", - "data_read_per_join": "48" - } /* cost_info */, - "used_columns": [ - "a", - "b" - ] /* used_columns */, - "materialized_from_subquery": { - "using_temporary_table": true, - "dependent": false, - "cacheable": true, - "query_block": { - "union_result": { - "using_temporary_table": false, - "query_specifications": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "message": "No tables used" - } /* query_block */ - }, - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 3, - "message": "No tables used" - } /* query_block */ - } - ] /* query_specifications */ - } /* union_result */ - } /* query_block */ - } /* materialized_from_subquery */ - } /* table */ - } /* grouping_operation */ - } /* ordering_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "4.40" - } /* cost_info */, - "grouping_operation": { - "using_temporary_table": true, - "using_filesort": true, - "cost_info": { - "sort_cost": "2.00" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "32" - } /* cost_info */, - "used_columns": [ - "a" - ] /* used_columns */ - } /* table */, - "group_by_subqueries": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "1.00" - } /* cost_info */, - "table": { - "table_name": "d", - "access_type": "system", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "0.00", - "eval_cost": "0.20", - "prefix_cost": "0.00", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "b" - ] /* used_columns */, - "materialized_from_subquery": { - "using_temporary_table": true, - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 3, - "cost_info": { - "query_cost": "5.21" - } /* cost_info */, - "ordering_operation": { - "using_temporary_table": true, - "using_filesort": true, - "nested_loop": [ - { - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "32" - } /* cost_info */, - "used_columns": [ - "a", - "b" - ] /* used_columns */ - } /* table */ - }, - { - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 4, - "filtered": "100.00", - "using_join_buffer": "Block Nested Loop", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.80", - "prefix_cost": "5.21", - "data_read_per_join": "64" - } /* cost_info */ - } /* table */ - } - ] /* nested_loop */ - } /* ordering_operation */ - } /* query_block */ - } /* materialized_from_subquery */ - } /* table */ - } /* query_block */ - } - ] /* group_by_subqueries */ - } /* grouping_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */ - } /* table */, - "optimized_away_subqueries": [ - { - "dependent": false, - "cacheable": true, - "query_block": { - "select_id": 3, - "cost_info": { - "query_cost": "4.40" - } /* cost_info */, - "grouping_operation": { - "using_temporary_table": true, - "using_filesort": true, - "cost_info": { - "sort_cost": "2.00" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "f1" - ] /* used_columns */ - } /* table */ - } /* grouping_operation */ - } /* query_block */ - } - ] /* optimized_away_subqueries */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "4.02" - } /* cost_info */, - "ordering_operation": { - "using_filesort": true, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 10, - "rows_produced_per_join": 10, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.02", - "eval_cost": "2.00", - "prefix_cost": "4.02", - "data_read_per_join": "80" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */, - "order_by_subqueries": [ - { - "dependent": true, - "cacheable": false, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "4.02" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 10, - "rows_produced_per_join": 1, - "filtered": "10.00", - "cost_info": { - "read_cost": "2.02", - "eval_cost": "0.20", - "prefix_cost": "4.02", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "i", - "j" - ] /* used_columns */, - "attached_condition": "(test.t2.i = test.t1.i)" - } /* table */ - } /* query_block */ - } - ] /* order_by_subqueries */ - } /* ordering_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "4.02" - } /* cost_info */, - "grouping_operation": { - "using_temporary_table": true, - "using_filesort": true, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 10, - "rows_produced_per_join": 10, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.02", - "eval_cost": "2.00", - "prefix_cost": "4.02", - "data_read_per_join": "80" - } /* cost_info */, - "used_columns": [ - "i" - ] /* used_columns */ - } /* table */, - "group_by_subqueries": [ - { - "dependent": true, - "cacheable": false, - "query_block": { - "select_id": 2, - "cost_info": { - "query_cost": "4.02" - } /* cost_info */, - "table": { - "table_name": "t2", - "access_type": "ALL", - "rows_examined_per_scan": 10, - "rows_produced_per_join": 1, - "filtered": "10.00", - "cost_info": { - "read_cost": "2.02", - "eval_cost": "0.20", - "prefix_cost": "4.02", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "i", - "j" - ] /* used_columns */, - "attached_condition": "(test.t2.i = test.t1.i)" - } /* table */ - } /* query_block */ - } - ] /* group_by_subqueries */ - } /* grouping_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "6.50" - } /* cost_info */, - "ordering_operation": { - "using_temporary_table": true, - "using_filesort": true, - "grouping_operation": { - "using_filesort": false, - "table": { - "table_name": "t1", - "access_type": "range", - "possible_keys": [ - "k1" - ] /* possible_keys */, - "key": "k1", - "used_key_parts": [ - "a" - ] /* used_key_parts */, - "key_length": "4", - "rows_examined_per_scan": 11, - "rows_produced_per_join": 11, - "filtered": "100.00", - "using_index_for_group_by": true, - "cost_info": { - "read_cost": "4.30", - "eval_cost": "2.20", - "prefix_cost": "6.50", - "data_read_per_join": "176" - } /* cost_info */, - "used_columns": [ - "a", - "b" - ] /* used_columns */ - } /* table */ - } /* grouping_operation */ - } /* ordering_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "6.20" - } /* cost_info */, - "ordering_operation": { - "using_temporary_table": true, - "using_filesort": true, - "grouping_operation": { - "using_filesort": true, - "nested_loop": [ - { - "table": { - "table_name": "t1", - "access_type": "ALL", - "possible_keys": [ - "PRIMARY" - ] /* possible_keys */, - "rows_examined_per_scan": 3, - "rows_produced_per_join": 3, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "0.60", - "prefix_cost": "2.61", - "data_read_per_join": "48" - } /* cost_info */, - "used_columns": [ - "a", - "b" - ] /* used_columns */ - } /* table */ - }, - { - "table": { - "table_name": "t2", - "access_type": "ref", - "possible_keys": [ - "PRIMARY" - ] /* possible_keys */, - "key": "PRIMARY", - "used_key_parts": [ - "a" - ] /* used_key_parts */, - "key_length": "4", - "ref": [ - "test.t1.a" - ] /* ref */, - "rows_examined_per_scan": 1, - "rows_produced_per_join": 3, - "filtered": "100.00", - "using_index": true, - "cost_info": { - "read_cost": "3.00", - "eval_cost": "0.60", - "prefix_cost": "6.21", - "data_read_per_join": "48" - } /* cost_info */, - "used_columns": [ - "a", - "b" - ] /* used_columns */ - } /* table */ - } - ] /* nested_loop */ - } /* grouping_operation */ - } /* ordering_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "12.82" - } /* cost_info */, - "grouping_operation": { - "using_filesort": true, - "cost_info": { - "sort_cost": "9.00" - } /* cost_info */, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 9, - "rows_produced_per_join": 9, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.02", - "eval_cost": "1.80", - "prefix_cost": "3.82", - "data_read_per_join": "144" - } /* cost_info */, - "used_columns": [ - "a", - "b" - ] /* used_columns */ - } /* table */ - } /* grouping_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "3.01" - } /* cost_info */, - "ordering_operation": { - "using_filesort": true, - "duplicates_removal": { - "using_temporary_table": true, - "using_filesort": false, - "grouping_operation": { - "using_temporary_table": true, - "using_filesort": false, - "table": { - "table_name": "t1", - "access_type": "ALL", - "rows_examined_per_scan": 5, - "rows_produced_per_join": 5, - "filtered": "100.00", - "cost_info": { - "read_cost": "2.01", - "eval_cost": "1.00", - "prefix_cost": "3.01", - "data_read_per_join": "80" - } /* cost_info */, - "used_columns": [ - "a", - "b" - ] /* used_columns */ - } /* table */ - } /* grouping_operation */ - } /* duplicates_removal */ - } /* ordering_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "2.40" - } /* cost_info */, - "ordering_operation": { - "using_filesort": false, - "duplicates_removal": { - "using_temporary_table": true, - "using_filesort": false, - "buffer_result": { - "using_temporary_table": true, - "nested_loop": [ - { - "table": { - "table_name": "t1", - "access_type": "system", - "rows_examined_per_scan": 1, - "rows_produced_per_join": 1, - "filtered": "100.00", - "cost_info": { - "read_cost": "0.00", - "eval_cost": "0.20", - "prefix_cost": "0.00", - "data_read_per_join": "8" - } /* cost_info */, - "used_columns": [ - "a" - ] /* used_columns */ - } /* table */ - }, - { - "table": { - "table_name": "t2", - "access_type": "index", - "key": "PRIMARY", - "used_key_parts": [ - "a" - ] /* used_key_parts */, - "key_length": "4", - "rows_examined_per_scan": 2, - "rows_produced_per_join": 2, - "filtered": "100.00", - "using_index": true, - "distinct": true, - "cost_info": { - "read_cost": "2.00", - "eval_cost": "0.40", - "prefix_cost": "2.40", - "data_read_per_join": "16" - } /* cost_info */, - "used_columns": [ - "a" - ] /* used_columns */ - } /* table */ - } - ] /* nested_loop */ - } /* buffer_result */ - } /* duplicates_removal */ - } /* ordering_operation */ - } /* query_block */ -}`, - `{ - "query_block": { - "select_id": 1, - "cost_info": { - "query_cost": "6.41" - } /* cost_info */, - "nested_loop": [ - { - "table": { - "table_name": "t1", - "access_type": "ALL", - "possible_keys": [ - "PRIMARY" - ] /* possible_keys */, - "rows_examined_per_scan": 4, - "rows_produced_per_join": 3, - "filtered": "75.00", - "cost_info": { - "read_cost": "2.21", - "eval_cost": "0.60", - "prefix_cost": "2.81", - "data_read_per_join": "48" - } /* cost_info */, - "used_columns": [ - "a", - "b" - ] /* used_columns */, - "attached_condition": "(test.t1.b <> 30)" - } /* table */ - }, - { - "table": { - "table_name": "t2", - "access_type": "eq_ref", - "possible_keys": [ - "PRIMARY" - ] /* possible_keys */, - "key": "PRIMARY", - "used_key_parts": [ - "a" - ] /* used_key_parts */, - "key_length": "4", - "ref": [ - "test.t1.a" - ] /* ref */, - "rows_examined_per_scan": 1, - "rows_produced_per_join": 3, - "filtered": "100.00", - "using_index": true, - "cost_info": { - "read_cost": "3.00", - "eval_cost": "0.60", - "prefix_cost": "6.41", - "data_read_per_join": "24" - } /* cost_info */, - "used_columns": [ - "a" - ] /* used_columns */ - } /* table */ - } - ] /* nested_loop */ - } /* query_block */ -}`, -} - -func TestExplain(t *testing.T) { - for _, sql := range sqls { - exp, err := connTest.Explain(sql, TraditionalExplainType, TraditionalFormatExplain) - //exp, err := conn.Explain(sql, TraditionalExplainType, JSONFormatExplain) - fmt.Println("Old: ", sql) - fmt.Println("New: ", exp.SQL) - if err != nil { - fmt.Println(err) - } - pretty.Println(exp) - fmt.Println() - } -} - -func TestParseExplainText(t *testing.T) { - for _, content := range exp { - pretty.Println(RemoveSQLComments(content)) - pretty.Println(ParseExplainText(content)) - } - /* - //length := len(exp) - pretty.Println(string(RemoveSQLComments([]byte(exp[9])))) - explainInfo, err := ParseExplainText(exp[9]) - pretty.Println(explainInfo) - fmt.Println(err) - */ -} - -func TestFindTablesInJson(t *testing.T) { - idx := 9 - for _, j := range exp[idx : idx+1] { - pretty.Println(j) - findTablesInJSON(j, 0) - } - pretty.Println(len(explainJSONTables), explainJSONTables) -} - -func TestFormatJsonIntoTraditional(t *testing.T) { - idx := 11 - for _, j := range exp[idx : idx+1] { - pretty.Println(j) - pretty.Println(FormatJSONIntoTraditional(j)) - } -} - -func TestPrintMarkdownExplainTable(t *testing.T) { - expInfo, err := connTest.Explain("select 1", TraditionalExplainType, TraditionalFormatExplain) - if err != nil { - t.Error(err) - } - err = common.GoldenDiff(func() { - PrintMarkdownExplainTable(expInfo) - }, t.Name(), update) - if err != nil { - t.Error(err) - } -} - -func TestExplainInfoTranslator(t *testing.T) { - expInfo, err := connTest.Explain("select 1", TraditionalExplainType, TraditionalFormatExplain) - if err != nil { - t.Error(err) - } - err = common.GoldenDiff(func() { - ExplainInfoTranslator(expInfo) - }, t.Name(), update) - if err != nil { - t.Error(err) - } -} - -func TestMySQLExplainWarnings(t *testing.T) { - expInfo, err := connTest.Explain("select 1", TraditionalExplainType, TraditionalFormatExplain) - if err != nil { - t.Error(err) - } - err = common.GoldenDiff(func() { - MySQLExplainWarnings(expInfo) - }, t.Name(), update) - if err != nil { - t.Error(err) - } -} - -func TestMySQLExplainQueryCost(t *testing.T) { - expInfo, err := connTest.Explain("select 1", TraditionalExplainType, TraditionalFormatExplain) - if err != nil { - t.Error(err) - } - err = common.GoldenDiff(func() { - MySQLExplainQueryCost(expInfo) - }, t.Name(), update) - if err != nil { - t.Error(err) - } -} - -func TestSupportExplainWrite(t *testing.T) { - _, err := connTest.supportExplainWrite() - if err != nil { - t.Error(err) - } -} - -func TestExplainAbleSQL(t *testing.T) { - for _, sql := range sqls { - if _, err := connTest.explainAbleSQL(sql); err != nil { - t.Errorf("SQL: %s, not explain able", sql) - } - } -} diff --git a/vendor/github.com/XiaoMi/soar/database/mysql.go b/vendor/github.com/XiaoMi/soar/database/mysql.go deleted file mode 100644 index fa6927d..0000000 --- a/vendor/github.com/XiaoMi/soar/database/mysql.go +++ /dev/null @@ -1,306 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "errors" - "fmt" - "io/ioutil" - "os" - "regexp" - "strconv" - "strings" - "time" - - "github.com/XiaoMi/soar/ast" - "github.com/XiaoMi/soar/common" - - "github.com/ziutek/mymysql/mysql" - // mymysql driver - _ "github.com/ziutek/mymysql/native" - "vitess.io/vitess/go/vt/sqlparser" -) - -// Connector 数据库连接基本对象 -type Connector struct { - Addr string - User string - Pass string - Database string - Charset string -} - -// QueryResult 数据库查询返回值 -type QueryResult struct { - Rows []mysql.Row - Result mysql.Result - Error error - Warning []mysql.Row - QueryCost float64 -} - -// NewConnection 创建新连接 -func (db *Connector) NewConnection() mysql.Conn { - return mysql.New("tcp", "", db.Addr, db.User, db.Pass, db.Database) -} - -// Query 执行SQL -func (db *Connector) Query(sql string, params ...interface{}) (*QueryResult, error) { - // 测试环境如果检查是关闭的,则SQL不会被执行 - if common.Config.TestDSN.Disable { - return nil, errors.New("Dsn Disable") - } - - // 数据库安全性检查:如果 Connector 的 IP 端口与 TEST 环境不一致,则启用SQL白名单 - // 不在白名单中的SQL不允许执行 - // 执行环境与test环境不相同 - if db.Addr != common.Config.TestDSN.Addr && db.dangerousQuery(sql) { - return nil, fmt.Errorf("query execution deny: execute SQL with DSN(%s/%s) '%s'", - db.Addr, db.Database, fmt.Sprintf(sql, params...)) - } - - common.Log.Debug("Execute SQL with DSN(%s/%s) : %s", db.Addr, db.Database, fmt.Sprintf(sql, params...)) - conn := db.NewConnection() - - // 设置SQL连接超时时间 - conn.SetTimeout(time.Duration(common.Config.ConnTimeOut) * time.Second) - defer conn.Close() - err := conn.Connect() - if err != nil { - return nil, err - } - - // 添加SQL执行超时限制 - ch := make(chan QueryResult, 1) - go func() { - res := QueryResult{} - res.Rows, res.Result, res.Error = conn.Query(sql, params...) - - if common.Config.ShowWarnings { - warning, _, err := conn.Query("SHOW WARNINGS") - if err == nil { - res.Warning = warning - } - } - - // SHOW WARNINGS 并不会影响 last_query_cost - if common.Config.ShowLastQueryCost { - cost, _, err := conn.Query("SHOW SESSION STATUS LIKE 'last_query_cost'") - if err == nil { - if len(cost) > 0 { - res.QueryCost = cost[0].Float(1) - } - } - } - - ch <- res - }() - - select { - case res := <-ch: - return &res, res.Error - case <-time.After(time.Duration(common.Config.QueryTimeOut) * time.Second): - return nil, errors.New("query execution timeout") - } - -} - -// Version 获取MySQL数据库版本 -func (db *Connector) Version() (int, error) { - version := 99999 - // 从数据库中获取版本信息 - res, err := db.Query("select @@version") - if err != nil { - common.Log.Warn("(db *Connector) Version() Error: %v", err) - return version, err - } - - // MariaDB https://mariadb.com/kb/en/library/comment-syntax/ - // MySQL https://dev.mysql.com/doc/refman/8.0/en/comments.html - versionStr := strings.Split(res.Rows[0].Str(0), "-")[0] - versionSeg := strings.Split(versionStr, ".") - if len(versionSeg) == 3 { - versionStr = fmt.Sprintf("%s%02s%02s", versionSeg[0], versionSeg[1], versionSeg[2]) - version, err = strconv.Atoi(versionStr) - } - return version, err -} - -// Source execute sql from file -func (db *Connector) Source(file string) ([]*QueryResult, error) { - var sqlCounter int // SQL 计数器 - var result []*QueryResult - - fd, err := os.Open(file) - defer func() { - err = fd.Close() - if err != nil { - common.Log.Error("(db *Connector) Source(%s) fd.Close failed: %s", file, err.Error()) - } - }() - if err != nil { - common.Log.Warning("(db *Connector) Source(%s) os.Open failed: %s", file, err.Error()) - return nil, err - } - data, err := ioutil.ReadAll(fd) - if err != nil { - common.Log.Critical("ioutil.ReadAll Error: %s", err.Error()) - return nil, err - } - - sql := strings.TrimSpace(string(data)) - buf := strings.TrimSpace(sql) - for ; ; sqlCounter++ { - if buf == "" { - break - } - - // 查询请求切分 - _, sql, bufBytes := ast.SplitStatement([]byte(buf), []byte(common.Config.Delimiter)) - buf = string(bufBytes) - sql = strings.TrimSpace(sql) - common.Log.Debug("Source Query SQL: %s", sql) - - res, e := db.Query(sql) - if e != nil { - common.Log.Error("(db *Connector) Source Filename: %s, SQLCounter.: %d", file, sqlCounter) - return result, e - } - result = append(result, res) - } - return result, nil -} - -// SingleIntValue 获取某个int型变量的值 -func (db *Connector) SingleIntValue(option string) (int, error) { - // 从数据库中获取信息 - res, err := db.Query("select @@%s", option) - if err != nil { - common.Log.Warn("(db *Connector) SingleIntValue() Error: %v", err) - return -1, err - } - - return res.Rows[0].Int(0), err -} - -// ColumnCardinality 粒度计算 -func (db *Connector) ColumnCardinality(tb, col string) float64 { - // 获取该表上的已有的索引 - - // show table status 获取总行数(近似) - tbStatus, err := db.ShowTableStatus(tb) - if err != nil { - common.Log.Warn("(db *Connector) ColumnCardinality() ShowTableStatus Error: %v", err) - return 0 - } - - // 如果是视图或表中无数据,rowTotal 都为0 - // 视图不需要加索引,无数据相当于散粒度为1 - if len(tbStatus.Rows) == 0 { - common.Log.Debug("(db *Connector) ColumnCardinality() No table status: %s", tb) - return 1 - } - rowTotal := tbStatus.Rows[0].Rows - if rowTotal == 0 { - if common.Config.Sampling { - common.Log.Debug("ColumnCardinality, %s rowTotal == 0", tb) - } - return 1 - } - - // rowTotal > xxx 时保护数据库,不对该值计算散粒度,xxx可以在配置中设置 - if rowTotal > common.Config.MaxTotalRows { - return 0.5 - } - - // 计算该列散粒度 - res, err := db.Query("select count(distinct `%s`) from `%s`.`%s`", col, db.Database, tb) - if err != nil { - common.Log.Warn("(db *Connector) ColumnCardinality() Query Error: %v", err) - return 0 - } - - colNum := res.Rows[0].Float(0) - - // 散粒度区间:[0,1] - return colNum / float64(rowTotal) -} - -// IsView 判断表是否是视图 -func (db *Connector) IsView(tbName string) bool { - tbStatus, err := db.ShowTableStatus(tbName) - if err != nil { - common.Log.Error("(db *Connector) IsView Error: %v:", err) - return false - } - - if len(tbStatus.Rows) > 0 { - if tbStatus.Rows[0].Comment == "VIEW" { - return true - } - } - - return false - -} - -// RemoveSQLComments 去除SQL中的注释 -func RemoveSQLComments(sql string) string { - buf := []byte(sql) - cmtReg := regexp.MustCompile(`("(""|[^"])*")|('(''|[^'])*')|(--[^\n\r]*)|(#.*)|(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)`) - - res := cmtReg.ReplaceAllFunc(buf, func(s []byte) []byte { - if (s[0] == '"' && s[len(s)-1] == '"') || - (s[0] == '\'' && s[len(s)-1] == '\'') || - (string(s[:3]) == "/*!") { - return s - } - return []byte("") - }) - return strings.TrimSpace(string(res)) -} - -// 为了防止在 Online 环境进行误操作,通过 dangerousQuery 来判断能否在 Online 执行 -func (db *Connector) dangerousQuery(query string) bool { - queries, err := sqlparser.SplitStatementToPieces(strings.TrimSpace(strings.ToLower(query))) - if err != nil { - return true - } - - for _, sql := range queries { - dangerous := true - whiteList := []string{ - "select", - "show", - "explain", - "describe", - } - - for _, prefix := range whiteList { - if strings.HasPrefix(sql, prefix) { - dangerous = false - break - } - } - - if dangerous { - return true - } - } - - return false -} diff --git a/vendor/github.com/XiaoMi/soar/database/mysql_test.go b/vendor/github.com/XiaoMi/soar/database/mysql_test.go deleted file mode 100644 index 79ea7bd..0000000 --- a/vendor/github.com/XiaoMi/soar/database/mysql_test.go +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "fmt" - "testing" - - "github.com/XiaoMi/soar/common" - "github.com/kr/pretty" -) - -// TODO: go test -race不通过待解决 -func TestQuery(t *testing.T) { - common.Config.QueryTimeOut = 1 - _, err := connTest.Query("select sleep(2)") - if err == nil { - t.Error("connTest.Query not timeout") - } -} - -func TestColumnCardinality(_ *testing.T) { - connTest.Database = "information_schema" - a := connTest.ColumnCardinality("TABLES", "TABLE_SCHEMA") - fmt.Println("TABLES.TABLE_SCHEMA:", a) -} - -func TestDangerousSQL(t *testing.T) { - testCase := map[string]bool{ - "select * from tb;delete from tb;": true, - "show database;": false, - "select * from t;": false, - "explain delete from t;": false, - } - - db := Connector{} - for sql, want := range testCase { - got := db.dangerousQuery(sql) - if got != want { - t.Errorf("SQL:%s got:%v want:%v", sql, got, want) - } - } -} - -func TestWarningsAndQueryCost(t *testing.T) { - common.Config.ShowWarnings = true - common.Config.ShowLastQueryCost = true - res, err := connTest.Query("explain select * from sakila.film") - if err != nil { - t.Error("Query Error: ", err) - } else { - for _, w := range res.Warning { - pretty.Println(w.Str(2)) - } - fmt.Println(res.QueryCost) - pretty.Println(err) - } -} - -func TestVersion(t *testing.T) { - version, err := connTest.Version() - if err != nil { - t.Error(err.Error()) - } - fmt.Println(version) -} - -func TestSource(t *testing.T) { - res, err := connTest.Source("testdata/" + t.Name() + ".sql") - if err != nil { - t.Error("Query Error: ", err) - } - if res[0].Rows[0].Int(0) != 1 || res[1].Rows[0].Int(0) != 1 { - t.Error("Source result not match, expect 1, 1") - } -} - -func TestRemoveSQLComments(t *testing.T) { - SQLs := []string{ - `-- comment`, - `--`, - `# comment`, - `/* multi-line -comment*/`, - `-- --- comment`, - } - - err := common.GoldenDiff(func() { - for _, sql := range SQLs { - fmt.Println(RemoveSQLComments(sql)) - } - }, t.Name(), update) - if err != nil { - t.Error(err) - } -} diff --git a/vendor/github.com/XiaoMi/soar/database/privilege.go b/vendor/github.com/XiaoMi/soar/database/privilege.go deleted file mode 100644 index 762de28..0000000 --- a/vendor/github.com/XiaoMi/soar/database/privilege.go +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "errors" - "strings" - - "github.com/XiaoMi/soar/common" -) - -// CurrentUser get current user with current_user() function -func (db *Connector) CurrentUser() (string, string, error) { - res, err := db.Query("select current_user()") - if err != nil { - return "", "", err - } - if len(res.Rows) > 0 { - cols := strings.Split(res.Rows[0].Str(0), "@") - if len(cols) == 2 { - user := strings.Trim(cols[0], "'") - host := strings.Trim(cols[1], "'") - if strings.Contains(user, "'") || strings.Contains(host, "'") { - return "", "", errors.New("user or host contains irregular character") - } - return user, host, nil - } - return "", "", errors.New("user or host contains irregular character") - } - return "", "", errors.New("no privilege info") -} - -// HasSelectPrivilege if user has select privilege -func (db *Connector) HasSelectPrivilege() bool { - user, host, err := db.CurrentUser() - if err != nil { - common.Log.Error("User: %s, HasSelectPrivilege: %s", db.User, err.Error()) - return false - } - res, err := db.Query("select Select_priv from mysql.user where user='%s' and host='%s'", user, host) - if err != nil { - common.Log.Error("HasSelectPrivilege, DSN: %s, Error: %s", db.Addr, err.Error()) - return false - } - // Select_priv - if len(res.Rows) > 0 { - if res.Rows[0].Str(0) == "Y" { - return true - } - } - return false -} - -// HasAllPrivilege if user has all privileges -func (db *Connector) HasAllPrivilege() bool { - user, host, err := db.CurrentUser() - if err != nil { - common.Log.Error("User: %s, HasAllPrivilege: %s", db.User, err.Error()) - return false - } - - // concat privilege columns - res, err := db.Query("SELECT GROUP_CONCAT(COLUMN_NAME) from information_schema.COLUMNS where TABLE_SCHEMA='mysql' and TABLE_NAME='user' and COLUMN_NAME like '%%_priv'") - if err != nil { - common.Log.Error("HasAllPrivilege, DSN: %s, Error: %s", db.Addr, err.Error()) - return false - } - var priv string - if len(res.Rows) > 0 { - priv = res.Rows[0].Str(0) - } else { - common.Log.Error("HasAllPrivilege, DSN: %s, get privilege string error", db.Addr) - return false - } - - // get all privilege status - res, err = db.Query("select concat("+priv+") from mysql.user where user='%s' and host='%s'", user, host) - if err != nil { - common.Log.Error("HasAllPrivilege, DSN: %s, Error: %s", db.Addr, err.Error()) - return false - } - - // %_priv - if len(res.Rows) > 0 { - if strings.Replace(res.Rows[0].Str(0), "Y", "", -1) == "" { - return true - } - } - return false -} diff --git a/vendor/github.com/XiaoMi/soar/database/privilege_test.go b/vendor/github.com/XiaoMi/soar/database/privilege_test.go deleted file mode 100644 index 4ed0fbc..0000000 --- a/vendor/github.com/XiaoMi/soar/database/privilege_test.go +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import "testing" - -func TestHasSelectPrivilege(t *testing.T) { - if !connTest.HasSelectPrivilege() { - t.Errorf("DSN: %s, User: %s, should has select privilege", connTest.Addr, connTest.User) - } -} - -func TestHasAllPrivilege(t *testing.T) { - if !connTest.HasAllPrivilege() { - t.Errorf("DSN: %s, User: %s, should has all privilege", connTest.Addr, connTest.User) - } -} diff --git a/vendor/github.com/XiaoMi/soar/database/profiling.go b/vendor/github.com/XiaoMi/soar/database/profiling.go deleted file mode 100644 index a607231..0000000 --- a/vendor/github.com/XiaoMi/soar/database/profiling.go +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "errors" - "fmt" - "io" - "strings" - "time" - - "github.com/XiaoMi/soar/common" - - "vitess.io/vitess/go/vt/sqlparser" -) - -// Profiling show profile输出的结果 -type Profiling struct { - Rows []ProfilingRow -} - -// ProfilingRow show profile每一行信息 -type ProfilingRow struct { - Status string - Duration float64 - // TODO: 支持show profile all,不过目前看all的信息过多有点眼花缭乱 -} - -// Profiling 执行SQL,并对其Profiling -func (db *Connector) Profiling(sql string, params ...interface{}) (*QueryResult, error) { - // 过滤不需要 profiling 的 SQL - switch sqlparser.Preview(sql) { - case sqlparser.StmtSelect, sqlparser.StmtUpdate, sqlparser.StmtDelete: - default: - return nil, errors.New("no need profiling") - } - - // 测试环境如果检查是关闭的,则SQL不会被执行 - if common.Config.TestDSN.Disable { - return nil, errors.New("Dsn Disable") - } - - // 数据库安全性检查:如果 Connector 的 IP 端口与 TEST 环境不一致,则启用 SQL 白名单 - // 不在白名单中的SQL不允许执行 - // 执行环境与test环境不相同 - if db.Addr != common.Config.TestDSN.Addr && db.dangerousQuery(sql) { - return nil, fmt.Errorf("query execution deny: Execute SQL with DSN(%s/%s) '%s'", - db.Addr, db.Database, fmt.Sprintf(sql, params...)) - } - - common.Log.Debug("Execute SQL with DSN(%s/%s) : %s", db.Addr, db.Database, sql) - conn := db.NewConnection() - - // 设置SQL连接超时时间 - conn.SetTimeout(time.Duration(common.Config.ConnTimeOut) * time.Second) - defer conn.Close() - err := conn.Connect() - if err != nil { - return nil, err - } - - // 添加SQL执行超时限制 - ch := make(chan QueryResult, 1) - go func() { - // 开启Profiling - _, _, err = conn.Query("set @@profiling=1") - common.LogIfError(err, "") - - // 执行SQL,抛弃返回结果 - result, err := conn.Start(sql, params...) - if err != nil { - ch <- QueryResult{ - Error: err, - } - return - } - row := result.MakeRow() - for { - err = result.ScanRow(row) - if err == io.EOF { - break - } - } - - // 返回Profiling结果 - res := QueryResult{} - res.Rows, res.Result, res.Error = conn.Query("show profile") - _, _, err = conn.Query("set @@profiling=0") - common.LogIfError(err, "") - ch <- res - }() - - select { - case res := <-ch: - return &res, res.Error - case <-time.After(time.Duration(common.Config.QueryTimeOut) * time.Second): - return nil, errors.New("query execution timeout") - } -} - -func getProfiling(res *QueryResult) Profiling { - var rows []ProfilingRow - for _, row := range res.Rows { - rows = append(rows, ProfilingRow{ - Status: row.Str(0), - Duration: row.Float(1), - }) - } - return Profiling{Rows: rows} -} - -// FormatProfiling 格式化输出Profiling信息 -func FormatProfiling(res *QueryResult) string { - profiling := getProfiling(res) - str := []string{"| Status | Duration |"} - str = append(str, "| --- | --- |") - for _, row := range profiling.Rows { - str = append(str, fmt.Sprintf("| %s | %f |", row.Status, row.Duration)) - } - return strings.Join(str, "\n") -} diff --git a/vendor/github.com/XiaoMi/soar/database/profiling_test.go b/vendor/github.com/XiaoMi/soar/database/profiling_test.go deleted file mode 100644 index 4b226ba..0000000 --- a/vendor/github.com/XiaoMi/soar/database/profiling_test.go +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "testing" - - "github.com/XiaoMi/soar/common" - - "github.com/kr/pretty" -) - -func TestProfiling(t *testing.T) { - common.Config.QueryTimeOut = 1 - res, err := connTest.Profiling("select 1") - if err == nil { - pretty.Println(res) - } else { - t.Error(err) - } -} - -func TestFormatProfiling(t *testing.T) { - res, err := connTest.Profiling("select 1") - if err == nil { - pretty.Println(FormatProfiling(res)) - } else { - t.Error(err) - } -} - -func TestGetProfiling(t *testing.T) { - res, err := connTest.Profiling("select 1") - if err == nil { - pretty.Println(getProfiling(res)) - } else { - t.Error(err) - } -} diff --git a/vendor/github.com/XiaoMi/soar/database/sampling.go b/vendor/github.com/XiaoMi/soar/database/sampling.go deleted file mode 100644 index f8565b4..0000000 --- a/vendor/github.com/XiaoMi/soar/database/sampling.go +++ /dev/null @@ -1,230 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "fmt" - "io" - "strconv" - "strings" - "time" - - "github.com/XiaoMi/soar/common" - "github.com/ziutek/mymysql/mysql" -) - -/*-------------------- -* The following choice of minrows is based on the paper -* "Random sampling for histogram construction: how much is enough?" -* by Surajit Chaudhuri, Rajeev Motwani and Vivek Narasayya, in -* Proceedings of ACM SIGMOD International Conference on Management -* of Data, 1998, Pages 436-447. Their Corollary 1 to Theorem 5 -* says that for table size n, histogram size k, maximum relative -* error in bin size f, and error probability gamma, the minimum -* random sample size is -* r = 4 * k * ln(2*n/gamma) / f^2 -* Taking f = 0.5, gamma = 0.01, n = 10^6 rows, we obtain -* r = 305.82 * k -* Note that because of the log function, the dependence on n is -* quite weak; even at n = 10^12, a 300*k sample gives <= 0.66 -* bin size error with probability 0.99. So there's no real need to -* scale for n, which is a good thing because we don't necessarily -* know it at this point. -*-------------------- - */ - -// SamplingData 将数据从Remote拉取到 db 中 -func (db *Connector) SamplingData(remote Connector, tables ...string) error { - // 计算需要泵取的数据量 - wantRowsCount := 300 * common.Config.SamplingStatisticTarget - - // 设置数据采样单条 SQL 中 value 的数量 - // 该数值越大,在内存中缓存的data就越多,但相对的,插入时速度就越快 - maxValCount := 200 - - // 获取数据库连接对象 - conn := remote.NewConnection() - localConn := db.NewConnection() - - // 连接数据库 - err := conn.Connect() - defer conn.Close() - if err != nil { - return err - } - - err = localConn.Connect() - defer localConn.Close() - if err != nil { - return err - } - - for _, table := range tables { - // 表类型检查 - if remote.IsView(table) { - return nil - } - - tableStatus, err := remote.ShowTableStatus(table) - if err != nil { - return err - } - - if len(tableStatus.Rows) == 0 { - common.Log.Info("SamplingData, Table %s with no data, stop sampling", table) - return nil - } - - tableRows := tableStatus.Rows[0].Rows - if tableRows == 0 { - common.Log.Info("SamplingData, Table %s with no data, stop sampling", table) - return nil - } - - factor := float64(wantRowsCount) / float64(tableRows) - common.Log.Debug("SamplingData, tableRows: %d, wantRowsCount: %d, factor: %f", tableRows, wantRowsCount, factor) - - err = startSampling(conn, localConn, db.Database, table, factor, wantRowsCount, maxValCount) - if err != nil { - common.Log.Error("(db *Connector) SamplingData Error : %v", err) - } - } - return nil -} - -// 开始从环境中泵取数据 -// 因为涉及到的数据量问题,所以泵取与插入时同时进行的 -// TODO 加 ref link -func startSampling(conn, localConn mysql.Conn, database, table string, factor float64, wants, maxValCount int) error { - // 从线上数据库获取所需dump的表中所有列的数据类型,备用 - // 由于测试库中的库表为刚建立的,所以在information_schema中很可能没有这个表的信息 - var dataTypes []string - q := fmt.Sprintf("select DATA_TYPE from information_schema.COLUMNS where TABLE_SCHEMA='%s' and TABLE_NAME = '%s'", - database, table) - common.Log.Debug("Sampling data execute: %s", q) - rs, _, err := localConn.Query(q) - if err != nil { - common.Log.Debug("Sampling data got data type Err: %v", err) - } else { - for _, r := range rs { - dataTypes = append(dataTypes, r.Str(0)) - } - } - - // 生成where条件 - where := fmt.Sprintf("where RAND()<=%f", factor) - if factor >= 1 { - where = "" - } - - sql := fmt.Sprintf("select * from `%s` %s limit %d;", table, where, wants) - res, err := conn.Start(sql) - if err != nil { - return err - } - - // GetRow method allocates a new chunk of memory for every received row. - row := res.MakeRow() - rowCount := 0 - valCount := 0 - - // 获取所有的列名 - columns := make([]string, len(res.Fields())) - for i, filed := range res.Fields() { - columns[i] = filed.Name - } - colDef := strings.Join(columns, ",") - - // 开始填充数据 - var valList []string - for { - err := res.ScanRow(row) - if err == io.EOF { - // 扫描结束 - if len(valList) > 0 { - // 如果缓存中还存在未插入的数据,则把缓存中的数据刷新到DB中 - doSampling(localConn, database, table, colDef, strings.Join(valList, ",")) - } - break - } - - if err != nil { - return err - } - - values := make([]string, len(columns)) - for i := range row { - // TODO 不支持坐标类型的导出 - switch data := row[i].(type) { - case nil: - // str = "" - case []byte: - // 先尝试转成数字,如果报错则转换成string - if v, err := row.Int64Err(i); err != nil { - values[i] = string(data) - } else { - values[i] = strconv.FormatInt(v, 10) - } - case time.Time: - values[i] = mysql.TimeString(data) - case time.Duration: - values[i] = mysql.DurationString(data) - default: - values[i] = fmt.Sprint(data) - } - - // 非text/varchar类的数据类型,如果dump出的数据为空,则说明该值为null值 - // 应转换其 value 为 null,如果用空('')进行替代,会导致出现语法错误。 - if len(dataTypes) == len(res.Fields()) && values[i] == "" && - (!strings.Contains(dataTypes[i], "char") || - !strings.Contains(dataTypes[i], "text")) { - values[i] = "null" - } else { - values[i] = "'" + values[i] + "'" - } - } - - valuesStr := fmt.Sprintf(`(%s)`, strings.Join(values, `,`)) - valList = append(valList, valuesStr) - - rowCount++ - valCount++ - - if rowCount%maxValCount == 0 { - doSampling(localConn, database, table, colDef, strings.Join(valList, ",")) - valCount = 0 - valList = make([]string, 0) - - } - } - - common.Log.Debug("%d rows sampling out", rowCount) - return nil -} - -// 将泵取的数据转换成Insert语句并在数据库中执行 -func doSampling(conn mysql.Conn, dbName, table, colDef, values string) { - sql := fmt.Sprintf("Insert into `%s`.`%s`(%s) values%s;", dbName, table, - colDef, values) - - _, _, err := conn.Query(sql) - - if err != nil { - common.Log.Error("doSampling Error from %s.%s: %v", dbName, table, err) - } - -} diff --git a/vendor/github.com/XiaoMi/soar/database/sampling_test.go b/vendor/github.com/XiaoMi/soar/database/sampling_test.go deleted file mode 100644 index 082d5e9..0000000 --- a/vendor/github.com/XiaoMi/soar/database/sampling_test.go +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "testing" - - "github.com/XiaoMi/soar/common" -) - -func init() { - common.BaseDir = common.DevPath -} - -func TestSamplingData(t *testing.T) { - online := &Connector{ - Addr: common.Config.OnlineDSN.Addr, - User: common.Config.OnlineDSN.User, - Pass: common.Config.OnlineDSN.Password, - Database: common.Config.OnlineDSN.Schema, - } - - offline := &Connector{ - Addr: common.Config.TestDSN.Addr, - User: common.Config.TestDSN.User, - Pass: common.Config.TestDSN.Password, - Database: common.Config.TestDSN.Schema, - } - - offline.Database = "test" - - err := connTest.SamplingData(*online, "film") - if err != nil { - t.Error(err) - } -} diff --git a/vendor/github.com/XiaoMi/soar/database/show.go b/vendor/github.com/XiaoMi/soar/database/show.go deleted file mode 100644 index a14a5fb..0000000 --- a/vendor/github.com/XiaoMi/soar/database/show.go +++ /dev/null @@ -1,584 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "fmt" - "regexp" - "strconv" - "strings" - "time" - - "github.com/XiaoMi/soar/common" -) - -// SHOW TABLE STATUS Syntax -// https://dev.mysql.com/doc/refman/5.7/en/show-table-status.html - -// TableStatInfo 用以保存 show table status 之后获取的table信息 -type TableStatInfo struct { - Name string - Rows []tableStatusRow -} - -// tableStatusRow 用于 show table status value -type tableStatusRow struct { - Name string // 表名 - Engine string // 该表使用的存储引擎 - Version int // 该表的 .frm 文件版本号 - RowFormat string // 该表使用的行存储格式 - Rows int64 // 表行数, InnoDB 引擎中为预估值,甚至可能会有40%~50%的数值偏差 - AvgRowLength int // 平均行长度 - - // MyISAM: Data_length 为数据文件的大小,单位为 bytes - // InnoDB: Data_length 为聚簇索引分配的近似内存量,单位为 bytes, 计算方式为聚簇索引数量乘以 InnoDB 页面大小 - // 其他不同的存储引擎中该值的意义可能不尽相同 - DataLength int - - // MyISAM: Max_data_length 为数据文件长度的最大值。这是在给定使用的数据指针大小的情况下,可以存储在表中的数据的最大字节数 - // InnoDB: 未使用 - // 其他不同的存储引擎中该值的意义可能不尽相同 - MaxDataLength int - - // MyISAM: Index_length 为 index 文件的大小,单位为 bytes - // InnoDB: Index_length 为非聚簇索引分配的近似内存量,单位为 bytes,计算方式为非聚簇索引数量乘以 InnoDB 页面大小 - // 其他不同的存储引擎中该值的意义可能不尽相同 - IndexLength int - - DataFree int // 已分配但未使用的字节数 - AutoIncrement int // 下一个自增值 - CreateTime time.Time // 创建时间 - UpdateTime time.Time // 最近一次更新时间,该值不准确 - CheckTime time.Time // 上次检查时间 - Collation string // 字符集及排序规则信息 - Checksum string // 校验和 - CreateOptions string // 创建表的时候的时候一切其他属性 - Comment string // 注释 -} - -// newTableStat 构造 table Stat 对象 -func newTableStat(tableName string) *TableStatInfo { - return &TableStatInfo{ - Name: tableName, - Rows: make([]tableStatusRow, 0), - } -} - -// ShowTables 执行 show tables -func (db *Connector) ShowTables() ([]string, error) { - defer func() { - err := recover() - if err != nil { - common.Log.Error("recover ShowTableStatus()", err) - } - }() - - // 执行 show table status - res, err := db.Query("show tables") - if err != nil { - return []string{}, err - } - - // 获取值 - var tables []string - for _, row := range res.Rows { - tables = append(tables, row.Str(0)) - } - - return tables, err -} - -// ShowTableStatus 执行 show table status -func (db *Connector) ShowTableStatus(tableName string) (*TableStatInfo, error) { - defer func() { - err := recover() - if err != nil { - common.Log.Error("recover ShowTableStatus()", err) - } - }() - - // 初始化struct - ts := newTableStat(tableName) - - // 执行 show table status - res, err := db.Query("show table status where name = '%s'", ts.Name) - if err != nil { - return ts, err - } - - rs := res.Result.Map("Rows") - name := res.Result.Map("Name") - df := res.Result.Map("Data_free") - sum := res.Result.Map("Checksum") - engine := res.Result.Map("Engine") - version := res.Result.Map("Version") - comment := res.Result.Map("Comment") - ai := res.Result.Map("Auto_increment") - collation := res.Result.Map("Collation") - rowFormat := res.Result.Map("Row_format") - checkTime := res.Result.Map("Check_time") - dataLength := res.Result.Map("Data_length") - idxLength := res.Result.Map("Index_length") - createTime := res.Result.Map("Create_time") - updateTime := res.Result.Map("Update_time") - options := res.Result.Map("Create_options") - avgRowLength := res.Result.Map("Avg_row_length") - maxDataLength := res.Result.Map("Max_data_length") - - // 获取值 - for _, row := range res.Rows { - value := tableStatusRow{ - Name: row.Str(name), - Engine: row.Str(engine), - Version: row.Int(version), - Rows: row.Int64(rs), - RowFormat: row.Str(rowFormat), - AvgRowLength: row.Int(avgRowLength), - DataLength: row.Int(dataLength), - MaxDataLength: row.Int(maxDataLength), - IndexLength: row.Int(idxLength), - DataFree: row.Int(df), - AutoIncrement: row.Int(ai), - CreateTime: row.Time(createTime, time.Local), - UpdateTime: row.Time(updateTime, time.Local), - CheckTime: row.Time(checkTime, time.Local), - Collation: row.Str(collation), - Checksum: row.Str(sum), - CreateOptions: row.Str(options), - Comment: row.Str(comment), - } - ts.Rows = append(ts.Rows, value) - } - - return ts, err -} - -// https://dev.mysql.com/doc/refman/5.7/en/show-index.html - -// TableIndexInfo 用以保存 show index 之后获取的 index 信息 -type TableIndexInfo struct { - TableName string - IdxRows []TableIndexRow -} - -// TableIndexRow 用以存放show index之后获取的每一条index信息 -type TableIndexRow struct { - Table string // 表名 - NonUnique int // 0:unique key,1:not unique - KeyName string // index的名称,如果是主键则为 "PRIMARY" - SeqInIndex int // 该列在索引中的位置。计数从 1 开始 - ColumnName string // 列名 - Collation string // A or Null - Cardinality int // 索引中唯一值的数量,"ANALYZE TABLE" 可更新该值 - SubPart int // 索引前缀字节数 - Packed int - Null string // 表示该列是否可以为空,如果可以为 'YES',反之'' - IndexType string // BTREE, FULLTEXT, HASH, RTREE - Comment string - IndexComment string -} - -// NewTableIndexInfo 构造 TableIndexInfo -func NewTableIndexInfo(tableName string) *TableIndexInfo { - return &TableIndexInfo{ - TableName: tableName, - IdxRows: make([]TableIndexRow, 0), - } -} - -// ShowIndex show Index -func (db *Connector) ShowIndex(tableName string) (*TableIndexInfo, error) { - tbIndex := NewTableIndexInfo(tableName) - - // 执行 show create table - res, err := db.Query("show index from `%s`.`%s`", db.Database, tableName) - if err != nil { - return nil, err - } - - table := res.Result.Map("Table") - unique := res.Result.Map("Non_unique") - keyName := res.Result.Map("Key_name") - seq := res.Result.Map("Seq_in_index") - cName := res.Result.Map("Column_name") - collation := res.Result.Map("Collation") - cardinality := res.Result.Map("Cardinality") - subPart := res.Result.Map("Sub_part") - packed := res.Result.Map("Packed") - null := res.Result.Map("Null") - idxType := res.Result.Map("Index_type") - comment := res.Result.Map("Comment") - idxComment := res.Result.Map("Index_comment") - - // 获取值 - for _, row := range res.Rows { - value := TableIndexRow{ - Table: row.Str(table), - NonUnique: row.Int(unique), - KeyName: row.Str(keyName), - SeqInIndex: row.Int(seq), - ColumnName: row.Str(cName), - Collation: row.Str(collation), - Cardinality: row.Int(cardinality), - SubPart: row.Int(subPart), - Packed: row.Int(packed), - Null: row.Str(null), - IndexType: row.Str(idxType), - Comment: row.Str(comment), - IndexComment: row.Str(idxComment), - } - tbIndex.IdxRows = append(tbIndex.IdxRows, value) - } - return tbIndex, err -} - -// IndexSelectKey 用以对 TableIndexInfo 进行查询 -type IndexSelectKey string - -// 索引相关 -const ( - IndexKeyName = IndexSelectKey("KeyName") // 索引名称 - IndexColumnName = IndexSelectKey("ColumnName") // 索引列名称 - IndexIndexType = IndexSelectKey("IndexType") // 索引类型 - IndexNonUnique = IndexSelectKey("NonUnique") // 唯一索引 -) - -// FindIndex 获取TableIndexInfo中需要的索引 -func (tbIndex *TableIndexInfo) FindIndex(arg IndexSelectKey, value string) []TableIndexRow { - var result []TableIndexRow - if tbIndex == nil { - return result - } - - value = strings.ToLower(value) - - switch arg { - case IndexKeyName: - for _, index := range tbIndex.IdxRows { - if strings.ToLower(index.KeyName) == value { - result = append(result, index) - } - } - - case IndexColumnName: - for _, index := range tbIndex.IdxRows { - if strings.ToLower(index.ColumnName) == value { - result = append(result, index) - } - } - - case IndexIndexType: - for _, index := range tbIndex.IdxRows { - if strings.ToLower(index.IndexType) == value { - result = append(result, index) - } - } - - case IndexNonUnique: - for _, index := range tbIndex.IdxRows { - unique := strconv.Itoa(index.NonUnique) - if unique == value { - result = append(result, index) - } - } - - default: - common.Log.Error("no such args: TableIndexRow") - } - - return result -} - -// desc table -// https://dev.mysql.com/doc/refman/5.7/en/show-columns.html - -// TableDesc show columns from rental; -type TableDesc struct { - Name string - DescValues []TableDescValue -} - -// TableDescValue 含有每一列的属性 -type TableDescValue struct { - Field string // 列名 - Type string // 数据类型 - Null string // 是否有NULL(NO、YES) - Collation string // 字符集 - Privileges string // 权限s - Key string // 键类型 - Default string // 默认值 - Extra string // 其他 - Comment string // 备注 -} - -// NewTableDesc 初始化一个*TableDesc -func NewTableDesc(tableName string) *TableDesc { - return &TableDesc{ - Name: tableName, - DescValues: make([]TableDescValue, 0), - } -} - -// ShowColumns 获取 DB 中所有的 columns -func (db *Connector) ShowColumns(tableName string) (*TableDesc, error) { - tbDesc := NewTableDesc(tableName) - - // 执行 show create table - res, err := db.Query("show full columns from `%s`.`%s`", db.Database, tableName) - if err != nil { - return nil, err - } - - field := res.Result.Map("Field") - tp := res.Result.Map("Type") - null := res.Result.Map("Null") - key := res.Result.Map("Key") - def := res.Result.Map("Default") - extra := res.Result.Map("Extra") - collation := res.Result.Map("Collation") - privileges := res.Result.Map("Privileges") - comm := res.Result.Map("Comment") - - // 获取值 - for _, row := range res.Rows { - value := TableDescValue{ - Field: row.Str(field), - Type: row.Str(tp), - Null: row.Str(null), - Key: row.Str(key), - Default: row.Str(def), - Extra: row.Str(extra), - Privileges: row.Str(privileges), - Collation: row.Str(collation), - Comment: row.Str(comm), - } - tbDesc.DescValues = append(tbDesc.DescValues, value) - } - return tbDesc, err -} - -// Columns 用于获取TableDesc中所有列的名称 -func (td TableDesc) Columns() []string { - var cols []string - for _, col := range td.DescValues { - cols = append(cols, col.Field) - } - return cols -} - -// showCreate show create -func (db *Connector) showCreate(createType, name string) (string, error) { - // 执行 show create table - res, err := db.Query("show create %s `%s`", createType, name) - if err != nil { - return "", err - } - - // 获取ddl - var ddl string - for _, row := range res.Rows { - ddl = row.Str(1) - } - - return ddl, err -} - -// ShowCreateDatabase show create database -func (db *Connector) ShowCreateDatabase(dbName string) (string, error) { - defer func() { - err := recover() - if err != nil { - common.Log.Error("recover ShowCreateDatabase()", err) - } - }() - return db.showCreate("database", dbName) -} - -// ShowCreateTable show create table -func (db *Connector) ShowCreateTable(tableName string) (string, error) { - defer func() { - err := recover() - if err != nil { - common.Log.Error("recover ShowCreateTable()", err) - } - }() - - ddl, err := db.showCreate("table", tableName) - - // 去除外键关联条件 - var noConstraint []string - relationReg, _ := regexp.Compile("CONSTRAINT") - for _, line := range strings.Split(ddl, "\n") { - - if relationReg.Match([]byte(line)) { - continue - } - - // 去除外键语句会使DDL中多一个','导致语法错误,要把多余的逗号去除 - if strings.Index(line, ")") == 0 { - lineWrongSyntax := noConstraint[len(noConstraint)-1] - // 如果')'前一句的末尾是',' 删除 ',' 保证语法正确性 - if strings.Index(lineWrongSyntax, ",") == len(lineWrongSyntax)-1 { - noConstraint[len(noConstraint)-1] = lineWrongSyntax[:len(lineWrongSyntax)-1] - } - } - - noConstraint = append(noConstraint, line) - } - - return strings.Join(noConstraint, "\n"), err -} - -// FindColumn find column -func (db *Connector) FindColumn(name, dbName string, tables ...string) ([]*common.Column, error) { - // 执行 show create table - var columns []*common.Column - sql := fmt.Sprintf("SELECT "+ - "c.TABLE_NAME,c.TABLE_SCHEMA,c.COLUMN_TYPE,c.CHARACTER_SET_NAME, c.COLLATION_NAME "+ - "FROM `INFORMATION_SCHEMA`.`COLUMNS` as c where c.COLUMN_NAME = '%s' ", name) - - if len(tables) > 0 { - var tmp []string - for _, table := range tables { - tmp = append(tmp, "'"+table+"'") - } - sql += fmt.Sprintf(" and c.table_name in (%s)", strings.Join(tmp, ",")) - } - - if dbName != "" { - sql += fmt.Sprintf(" and c.table_schema = '%s'", dbName) - } - - res, err := db.Query(sql) - if err != nil { - common.Log.Error("(db *Connector) FindColumn Error : ", err) - return columns, err - } - - tbName := res.Result.Map("TABLE_NAME") - schema := res.Result.Map("TABLE_SCHEMA") - colTyp := res.Result.Map("COLUMN_TYPE") - colCharset := res.Result.Map("CHARACTER_SET_NAME") - collation := res.Result.Map("COLLATION_NAME") - - // 获取ddl - for _, row := range res.Rows { - col := &common.Column{ - Name: name, - Table: row.Str(tbName), - DB: row.Str(schema), - DataType: row.Str(colTyp), - Character: row.Str(colCharset), - Collation: row.Str(collation), - } - - // 填充字符集和排序规则 - if col.Character == "" { - // 当从`INFORMATION_SCHEMA`.`COLUMNS`表中查询不到相关列的character和collation的信息时 - // 认为该列使用的character和collation与其所处的表一致 - // 由于`INFORMATION_SCHEMA`.`TABLES`表中未找到表的character,所以从按照MySQL中collation的规则从中截取character - - sql = fmt.Sprintf("SELECT `t`.`TABLE_COLLATION` FROM `INFORMATION_SCHEMA`.`TABLES` AS `t` "+ - "WHERE `t`.`TABLE_NAME`='%s' AND `t`.`TABLE_SCHEMA` = '%s'", col.Table, col.DB) - var newRes *QueryResult - newRes, err = db.Query(sql) - if err != nil { - common.Log.Error("(db *Connector) FindColumn Error : ", err) - return columns, err - } - - tbCollation := newRes.Rows[0].Str(0) - if tbCollation != "" { - col.Character = strings.Split(tbCollation, "_")[0] - col.Collation = tbCollation - } - } - - columns = append(columns, col) - } - - return columns, err -} - -// IsFKey 判断列是否是外键 -func (db *Connector) IsFKey(dbName, tbName, column string) bool { - sql := fmt.Sprintf("SELECT REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE C "+ - "WHERE REFERENCED_TABLE_SCHEMA <> 'NULL' AND"+ - " TABLE_NAME='%s' AND"+ - " TABLE_SCHEMA='%s' AND"+ - " COLUMN_NAME='%s'", tbName, dbName, column) - - res, err := db.Query(sql) - if err == nil && len(res.Rows) == 0 { - return false - } - - return true -} - -// Reference 用于存储关系 -type Reference map[string][]ReferenceValue - -// ReferenceValue 用于处理表之间的关系 -type ReferenceValue struct { - RefDBName string // 夫表所属数据库 - RefTable string // 父表 - DBName string // 子表所属数据库 - Table string // 子表 - ConstraintName string // 关系名称 -} - -// ShowReference 查找所有的外键信息 -func (db *Connector) ShowReference(dbName string, tbName ...string) ([]ReferenceValue, error) { - var referenceValues []ReferenceValue - sql := `SELECT C.REFERENCED_TABLE_SCHEMA,C.REFERENCED_TABLE_NAME,C.TABLE_SCHEMA,C.TABLE_NAME,C.CONSTRAINT_NAME -FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE C JOIN INFORMATION_SCHEMA. TABLES T ON T.TABLE_NAME = C.TABLE_NAME -WHERE C.REFERENCED_TABLE_NAME IS NOT NULL` - sql = sql + fmt.Sprintf(` AND C.TABLE_SCHEMA = "%s"`, dbName) - - if len(tbName) > 0 { - extra := fmt.Sprintf(` AND C.TABLE_NAME IN ("%s")`, strings.Join(tbName, `","`)) - sql = sql + extra - } - - // 执行SQL查找外键关联关系 - res, err := db.Query(sql) - if err != nil { - return referenceValues, err - } - - refDb := res.Result.Map("REFERENCED_TABLE_SCHEMA") - refTb := res.Result.Map("REFERENCED_TABLE_NAME") - schema := res.Result.Map("TABLE_SCHEMA") - tb := res.Result.Map("TABLE_NAME") - cName := res.Result.Map("CONSTRAINT_NAME") - - // 获取值 - for _, row := range res.Rows { - value := ReferenceValue{ - RefDBName: row.Str(refDb), - RefTable: row.Str(refTb), - DBName: row.Str(schema), - Table: row.Str(tb), - ConstraintName: row.Str(cName), - } - referenceValues = append(referenceValues, value) - } - - return referenceValues, err - -} diff --git a/vendor/github.com/XiaoMi/soar/database/show_test.go b/vendor/github.com/XiaoMi/soar/database/show_test.go deleted file mode 100644 index 66f98d4..0000000 --- a/vendor/github.com/XiaoMi/soar/database/show_test.go +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "fmt" - "testing" - - "github.com/kr/pretty" - "vitess.io/vitess/go/vt/sqlparser" -) - -func TestShowTableStatus(t *testing.T) { - connTest.Database = "information_schema" - ts, err := connTest.ShowTableStatus("TABLES") - if err != nil { - t.Error("ShowTableStatus Error: ", err) - } - pretty.Println(ts) -} - -func TestShowTables(t *testing.T) { - connTest.Database = "information_schema" - ts, err := connTest.ShowTables() - if err != nil { - t.Error("ShowTableStatus Error: ", err) - } - pretty.Println(ts) -} - -func TestShowCreateTable(t *testing.T) { - connTest.Database = "information_schema" - ts, err := connTest.ShowCreateTable("TABLES") - if err != nil { - t.Error("ShowCreateTable Error: ", err) - } - fmt.Println(ts) - stmt, err := sqlparser.Parse(ts) - pretty.Println(stmt, err) -} - -func TestShowIndex(t *testing.T) { - connTest.Database = "information_schema" - ti, err := connTest.ShowIndex("TABLES") - if err != nil { - t.Error("ShowIndex Error: ", err) - } - pretty.Println(ti.FindIndex(IndexKeyName, "idx_store_id_film_id")) -} - -func TestShowColumns(t *testing.T) { - connTest.Database = "information_schema" - ti, err := connTest.ShowColumns("TABLES") - if err != nil { - t.Error("ShowColumns Error: ", err) - } - pretty.Println(ti) -} - -func TestFindColumn(t *testing.T) { - ti, err := connTest.FindColumn("id", "") - if err != nil { - t.Error("FindColumn Error: ", err) - } - pretty.Println(ti) -} - -func TestShowReference(t *testing.T) { - rv, err := connTest.ShowReference("test2", "homeImg") - if err != nil { - t.Error("ShowReference Error: ", err) - } - pretty.Println(rv) -} - -func TestIsFKey(t *testing.T) { - if !connTest.IsFKey("sakila", "film", "language_id") { - t.Error("want True. got false") - } -} diff --git a/vendor/github.com/XiaoMi/soar/database/trace.go b/vendor/github.com/XiaoMi/soar/database/trace.go deleted file mode 100644 index 0ba611a..0000000 --- a/vendor/github.com/XiaoMi/soar/database/trace.go +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "errors" - "fmt" - "io" - "regexp" - "strings" - "time" - - "github.com/XiaoMi/soar/common" - - "vitess.io/vitess/go/vt/sqlparser" -) - -// Trace 用于存放 Select * From Information_Schema.Optimizer_Trace;输出的结果 -type Trace struct { - Rows []TraceRow -} - -// TraceRow 中含有trace的基本信息 -type TraceRow struct { - Query string - Trace string - MissingBytesBeyondMaxMemSize int - InsufficientPrivileges int -} - -// Trace 执行SQL,并对其Trace -func (db *Connector) Trace(sql string, params ...interface{}) (*QueryResult, error) { - common.Log.Debug("Trace SQL: %s", sql) - if common.Config.TestDSN.Version < 50600 { - return nil, errors.New("version < 5.6, not support trace") - } - - // 过滤不需要 Trace 的 SQL - switch sqlparser.Preview(sql) { - case sqlparser.StmtSelect, sqlparser.StmtUpdate, sqlparser.StmtDelete: - sql = "explain " + sql - case sqlparser.EXPLAIN: - default: - return nil, errors.New("no need trace") - } - - // 测试环境如果检查是关闭的,则SQL不会被执行 - if common.Config.TestDSN.Disable { - return nil, errors.New("Dsn Disable") - } - - // 数据库安全性检查:如果 Connector 的 IP 端口与 TEST 环境不一致,则启用SQL白名单 - // 不在白名单中的SQL不允许执行 - // 执行环境与test环境不相同 - if db.Addr != common.Config.TestDSN.Addr && db.dangerousQuery(sql) { - return nil, fmt.Errorf("query Execution Deny: Execute SQL with DSN(%s/%s) '%s'", - db.Addr, db.Database, fmt.Sprintf(sql, params...)) - } - - common.Log.Debug("Execute SQL with DSN(%s/%s) : %s", db.Addr, db.Database, sql) - conn := db.NewConnection() - - // 设置SQL连接超时时间 - conn.SetTimeout(time.Duration(common.Config.ConnTimeOut) * time.Second) - defer conn.Close() - err := conn.Connect() - if err != nil { - return nil, err - } - - // 添加SQL执行超时限制 - ch := make(chan QueryResult, 1) - go func() { - // 开启Trace - common.Log.Debug("SET SESSION OPTIMIZER_TRACE='enabled=on'") - _, _, err = conn.Query("SET SESSION OPTIMIZER_TRACE='enabled=on'") - common.LogIfError(err, "") - - // 执行SQL,抛弃返回结果 - result, err := conn.Start(sql, params...) - if err != nil { - ch <- QueryResult{ - Error: err, - } - return - } - row := result.MakeRow() - for { - err = result.ScanRow(row) - if err == io.EOF { - break - } - } - - // 返回Trace结果 - res := QueryResult{} - res.Rows, res.Result, res.Error = conn.Query("SELECT * FROM information_schema.OPTIMIZER_TRACE") - - // 关闭Trace - common.Log.Debug("SET SESSION OPTIMIZER_TRACE='enabled=off'") - _, _, err = conn.Query("SET SESSION OPTIMIZER_TRACE='enabled=off'") - if err != nil { - fmt.Println(err.Error()) - } - ch <- res - }() - - select { - case res := <-ch: - return &res, res.Error - case <-time.After(time.Duration(common.Config.QueryTimeOut) * time.Second): - return nil, errors.New("query execution timeout") - } -} - -// getTrace 获取trace信息 -func getTrace(res *QueryResult) Trace { - var rows []TraceRow - for _, row := range res.Rows { - rows = append(rows, TraceRow{ - Query: row.Str(0), - Trace: row.Str(1), - MissingBytesBeyondMaxMemSize: row.Int(2), - InsufficientPrivileges: row.Int(3), - }) - } - return Trace{Rows: rows} -} - -// FormatTrace 格式化输出Trace信息 -func FormatTrace(res *QueryResult) string { - explainReg := regexp.MustCompile(`(?i)^explain\s+`) - trace := getTrace(res) - str := []string{""} - for _, row := range trace.Rows { - str = append(str, "```sql") - sql := explainReg.ReplaceAllString(row.Query, "") - str = append(str, sql) - str = append(str, "```\n") - str = append(str, "```json") - str = append(str, row.Trace) - str = append(str, "```\n") - } - return strings.Join(str, "\n") -} diff --git a/vendor/github.com/XiaoMi/soar/database/trace_test.go b/vendor/github.com/XiaoMi/soar/database/trace_test.go deleted file mode 100644 index 8dea2d7..0000000 --- a/vendor/github.com/XiaoMi/soar/database/trace_test.go +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package database - -import ( - "flag" - "testing" - - "github.com/XiaoMi/soar/common" - - "github.com/kr/pretty" -) - -var update = flag.Bool("update", false, "update .golden files") - -func TestTrace(t *testing.T) { - common.Config.QueryTimeOut = 1 - res, err := connTest.Trace("select 1") - if err == nil { - common.GoldenDiff(func() { - pretty.Println(res) - }, t.Name(), update) - } else { - t.Error(err) - } -} - -func TestFormatTrace(t *testing.T) { - res, err := connTest.Trace("select 1") - if err == nil { - pretty.Println(FormatTrace(res)) - } else { - t.Error(err) - } -} - -func TestGetTrace(t *testing.T) { - res, err := connTest.Trace("select 1") - if err == nil { - pretty.Println(getTrace(res)) - } else { - t.Error(err) - } -} diff --git a/vendor/github.com/XiaoMi/soar/deps.sh b/vendor/github.com/XiaoMi/soar/deps.sh deleted file mode 100755 index afeb3e0..0000000 --- a/vendor/github.com/XiaoMi/soar/deps.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -NEEDED_COMMANDS="mysql docker git go govendor retool" - -for cmd in ${NEEDED_COMMANDS} ; do - if ! command -v "${cmd}" &> /dev/null ; then - echo -e "\033[91m${cmd} missing\033[0m" - exit 1 - else - echo "${cmd} found" - fi -done diff --git a/vendor/github.com/XiaoMi/soar/doc.go b/vendor/github.com/XiaoMi/soar/doc.go deleted file mode 100644 index 3c347f0..0000000 --- a/vendor/github.com/XiaoMi/soar/doc.go +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Package soar is a command-line tool for SQL optimizing and rewriting. -package soar diff --git a/vendor/github.com/XiaoMi/soar/env/doc.go b/vendor/github.com/XiaoMi/soar/env/doc.go deleted file mode 100644 index e0b20d0..0000000 --- a/vendor/github.com/XiaoMi/soar/env/doc.go +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Package env contain virtual database build, rehash, cleanup. -package env diff --git a/vendor/github.com/XiaoMi/soar/env/env.go b/vendor/github.com/XiaoMi/soar/env/env.go deleted file mode 100644 index 9b3f6ea..0000000 --- a/vendor/github.com/XiaoMi/soar/env/env.go +++ /dev/null @@ -1,546 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package env - -import ( - "fmt" - "strings" - "time" - - "github.com/XiaoMi/soar/ast" - "github.com/XiaoMi/soar/common" - "github.com/XiaoMi/soar/database" - - "github.com/dchest/uniuri" - "vitess.io/vitess/go/vt/sqlparser" -) - -// VirtualEnv SQL优化评审 测试环境 -// DB使用的信息从配置文件中获取 -type VirtualEnv struct { - *database.Connector - - // 保存DB测试环境映射关系,防止vEnv环境冲突。 - DBRef map[string]string // db -> optimizer_xxx - hash2Db map[string]string // optimizer_xxx -> db - // 保存Table创建关系,防止重复创建表 - TableMap map[string]map[string]string - // 错误 - Error error -} - -// NewVirtualEnv 初始化一个新的测试环境 -func NewVirtualEnv(vEnv *database.Connector) *VirtualEnv { - return &VirtualEnv{ - Connector: vEnv, - DBRef: make(map[string]string), - hash2Db: make(map[string]string), - TableMap: make(map[string]map[string]string), - } -} - -// BuildEnv 测试环境初始化&连接线上环境检查 -// @output *VirtualEnv 测试环境 -// @output *database.Connector 线上环境连接句柄 -func BuildEnv() (*VirtualEnv, *database.Connector) { - // 生成测试环境 - vEnv := NewVirtualEnv(&database.Connector{ - Addr: common.Config.TestDSN.Addr, - User: common.Config.TestDSN.User, - Pass: common.Config.TestDSN.Password, - Database: common.Config.TestDSN.Schema, - Charset: common.Config.TestDSN.Charset, - }) - - // 检查测试环境可用性,并记录数据库版本 - vEnvVersion, err := vEnv.Version() - common.Config.TestDSN.Version = vEnvVersion - if err != nil { - common.Log.Warn("BuildEnv TestDSN: %s:********@%s/%s not available , Error: %s", - vEnv.User, vEnv.Addr, vEnv.Database, err.Error()) - common.Config.TestDSN.Disable = true - } - - // 连接线上环境 - // 如果未配置线上环境线测试环境配置为线上环境 - if common.Config.OnlineDSN.Addr == "" { - common.Log.Warn("BuildEnv AllowOnlineAsTest: OnlineDSN not config, use TestDSN: %s:********@%s/%s as OnlineDSN", - vEnv.User, vEnv.Addr, vEnv.Database) - common.Config.OnlineDSN = common.Config.TestDSN - } - conn := &database.Connector{ - Addr: common.Config.OnlineDSN.Addr, - User: common.Config.OnlineDSN.User, - Pass: common.Config.OnlineDSN.Password, - Database: common.Config.OnlineDSN.Schema, - Charset: common.Config.OnlineDSN.Charset, - } - - // 检查线上环境可用性版本 - rEnvVersion, err := vEnv.Version() - common.Config.OnlineDSN.Version = rEnvVersion - if err != nil { - common.Log.Warn("BuildEnv OnlineDSN: %s:********@%s/%s not available , Error: %s", - vEnv.User, vEnv.Addr, vEnv.Database, err.Error()) - common.Config.TestDSN.Disable = true - } - - // 检查是否允许Online和Test一致,防止误操作 - if common.FormatDSN(common.Config.OnlineDSN) == common.FormatDSN(common.Config.TestDSN) && - !common.Config.AllowOnlineAsTest { - common.Log.Warn("BuildEnv AllowOnlineAsTest: %s:********@%s/%s OnlineDSN can't config as TestDSN", - vEnv.User, vEnv.Addr, vEnv.Database) - common.Config.TestDSN.Disable = true - common.Config.OnlineDSN.Disable = true - } - - // 判断测试环境与remote环境版本是否一致 - if vEnvVersion < rEnvVersion { - common.Log.Warning("TestDSN MySQL version older than OnlineDSN, TestDSN will not be used", vEnvVersion, rEnvVersion) - common.Config.TestDSN.Disable = true - } - - return vEnv, conn -} - -// RealDB 从测试环境中获取通过hash后的DB -func (ve VirtualEnv) RealDB(hash string) string { - if _, ok := ve.hash2Db[hash]; ok { - return ve.hash2Db[hash] - } - return hash -} - -// DBHash 从测试环境中根据DB找到对应的hash值 -func (ve VirtualEnv) DBHash(db string) string { - if _, ok := ve.DBRef[db]; ok { - return ve.DBRef[db] - } - return db -} - -// CleanUp 环境清理 -func (ve VirtualEnv) CleanUp() bool { - if !common.Config.TestDSN.Disable && common.Config.DropTestTemporary { - common.Log.Debug("CleanUp ...") - for db := range ve.hash2Db { - ve.Database = db - _, err := ve.Query("drop database %s", db) - if err != nil { - common.Log.Error("CleanUp failed Error: %s", err) - return false - } - } - common.Log.Debug("CleanUp, done") - } - return true -} - -// CleanupTestDatabase 清除一小时前的环境 -func (ve *VirtualEnv) CleanupTestDatabase() { - common.Log.Debug("CleanupTestDatabase ...") - dbs, err := ve.Query("show databases like 'optimizer%%'") - if err != nil { - common.Log.Error("CleanupTestDatabase failed Error:%s", err.Error()) - return - } - - // TODO: 1 hour should be config-able - minHour := 1 - for _, row := range dbs.Rows { - testDatabase := row.Str(0) - // test temporary database format `optimizer_YYMMDDHHmmss_randomString(16)` - if len(testDatabase) != 39 { - common.Log.Debug("CleanupTestDatabase by pass %s", testDatabase) - continue - } - s := strings.Split(testDatabase, "_") - pastTime, err := time.Parse("060102150405", s[1]) - if err != nil { - common.Log.Error("CleanupTestDatabase compute pastTime Error: %s", err.Error()) - continue - } - - subHour := time.Since(pastTime).Hours() - if subHour > float64(minHour) { - if _, err := ve.Query("drop database %s", testDatabase); err != nil { - common.Log.Error("CleanupTestDatabase failed Error: %s", err.Error()) - continue - } - common.Log.Debug("CleanupTestDatabase drop database %s success", testDatabase) - continue - } - common.Log.Debug("CleanupTestDatabase by pass database %s, %.2f less than %d hours", testDatabase, subHour, minHour) - } - - common.Log.Debug("CleanupTestDatabase done") -} - -// BuildVirtualEnv rEnv为SQL源环境,DB使用的信息从接口获取 -// 注意:如果是USE,DDL等语句,执行完第一条就会返回,后面的SQL不会执行 -func (ve *VirtualEnv) BuildVirtualEnv(rEnv *database.Connector, SQLs ...string) bool { - var stmt sqlparser.Statement - var err error - - // 置空错误信息 - ve.Error = nil - // 检测是否已经创建初始数据库,如果未创建则创建一个名称hash过的映射数据库 - err = ve.createDatabase(*rEnv, rEnv.Database) - common.LogIfWarn(err, "") - - // 测试环境检测 - if common.Config.TestDSN.Disable { - common.Log.Info("BuildVirtualEnv TestDSN not config") - return true - } - - // 判断rEnv中是否指定了DB - if rEnv.Database == "" { - common.Log.Error("BuildVirtualEnv no database specified, TestDSN init failed") - return false - } - - // 库表提取 - meta := make(map[string]*common.DB) - for _, sql := range SQLs { - - common.Log.Debug("BuildVirtualEnv Database&Table Mapping, SQL: %s", sql) - - stmt, err = sqlparser.Parse(sql) - if err != nil { - common.Log.Error("BuildVirtualEnv Error : %v", err) - return false - } - - // 语句类型判断 - switch stmt := stmt.(type) { - case *sqlparser.Use: - // 如果是use语句,则更改基础环配置 - if _, ok := meta[stmt.DBName.String()]; !ok { - // 如果USE了一个线上环境不存在的数据库,将创建该数据库,字符集默认utf8mb4 - meta[stmt.DBName.String()] = common.NewDB(stmt.DBName.String()) - rEnv.Database = stmt.DBName.String() - - // use DB 后检查 DB是否已经创建,如果没有创建则创建DB - err = ve.createDatabase(*rEnv, rEnv.Database) - common.LogIfWarn(err, "") - } - return true - case *sqlparser.DDL: - // 如果是DDL,则先获取DDL对应的表结构,然后直接在测试环境接执行SQL - // 为不影响其他SQL操作,复制一个Connector对象,将数据库切换到对应的DB上直接执行 - tmpDB := *ve.Connector - tmpDB.Database = ve.DBRef[rEnv.Database] - - // 为了支持并发,需要将DB进行映射,但db.table这种形式无法保证DB的映射是正确的 - // TODO:暂不支持 create db.tableName (id int) 形式的建表语句 - if stmt.Table.Qualifier.String() != "" { - common.Log.Error("BuildVirtualEnv DDL Not support '.'") - return false - } - - for _, tb := range stmt.FromTables { - if tb.Qualifier.String() != "" { - common.Log.Error("BuildVirtualEnv DDL Not support '.'") - return false - } - } - - for _, tb := range stmt.ToTables { - if tb.Qualifier.String() != "" { - common.Log.Error("BuildVirtualEnv DDL Not support '.'") - return false - } - } - - // 拉取表结构 - table := stmt.Table.Name.String() - if table != "" { - err = ve.createTable(*rEnv, rEnv.Database, table) - // 这里如果报错可能有两种可能: - // 1. SQL 是 Create 语句,线上环境并没有相关的库表结构 - // 2. 在测试环境中执行 SQL 报错 - // 如果是因为 Create 语句报错,后续会在测试环境中直接执行 create 语句,不会对程序有负面影响 - // 如果是因为执行 SQL 报错,那么其他地方执行 SQL 的时候也一定会报错 - // 所以这里不需要 `return false`,可以继续执行 - if err != nil { - common.Log.Error("BuildVirtualEnv Error : %v", err) - } - } - - _, err = tmpDB.Query(sql) - if err != nil { - switch stmt.Action { - case "create", "alter": - // 如果是创建或者修改语句,且报错信息为如重复建表、重复索引等信息,将错误反馈到上一次层输出建议 - ve.Error = err - default: - common.Log.Error("BuildVirtualEnv DDL Execute Error : %v", err) - } - } - return true - } - - meta := ast.GetMeta(stmt, nil) - - // 由于DB环境可能是变的,所以需要每一次都单独的提取库表结构,整体随着rEnv的变动而发生变化 - for db, table := range meta { - if db == "" { - db = rEnv.Database - } - tmpEnv := *rEnv - tmpEnv.Database = db - - // 创建数据库环境 - for _, tb := range table.Table { - if tb.TableName == "" { - continue - } - - // 视图检查 - common.Log.Debug("BuildVirtualEnv Checking view -- %s.%s", tmpEnv.Database, tb.TableName) - tbStatus, err := tmpEnv.ShowTableStatus(tb.TableName) - if err != nil { - common.Log.Error("BuildVirtualEnv ShowTableStatus Error : %v", err) - return false - } - - // 如果是视图,解析语句 - if len(tbStatus.Rows) > 0 && tbStatus.Rows[0].Comment == "VIEW" { - tmpEnv.Database = db - var viewDDL string - viewDDL, err = tmpEnv.ShowCreateTable(tb.TableName) - if err != nil { - common.Log.Error("BuildVirtualEnv create view failed: %v", err) - return false - } - - startIdx := strings.Index(viewDDL, "AS") - viewDDL = viewDDL[startIdx+2:] - if !ve.BuildVirtualEnv(&tmpEnv, viewDDL) { - return false - } - } - - err = ve.createTable(tmpEnv, db, tb.TableName) - if err != nil { - common.Log.Error("BuildVirtualEnv Error : %v", err) - return false - } - } - } - } - return true -} - -func (ve VirtualEnv) createDatabase(rEnv database.Connector, dbName string) error { - // 生成映射关系 - if _, ok := ve.DBRef[dbName]; ok { - common.Log.Debug("createDatabase, Database `%s` created", dbName) - return nil - } - - // optimizer_YYMMDDHHmmss_xxxx - dbHash := fmt.Sprintf("optimizer_%s_%s", time.Now().Format("060102150405"), uniuri.New()) - common.Log.Debug("createDatabase, mapping `%s` :`%s`-->`%s`", dbName, dbName, dbHash) - ddl, err := rEnv.ShowCreateDatabase(dbName) - if err != nil { - common.Log.Warning("createDatabase, rEnv.ShowCreateDatabase Error : %v", err) - ddl = fmt.Sprintf("create database `%s` character set utf8mb4", dbName) - } - - ddl = strings.Replace(ddl, dbName, dbHash, -1) - _, err = ve.Query(ddl) - if err != nil { - common.Log.Warning("createDatabase, Error : %v", err) - return err - } - - // 创建成功,添加映射记录 - ve.DBRef[dbName] = dbHash - ve.hash2Db[dbHash] = dbName - return nil -} - -/* - @input: - database.Connector 为一个线上环境数据库连接句柄的复制,因为在处理SQL时需要对上下文进行关联处理, - 所以存在修改DB连接参数(主要是数据库名称变更)的可能性,为了不影响整体上下文的环境,所以需要一个镜像句柄来做当前环境的操作。 - - dbName, tbName: 需要在环境中操作的库表名称, - - @output: - return 执行过程中的错误 - - NOTE: - 该函数会将线上环境中使用到的库表结构复制到测试环境中,为后续操作提供基础环境。 - 传入的库表名称均来自于对AST的解析,库表名称的获取遵循以下原则: - 如果未在SQL中指定数据库名称,则数据库一定是配置文件(或命令行参数传入DSN)中指定的数据库 - 如果一个SQL中存在多个数据库,则只能有一个数据库是没有在SQL中被显示指定的(即DSN中指定的数据库) - TODO: - 在一些可能的情况下,由于数据库配置的不一致(如SQL_MODE不同)导致remote环境的库表无法正确的在测试环境进行同步, - soar 能够做出判断并进行 session 级别的修改,但是这一阶段可用性保证应该是由用户提供两个完全相同(或测试环境兼容线上环境) - 的数据库环境来实现的。 -*/ -func (ve VirtualEnv) createTable(rEnv database.Connector, dbName, tbName string) error { - - if dbName == "" { - dbName = rEnv.Database - } - // 如果 dbName 不为空,说明指定了DB,临时修改rEnv中DB参数,来确保执行正确性 - rEnv.Database = dbName - - if ve.TableMap[dbName] == nil { - ve.TableMap[dbName] = make(map[string]string) - } - - if strings.ToLower(tbName) == "dual" { - common.Log.Debug("createTable, %s no need create", tbName) - return nil - } - - if ve.TableMap[dbName][tbName] != "" { - common.Log.Debug("createTable, `%s`.`%s` created", dbName, tbName) - return nil - } - - common.Log.Debug("createTable, Database: %s, Table: %s", dbName, tbName) - - // TODO:查看是否有外键关联(done),对外键的支持 (未解决循环依赖的问题) - - // 判断数据库是否已经创建 - if ve.DBRef[dbName] == "" { - // 若没创建,则创建数据库 - err := ve.createDatabase(rEnv, dbName) - if err != nil { - return err - } - } - - // 记录Table创建信息 - ve.TableMap[dbName][tbName] = tbName - - // 生成建表语句 - common.Log.Debug("createTable DSN(%s/%s): generate ddl", rEnv.Addr, rEnv.Database) - - ddl, err := rEnv.ShowCreateTable(tbName) - if err != nil { - // 有可能是用户新建表,因此线上环境查不到 - common.Log.Error("createTable, %s DDL Error : %v", tbName, err) - return err - } - - // 改变数据环境 - ve.Database = ve.DBRef[dbName] - _, err = ve.Query(ddl) - if err != nil { - // 有可能是用户新建表,因此线上环境查不到 - common.Log.Error("createTable, %s Error : %v", tbName, err) - return err - } - - // 泵取数据 - if common.Config.Sampling { - common.Log.Debug("createTable, Start Sampling data from %s.%s to %s.%s ...", dbName, tbName, ve.DBRef[dbName], tbName) - err := ve.SamplingData(rEnv, tbName) - if err != nil { - common.Log.Error(" (ve VirtualEnv) createTable SamplingData Error: %v", err) - return err - } - } - return nil -} - -// GenTableColumns 为Rewrite提供的结构体初始化 -func (ve *VirtualEnv) GenTableColumns(meta common.Meta) common.TableColumns { - tableColumns := make(common.TableColumns) - for dbName, db := range meta { - for _, tb := range db.Table { - // 防止传入非预期值 - if tb == nil { - break - } - td, err := ve.Connector.ShowColumns(tb.TableName) - if err != nil { - common.Log.Warn("GenTableColumns, ShowColumns Error: " + err.Error()) - break - } - - // tableColumns 初始化 - if dbName == "" { - dbName = ve.RealDB(ve.Connector.Database) - } - - if _, ok := tableColumns[dbName]; !ok { - tableColumns[dbName] = make(map[string][]*common.Column) - } - - if _, ok := tableColumns[dbName][tb.TableName]; !ok { - tableColumns[dbName][tb.TableName] = make([]*common.Column, 0) - } - - if len(tb.Column) == 0 { - // tb.column为空说明SQL里这个表是用的*来查询 - if err != nil { - common.Log.Error("ast.Rewrite ShowColumns, Error: %v", err) - break - } - - for _, colInfo := range td.DescValues { - tableColumns[dbName][tb.TableName] = append(tableColumns[dbName][tb.TableName], &common.Column{ - Name: colInfo.Field, - DB: dbName, - Table: tb.TableName, - DataType: colInfo.Type, - Character: colInfo.Collation, - Key: colInfo.Key, - Default: colInfo.Default, - Extra: colInfo.Extra, - Comment: colInfo.Comment, - Privileges: colInfo.Privileges, - Null: colInfo.Null, - }) - } - } else { - // tb.column如果不为空则需要把使用到的列填写进去 - var columns []*common.Column - for _, col := range tb.Column { - for _, colInfo := range td.DescValues { - if col.Name == colInfo.Field { - // 根据获取的信息将列的信息补全 - col.DB = dbName - col.Table = tb.TableName - col.DataType = colInfo.Type - col.Character = colInfo.Collation - col.Key = colInfo.Key - col.Default = colInfo.Default - col.Extra = colInfo.Extra - col.Comment = colInfo.Comment - col.Privileges = colInfo.Privileges - col.Null = colInfo.Null - - columns = append(columns, col) - break - } - } - } - tableColumns[dbName][tb.TableName] = columns - } - } - } - return tableColumns -} diff --git a/vendor/github.com/XiaoMi/soar/env/env_test.go b/vendor/github.com/XiaoMi/soar/env/env_test.go deleted file mode 100644 index 887c347..0000000 --- a/vendor/github.com/XiaoMi/soar/env/env_test.go +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Copyright 2018 Xiaomi, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package env - -import ( - "flag" - "testing" - - "github.com/XiaoMi/soar/common" - "github.com/XiaoMi/soar/database" - "github.com/kr/pretty" - "github.com/ziutek/mymysql/mysql" -) - -var connTest *database.Connector -var update = flag.Bool("update", false, "update .golden files") - -func init() { - common.BaseDir = common.DevPath - err := common.ParseConfig("") - common.LogIfError(err, "init ParseConfig") - connTest = &database.Connector{ - Addr: common.Config.TestDSN.Addr, - User: common.Config.TestDSN.User, - Pass: common.Config.TestDSN.Password, - Database: common.Config.TestDSN.Schema, - Charset: common.Config.TestDSN.Charset, - } -} - -func TestNewVirtualEnv(t *testing.T) { - testSQL := []string{ - "create table t(id int,c1 varchar(20),PRIMARY KEY (id));", - "alter table t add index `idx_c1`(c1);", - "alter table t add index `idx_c1`(c1);", - "select * from city where country_id = 44;", - "select * from address where address2 is not null;", - "select * from address where address2 is null;", - "select * from address where address2 >= 44;", - "select * from city where country_id between 44 and 107;", - "select * from city where city like 'Ad%';", - "select * from city where city = 'Aden' and country_id = 107;", - "select * from city where country_id > 31 and city = 'Aden';", - "select * from address where address_id > 8 and city_id < 400 and district = 'Nantou';", - "select * from address where address_id > 8 and city_id < 400;", - "select * from actor where last_update='2006-02-15 04:34:33' and last_name='CHASE' group by first_name;", - "select * from address where last_update >='2014-09-25 22:33:47' group by district;", - "select * from address group by address,district;", - "select * from address where last_update='2014-09-25 22:30:27' group by district,(address_id+city_id);", - "select * from customer where active=1 order by last_name limit 10;", - "select * from customer order by last_name limit 10;", - "select * from customer where address_id > 224 order by address_id limit 10;", - "select * from customer where address_id < 224 order by address_id limit 10;", - "select * from customer where active=1 order by last_name;", - "select * from customer where address_id > 224 order by address_id;", - "select * from customer where address_id in (224,510) order by last_name;", - "select city from city where country_id = 44;", - "select city,city_id from city where country_id = 44 and last_update='2006-02-15 04:45:25';", - "select city from city where country_id > 44 and last_update > '2006-02-15 04:45:25';", - "select * from city where country_id=1 and city='Kabul' order by last_update;", - "select * from city where country_id>1 and city='Kabul' order by last_update;", - "select * from city where city_id>251 order by last_update; ", - "select * from city i inner join country o on i.country_id=o.country_id;", - "select * from city i left join country o on i.city_id=o.country_id;", - "select * from city i right join country o on i.city_id=o.country_id;", - "select * from city i left join country o on i.city_id=o.country_id where o.country_id is null;", - "select * from city i right join country o on i.city_id=o.country_id where i.city_id is null;", - "select * from city i left join country o on i.city_id=o.country_id union select * from city i right join country o on i.city_id=o.country_id;", - "select * from city i left join country o on i.city_id=o.country_id where o.country_id is null union select * from city i right join country o on i.city_id=o.country_id where i.city_id is null;", - "select first_name,last_name,email from customer natural left join address;", - "select first_name,last_name,email from customer natural left join address;", - "select first_name,last_name,email from customer natural right join address;", - "select first_name,last_name,email from customer STRAIGHT_JOIN address on customer.address_id=address.address_id;", - "select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;", - } - - rEnv := connTest - - env := NewVirtualEnv(connTest) - defer env.CleanUp() - common.GoldenDiff(func() { - for _, sql := range testSQL { - env.BuildVirtualEnv(rEnv, sql) - switch err := env.Error.(type) { - case nil: - pretty.Println(sql, "OK") - case error: - // unexpected EOF - // 测试环境无法访问,或者被Disable的时候会进入这个分支 - pretty.Println(sql, err) - case *mysql.Error: - if err.Code != 1061 { - t.Error(err) - } - default: - t.Error(err) - } - } - }, t.Name(), update) -} - -func TestCleanupTestDatabase(t *testing.T) { - vEnv, _ := BuildEnv() - if common.Config.TestDSN.Disable { - common.Log.Warn("common.Config.TestDSN.Disable=true, by pass TestCleanupTestDatabase") - return - } - vEnv.Query("drop database if exists optimizer_060102150405_xxxxxxxxxxxxxxxx") - _, err := vEnv.Query("create database optimizer_060102150405_xxxxxxxxxxxxxxxx") - if err != nil { - t.Error(err) - } - vEnv.CleanupTestDatabase() - _, err = vEnv.Query("show create database optimizer_060102150405_xxxxxxxxxxxxxxxx") - if err == nil { - t.Error("optimizer_060102150405_xxxxxxxxxxxxxxxx exist, should be dropped") - } - - vEnv.Query("drop database if exists optimizer_060102150405") - _, err = vEnv.Query("create database optimizer_060102150405") - if err != nil { - t.Error(err) - } - vEnv.CleanupTestDatabase() - _, err = vEnv.Query("drop database optimizer_060102150405") - if err != nil { - t.Error("optimizer_060102150405 not exist, should not be dropped") - } -} - -func TestGenTableColumns(t *testing.T) { - vEnv, rEnv := BuildEnv() - defer vEnv.CleanUp() - - pretty.Println(common.Config.TestDSN.Disable) - if common.Config.TestDSN.Disable { - common.Log.Warn("common.Config.TestDSN.Disable=true, by pass TestGenTableColumns") - return - } - - // 只能对sakila数据库进行测试 - if rEnv.Database == "sakila" { - testSQL := []string{ - "select * from city where country_id = 44;", - "select country_id from city where country_id = 44;", - "select country_id from city where country_id > 44;", - } - - metaList := []common.Meta{ - { - "": &common.DB{ - Table: map[string]*common.Table{ - "city": common.NewTable("city"), - }, - }, - }, - { - "sakila": &common.DB{ - Table: map[string]*common.Table{ - "city": common.NewTable("city"), - }, - }, - }, - { - "sakila": &common.DB{ - Table: map[string]*common.Table{ - "city": { - TableName: "city", - Column: map[string]*common.Column{ - "country_id": { - Name: "country_id", - }, - }, - }, - }, - }, - }, - } - - for i, sql := range testSQL { - vEnv.BuildVirtualEnv(rEnv, sql) - tFlag := false - columns := vEnv.GenTableColumns(metaList[i]) - if _, ok := columns["sakila"]; ok { - if _, okk := columns["sakila"]["city"]; okk { - if length := len(columns["sakila"]["city"]); length >= 1 { - tFlag = true - } - } - } - - if !tFlag { - t.Errorf("columns: \n%s", pretty.Sprint(columns)) - } - } - } -} diff --git a/vendor/github.com/XiaoMi/soar/genver.sh b/vendor/github.com/XiaoMi/soar/genver.sh deleted file mode 100755 index 5255025..0000000 --- a/vendor/github.com/XiaoMi/soar/genver.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash - -## Generate Repository Version -version="$(git log --date=iso --pretty=format:"%cd" -1) $(git describe --tags --always)" -if [ "X${version}" == "X" ]; then - version="not a git repo" -fi - -git_dirty=$(git diff --no-ext-diff 2>/dev/null | wc -l) - -compile="$(date +"%F %T %z") by $(go version)" - -branch=$(git rev-parse --abbrev-ref HEAD) - -dev_path=$( - cd "$(dirname "$0")" || exit - pwd -) - -cat <common/version.go -package common - -// -version输出信息 -const ( - Version = "${version}" - Compile = "${compile}" - Branch = "${branch}" - GitDirty= ${git_dirty} - DevPath = "${dev_path}" -) -EOF diff --git a/vendor/github.com/XiaoMi/soar/retool-install.sh b/vendor/github.com/XiaoMi/soar/retool-install.sh deleted file mode 100755 index 877ef91..0000000 --- a/vendor/github.com/XiaoMi/soar/retool-install.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -# This script generates tools.json -# It helps record what releases/branches are being used -which retool >/dev/null || go get -u github.com/twitchtv/retool - -# This tool can run other checks in a standardized way -retool add gopkg.in/alecthomas/gometalinter.v2 v2.0.11 - -# check spelling -# misspell works with gometalinter -retool add github.com/client9/misspell/cmd/misspell v0.3.4 -# goword adds additional capability to check comments -retool add github.com/chzchzchz/goword a9744cb52b033fe5c269df48eeef2c954526cd79 - -# checks correctness -retool add github.com/gordonklaus/ineffassign 7bae11eba15a3285c75e388f77eb6357a2d73ee2 -retool add honnef.co/go/tools/cmd/megacheck master -retool add github.com/dnephin/govet 4a96d43e39d340b63daa8bc5576985aa599885f6 - -# slow checks -retool add github.com/kisielk/errcheck v1.1.0 -retool add github.com/securego/gosec/cmd/gosec 1.0.0 - -# linter -retool add github.com/mgechev/revive 7773f47324c2bf1c8f7a5500aff2b6c01d3ed73b -retool add github.com/golangci/golangci-lint/cmd/golangci-lint v1.10 diff --git a/vendor/github.com/XiaoMi/soar/revive.toml b/vendor/github.com/XiaoMi/soar/revive.toml deleted file mode 100644 index ebe13dd..0000000 --- a/vendor/github.com/XiaoMi/soar/revive.toml +++ /dev/null @@ -1,51 +0,0 @@ -ignoreGeneratedHeader = false -severity = "error" -confidence = 0.8 -errorCode = 0 -warningCode = 0 - -[rule.blank-imports] -[rule.context-as-argument] -[rule.dot-imports] -[rule.error-return] -[rule.error-strings] -[rule.error-naming] -[rule.exported] -[rule.if-return] -[rule.var-naming] -[rule.package-comments] -[rule.range] -[rule.receiver-naming] -[rule.indent-error-flow] -[rule.superfluous-else] -[rule.modifies-parameter] - -# This can be checked by other tools like megacheck -#[rule.unreachable-code] - - -# Currently this makes too much noise, but should add it in -# and perhaps ignore it in a few files -#[rule.confusing-naming] -# severity = "warning" -#[rule.confusing-results] -# severity = "warning" -#[rule.unused-parameter] -# severity = "warning" -#[rule.deep-exit] -# severity = "warning" -#[rule.flag-parameter] -# severity = "warning" - - - -# Adding these will slow down the linter -# They are already provided by megacheck -#[rule.unexported-return] -#[rule.time-naming] -#[rule.errorf] - -# Adding these will slow down the linter -# Not sure if they are already provided by megacheck -#[rule.var-declaration] -#[rule.context-keys-type] diff --git a/vendor/github.com/XiaoMi/soar/tools.json b/vendor/github.com/XiaoMi/soar/tools.json deleted file mode 100644 index b9e5b2d..0000000 --- a/vendor/github.com/XiaoMi/soar/tools.json +++ /dev/null @@ -1,45 +0,0 @@ -{ - "Tools": [ - { - "Repository": "gopkg.in/alecthomas/gometalinter.v2", - "Commit": "46cc1ea3778b247666c2949669a3333c532fa9c6" - }, - { - "Repository": "github.com/client9/misspell/cmd/misspell", - "Commit": "7888c6b6ce89353cd98e196bce3c3f9e4cdf31f6" - }, - { - "Repository": "github.com/chzchzchz/goword", - "Commit": "a9744cb52b033fe5c269df48eeef2c954526cd79" - }, - { - "Repository": "github.com/gordonklaus/ineffassign", - "Commit": "7bae11eba15a3285c75e388f77eb6357a2d73ee2" - }, - { - "Repository": "github.com/dnephin/govet", - "Commit": "4a96d43e39d340b63daa8bc5576985aa599885f6" - }, - { - "Repository": "github.com/securego/gosec/cmd/gosec", - "Commit": "5fb530cda357c16175f2c049577d2030de735b28" - }, - { - "Repository": "github.com/kisielk/errcheck", - "Commit": "55d8f507faff4d6eddd0c41a3e713e2567fca4e5" - }, - { - "Repository": "github.com/mgechev/revive", - "Commit": "7773f47324c2bf1c8f7a5500aff2b6c01d3ed73b" - }, - { - "Repository": "github.com/golangci/golangci-lint/cmd/golangci-lint", - "Commit": "a2b901227c37337bce9860499a413db2b464481b" - }, - { - "Repository": "honnef.co/go/tools/cmd/megacheck", - "Commit": "88497007e8588ea5b6baee991f74a1607e809487" - } - ], - "RetoolVersion": "1.3.7" -} \ No newline at end of file diff --git a/vendor/vendor.json b/vendor/vendor.json index db63b83..b55cd07 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -8,48 +8,6 @@ "revision": "b12b22c5341f0c26d88c4d66176330500e84db68", "revisionTime": "2018-07-25T03:58:23Z" }, - { - "checksumSHA1": "bfIrhd+IsH6lmvRWzhSWWTwmmpw=", - "path": "github.com/XiaoMi/soar", - "revision": "bbf012d54a56327a43efd7ab576e841462662c7d", - "revisionTime": "2018-11-27T01:12:27Z" - }, - { - "checksumSHA1": "Ygylqjfz7kg06wX9OajY/1dQGPw=", - "path": "github.com/XiaoMi/soar/advisor", - "revision": "bbf012d54a56327a43efd7ab576e841462662c7d", - "revisionTime": "2018-11-27T01:12:27Z" - }, - { - "checksumSHA1": "fWjgVdgalpgU7pOudJ6l+HcUo+A=", - "path": "github.com/XiaoMi/soar/ast", - "revision": "bbf012d54a56327a43efd7ab576e841462662c7d", - "revisionTime": "2018-11-27T01:12:27Z" - }, - { - "checksumSHA1": "tJ9961Sn8qPjyroAvVHGrGoaeIY=", - "path": "github.com/XiaoMi/soar/cmd/soar", - "revision": "bbf012d54a56327a43efd7ab576e841462662c7d", - "revisionTime": "2018-11-27T01:12:27Z" - }, - { - "checksumSHA1": "oemy8Awsr7GYl+w/I1N0LZJTUVA=", - "path": "github.com/XiaoMi/soar/common", - "revision": "bbf012d54a56327a43efd7ab576e841462662c7d", - "revisionTime": "2018-11-27T01:12:27Z" - }, - { - "checksumSHA1": "CLW1yvXeT/gZLxIeSjHaGn95Ifw=", - "path": "github.com/XiaoMi/soar/database", - "revision": "bbf012d54a56327a43efd7ab576e841462662c7d", - "revisionTime": "2018-11-27T01:12:27Z" - }, - { - "checksumSHA1": "Z2P7fVYLlPq5fqwl1E+5wwbRKQY=", - "path": "github.com/XiaoMi/soar/env", - "revision": "bbf012d54a56327a43efd7ab576e841462662c7d", - "revisionTime": "2018-11-27T01:12:27Z" - }, { "checksumSHA1": "OksAzF6VvpfTlte99lpVNV+iSnY=", "path": "github.com/astaxie/beego/logs", -- GitLab