关于shardingsphere:详探-Apache-ShardingSphere-SQL-Parse-Format-功能

44次阅读

共计 78987 个字符,预计需要花费 198 分钟才能阅读完成。

图片

陈出新,SphereEx 中间件研发工程师,Apache ShardingSphere Committer,目前专一于 Apache ShardingSphere 内核模块的研发工作。

常常应用数据库的敌人们肯定见过无比简单的 SQL,以上面的 SQL 语句为例,你能立即看进去这条 SQL 的含意吗?

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">select</span> a.order_id,a.status,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">sum</span>(b.money) <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">as</span> money <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">from</span> t_order a <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">inner</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">join</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">select</span> c.order_id <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">as</span> order_id, c.number * d.price <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">as</span> money <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">from</span> t_order_detail c <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">inner</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">join</span> t_order_price d <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">on</span> c.s_id = d.s_id) b <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">on</span> a.order_id = b.order_id <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">where</span> b.money > <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">100</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">group</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">by</span> a.order_id
</pre></div>

通过格式化之后是不是容易了解多了:

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">SELECT</span> a . order_id , a . <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">status</span> , <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">SUM</span>(b . money) <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">AS</span> money
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">FROM</span> t_order a <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">INNER</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">JOIN</span> 
(
        <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">SELECT</span> c . order_id <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">AS</span> order_id, c . <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">number</span> * d . price <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">AS</span> money
        <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">FROM</span> t_order_detail c <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">INNER</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">JOIN</span> t_order_price d <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">ON</span> c . s_id = d . s_id
) b <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">ON</span> a . order_id = b . order_id
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">WHERE</span> 
        b . money > <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">100</span>
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">GROUP</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">BY</span> a . order_id;
</pre></div>

置信大家拿到简单 SQL 剖析的第一步就是格式化这个 SQL,而后能力基于格式化之后的内容进一步剖析 SQL 语义。SQL 的格式化性能也是泛滥数据库相干软件的必备性能之一。Apache ShardingSphere 基于这样的需要,依靠自带的数据库方言解析引擎,推出了本人的 SQL 格式化工具——SQL Parse Format。

SQL Parse Format 是 Apache ShardingSphere 解析引擎的性能之一,也将是将来布局版本中 SQL 审计性能的根底。 本文将率领读者深入浅出地了解 SQL Parse Format 性能,理解它的底层原理、应用形式以及如何参加 SQL Parse Format 开发。

Parser Engine

SQL Parse Format 作为 Apache ShardingSphere 解析引擎的性能之一,是解析引擎中独特并且绝对独立的性能。要了解 SQL Parse Format 性能,须要首先须要理解 Apache ShardingSphere 的解析引擎。

Apache ShardingSphere 解析引擎创立的初衷是为了提取 SQL 中的要害信息,例如用于分库分表的字段、加密改写的列等等内容。随着 Apache ShardingSphere 的一直倒退,解析引擎也经验了 3 代产品的更新迭代。

第一代解析引擎采纳 Druid 作为 SQL 解析器,它在 1.4.x 之前的版本应用,性能优异。第二代解析引擎采纳了全自研的形式,因为应用目标不同,第二代产品采纳对 SQL 半了解的形式,仅仅提取分片数据关怀的上下文信息,无需生成解析树,也不必二次遍历,因而性能和兼容性进一步晋升。第三代解析引擎采纳 ANTLR 作为解析引擎的生成器,从而生成解析树,而后再对解析树进行二次遍历拜访提取上下文信息。利用 ANTLR 作为解析引擎生成器之后,SQL 的兼容性得以大幅晋升,Apache ShardingSphere 的泛滥性能也可能基于这个根底疾速开展。5.0.x 的版本也对第三代解析引擎进行了大量的性能优化,包含将遍历形式从 Listener 变为 Visitor,为预编译的 SQL 语句增加解析后果缓存等等。

SQL Parse Format 性能的实现正是得益于第三代解析引擎的创立。接下来,就让咱们将眼光汇集到 SQL Parse Format 性能之上。

SQL Parse Format

SQL Parse Format 是一款 SQL 语句格式化的工具。SQL Parse Format 性能未来还会用于 SQL 审计性能之中,它能够不便用户查看历史 SQL、通过报表展现格式化的 SQL 或者对 SQL 作进一步的剖析解决。

例如如下 SQL 通过 SQL Parse Format 格式化之后会变成以下格局。它通过换行和关键字大写的形式让 SQL 的各个局部更加突出和清晰。

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">select</span> age <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">as</span> b, <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">name</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">as</span> n <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">from</span> table1 <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">join</span> table2 <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">where</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">id</span> = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">1</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">and</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">name</span> = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">'lu'</span>;
<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(101, 95, 109); word-wrap: inherit !important; word-break: inherit !important;">-- 格式化 </span>
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">SELECT</span> age <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">AS</span> b, <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">name</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">AS</span> n
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">FROM</span> table1 <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">JOIN</span> table2
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">WHERE</span> 
        <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">id</span> = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">1</span>
        <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">and</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">name</span> = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">'lu'</span>;
</pre></div>

理解了 SQL Parse Format 的基本功能之后,让咱们一起来探索 SQL Parse Format 背地的原理。

SQL Parse Format 原理解读

以如下 SQL 为例,咱们来一起探索它在 Apache ShardingSphere 中是如何被格式化的。

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">select</span> order_id <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">from</span> t_order <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">where</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">status</span> = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">'OK'</span>
</pre></div>

Apache ShardingSphere 采纳了 ANTLR4 作为解析引擎生成器工具,所以咱们首先依照 ANTLR4 的形式,在 <div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;”>.g4</p></div> 文件中定义了 <div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;”>select</p></div> 的语法(以 MySQL 为例)。

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>simpleSelect
    : <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">SELECT</span> ALL? targetList? intoClause? fromClause? whereClause? groupClause? havingClause? windowClause?
    | <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">SELECT</span> distinctClause targetList intoClause? fromClause? whereClause? groupClause? havingClause? windowClause?
    | valuesClause
    | <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">TABLE</span> relationExpr
    ;
</pre></div>

咱们通过 IDEA 的 ANTLR4 插件(https://plugins.jetbrains.com…)能够不便地查看 SQL 生成的语法树。

图片

ANTLR4 会将咱们定义的语法文件编译,首先对 SQL 进行词法剖析,将 SQL 拆分成不可分割的局部,也就是 token,并且会依据不同数据库提供的字典值,将这些 token 划分为关键字,表达式,字面量和操作符。例如,上图中咱们别离失去了关键字 <div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;”>SELECTFROMWHERE=</p></div> 以及变量 <div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;”>order_idt_orderstatusOK</p></div>。接着 ANTLR4 会将语法分析器的输入后果转化为上图的语法树。

联合 Apache ShardingSphere 中的源码,上述过程复现如下所示。

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="hljs-attribute" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">String</span> sql = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"select order_id from t_order where status ='OK'"</span>;
<span class="hljs-attribute"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">CacheOption</span> cacheOption = new CacheOption(<span class="hljs-number"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">128</span>, 1024L, <span class="hljs-number"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">4</span>);
<span class="hljs-attribute"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">SQLParserEngine</span> parserEngine = new SQLParserEngine(<span class="hljs-string"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"MySQL"</span>, cacheOption, <span class="hljs-literal"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">false</span>);
<span class="hljs-attribute"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">ParseContext</span> parseContext = parserEngine.parse(sql, <span class="hljs-literal"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">false</span>);
</pre></div>

Apache ShardingSphere 的 SQLParserEngine 是对 ANTLR4 解析的封装和形象,它会通过 SPI 的形式来加载数据库方言的解析器,用户能够通过 SPI 扩大点对数据方言进行进一步扩大。另外外部还减少了缓存机制,用来进步性能。咱们着重来看一下解析的相干代码。

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">public</span> ParseContext <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;">parse</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">final</span> String sql)</span> </span>{
    ParseASTNode result = twoPhaseParse(sql);
    <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">if</span> (result.getRootNode() <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">instanceof</span> ErrorNode) {
        <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">throw</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">new</span> SQLParsingException(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"Unsupported SQL of %s"</span>, sql);
    }
    <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">return</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">new</span> ParseContext(result.getRootNode(), result.getHiddenTokens());
}

<span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">private</span> ParseASTNode <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;">twoPhaseParse</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">final</span> String sql)</span> </span>{
    DatabaseTypedSQLParserFacade sqlParserFacade = DatabaseTypedSQLParserFacadeRegistry.getFacade(databaseType);
    SQLParser sqlParser = SQLParserFactory.newInstance(sql, sqlParserFacade.getLexerClass(), sqlParserFacade.getParserClass(), sqlCommentParseEnabled);
    <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">try</span> {
        ((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.SLL);
        <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">return</span> (ParseASTNode) sqlParser.parse();
    } <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">catch</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">final</span> ParseCancellationException ex) {
        ((Parser) sqlParser).reset();
        ((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.LL);
        <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">try</span> {
            <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">return</span> (ParseASTNode) sqlParser.parse();
        } <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">catch</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">final</span> ParseCancellationException e) {
            <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">throw</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">new</span> SQLParsingException(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"You have an error in your SQL syntax"</span>);
        }
    }
}
</pre></div>

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;”>twoPhaseParse</p></div> 是解析的外围,首先会依据数据库类型加载到对应解析类,接着通过反射机制,生成 ANTLR4 的解析器实例。而后通过 ANTLR4 官网提供的两种解析形式,首先进行疾速解析,疾速解析失败,会进行惯例解析,大部分 SQL 都可能通过疾速解析失去后果从而进步解析性能。解析过后,咱们便失去了解析树。

那么 Apache ShardingSphere 又是如何从解析树失去格式化的 SQL 呢?其实就是通过 Visitor 办法来实现的。ANTLR4 提供了两种拜访语法树的形式,包含 Listener 和 Visitor,ShardingSphere 采纳 Visitor 的形式来拜访语法树。下方代码展现了如何从语法树失去格式化 SQL 的形式。

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>SQLVisitorEngine visitorEngine = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">new</span> SQLVisitorEngine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"MySQL"</span>, <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"FORMAT"</span>, <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">new</span> Properties());
<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">String</span> result = visitorEngine.visit(parseContext);
</pre></div>

Apache ShardingSphere 的 <div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;”>SQLVisitorEngine</p></div> 也是对各种方言拜访器的形象和封装。外围办法如下。

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>public <<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">T</span>> <span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">T</span> visit(final ParseContext parseContext) {
    ParseTreeVisitor<<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">T</span>> visitor = SQLVisitorFactory.newInstance(databaseType, visitorType, SQLVisitorRule.valueOf(parseContext.getParseTree().getClass()), props);
    <span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">T</span> result = parseContext.getParseTree().accept(visitor);
    appendSQLComments(parseContext, result);
    <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">return</span> result;
}
</pre></div>

上述 visit 办法首先依据数据库类型以及拜访器类型来决定采纳的拜访器,外部也是通过反射的形式来实例化拜访器。目前 visitorType 反对两种形式,一种是 <div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;”>FORMAT</p></div> 也就是格式化,另外一种是 <div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;”>STATEMENT</p></div> 也就是 Apache ShardingSphere 罕用的形式,将 SQL 转化为 Statement 信息,提取相干上下文信息,为后续例如分库分表等性能服务。其实这也是 SQL Parse Format 性能和一般的解析引擎性能的惟一区别。接下来我还是以上述 SQL 为例,通过具体代码展现 Visitor 如何将 SQL 格式化。

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;”>MySQLFormatSQLVisitor</p></div> 负责该 SQL 的拜访工作,通过 DEBUG 代码咱们能够清晰地看到本次拜访的执行门路,如下图所示。Visitor 对语法树的各个局部进行了遍历,ANTLR4 会依据定义的语法规定对各个节点的拜访生成默认办法。Apache ShardingSphere 对要害办法进行了笼罩,从而实现 SQL 的格式化性能。

通过如下代码片段能够帮忙咱们了解 Visitor 实现格式化的形式。当 Visitor 遍历到了 select 之后,会首先对其格式化,接着拜访 projection,projection 外部格式化的实现,会进一步通过 visitProjections 办法实现。在拜访 from 之前会先空行解决。Visitor 实例化的对象外部会保护一个 StringBuilder 用来存储格式化的后果。因为每条 SQL 应用的解析器和拜访器都是新实例化的对象,因而也不会存在线程平安的问题。最初遍历完结后,Apache ShardingSphere 会将 StringBuilder 的后果输入,那么咱们也就失去了格式化的 SQL。

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">public</span> String <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;">visitQuerySpecification</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">final</span> QuerySpecificationContext ctx)</span> </span>{
    formatPrint(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"SELECT"</span>);
    <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">int</span> selectSpecCount = ctx.selectSpecification().size();
    <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">for</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">int</span> i = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">0</span>; i < selectSpecCount; i++) {
        visit(ctx.selectSpecification(i));
        formatPrint(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">""</span>);
    }
    visit(ctx.projections());
    <span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">if</span> (<span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">null</span> != ctx.fromClause()) {
        formatPrintln();
        visit(ctx.fromClause());
    }
    <span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">if</span> (<span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">null</span> != ctx.whereClause()) {
        formatPrintln();
        visit(ctx.whereClause());
    }
    <span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">if</span> (<span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">null</span> != ctx.groupByClause()) {
        formatPrintln();
        visit(ctx.groupByClause());
    }
    <span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">if</span> (<span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">null</span> != ctx.havingClause()) {
        formatPrintln();
        visit(ctx.havingClause());
    }
    <span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">if</span> (<span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">null</span> != ctx.windowClause()) {
        formatPrintln();
        visit(ctx.windowClause());
    }
    <span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">return</span> result.toString();
}
</pre></div>

置信通过上述的过程剖析和代码展现,读者敌人们都可能大略理解到 SQL Parse Format 的原理了。

SQL Parse Format 使用指南

在理解了 SQL Parse Format 的原理之后,应用 SQL Parse Format 也非常简单了。

对于 Java 利用而言,只须要增加依赖,调用 api 即可。

  • 引入依赖

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>></span>
    <span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>></span>org.apache.shardingsphere<span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>></span>
    <span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>></span>shardingsphere-sql-parser-engine<span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>></span>
    <span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">version</span>></span>${project.version}<span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span><br><span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span><br><br><span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>org.apache.shardingsphere<span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>shardingsphere-sql-parser-mysql<span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>${project.version}<span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">version</span>></span>
<span class="hljs-tag" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(190, 70, 120); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>></span>
</pre></div>

  • 调用 api

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">static</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">void</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;">main</span>(<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">String[] args</span>) </span>{
    String sql = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"select order_id from t_order where status ='OK'"</span>;
    CacheOption cacheOption = <span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">new</span> CacheOption(<span class="hljs-number"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">128</span>, <span class="hljs-number"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">1024</span>L, <span class="hljs-number"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">4</span>);
    SQLParserEngine parserEngine = <span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">new</span> SQLParserEngine(<span class="hljs-string"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"MySQL"</span>, cacheOption, <span class="hljs-literal"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">false</span>);
    ParseContext parseContext = parserEngine.parse(sql, <span class="hljs-literal"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">false</span>);
    SQLVisitorEngine visitorEngine = <span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">new</span> SQLVisitorEngine(<span class="hljs-string"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"MySQL"</span>, <span class="hljs-string"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"FORMAT"</span>, <span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">new</span> Properties());
    String result = visitorEngine.visit(parseContext);
    System.<span class="hljs-keyword"style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;">out</span>.println(result);
}
</pre></div>

  • Properties 反对参数如下

表格

如果应用 ShardingSphere-Proxy 那么还能够通过 DistSQL 语法来应用 SQL Parse Format 性能。
<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>mysql> FORMAT <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">select</span> order_id from t_user where <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">status</span> = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">'OK'</span>;
+<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(101, 95, 109); word-wrap: inherit !important; word-break: inherit !important;">-----------------------------------------------------+</span>
| formatted_result                                    |
+<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(101, 95, 109); word-wrap: inherit !important; word-break: inherit !important;">-----------------------------------------------------+</span>
| SELECT order_id
FROM t_user
WHERE
        <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">status</span> = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">'OK'</span>; |
+<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(101, 95, 109); word-wrap: inherit !important; word-break: inherit !important;">-----------------------------------------------------+</span>
</pre></div>

对于前文提到的 Statement 形式的解析引擎,也能够不便地查看将 SQL 转化为 SQLStatement 的后果。

<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>mysql> parse SELECT id, name FROM t_user WHERE <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">status</span> = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">'ACTIVE'</span> AND age > <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">18</span>;
+<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(101, 95, 109); word-wrap: inherit !important; word-break: inherit !important;">----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+</span>
| parsed_statement     | parsed_statement_detail                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(101, 95, 109); word-wrap: inherit !important; word-break: inherit !important;">----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+</span>
| MySQLSelectStatement | {<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"projections"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">7</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">14</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"distinctRow"</span>:<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">false</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"projections"</span>:[{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"column"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">7</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">8</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"identifier"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"value"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"id"</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"quoteCharacter"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"NONE"</span>}}},{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"column"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">11</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">14</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"identifier"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"value"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"name"</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"quoteCharacter"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"NONE"</span>}}}]},<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"from"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"tableName"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">21</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">26</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"identifier"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"value"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"t_user"</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"quoteCharacter"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"NONE"</span>}}},<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"where"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">28</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">63</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"expr"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">34</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">63</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"left"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">34</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">50</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"left"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">34</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">39</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"identifier"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"value"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"status"</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"quoteCharacter"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"NONE"</span>}},<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"right"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">43</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">50</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"literals"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"ACTIVE"</span>},<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"operator"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"\u003d"</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"text"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"status \u003d \u0027ACTIVE\u0027"</span>},<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"right"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">56</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">63</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"left"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">56</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">58</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"identifier"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"value"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"age"</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"quoteCharacter"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"NONE"</span>}},<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"right"</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"startIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">62</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"stopIndex"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">63</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"literals"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">18</span>},<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"operator"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"\u003e"</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"text"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"age \u003e 18"</span>},<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"operator"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"AND"</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"text"</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"status \u003d \u0027ACTIVE\u0027 AND age \u003e 18"</span>}},<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"unionSegments"</span>:[],<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"parameterCount"</span>:<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;">0</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;">"commentSegments"</span>:[]} |
+<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(101, 95, 109); word-wrap: inherit !important; word-break: inherit !important;">----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+</span>
</pre></div>

更多 DistSQL 性能欢送查看 DistSQL 文档:https://shardingsphere.apache.org/document/current/cn/concepts/distsql/

结语

目前 Apache ShardingSphere 只实现了 MySQL 方言的格式化性能,其它方言还未实现,置信理解了原理和应用的你肯定也可能轻松参加到 SQL Parse Format 性能的开发中。如果你对参加奉献开源代码有趣味,也欢送来到社区参加奉献,置信通过奉献代码,你能更粗浅地了解 Apache ShardingSphere 的相干性能。

GitHub issue:

https://github.com/apache/shardingsphere/issues

奉献指南:

https://shardingsphere.apache.org/community/cn/contribute/

中文社区:

https://community.sphere-ex.com/

正文完
 0