How to use the plywood.Expression.fromJSLoose function in plywood

To help you get started, we’ve selected a few plywood examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github implydata / plyql / src / plyql-json-server.ts View on Github external
createJSONServer(port, (parameters: JSONParameters, res: any) => {
    let { sql, expression } = parameters;

    let resultPromise: Promise;

    if (expression) {
      let ex: Expression;
      try {
        ex = Expression.fromJSLoose(expression);
      } catch (e) {
        res.status(400).send({ error: e.message });
        return;
      }

      resultPromise = executePlywood(ex, context, timezone);
    } else {
      let sqlParse: SQLParse;
      try {
        sqlParse = Expression.parseSQL(sql);
      } catch (e) {
        res.status(400).send({ error: e.message });
        return;
      }

      if (sqlParse.verb && sqlParse.verb !== 'SELECT') { // DESCRIBE + SHOW get re-written
github allegro / turnilo / src / server / routes / query / query.ts View on Github external
function makeExternal(dataSource: any): External {
  var attributes: AttributeJSs = {};

  // Right here we have the classic mega hack.
  for (var dimension of dataSource.dimensions) {
    attributes[dimension.name] = { type: dimension.type || 'STRING' };
  }

  for (var measure of dataSource.measures) {
    var expression = measure.expression ? Expression.fromJSLoose(measure.expression) : $(measure.name);
    var freeReferences = expression.getFreeReferences();
    for (var freeReference of freeReferences) {
      if (freeReference === 'main') continue;
      if (JSON.stringify(expression).indexOf('countDistinct') !== -1) {
        attributes[freeReference] = { special: 'unique' };
      } else {
        attributes[freeReference] = { type: 'NUMBER' };
      }
    }
  }
  // Mega hack ends here

  return External.fromJS({
    engine: 'druid',
    dataSource: dataSource.source,
    timeAttribute: dataSource.timeAttribute,
github allegro / turnilo / src / common / models / dimension / dimensions.mocha.ts View on Github external
it("should find dimension by expression", () => {
    const dimension = dimensions.getDimensionByExpression(Expression.fromJSLoose("$time"));

    expect(dimension.toJS()).to.deep.equal(DimensionFixtures.wikiTimeJS());
  });
github allegro / turnilo / src / common / models / measure / measures.mocha.ts View on Github external
it("should find measure by expression", () => {
    const measure = measures.getMeasureByExpression(Expression.fromJSLoose("$main.sum($count)"));

    expect(measure.toJS()).to.deep.equal(MeasureFixtures.wikiCountJS());
  });
github allegro / turnilo / src / common / models / data-cube / data-cube.ts View on Github external
function measuresFromLongForm(longForm: LongForm): Measure[] {
  const { metricColumn, measures, possibleAggregates } = longForm;
  let myPossibleAggregates: Record = {};
  for (let agg in possibleAggregates) {
    if (!hasOwnProperty(possibleAggregates, agg)) continue;
    myPossibleAggregates[agg] = Expression.fromJSLoose(possibleAggregates[agg]);
  }

  return measures.map(measure => {
    if (hasOwnProperty(measure, "name")) {
      return Measure.fromJS(measure as MeasureJS);
    }

    const title = measure.title;
    if (!title) {
      throw new Error("must have title in longForm value");
    }

    const value = (measure as LongFormMeasure).value;
    const aggregate = (measure as LongFormMeasure).aggregate;
    if (!aggregate) {
      throw new Error("must have aggregates in longForm value");
github allegro / turnilo / src / common / models / split-combine / split-combine.ts View on Github external
static fromJS(parameters: SplitCombineJS, context?: SplitCombineContext): SplitCombine {
    if (typeof parameters === "string") {
      if (!context) throw new Error("must have context for string split");
      const dimension = context.dimensions.getDimensionByName(parameters);
      if (!dimension) throw new Error(`can not find dimension ${parameters}`);
      return new SplitCombine({
        expression: dimension.expression,
        bucketAction: null,
        sortAction: null,
        limitAction: null
      });
    } else {
      const value: SplitCombineValue = {
        expression: Expression.fromJSLoose(parameters.expression),
        bucketAction: null,
        sortAction: null,
        limitAction: null
      };

      if (parameters.bucketAction) value.bucketAction = Expression.fromJS(parameters.bucketAction);
      if (parameters.sortAction) value.sortAction = SortExpression.fromJS(parameters.sortAction);
      if (parameters.limitAction) value.limitAction = LimitExpression.fromJS(parameters.limitAction);
      return new SplitCombine(value);
    }
  }
github allegro / turnilo / src / common / view-definitions / version-2 / view-definition-converter-2.ts View on Github external
function filterJSConverter(filter: any, dataCube: DataCube): FilterClause[] {
  const filterExpression = Expression.fromJSLoose(filter);
  if (filterExpression instanceof LiteralExpression && filterExpression.simple) return [];
  if (filterExpression instanceof AndExpression) {
    return filterExpression.getExpressionList().map(exp => convertFilterExpression(exp as ChainableUnaryExpression, dataCube));
  } else {
    return [convertFilterExpression(filterExpression as ChainableUnaryExpression, dataCube)];
  }
}