Explorar o código

HUE-7738 [editor] Extract shared type logic from the parsers into one place

Johan Ahlen %!s(int64=5) %!d(string=hai) anos
pai
achega
bc9968e21d

+ 8 - 59
desktop/core/src/desktop/js/parse/sql/calcite/sqlParseSupport.js

@@ -17,6 +17,11 @@
 import { SqlFunctions } from 'sql/sqlFunctions';
 import { matchesType } from 'sql/reference/typeUtils';
 import stringDistance from 'sql/stringDistance';
+import {
+  applyTypeToSuggestions,
+  getSubQuery,
+  valueExpressionSuggest
+} from 'parse/sql/sqlParseUtils';
 
 const identifierEquals = (a, b) =>
   a &&
@@ -288,39 +293,9 @@ const initSqlParser = function(parser) {
     parser.yy.result.suggestJoins = details || {};
   };
 
-  parser.valueExpressionSuggest = function(oppositeValueExpression, operator) {
-    if (oppositeValueExpression && oppositeValueExpression.columnReference) {
-      parser.suggestValues();
-      parser.yy.result.colRef = { identifierChain: oppositeValueExpression.columnReference };
-    }
-    parser.suggestColumns();
-    parser.suggestFunctions();
-    let keywords = [
-      { value: 'CASE', weight: 450 },
-      { value: 'FALSE', weight: 450 },
-      { value: 'NULL', weight: 450 },
-      { value: 'TRUE', weight: 450 }
-    ];
-    if (typeof oppositeValueExpression === 'undefined' || typeof operator === 'undefined') {
-      keywords = keywords.concat(['EXISTS', 'NOT']);
-    }
-    if (oppositeValueExpression && oppositeValueExpression.types[0] === 'NUMBER') {
-      parser.applyTypeToSuggestions(['NUMBER']);
-    }
-    parser.suggestKeywords(keywords);
-  };
+  parser.valueExpressionSuggest = valueExpressionSuggest.bind(null, parser);
 
-  parser.applyTypeToSuggestions = function(types) {
-    if (types[0] === 'BOOLEAN') {
-      return;
-    }
-    if (parser.yy.result.suggestFunctions && !parser.yy.result.suggestFunctions.types) {
-      parser.yy.result.suggestFunctions.types = types;
-    }
-    if (parser.yy.result.suggestColumns && !parser.yy.result.suggestColumns.types) {
-      parser.yy.result.suggestColumns.types = types;
-    }
-  };
+  parser.applyTypeToSuggestions = applyTypeToSuggestions.bind(null, parser);
 
   parser.findCaseType = function(whenThenList) {
     const types = {};
@@ -1042,33 +1017,7 @@ const initSqlParser = function(parser) {
     }
   };
 
-  parser.getSubQuery = function(cols) {
-    const columns = [];
-    cols.selectList.forEach(col => {
-      const result = {};
-      if (col.alias) {
-        result.alias = col.alias;
-      }
-      if (col.valueExpression && col.valueExpression.columnReference) {
-        result.identifierChain = col.valueExpression.columnReference;
-      } else if (col.asterisk) {
-        result.identifierChain = [{ asterisk: true }];
-      }
-      if (
-        col.valueExpression &&
-        col.valueExpression.types &&
-        col.valueExpression.types.length === 1
-      ) {
-        result.type = col.valueExpression.types[0];
-      }
-
-      columns.push(result);
-    });
-
-    return {
-      columns: columns
-    };
-  };
+  parser.getSubQuery = getSubQuery;
 
   parser.addTablePrimary = function(ref) {
     if (typeof parser.yy.latestTablePrimaries === 'undefined') {

+ 8 - 59
desktop/core/src/desktop/js/parse/sql/druid/sqlParseSupport.js

@@ -17,6 +17,11 @@
 import { SqlFunctions } from 'sql/sqlFunctions';
 import { matchesType } from 'sql/reference/typeUtils';
 import stringDistance from 'sql/stringDistance';
+import {
+  applyTypeToSuggestions,
+  getSubQuery,
+  valueExpressionSuggest
+} from 'parse/sql/sqlParseUtils';
 
 const identifierEquals = (a, b) =>
   a &&
@@ -288,39 +293,9 @@ const initSqlParser = function(parser) {
     parser.yy.result.suggestJoins = details || {};
   };
 
-  parser.valueExpressionSuggest = function(oppositeValueExpression, operator) {
-    if (oppositeValueExpression && oppositeValueExpression.columnReference) {
-      parser.suggestValues();
-      parser.yy.result.colRef = { identifierChain: oppositeValueExpression.columnReference };
-    }
-    parser.suggestColumns();
-    parser.suggestFunctions();
-    let keywords = [
-      { value: 'CASE', weight: 450 },
-      { value: 'FALSE', weight: 450 },
-      { value: 'NULL', weight: 450 },
-      { value: 'TRUE', weight: 450 }
-    ];
-    if (typeof oppositeValueExpression === 'undefined' || typeof operator === 'undefined') {
-      keywords = keywords.concat(['EXISTS', 'NOT']);
-    }
-    if (oppositeValueExpression && oppositeValueExpression.types[0] === 'NUMBER') {
-      parser.applyTypeToSuggestions(['NUMBER']);
-    }
-    parser.suggestKeywords(keywords);
-  };
+  parser.valueExpressionSuggest = valueExpressionSuggest.bind(null, parser);
 
-  parser.applyTypeToSuggestions = function(types) {
-    if (types[0] === 'BOOLEAN') {
-      return;
-    }
-    if (parser.yy.result.suggestFunctions && !parser.yy.result.suggestFunctions.types) {
-      parser.yy.result.suggestFunctions.types = types;
-    }
-    if (parser.yy.result.suggestColumns && !parser.yy.result.suggestColumns.types) {
-      parser.yy.result.suggestColumns.types = types;
-    }
-  };
+  parser.applyTypeToSuggestions = applyTypeToSuggestions.bind(null, parser);
 
   parser.findCaseType = function(whenThenList) {
     const types = {};
@@ -1042,33 +1017,7 @@ const initSqlParser = function(parser) {
     }
   };
 
-  parser.getSubQuery = function(cols) {
-    const columns = [];
-    cols.selectList.forEach(col => {
-      const result = {};
-      if (col.alias) {
-        result.alias = col.alias;
-      }
-      if (col.valueExpression && col.valueExpression.columnReference) {
-        result.identifierChain = col.valueExpression.columnReference;
-      } else if (col.asterisk) {
-        result.identifierChain = [{ asterisk: true }];
-      }
-      if (
-        col.valueExpression &&
-        col.valueExpression.types &&
-        col.valueExpression.types.length === 1
-      ) {
-        result.type = col.valueExpression.types[0];
-      }
-
-      columns.push(result);
-    });
-
-    return {
-      columns: columns
-    };
-  };
+  parser.getSubQuery = getSubQuery;
 
   parser.addTablePrimary = function(ref) {
     if (typeof parser.yy.latestTablePrimaries === 'undefined') {

+ 8 - 59
desktop/core/src/desktop/js/parse/sql/elasticsearch/sqlParseSupport.js

@@ -17,6 +17,11 @@
 import { SqlFunctions } from 'sql/sqlFunctions';
 import { matchesType } from 'sql/reference/typeUtils';
 import stringDistance from 'sql/stringDistance';
+import {
+  applyTypeToSuggestions,
+  getSubQuery,
+  valueExpressionSuggest
+} from 'parse/sql/sqlParseUtils';
 
 const identifierEquals = (a, b) =>
   a &&
@@ -288,39 +293,9 @@ const initSqlParser = function(parser) {
     parser.yy.result.suggestJoins = details || {};
   };
 
-  parser.valueExpressionSuggest = function(oppositeValueExpression, operator) {
-    if (oppositeValueExpression && oppositeValueExpression.columnReference) {
-      parser.suggestValues();
-      parser.yy.result.colRef = { identifierChain: oppositeValueExpression.columnReference };
-    }
-    parser.suggestColumns();
-    parser.suggestFunctions();
-    let keywords = [
-      { value: 'CASE', weight: 450 },
-      { value: 'FALSE', weight: 450 },
-      { value: 'NULL', weight: 450 },
-      { value: 'TRUE', weight: 450 }
-    ];
-    if (typeof oppositeValueExpression === 'undefined' || typeof operator === 'undefined') {
-      keywords = keywords.concat(['EXISTS', 'NOT']);
-    }
-    if (oppositeValueExpression && oppositeValueExpression.types[0] === 'NUMBER') {
-      parser.applyTypeToSuggestions(['NUMBER']);
-    }
-    parser.suggestKeywords(keywords);
-  };
+  parser.valueExpressionSuggest = valueExpressionSuggest.bind(null, parser);
 
-  parser.applyTypeToSuggestions = function(types) {
-    if (types[0] === 'BOOLEAN') {
-      return;
-    }
-    if (parser.yy.result.suggestFunctions && !parser.yy.result.suggestFunctions.types) {
-      parser.yy.result.suggestFunctions.types = types;
-    }
-    if (parser.yy.result.suggestColumns && !parser.yy.result.suggestColumns.types) {
-      parser.yy.result.suggestColumns.types = types;
-    }
-  };
+  parser.applyTypeToSuggestions = applyTypeToSuggestions.bind(null, parser);
 
   parser.findCaseType = function(whenThenList) {
     const types = {};
@@ -1042,33 +1017,7 @@ const initSqlParser = function(parser) {
     }
   };
 
-  parser.getSubQuery = function(cols) {
-    const columns = [];
-    cols.selectList.forEach(col => {
-      const result = {};
-      if (col.alias) {
-        result.alias = col.alias;
-      }
-      if (col.valueExpression && col.valueExpression.columnReference) {
-        result.identifierChain = col.valueExpression.columnReference;
-      } else if (col.asterisk) {
-        result.identifierChain = [{ asterisk: true }];
-      }
-      if (
-        col.valueExpression &&
-        col.valueExpression.types &&
-        col.valueExpression.types.length === 1
-      ) {
-        result.type = col.valueExpression.types[0];
-      }
-
-      columns.push(result);
-    });
-
-    return {
-      columns: columns
-    };
-  };
+  parser.getSubQuery = getSubQuery;
 
   parser.addTablePrimary = function(ref) {
     if (typeof parser.yy.latestTablePrimaries === 'undefined') {

+ 8 - 59
desktop/core/src/desktop/js/parse/sql/flink/sqlParseSupport.js

@@ -17,6 +17,11 @@
 import { SqlFunctions } from 'sql/sqlFunctions';
 import { matchesType } from 'sql/reference/typeUtils';
 import stringDistance from 'sql/stringDistance';
+import {
+  applyTypeToSuggestions,
+  getSubQuery,
+  valueExpressionSuggest
+} from 'parse/sql/sqlParseUtils';
 
 const identifierEquals = (a, b) =>
   a &&
@@ -288,39 +293,9 @@ const initSqlParser = function(parser) {
     parser.yy.result.suggestJoins = details || {};
   };
 
-  parser.valueExpressionSuggest = function(oppositeValueExpression, operator) {
-    if (oppositeValueExpression && oppositeValueExpression.columnReference) {
-      parser.suggestValues();
-      parser.yy.result.colRef = { identifierChain: oppositeValueExpression.columnReference };
-    }
-    parser.suggestColumns();
-    parser.suggestFunctions();
-    let keywords = [
-      { value: 'CASE', weight: 450 },
-      { value: 'FALSE', weight: 450 },
-      { value: 'NULL', weight: 450 },
-      { value: 'TRUE', weight: 450 }
-    ];
-    if (typeof oppositeValueExpression === 'undefined' || typeof operator === 'undefined') {
-      keywords = keywords.concat(['EXISTS', 'NOT']);
-    }
-    if (oppositeValueExpression && oppositeValueExpression.types[0] === 'NUMBER') {
-      parser.applyTypeToSuggestions(['NUMBER']);
-    }
-    parser.suggestKeywords(keywords);
-  };
+  parser.valueExpressionSuggest = valueExpressionSuggest.bind(null, parser);
 
-  parser.applyTypeToSuggestions = function(types) {
-    if (types[0] === 'BOOLEAN') {
-      return;
-    }
-    if (parser.yy.result.suggestFunctions && !parser.yy.result.suggestFunctions.types) {
-      parser.yy.result.suggestFunctions.types = types;
-    }
-    if (parser.yy.result.suggestColumns && !parser.yy.result.suggestColumns.types) {
-      parser.yy.result.suggestColumns.types = types;
-    }
-  };
+  parser.applyTypeToSuggestions = applyTypeToSuggestions.bind(null, parser);
 
   parser.findCaseType = function(whenThenList) {
     const types = {};
@@ -1042,33 +1017,7 @@ const initSqlParser = function(parser) {
     }
   };
 
-  parser.getSubQuery = function(cols) {
-    const columns = [];
-    cols.selectList.forEach(col => {
-      const result = {};
-      if (col.alias) {
-        result.alias = col.alias;
-      }
-      if (col.valueExpression && col.valueExpression.columnReference) {
-        result.identifierChain = col.valueExpression.columnReference;
-      } else if (col.asterisk) {
-        result.identifierChain = [{ asterisk: true }];
-      }
-      if (
-        col.valueExpression &&
-        col.valueExpression.types &&
-        col.valueExpression.types.length === 1
-      ) {
-        result.type = col.valueExpression.types[0];
-      }
-
-      columns.push(result);
-    });
-
-    return {
-      columns: columns
-    };
-  };
+  parser.getSubQuery = getSubQuery;
 
   parser.addTablePrimary = function(ref) {
     if (typeof parser.yy.latestTablePrimaries === 'undefined') {

+ 8 - 59
desktop/core/src/desktop/js/parse/sql/generic/sqlParseSupport.js

@@ -17,6 +17,11 @@
 import { SqlFunctions } from 'sql/sqlFunctions';
 import { matchesType } from 'sql/reference/typeUtils';
 import stringDistance from 'sql/stringDistance';
+import {
+  applyTypeToSuggestions,
+  getSubQuery,
+  valueExpressionSuggest
+} from 'parse/sql/sqlParseUtils';
 
 const identifierEquals = (a, b) =>
   a &&
@@ -288,39 +293,9 @@ const initSqlParser = function(parser) {
     parser.yy.result.suggestJoins = details || {};
   };
 
-  parser.valueExpressionSuggest = function(oppositeValueExpression, operator) {
-    if (oppositeValueExpression && oppositeValueExpression.columnReference) {
-      parser.suggestValues();
-      parser.yy.result.colRef = { identifierChain: oppositeValueExpression.columnReference };
-    }
-    parser.suggestColumns();
-    parser.suggestFunctions();
-    let keywords = [
-      { value: 'CASE', weight: 450 },
-      { value: 'FALSE', weight: 450 },
-      { value: 'NULL', weight: 450 },
-      { value: 'TRUE', weight: 450 }
-    ];
-    if (typeof oppositeValueExpression === 'undefined' || typeof operator === 'undefined') {
-      keywords = keywords.concat(['EXISTS', 'NOT']);
-    }
-    if (oppositeValueExpression && oppositeValueExpression.types[0] === 'NUMBER') {
-      parser.applyTypeToSuggestions(['NUMBER']);
-    }
-    parser.suggestKeywords(keywords);
-  };
+  parser.valueExpressionSuggest = valueExpressionSuggest.bind(null, parser);
 
-  parser.applyTypeToSuggestions = function(types) {
-    if (types[0] === 'BOOLEAN') {
-      return;
-    }
-    if (parser.yy.result.suggestFunctions && !parser.yy.result.suggestFunctions.types) {
-      parser.yy.result.suggestFunctions.types = types;
-    }
-    if (parser.yy.result.suggestColumns && !parser.yy.result.suggestColumns.types) {
-      parser.yy.result.suggestColumns.types = types;
-    }
-  };
+  parser.applyTypeToSuggestions = applyTypeToSuggestions.bind(null, parser);
 
   parser.findCaseType = function(whenThenList) {
     const types = {};
@@ -1042,33 +1017,7 @@ const initSqlParser = function(parser) {
     }
   };
 
-  parser.getSubQuery = function(cols) {
-    const columns = [];
-    cols.selectList.forEach(col => {
-      const result = {};
-      if (col.alias) {
-        result.alias = col.alias;
-      }
-      if (col.valueExpression && col.valueExpression.columnReference) {
-        result.identifierChain = col.valueExpression.columnReference;
-      } else if (col.asterisk) {
-        result.identifierChain = [{ asterisk: true }];
-      }
-      if (
-        col.valueExpression &&
-        col.valueExpression.types &&
-        col.valueExpression.types.length === 1
-      ) {
-        result.type = col.valueExpression.types[0];
-      }
-
-      columns.push(result);
-    });
-
-    return {
-      columns: columns
-    };
-  };
+  parser.getSubQuery = getSubQuery;
 
   parser.addTablePrimary = function(ref) {
     if (typeof parser.yy.latestTablePrimaries === 'undefined') {

+ 3 - 38
desktop/core/src/desktop/js/parse/sql/hive/sqlParseSupport.js

@@ -17,6 +17,7 @@
 import { SqlFunctions } from 'sql/sqlFunctions';
 import { matchesType } from 'sql/reference/typeUtils';
 import stringDistance from 'sql/stringDistance';
+import { applyTypeToSuggestions, getSubQuery } from 'parse/sql/sqlParseUtils';
 
 const identifierEquals = (a, b) =>
   a &&
@@ -316,17 +317,7 @@ const initSqlParser = function(parser) {
     parser.suggestKeywords(keywords);
   };
 
-  parser.applyTypeToSuggestions = function(types) {
-    if (types[0] === 'BOOLEAN') {
-      return;
-    }
-    if (parser.yy.result.suggestFunctions && !parser.yy.result.suggestFunctions.types) {
-      parser.yy.result.suggestFunctions.types = types;
-    }
-    if (parser.yy.result.suggestColumns && !parser.yy.result.suggestColumns.types) {
-      parser.yy.result.suggestColumns.types = types;
-    }
-  };
+  parser.applyTypeToSuggestions = applyTypeToSuggestions.bind(null, parser);
 
   parser.findCaseType = function(whenThenList) {
     const types = {};
@@ -1170,33 +1161,7 @@ const initSqlParser = function(parser) {
     }
   };
 
-  parser.getSubQuery = function(cols) {
-    const columns = [];
-    cols.selectList.forEach(col => {
-      const result = {};
-      if (col.alias) {
-        result.alias = col.alias;
-      }
-      if (col.valueExpression && col.valueExpression.columnReference) {
-        result.identifierChain = col.valueExpression.columnReference;
-      } else if (col.asterisk) {
-        result.identifierChain = [{ asterisk: true }];
-      }
-      if (
-        col.valueExpression &&
-        col.valueExpression.types &&
-        col.valueExpression.types.length === 1
-      ) {
-        result.type = col.valueExpression.types[0];
-      }
-
-      columns.push(result);
-    });
-
-    return {
-      columns: columns
-    };
-  };
+  parser.getSubQuery = getSubQuery;
 
   parser.addTablePrimary = function(ref) {
     if (typeof parser.yy.latestTablePrimaries === 'undefined') {

+ 3 - 38
desktop/core/src/desktop/js/parse/sql/impala/sqlParseSupport.js

@@ -17,6 +17,7 @@
 import { SqlFunctions } from 'sql/sqlFunctions';
 import { matchesType } from 'sql/reference/typeUtils';
 import stringDistance from 'sql/stringDistance';
+import { applyTypeToSuggestions, getSubQuery } from 'parse/sql/sqlParseUtils';
 
 const identifierEquals = (a, b) =>
   a &&
@@ -321,17 +322,7 @@ const initSqlParser = function(parser) {
     parser.suggestKeywords(keywords);
   };
 
-  parser.applyTypeToSuggestions = function(types) {
-    if (types[0] === 'BOOLEAN') {
-      return;
-    }
-    if (parser.yy.result.suggestFunctions && !parser.yy.result.suggestFunctions.types) {
-      parser.yy.result.suggestFunctions.types = types;
-    }
-    if (parser.yy.result.suggestColumns && !parser.yy.result.suggestColumns.types) {
-      parser.yy.result.suggestColumns.types = types;
-    }
-  };
+  parser.applyTypeToSuggestions = applyTypeToSuggestions.bind(null, parser);
 
   parser.findCaseType = function(whenThenList) {
     const types = {};
@@ -1257,33 +1248,7 @@ const initSqlParser = function(parser) {
     }
   };
 
-  parser.getSubQuery = function(cols) {
-    const columns = [];
-    cols.selectList.forEach(col => {
-      const result = {};
-      if (col.alias) {
-        result.alias = col.alias;
-      }
-      if (col.valueExpression && col.valueExpression.columnReference) {
-        result.identifierChain = col.valueExpression.columnReference;
-      } else if (col.asterisk) {
-        result.identifierChain = [{ asterisk: true }];
-      }
-      if (
-        col.valueExpression &&
-        col.valueExpression.types &&
-        col.valueExpression.types.length === 1
-      ) {
-        result.type = col.valueExpression.types[0];
-      }
-
-      columns.push(result);
-    });
-
-    return {
-      columns: columns
-    };
-  };
+  parser.getSubQuery = getSubQuery;
 
   parser.addTablePrimary = function(ref) {
     if (typeof parser.yy.latestTablePrimaries === 'undefined') {

+ 8 - 59
desktop/core/src/desktop/js/parse/sql/ksql/sqlParseSupport.js

@@ -17,6 +17,11 @@
 import { SqlFunctions } from 'sql/sqlFunctions';
 import { matchesType } from 'sql/reference/typeUtils';
 import stringDistance from 'sql/stringDistance';
+import {
+  applyTypeToSuggestions,
+  getSubQuery,
+  valueExpressionSuggest
+} from 'parse/sql/sqlParseUtils';
 
 const identifierEquals = (a, b) =>
   a &&
@@ -288,39 +293,9 @@ const initSqlParser = function(parser) {
     parser.yy.result.suggestJoins = details || {};
   };
 
-  parser.valueExpressionSuggest = function(oppositeValueExpression, operator) {
-    if (oppositeValueExpression && oppositeValueExpression.columnReference) {
-      parser.suggestValues();
-      parser.yy.result.colRef = { identifierChain: oppositeValueExpression.columnReference };
-    }
-    parser.suggestColumns();
-    parser.suggestFunctions();
-    let keywords = [
-      { value: 'CASE', weight: 450 },
-      { value: 'FALSE', weight: 450 },
-      { value: 'NULL', weight: 450 },
-      { value: 'TRUE', weight: 450 }
-    ];
-    if (typeof oppositeValueExpression === 'undefined' || typeof operator === 'undefined') {
-      keywords = keywords.concat(['EXISTS', 'NOT']);
-    }
-    if (oppositeValueExpression && oppositeValueExpression.types[0] === 'NUMBER') {
-      parser.applyTypeToSuggestions(['NUMBER']);
-    }
-    parser.suggestKeywords(keywords);
-  };
+  parser.valueExpressionSuggest = valueExpressionSuggest.bind(null, parser);
 
-  parser.applyTypeToSuggestions = function(types) {
-    if (types[0] === 'BOOLEAN') {
-      return;
-    }
-    if (parser.yy.result.suggestFunctions && !parser.yy.result.suggestFunctions.types) {
-      parser.yy.result.suggestFunctions.types = types;
-    }
-    if (parser.yy.result.suggestColumns && !parser.yy.result.suggestColumns.types) {
-      parser.yy.result.suggestColumns.types = types;
-    }
-  };
+  parser.applyTypeToSuggestions = applyTypeToSuggestions.bind(null, parser);
 
   parser.findCaseType = function(whenThenList) {
     const types = {};
@@ -1042,33 +1017,7 @@ const initSqlParser = function(parser) {
     }
   };
 
-  parser.getSubQuery = function(cols) {
-    const columns = [];
-    cols.selectList.forEach(col => {
-      const result = {};
-      if (col.alias) {
-        result.alias = col.alias;
-      }
-      if (col.valueExpression && col.valueExpression.columnReference) {
-        result.identifierChain = col.valueExpression.columnReference;
-      } else if (col.asterisk) {
-        result.identifierChain = [{ asterisk: true }];
-      }
-      if (
-        col.valueExpression &&
-        col.valueExpression.types &&
-        col.valueExpression.types.length === 1
-      ) {
-        result.type = col.valueExpression.types[0];
-      }
-
-      columns.push(result);
-    });
-
-    return {
-      columns: columns
-    };
-  };
+  parser.getSubQuery = getSubQuery;
 
   parser.addTablePrimary = function(ref) {
     if (typeof parser.yy.latestTablePrimaries === 'undefined') {

+ 8 - 59
desktop/core/src/desktop/js/parse/sql/phoenix/sqlParseSupport.js

@@ -17,6 +17,11 @@
 import { SqlFunctions } from 'sql/sqlFunctions';
 import { matchesType } from 'sql/reference/typeUtils';
 import stringDistance from 'sql/stringDistance';
+import {
+  applyTypeToSuggestions,
+  getSubQuery,
+  valueExpressionSuggest
+} from 'parse/sql/sqlParseUtils';
 
 const identifierEquals = (a, b) =>
   a &&
@@ -288,39 +293,9 @@ const initSqlParser = function(parser) {
     parser.yy.result.suggestJoins = details || {};
   };
 
-  parser.valueExpressionSuggest = function(oppositeValueExpression, operator) {
-    if (oppositeValueExpression && oppositeValueExpression.columnReference) {
-      parser.suggestValues();
-      parser.yy.result.colRef = { identifierChain: oppositeValueExpression.columnReference };
-    }
-    parser.suggestColumns();
-    parser.suggestFunctions();
-    let keywords = [
-      { value: 'CASE', weight: 450 },
-      { value: 'FALSE', weight: 450 },
-      { value: 'NULL', weight: 450 },
-      { value: 'TRUE', weight: 450 }
-    ];
-    if (typeof oppositeValueExpression === 'undefined' || typeof operator === 'undefined') {
-      keywords = keywords.concat(['EXISTS', 'NOT']);
-    }
-    if (oppositeValueExpression && oppositeValueExpression.types[0] === 'NUMBER') {
-      parser.applyTypeToSuggestions(['NUMBER']);
-    }
-    parser.suggestKeywords(keywords);
-  };
+  parser.valueExpressionSuggest = valueExpressionSuggest.bind(null, parser);
 
-  parser.applyTypeToSuggestions = function(types) {
-    if (types[0] === 'BOOLEAN') {
-      return;
-    }
-    if (parser.yy.result.suggestFunctions && !parser.yy.result.suggestFunctions.types) {
-      parser.yy.result.suggestFunctions.types = types;
-    }
-    if (parser.yy.result.suggestColumns && !parser.yy.result.suggestColumns.types) {
-      parser.yy.result.suggestColumns.types = types;
-    }
-  };
+  parser.applyTypeToSuggestions = applyTypeToSuggestions.bind(null, parser);
 
   parser.findCaseType = function(whenThenList) {
     const types = {};
@@ -1042,33 +1017,7 @@ const initSqlParser = function(parser) {
     }
   };
 
-  parser.getSubQuery = function(cols) {
-    const columns = [];
-    cols.selectList.forEach(col => {
-      const result = {};
-      if (col.alias) {
-        result.alias = col.alias;
-      }
-      if (col.valueExpression && col.valueExpression.columnReference) {
-        result.identifierChain = col.valueExpression.columnReference;
-      } else if (col.asterisk) {
-        result.identifierChain = [{ asterisk: true }];
-      }
-      if (
-        col.valueExpression &&
-        col.valueExpression.types &&
-        col.valueExpression.types.length === 1
-      ) {
-        result.type = col.valueExpression.types[0];
-      }
-
-      columns.push(result);
-    });
-
-    return {
-      columns: columns
-    };
-  };
+  parser.getSubQuery = getSubQuery;
 
   parser.addTablePrimary = function(ref) {
     if (typeof parser.yy.latestTablePrimaries === 'undefined') {

+ 3 - 38
desktop/core/src/desktop/js/parse/sql/presto/sqlParseSupport.js

@@ -15,6 +15,7 @@
 // limitations under the License.
 
 import { matchesType } from 'sql/reference/typeUtils';
+import { applyTypeToSuggestions, getSubQuery } from 'parse/sql/sqlParseUtils';
 
 /**
  * Calculates the Optimal String Alignment distance between two strings. Returns 0 when the strings are equal and the
@@ -373,17 +374,7 @@ const initSqlParser = function(parser) {
     parser.suggestKeywords(keywords);
   };
 
-  parser.applyTypeToSuggestions = function(types) {
-    if (types[0] === 'BOOLEAN') {
-      return;
-    }
-    if (parser.yy.result.suggestFunctions && !parser.yy.result.suggestFunctions.types) {
-      parser.yy.result.suggestFunctions.types = types;
-    }
-    if (parser.yy.result.suggestColumns && !parser.yy.result.suggestColumns.types) {
-      parser.yy.result.suggestColumns.types = types;
-    }
-  };
+  parser.applyTypeToSuggestions = applyTypeToSuggestions.bind(null, parser);
 
   parser.findCaseType = function(whenThenList) {
     const types = {};
@@ -1225,33 +1216,7 @@ const initSqlParser = function(parser) {
     }
   };
 
-  parser.getSubQuery = function(cols) {
-    const columns = [];
-    cols.selectList.forEach(col => {
-      const result = {};
-      if (col.alias) {
-        result.alias = col.alias;
-      }
-      if (col.valueExpression && col.valueExpression.columnReference) {
-        result.identifierChain = col.valueExpression.columnReference;
-      } else if (col.asterisk) {
-        result.identifierChain = [{ asterisk: true }];
-      }
-      if (
-        col.valueExpression &&
-        col.valueExpression.types &&
-        col.valueExpression.types.length === 1
-      ) {
-        result.type = col.valueExpression.types[0];
-      }
-
-      columns.push(result);
-    });
-
-    return {
-      columns: columns
-    };
-  };
+  parser.getSubQuery = getSubQuery;
 
   parser.addTablePrimary = function(ref) {
     if (typeof parser.yy.latestTablePrimaries === 'undefined') {

+ 77 - 0
desktop/core/src/desktop/js/parse/sql/sqlParseUtils.js

@@ -0,0 +1,77 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+export const getSubQuery = cols => {
+  const columns = [];
+  cols.selectList.forEach(col => {
+    const result = {};
+    if (col.alias) {
+      result.alias = col.alias;
+    }
+    if (col.valueExpression && col.valueExpression.columnReference) {
+      result.identifierChain = col.valueExpression.columnReference;
+    } else if (col.asterisk) {
+      result.identifierChain = [{ asterisk: true }];
+    }
+    if (
+      col.valueExpression &&
+      col.valueExpression.types &&
+      col.valueExpression.types.length === 1
+    ) {
+      result.type = col.valueExpression.types[0];
+    }
+
+    columns.push(result);
+  });
+
+  return {
+    columns: columns
+  };
+};
+
+export const applyTypeToSuggestions = (parser, types) => {
+  if (types[0] === 'BOOLEAN') {
+    return;
+  }
+  if (this.yy.result.suggestFunctions && !this.yy.result.suggestFunctions.types) {
+    this.yy.result.suggestFunctions.types = types;
+  }
+  if (this.yy.result.suggestColumns && !this.yy.result.suggestColumns.types) {
+    this.yy.result.suggestColumns.types = types;
+  }
+};
+
+export const valueExpressionSuggest = (parser, oppositeValueExpression, operator) => {
+  if (oppositeValueExpression && oppositeValueExpression.columnReference) {
+    parser.suggestValues();
+    parser.yy.result.colRef = { identifierChain: oppositeValueExpression.columnReference };
+  }
+  parser.suggestColumns();
+  parser.suggestFunctions();
+  let keywords = [
+    { value: 'CASE', weight: 450 },
+    { value: 'FALSE', weight: 450 },
+    { value: 'NULL', weight: 450 },
+    { value: 'TRUE', weight: 450 }
+  ];
+  if (typeof oppositeValueExpression === 'undefined' || typeof operator === 'undefined') {
+    keywords = keywords.concat(['EXISTS', 'NOT']);
+  }
+  if (oppositeValueExpression && oppositeValueExpression.types[0] === 'NUMBER') {
+    applyTypeToSuggestions(parser, ['NUMBER']);
+  }
+  parser.suggestKeywords(keywords);
+};