-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add APPROX_PERCENTILE aggregation function #16041
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
fa744da
Implement APPROX_PERCENTILE aggregation function
FearfulTomcat27 a73efb0
feat: Enhance APPROX_PERCENTILE function with improved argument handl…
FearfulTomcat27 59823b0
fix: Refactor APPROX_PERCENTILE accumulators for improved argument ha…
FearfulTomcat27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
166 changes: 166 additions & 0 deletions
166
...execution/operator/source/relational/aggregation/AbstractApproxPercentileAccumulator.java
This file contains hidden or 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,166 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation; | ||
|
|
||
| import org.apache.iotdb.db.exception.sql.SemanticException; | ||
| import org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.approximate.TDigest; | ||
|
|
||
| import org.apache.tsfile.block.column.Column; | ||
| import org.apache.tsfile.block.column.ColumnBuilder; | ||
| import org.apache.tsfile.enums.TSDataType; | ||
| import org.apache.tsfile.file.metadata.statistics.Statistics; | ||
| import org.apache.tsfile.utils.Binary; | ||
| import org.apache.tsfile.utils.RamUsageEstimator; | ||
| import org.apache.tsfile.utils.ReadWriteIOUtils; | ||
| import org.apache.tsfile.write.UnSupportedDataTypeException; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| public abstract class AbstractApproxPercentileAccumulator implements TableAccumulator { | ||
| private static final long INSTANCE_SIZE = | ||
| RamUsageEstimator.shallowSizeOfInstance(ApproxPercentileAccumulator.class); | ||
|
|
||
| protected final TDigest tDigest = new TDigest(); | ||
| protected final TSDataType seriesDataType; | ||
| protected double percentage; | ||
|
|
||
| public AbstractApproxPercentileAccumulator(TSDataType seriesDataType) { | ||
| this.seriesDataType = seriesDataType; | ||
| } | ||
|
|
||
| @Override | ||
| public long getEstimatedSize() { | ||
| return INSTANCE_SIZE + tDigest.getEstimatedSize(); | ||
| } | ||
|
|
||
| @Override | ||
| public TableAccumulator copy() { | ||
| return new ApproxPercentileAccumulator(seriesDataType); | ||
| } | ||
|
|
||
| @Override | ||
| public void addInput(Column[] arguments, AggregationMask mask) { | ||
| if (arguments.length == 2) { | ||
| percentage = arguments[1].getDouble(0); | ||
| } else if (arguments.length == 3) { | ||
| percentage = arguments[2].getDouble(0); | ||
| } else { | ||
| throw new SemanticException( | ||
| String.format( | ||
| "APPROX_PERCENTILE requires 2 or 3 arguments, but got %d", arguments.length)); | ||
| } | ||
| switch (seriesDataType) { | ||
| case INT32: | ||
| addIntInput(arguments, mask); | ||
| return; | ||
| case INT64: | ||
| case TIMESTAMP: | ||
| addLongInput(arguments, mask); | ||
| return; | ||
| case FLOAT: | ||
| addFloatInput(arguments, mask); | ||
| return; | ||
| case DOUBLE: | ||
| addDoubleInput(arguments, mask); | ||
| return; | ||
| default: | ||
| throw new UnSupportedDataTypeException( | ||
| String.format( | ||
| "Unsupported data type in APPROX_PERCENTILE Aggregation: %s", seriesDataType)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void addIntermediate(Column argument) { | ||
| for (int i = 0; i < argument.getPositionCount(); i++) { | ||
| if (!argument.isNull(i)) { | ||
| byte[] data = argument.getBinary(i).getValues(); | ||
| // Read percentage from the first 8 bytes and TDigest from the rest | ||
| ByteBuffer buffer = ByteBuffer.wrap(data); | ||
| this.percentage = ReadWriteIOUtils.readDouble(buffer); | ||
| byte[] tDigestData = new byte[data.length - 8]; | ||
| buffer.get(tDigestData); | ||
|
FearfulTomcat27 marked this conversation as resolved.
Outdated
|
||
| TDigest other = TDigest.fromByteArray(tDigestData); | ||
| tDigest.add(other); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void evaluateIntermediate(ColumnBuilder columnBuilder) { | ||
| byte[] tDigestData = tDigest.toByteArray(); | ||
| // Create a buffer with space for percentage (8 bytes) + TDigest data | ||
| ByteBuffer buffer = ByteBuffer.allocate(8 + tDigestData.length); | ||
| ReadWriteIOUtils.write(percentage, buffer); | ||
| buffer.put(tDigestData); | ||
|
FearfulTomcat27 marked this conversation as resolved.
Outdated
|
||
| columnBuilder.writeBinary(new Binary(buffer.array())); | ||
| } | ||
|
|
||
| @Override | ||
| public void evaluateFinal(ColumnBuilder columnBuilder) { | ||
| switch (seriesDataType) { | ||
| case INT32: | ||
| columnBuilder.writeInt((int) tDigest.quantile(percentage)); | ||
| break; | ||
| case INT64: | ||
| case TIMESTAMP: | ||
| columnBuilder.writeLong((long) tDigest.quantile(percentage)); | ||
| break; | ||
| case FLOAT: | ||
| columnBuilder.writeFloat((float) tDigest.quantile(percentage)); | ||
| break; | ||
| case DOUBLE: | ||
| columnBuilder.writeDouble(tDigest.quantile(percentage)); | ||
| break; | ||
| default: | ||
| throw new UnSupportedDataTypeException( | ||
| String.format( | ||
| "Unsupported data type in APPROX_PERCENTILE Aggregation: %s", seriesDataType)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasFinalResult() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void addStatistics(Statistics[] statistics) { | ||
| throw new UnsupportedOperationException( | ||
| "ApproxPercentileAccumulator does not support statistics"); | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() { | ||
| tDigest.reset(); | ||
| } | ||
|
|
||
| public abstract void addIntInput(Column[] arguments, AggregationMask mask); | ||
|
|
||
| public abstract void addLongInput(Column[] arguments, AggregationMask mask); | ||
|
|
||
| public abstract void addFloatInput(Column[] arguments, AggregationMask mask); | ||
|
|
||
| public abstract void addDoubleInput(Column[] arguments, AggregationMask mask); | ||
|
|
||
| public static double toDoubleExact(long value) { | ||
| double doubleValue = (double) value; | ||
| if ((long) doubleValue != value) { | ||
| throw new SemanticException( | ||
| String.format("no exact double representation for long: %s", value)); | ||
| } | ||
| return value; | ||
| } | ||
| } | ||
This file contains hidden or 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
119 changes: 119 additions & 0 deletions
119
...yengine/execution/operator/source/relational/aggregation/ApproxPercentileAccumulator.java
This file contains hidden or 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,119 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation; | ||
|
|
||
| import org.apache.tsfile.block.column.Column; | ||
| import org.apache.tsfile.enums.TSDataType; | ||
|
|
||
| public class ApproxPercentileAccumulator extends AbstractApproxPercentileAccumulator { | ||
|
|
||
| public ApproxPercentileAccumulator(TSDataType seriesDataType) { | ||
| super(seriesDataType); | ||
| } | ||
|
|
||
| @Override | ||
| public void addIntInput(Column[] arguments, AggregationMask mask) { | ||
|
|
||
| Column valueColumn = arguments[0]; | ||
| int positionCount = mask.getPositionCount(); | ||
|
|
||
| if (mask.isSelectAll()) { | ||
| for (int i = 0; i < valueColumn.getPositionCount(); i++) { | ||
| if (!valueColumn.isNull(i)) { | ||
| tDigest.add(valueColumn.getInt(i)); | ||
| } | ||
| } | ||
| } else { | ||
| int[] selectedPositions = mask.getSelectedPositions(); | ||
| int position; | ||
| for (int i = 0; i < positionCount; i++) { | ||
| position = selectedPositions[i]; | ||
| if (!valueColumn.isNull(position)) { | ||
| tDigest.add(valueColumn.getInt(position)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void addLongInput(Column[] arguments, AggregationMask mask) { | ||
|
|
||
| Column valueColumn = arguments[0]; | ||
| int positionCount = mask.getPositionCount(); | ||
|
|
||
| if (mask.isSelectAll()) { | ||
| for (int i = 0; i < valueColumn.getPositionCount(); i++) { | ||
| if (!valueColumn.isNull(i)) { | ||
| tDigest.add(toDoubleExact(valueColumn.getLong(i))); | ||
| } | ||
| } | ||
| } else { | ||
| int[] selectedPositions = mask.getSelectedPositions(); | ||
| int position; | ||
| for (int i = 0; i < positionCount; i++) { | ||
| position = selectedPositions[i]; | ||
| if (!valueColumn.isNull(position)) { | ||
| tDigest.add(toDoubleExact(valueColumn.getLong(position))); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void addFloatInput(Column[] arguments, AggregationMask mask) { | ||
|
|
||
| Column valueColumn = arguments[0]; | ||
| int positionCount = mask.getPositionCount(); | ||
|
|
||
| if (mask.isSelectAll()) { | ||
| for (int i = 0; i < valueColumn.getPositionCount(); i++) { | ||
| if (!valueColumn.isNull(i)) { | ||
| tDigest.add(valueColumn.getFloat(i)); | ||
| } | ||
| } | ||
| } else { | ||
| int[] selectedPositions = mask.getSelectedPositions(); | ||
| int position; | ||
| for (int i = 0; i < positionCount; i++) { | ||
| position = selectedPositions[i]; | ||
| if (!valueColumn.isNull(position)) { | ||
| tDigest.add(valueColumn.getFloat(position)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void addDoubleInput(Column[] arguments, AggregationMask mask) { | ||
| Column valueColumn = arguments[0]; | ||
| int positionCount = mask.getPositionCount(); | ||
| if (mask.isSelectAll()) { | ||
| for (int i = 0; i < valueColumn.getPositionCount(); i++) { | ||
| if (!valueColumn.isNull(i)) { | ||
| tDigest.add(valueColumn.getDouble(i)); | ||
| } | ||
| } | ||
| } else { | ||
| int[] selectedPositions = mask.getSelectedPositions(); | ||
| int position; | ||
| for (int i = 0; i < positionCount; i++) { | ||
| position = selectedPositions[i]; | ||
| if (!valueColumn.isNull(position)) { | ||
| tDigest.add(valueColumn.getDouble(position)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.