Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DECIMAL trigonometric functions removed because postgresql does not h… #261

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/expr-library/decimal/fractional-div.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Decimal} from "../../decimal";
import {makeOperator2} from "../factory";

/**
* Performs regular floating-point division
* Performs regular fixed-point division
*/
export const fractionalDiv = makeOperator2<OperatorType.FRACTIONAL_DIVISION, Decimal, Decimal>(
OperatorType.FRACTIONAL_DIVISION,
Expand Down
28 changes: 14 additions & 14 deletions src/expr-library/decimal/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
export * from "./aggregate";

export * from "./abs";
export * from "./acos";
//export * from "./acos";
export * from "./add";
export * from "./asin";
export * from "./atan";
export * from "./atan2";
export * from "./cbrt";
//export * from "./asin";
//export * from "./atan";
//export * from "./atan2";
//export * from "./cbrt";
export * from "./ceiling";
export * from "./cos";
export * from "./cot";
export * from "./degrees";
//export * from "./cos";
//export * from "./cot";
//export * from "./degrees";
export * from "./exp";
export * from "./floor";
export * from "./fractional-div";
/**
* PostgreSQL and SQLite do not support fractional remainder `frem(x, y)`
*/
//export * from "./fractional-remainder";
export * from "./integer-div";
export * from "./integer-remainder";
//export * from "./integer-div";
//export * from "./integer-remainder";
export * from "./ln";
export * from "./log";
export * from "./log2";
export * from "./log10";
export * from "./mul";
export * from "./neg";
export * from "./pi";
//export * from "./pi";
export * from "./power";
export * from "./radians";
//export * from "./radians";
export * from "./random";
//export * from "./round";
export * from "./sign";
export * from "./sin";
//export * from "./sin";
export * from "./sqrt";
export * from "./sub";
export * from "./tan";
//export * from "./tan";
//export * from "./truncate";
67 changes: 62 additions & 5 deletions src/expr-library/double/aggregate/avg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,33 @@ import {makeAggregateOperator2, AggregateOperator1} from "../../aggregate-factor
import {BuiltInExpr_NonAggregate} from "../../../built-in-expr";
import {ExprUtil} from "../../../expr";

const avgImpl = makeAggregateOperator2<OperatorType.AGGREGATE_AVERAGE, boolean, number, number|null>(
const avgImpl = makeAggregateOperator2<OperatorType.AGGREGATE_AVERAGE, boolean, number|null, number|null>(
OperatorType.AGGREGATE_AVERAGE,
tm.mysql.double().orNull(),
TypeHint.DOUBLE
);

export const avgDistinct : AggregateOperator1<number, number|null> = <
ArgT extends BuiltInExpr_NonAggregate<number>
/**
* Returns the average value of non-`NULL` values from a group.
*
* It returns `NULL` if there are no non-`NULL` values.
*
* + https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_avg
* + https://www.postgresql.org/docs/9.2/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
* + https://www.sqlite.org/lang_aggfunc.html#avg
*
* -----
*
* + MySQL : `AVG(DISTINCT x)`
* + PostgreSQL : `AVG(DISTINCT x)`
* + SQLite : `AVG(DISTINCT x)`
*
* -----
*
* No guarantees are made about the precision of the return type.
*/
export const avgDistinct : AggregateOperator1<number|null, number|null> = <
ArgT extends BuiltInExpr_NonAggregate<number|null>
>(
arg : ArgT
) : (
Expand All @@ -21,8 +40,27 @@ export const avgDistinct : AggregateOperator1<number, number|null> = <
return avgImpl(true, arg) as ExprUtil.AggregateIntersect<number|null, ArgT>;
};

export const avgAll : AggregateOperator1<number, number|null> = <
ArgT extends BuiltInExpr_NonAggregate<number>
/**
* Returns the average value of non-`NULL` values from a group.
*
* It returns `NULL` if there are no non-`NULL` values.
*
* + https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_avg
* + https://www.postgresql.org/docs/9.2/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
* + https://www.sqlite.org/lang_aggfunc.html#avg
*
* -----
*
* + MySQL : `AVG(x)`
* + PostgreSQL : `AVG(x)`
* + SQLite : `AVG(x)`
*
* -----
*
* No guarantees are made about the precision of the return type.
*/
export const avgAll : AggregateOperator1<number|null, number|null> = <
ArgT extends BuiltInExpr_NonAggregate<number|null>
>(
arg : ArgT
) : (
Expand All @@ -31,4 +69,23 @@ export const avgAll : AggregateOperator1<number, number|null> = <
return avgImpl(false, arg) as ExprUtil.AggregateIntersect<number|null, ArgT>;
};

/**
* Returns the average value of non-`NULL` values from a group.
*
* It returns `NULL` if there are no non-`NULL` values.
*
* + https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_avg
* + https://www.postgresql.org/docs/9.2/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
* + https://www.sqlite.org/lang_aggfunc.html#avg
*
* -----
*
* + MySQL : `AVG(x)`
* + PostgreSQL : `AVG(x)`
* + SQLite : `AVG(x)`
*
* -----
*
* No guarantees are made about the precision of the return type.
*/
export const avg = avgAll;
67 changes: 62 additions & 5 deletions src/expr-library/double/aggregate/sum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,33 @@ import {makeAggregateOperator2, AggregateOperator1} from "../../aggregate-factor
import {BuiltInExpr_NonAggregate} from "../../../built-in-expr";
import {ExprUtil} from "../../../expr";

const sumImpl = makeAggregateOperator2<OperatorType.AGGREGATE_SUM, boolean, number, number|null>(
const sumImpl = makeAggregateOperator2<OperatorType.AGGREGATE_SUM, boolean, number|null, number|null>(
OperatorType.AGGREGATE_SUM,
tm.mysql.double().orNull(),
TypeHint.DOUBLE
);

export const sumDistinct : AggregateOperator1<number, number|null> = <
ArgT extends BuiltInExpr_NonAggregate<number>
/**
* Returns the total sum of non-`NULL` values from a group.
*
* It returns `NULL` if there are no non-`NULL` values.
*
* + https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_sum
* + https://www.postgresql.org/docs/9.2/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
* + https://www.sqlite.org/lang_aggfunc.html#sumunc
*
* -----
*
* + MySQL : `SUM(DISTINCT x)`
* + PostgreSQL : `SUM(DISTINCT x)`
* + SQLite : `SUM(DISTINCT x)`
*
* -----
*
* No guarantees are made about the precision of the return type.
*/
export const sumDistinct : AggregateOperator1<number|null, number|null> = <
ArgT extends BuiltInExpr_NonAggregate<number|null>
>(
arg : ArgT
) : (
Expand All @@ -21,8 +40,27 @@ export const sumDistinct : AggregateOperator1<number, number|null> = <
return sumImpl(true, arg) as ExprUtil.AggregateIntersect<number|null, ArgT>;
};

export const sumAll : AggregateOperator1<number, number|null> = <
ArgT extends BuiltInExpr_NonAggregate<number>
/**
* Returns the total sum of non-`NULL` values from a group.
*
* It returns `NULL` if there are no non-`NULL` values.
*
* + https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_sum
* + https://www.postgresql.org/docs/9.2/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
* + https://www.sqlite.org/lang_aggfunc.html#sumunc
*
* -----
*
* + MySQL : `SUM(x)`
* + PostgreSQL : `SUM(x)`
* + SQLite : `SUM(x)`
*
* -----
*
* No guarantees are made about the precision of the return type.
*/
export const sumAll : AggregateOperator1<number|null, number|null> = <
ArgT extends BuiltInExpr_NonAggregate<number|null>
>(
arg : ArgT
) : (
Expand All @@ -31,4 +69,23 @@ export const sumAll : AggregateOperator1<number, number|null> = <
return sumImpl(false, arg) as ExprUtil.AggregateIntersect<number|null, ArgT>;
};

/**
* Returns the total sum of non-`NULL` values from a group.
*
* It returns `NULL` if there are no non-`NULL` values.
*
* + https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_sum
* + https://www.postgresql.org/docs/9.2/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
* + https://www.sqlite.org/lang_aggfunc.html#sumunc
*
* -----
*
* + MySQL : `SUM(x)`
* + PostgreSQL : `SUM(x)`
* + SQLite : `SUM(x)`
*
* -----
*
* No guarantees are made about the precision of the return type.
*/
export const sum = sumAll;
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {Test} from "../../../../test";
import * as tsql from "../../../../../dist";

export const test : Test = ({tape, pool}) => {
tape(__filename, async (t) => {
await pool.acquire(async (connection) => {
await tsql.selectValue(() => tsql.bigIntSignedLiteral(9001))
.fetchValue(connection)
.then((value) => {
t.deepEqual(value, BigInt(9001));
});

/**
* MAX_SAFE_INTEGER
*/
await tsql.selectValue(() => tsql.bigIntSignedLiteral(Number.MAX_SAFE_INTEGER))
.fetchValue(connection)
.then((value) => {
t.deepEqual(value, BigInt(Number.MAX_SAFE_INTEGER));
});

/**
* -MAX_SAFE_INTEGER
*/
await tsql.selectValue(() => tsql.bigIntSignedLiteral(-Number.MAX_SAFE_INTEGER))
.fetchValue(connection)
.then((value) => {
t.deepEqual(value, BigInt(-Number.MAX_SAFE_INTEGER));
});

t.throws(() => {
/**
* Too small
*/
tsql.selectValue(() => tsql.bigIntSignedLiteral(-1e300));
});

t.throws(() => {
/**
* Too big
*/
tsql.selectValue(() => tsql.bigIntSignedLiteral(1e300));
});

t.throws(() => {
/**
* Fractional
*/
tsql.selectValue(() => tsql.bigIntSignedLiteral(9001.1));
});
});

t.end();
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {Test} from "../../../../test";
import * as tsql from "../../../../../dist";

export const test : Test = ({tape, pool}) => {
tape(__filename, async (t) => {
await pool.acquire(async (connection) => {
await tsql.selectValue(() => tsql.bigIntSignedLiteral("9001"))
.fetchValue(connection)
.then((value) => {
t.deepEqual(value, BigInt(9001));
});

/**
* MAX BIGINT SIGNED VALUE
*/
await tsql.selectValue(() => tsql.bigIntSignedLiteral("9223372036854775807"))
.fetchValue(connection)
.then((value) => {
t.deepEqual(value, BigInt("9223372036854775807"));
});

/**
* MIN BIGINT SIGNED VALUE
*/
await tsql.selectValue(() => tsql.bigIntSignedLiteral("-9223372036854775808"))
.fetchValue(connection)
.then((value) => {
t.deepEqual(value, BigInt("-9223372036854775808"));
});

t.throws(() => {
/**
* Too small
*/
tsql.selectValue(() => tsql.bigIntSignedLiteral("-9223372036854775809"));
});

t.throws(() => {
/**
* Too big
*/
tsql.selectValue(() => tsql.bigIntSignedLiteral("9223372036854775808"));
});
});

t.end();
});
};
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import * as tm from "type-mapping";
import {Test} from "../../../../test";
import * as tsql from "../../../../../dist";

export const test : Test = ({tape, pool}) => {
tape(__filename, async (t) => {
await pool.acquire(async (connection) => {
const arr = [
"-123e0",
"-1.456",
"abcd",
"123",
"123e0",
"1.456",
tm.BigInt(876),
tm.BigInt(-999),
"hello, world",
"case-SENSITIVE",
];
for (const a of arr) {
await tsql.selectValue(() => tsql.unsafeCastAsDouble(a))
await tsql.selectValue(() => tsql.unsafeCastAsBinary(a))
.fetchValue(connection)
.then((value) => {
t.deepEqual(value, Number(a));
t.deepEqual(value, Buffer.from(a));
})
.catch((err) => {
t.fail(err.message);
Expand Down
Loading