Skip to content

Commit

Permalink
Add atan2 binop support (#117)
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Volz <julius.volz@gmail.com>
  • Loading branch information
juliusv authored Aug 9, 2023
1 parent 652e9c1 commit 0d65c96
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const scalarBinOp = (op: binaryOperatorType, lhs: number, rhs: number): n
return Number(lhs >= rhs);
case '<=':
return Number(lhs <= rhs);
case 'atan2':
return Math.atan2(lhs, rhs);
default:
throw new Error('invalid binop');
}
Expand Down Expand Up @@ -57,6 +59,8 @@ export const vectorElemBinop = (op: binaryOperatorType, lhs: number, rhs: number
return { value: lhs, keep: lhs >= rhs };
case '<=':
return { value: lhs, keep: lhs <= rhs };
case 'atan2':
return { value: Math.atan2(lhs, rhs), keep: true };
default:
throw new Error('invalid binop');
}
Expand Down
1 change: 1 addition & 0 deletions app/src/promql/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export enum binaryOperatorType {
and = 'and',
or = 'or',
unless = 'unless',
atan2 = 'atan2',
}

export const compOperatorTypes: binaryOperatorType[] = [
Expand Down
2 changes: 1 addition & 1 deletion app/src/promql/format.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ const formatNodeInternal = (node: ASTNode, showChildren: boolean, maxDepth?: num
return (
<>
{showChildren && formatNode(maybeParenthesizeBinopChild(node.op, node.lhs), showChildren, childMaxDepth)}{' '}
{['and', 'or', 'unless'].includes(node.op) ? (
{['atan2', 'and', 'or', 'unless'].includes(node.op) ? (
<span className="promql-keyword">{node.op}</span>
) : (
<span className="promql-operator">{node.op}</span>
Expand Down
1 change: 1 addition & 0 deletions app/src/promql/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const binOpPrecedence = {
[binaryOperatorType.and]: 5,
[binaryOperatorType.or]: 6,
[binaryOperatorType.unless]: 5,
[binaryOperatorType.atan2]: 2,
};

export const maybeParenthesizeBinopChild = (op: binaryOperatorType, child: ASTNode): ASTNode => {
Expand Down

0 comments on commit 0d65c96

Please sign in to comment.