github.com/XiaoMi/Gaea@v1.2.5/parser/ast/functions_test.go (about) 1 // Copyright 2017 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package ast_test 15 16 import ( 17 . "github.com/pingcap/check" 18 19 . "github.com/XiaoMi/Gaea/parser/ast" 20 ) 21 22 var _ = Suite(&testFunctionsSuite{}) 23 24 type testFunctionsSuite struct { 25 } 26 27 func (ts *testFunctionsSuite) TestFunctionsVisitorCover(c *C) { 28 valueExpr := NewValueExpr(42) 29 stmts := []Node{ 30 &AggregateFuncExpr{Args: []ExprNode{valueExpr}}, 31 &FuncCallExpr{Args: []ExprNode{valueExpr}}, 32 &FuncCastExpr{Expr: valueExpr}, 33 &WindowFuncExpr{Spec: WindowSpec{}}, 34 } 35 36 for _, stmt := range stmts { 37 stmt.Accept(visitor{}) 38 stmt.Accept(visitor1{}) 39 } 40 } 41 42 func (ts *testFunctionsSuite) TestFuncCallExprRestore(c *C) { 43 testCases := []NodeRestoreTestCase{ 44 {"JSON_ARRAYAGG(attribute)", "JSON_ARRAYAGG(`attribute`)"}, 45 {"JSON_OBJECTAGG(attribute, value)", "JSON_OBJECTAGG(`attribute`, `value`)"}, 46 {"ABS(-1024)", "ABS(-1024)"}, 47 {"ACOS(3.14)", "ACOS(3.14)"}, 48 {"CONV('a',16,2)", "CONV('a', 16, 2)"}, 49 {"COS(PI())", "COS(PI())"}, 50 {"RAND()", "RAND()"}, 51 {"ADDDATE('2000-01-01', 1)", "ADDDATE('2000-01-01', INTERVAL 1 DAY)"}, 52 {"DATE_ADD('2000-01-01', INTERVAL 1 DAY)", "DATE_ADD('2000-01-01', INTERVAL 1 DAY)"}, 53 {"DATE_ADD('2000-01-01', INTERVAL '1 1:12:23.100000' DAY_MICROSECOND)", "DATE_ADD('2000-01-01', INTERVAL '1 1:12:23.100000' DAY_MICROSECOND)"}, 54 {"EXTRACT(DAY FROM '2000-01-01')", "EXTRACT(DAY FROM '2000-01-01')"}, 55 {"extract(day from '1999-01-01')", "EXTRACT(DAY FROM '1999-01-01')"}, 56 {"GET_FORMAT(DATE, 'EUR')", "GET_FORMAT(DATE, 'EUR')"}, 57 {"POSITION('a' IN 'abc')", "POSITION('a' IN 'abc')"}, 58 {"TRIM(' bar ')", "TRIM(' bar ')"}, 59 {"TRIM('a' FROM ' bar ')", "TRIM('a' FROM ' bar ')"}, 60 {"TRIM(LEADING FROM ' bar ')", "TRIM(LEADING FROM ' bar ')"}, 61 {"TRIM(BOTH FROM ' bar ')", "TRIM(BOTH FROM ' bar ')"}, 62 {"TRIM(TRAILING FROM ' bar ')", "TRIM(TRAILING FROM ' bar ')"}, 63 {"TRIM(LEADING 'x' FROM 'xxxyxxx')", "TRIM(LEADING 'x' FROM 'xxxyxxx')"}, 64 {"TRIM(BOTH 'x' FROM 'xxxyxxx')", "TRIM(BOTH 'x' FROM 'xxxyxxx')"}, 65 {"TRIM(TRAILING 'x' FROM 'xxxyxxx')", "TRIM(TRAILING 'x' FROM 'xxxyxxx')"}, 66 {"DATE_ADD('2008-01-02', INTERVAL INTERVAL(1, 0, 1) DAY)", "DATE_ADD('2008-01-02', INTERVAL INTERVAL(1, 0, 1) DAY)"}, 67 {"BENCHMARK(1000000, AES_ENCRYPT('text', UNHEX('F3229A0B371ED2D9441B830D21A390C3')))", "BENCHMARK(1000000, AES_ENCRYPT('text', UNHEX('F3229A0B371ED2D9441B830D21A390C3')))"}, 68 {"SUBSTRING('Quadratically', 5)", "SUBSTRING('Quadratically', 5)"}, 69 {"SUBSTRING('Quadratically' FROM 5)", "SUBSTRING('Quadratically', 5)"}, 70 {"SUBSTRING('Quadratically', 5, 6)", "SUBSTRING('Quadratically', 5, 6)"}, 71 {"SUBSTRING('Quadratically' FROM 5 FOR 6)", "SUBSTRING('Quadratically', 5, 6)"}, 72 {"MASTER_POS_WAIT(@log_name, @log_pos, @timeout, @channel_name)", "MASTER_POS_WAIT(@`log_name`, @`log_pos`, @`timeout`, @`channel_name`)"}, 73 {"JSON_TYPE('[123]')", "JSON_TYPE('[123]')"}, 74 {"bit_and(all c1)", "BIT_AND(`c1`)"}, 75 } 76 extractNodeFunc := func(node Node) Node { 77 return node.(*SelectStmt).Fields.Fields[0].Expr 78 } 79 RunNodeRestoreTest(c, testCases, "select %s", extractNodeFunc) 80 } 81 82 func (ts *testFunctionsSuite) TestFuncCastExprRestore(c *C) { 83 testCases := []NodeRestoreTestCase{ 84 {"CONVERT('Müller' USING UtF8Mb4)", "CONVERT('Müller' USING UTF8MB4)"}, 85 {"CONVERT('Müller', CHAR(32) CHARACTER SET UtF8)", "CONVERT('Müller', CHAR(32) CHARACTER SET UtF8)"}, 86 {"CAST('test' AS CHAR CHARACTER SET UtF8)", "CAST('test' AS CHAR CHARACTER SET UtF8)"}, 87 {"BINARY 'New York'", "BINARY 'New York'"}, 88 } 89 extractNodeFunc := func(node Node) Node { 90 return node.(*SelectStmt).Fields.Fields[0].Expr 91 } 92 RunNodeRestoreTest(c, testCases, "select %s", extractNodeFunc) 93 } 94 95 func (ts *testFunctionsSuite) TestAggregateFuncExprRestore(c *C) { 96 testCases := []NodeRestoreTestCase{ 97 {"AVG(test_score)", "AVG(`test_score`)"}, 98 {"AVG(distinct test_score)", "AVG(DISTINCT `test_score`)"}, 99 {"BIT_AND(test_score)", "BIT_AND(`test_score`)"}, 100 {"BIT_OR(test_score)", "BIT_OR(`test_score`)"}, 101 {"BIT_XOR(test_score)", "BIT_XOR(`test_score`)"}, 102 {"COUNT(test_score)", "COUNT(`test_score`)"}, 103 {"COUNT(*)", "COUNT(1)"}, 104 {"COUNT(DISTINCT scores, results)", "COUNT(DISTINCT `scores`, `results`)"}, 105 {"MIN(test_score)", "MIN(`test_score`)"}, 106 {"MIN(DISTINCT test_score)", "MIN(DISTINCT `test_score`)"}, 107 {"MAX(test_score)", "MAX(`test_score`)"}, 108 {"MAX(DISTINCT test_score)", "MAX(DISTINCT `test_score`)"}, 109 {"STD(test_score)", "STD(`test_score`)"}, 110 {"STDDEV(test_score)", "STDDEV(`test_score`)"}, 111 {"STDDEV_POP(test_score)", "STDDEV_POP(`test_score`)"}, 112 {"STDDEV_SAMP(test_score)", "STDDEV_SAMP(`test_score`)"}, 113 {"SUM(test_score)", "SUM(`test_score`)"}, 114 {"SUM(DISTINCT test_score)", "SUM(DISTINCT `test_score`)"}, 115 {"VAR_POP(test_score)", "VAR_POP(`test_score`)"}, 116 {"VAR_SAMP(test_score)", "VAR_SAMP(`test_score`)"}, 117 {"VARIANCE(test_score)", "VAR_POP(`test_score`)"}, 118 } 119 extractNodeFunc := func(node Node) Node { 120 return node.(*SelectStmt).Fields.Fields[0].Expr 121 } 122 RunNodeRestoreTest(c, testCases, "select %s", extractNodeFunc) 123 } 124 125 func (ts *testDMLSuite) TestWindowFuncExprRestore(c *C) { 126 testCases := []NodeRestoreTestCase{ 127 {"RANK() OVER w", "RANK() OVER (`w`)"}, 128 {"RANK() OVER (PARTITION BY a)", "RANK() OVER (PARTITION BY `a`)"}, 129 {"MAX(DISTINCT a) OVER (PARTITION BY a)", "MAX(DISTINCT `a`) OVER (PARTITION BY `a`)"}, 130 {"MAX(DISTINCTROW a) OVER (PARTITION BY a)", "MAX(DISTINCT `a`) OVER (PARTITION BY `a`)"}, 131 {"MAX(DISTINCT ALL a) OVER (PARTITION BY a)", "MAX(DISTINCT `a`) OVER (PARTITION BY `a`)"}, 132 {"MAX(ALL a) OVER (PARTITION BY a)", "MAX(`a`) OVER (PARTITION BY `a`)"}, 133 {"FIRST_VALUE(val) IGNORE NULLS OVER w", "FIRST_VALUE(`val`) IGNORE NULLS OVER (`w`)"}, 134 {"FIRST_VALUE(val) RESPECT NULLS OVER w", "FIRST_VALUE(`val`) OVER (`w`)"}, 135 {"NTH_VALUE(val, 233) FROM LAST IGNORE NULLS OVER w", "NTH_VALUE(`val`, 233) FROM LAST IGNORE NULLS OVER (`w`)"}, 136 {"NTH_VALUE(val, 233) FROM FIRST IGNORE NULLS OVER w", "NTH_VALUE(`val`, 233) IGNORE NULLS OVER (`w`)"}, 137 } 138 extractNodeFunc := func(node Node) Node { 139 return node.(*SelectStmt).Fields.Fields[0].Expr 140 } 141 RunNodeRestoreTest(c, testCases, "select %s from t", extractNodeFunc) 142 }