Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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,
it("should find dimension by expression", () => {
const dimension = dimensions.getDimensionByExpression(Expression.fromJSLoose("$time"));
expect(dimension.toJS()).to.deep.equal(DimensionFixtures.wikiTimeJS());
});
it("should find measure by expression", () => {
const measure = measures.getMeasureByExpression(Expression.fromJSLoose("$main.sum($count)"));
expect(measure.toJS()).to.deep.equal(MeasureFixtures.wikiCountJS());
});
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");
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);
}
}
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)];
}
}