1. 程式人生 > >DLA SQL分析函式:SQL語句審計與分析的利器

DLA SQL分析函式:SQL語句審計與分析的利器

1. 簡介

Data Lake Analytics最新release一組SQL內建函式,用來進行SQL語句的分析、資訊提取,方便使用者對SQL語句進行語法層面的審計、分析,可以應用於很多安全、資料庫日誌分析等場景。參考DLA(Data Lake Analytics)之前的文件,只要您的SQL語句文字儲存在DLA支援的資料來源系統中,就能使用DLA的SQL分析函式,方便、快捷的進行SQL語句的審計和分析,或者基於此構建相關應用程式和系統。

前提條件,您的SQL語句文字已經作為一個字串、文字欄位儲存在DLA支援的資料來源中,或者在統一格式的日誌檔案中,日誌檔案放到OSS中(應用自己上傳、開源日誌工具上傳、日誌服務投遞、Datahub投遞等),然後採用DLA進行SQL分析。

 

2. SQL分析函式詳解

所有提供的SQL分析函式都是scalar標量函式。SQL分析函式支援多種SQL方言,預設SQL方言是mysql。

SQL方言
mysql
postgresql
oracle
db2
sqlserver
hive
odps

支援的函式列表:

Name Description
sql_export_columns 提取SQL語句中所有出現的列
sql_export_functions 提取SQL語句中所有出現的函式
sql_export_predicate_columns 提取SQL語句中所有出現的謂詞條件表示式涉及的列
sql_export_predicates 提取SQL語句中所有出現的謂詞條件表示式
sql_export_select_list_columns 提取SQL語句中SELECT子句表示式中出現的列(包括子查詢)
sql_export_tables 提取SQL語句中所有出現的表
sql_format 對SQL語句進行格式化
sql_params 提取SQL語句中所有的literal值
sql_pattern 提取SQL語句引數化後的SQL pattern,literal用?代替
sql_pattern_hash 提取sqlText的SQL語句引數化後的SQL pattern,並生成hash值
sql_syntax_check 對sqlText進行語法檢查

 

2.1 sql_export_columns

函式呼叫形式:

  • sql_export_columns(sqlText)
  • sql_export_columns(sqlText, dbType)
  • sql_export_columns(sqlText, dbType, throwError)

引數說明:

引數名 型別 說明
sqlText VARCHAR 必選
dbType VARCHAR 可選。SQL方言,預設為mysql
throwError BOOLEAN 可選。true時,遇到非法SQL丟擲異常;false時,遇到非法SQL不丟擲異常,返回null

返回值:

型別 說明
VARCHAR sqlText的SQL語句中所有出現的列,用逗號分隔,列所屬的表會進行關聯推導,如果沒有找到明確的表,則為UNKNOWN

示例:

SELECT sql_export_columns(a.sql_text)
FROM (
SELECT 'SELECT   s_acctbal, 
         s_name, 
         n_name, 
         p_partkey, 
         p_mfgr, 
         s_address, 
         s_phone, 
         s_comment 
FROM     part, 
         supplier, 
         partsupp, 
         nation, 
         region 
WHERE    p_partkey = ps_partkey 
AND      s_suppkey = ps_suppkey 
AND      p_size = 35 
AND      p_type LIKE ''%NICKEL'' 
AND      s_nationkey = n_nationkey 
AND      n_regionkey = r_regionkey 
AND      r_name = ''MIDDLE EAST'' 
AND      ps_supplycost IN 
         ( 
                SELECT min(ps_supplycost) 
                FROM   partsupp, 
                       supplier, 
                       nation, 
                       region 
                WHERE  s_suppkey = ps_suppkey 
                AND    s_nationkey = n_nationkey 
                AND    n_regionkey = r_regionkey 
                AND    r_name = ''MIDDLE EAST'' ) 
ORDER BY s_acctbal DESC, 
         n_name, 
         s_name, 
         p_partkey 
LIMIT    100;' AS sql_text
) a;


+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_export_columns(a.sql_text)                                                                                                                                                                                                                                                                                                                  |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| UNKNOWN.s_acctbal,UNKNOWN.s_name,UNKNOWN.n_name,UNKNOWN.p_partkey,UNKNOWN.p_mfgr,UNKNOWN.s_address,UNKNOWN.s_phone,UNKNOWN.s_comment,UNKNOWN.ps_partkey,UNKNOWN.s_suppkey,UNKNOWN.ps_suppkey,UNKNOWN.p_size,UNKNOWN.p_type,UNKNOWN.s_nationkey,UNKNOWN.n_nationkey,UNKNOWN.n_regionkey,UNKNOWN.r_regionkey,UNKNOWN.r_name,UNKNOWN.ps_supplycost |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

 

 

2.2 sql_export_functions

函式呼叫形式:

  • sql_export_functions(sqlText)
  • sql_export_functions(sqlText, dbType)
  • sql_export_functions(sqlText, dbType, throwError)

引數說明:

引數名 型別 說明
sqlText VARCHAR 必選
dbType VARCHAR 可選。SQL方言,預設為mysql
throwError BOOLEAN 可選。true時,遇到非法SQL丟擲異常;false時,遇到非法SQL不丟擲異常,返回null

返回值:

型別 說明
VARCHAR sqlText的SQL語句中所有出現的函式名,用逗號分隔,列所屬的表會進行關聯推導,如果沒有找到明確的表,則為UNKNOWN

示例:

SELECT sql_export_functions(a.sql_text)
FROM (
SELECT 'SELECT   s_acctbal, 
         s_name, 
         n_name, 
         p_partkey, 
         p_mfgr, 
         s_address, 
         s_phone, 
         s_comment 
FROM     part, 
         supplier, 
         partsupp, 
         nation, 
         region 
WHERE    p_partkey = ps_partkey 
AND      s_suppkey = ps_suppkey 
AND      p_size = 35 
AND      p_type LIKE ''%NICKEL'' 
AND      s_nationkey = n_nationkey 
AND      n_regionkey = r_regionkey 
AND      r_name = ''MIDDLE EAST'' 
AND      ps_supplycost IN 
         ( 
                SELECT min(ps_supplycost) 
                FROM   partsupp, 
                       supplier, 
                       nation, 
                       region 
                WHERE  s_suppkey = ps_suppkey 
                AND    s_nationkey = n_nationkey 
                AND    n_regionkey = r_regionkey 
                AND    r_name = ''MIDDLE EAST'' ) 
ORDER BY s_acctbal DESC, 
         n_name, 
         s_name, 
         p_partkey 
LIMIT    100;' AS sql_text
) a;


+----------------------------------+
| sql_export_functions(a.sql_text) |
+----------------------------------+
| min                              |
+----------------------------------+

 

2.3 sql_export_predicate_columns

函式呼叫形式:

  • sql_export_predicate_columns(sqlText)
  • sql_export_predicate_columns(sqlText, dbType)
  • sql_export_predicate_columns(sqlText, dbType, throwError)

引數說明:

引數名 型別 說明
sqlText VARCHAR 必選
dbType VARCHAR 可選。SQL方言,預設為mysql
throwError BOOLEAN 可選。true時,遇到非法SQL丟擲異常;false時,遇到非法SQL不丟擲異常,返回null

返回值:

型別 說明
VARCHAR sqlText的SQL語句中出現在謂詞條件表示式中的列名,用逗號分隔,列所屬的表會進行關聯推導,如果沒有找到明確的表,則為UNKNOWN

示例:

SELECT sql_export_predicate_columns(a.sql_text)
FROM (
SELECT 'SELECT   s_acctbal, 
         s_name, 
         n_name, 
         p_partkey, 
         p_mfgr, 
         s_address, 
         s_phone, 
         s_comment 
FROM     part, 
         supplier, 
         partsupp, 
         nation, 
         region 
WHERE    p_partkey = ps_partkey 
AND      s_suppkey = ps_suppkey 
AND      p_size = 35 
AND      p_type LIKE ''%NICKEL'' 
AND      s_nationkey = n_nationkey 
AND      n_regionkey = r_regionkey 
AND      r_name = ''MIDDLE EAST'' 
AND      ps_supplycost IN 
         ( 
                SELECT min(ps_supplycost) 
                FROM   partsupp, 
                       supplier, 
                       nation, 
                       region 
                WHERE  s_suppkey = ps_suppkey 
                AND    s_nationkey = n_nationkey 
                AND    n_regionkey = r_regionkey 
                AND    r_name = ''MIDDLE EAST'' ) 
ORDER BY s_acctbal DESC, 
         n_name, 
         s_name, 
         p_partkey 
LIMIT    100;' AS sql_text
) a;


+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_export_predicate_columns(a.sql_text)                                                                                                                                                                                     |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| UNKNOWN.p_partkey,UNKNOWN.ps_partkey,UNKNOWN.s_suppkey,UNKNOWN.ps_suppkey,UNKNOWN.p_size,UNKNOWN.p_type,UNKNOWN.s_nationkey,UNKNOWN.n_nationkey,UNKNOWN.n_regionkey,UNKNOWN.r_regionkey,UNKNOWN.r_name,UNKNOWN.ps_supplycost |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

 

2.4 sql_export_predicates

函式呼叫形式:

  • sql_export_predicates(sqlText)
  • sql_export_predicates(sqlText, dbType)
  • sql_export_predicates(sqlText, dbType, compactValues)
  • sql_export_predicates(sqlText, dbType, compactValues, throwError)

引數說明:

引數名 型別 說明
sqlText VARCHAR 必選
dbType VARCHAR 可選。SQL方言,預設為mysql
compactValues BOOLEAN 可選。true時,謂詞條件中值以陣列的形式出現在返回值中
throwError BOOLEAN 可選。true時,遇到非法SQL丟擲異常;false時,遇到非法SQL不丟擲異常,返回null

返回值:

型別 說明
VARCHAR sqlText的SQL語句中所有出現的謂詞條件表示式元素陣列,用逗號分隔,列所屬的表會進行關聯推導,如果沒有找到明確的表,則為UNKNOWN

示例:

SELECT sql_export_predicates(a.sql_text, 'mysql', true)
FROM (
SELECT 'SELECT   s_acctbal, 
         s_name, 
         n_name, 
         p_partkey, 
         p_mfgr, 
         s_address, 
         s_phone, 
         s_comment 
FROM     part, 
         supplier, 
         partsupp, 
         nation, 
         region 
WHERE    p_partkey = ps_partkey 
AND      s_suppkey = ps_suppkey 
AND      p_size = 35 
AND      p_type LIKE ''%NICKEL'' 
AND      s_nationkey = n_nationkey 
AND      n_regionkey = r_regionkey 
AND      r_name = ''MIDDLE EAST'' 
AND      ps_supplycost IN 
         ( 
                SELECT min(ps_supplycost) 
                FROM   partsupp, 
                       supplier, 
                       nation, 
                       region 
                WHERE  s_suppkey = ps_suppkey 
                AND    s_nationkey = n_nationkey 
                AND    n_regionkey = r_regionkey 
                AND    r_name = ''MIDDLE EAST'' ) 
ORDER BY s_acctbal DESC, 
         n_name, 
         s_name, 
         p_partkey 
LIMIT    100;' AS sql_text
) a;


+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_export_predicates(a.sql_text)                                                                                                                                                                                                                                                                                                                                                                                                                  |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [["UNKNOWN","p_partkey","=",null],["UNKNOWN","ps_partkey","=",null],["UNKNOWN","s_suppkey","=",null],["UNKNOWN","ps_suppkey","=",null],["UNKNOWN","p_size","=",35],["UNKNOWN","p_type","LIKE","%NICKEL"],["UNKNOWN","s_nationkey","=",null],["UNKNOWN","n_nationkey","=",null],["UNKNOWN","n_regionkey","=",null],["UNKNOWN","r_regionkey","=",null],["UNKNOWN","r_name","=",["MIDDLE EAST","MIDDLE EAST"]],["UNKNOWN","ps_supplycost","IN",null]] |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

 

2.5 sql_export_select_list_columns

函式呼叫形式:

  • sql_export_select_list_columns(sqlText)
  • sql_export_select_list_columns(sqlText, dbType)
  • sql_export_select_list_columns(sqlText, dbType, throwError)

引數說明:

引數名 型別 說明
sqlText VARCHAR 必選
dbType VARCHAR 可選。SQL方言,預設為mysql
throwError BOOLEAN 可選。true時,遇到非法SQL丟擲異常;false時,遇到非法SQL不丟擲異常,返回null

返回值:

型別 說明
VARCHAR sqlText的SQL語句中SELECT子句返回列中出現的列名列表(包括子查詢),用逗號分隔,列所屬的表會進行關聯推導,如果沒有找到明確的表,則為UNKNOWN

示例:

SELECT sql_export_select_list_columns(a.sql_text)
FROM (
SELECT 'SELECT   s_acctbal, 
         s_name, 
         n_name, 
         p_partkey, 
         p_mfgr, 
         s_address, 
         s_phone, 
         s_comment 
FROM     part, 
         supplier, 
         partsupp, 
         nation, 
         region 
WHERE    p_partkey = ps_partkey 
AND      s_suppkey = ps_suppkey 
AND      p_size = 35 
AND      p_type LIKE ''%NICKEL'' 
AND      s_nationkey = n_nationkey 
AND      n_regionkey = r_regionkey 
AND      r_name = ''MIDDLE EAST'' 
AND      ps_supplycost IN 
         ( 
                SELECT min(ps_supplycost) 
                FROM   partsupp, 
                       supplier, 
                       nation, 
                       region 
                WHERE  s_suppkey = ps_suppkey 
                AND    s_nationkey = n_nationkey 
                AND    n_regionkey = r_regionkey 
                AND    r_name = ''MIDDLE EAST'' ) 
ORDER BY s_acctbal DESC, 
         n_name, 
         s_name, 
         p_partkey 
LIMIT    100;' AS sql_text
) a;


+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_export_select_list_columns(a.sql_text)                                                                                                                 |
+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| UNKNOWN.s_acctbal,UNKNOWN.s_name,UNKNOWN.n_name,UNKNOWN.p_partkey,UNKNOWN.p_mfgr,UNKNOWN.s_address,UNKNOWN.s_phone,UNKNOWN.s_comment,UNKNOWN.ps_supplycost |
+------------------------------------------------------------------------------------------------------------------------------------------------------------+

 

2.6 sql_export_tables

函式呼叫形式:

  • sql_export_tables(sqlText)
  • sql_export_tables(sqlText, dbType)
  • sql_export_tables(sqlText, dbType, throwError)

引數說明:

引數名 型別 說明
sqlText VARCHAR 必選
dbType VARCHAR 可選。SQL方言,預設為mysql
throwError BOOLEAN 可選。true時,遇到非法SQL丟擲異常;false時,遇到非法SQL不丟擲異常,返回null

返回值:

型別 說明
VARCHAR sqlText的SQL語句中所有出現的表名,用逗號分隔

示例:

SELECT sql_export_tables(a.sql_text)
FROM (
SELECT 'SELECT   s_acctbal, 
         s_name, 
         n_name, 
         p_partkey, 
         p_mfgr, 
         s_address, 
         s_phone, 
         s_comment 
FROM     part, 
         supplier, 
         partsupp, 
         nation, 
         region 
WHERE    p_partkey = ps_partkey 
AND      s_suppkey = ps_suppkey 
AND      p_size = 35 
AND      p_type LIKE ''%NICKEL'' 
AND      s_nationkey = n_nationkey 
AND      n_regionkey = r_regionkey 
AND      r_name = ''MIDDLE EAST'' 
AND      ps_supplycost IN 
         ( 
                SELECT min(ps_supplycost) 
                FROM   partsupp, 
                       supplier, 
                       nation, 
                       region 
                WHERE  s_suppkey = ps_suppkey 
                AND    s_nationkey = n_nationkey 
                AND    n_regionkey = r_regionkey 
                AND    r_name = ''MIDDLE EAST'' ) 
ORDER BY s_acctbal DESC, 
         n_name, 
         s_name, 
         p_partkey 
LIMIT    100;' AS sql_text
) a;


+--------------------------------------+
| sql_export_tables(a.sql_text)        |
+--------------------------------------+
| part,supplier,partsupp,nation,region |
+--------------------------------------+

 

2.7 sql_format

函式呼叫形式:

  • sql_format(sqlText)
  • sql_format(sqlText, dbType)
  • sql_format(sqlText, dbType, throwError)

引數說明:

引數名 型別 說明
sqlText VARCHAR 必選
dbType VARCHAR 可選。SQL方言,預設為mysql
throwError BOOLEAN 可選。true時,遇到非法SQL丟擲異常;false時,遇到非法SQL不丟擲異常,返回null

返回值:

型別 說明
VARCHAR 格式化sqlText的SQL語句

示例:

SELECT sql_format(a.sql_text)
FROM (
SELECT 'SELECT   s_acctbal, 
         s_name, 
         n_name, 
         p_partkey, 
         p_mfgr, 
         s_address, 
         s_phone, 
         s_comment 
FROM     part, 
         supplier, 
         partsupp, 
         nation, 
         region 
WHERE    p_partkey = ps_partkey 
AND      s_suppkey = ps_suppkey 
AND      p_size = 35 
AND      p_type LIKE ''%NICKEL'' 
AND      s_nationkey = n_nationkey 
AND      n_regionkey = r_regionkey 
AND      r_name = ''MIDDLE EAST'' 
AND      ps_supplycost IN 
         ( 
                SELECT min(ps_supplycost) 
                FROM   partsupp, 
                       supplier, 
                       nation, 
                       region 
                WHERE  s_suppkey = ps_suppkey 
                AND    s_nationkey = n_nationkey 
                AND    n_regionkey = r_regionkey 
                AND    r_name = ''MIDDLE EAST'' ) 
ORDER BY s_acctbal DESC, 
         n_name, 
         s_name, 
         p_partkey 
LIMIT    100;' AS sql_text
) a;


+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_format(a.sql_text)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SELECT s_acctbal, s_name, n_name, p_partkey, p_mfgr
    , s_address, s_phone, s_comment
FROM part, supplier, partsupp, nation, region
WHERE p_partkey = ps_partkey
    AND s_suppkey = ps_suppkey
    AND p_size = 35
    AND p_type LIKE '%NICKEL'
    AND s_nationkey = n_nationkey
    AND n_regionkey = r_regionkey
    AND r_name = 'MIDDLE EAST'
    AND ps_supplycost IN (
        SELECT min(ps_supplycost)
        FROM partsupp, supplier, nation, region
        WHERE s_suppkey = ps_suppkey
            AND s_nationkey = n_nationkey
            AND n_regionkey = r_regionkey
            AND r_name = 'MIDDLE EAST'
    )
ORDER BY s_acctbal DESC, n_name, s_name, p_partkey
LIMIT 100; |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

 

2.8 sql_params

函式呼叫形式:

  • sql_params(sqlText)
  • sql_params(sqlText, dbType)
  • sql_params(sqlText, dbType, throwError)

引數說明:

引數名 型別 說明
sqlText VARCHAR 必選
dbType VARCHAR 可選。SQL方言,預設為mysql
throwError BOOLEAN 可選。true時,遇到非法SQL丟擲異常;false時,遇到非法SQL不丟擲異常,返回null

返回值:

型別 說明
VARCHAR 提取sqlText的SQL語句中的literal值

示例:

SELECT sql_params(a.sql_text)
FROM (
SELECT 'SELECT   s_acctbal, 
         s_name, 
         n_name, 
         p_partkey, 
         p_mfgr, 
         s_address, 
         s_phone, 
         s_comment 
FROM     part, 
         supplier, 
         partsupp, 
         nation, 
         region 
WHERE    p_partkey = ps_partkey 
AND      s_suppkey = ps_suppkey 
AND      p_size = 35 
AND      p_type LIKE ''%NICKEL'' 
AND      s_nationkey = n_nationkey 
AND      n_regionkey = r_regionkey 
AND      r_name = ''MIDDLE EAST'' 
AND      ps_supplycost IN 
         ( 
                SELECT min(ps_supplycost) 
                FROM   partsupp, 
                       supplier, 
                       nation, 
                       region 
                WHERE  s_suppkey = ps_suppkey 
                AND    s_nationkey = n_nationkey 
                AND    n_regionkey = r_regionkey 
                AND    r_name = ''MIDDLE EAST'' ) 
ORDER BY s_acctbal DESC, 
         n_name, 
         s_name, 
         p_partkey 
LIMIT    100;' AS sql_text
) a;


+------------------------------------------------+
| sql_params(a.sql_text)                         |
+------------------------------------------------+
| [35,"%NICKEL","MIDDLE EAST","MIDDLE EAST",100] |
+------------------------------------------------+

 

2.10 sql_pattern

函式呼叫形式:

  • sql_pattern(sqlText)
  • sql_pattern(sqlText, dbType)
  • sql_pattern(sqlText, dbType, throwError)

引數說明:

引數名 型別 說明
sqlText VARCHAR 必選
dbType VARCHAR 可選。SQL方言,預設為mysql
throwError BOOLEAN 可選。true時,遇到非法SQL丟擲異常;false時,遇到非法SQL不丟擲異常,返回null

返回值:

型別 說明
VARCHAR 提取sqlText的SQL語句引數化後的SQL pattern, literal值換成?

示例:

SELECT sql_pattern(a.sql_text)
FROM (
SELECT 'SELECT   s_acctbal, 
         s_name, 
         n_name, 
         p_partkey, 
         p_mfgr, 
         s_address, 
         s_phone, 
         s_comment 
FROM     part, 
         supplier, 
         partsupp, 
         nation, 
         region 
WHERE    p_partkey = ps_partkey 
AND      s_suppkey = ps_suppkey 
AND      p_size = 35 
AND      p_type LIKE ''%NICKEL'' 
AND      s_nationkey = n_nationkey 
AND      n_regionkey = r_regionkey 
AND      r_name = ''MIDDLE EAST'' 
AND      ps_supplycost IN 
         ( 
                SELECT min(ps_supplycost) 
                FROM   partsupp, 
                       supplier, 
                       nation, 
                       region 
                WHERE  s_suppkey = ps_suppkey 
                AND    s_nationkey = n_nationkey 
                AND    n_regionkey = r_regionkey 
                AND    r_name = ''MIDDLE EAST'' ) 
ORDER BY s_acctbal DESC, 
         n_name, 
         s_name, 
         p_partkey 
LIMIT    100;' AS sql_text
) a;


+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_pattern(a.sql_text)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SELECT s_acctbal, s_name, n_name, p_partkey, p_mfgr
    , s_address, s_phone, s_comment
FROM part, supplier, partsupp, nation, region
WHERE p_partkey = ps_partkey
    AND s_suppkey = ps_suppkey
    AND p_size = ?
    AND p_type LIKE ?
    AND s_nationkey = n_nationkey
    AND n_regionkey = r_regionkey
    AND r_name = ?
    AND ps_supplycost IN (
        SELECT min(ps_supplycost)
        FROM partsupp, supplier, nation, region
        WHERE s_suppkey = ps_suppkey
            AND s_nationkey = n_nationkey
            AND n_regionkey = r_regionkey
            AND r_name = ?
    )
ORDER BY s_acctbal DESC, n_name, s_name, p_partkey
LIMIT ?; |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

 

2.11 sql_pattern_hash

函式呼叫形式:

  • sql_pattern_hash(sqlText)
  • sql_pattern_hash(sqlText, dbType)
  • sql_pattern_hash(sqlText, dbType, throwError)

引數說明:

引數名 型別 說明
sqlText VARCHAR 必選
dbType VARCHAR 可選。SQL方言,預設為mysql
throwError BOOLEAN 可選。true時,遇到非法SQL丟擲異常;false時,遇到非法SQL不丟擲異常,返回null

返回值:

型別 說明
VARCHAR 提取sqlText的SQL語句引數化後的SQL pattern,並生成hash值

示例:

SELECT sql_pattern_hash(a.sql_text)
FROM (
SELECT 'SELECT   s_acctbal, 
         s_name, 
         n_name, 
         p_partkey, 
         p_mfgr, 
         s_address, 
         s_phone, 
         s_comment 
FROM     part, 
         supplier, 
         partsupp, 
         nation, 
         region 
WHERE    p_partkey = ps_partkey 
AND      s_suppkey = ps_suppkey 
AND      p_size = 35 
AND      p_type LIKE ''%NICKEL'' 
AND      s_nationkey = n_nationkey 
AND      n_regionkey = r_regionkey 
AND      r_name = ''MIDDLE EAST'' 
AND      ps_supplycost IN 
         ( 
                SELECT min(ps_supplycost) 
                FROM   partsupp, 
                       supplier, 
                       nation, 
                       region 
                WHERE  s_suppkey = ps_suppkey 
                AND    s_nationkey = n_nationkey 
                AND    n_regionkey = r_regionkey 
                AND    r_name = ''MIDDLE EAST'' ) 
ORDER BY s_acctbal DESC, 
         n_name, 
         s_name, 
         p_partkey 
LIMIT    100;' AS sql_text
) a;


+------------------------------+
| sql_pattern_hash(a.sql_text) |
+------------------------------+
|           925870115679910184 |
+------------------------------+

 

2.12 sql_syntax_check

函式呼叫形式:

  • sql_syntax_check(sqlText)
  • sql_syntax_check(sqlText, dbType)
  • sql_syntax_check(sqlText, dbType, throwError)

引數說明:

引數名 型別 說明
sqlText VARCHAR 必選
dbType VARCHAR 可選。SQL方言,預設為mysql
throwError BOOLEAN 可選。true時,遇到非法SQL丟擲異常;false時,遇到非法SQL不丟擲異常,返回null

返回值:

型別 說明
BOOLEAN 對sqlText進行語法檢查,1表示正確,0表示錯誤

示例:

SELECT sql_syntax_check(a.sql_text)
FROM (
SELECT 'SELECT   s_acctbal, 
         s_name, 
         n_name, 
         p_partkey, 
         p_mfgr, 
         s_address, 
         s_phone, 
         s_comment 
FROM     part, 
         supplier, 
         partsupp, 
         nation, 
         region 
WHERE    p_partkey = ps_partkey 
AND      s_suppkey = ps_suppkey 
AND      p_size = 35 
AND      p_type LIKE ''%NICKEL'' 
AND      s_nationkey = n_nationkey 
AND      n_regionkey = r_regionkey 
AND      r_name = ''MIDDLE EAST'' 
AND      ps_supplycost IN 
         ( 
                SELECT min(ps_supplycost) 
                FROM   partsupp, 
                       supplier, 
                       nation, 
                       region 
                WHERE  s_suppkey = ps_suppkey 
                AND    s_nationkey = n_nationkey 
                AND    n_regionkey = r_regionkey 
                AND    r_name = ''MIDDLE EAST'' ) 
ORDER BY s_acctbal DESC, 
         n_name, 
         s_name, 
         p_partkey 
LIMIT    100;' AS sql_text
) a;


+------------------------------+
| sql_syntax_check(a.sql_text) |
+------------------------------+
|                            1 |
+------------------------------+

原文連結
本文為雲棲社群原創內容,未經