فهرست منبع

HUE-7913 [autocomplete] Report quoted variable locations with possible column references

Johan Ahlen 7 سال پیش
والد
کامیت
80a6c7d

+ 18 - 2
desktop/core/src/desktop/static/desktop/js/autocomplete/jison/sql_main.jison

@@ -2565,8 +2565,24 @@ ApproximateNumericLiteral
  ;
 
 GeneralLiteral
- : SingleQuotedValue  -> { types: [ 'STRING' ] }
- | DoubleQuotedValue  -> { types: [ 'STRING' ] }
+ : SingleQuotedValue
+   {
+     if (/\$\{[^}]*\}/.test($1)) {
+       parser.addVariableLocation(@1, $1);
+       $$ = { types: [ 'STRING' ], columnReference: [{ name: $1 }] }
+     } else {
+       $$ = { types: [ 'STRING' ] }
+     }
+   }
+ | DoubleQuotedValue
+   {
+     if (/\$\{[^}]*\}/.test($1)) {
+       parser.addVariableLocation(@1, $1);
+       $$ = { types: [ 'STRING' ], columnReference: [{ name: $1 }] }
+     } else {
+       $$ = { types: [ 'STRING' ] }
+     }
+   }
  | TruthValue         -> { types: [ 'BOOLEAN' ] }
  ;
 

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

@@ -1048,6 +1048,38 @@
             ]
           });
         });
+
+        it('should variable location for "select * from tbl where col = \'${var_name=10}\'; |"', function() {
+          assertLocations({
+            beforeCursor: 'select * from tbl where col = \'${var_name=10}\'; ',
+            expectedLocations: [
+              { type: 'statement', location: { first_line: 1, last_line: 1, first_column: 1, last_column: 47 } },
+              { type: 'selectList', missing: false, location: { first_line: 1, last_line: 1, first_column: 8, last_column: 9 } },
+              { type: 'asterisk', location: { first_line: 1, last_line: 1, first_column: 8, last_column: 9 }, tables: [{ identifierChain: [{ name: 'tbl' }] }] },
+              { type: 'table', location: { first_line: 1, last_line: 1, first_column: 15, last_column: 18 }, identifierChain: [{ name: 'tbl' }] },
+              { type: 'whereClause', missing: false, location: { first_line: 1, last_line: 1, first_column: 19, last_column: 47 } },
+              { type: 'column', location: { first_line: 1, last_line: 1, first_column: 25, last_column: 28 }, identifierChain: [{ name: 'col' }], qualified: false, tables: [{ identifierChain: [{ name: 'tbl' }] }] },
+              { type: 'variable', location: { first_line: 1, last_line: 1, first_column: 31, last_column: 47 }, value: '${var_name=10}', colRef: { identifierChain: [{ name: 'col' }], tables: [{ identifierChain: [{ name: 'tbl' }] }] } },
+              { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 47, last_column: 47 } }
+            ]
+          });
+        });
+
+        it('should variable location for "select * from tbl where col = "${var_name=10}"; |"', function() {
+          assertLocations({
+            beforeCursor: 'select * from tbl where col = "${var_name=10}"; ',
+            expectedLocations: [
+              { type: 'statement', location: { first_line: 1, last_line: 1, first_column: 1, last_column: 47 } },
+              { type: 'selectList', missing: false, location: { first_line: 1, last_line: 1, first_column: 8, last_column: 9 } },
+              { type: 'asterisk', location: { first_line: 1, last_line: 1, first_column: 8, last_column: 9 }, tables: [{ identifierChain: [{ name: 'tbl' }] }] },
+              { type: 'table', location: { first_line: 1, last_line: 1, first_column: 15, last_column: 18 }, identifierChain: [{ name: 'tbl' }] },
+              { type: 'whereClause', missing: false, location: { first_line: 1, last_line: 1, first_column: 19, last_column: 47 } },
+              { type: 'column', location: { first_line: 1, last_line: 1, first_column: 25, last_column: 28 }, identifierChain: [{ name: 'col' }], qualified: false, tables: [{ identifierChain: [{ name: 'tbl' }] }] },
+              { type: 'variable', location: { first_line: 1, last_line: 1, first_column: 31, last_column: 47 }, value: '${var_name=10}', colRef: { identifierChain: [{ name: 'col' }], tables: [{ identifierChain: [{ name: 'tbl' }] }] } },
+              { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 47, last_column: 47 } }
+            ]
+          });
+        });
       });
     })
   });

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlAutocompleteParser.js


+ 12 - 3
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlParseSupport.js

@@ -1528,6 +1528,16 @@ var SqlParseSupport = (function () {
       });
     };
 
+    parser.addVariableLocation = function (location, value) {
+      if (/\$\{[^}]*\}/.test(value)) {
+        parser.yy.locations.push({
+          type: 'variable',
+          location: adjustLocationForCursor(location),
+          value: value
+        });
+      }
+    };
+
     parser.addColumnLocation = function (location, identifierChain) {
       var isVariable = identifierChain.length && /\$\{[^}]*\}/.test(identifierChain[identifierChain.length - 1].name);
       if (isVariable) {
@@ -1578,7 +1588,6 @@ var SqlParseSupport = (function () {
 
     parser.addColRefToVariableIfExists = function (left, right) {
       if (left && left.columnReference && left.columnReference.length && right && right.columnReference && right.columnReference.length && parser.yy.locations.length > 1) {
-
         var addColRefToVariableLocation = function (variableValue, colRef) {
           for (var i = parser.yy.locations.length - 1; i > 0; i--) {
             var location = parser.yy.locations[i];
@@ -1836,8 +1845,8 @@ var SqlParseSupport = (function () {
     'suggestKeywords', 'suggestColRefKeywords', 'suggestTablesOrColumns', 'suggestFunctions', 'suggestAggregateFunctions', 'suggestAnalyticFunctions',
     'suggestColumns', 'suggestGroupBys', 'suggestOrderBys', 'suggestFilters', 'suggestKeyValues', 'suggestTables', 'addFunctionLocation',
     'addStatementLocation', 'firstDefined', 'addClauseLocation', 'addHdfsLocation', 'addDatabaseLocation', 'addColumnAliasLocation', 'addTableAliasLocation',
-    'addSubqueryAliasLocation', 'addTableLocation', 'addAsteriskLocation', 'addColumnLocation', 'addCteAliasLocation', 'addUnknownLocation',
-    'suggestDatabases', 'suggestHdfs', 'suggestValues'];
+    'addSubqueryAliasLocation', 'addTableLocation', 'addAsteriskLocation', 'addVariableLocation', 'addColumnLocation', 'addCteAliasLocation', 'addUnknownLocation',
+    'addColRefToVariableIfExists', 'suggestDatabases', 'suggestHdfs', 'suggestValues'];
 
   var SYNTAX_PARSER_NOOP = function () {};
 

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlSyntaxParser.js


برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است