|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.db.queryengine.execution.aggregation; |
| 21 | + |
| 22 | +import org.apache.tsfile.block.column.Column; |
| 23 | +import org.apache.tsfile.block.column.ColumnBuilder; |
| 24 | +import org.apache.tsfile.enums.TSDataType; |
| 25 | +import org.apache.tsfile.file.metadata.statistics.Statistics; |
| 26 | +import org.apache.tsfile.utils.Binary; |
| 27 | +import org.apache.tsfile.utils.BitMap; |
| 28 | + |
| 29 | +import java.nio.ByteBuffer; |
| 30 | + |
| 31 | +import static com.google.common.base.Preconditions.checkArgument; |
| 32 | + |
| 33 | +public class CovarianceAccumulator implements Accumulator { |
| 34 | + |
| 35 | + public enum CovarianceType { |
| 36 | + COVAR_POP, |
| 37 | + COVAR_SAMP |
| 38 | + } |
| 39 | + |
| 40 | + private final TSDataType[] seriesDataTypes; |
| 41 | + private final CovarianceType covarianceType; |
| 42 | + |
| 43 | + private long count; |
| 44 | + private double meanX; |
| 45 | + private double meanY; |
| 46 | + private double c2; |
| 47 | + |
| 48 | + public CovarianceAccumulator(TSDataType[] seriesDataTypes, CovarianceType covarianceType) { |
| 49 | + this.seriesDataTypes = seriesDataTypes; |
| 50 | + this.covarianceType = covarianceType; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void addInput(Column[] columns, BitMap bitMap) { |
| 55 | + int size = columns[0].getPositionCount(); |
| 56 | + for (int i = 0; i < size; i++) { |
| 57 | + if (bitMap != null && !bitMap.isMarked(i)) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + if (columns[1].isNull(i) || columns[2].isNull(i)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + double x = getDoubleValue(columns[1], i, seriesDataTypes[0]); |
| 65 | + double y = getDoubleValue(columns[2], i, seriesDataTypes[1]); |
| 66 | + update(x, y); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private double getDoubleValue(Column column, int position, TSDataType dataType) { |
| 71 | + switch (dataType) { |
| 72 | + case INT32: |
| 73 | + return column.getInt(position); |
| 74 | + case INT64: |
| 75 | + case TIMESTAMP: |
| 76 | + return column.getLong(position); |
| 77 | + case FLOAT: |
| 78 | + return column.getFloat(position); |
| 79 | + case DOUBLE: |
| 80 | + return column.getDouble(position); |
| 81 | + default: |
| 82 | + throw new IllegalArgumentException("Unsupported data type: " + dataType); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void update(double x, double y) { |
| 87 | + long newCount = count + 1; |
| 88 | + double oldMeanX = meanX; |
| 89 | + meanX = oldMeanX + (x - oldMeanX) / newCount; |
| 90 | + double oldMeanY = meanY; |
| 91 | + double newMeanY = oldMeanY + (y - oldMeanY) / newCount; |
| 92 | + meanY = newMeanY; |
| 93 | + c2 += (x - oldMeanX) * (y - newMeanY); |
| 94 | + count = newCount; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void addIntermediate(Column[] partialResult) { |
| 99 | + checkArgument(partialResult.length == 1, "partialResult of Covariance should be 1"); |
| 100 | + if (partialResult[0].isNull(0)) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + byte[] bytes = partialResult[0].getBinary(0).getValues(); |
| 105 | + ByteBuffer buffer = ByteBuffer.wrap(bytes); |
| 106 | + |
| 107 | + long otherCount = buffer.getLong(); |
| 108 | + double otherMeanX = buffer.getDouble(); |
| 109 | + double otherMeanY = buffer.getDouble(); |
| 110 | + double otherC2 = buffer.getDouble(); |
| 111 | + |
| 112 | + merge(otherCount, otherMeanX, otherMeanY, otherC2); |
| 113 | + } |
| 114 | + |
| 115 | + private void merge(long otherCount, double otherMeanX, double otherMeanY, double otherC2) { |
| 116 | + if (otherCount == 0) { |
| 117 | + return; |
| 118 | + } |
| 119 | + if (count == 0) { |
| 120 | + count = otherCount; |
| 121 | + meanX = otherMeanX; |
| 122 | + meanY = otherMeanY; |
| 123 | + c2 = otherC2; |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + long newCount = count + otherCount; |
| 128 | + double deltaX = otherMeanX - meanX; |
| 129 | + double deltaY = otherMeanY - meanY; |
| 130 | + |
| 131 | + c2 += otherC2 + deltaX * deltaY * count * otherCount / newCount; |
| 132 | + meanX += deltaX * otherCount / newCount; |
| 133 | + meanY += deltaY * otherCount / newCount; |
| 134 | + count = newCount; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void outputIntermediate(ColumnBuilder[] columnBuilders) { |
| 139 | + checkArgument(columnBuilders.length == 1, "partialResult of Covariance should be 1"); |
| 140 | + if (count == 0) { |
| 141 | + columnBuilders[0].appendNull(); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + Double.BYTES * 3); |
| 146 | + buffer.putLong(count); |
| 147 | + buffer.putDouble(meanX); |
| 148 | + buffer.putDouble(meanY); |
| 149 | + buffer.putDouble(c2); |
| 150 | + columnBuilders[0].writeBinary(new Binary(buffer.array())); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void outputFinal(ColumnBuilder columnBuilder) { |
| 155 | + switch (covarianceType) { |
| 156 | + case COVAR_POP: |
| 157 | + if (count == 0) { |
| 158 | + columnBuilder.appendNull(); |
| 159 | + } else { |
| 160 | + columnBuilder.writeDouble(c2 / count); |
| 161 | + } |
| 162 | + break; |
| 163 | + case COVAR_SAMP: |
| 164 | + if (count < 2) { |
| 165 | + columnBuilder.appendNull(); |
| 166 | + } else { |
| 167 | + columnBuilder.writeDouble(c2 / (count - 1)); |
| 168 | + } |
| 169 | + break; |
| 170 | + default: |
| 171 | + throw new UnsupportedOperationException("Unknown type: " + covarianceType); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void removeIntermediate(Column[] input) { |
| 177 | + throw new UnsupportedOperationException("Remove not implemented for Covariance"); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void addStatistics(Statistics statistics) { |
| 182 | + throw new UnsupportedOperationException(getClass().getName()); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public void setFinal(Column finalResult) {} |
| 187 | + |
| 188 | + @Override |
| 189 | + public void reset() { |
| 190 | + count = 0; |
| 191 | + meanX = 0; |
| 192 | + meanY = 0; |
| 193 | + c2 = 0; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public boolean hasFinalResult() { |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public TSDataType[] getIntermediateType() { |
| 203 | + return new TSDataType[] {TSDataType.TEXT}; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public TSDataType getFinalType() { |
| 208 | + return TSDataType.DOUBLE; |
| 209 | + } |
| 210 | +} |
0 commit comments