Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4366,6 +4366,42 @@ public void approxPercentileTest() {
DATABASE_NAME);
}

@Test
public void percentileTest() {
tableResultSetEqualTest(
"select percentile(time, 0.5),percentile(s1,0.5),percentile(s2,0.5),percentile(s3,0.5),percentile(s4,0.5),percentile(s9,0.5) from table1",
buildHeaders(6),
new String[] {"2024-09-24T06:15:40.000Z,40,43000,37.5,43.0,2024-09-24T06:15:40.000Z,"},
DATABASE_NAME);

tableResultSetEqualTest(
"select time,province,percentile(time, 0.5),percentile(s1,0.5),percentile(s2,0.5) from table1 group by 1,2 order by 2,1",
new String[] {"time", "province", "_col2", "_col3", "_col4"},
new String[] {
"2024-09-24T06:15:30.000Z,beijing,2024-09-24T06:15:30.000Z,30,null,",
"2024-09-24T06:15:31.000Z,beijing,2024-09-24T06:15:31.000Z,null,31000,",
"2024-09-24T06:15:35.000Z,beijing,2024-09-24T06:15:35.000Z,null,35000,",
"2024-09-24T06:15:36.000Z,beijing,2024-09-24T06:15:36.000Z,36,null,",
"2024-09-24T06:15:40.000Z,beijing,2024-09-24T06:15:40.000Z,40,40000,",
"2024-09-24T06:15:41.000Z,beijing,2024-09-24T06:15:41.000Z,41,null,",
"2024-09-24T06:15:46.000Z,beijing,2024-09-24T06:15:46.000Z,null,46000,",
"2024-09-24T06:15:50.000Z,beijing,2024-09-24T06:15:50.000Z,null,50000,",
"2024-09-24T06:15:51.000Z,beijing,2024-09-24T06:15:51.000Z,null,null,",
"2024-09-24T06:15:55.000Z,beijing,2024-09-24T06:15:55.000Z,55,null,",
"2024-09-24T06:15:30.000Z,shanghai,2024-09-24T06:15:30.000Z,30,null,",
"2024-09-24T06:15:31.000Z,shanghai,2024-09-24T06:15:31.000Z,null,31000,",
"2024-09-24T06:15:35.000Z,shanghai,2024-09-24T06:15:35.000Z,null,35000,",
"2024-09-24T06:15:36.000Z,shanghai,2024-09-24T06:15:36.000Z,36,null,",
"2024-09-24T06:15:40.000Z,shanghai,2024-09-24T06:15:40.000Z,40,40000,",
"2024-09-24T06:15:41.000Z,shanghai,2024-09-24T06:15:41.000Z,41,null,",
"2024-09-24T06:15:46.000Z,shanghai,2024-09-24T06:15:46.000Z,null,46000,",
"2024-09-24T06:15:50.000Z,shanghai,2024-09-24T06:15:50.000Z,null,50000,",
"2024-09-24T06:15:51.000Z,shanghai,2024-09-24T06:15:51.000Z,null,null,",
"2024-09-24T06:15:55.000Z,shanghai,2024-09-24T06:15:55.000Z,55,null,",
},
DATABASE_NAME);
}

@Test
public void exceptionTest() {
tableAssertTestFail(
Expand Down Expand Up @@ -4456,6 +4492,22 @@ public void exceptionTest() {
"select 1 as g, approx_percentile(s1,s2,0.5) from table1 group by 1",
"701: Aggregation functions [approx_percentile] do not support weight as INT64 type",
DATABASE_NAME);
tableAssertTestFail(
"select percentile() from table1",
"701: Aggregation functions [percentile] should only have two arguments",
DATABASE_NAME);
tableAssertTestFail(
"select percentile(s1,1.1) from table1",
"701: percentage should be in [0,1], got 1.1",
DATABASE_NAME);
tableAssertTestFail(
"select percentile(s1,'test') from table1",
"701: The second argument of 'percentile' function percentage must be a double literal",
DATABASE_NAME);
tableAssertTestFail(
"select percentile(s5,0.5) from table1",
"701: Aggregation functions [percentile] should have value column as numeric type [INT32, INT64, FLOAT, DOUBLE, TIMESTAMP]",
DATABASE_NAME);
}

// ==================================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* 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;

import org.apache.iotdb.db.exception.sql.SemanticException;

import org.apache.tsfile.utils.RamUsageEstimator;
import org.apache.tsfile.utils.ReadWriteIOUtils;

import java.nio.ByteBuffer;
import java.util.Arrays;

public class Percentile {
private double[] values;
private int size;
private int capacity;
private boolean sorted;

private static final int INITIAL_CAPACITY = 32;
private static final double GROWTH_FACTOR = 1.5;

public Percentile() {
this.capacity = INITIAL_CAPACITY;
this.values = new double[capacity];
this.size = 0;
this.sorted = true;
}

public void addValue(double value) {
ensureCapacity();
values[size++] = value;
sorted = false;
}

public void addValues(double... vals) {
if (vals == null || vals.length == 0) return;

int newSize = size + vals.length;
if (newSize > capacity) {
grow(newSize);
}

System.arraycopy(vals, 0, values, size, vals.length);
size = newSize;
sorted = false;
}

public void merge(Percentile other) {
if (other == null || other.size == 0) {
return;
}

int newSize = size + other.size;
if (newSize > capacity) {
grow(newSize);
}

System.arraycopy(other.values, 0, values, size, other.size);
size = newSize;
sorted = false;
}

public double getPercentile(double percentile) {
if (size == 0) {
return Double.NaN;
}
if (percentile < 0.0 || percentile > 1.0) {
throw new SemanticException("percentage should be in [0,1], got " + percentile);
}

ensureSorted();

if (size == 1) {
return values[0];
}

double realIndex = percentile * (size - 1);
int index = (int) realIndex;
double fraction = realIndex - index;

if (index >= size - 1) {
return values[size - 1];
}

return values[index] + fraction * (values[index + 1] - values[index]);
}

public int getSize() {
return size;
}

public void clear() {
size = 0;
sorted = true;
}

private void ensureCapacity() {
if (size >= capacity) {
grow(size + 1);
}
}

private void grow(int minCapacity) {
int newCapacity = Math.max((int) (capacity * GROWTH_FACTOR), minCapacity);
double[] newValues = new double[newCapacity];
System.arraycopy(values, 0, newValues, 0, size);
values = newValues;
capacity = newCapacity;
}

private void ensureSorted() {
if (!sorted && size > 1) {
Arrays.sort(values, 0, size);
sorted = true;
}
}

public void serialize(ByteBuffer buffer) {
ReadWriteIOUtils.write(size, buffer);
for (int i = 0; i < size; i++) {
ReadWriteIOUtils.write(values[i], buffer);
}
}

public static Percentile deserialize(ByteBuffer buffer) {
int size = ReadWriteIOUtils.readInt(buffer);
Percentile percentile = new Percentile();
if (size > percentile.capacity) {
percentile.capacity = size;
percentile.values = new double[size];
}
Comment on lines +139 to +143
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Percentile.deserialize() does not restore the 'sorted' state. Since serialize() writes values in their current internal order (which may be unsorted), leaving the deserialized instance with sorted=true can lead to incorrect getPercentile() results if the deserialized object is queried directly. Set sorted=false on deserialization (or serialize in sorted order) to preserve correctness.

Copilot uses AI. Check for mistakes.
percentile.size = size;
for (int i = 0; i < size; i++) {
percentile.values[i] = ReadWriteIOUtils.readDouble(buffer);
}
return percentile;
}

public int getSerializedSize() {
return Integer.BYTES + (size * Double.BYTES);
}

public long getEstimatedSize() {
return RamUsageEstimator.shallowSizeOfInstance(Percentile.class)
+ (long) capacity * Double.BYTES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.GroupedMinAccumulator;
import org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.GroupedMinByAccumulator;
import org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.GroupedModeAccumulator;
import org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.GroupedPercentileAccumulator;
import org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.GroupedSumAccumulator;
import org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.GroupedUserDefinedAggregateAccumulator;
import org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.GroupedVarianceAccumulator;
Expand Down Expand Up @@ -290,6 +291,8 @@ private static GroupedAccumulator createBuiltinGroupedAccumulator(
} else {
return new GroupedApproxPercentileWithWeightAccumulator(inputDataTypes.get(0));
}
case PERCENTILE:
return new GroupedPercentileAccumulator(inputDataTypes.get(0));
default:
throw new IllegalArgumentException("Invalid Aggregation function: " + aggregationType);
}
Expand Down Expand Up @@ -353,6 +356,8 @@ public static TableAccumulator createBuiltinAccumulator(
} else {
return new ApproxPercentileWithWeightAccumulator(inputDataTypes.get(0));
}
case PERCENTILE:
return new PercentileAccumulator(inputDataTypes.get(0));
default:
throw new IllegalArgumentException("Invalid Aggregation function: " + aggregationType);
}
Expand Down
Loading
Loading