|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.operator.aggregation; |
| 15 | + |
| 16 | +import com.facebook.presto.common.block.BlockBuilder; |
| 17 | +import com.facebook.presto.common.type.StandardTypes; |
| 18 | +import com.facebook.presto.operator.aggregation.state.ExtendedRegressionState; |
| 19 | +import com.facebook.presto.spi.function.AggregationFunction; |
| 20 | +import com.facebook.presto.spi.function.AggregationState; |
| 21 | +import com.facebook.presto.spi.function.CombineFunction; |
| 22 | +import com.facebook.presto.spi.function.InputFunction; |
| 23 | +import com.facebook.presto.spi.function.OutputFunction; |
| 24 | +import com.facebook.presto.spi.function.SqlType; |
| 25 | + |
| 26 | +import static com.facebook.presto.common.type.DoubleType.DOUBLE; |
| 27 | +import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionAvgx; |
| 28 | +import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionAvgy; |
| 29 | +import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionCount; |
| 30 | +import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionR2; |
| 31 | +import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionSxx; |
| 32 | +import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionSxy; |
| 33 | +import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionSyy; |
| 34 | +import static com.facebook.presto.operator.aggregation.AggregationUtils.mergeExtendedRegressionState; |
| 35 | +import static com.facebook.presto.operator.aggregation.AggregationUtils.updateExtendedRegressionState; |
| 36 | + |
| 37 | +@AggregationFunction |
| 38 | +public class DoubleRegressionExtendedAggregation |
| 39 | +{ |
| 40 | + private DoubleRegressionExtendedAggregation() {} |
| 41 | + |
| 42 | + @InputFunction |
| 43 | + public static void input(@AggregationState ExtendedRegressionState state, @SqlType(StandardTypes.DOUBLE) double dependentValue, @SqlType(StandardTypes.DOUBLE) double independentValue) |
| 44 | + { |
| 45 | + updateExtendedRegressionState(state, independentValue, dependentValue); |
| 46 | + } |
| 47 | + |
| 48 | + @CombineFunction |
| 49 | + public static void combine(@AggregationState ExtendedRegressionState state, @AggregationState ExtendedRegressionState otherState) |
| 50 | + { |
| 51 | + mergeExtendedRegressionState(state, otherState); |
| 52 | + } |
| 53 | + |
| 54 | + @AggregationFunction("regr_sxy") |
| 55 | + @OutputFunction(StandardTypes.DOUBLE) |
| 56 | + public static void regrSxy(@AggregationState ExtendedRegressionState state, BlockBuilder out) |
| 57 | + { |
| 58 | + double result = getRegressionSxy(state); |
| 59 | + double count = getRegressionCount(state); |
| 60 | + if (Double.isFinite(result) && Double.isFinite(count) && count > 0) { |
| 61 | + DOUBLE.writeDouble(out, result); |
| 62 | + } |
| 63 | + else { |
| 64 | + out.appendNull(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @AggregationFunction("regr_sxx") |
| 69 | + @OutputFunction(StandardTypes.DOUBLE) |
| 70 | + public static void regrSxx(@AggregationState ExtendedRegressionState state, BlockBuilder out) |
| 71 | + { |
| 72 | + double result = getRegressionSxx(state); |
| 73 | + double count = getRegressionCount(state); |
| 74 | + if (Double.isFinite(result) && Double.isFinite(count) && count > 0) { |
| 75 | + DOUBLE.writeDouble(out, result); |
| 76 | + } |
| 77 | + else { |
| 78 | + out.appendNull(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @AggregationFunction("regr_syy") |
| 83 | + @OutputFunction(StandardTypes.DOUBLE) |
| 84 | + public static void regrSyy(@AggregationState ExtendedRegressionState state, BlockBuilder out) |
| 85 | + { |
| 86 | + double result = getRegressionSyy(state); |
| 87 | + double count = getRegressionCount(state); |
| 88 | + if (Double.isFinite(result) && Double.isFinite(count) && count > 0) { |
| 89 | + DOUBLE.writeDouble(out, result); |
| 90 | + } |
| 91 | + else { |
| 92 | + out.appendNull(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @AggregationFunction("regr_r2") |
| 97 | + @OutputFunction(StandardTypes.DOUBLE) |
| 98 | + public static void regrR2(@AggregationState ExtendedRegressionState state, BlockBuilder out) |
| 99 | + { |
| 100 | + double result = getRegressionR2(state); |
| 101 | + if (Double.isFinite(result)) { |
| 102 | + DOUBLE.writeDouble(out, result); |
| 103 | + } |
| 104 | + else { |
| 105 | + out.appendNull(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @AggregationFunction("regr_count") |
| 110 | + @OutputFunction(StandardTypes.DOUBLE) |
| 111 | + public static void regrCount(@AggregationState ExtendedRegressionState state, BlockBuilder out) |
| 112 | + { |
| 113 | + double result = getRegressionCount(state); |
| 114 | + if (Double.isFinite(result) && result > 0) { |
| 115 | + DOUBLE.writeDouble(out, result); |
| 116 | + } |
| 117 | + else { |
| 118 | + out.appendNull(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @AggregationFunction("regr_avgy") |
| 123 | + @OutputFunction(StandardTypes.DOUBLE) |
| 124 | + public static void regrAvgy(@AggregationState ExtendedRegressionState state, BlockBuilder out) |
| 125 | + { |
| 126 | + double result = getRegressionAvgy(state); |
| 127 | + double count = getRegressionCount(state); |
| 128 | + if (Double.isFinite(result) && Double.isFinite(count) && count > 0) { |
| 129 | + DOUBLE.writeDouble(out, result); |
| 130 | + } |
| 131 | + else { |
| 132 | + out.appendNull(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @AggregationFunction("regr_avgx") |
| 137 | + @OutputFunction(StandardTypes.DOUBLE) |
| 138 | + public static void regrAvgx(@AggregationState ExtendedRegressionState state, BlockBuilder out) |
| 139 | + { |
| 140 | + double result = getRegressionAvgx(state); |
| 141 | + double count = getRegressionCount(state); |
| 142 | + if (Double.isFinite(result) && Double.isFinite(count) && count > 0) { |
| 143 | + DOUBLE.writeDouble(out, result); |
| 144 | + } |
| 145 | + else { |
| 146 | + out.appendNull(); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments