forked from pjungwir/aggs_for_vecs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add vec_sub(l,r) to help with array-based subtraction between columns.
- Loading branch information
Showing
4 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
load test_helper | ||
|
||
@test "int16 sub" { | ||
result="$(query "SELECT vec_sub(ARRAY[1,2,3]::smallint[], ARRAY[3,2,1]::smallint[])")"; | ||
echo $result; | ||
[ "$result" = "{-2,0,2}" ] | ||
} | ||
|
||
@test "int32 sub" { | ||
result="$(query "SELECT vec_sub(ARRAY[1,2,3]::integer[], ARRAY[3,2,1]::integer[])")"; | ||
echo $result; | ||
[ "$result" = "{-2,0,2}" ] | ||
} | ||
|
||
@test "int64 sub" { | ||
result="$(query "SELECT vec_sub(ARRAY[1,2,3]::bigint[], ARRAY[3,2,1]::bigint[])")"; | ||
echo $result; | ||
[ "$result" = "{-2,0,2}" ] | ||
} | ||
|
||
@test "float32 sub" { | ||
result="$(query "SELECT vec_sub(ARRAY[1.0,2.2,3.4]::real[], ARRAY[3.4,2.2,1.0]::real[])")"; | ||
echo $result; | ||
[ "$result" = "{-2.4,0,2.4}" ] | ||
} | ||
|
||
@test "float64 sub" { | ||
result="$(query "SELECT vec_sub(ARRAY[1.1,2.2,3.4]::float[], ARRAY[3.4,2.2,1.1]::float[])")"; | ||
echo $result; | ||
[ "$result" = "{-2.3,0,2.3}" ] | ||
} | ||
|
||
@test "numeric sub measurements" { | ||
result="$(query "SELECT diff FROM (SELECT vec_sub(nums, lag(nums) OVER (ORDER BY sensor_id)) AS diff FROM measurements d WHERE sensor_id IN (3,4)) d WHERE d.diff IS NOT NULL")"; | ||
echo $result; | ||
[ "$result" = "{0.00,NULL,-1.11}" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
|
||
Datum vec_sub(PG_FUNCTION_ARGS); | ||
PG_FUNCTION_INFO_V1(vec_sub); | ||
|
||
/** | ||
* Subtract elements of two arrays. | ||
* | ||
* This function takes two array of n-elements and returns an array of | ||
* n-elements where each element is difference between the input elements | ||
* of the same index. | ||
*/ | ||
Datum | ||
vec_sub(PG_FUNCTION_ARGS) | ||
{ | ||
Oid elemTypeId; | ||
int16 elemTypeWidth; | ||
bool elemTypeByValue; | ||
char elemTypeAlignmentCode; | ||
int valsLength; | ||
ArrayType *leftArray, *rightArray, *retArray; | ||
Datum *leftContent, *rightContent, *retContent; | ||
bool *leftNulls, *rightNulls, *retNulls; | ||
int i; | ||
int dims[1]; | ||
int lbs[1]; | ||
|
||
if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) { | ||
PG_RETURN_NULL(); | ||
} | ||
|
||
leftArray = PG_GETARG_ARRAYTYPE_P(0); | ||
rightArray = PG_GETARG_ARRAYTYPE_P(1); | ||
|
||
if (ARR_NDIM(leftArray) == 0 || ARR_NDIM(rightArray) == 0) { | ||
PG_RETURN_NULL(); | ||
} | ||
if (ARR_NDIM(leftArray) > 1 || ARR_NDIM(rightArray) > 1) { | ||
ereport(ERROR, (errmsg("vec_sub: one-dimensional arrays are required"))); | ||
} | ||
|
||
elemTypeId = ARR_ELEMTYPE(leftArray); | ||
|
||
if (elemTypeId != INT2OID && | ||
elemTypeId != INT4OID && | ||
elemTypeId != INT8OID && | ||
elemTypeId != FLOAT4OID && | ||
elemTypeId != FLOAT8OID && | ||
elemTypeId != NUMERICOID) { | ||
ereport(ERROR, (errmsg("vec_sub input must be array of SMALLINT, INTEGER, BIGINT, REAL, DOUBLE PRECISION, or NUMERIC"))); | ||
} | ||
if (rightArray && elemTypeId != ARR_ELEMTYPE(rightArray)) { | ||
ereport(ERROR, (errmsg("vec_sub mins array must be the same type as input array"))); | ||
} | ||
|
||
valsLength = (ARR_DIMS(leftArray))[0]; | ||
if (rightArray && valsLength != (ARR_DIMS(rightArray))[0]) { | ||
ereport(ERROR, (errmsg("vec_sub right array must be the same length as left array"))); | ||
} | ||
|
||
get_typlenbyvalalign(elemTypeId, &elemTypeWidth, &elemTypeByValue, &elemTypeAlignmentCode); | ||
|
||
deconstruct_array(leftArray, elemTypeId, elemTypeWidth, elemTypeByValue, elemTypeAlignmentCode, | ||
&leftContent, &leftNulls, &valsLength); | ||
if (rightArray) { | ||
deconstruct_array(rightArray, elemTypeId, elemTypeWidth, elemTypeByValue, elemTypeAlignmentCode, | ||
&rightContent, &rightNulls, &valsLength); | ||
} | ||
|
||
retContent = palloc0(sizeof(Datum) * valsLength); | ||
retNulls = palloc0(sizeof(bool) * valsLength); | ||
|
||
for (i = 0; i < valsLength; i++) { | ||
if (leftNulls[i] || rightNulls[i]) { | ||
retNulls[i] = true; | ||
continue; | ||
} | ||
retNulls[i] = false; | ||
switch(elemTypeId) { | ||
case INT2OID: | ||
retContent[i] = Int16GetDatum(DatumGetInt16(leftContent[i]) - DatumGetInt16(rightContent[i])); | ||
break; | ||
case INT4OID: | ||
retContent[i] = Int32GetDatum(DatumGetInt32(leftContent[i]) - DatumGetInt32(rightContent[i])); | ||
break; | ||
case INT8OID: | ||
retContent[i] = Int64GetDatum(DatumGetInt64(leftContent[i]) - DatumGetInt64(rightContent[i])); | ||
break; | ||
case FLOAT4OID: | ||
retContent[i] = Float4GetDatum(DatumGetFloat4(leftContent[i]) - DatumGetFloat4(rightContent[i])); | ||
break; | ||
case FLOAT8OID: | ||
retContent[i] = Float8GetDatum(DatumGetFloat8(leftContent[i]) - DatumGetFloat8(rightContent[i])); | ||
break; | ||
case NUMERICOID: | ||
retContent[i] = DirectFunctionCall2(numeric_sub, leftContent[i], rightContent[i]); | ||
break; | ||
} | ||
} | ||
|
||
dims[0] = valsLength; | ||
lbs[0] = 1; | ||
|
||
retArray = construct_md_array(retContent, retNulls, 1, dims, lbs, | ||
elemTypeId, elemTypeWidth, elemTypeByValue, elemTypeAlignmentCode); | ||
|
||
PG_RETURN_ARRAYTYPE_P(retArray); | ||
} | ||
|