Skip to content

Commit

Permalink
[Fix](func) CoreDump and Result Error in percentile function #36643 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
HappenLee authored Jun 23, 2024
1 parent 7976bf3 commit 4c7af57
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ struct PercentileState {
inited_flag = false;
}

double get() const { return vec_counts[0].terminate(vec_quantile[0]); }
double get() const { return vec_counts.empty() ? 0 : vec_counts[0].terminate(vec_quantile[0]); }

void insert_result_into(IColumn& to) const {
auto& column_data = assert_cast<ColumnVector<Float64>&>(to).get_data();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BigIntType;
Expand All @@ -35,8 +34,8 @@
/**
* AggregateFunction 'percentile'. This class is generated by GenerateFunction.
*/
public class Percentile extends AggregateFunction
implements BinaryExpression, ExplicitlyCastableSignature, PropagateNullable {
public class Percentile extends NullableAggregateFunction
implements BinaryExpression, ExplicitlyCastableSignature {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DoubleType.INSTANCE).args(BigIntType.INSTANCE, DoubleType.INSTANCE)
Expand All @@ -46,14 +45,18 @@ public class Percentile extends AggregateFunction
* constructor with 2 arguments.
*/
public Percentile(Expression arg0, Expression arg1) {
super("percentile", arg0, arg1);
this(false, arg0, arg1);
}

/**
* constructor with 2 arguments.
*/
public Percentile(boolean distinct, Expression arg0, Expression arg1) {
super("percentile", distinct, arg0, arg1);
this(distinct, false, arg0, arg1);
}

public Percentile(boolean distinct, boolean alwaysNullable, Expression arg0, Expression arg1) {
super("percentile", distinct, alwaysNullable, arg0, arg1);
}

@Override
Expand All @@ -70,7 +73,12 @@ public void checkLegalityBeforeTypeCoercion() {
@Override
public Percentile withDistinctAndChildren(boolean distinct, List<Expression> children) {
Preconditions.checkArgument(children.size() == 2);
return new Percentile(distinct, children.get(0), children.get(1));
return new Percentile(distinct, alwaysNullable, children.get(0), children.get(1));
}

@Override
public NullableAggregateFunction withAlwaysNullable(boolean alwaysNullable) {
return new Percentile(distinct, alwaysNullable, children.get(0), children.get(1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ default R visitOrthogonalBitmapUnionCount(OrthogonalBitmapUnionCount function, C
}

default R visitPercentile(Percentile percentile, C context) {
return visitAggregateFunction(percentile, context);
return visitNullableAggregateFunction(percentile, context);
}

default R visitPercentileApprox(PercentileApprox percentileApprox, C context) {
Expand Down
3 changes: 3 additions & 0 deletions regression-test/data/nereids_function_p0/agg_function/agg.out
Original file line number Diff line number Diff line change
Expand Up @@ -2708,6 +2708,9 @@
-- !sql_percentile_BigInt_Double --
7.6

-- !sql_percentile_Null_Empty --
\N

-- !sql_percentile_BigInt_Double_agg_phase_1 --
0 \N
1 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,8 @@ suite("nereids_agg_fn") {
select percentile(kbint, 0.6) from fn_test group by kbool order by kbool'''
qt_sql_percentile_BigInt_Double '''
select percentile(kbint, 0.6) from fn_test'''
qt_sql_percentile_Null_Empty '''
select percentile(1, 0.6) from fn_test where kbint > 100;'''
qt_sql_percentile_BigInt_Double_agg_phase_1 '''
select count(id), percentile(kbint, 0.6) from fn_test group by id order by id'''
qt_sql_percentile_BigInt_Double_agg_phase_2 '''
Expand Down

0 comments on commit 4c7af57

Please sign in to comment.