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

[Bug](lead) fix wrong child expression of lead function #12587

Merged
merged 2 commits into from
Sep 15, 2022
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 @@ -691,7 +691,10 @@ private void standardize(Analyzer analyzer) throws AnalysisException {
Type type = getFnCall().getChildren().get(2).getType();

try {
getFnCall().uncheckedCastChild(getFnCall().getChildren().get(0).getType(), 2);
if (!Type.matchExactType(getFnCall().getChildren().get(0).getType(),
getFnCall().getChildren().get(2).getType())) {
getFnCall().uncheckedCastChild(getFnCall().getChildren().get(0).getType(), 2);
}
} catch (Exception e) {
LOG.warn("", e);
throw new AnalysisException("Convert type error in offset fn(default value); old_type="
Expand Down
20 changes: 1 addition & 19 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package org.apache.doris.analysis;

import org.apache.doris.catalog.ArrayType;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.Function;
import org.apache.doris.catalog.FunctionSet;
Expand Down Expand Up @@ -269,24 +268,7 @@ public void analyze() throws AnalysisException {
Type childType = getChild(0).getType();

// this cast may result in loss of precision, but the user requested it
if (childType.matchesType(type)) {
if (PrimitiveType.typeWithPrecision.contains(type.getPrimitiveType())) {
// For types which has precision and scale, we also need to check quality between precisions and scales
if ((((ScalarType) type).decimalPrecision()
== ((ScalarType) childType).decimalPrecision()) && (((ScalarType) type).decimalScale()
== ((ScalarType) childType).decimalScale())) {
noOp = true;
}
} else if (type.isArrayType()) {
// For types array, we also need to check contains null for case like
// cast(array<not_null(int)> as array<int>)
if (((ArrayType) type).getContainsNull() == ((ArrayType) childType).getContainsNull()) {
noOp = true;
}
} else {
noOp = true;
}
}
noOp = Type.matchExactType(childType, type);

if (noOp) {
return;
Expand Down
23 changes: 23 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -1637,4 +1637,27 @@ public int getIndexSize() {
return this.getPrimitiveType().getOlapColumnIndexSize();
}
}

// Whether `type1` matches the exact type of `type2`.
public static boolean matchExactType(Type type1, Type type2) {
if (type1.matchesType(type2)) {
if (PrimitiveType.typeWithPrecision.contains(type2.getPrimitiveType())) {
// For types which has precision and scale, we also need to check quality between precisions and scales
if ((((ScalarType) type2).decimalPrecision()
== ((ScalarType) type1).decimalPrecision()) && (((ScalarType) type2).decimalScale()
== ((ScalarType) type1).decimalScale())) {
return true;
}
} else if (type2.isArrayType()) {
// For types array, we also need to check contains null for case like
// cast(array<not_null(int)> as array<int>)
if (((ArrayType) type2).getContainsNull() == ((ArrayType) type1).getContainsNull()) {
return true;
}
} else {
return true;
}
}
return false;
}
}
10 changes: 10 additions & 0 deletions regression-test/data/correctness_p0/test_lag_lead_window.out
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ 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 --
c 2022-09-06T00:00:02 2022-09-06T00:00:01
b 2022-09-06T00:00:01 2022-09-06T00:00
a 2022-09-06T00:00 2022-09-06T00:00

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

11 changes: 11 additions & 0 deletions regression-test/suites/correctness_p0/test_lag_lead_window.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,15 @@ suite("test_lag_lead_window") {
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; """
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; """
sql """ DROP TABLE IF EXISTS test1 """
}