-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Add linear_interpolate to math functions (#15798) #15829
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,8 @@ | |
import com.facebook.presto.type.LiteralParameter; | ||
import com.google.common.primitives.Doubles; | ||
import io.airlift.slice.Slice; | ||
import org.apache.commons.math3.analysis.interpolation.LinearInterpolator; | ||
import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction; | ||
import org.apache.commons.math3.distribution.BetaDistribution; | ||
import org.apache.commons.math3.special.Erf; | ||
|
||
|
@@ -1279,6 +1281,54 @@ public static long widthBucket(@SqlType(StandardTypes.DOUBLE) double operand, @S | |
return lower; | ||
} | ||
|
||
@Description("Linearly interpolate a value at x given coordinates") | ||
@ScalarFunction("linear_interpolate") | ||
@SqlNullable | ||
@SqlType(StandardTypes.DOUBLE) | ||
public static Double linearInterpolate( | ||
@SqlType(StandardTypes.DOUBLE) double x, | ||
@SqlType("array(double)") Block xArray, | ||
@SqlType("array(double)") Block yArray) | ||
{ | ||
int xCount = xArray.getPositionCount(); | ||
int yCount = yArray.getPositionCount(); | ||
checkCondition(xCount == yCount, INVALID_FUNCTION_ARGUMENT, "Arrays must be the same length"); | ||
checkCondition(xCount >= 2, INVALID_FUNCTION_ARGUMENT, "Arrays must have length >= 2"); | ||
checkCondition(!Double.isNaN(x) && !Double.isInfinite(x), INVALID_FUNCTION_ARGUMENT, "NaNs not supported"); | ||
|
||
double[] xPrimitiveArray = new double[xCount]; | ||
double[] yPrimitiveArray = new double[xCount]; | ||
boolean yIsNull = false; | ||
for (int i = 0; i < xCount; i++) { | ||
Double xValue = DOUBLE.getDouble(xArray, i); | ||
Double yValue = DOUBLE.getDouble(yArray, i); | ||
checkCondition(!xArray.isNull(i), INVALID_FUNCTION_ARGUMENT, "x array must be strictly increasing"); | ||
checkCondition(!Double.isNaN(xValue) && !Double.isInfinite(xValue), INVALID_FUNCTION_ARGUMENT, "NaNs not supported"); | ||
checkCondition(!Double.isNaN(yValue) && !Double.isInfinite(yValue), INVALID_FUNCTION_ARGUMENT, "NaNs not supported"); | ||
if (i < xCount - 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove this if:
|
||
checkCondition(xValue < DOUBLE.getDouble(xArray, i + 1), INVALID_FUNCTION_ARGUMENT, "x array must be strictly increasing"); | ||
} | ||
xPrimitiveArray[i] = xValue; | ||
yPrimitiveArray[i] = yValue; | ||
yIsNull = yIsNull || yArray.isNull(i); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Execute this logic first before the for loop. Handle all special cases that could terminate the logic early first to you don't waste cpu.
|
||
} | ||
|
||
if (yIsNull) { | ||
return null; | ||
} | ||
|
||
if (x < xPrimitiveArray[0]) { | ||
return yPrimitiveArray[0]; | ||
} | ||
|
||
if (x > xPrimitiveArray[xCount - 1]) { | ||
return yPrimitiveArray[xCount - 1]; | ||
} | ||
|
||
PolynomialSplineFunction func = (new LinearInterpolator()).interpolate(xPrimitiveArray, yPrimitiveArray); | ||
return func.value(x); | ||
} | ||
|
||
@Description("cosine similarity between the given sparse vectors") | ||
@ScalarFunction | ||
@SqlNullable | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How big these arrays tend to be? There are way too many conditionals in the for loop. It would probably be more efficient if you take the conditional checks out. For example, do the null check before processing the data.