Skip to content

Commit ae4a4da

Browse files
committed
Fixed sub(), added more doc
1 parent f6241e9 commit ae4a4da

File tree

9 files changed

+43
-177
lines changed

9 files changed

+43
-177
lines changed

dist/src/main/v3/expr-library/operator/arithmetic/bigint/sub.js

Lines changed: 3 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/main/v3/expr-library/operator/arithmetic/bigint/sub.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/main/v3/expr-library/operator/arithmetic/sub.js

Lines changed: 3 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/main/v3/expr-library/operator/arithmetic/sub.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/expressions/README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,33 @@ Asterisks (*) denote expressions not natively from MySQL.
5656
+ [`utcTimestamp(0|1|2|3)`](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_utc-timestamp)
5757

5858
+ Information
59-
+ DATABASE
60-
+ FOUND_ROWS
59+
+ [`database()`](https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_database)
60+
+ [`foundRows()`](https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_found-rows)
6161

6262
+ Math
63-
+ CEIL
64-
+ FLOOR
65-
+ RAND
66-
+ ROUND
63+
+ [`ceil(number)`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_ceil)
64+
+ [`floor(number)`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_floor)
65+
+ [`rand()`/`rand(bigint)`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_rand)
66+
+ [`round(number)`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_round)
6767

6868
+ String
69-
+ ASCII
70-
+ BIN
71-
+ BIT_LENGTH
72-
+ CHAR
73-
+ CHAR_LENGTH
74-
+ CONCAT
75-
+ CONCAT_WS
76-
+ ELT
77-
+ EXPORT_SET
78-
+ FIELD
79-
+ FIND_IN_SET
80-
+ FORMAT
81-
+ FROM_BASE64
82-
+ HEX
69+
+ [`ascii(string)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_ascii)
70+
+ [`bin(bigint|number`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_bin)
71+
+ [`bitLength(string)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_bit-length)
72+
+ [`char(number, ...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_char)
73+
+ [`charLength(string)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_char-length)
74+
+ [`concat(string, ...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat)
75+
+ [`concatWs(string, string|null, ...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws)
76+
+ [`elt(number, string, ...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_elt)
77+
+ [`exportSet(number, string, string)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_export-set)
78+
+ [`field(string, string, ...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_field)
79+
+ [`findInSet(string, string)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set)
80+
+ [`format(number, number)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_format)
81+
+ [`fromBase64(string)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_from-base64)
82+
+ [`hex(bigint|number|string)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_hex)
8383

8484
+ Subquery
85-
+ EXISTS
85+
+ [`exists(query)`](https://dev.mysql.com/doc/refman/8.0/en/exists-and-not-exists-subqueries.html)
8686

8787
+ Arithmetic
8888
+ ADD

src/main/v3/expr-library/operator/arithmetic/bigint/sub.ts

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
import * as sd from "type-mapping";
22
import {RawExpr, RawExprUtil} from "../../../../raw-expr";
3-
import {Expr, ExprUtil} from "../../../../expr";
4-
import {Parentheses, QueryTreeArray} from "../../../../query-tree";
3+
import {Expr} from "../../../../expr";
4+
import {QueryTreeArray} from "../../../../query-tree";
55
import * as dataType from "../../../../data-type";
66
import {castAsSignedInteger} from "../../../function";
77

88
//https://dev.mysql.com/doc/refman/8.0/en/arithmetic-functions.html#operator_minus
9-
function tryGetSubQueryTree (rawExpr : RawExpr<bigint>) : QueryTreeArray|undefined {
10-
if (ExprUtil.isExpr(rawExpr)) {
11-
if (rawExpr.queryTree instanceof Parentheses) {
12-
const tree = rawExpr.queryTree.getTree();
13-
if (tree instanceof Array) {
14-
if (tree.length == 0) {
15-
//This shouldn't happen, in general...
16-
return [];
17-
}
18-
for (let i=1; i<tree.length; i+=2) {
19-
if (tree[i] !== "-") {
20-
return undefined;
21-
}
22-
}
23-
return tree;
24-
}
25-
}
26-
}
27-
return undefined;
28-
}
299
export function bigIntSub<ArrT extends RawExpr<bigint>[]> (
3010
...arr : ArrT
3111
) : (
@@ -38,22 +18,10 @@ export function bigIntSub<ArrT extends RawExpr<bigint>[]> (
3818
const queryTree : QueryTreeArray = [];
3919

4020
for (let rawExpr of arr) {
41-
const subQueryTree = tryGetSubQueryTree(rawExpr);
42-
if (subQueryTree != undefined) {
43-
if (subQueryTree.length == 0) {
44-
continue;
45-
} else {
46-
if (queryTree.length > 0) {
47-
queryTree.push("-");
48-
}
49-
queryTree.push(...subQueryTree);
50-
}
51-
} else {
52-
if (queryTree.length > 0) {
53-
queryTree.push("-");
54-
}
55-
queryTree.push(RawExprUtil.queryTree(rawExpr));
21+
if (queryTree.length > 0) {
22+
queryTree.push("-");
5623
}
24+
queryTree.push(RawExprUtil.queryTree(rawExpr));
5725
}
5826
if (queryTree.length == 0) {
5927
//By convention, the subtraction of zero numbers is zero

src/main/v3/expr-library/operator/arithmetic/sub.ts

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
import * as sd from "type-mapping";
22
import {RawExpr, RawExprUtil} from "../../../raw-expr";
3-
import {Expr, ExprUtil} from "../../../expr";
3+
import {Expr} from "../../../expr";
44
import {Tuple} from "../../../tuple";
5-
import {Parentheses, QueryTreeArray} from "../../../query-tree";
5+
import {QueryTreeArray} from "../../../query-tree";
66
import * as dataType from "../../../data-type";
77

88
//https://dev.mysql.com/doc/refman/8.0/en/arithmetic-functions.html#operator_minus
9-
function tryGetSubQueryTree (rawExpr : RawExpr<number>) : QueryTreeArray|undefined {
10-
if (ExprUtil.isExpr(rawExpr)) {
11-
if (rawExpr.queryTree instanceof Parentheses) {
12-
const tree = rawExpr.queryTree.getTree();
13-
if (tree instanceof Array) {
14-
if (tree.length == 0) {
15-
//This shouldn't happen, in general...
16-
return [];
17-
}
18-
for (let i=1; i<tree.length; i+=2) {
19-
if (tree[i] !== "-") {
20-
return undefined;
21-
}
22-
}
23-
return tree;
24-
}
25-
}
26-
}
27-
return undefined;
28-
}
299
export function sub<ArrT extends Tuple<RawExpr<number>>> (
3010
...arr : ArrT
3111
) : (
@@ -38,22 +18,10 @@ export function sub<ArrT extends Tuple<RawExpr<number>>> (
3818
const queryTree : QueryTreeArray = [];
3919

4020
for (let rawExpr of arr) {
41-
const subQueryTree = tryGetSubQueryTree(rawExpr);
42-
if (subQueryTree != undefined) {
43-
if (subQueryTree.length == 0) {
44-
continue;
45-
} else {
46-
if (queryTree.length > 0) {
47-
queryTree.push("-");
48-
}
49-
queryTree.push(...subQueryTree);
50-
}
51-
} else {
52-
if (queryTree.length > 0) {
53-
queryTree.push("-");
54-
}
55-
queryTree.push(RawExprUtil.queryTree(rawExpr));
21+
if (queryTree.length > 0) {
22+
queryTree.push("-");
5623
}
24+
queryTree.push(RawExprUtil.queryTree(rawExpr));
5725
}
5826
if (queryTree.length == 0) {
5927
//By convention, the subtraction of zero numbers is zero

test/run-time/input/expr-library/operator/arithmetic/bigint/sub.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ tape(__filename, (t) => {
4545
new o.SqlFormatter().format(
4646
o.QueryTreeUtil.toSql(bigIntSub3.queryTree)
4747
),
48-
"(`table`.`x` - `table`.`y` - `table`.`a`)"
48+
"((`table`.`x` - `table`.`y`) - `table`.`a`)"
4949
);
5050

5151
const bigIntSub4 = o.bigIntSub(
@@ -56,7 +56,7 @@ tape(__filename, (t) => {
5656
new o.SqlFormatter().format(
5757
o.QueryTreeUtil.toSql(bigIntSub4.queryTree)
5858
),
59-
"(\n `table`.`x` - `table`.`y` - `table`.`a` - `table`.`b`\n)"
59+
"(\n ((`table`.`x` - `table`.`y`) - `table`.`a`) - `table`.`b`\n)"
6060
);
6161

6262
const bigIntSub5 = o.bigIntSub(

test/run-time/input/expr-library/operator/arithmetic/sub.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ tape(__filename, (t) => {
4646
new o.SqlFormatter().format(
4747
o.QueryTreeUtil.toSql(sub3.queryTree)
4848
),
49-
"(`table`.`x` - `table`.`y` - `table`.`a`)"
49+
"((`table`.`x` - `table`.`y`) - `table`.`a`)"
5050
);
5151

5252
const sub4 = o.sub(
@@ -57,7 +57,7 @@ tape(__filename, (t) => {
5757
new o.SqlFormatter().format(
5858
o.QueryTreeUtil.toSql(sub4.queryTree)
5959
),
60-
"(\n `table`.`x` - `table`.`y` - `table`.`a` - `table`.`b`\n)"
60+
"(\n ((`table`.`x` - `table`.`y`) - `table`.`a`) - `table`.`b`\n)"
6161
);
6262

6363
const sub5 = o.sub(

0 commit comments

Comments
 (0)