Эх сурвалжийг харах

[core] Support value sample autocomplete for nested structs in complex types

Johan Ahlen 10 жил өмнө
parent
commit
64d5baa

+ 29 - 13
desktop/core/src/desktop/static/desktop/js/sqlAutocompleter.js

@@ -188,22 +188,38 @@
       tableName = tableAndComplexRefs[0].value;
     }
     if (tableName) {
-      self.snippet.getAssistHelper().fetchFields(self.snippet, tableName, fields, function(data) {
-        if (data.sample) {
-          var isString = data.type === "string";
-          var values = $.map(data.sample.sort(), function(value, index) {
-            return {
-              meta: "value",
-              score: 900 - index,
-              value: isString ? "'" + value + "'" : new String(value)
+      var completeFields = [];
+      // For impala we need to check each part with the API, it could be a map or array in which case we need to add
+      // either "value" or "item" in between.
+      var fetchImpalaFields = function (remainingParts) {
+        completeFields.push(remainingParts.shift());
+        if (remainingParts.length > 0 && remainingParts[0] == "value" || remainingParts[0] == "key") {
+          fetchImpalaFields(remainingParts);
+        } else {
+          self.snippet.getAssistHelper().fetchFields(self.snippet, tableName, completeFields, function (data) {
+            if (data.type === "map") {
+              completeFields.push("value");
+              fetchImpalaFields(remainingParts);
+            } else if (data.type === "array") {
+              completeFields.push("item");
+              fetchImpalaFields(remainingParts);
+            } else if (remainingParts.length == 0 && data.sample) {
+              var isString = data.type === "string";
+              var values = $.map(data.sample.sort(), function(value, index) {
+                return {
+                  meta: "value",
+                  score: 900 - index,
+                  value: isString ? "'" + value + "'" : new String(value)
+                }
+              });
+              callback(tableAndComplexRefs.concat(values));
+            } else {
+              callback(tableAndComplexRefs);
             }
           });
-          callback(tableAndComplexRefs.concat(values));
-        } else {
-          callback(tableAndComplexRefs);
         }
-      });
-      return;
+      };
+      fetchImpalaFields(fields);
     }
   };
 

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

@@ -723,6 +723,23 @@ define([
           expectedSuggestions: ["testTable", "1", "2", "3"]
         });
       });
+
+      it("should suggest values from fields in map values in conditions", function() {
+        assertAutoComplete({
+          serverResponses: {
+            "/notebook/api/autocomplete/testDb/testTable/testMap" : {
+              type: "map"
+            },
+            "/notebook/api/autocomplete/testDb/testTable/testMap/value/field" : {
+              sample: [1, 2, 3],
+              type: "int"
+            }
+          },
+          beforeCursor: "SELECT * FROM testTable t, t.testMap m WHERE m.field = ",
+          afterCursor: "",
+          expectedSuggestions: ["t.", "m.", "1", "2", "3"]
+        });
+      })
     });
 
     describe("field completion", function() {