Skip to content

Commit

Permalink
#1306 - Can't negate analytic value
Browse files Browse the repository at this point in the history
  • Loading branch information
tanclary committed Sep 12, 2023
1 parent e0b8096 commit dabd2c3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 2 additions & 2 deletions developing.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Some of the Postgres tests depend on static tables (i.e. `test/src/nomodel.spec.

### VS Code tips

We provide a task in VS Code (.vscode/tasks.json) to watch the entire Malloy repo for typscript changes - this allows VS Code to output typescript errors even when files are closed. The default behavior is for errors to only appear in open files. If you want the watcher task to compile all files in the background, you can either run the task manually (Command Palette -> Tasks -> Run Task -> tsc-compile-watch). If you want to enable this task to always start when you open the project, run Command Palette -> Tasks: Manage Automatic Tasks in Folder -> Allow Automatic Tasks in folder.
We provide a task in VS Code (.vscode/tasks.json) to watch the entire Malloy repo for typescript changes - this allows VS Code to output typescript errors even when files are closed. The default behavior is for errors to only appear in open files. If you want the watcher task to compile all files in the background, you can either run the task manually (Command Palette -> Tasks -> Run Task -> tsc-compile-watch). If you want to enable this task to always start when you open the project, run Command Palette -> Tasks: Manage Automatic Tasks in Folder -> Allow Automatic Tasks in folder.

## Running Tests

See `test/README.md` for infomration about running tests.
See `test/README.md` for information about running tests.

## Malloy VSCode Extension

Expand Down
2 changes: 1 addition & 1 deletion packages/malloy/src/lang/ast/expressions/expr-minus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ExprMinus extends ExpressionDef {

getExpression(fs: FieldSpace): ExprValue {
const expr = this.expr.getExpression(fs);
if (this.typeCheck(this.expr, expr)) {
if (FT.typeIn(expr, this.legalChildTypes)) {
if (expr.value.length > 1) {
return {...expr, dataType: 'number', value: ['-(', ...expr.value, ')']};
}
Expand Down
13 changes: 13 additions & 0 deletions packages/malloy/src/lang/ast/fragtype-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ export class FT {
);
}

/**
* Checks if a given type is in a list, ignoring aggregate
* @param check The type to check (can be undefined)
* @param from List of types which are OK
*/
static typeIn(check: TypeDesc | undefined, from: TypeDesc[]): boolean {
if (check) {
const found = from.find(okType => FT.typeEq(okType, check));
return found !== undefined;
}
return false;
}

/**
* Checks if the base types, ignoring aggregate, are equal
* @param left Left type
Expand Down
16 changes: 16 additions & 0 deletions test/src/databases/all/functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,22 @@ expressionModels.forEach((expressionModel, databaseName) => {
expect(result.data.path(2, 'r').value).toBe(3);
expect(result.data.path(3, 'r').value).toBe(3);
});

it(`works using unary minus in calculate block - ${databaseName}`, async () => {
const result = await expressionModel
.loadQuery(
`query: state_facts -> {
group_by: first_letter is substr(state, 1, 1)
aggregate: states_with_first_letter_ish is round(count() / 2) * 2
calculate: r is rank() neg_r is - r
}`
)
.run();
expect(result.data.path(0, 'neg_r').value).toBe(-1);
expect(result.data.path(1, 'neg_r').value).toBe(-1);
expect(result.data.path(2, 'neg_r').value).toBe(-3);
expect(result.data.path(3, 'neg_r').value).toBe(-3);
});
});

describe('lag', () => {
Expand Down

0 comments on commit dabd2c3

Please sign in to comment.