Browse Source

HUE-3367 [editor] Add data sample for columns

Johan Ahlen 9 years ago
parent
commit
48e1fac87a

+ 2 - 1
desktop/core/src/desktop/static/desktop/js/assist/assistHelper.js

@@ -590,10 +590,11 @@
    *
    * @param {string} options.databaseName
    * @param {string} options.tableName
+   * @param {string} [options.columnName]
    */
   AssistHelper.prototype.fetchTableSample = function (options) {
     var self = this;
-    var url = SAMPLE_API_PREFIX + options.databaseName + '/' + options.tableName;
+    var url = SAMPLE_API_PREFIX + options.databaseName + '/' + options.tableName + (options.columnName ? '/' + options.columnName : '');
 
     $.post(url, {
       notebook: {},

+ 36 - 4
desktop/core/src/desktop/static/desktop/js/sqlAutocompleter.js

@@ -291,7 +291,39 @@
         }
       };
 
-      if (fields.length === 1 && self.optEnabled) {
+      if (fields.length === 1 && !self.optEnabled) {
+        self.snippet.getAssistHelper().fetchTableSample({
+          sourceType: self.snippet.type(),
+          databaseName: database,
+          tableName: tableName,
+          columnName: fields.length === 1 ? fields[0] : null,
+          successCallback: function (data) {
+            if (data.status === 0 && data.headers.length === 1) {
+              var values = $.map(data.rows, function (row, index) {
+                return {
+                  meta: 'value',
+                  score: 1000 - index,
+                  value: typeof row[0] === 'string' ? "'" + row[0] + "'" :  '' + row[0]
+                }
+              });
+              if (self.snippet.type() === 'impala') {
+                fetchImpalaFields(fields, values);
+              } else {
+                callback(values);
+              }
+            } else {
+              if (self.snippet.type() === 'impala') {
+                fetchImpalaFields(fields, []);
+              }
+            }
+          },
+          errorCallback: function () {
+            if (self.snippet.type() === 'impala') {
+              fetchImpalaFields(fields, []);
+            }
+          }
+        });
+      } else if (fields.length === 1 && self.optEnabled) {
         $.post('/metadata/api/optimizer_api/popular_values', {
           database: database,
           tableName: tableName,
@@ -590,7 +622,7 @@
 
       var fromReferences = self.getFromReferenceIndex(beforeCursor + upToNextStatement);
       var viewReferences = self.getViewReferenceIndex(beforeCursor + upToNextStatement, hiveSyntax);
-      var conditionMatch = beforeCursor.match(/(\S+)\s*=\s*$/);
+      var conditionMatch = beforeCursor.match(/(\S+)\s*=\s*([^\s;]+)?$/);
 
       var tableName = "";
 
@@ -633,7 +665,7 @@
           };
         });
 
-        if (conditionMatch && impalaSyntax) {
+        if (conditionMatch) {
           self.getValueReferences(conditionMatch, database, fromReferences, tableRefs.concat(complexRefs), callback, editor);
         } else {
           callback(tableRefs.concat(complexRefs));
@@ -686,7 +718,7 @@
                 suggestions = self.extractFields(data, tableName, database, "", !fieldTermBefore);
               }
 
-              var startedMatch = beforeCursorU.match(/.* (WHERE|AND|OR)\s+(\S*)$/);
+              var startedMatch = beforeCursorU.match(/.* (WHERE|AND|OR)\s+(\S+)$/);
               if (self.optEnabled && keywordBeforeCursor === "WHERE" && startedMatch) {
                 $.post('/metadata/api/optimizer_api/popular_values', {
                   database: database,

+ 66 - 0
desktop/core/src/desktop/static/desktop/spec/sqlAutocompleterSpec.js

@@ -933,6 +933,10 @@ define([
             "/notebook/api/autocomplete/database_one/testTable/id" : {
               sample: [1, 2, 3],
               type: "int"
+            },
+            "/notebook/api/sample/database_one/testTable/id": {
+              status: 0,
+              headers: []
             }
           },
           beforeCursor: "SELECT * FROM testTable WHERE id =",
@@ -959,6 +963,68 @@ define([
       })
     });
 
+    describe("value completion", function () {
+      it("should suggest numeric sample values for columns in conditions", function() {
+        assertAutoComplete({
+          serverResponses: {
+            "/notebook/api/sample/database_one/testTable/id": {
+              status: 0,
+              headers: ['id'],
+              rows: [[1], [2], [3]]
+            }
+          },
+          beforeCursor: "SELECT * FROM testTable WHERE id =",
+          afterCursor: "",
+          expectedSuggestions: ['1', '2', '3']
+        });
+      });
+
+      it("should suggest string sample values for columns in conditions with started value", function() {
+        assertAutoComplete({
+          serverResponses: {
+            "/notebook/api/sample/database_one/testTable/string": {
+              status: 0,
+              headers: ['id'],
+              rows: [['abc'], ['def'], ['ghi']]
+            }
+          },
+          beforeCursor: "SELECT * FROM testTable WHERE string = 'd",
+          afterCursor: "",
+          expectedSuggestions: ["'abc'", "'def'", "'ghi'"]
+        });
+      });
+
+      it("should suggest string sample values for columns in conditions with started value after AND", function() {
+        assertAutoComplete({
+          serverResponses: {
+            "/notebook/api/sample/database_one/testTable/string": {
+              status: 0,
+              headers: ['id'],
+              rows: [['abc'], ['def'], ['ghi']]
+            }
+          },
+          beforeCursor: "SELECT * FROM testTable WHERE id = 1 AND string =",
+          afterCursor: "",
+          expectedSuggestions: ["'abc'", "'def'", "'ghi'"]
+        });
+      });
+
+      it("should suggest string sample values for columns in conditions with started value after OR", function() {
+        assertAutoComplete({
+          serverResponses: {
+            "/notebook/api/sample/database_one/testTable/string": {
+              status: 0,
+              headers: ['id'],
+              rows: [['ab'], ['cd'], ['ef']]
+            }
+          },
+          beforeCursor: "SELECT * FROM testTable WHERE id = 1 OR string =",
+          afterCursor: "",
+          expectedSuggestions: ["'ab'", "'cd'", "'ef'"]
+        });
+      });
+    });
+
     describe("field completion", function() {
 
       it("should suggest columns for table", function() {