Skip to content

Commit

Permalink
New promise-based evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
grassick committed Dec 16, 2019
1 parent 3903e3b commit 6dc1b56
Show file tree
Hide file tree
Showing 15 changed files with 2,377 additions and 142 deletions.
3 changes: 1 addition & 2 deletions lib/ExprEvaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1357,8 +1357,7 @@ function () {
}, {
key: "evaluateVariable",
value: function evaluateVariable(expr, context, callback) {
var value, variable;
console.log(this.variables); // Get variable
var value, variable; // Get variable

variable = _.findWhere(this.variables, {
id: expr.variableId
Expand Down
39 changes: 39 additions & 0 deletions lib/ExprUtils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Schema from "./Schema";
import { Variable, AggrStatus, Expr, LocalizedString, FieldExpr, EnumValue } from "./types";

export default class ExprUtils {
constructor(schema: Schema, variables?: Variable[])

summarizeExpr(expr: Expr, locale?: string): string

getExprType(expr: Expr): string | null

getExprAggrStatus(expr: Expr): AggrStatus | null

getExprEnumValues(expr: Expr): EnumValue[] | null

/** Gets the id table of an expression of type id */
getExprIdTable(expr: Expr): string | null

/** Converts a literal value related to an expression to a string, using name of enums. preferEnumCodes tries to use code over name */
stringifyExprLiteral(expr: Expr, literal: any, locale?: string, preferEnumCodes?: boolean): string

/** Localize a localized string */
static localizeString(str?: LocalizedString | null, locale?: string): string

/** Localize a localized string */
localizeString(str?: LocalizedString | null, locale?: string): string

/** Get a list of fields that are referenced in a an expression
* Useful to know which fields and joins are used. Includes joins as fields */
getReferencedFields(expr: Expr): FieldExpr[]

/** Replace variables with literal values */
inlineVariableValues(expr: Expr, variableValues: { [variableId: string]: any }): Expr

/** Determine if op is aggregate */
static isOpAggr(op: string): boolean

/** Determine if op is prefix */
static isOpPrefix(op: string): boolean
}
36 changes: 31 additions & 5 deletions lib/PromiseExprEvaluator.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import ExprEvaluator from "./ExprEvaluator";
import { Expr } from "./types";
import { Expr, Variable, CaseExpr, ScalarExpr, VariableExpr, ScoreExpr, BuildEnumsetExpr } from "./types";
import Schema from "./Schema";
/** Represents a row to be evaluated */
export interface PromiseExprEvaluatorRow {
/** gets primary key of row */
getPrimaryKey(): Promise<any>;
/** gets the value of a column.
* For joins, getField will get array of rows for 1-n and n-n joins and a row for n-1 and 1-1 joins
* For joins, getField will get array of primary keys for 1-n and n-n joins and a primary key (or null) for n-1 and 1-1 joins
*/
getField(columnId: string): Promise<any>;
/** Get row, rows, or null of a join */
followJoin(columnId: string): Promise<PromiseExprEvaluatorRow[] | PromiseExprEvaluatorRow | null>;
}
export interface PromiseExprEvaluatorContext {
/** current row. Optional for aggr expressions */
Expand All @@ -17,7 +19,31 @@ export interface PromiseExprEvaluatorContext {
}
/** Expression evaluator that is promise-based */
export declare class PromiseExprEvaluator {
private exprEvaluator;
constructor(exprEvaluator: ExprEvaluator);
schema?: Schema;
locale?: string;
variables?: Variable[];
variableValues?: {
[variableId: string]: any;
};
constructor(options: {
schema?: Schema;
locale?: string;
variables?: Variable[];
variableValues?: {
[variableId: string]: any;
};
});
/** Evaluate an expression given the context */
evaluate(expr: Expr, context: PromiseExprEvaluatorContext): Promise<any>;
evaluateBuildEnumset(expr: BuildEnumsetExpr, context: PromiseExprEvaluatorContext): Promise<any>;
evaluateScore(expr: ScoreExpr, context: PromiseExprEvaluatorContext): Promise<any>;
evaluateCase(expr: CaseExpr, context: PromiseExprEvaluatorContext): Promise<any>;
evaluateScalar(expr: ScalarExpr, context: PromiseExprEvaluatorContext): Promise<any>;
evaluateOp(table: string, op: string, exprs: Expr[], context: PromiseExprEvaluatorContext): Promise<any>;
/** NOTE: This is not technically correct. It's not a window function (as window
* functions can't be used in where clauses) but rather a special query */
evaluateIsLatest(table: string, exprs: Expr[], context: PromiseExprEvaluatorContext): Promise<boolean | null>;
evaluteAggrOp(table: string, op: string, exprs: Expr[], context: PromiseExprEvaluatorContext): Promise<any>;
evaluateOpValues(op: string, exprs: Expr[], values: any[]): any;
evaluateVariable(expr: VariableExpr, context: PromiseExprEvaluatorContext): Promise<any>;
}
Loading

0 comments on commit 6dc1b56

Please sign in to comment.