浏览代码

[editor] Add types for the data catalog and autocomplete parsers

Johan Ahlen 5 年之前
父节点
当前提交
2b07905f8a

+ 2 - 3
desktop/core/src/desktop/js/catalog/dataCatalog.js

@@ -271,7 +271,6 @@ export class DataCatalog {
    * @param {Object} options
    * @param {ContextNamespace} options.namespace - The context namespace
    * @param {ContextCompute} options.compute - The context compute
-   * @param {Connector} options.connector
    * @param {string[][]} options.paths
    * @param {boolean} [options.silenceErrors] - Default true
    * @param {boolean} [options.cancellable] - Default false
@@ -719,7 +718,7 @@ export class DataCatalog {
    * @param {Connector} options.connector
    * @param {string[][]} options.paths
    *
-   * @return {Promise}
+   * @return {JQueryPromise}
    */
   getMultiTableEntry(options) {
     const self = this;
@@ -868,7 +867,7 @@ export default {
    * @param {Connector} options.connector
    * @param {string[][]} options.paths
    *
-   * @return {Promise}
+   * @return {JQueryPromise}
    */
   getMultiTableEntry: function (options) {
     return getCatalog(options.connector).getMultiTableEntry(options);

+ 2 - 4
desktop/core/src/desktop/js/catalog/dataCatalogEntry.js

@@ -212,7 +212,7 @@ const getFromMultiTableCatalog = function (catalogEntry, options, functionName)
  *
  * @constructor
  */
-class DataCatalogEntry {
+export default class DataCatalogEntry {
   constructor(options) {
     const self = this;
 
@@ -1482,7 +1482,7 @@ class DataCatalogEntry {
    * @param {boolean} [options.refreshCache]
    * @param {boolean} [options.cancellable] - Default false
    *
-   * @return {CancellableJqPromise}
+   * @return {CancellableJqPromise<SourceMeta>}
    */
   getSourceMeta(options) {
     const self = this;
@@ -1796,5 +1796,3 @@ class DataCatalogEntry {
     return getFromMultiTableCatalog(self, options, 'getTopJoins');
   }
 }
-
-export default DataCatalogEntry;

+ 128 - 0
desktop/core/src/desktop/js/catalog/dataCatalogEntry/index.d.ts

@@ -0,0 +1,128 @@
+// 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.
+
+import CancellableJqPromise from 'api/cancellableJqPromise';
+import { UdfDetails } from 'sql/reference/types';
+
+export = DataCatalogEntry;
+
+declare class DataCatalogEntry {
+  name: string;
+  optimizerPopularity: {
+    [attr: string]: DataCatalogEntry.OptimizerPopularity | number;
+  };
+  getChildren(
+    options: DataCatalogEntry.CatalogGetOptions
+  ): CancellableJqPromise<DataCatalogEntry[]>;
+  getQualifiedPath(): string;
+  getSourceMeta(
+    options: DataCatalogEntry.CatalogGetOptions
+  ): CancellableJqPromise<DataCatalogEntry.SourceMeta>;
+  getType(): string;
+  getTopJoins(
+    options: DataCatalogEntry.CatalogGetOptions
+  ): CancellableJqPromise<DataCatalogEntry.TopJoins>;
+  getTopAggs(
+    options: DataCatalogEntry.CatalogGetOptions
+  ): CancellableJqPromise<DataCatalogEntry.TopAggs>;
+  getTopFilters(
+    options: DataCatalogEntry.CatalogGetOptions
+  ): CancellableJqPromise<DataCatalogEntry.TopFilters>;
+  isArray(): boolean;
+  isComplex(): boolean;
+  isMap(): boolean;
+  isTable(): boolean;
+  isView(): boolean;
+  loadOptimizerPopularityForChildren(
+    options: DataCatalogEntry.CatalogGetOptions
+  ): CancellableJqPromise<DataCatalogEntry[]>;
+}
+
+declare namespace DataCatalogEntry {
+  export interface CatalogGetOptions {
+    silenceErrors?: boolean;
+    cachedOnly?: boolean;
+    refreshCache?: boolean;
+    cancellable?: boolean;
+  }
+
+  export interface OptimizerPopularity {
+    columnCount: number;
+    dbName?: string;
+    tableName?: string;
+    columnName?: string;
+  }
+
+  export interface Field {
+    type: string;
+    name: string;
+  }
+
+  export interface SourceMeta {
+    type?: string;
+    samples?: string[];
+    value?: { fields?: Field[] };
+    item?: { fields?: Field[] };
+    extended_columns?: { type: string; name: string }[];
+  }
+
+  export interface TopJoinValue {
+    totalQueryCount: number;
+    joinType: string;
+    tables: string[];
+    joinCols: { columns: string[] }[];
+    relativePopularity?: number;
+  }
+
+  export interface TopJoins {
+    values: TopJoinValue[];
+  }
+
+  export interface TopAggValue {
+    aggregateClause: string;
+    aggregateFunction: string;
+    aggregateInfo: {
+      columnName: string;
+      databaseName: string;
+      tableName: string;
+    }[];
+    function?: UdfDetails;
+    relativePopularity?: number;
+    totalQueryCount: number;
+  }
+
+  export interface TopAggs {
+    values: TopAggValue[];
+  }
+
+  export interface TopFilterValue {
+    count: number;
+    relativePopularity?: number;
+    group?: {
+      columnName: string;
+      op: string;
+      literal: string;
+    }[];
+  }
+
+  export interface TopFilters {
+    values: {
+      tableName: string;
+      count: number;
+      popularValues?: TopFilterValue[];
+    }[];
+  }
+}

+ 0 - 19
desktop/core/src/desktop/js/catalog/types.ts

@@ -1,19 +0,0 @@
-// 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 interface CatalogEntry {
-  getType: () => string;
-}

+ 111 - 1
desktop/core/src/desktop/js/parse/types.ts

@@ -20,12 +20,20 @@ import { ParsedSqlStatement } from './sqlStatementsParser';
 
 export interface IdentifierChainEntry {
   name: string;
+  cte?: string;
+  subQuery?: string;
+}
+
+export interface SubQuery {
+  alias: string;
+  columns: ColumnDetails[];
+  subQueries?: SubQuery[];
 }
 
 export interface ParsedTable {
   alias?: string;
   identifierChain: IdentifierChainEntry[];
-  subQuery?: unknown; // TODO: Define
+  subQuery?: SubQuery; // TODO: Define
 }
 
 export interface ParsedLocation {
@@ -72,6 +80,108 @@ export interface StatementDetails {
   followingStatements: ParsedSqlStatement[];
 }
 
+export interface ColumnAliasDetails {
+  name: string;
+  udfRef?: string;
+  types: string[];
+}
+
+export interface ColumnDetails {
+  type: string;
+  alias: string;
+  identifierChain: IdentifierChainEntry[];
+  subQuery?: string;
+}
+
+export interface CommonPopularSuggestion {
+  tables: ParsedTable[];
+  prefix?: string;
+}
+
+export interface AutocompleteParseResult {
+  colRef?: {
+    identifierChain: IdentifierChainEntry[];
+  };
+  commonTableExpressions?: {
+    alias: string;
+    columns: ColumnDetails[];
+  }[];
+  lowerCase: boolean;
+  subQueries: SubQuery[];
+  suggestAggregateFunctions?: {
+    tables: ParsedTable[];
+  };
+  suggestAnalyticFunctions?: boolean;
+  suggestColRefKeywords?: {
+    [type: string]: string[];
+  };
+  suggestColumnAliases?: ColumnAliasDetails[];
+  suggestColumns?: {
+    identifierChain?: IdentifierChainEntry[];
+    source?: string;
+    tables: ParsedTable[];
+    types?: string[];
+    udfRef?: string;
+  };
+  suggestCommonTableExpressions: {
+    name: string;
+    prependFrom: boolean;
+    prependQuestionMark: boolean;
+  }[];
+  suggestDatabases: {
+    appendDot?: boolean;
+    prependFrom?: boolean;
+    prependQuestionMark?: boolean;
+  };
+  suggestFilters?: {
+    tables: ParsedTable[];
+    prefix?: string;
+  };
+  suggestFunctions?: {
+    types: string[];
+    udfRef?: string;
+  };
+  suggestGroupBys?: CommonPopularSuggestion;
+  suggestHdfs?: {
+    path: string;
+  };
+  suggestJoins?: {
+    prependJoin?: boolean;
+    tables: ParsedTable[];
+  };
+  suggestJoinConditions: {
+    prependOn?: boolean;
+    tables: ParsedTable[];
+  };
+  suggestIdentifiers?: {
+    name: string;
+    type: string;
+  }[];
+  suggestKeyValues?: unknown;
+  suggestKeywords?: {
+    value: string;
+    weight: number;
+  }[];
+  suggestOrderBys?: CommonPopularSuggestion;
+  suggestSetOptions?: boolean;
+  suggestTables?: {
+    identifierChain: IdentifierChainEntry[];
+    onlyTables?: boolean;
+    onlyViews?: boolean;
+    prependFrom?: boolean;
+    prependQuestionMark?: boolean;
+  };
+  suggestValues?: {
+    missingEndQuote?: boolean;
+    partialQuote?: boolean;
+  };
+  udfArgument: {
+    name: string;
+    position: number;
+  };
+  useDatabase?: string;
+}
+
 export interface SqlStatementsParser {
   parse(text: string): ParsedSqlStatement;
 }

+ 4 - 4
desktop/core/src/desktop/js/sql/sqlUtils.ts

@@ -21,7 +21,7 @@ import CancellableJqPromise from 'api/cancellableJqPromise';
 import dataCatalog from 'catalog/dataCatalog';
 import { IdentifierChainEntry, ParsedLocation, ParsedTable } from 'parse/types';
 import { isReserved } from 'sql/reference/sqlReferenceRepository';
-import { Suggestion } from 'sql/types';
+import { Suggestion } from 'apps/notebook2/components/aceEditor/autocomplete/AutocompleteResults';
 import { Compute, Connector, Namespace } from 'types/config';
 
 const identifierEquals = (a?: string, b?: string): boolean =>
@@ -65,7 +65,7 @@ const autocompleteFilter = (filter: string, entries: Suggestion[]): Suggestion[]
   });
 };
 
-interface SortOverride {
+export interface SortOverride {
   partitionColumnsFirst?: boolean;
 }
 
@@ -98,8 +98,8 @@ const sortSuggestions = (
         return 1;
       }
     }
-    const aWeight = a.category.weight + (a.weightAdjust || 0);
-    const bWeight = b.category.weight + (b.weightAdjust || 0);
+    const aWeight = (a.category.weight || 0) + (a.weightAdjust || 0);
+    const bWeight = (b.category.weight || 0) + (b.weightAdjust || 0);
     if (typeof aWeight !== 'undefined' && typeof bWeight !== 'undefined' && bWeight !== aWeight) {
       return bWeight - aWeight;
     }

+ 0 - 32
desktop/core/src/desktop/js/sql/types.ts

@@ -1,32 +0,0 @@
-// 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 interface Suggestion {
-  value: string;
-  filterValue?: string;
-  filterWeight?: number;
-  details?: {
-    comment?: string;
-  };
-  matchComment?: boolean;
-  matchIndex?: number;
-  matchLength?: number;
-  category: {
-    weight: number;
-  };
-  weightAdjust?: number;
-  partitionKey?: boolean;
-}

+ 2 - 0
desktop/core/src/desktop/js/types/types.ts

@@ -31,6 +31,8 @@ declare global {
 }
 
 export interface hueWindow {
+  HAS_OPTIMIZER?: boolean;
+  AUTOCOMPLETE_TIMEOUT?: number;
   ENABLE_SQL_SYNTAX_CHECK?: boolean;
   LOGGED_USERNAME?: string;
   WEB_SOCKETS_ENABLED?: boolean;