github.com/seashell-org/golang-migrate/v4@v4.15.3-0.20220722221203-6ab6c6c062d1/database/spanner/spansql/keywords.go (about)

     1  /*
     2  Copyright 2020 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package spansql
    18  
    19  import (
    20  	"strings"
    21  )
    22  
    23  // IsKeyword reports whether the identifier is a reserved keyword.
    24  func IsKeyword(id string) bool {
    25  	return keywords[strings.ToUpper(id)]
    26  }
    27  
    28  // keywords is the set of reserved keywords.
    29  // https://cloud.google.com/spanner/docs/lexical#reserved-keywords
    30  var keywords = map[string]bool{
    31  	"ALL":                  true,
    32  	"AND":                  true,
    33  	"ANY":                  true,
    34  	"ARRAY":                true,
    35  	"AS":                   true,
    36  	"ASC":                  true,
    37  	"ASSERT_ROWS_MODIFIED": true,
    38  	"AT":                   true,
    39  	"BETWEEN":              true,
    40  	"BY":                   true,
    41  	"CASE":                 true,
    42  	"CAST":                 true,
    43  	"COLLATE":              true,
    44  	"CONTAINS":             true,
    45  	"CREATE":               true,
    46  	"CROSS":                true,
    47  	"CUBE":                 true,
    48  	"CURRENT":              true,
    49  	"DEFAULT":              true,
    50  	"DEFINE":               true,
    51  	"DESC":                 true,
    52  	"DISTINCT":             true,
    53  	"ELSE":                 true,
    54  	"END":                  true,
    55  	"ENUM":                 true,
    56  	"ESCAPE":               true,
    57  	"EXCEPT":               true,
    58  	"EXCLUDE":              true,
    59  	"EXISTS":               true,
    60  	"EXTRACT":              true,
    61  	"FALSE":                true,
    62  	"FETCH":                true,
    63  	"FOLLOWING":            true,
    64  	"FOR":                  true,
    65  	"FROM":                 true,
    66  	"FULL":                 true,
    67  	"GROUP":                true,
    68  	"GROUPING":             true,
    69  	"GROUPS":               true,
    70  	"HASH":                 true,
    71  	"HAVING":               true,
    72  	"IF":                   true,
    73  	"IGNORE":               true,
    74  	"IN":                   true,
    75  	"INNER":                true,
    76  	"INTERSECT":            true,
    77  	"INTERVAL":             true,
    78  	"INTO":                 true,
    79  	"IS":                   true,
    80  	"JOIN":                 true,
    81  	"LATERAL":              true,
    82  	"LEFT":                 true,
    83  	"LIKE":                 true,
    84  	"LIMIT":                true,
    85  	"LOOKUP":               true,
    86  	"MERGE":                true,
    87  	"NATURAL":              true,
    88  	"NEW":                  true,
    89  	"NO":                   true,
    90  	"NOT":                  true,
    91  	"NULL":                 true,
    92  	"NULLS":                true,
    93  	"OF":                   true,
    94  	"ON":                   true,
    95  	"OR":                   true,
    96  	"ORDER":                true,
    97  	"OUTER":                true,
    98  	"OVER":                 true,
    99  	"PARTITION":            true,
   100  	"PRECEDING":            true,
   101  	"PROTO":                true,
   102  	"RANGE":                true,
   103  	"RECURSIVE":            true,
   104  	"RESPECT":              true,
   105  	"RIGHT":                true,
   106  	"ROLLUP":               true,
   107  	"ROWS":                 true,
   108  	"SELECT":               true,
   109  	"SET":                  true,
   110  	"SOME":                 true,
   111  	"STRUCT":               true,
   112  	"TABLESAMPLE":          true,
   113  	"THEN":                 true,
   114  	"TO":                   true,
   115  	"TREAT":                true,
   116  	"TRUE":                 true,
   117  	"UNBOUNDED":            true,
   118  	"UNION":                true,
   119  	"UNNEST":               true,
   120  	"USING":                true,
   121  	"WHEN":                 true,
   122  	"WHERE":                true,
   123  	"WINDOW":               true,
   124  	"WITH":                 true,
   125  	"WITHIN":               true,
   126  }
   127  
   128  // funcs is the set of reserved keywords that are functions.
   129  // https://cloud.google.com/spanner/docs/functions-and-operators
   130  var funcs = make(map[string]bool)
   131  var funcArgParsers = make(map[string]func(*parser) (Expr, *parseError))
   132  
   133  func init() {
   134  	for _, f := range allFuncs {
   135  		funcs[f] = true
   136  	}
   137  	// Special case for CAST, SAFE_CAST and EXTRACT
   138  	funcArgParsers["CAST"] = typedArgParser
   139  	funcArgParsers["SAFE_CAST"] = typedArgParser
   140  	funcArgParsers["EXTRACT"] = extractArgParser
   141  }
   142  
   143  var allFuncs = []string{
   144  	// TODO: many more
   145  
   146  	// Aggregate functions.
   147  	"ANY_VALUE",
   148  	"ARRAY_AGG",
   149  	"AVG",
   150  	"BIT_XOR",
   151  	"COUNT",
   152  	"MAX",
   153  	"MIN",
   154  	"SUM",
   155  
   156  	// Cast functions.
   157  	"CAST",
   158  	"SAFE_CAST",
   159  
   160  	// Mathematical functions.
   161  	"ABS",
   162  	"MOD",
   163  
   164  	// Hash functions.
   165  	"FARM_FINGERPRINT",
   166  	"SHA1",
   167  	"SHA256", "SHA512",
   168  
   169  	// String functions.
   170  	"BYTE_LENGTH", "CHAR_LENGTH", "CHARACTER_LENGTH",
   171  	"CODE_POINTS_TO_BYTES", "CODE_POINTS_TO_STRING",
   172  	"CONCAT",
   173  	"ENDS_WITH",
   174  	"FORMAT",
   175  	"FROM_BASE32", "FROM_BASE64", "FROM_HEX",
   176  	"LENGTH",
   177  	"LOWER",
   178  	"LPAD",
   179  	"LTRIM",
   180  	"REGEXP_CONTAINS", "REGEXP_EXTRACT", "REGEXP_EXTRACT_ALL", "REGEXP_REPLACE",
   181  	"REPEAT",
   182  	"REPLACE",
   183  	"REVERSE",
   184  	"RPAD",
   185  	"RTRIM",
   186  	"SAFE_CONVERT_BYTES_TO_STRING",
   187  	"SPLIT",
   188  	"STARTS_WITH",
   189  	"STRPOS",
   190  	"SUBSTR",
   191  	"TO_BASE32", "TO_BASE64", "TO_CODE_POINTS", "TO_HEX",
   192  	"TRIM",
   193  	"UPPER",
   194  
   195  	// Array functions.
   196  	"ARRAY",
   197  	"ARRAY_CONCAT",
   198  	"ARRAY_LENGTH",
   199  	"ARRAY_TO_STRING",
   200  	"GENERATE_ARRAY", "GENERATE_DATE_ARRAY",
   201  	"OFFSET", "ORDINAL",
   202  	"ARRAY_REVERSE",
   203  	"ARRAY_IS_DISTINCT",
   204  	"SAFE_OFFSET", "SAFE_ORDINAL",
   205  
   206  	// Date functions.
   207  	"CURRENT_DATE",
   208  	"EXTRACT",
   209  	"DATE",
   210  	"DATE_ADD",
   211  	"DATE_SUB",
   212  	"DATE_DIFF",
   213  	"DATE_TRUNC",
   214  	"DATE_FROM_UNIX_DATE",
   215  	"FORMAT_DATE",
   216  	"PARSE_DATE",
   217  	"UNIX_DATE",
   218  
   219  	// Timestamp functions.
   220  	"CURRENT_TIMESTAMP",
   221  	"STRING",
   222  	"TIMESTAMP",
   223  	"TIMESTAMP_ADD",
   224  	"TIMESTAMP_SUB",
   225  	"TIMESTAMP_DIFF",
   226  	"TIMESTAMP_TRUNC",
   227  	"FORMAT_TIMESTAMP",
   228  	"PARSE_TIMESTAMP",
   229  	"TIMESTAMP_SECONDS",
   230  	"TIMESTAMP_MILLIS",
   231  	"TIMESTAMP_MICROS",
   232  	"UNIX_SECONDS",
   233  	"UNIX_MILLIS",
   234  	"UNIX_MICROS",
   235  	"PENDING_COMMIT_TIMESTAMP",
   236  
   237  	// JSON functions.
   238  	"JSON_VALUE",
   239  }