Explorar o código

HUE-2451 [dashboard] Use SQL autocomplete for the query input

Johan Ahlen %!s(int64=8) %!d(string=hai) anos
pai
achega
17ff1c5

+ 6 - 3
desktop/core/src/desktop/static/desktop/js/sqlAutocompleter3.js

@@ -1819,12 +1819,16 @@ var SqlAutocompleter3 = (function () {
   /**
    * @param {Object} options
    * @param {Snippet} options.snippet
+   * @param {string) [options.fixedPrefix] - Optional prefix to always use on parse
+   * @param {string) [options.fixedPostfix] - Optional postfix to always use on parse
    * @constructor
    */
   function SqlAutocompleter3(options) {
     var self = this;
     self.snippet = options.snippet;
     self.editor = options.editor;
+    self.fixedPrefix = options.fixedPrefix || function () { return '' };
+    self.fixedPostfix = options.fixedPostfix || function () { return '' };
     self.suggestions = new AutocompleteResults(options);
   }
 
@@ -1836,21 +1840,20 @@ var SqlAutocompleter3 = (function () {
 
       if ((activeStatementLocation.first_line - 1 < cursorPosition.row || (activeStatementLocation.first_line - 1 === cursorPosition.row && activeStatementLocation.first_column <= cursorPosition.column)) &&
         (activeStatementLocation.last_line - 1 > cursorPosition.row || (activeStatementLocation.last_line - 1 === cursorPosition.row && activeStatementLocation.last_column >= cursorPosition.column))) {
-        var beforeCursor = self.editor().session.getTextRange({
+        var beforeCursor = self.fixedPrefix() + self.editor().session.getTextRange({
           start: {
             row: activeStatementLocation.first_line - 1,
             column: activeStatementLocation.first_column
           },
           end: cursorPosition
         });
-
         var afterCursor = self.editor().session.getTextRange({
           start: cursorPosition,
           end: {
             row: activeStatementLocation.last_line - 1,
             column: activeStatementLocation.last_column
           }
-        });
+        }) + self.fixedPostfix();
         return sqlAutocompleteParser.parseSql(beforeCursor, afterCursor, self.snippet.type(), false);
       }
     }

+ 1 - 0
desktop/core/src/desktop/templates/hue.mako

@@ -486,6 +486,7 @@ ${ commonshare() | n,unicode }
 <script src="${ static('desktop/js/autocomplete/solrFormulaParser.js') }"></script>
 <script src="${ static('desktop/js/sqlAutocompleter.js') }"></script>
 <script src="${ static('desktop/js/sqlAutocompleter2.js') }"></script>
+<script src="${ static('desktop/js/sqlAutocompleter3.js') }"></script>
 <script src="${ static('desktop/js/hdfsAutocompleter.js') }"></script>
 <script src="${ static('desktop/js/autocompleter.js') }"></script>
 <script src="${ static('desktop/js/hue.json.js') }"></script>

+ 33 - 4
desktop/core/src/desktop/templates/ko_components/ko_simple_ace_editor.mako

@@ -555,7 +555,7 @@ from desktop.views import _ko
          */
         var SolrFormulaAutocompleter = function (options) {
           var self = this;
-          self.editor = options.editor;
+          self.editor = options.editor();
           self.suggestions = new SolrFormulaSuggestions(options.support.fields);
         };
 
@@ -736,7 +736,7 @@ from desktop.views import _ko
          */
         var SolrQueryAutocompleter = function (options) {
           var self = this;
-          self.editor = options.editor;
+          self.editor = options.editor();
           self.suggestions = new SolrQuerySuggestions(options.support.collection, self.editor);
         };
 
@@ -756,7 +756,9 @@ from desktop.views import _ko
 
       var AVAILABLE_AUTOCOMPLETERS = {
         'solrFormula': SolrFormulaAutocompleter,
-        'solrQuery':  SolrQueryAutocompleter
+        'solrQuery':  SolrQueryAutocompleter,
+        'impalaQuery': SqlAutocompleter3,
+        'hiveQuery': SqlAutocompleter3
       };
 
       var SimpleAceEditor = function (params, element) {
@@ -784,7 +786,34 @@ from desktop.views import _ko
             throw new Error('Could not find autocompleter for "' + params.autocomplete.type + '"');
           }
 
-          self.autocompleter = new AVAILABLE_AUTOCOMPLETERS[params.autocomplete.type]({ editor: editor, support: params.autocomplete.support });
+          var sourceType = params.autocomplete.type.substring(0, params.autocomplete.type.indexOf('Query'));
+
+          var autocompleteArgs = {
+            editor: function() { return editor },
+            snippet: {
+              type: function () {
+                return sourceType;
+              },
+              database: function () {
+                if (params.autocomplete.support.collection.name()) {
+                  var path = params.autocomplete.support.collection.name().split('.');
+                  return path.length > 1 ? path[0] : 'default';
+                } else {
+                  return 'default';
+                }
+              },
+              positionStatement: function () {
+                return {
+                  location: { first_line: 1, last_line: 1, first_column: 0, last_column: editor.getValue().length }
+                }
+              }
+            },
+            fixedPrefix: sourceType !== 'solr' ? function() { return 'SELECT * FROM ' + params.autocomplete.support.collection.name() + ' WHERE '; } : undefined,
+            fixedPostfix: sourceType !== 'solr' ? function() { return ' GROUP BY 1;' } : undefined, // By adding group by it will limit it to the filter suggestions
+            support: params.autocomplete.support
+          };
+
+          self.autocompleter = new AVAILABLE_AUTOCOMPLETERS[params.autocomplete.type](autocompleteArgs);
         } else {
           self.autocompleter = null;
         }

+ 7 - 6
desktop/libs/dashboard/src/dashboard/templates/common_search.mako

@@ -104,12 +104,13 @@ from dashboard.conf import USE_GRIDSTER, HAS_REPORT_ENABLED
           </div>
           <div class="search-bar-query" data-bind="foreach: query.qs">
 
-            <!-- ko if: $root.collection.engine() === 'solr' -->
-            <div data-bind="component: { name: 'hue-simple-ace-editor', params: { value: q, onExec: $parent.searchBtn, placeHolder: '${ _ko('Example: field:value, or press CTRL + space') }', autocomplete: { type: 'solrQuery', support: { collection: $root.collection } }, singleLine: true } }"></div>
-            <!-- /ko -->
-            <!-- ko if: $root.collection.engine() !== 'solr' -->
-            <div data-bind="component: { name: 'hue-simple-ace-editor', params: { value: q, onExec: $parent.searchBtn, placeHolder: '${ _ko('Example: col = value') }', singleLine: true } }"></div>
-            <!-- /ko -->
+            <div data-bind="component: { name: 'hue-simple-ace-editor', params: {
+              value: q,
+              onExec: $parent.searchBtn,
+              placeHolder: $root.collection.engine() === 'solr' ? '${ _ko('Example: field:value, or press CTRL + space') }' : '${ _ko('Example: col = value, or press CTRL + space') }',
+              autocomplete: { type: $root.collection.engine() + 'Query', support: { collection: $root.collection } },
+              singleLine: true }
+            }"></div>
 ##             <input data-bind="clearable: q, valueUpdate:'afterkeydown', typeahead: { target: q, nonBindableSource: queryTypeahead, multipleValues: true, multipleValuesSeparator: ':', extraKeywords: 'AND OR TO', completeSolrRanges: true }, css: {'input-small': $root.query.qs().length > 1, 'flat-left': $index() === 0, 'input-xlarge': $root.collection.supportAnalytics()}" maxlength="4096" type="text" class="search-query">
             <!-- ko if: $index() >= 1 -->
               <a class="btn flat-left" href="javascript:void(0)" data-bind="click: $root.query.removeQ"><i class="fa fa-minus"></i></a>