Sfoglia il codice sorgente

HUE-6976 [editor] Don't suggest to add filters in subqueries for risk quick fix

Johan Ahlen 8 anni fa
parent
commit
d9238dd

+ 8 - 8
desktop/core/src/desktop/static/desktop/js/autocomplete/spec/sqlSpecLocations.js

@@ -242,8 +242,8 @@
           { type: 'table', location: { first_line: 1, last_line: 1, first_column: 16, last_column: 25 }, identifierChain: [{ name: 'blablabla' }] },
           { type: 'asterisk', location: { first_line: 1, last_line: 1, first_column: 39, last_column: 40 }, tables: [{ identifierChain: [{ name: 'blablabla' }] }] },
           { type: 'table', location: { first_line: 1, last_line: 1, first_column: 46, last_column: 55 }, identifierChain: [{ name: 'blablabla' }] },
-          { type: 'whereClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 55, last_column: 55 }}, // Subquery
-          { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 55, last_column: 55 }}, // Subquery
+          { type: 'whereClause', subquery: true, missing: true, location: { first_line: 1, last_line: 1, first_column: 55, last_column: 55 }},
+          { type: 'limitClause', subquery: true, missing: true, location: { first_line: 1, last_line: 1, first_column: 55, last_column: 55 }},
           { type: 'alias', source: 'subquery', alias: 's1', location: { first_line: 1, last_line: 1, first_column: 57, last_column: 59 } },
           { type: 'whereClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 59, last_column: 59 }},
           { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 59, last_column: 59 }}]
@@ -285,8 +285,8 @@
           { type: 'column', location: { first_line: 6, last_line: 6, first_column: 39, last_column: 42 }, identifierChain: [{ name: 'bla' }], tables: [{ identifierChain: [{ name: 'bar' }], alias: 'baa' }] },
           { type: 'column', location: { first_line: 6, last_line: 6, first_column: 54, last_column: 57 }, identifierChain: [{ name: 'ble' }], tables: [{ identifierChain: [{ name: 'bla' }] }] },
           { type: 'table', location: { first_line: 6, last_line: 6, first_column: 63, last_column: 66 }, identifierChain: [{ name: 'bla' }]},
-          { type: 'whereClause', missing: true, location: { first_line: 6, last_line: 6, first_column: 66, last_column: 66 } }, // Subquery
-          { type: 'limitClause', missing: true, location: { first_line: 6, last_line: 6, first_column: 66, last_column: 66 } }, // Subquery
+          { type: 'whereClause', subquery: true, missing: true, location: { first_line: 6, last_line: 6, first_column: 66, last_column: 66 } },
+          { type: 'limitClause', subquery: true, missing: true, location: { first_line: 6, last_line: 6, first_column: 66, last_column: 66 } },
           { type: 'limitClause', missing: true, location: { first_line: 6, last_line: 6, first_column: 67, last_column: 67 } }
         ]
       });
@@ -421,8 +421,8 @@
             { type: 'column', location: { first_line: 1, last_line: 1, first_column: 25, last_column: 28 }, identifierChain:[{ name: 'bar'}], tables: [{ identifierChain: [{ name: 'foo' }] }]},
             { type: 'asterisk', location: { first_line: 1, last_line: 1, first_column: 40, last_column: 41 }, tables: [{ identifierChain: [{ name: 'bla' }] }] },
             { type: 'table', location: { first_line: 1, last_line: 1, first_column: 47, last_column: 50 }, identifierChain: [{ name: 'bla' }]},
-            { type: 'whereClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 50, last_column: 50 }}, // Subquery
-            { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 50, last_column: 50 }}, // Subquery
+            { type: 'whereClause', subquery: true, missing: true, location: { first_line: 1, last_line: 1, first_column: 50, last_column: 50 }},
+            { type: 'limitClause', subquery: true, missing: true, location: { first_line: 1, last_line: 1, first_column: 50, last_column: 50 }},
             { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 51, last_column: 51 }}
           ]
         });
@@ -669,11 +669,11 @@
             { type: 'function', location: { first_line: 1, last_line: 1, first_column: 178, last_column: 181 }, function: 'year' },
             { type: 'table', location: { first_line: 1, last_line: 1, first_column: 183, last_column: 187 }, identifierChain: [{ name:'tran' }]},
             { type: 'column', location: { first_line: 1, last_line: 1, first_column: 188, last_column: 194 }, identifierChain: [{ name: 'tran_d' }], tables: [{ identifierChain: [{ name:'tran' }] }]},
-            { type: 'whereClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 218, last_column: 218 }},
+            { type: 'whereClause', subquery: true, missing: true, location: { first_line: 1, last_line: 1, first_column: 218, last_column: 218 }},
             { type: 'database', location: { first_line: 1, last_line: 1, first_column: 228, last_column: 234 }, identifierChain: [{ name: 'tstDb1' }]},
             { type: 'table', location: { first_line: 1, last_line: 1, first_column: 235, last_column: 237 }, identifierChain: [{ name:'tstDb1' },{ name: 'b1' }]},
             { type: 'column', location: { first_line: 1, last_line: 1, first_column: 238, last_column: 241 }, identifierChain: [{ name: 'cat' }], tables: [{ identifierChain: [{ name:'tstDb1' }, {name: 'b1' }] }]},
-            { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 241, last_column: 241 }},
+            { type: 'limitClause', subquery: true, missing: true, location: { first_line: 1, last_line: 1, first_column: 241, last_column: 241 }},
             { type: 'alias', source: 'subquery', alias: 'tmp', location: { first_line: 1, last_line: 1, first_column: 243, last_column: 246 } },
             { type: 'whereClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 246, last_column: 246 }},
             { type: 'column', location: { first_line: 1, last_line: 1, first_column: 256, last_column: 257 }, identifierChain: [{ name: 'r' }], tables: [{ subQuery: 'tmp' }]},

+ 5 - 4
desktop/core/src/desktop/static/desktop/js/autocomplete/spec/sqlSpecSelect.js

@@ -6964,6 +6964,7 @@
         });
       });
 
+      // TODO: In this case the WHERE clause exists but isn't completely defined both for the query and the subquery
       it('should suggest columns for "SELECT "contains an even number" FROM t1, t2 AS ta2 WHERE EXISTS (SELECT t3.foo FROM t3 WHERE | % 2 = 0"', function() {
         assertAutoComplete({
           beforeCursor: 'SELECT "contains an even number" FROM t1, t2 AS ta2 WHERE EXISTS (SELECT t3.foo FROM t3 WHERE ',
@@ -6984,8 +6985,8 @@
               { type: 'table', location: { first_line: 1, last_line: 1, first_column: 74, last_column: 76}, identifierChain: [{ name: 't3' }]},
               { type: 'column', location: { first_line: 1, last_line: 1, first_column: 77, last_column: 80}, identifierChain: [{ name: 'foo'}], tables: [{ identifierChain: [{ name: 't3' }] }]},
               { type: 'table', location: { first_line: 1, last_line: 1, first_column: 86, last_column: 88}, identifierChain: [{ name: 't3' }] },
-              { type: 'whereClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 88, last_column: 88 } },
-              { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 88, last_column: 88 } }
+              { type: 'whereClause', subquery: true, missing: true, location: { first_line: 1, last_line: 1, first_column: 88, last_column: 88 } },
+              { type: 'limitClause', subquery: true, missing: true, location: { first_line: 1, last_line: 1, first_column: 88, last_column: 88 } }
             ]
           }
         });
@@ -7003,9 +7004,9 @@
               { type: 'alias', source: 'table', alias: 'tt', location: { first_line: 1, last_line: 1, first_column: 24, last_column: 26 }, identifierChain: [{ name: 'testTable' }] },
               { type: 'column', location: { first_line: 1, last_line: 1, first_column: 36, last_column: 39}, identifierChain: [{ name: 'bla'}], tables: [{ identifierChain: [{ name: 'abc' }] }] },
               { type: 'table', location: { first_line: 1, last_line: 1, first_column: 45, last_column: 48}, identifierChain: [{ name: 'abc' }] },
-              { type: 'whereClause', missing: false, location: { first_line: 1, last_line: 1, first_column: 49, last_column: 62 } },
+              { type: 'whereClause', subquery: true, missing: false, location: { first_line: 1, last_line: 1, first_column: 49, last_column: 62 } },
               { type: 'column', location: { first_line: 1, last_line: 1, first_column: 55, last_column: 58}, identifierChain: [{ name: 'foo'}], tables: [{ identifierChain: [{ name: 'abc' }] }] },
-              { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 62, last_column: 62 } },
+              { type: 'limitClause', subquery: true, missing: true, location: { first_line: 1, last_line: 1, first_column: 62, last_column: 62 } },
               { type: 'alias', source: 'subquery', alias: 'bar', location: { first_line: 1, last_line: 1, first_column: 64, last_column: 67 } },
               { type: 'whereClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 67, last_column: 67 } },
               { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 67, last_column: 67 } }

+ 10 - 2
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlParseSupport.js

@@ -108,6 +108,10 @@ var SqlParseSupport = (function () {
       parser.yy.latestCommonTableExpressions = identifiers;
     };
 
+    parser.isInSubquery = function () {
+      return !!parser.yy.primariesStack.length
+    };
+
     parser.pushQueryState = function () {
       parser.yy.resultStack.push(parser.yy.result);
       parser.yy.locationsStack.push(parser.yy.locations);
@@ -1280,11 +1284,15 @@ var SqlParseSupport = (function () {
     };
 
     parser.addClauseLocation = function (type, precedingLocation, locationIfPresent) {
-      parser.yy.locations.push({
+      var location = {
         type: type,
         missing: !locationIfPresent,
         location: adjustLocationForCursor(locationIfPresent || { first_line: precedingLocation.last_line, first_column: precedingLocation.last_column, last_line: precedingLocation.last_line, last_column: precedingLocation.last_column })
-      })
+      };
+      if (parser.isInSubquery()) {
+        location.subquery = true;
+      }
+      parser.yy.locations.push(location)
     };
 
     parser.addHdfsLocation = function (location, path) {

+ 1 - 1
desktop/core/src/desktop/templates/assist.mako

@@ -2162,7 +2162,7 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, get_ord
         var self = this;
         if (self.activeLocations() && self.activeEditor()) {
           self.activeLocations().activeStatementLocations.every(function (location) {
-            if (location.type === 'whereClause' && location.missing) {
+            if (location.type === 'whereClause' && location.missing && !location.subquery) {
 
               self.activeEditor().moveCursorToPosition({
                 row: location.location.last_line - 1,