codemirror-isql-hint.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. (function () {
  17. function forEach(arr, f) {
  18. for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
  19. }
  20. function arrayContains(arr, item) {
  21. if (!Array.prototype.indexOf) {
  22. var i = arr.length;
  23. while (i--) {
  24. if (arr[i] === item) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. return arr.indexOf(item) != -1;
  31. }
  32. function scriptHint(editor, _keywords, getToken) {
  33. // Find the token at the cursor
  34. var cur = editor.getCursor(), token = getToken(editor, cur), tprop = token;
  35. // If it's not a 'word-style' or dot token, ignore the token.
  36. if (!/^[\.\w$_]*$/.test(token.string)) {
  37. token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state,
  38. className: token.string == "." ? "impalasql-type" : null};
  39. }
  40. if (!context) var context = [];
  41. context.push(tprop);
  42. var completionList = getCompletions(token, context);
  43. //prevent autocomplete for last word, instead show dropdown with one word
  44. if (completionList.length == 1) {
  45. completionList.push(" ");
  46. }
  47. return {list: completionList,
  48. from: CodeMirror.Pos(cur.line, token.start),
  49. to: CodeMirror.Pos(cur.line, token.end)};
  50. }
  51. CodeMirror.catalogTables = "";
  52. CodeMirror.catalogFields = "";
  53. CodeMirror.possibleTable = false;
  54. CodeMirror.possibleSoloField = false;
  55. CodeMirror.tableFieldMagic = false;
  56. CodeMirror.impalaSQLHint = function (editor) {
  57. return scriptHint(editor, impalaSQLKeywordsU, function (e, cur) {
  58. return e.getTokenAt(cur);
  59. });
  60. };
  61. var impalaSQLKeywords = "add aggregate all and api_version as avro binary by cached case change char class close_fn column columns comment compute create cross data database databases decimal delimited describe distinct div drop else end escaped exists explain external false fields fileformat finalize_fn first format formatted from full function functions group having if init_fn inner inpath insert integer intermediate interval into invalidate join last left like limit lines load location merge_fn metadata not null nulls offset or order outer overwrite parquet partition partitioned partitions prepare_fn produced real refresh regexp rename replace returns right rlike row schema schemas select semi serdeproperties serialize_fn show stats stored straight_join symbol table tables tblproperties terminated then to true uncached union update_fn use using view when where with";
  62. var impalaSQLKeywordsU = impalaSQLKeywords.toUpperCase().split(" ");
  63. var impalaSQLKeywordsL = impalaSQLKeywords.toLowerCase().split(" ");
  64. var impalaSQLKeywordsAfterTables = "JOIN ON WHERE ORDER BY ASC DESC LIMIT";
  65. var impalaSQLKeywordsAfterTablesU = impalaSQLKeywordsAfterTables.toUpperCase().split(" ");
  66. var impalaSQLKeywordsAfterTablesL = impalaSQLKeywordsAfterTables.toLowerCase().split(" ");
  67. var impalaSQLTypes = "DATE DATETIME TINYINT SMALLINT INT BIGINT BOOLEAN FLOAT DOUBLE STRING TIMESTAMP PARQUETFILE SEQUENCEFILE TEXTFILE RCFILE";
  68. var impalaSQLTypesU = impalaSQLTypes.toUpperCase().split(" ");
  69. var impalaSQLTypesL = impalaSQLTypes.toLowerCase().split(" ");
  70. var impalaSQLBuiltins = "ABS ACOS ASCII ASIN ATAN AVG BIN CAST CEIL CEILING COALESCE CONCAT CONCAT_WS COUNT CONV COS DATE_ADD DATE_SUB DATEDIFF DAY DAYNAME DAYOFMONTH DAYOFWEEK DEGREES E EXP FIND_IN_SET FLOOR FNV_HASH FROM_UNIXTIME FROM_UTC_TIMESTAMP GREATEST GROUP_CONCAT HEX HOUR IF INITCAP INSTR ISNULL LCASE LEAST LENGTH LN LOCATE LOG LOG10 LOG2 LOWER LPAD LTRIM MAX MIN MINUTE MONTH NDV NEGATIVE NOW NVL PARSE_URL PI PMOD POSITIVE POW POWER QUOTIENT RADIANS RAND REGEXP_EXTRACT REPEAT REVERSE ROUND RPAD RTRIM SECOND SIGN SIN SPACE SQRT SUBSTR SUBSTRING SUM TAN TO_DATE TO_UTC_TIMESTAMP TRANSLATE TRIM UCASE UNHEX UNIX_TIMESTAMP UPPER USER WEEKOFYEAR YEAR";
  71. var impalaSQLBuiltinsU = impalaSQLBuiltins.toUpperCase().split(" ").join("() ").split(" ");
  72. var impalaSQLBuiltinsL = impalaSQLBuiltins.toLowerCase().split(" ").join("() ").split(" ");
  73. var impalaSQLAnalytics = "dense_rank first_value lag last_value lead over rank row_number window";
  74. var impalaSQLAnalyticsU = impalaSQLAnalytics.toUpperCase().split(" ").join("() ").split(" ");
  75. var impalaSQLAnalyticsL = impalaSQLAnalytics.toLowerCase().split(" ").join("() ").split(" ");
  76. function getCompletions(token, context) {
  77. var catalogTablesL = CodeMirror.catalogTables.toLowerCase().split(" ");
  78. var catalogFieldsL = CodeMirror.catalogFields.toLowerCase().split(" ");
  79. var found = [], start = token.string, extraFound = [];
  80. function maybeAdd(str) {
  81. if (str.indexOf(start) == 0 && !arrayContains(found, str)) found.push(str);
  82. }
  83. function maybeAddToExtra(str) {
  84. var _match = str;
  85. if (_match.indexOf("fa-magic") > -1) {
  86. _match = _match.substring(_match.indexOf("FROM ") + 5);
  87. }
  88. if (_match.indexOf(start) == 0 && !arrayContains(found, str)) extraFound.push(str);
  89. }
  90. function gatherCompletions(obj) {
  91. if (obj.indexOf(".") == 0 || obj.indexOf("(") == 0) {
  92. forEach(catalogFieldsL, maybeAdd);
  93. }
  94. else {
  95. if (!CodeMirror.possibleTable) {
  96. if (CodeMirror.tableFieldMagic) {
  97. var _specialCatalogTablesL = CodeMirror.catalogTables.toLowerCase().split(" ");
  98. for (var i = 0; i < _specialCatalogTablesL.length; i++) {
  99. _specialCatalogTablesL[i] = "<i class='fa fa-magic'></i> FROM " + _specialCatalogTablesL[i];
  100. }
  101. forEach(_specialCatalogTablesL, maybeAddToExtra);
  102. }
  103. if (CodeMirror.possibleSoloField) {
  104. forEach(catalogFieldsL, maybeAddToExtra);
  105. }
  106. forEach(impalaSQLBuiltinsU, maybeAdd);
  107. forEach(impalaSQLBuiltinsL, maybeAdd);
  108. forEach(impalaSQLTypesU, maybeAdd);
  109. forEach(impalaSQLTypesL, maybeAdd);
  110. forEach(impalaSQLKeywordsU, maybeAdd);
  111. forEach(impalaSQLKeywordsL, maybeAdd);
  112. forEach(impalaSQLAnalyticsU, maybeAdd);
  113. forEach(impalaSQLAnalyticsL, maybeAdd);
  114. }
  115. else {
  116. forEach(catalogTablesL, maybeAddToExtra);
  117. forEach(impalaSQLKeywordsAfterTablesU, maybeAdd);
  118. forEach(impalaSQLKeywordsAfterTablesL, maybeAdd);
  119. }
  120. }
  121. }
  122. if (context) {
  123. // If this is a property, see if it belongs to some object we can
  124. // find in the current environment.
  125. var obj = context.pop(), base;
  126. base = obj.string;
  127. while (base != null && context.length)
  128. base = base[context.pop().string];
  129. if (base != null) gatherCompletions(base);
  130. }
  131. return extraFound.sort().concat(found.sort());
  132. }
  133. })();