Skip to content
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

[enhance](nereids) remove first_value and second_value second parameter if the second parameter is false #45264

Merged
merged 1 commit into from
Dec 11, 2024
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 @@ -317,9 +317,15 @@ public Lead visitLead(Lead lead, Void ctx) {
@Override
public FirstOrLastValue visitFirstValue(FirstValue firstValue, Void ctx) {
FirstOrLastValue.checkSecondParameter(firstValue);
if (2 == firstValue.arity() && firstValue.child(1).equals(BooleanLiteral.TRUE)) {
return firstValue;
if (2 == firstValue.arity()) {
if (firstValue.child(1).equals(BooleanLiteral.TRUE)) {
return firstValue;
} else {
firstValue = (FirstValue) firstValue.withChildren(firstValue.child(0));
windowExpression = windowExpression.withFunction(firstValue);
}
}

Optional<WindowFrame> windowFrame = windowExpression.getWindowFrame();
if (windowFrame.isPresent()) {
WindowFrame wf = windowFrame.get();
Expand Down Expand Up @@ -347,6 +353,10 @@ public FirstOrLastValue visitFirstValue(FirstValue firstValue, Void ctx) {
@Override
public FirstOrLastValue visitLastValue(LastValue lastValue, Void ctx) {
FirstOrLastValue.checkSecondParameter(lastValue);
if (2 == lastValue.arity() && lastValue.child(1).equals(BooleanLiteral.FALSE)) {
lastValue = (LastValue) lastValue.withChildren(lastValue.child(0));
windowExpression = windowExpression.withFunction(lastValue);
}
return lastValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
import org.apache.doris.nereids.trees.expressions.WindowFrame.FrameBoundary;
import org.apache.doris.nereids.trees.expressions.WindowFrame.FrameUnitsType;
import org.apache.doris.nereids.trees.expressions.functions.window.DenseRank;
import org.apache.doris.nereids.trees.expressions.functions.window.FirstValue;
import org.apache.doris.nereids.trees.expressions.functions.window.Lag;
import org.apache.doris.nereids.trees.expressions.functions.window.LastValue;
import org.apache.doris.nereids.trees.expressions.functions.window.Lead;
import org.apache.doris.nereids.trees.expressions.functions.window.Rank;
import org.apache.doris.nereids.trees.expressions.functions.window.RowNumber;
import org.apache.doris.nereids.trees.expressions.functions.window.WindowFunction;
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DoubleLiteral;
import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
import org.apache.doris.nereids.trees.plans.Plan;
Expand Down Expand Up @@ -240,6 +243,29 @@ public void testCheckWindowFrameBeforeFunc5() {
forCheckWindowFrameBeforeFunc(windowFrame2, errorMsg2);
}

@Test
public void testFirstValueRewrite() {
age = rStudent.getOutput().get(3).toSlot();
WindowExpression window = new WindowExpression(new FirstValue(age, BooleanLiteral.FALSE), partitionKeyList, orderKeyList);
Alias windowAlias = new Alias(window, window.toSql());
WindowExpression windowLastValue = new WindowExpression(new LastValue(age, BooleanLiteral.FALSE), partitionKeyList, orderKeyList);
Alias windowLastValueAlias = new Alias(windowLastValue, windowLastValue.toSql());
List<NamedExpression> outputExpressions = Lists.newArrayList(windowAlias, windowLastValueAlias);
Plan root = new LogicalWindow<>(outputExpressions, rStudent);

PlanChecker.from(MemoTestUtils.createConnectContext(), root)
.applyTopDown(new ExtractAndNormalizeWindowExpression())
.applyTopDown(new CheckAndStandardizeWindowFunctionAndFrame())
.matches(
logicalWindow()
.when(logicalWindow -> {
WindowExpression newWindowFirstValue = (WindowExpression) logicalWindow.getWindowExpressions().get(0).child(0);
WindowExpression newWindowLastValue = (WindowExpression) logicalWindow.getWindowExpressions().get(0).child(0);
return newWindowFirstValue.getFunction().arity() == 1 && newWindowLastValue.getFunction().arity() == 1;
})
);
}

private void forCheckWindowFrameBeforeFunc(WindowFrame windowFrame, String errorMsg) {
WindowExpression window = new WindowExpression(new Rank(), partitionKeyList, orderKeyList, windowFrame);
forCheckWindowFrameBeforeFunc(window, errorMsg);
Expand Down
32 changes: 32 additions & 0 deletions regression-test/data/nereids_syntax_p0/window_function.out
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,35 @@
-- !multi_winf2 --
1 35

-- !first_value_false --
1
1
1
1
1
1
1
1
2
2
2
2
2
2

-- !last_value_false --
1
1
1
1
1
1
1
1
1
1
1
1
1
1

Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,7 @@ suite("window_function") {
sql "select last_value(c1,false) over() from window_test"
sql "select first_value(c1,1) over() from window_test"
sql "select last_value(c1,0) over() from window_test"

qt_first_value_false "select last_value(c1,false) over(partition by c2 order by c1) from window_test order by 1"
qt_last_value_false "select first_value(c1,false) over(partition by c2 order by c1) from window_test order by 1"
}
Loading