Skip to content
Merged
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 @@ -46,7 +46,6 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
* executable functions:
Expand Down Expand Up @@ -1136,13 +1135,16 @@ public static Expression interval(BigIntLiteral compareValue, Literal... thresho
}
}

int pos = Arrays.binarySearch(thresholdValues, value);

if (pos >= 0) {
return new IntegerLiteral(pos + 1);
} else {
int insertionPoint = -(pos + 1);
return new IntegerLiteral(insertionPoint);
int low = 0;
int high = thresholdValues.length;
while (low < high) {
int mid = low + ((high - low) >>> 1);
if (thresholdValues[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return new IntegerLiteral(low);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.apache.doris.nereids.trees.expressions.functions.executable;

import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DecimalLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DecimalV3Literal;
import org.apache.doris.nereids.trees.expressions.literal.DoubleLiteral;
import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
import org.apache.doris.nereids.types.DecimalV2Type;
import org.apache.doris.nereids.types.DecimalV3Type;

Expand Down Expand Up @@ -54,4 +58,32 @@ public void testSignBit() {
Assertions.assertEquals(BooleanLiteral.FALSE, NumericArithmetic.signbit(new DoubleLiteral(1.0)));
Assertions.assertEquals(BooleanLiteral.TRUE, NumericArithmetic.signbit(new DoubleLiteral(-1.0)));
}

@Test
public void testIntervalUsesUpperBoundForRepeatedThresholds() {
assertInterval(0, 0);
assertInterval(0, -3, -2, -1);
assertInterval(2, 0, 0, 0);
assertInterval(3, 0, 0, 0, 0);
assertInterval(3, 1, 0, 1, 1, 2);
assertInterval(4, 3, 0, 1, 1, 2);

IntegerLiteral nullThresholdResult = (IntegerLiteral) NumericArithmetic.interval(
new BigIntLiteral(0), NullLiteral.INSTANCE, new BigIntLiteral(0));
Assertions.assertEquals(2, nullThresholdResult.getValue());

IntegerLiteral nullCompareResult = (IntegerLiteral) NumericArithmetic.interval(
NullLiteral.INSTANCE, new BigIntLiteral(0));
Assertions.assertEquals(-1, nullCompareResult.getValue());
}

private void assertInterval(int expected, long compareValue, long... thresholds) {
Literal[] thresholdLiterals = new Literal[thresholds.length];
for (int i = 0; i < thresholds.length; i++) {
thresholdLiterals[i] = new BigIntLiteral(thresholds[i]);
}
IntegerLiteral result = (IntegerLiteral) NumericArithmetic.interval(
new BigIntLiteral(compareValue), thresholdLiterals);
Assertions.assertEquals(expected, result.getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !interval_folded --
2 3 3

-- !interval_runtime --
2 3 3

-- !interval_boundaries --
-1 0 4 2

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.

suite("interval_constant_fold_consistency") {
qt_interval_folded """
select interval(0, 0, 0), interval(0, 0, 0, 0), interval(1, 0, 1, 1, 2)
"""

qt_interval_runtime """
select interval(number * 0, 0, 0),
interval(number * 0, 0, 0, 0),
interval(number * 0 + 1, 0, 1, 1, 2)
from numbers("number" = "1")
"""

qt_interval_boundaries """
select interval(null, 0),
interval(-3, -2, -1),
interval(3, 0, 1, 1, 2),
interval(0, null, 0)
"""
}
Loading