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
2 changes: 2 additions & 0 deletions be/src/vec/aggregate_functions/aggregate_function_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ struct LeadLagData {
const auto* nullable_column = assert_cast<const ColumnNullable*>(column);
if (nullable_column->is_null_at(0)) {
_default_value.reset();
} else {
_default_value.set_value(nullable_column->get_nested_column_ptr(), 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

seems the default value must be const, so if it's nullable_column, the default value must be NULL?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

new nereids planner may add a cast to literal string internally like:
select id, create_time, lead(create_time, 1, '2022-09-06 00:00:00') over (order by create_time desc) as "prev_time" from test1;
->
select id, create_time, lead(create_time, 1, cast('2022-09-06 00:00:00' as datetime)) over (order by create_time desc) as "prev_time" from test1;

so, the above code is added for compatibility of nereids planner

}
} else {
_default_value.set_value(column, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,12 +811,18 @@ public PlanFragment visitPhysicalWindow(PhysicalWindow<? extends Plan> physicalW

if (partitionExprs.isEmpty() && orderByElements.isEmpty()) {
if (inputPlanFragment.isPartitioned()) {
inputPlanFragment = createParentFragment(inputPlanFragment, DataPartition.UNPARTITIONED, context);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

when fragment.root is exchange, it is wrong to call createParentFragment(fragment, ...)
How about move this logical into createParentFragment, or remove this function.
it is error prone

PlanFragment parentFragment = new PlanFragment(context.nextFragmentId(), analyticEvalNode,
DataPartition.UNPARTITIONED);
context.addPlanFragment(parentFragment);
connectChildFragment(analyticEvalNode, 0, parentFragment, inputPlanFragment, context);
inputPlanFragment = parentFragment;
} else {
inputPlanFragment.addPlanRoot(analyticEvalNode);
}
} else {
analyticEvalNode.setNumInstances(inputPlanFragment.getPlanRoot().getNumInstances());
inputPlanFragment.addPlanRoot(analyticEvalNode);
}
addPlanRoot(inputPlanFragment, analyticEvalNode, physicalWindow);
return inputPlanFragment;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ private PhysicalProperties enforceDistributionButMeetSort(PhysicalProperties out

private PhysicalProperties enforceGlobalSort(PhysicalProperties oldOutputProperty, PhysicalProperties required) {
// keep consistent in DistributionSpec with the oldOutputProperty
PhysicalProperties newOutputProperty = new PhysicalProperties(required.getOrderSpec());
PhysicalProperties newOutputProperty = new PhysicalProperties(
oldOutputProperty.getDistributionSpec(), required.getOrderSpec());
GroupExpression enforcer = required.getOrderSpec().addGlobalQuickSortEnforcer(groupExpression.getOwnerGroup());

addEnforcerUpdateCost(enforcer, oldOutputProperty, newOutputProperty);
Expand Down Expand Up @@ -128,7 +129,9 @@ private PhysicalProperties enforceSortAndDistribution(PhysicalProperties outputP
PhysicalProperties requiredProperty) {
PhysicalProperties enforcedProperty;
if (requiredProperty.getDistributionSpec().equals(new DistributionSpecGather())) {
enforcedProperty = enforceGlobalSort(outputProperty, requiredProperty);
enforcedProperty = enforceLocalSort(outputProperty, requiredProperty);
enforcedProperty = enforceDistribution(enforcedProperty, requiredProperty);
enforcedProperty = enforceGlobalSort(enforcedProperty, requiredProperty);
} else {
enforcedProperty = enforceDistribution(outputProperty, requiredProperty);
enforcedProperty = enforceLocalSort(enforcedProperty, requiredProperty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,10 @@ public FirstOrLastValue visitFirstValue(FirstValue firstValue, Void ctx) {
WindowFrame wf = windowFrame.get();
if (wf.getLeftBoundary().isNot(FrameBoundType.UNBOUNDED_PRECEDING)
&& wf.getLeftBoundary().isNot(FrameBoundType.PRECEDING)) {
windowExpression = windowExpression.withWindowFrame(wf.withRightBoundary(wf.getLeftBoundary()));
windowExpression = windowExpression.withWindowFrame(
wf.withFrameUnits(FrameUnitsType.ROWS).withRightBoundary(wf.getLeftBoundary()));
LastValue lastValue = new LastValue(firstValue.child());
windowExpression = windowExpression.withFunction(lastValue);
return lastValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private PhysicalWindow<Plan> createPhysicalWindow(Plan root, WindowFrameGroup wi
// todo: WFGs in the same OKG only need same RequiredProperties
PhysicalProperties properties;
if (windowFrameGroup.partitionKeys.isEmpty()) {
properties = new PhysicalProperties(new OrderSpec(requiredOrderKeys));
properties = PhysicalProperties.GATHER.withOrderSpec(new OrderSpec(requiredOrderKeys));
} else {
properties = PhysicalProperties.createHash(
windowFrameGroup.partitionKeys, DistributionSpecHash.ShuffleType.ENFORCED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public WindowFrame reverseWindow() {
return new WindowFrame(frameUnits, rightBoundary.reverse(), leftBoundary.reverse());
}

public WindowFrame withFrameUnits(FrameUnitsType newFrameUnits) {
return new WindowFrame(newFrameUnits, leftBoundary, rightBoundary);
}

public WindowFrame withRightBoundary(FrameBoundary newRightBoundary) {
return new WindowFrame(frameUnits, leftBoundary, newRightBoundary);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DataType;

import com.google.common.base.Preconditions;

import java.util.List;

/**
* Window function: Last_value()
*/
Expand All @@ -30,6 +34,12 @@ public LastValue(Expression child) {
super("last_value", child);
}

@Override
public LastValue withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new LastValue(children.get(0));
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitLastValue(this, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select_default --
21 04-21-11 1 1
22 04-22-10-21 0 0
22 04-22-10-21 1 1
23 04-23-10 1 1
24 02-24-10-21 1 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select_default --
a aa /wyyt-image/2021/11/13/595345040188712460.jpg unknown
b aa /wyyt-image/2022/04/13/1434607674511761493.jpg unknown
c cc /wyyt-image/2022/04/13/1434607674511761493.jpg /wyyt-image/2022/04/13/1434607674511761493.jpg

-- !select_default2 --
a aa /wyyt-image/2021/11/13/595345040188712460.jpg
b aa /wyyt-image/2022/04/13/1434607674511761493.jpg /wyyt-image/2022/04/13/1434607674511761493.jpg
c cc /wyyt-image/2022/04/13/1434607674511761493.jpg

-- !select_default --
a 2022-09-06T00:00 2022-09-06T00:00
b 2022-09-06T00:00:01 2022-09-06T00:00
c 2022-09-06T00:00:02 2022-09-06T00:00:01

-- !select_default --
a 2022-09-06T00:00 2022-08-30T00:00
b 2022-09-06T00:00:01 2022-09-06T00:00
c 2022-09-06T00:00:02 2022-09-06T00:00:01

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select --

-- !select --
1 1 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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("nereids_first_value_window") {
sql 'use regression_test_nereids_function_p0'
sql 'set enable_nereids_planner=true'
sql 'set enable_fallback_to_original_planner=false'
def tableName = "nereids_first_value_window_state"

sql """ DROP TABLE IF EXISTS ${tableName} """
sql """
CREATE TABLE IF NOT EXISTS ${tableName} (
`myday` INT,
`time_col` VARCHAR(40) NOT NULL,
`state` INT
) ENGINE=OLAP
DUPLICATE KEY(`myday`,time_col,state)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`myday`) BUCKETS 2
PROPERTIES (
"replication_num" = "1",
"in_memory" = "false",
"storage_format" = "V2"
);
"""

sql """ INSERT INTO ${tableName} VALUES
(21,"04-21-11",1),
(22,"04-22-10-21",0),
(22,"04-22-10-21",1),
(23,"04-23-10",1),
(24,"02-24-10-21",1); """

qt_select_default """ select *,first_value(state) over(partition by myday order by time_col range between current row and unbounded following) from ${tableName} order by myday, time_col, state; """
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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("nereids_lag_lead_window") {
sql 'use regression_test_nereids_function_p0'
sql 'set enable_nereids_planner=true'
sql 'set enable_fallback_to_original_planner=false'
def tableName = "wftest"


sql """ DROP TABLE IF EXISTS ${tableName} """
sql """
CREATE TABLE IF NOT EXISTS ${tableName} ( `aa` varchar(10) NULL COMMENT "", `bb` text NULL COMMENT "", `cc` text NULL COMMENT "" )
ENGINE=OLAP UNIQUE KEY(`aa`) DISTRIBUTED BY HASH(`aa`) BUCKETS 3
PROPERTIES ( "replication_allocation" = "tag.location.default: 1", "in_memory" = "false", "storage_format" = "V2" );
"""

sql """ INSERT INTO ${tableName} VALUES
('a','aa','/wyyt-image/2021/11/13/595345040188712460.jpg'),
('b','aa','/wyyt-image/2022/04/13/1434607674511761493.jpg'),
('c','cc','/wyyt-image/2022/04/13/1434607674511761493.jpg') """

qt_select_default """
select aa, bb, min(cc) over(PARTITION by cc order by aa) ,
lag(cc,1,'unknown') over (PARTITION by cc order by aa) as lag_cc
from ${tableName}
order by aa; """

qt_select_default2 """ select aa, bb, min(cc) over(PARTITION by cc order by aa) ,
lead(cc,1,'') over (PARTITION by cc order by aa) as lead_cc
from ${tableName}
order by aa; """
sql """ DROP TABLE IF EXISTS test1 """
sql """ CREATE TABLE IF NOT EXISTS test1 (id varchar(255), create_time datetime)
DISTRIBUTED BY HASH(id) PROPERTIES("replication_num" = "1"); """
sql """ INSERT INTO test1 VALUES
('a','2022-09-06 00:00:00'),
('b','2022-09-06 00:00:01'),
('c','2022-09-06 00:00:02') """
qt_select_default """ select id, create_time, lead(create_time, 1, '2022-09-06 00:00:00') over
(order by create_time desc) as "prev_time" from test1 order by id; """
qt_select_default """ select id, create_time, lead(create_time, 1, date_sub('2022-09-06 00:00:00', interval 7 day)) over (order by create_time desc) as "prev_time" from test1 order by id; """
sql """ DROP TABLE IF EXISTS test1 """
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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("nereids_outer_join_with_empty_node") {
sql "SET enable_nereids_planner=true"
sql "SET enable_fallback_to_original_planner=false"

sql """
drop table if exists outer_join_with_empty_node_t1;
"""

sql """
drop table if exists outer_join_with_empty_node_t2;
"""

sql """
CREATE TABLE IF NOT EXISTS `outer_join_with_empty_node_t1` (
`k1` int(11) NULL COMMENT "",
`k2` int(11) NULL COMMENT ""
) ENGINE=OLAP
DUPLICATE KEY(`k1`, `k2`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`k1`) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2"
);
"""

sql """
CREATE TABLE IF NOT EXISTS `outer_join_with_empty_node_t2` (
`j1` int(11) NULL COMMENT "",
`j2` int(11) NULL COMMENT ""
) ENGINE=OLAP
DUPLICATE KEY(`j1`, `j2`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`j1`) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2"
);
"""

sql """
insert into outer_join_with_empty_node_t1 values(1, 1);
"""

sql """
insert into outer_join_with_empty_node_t2 values(1, 1);
"""

qt_select """
select * from outer_join_with_empty_node_t1 left join (select max(j1) over() as x from outer_join_with_empty_node_t2)a on outer_join_with_empty_node_t1.k1=a.x where 1=0;
"""

qt_select """
select * from outer_join_with_empty_node_t1 left join (select max(j1) over() as x from outer_join_with_empty_node_t2)a on outer_join_with_empty_node_t1.k1=a.x where 1=1;
"""

sql """
drop table if exists outer_join_with_empty_node_t1;
"""

sql """
drop table if exists outer_join_with_empty_node_t2;
"""
}