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

[Enhancement] fixup isNullable of cast #15380

Merged
merged 4 commits into from Dec 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -229,6 +229,10 @@ public boolean canHashPartition() {

@Override
public boolean isNullable() {
Expr fromExpr = getChild(0);
if (ScalarType.isImplicitlyCastable(fromExpr.getType(), getType(), true)) {
mofeiatwork marked this conversation as resolved.
Show resolved Hide resolved
return fromExpr.isNullable();
}
return true;
}

Expand Down
Expand Up @@ -16,6 +16,7 @@
package com.starrocks.sql.optimizer.operator.scalar;

import com.google.common.collect.Lists;
import com.starrocks.catalog.ScalarType;
import com.starrocks.catalog.Type;

import java.util.Objects;
Expand Down Expand Up @@ -43,6 +44,10 @@ public boolean isImplicit() {

@Override
public boolean isNullable() {
ScalarOperator fromOperator = getChild(0);
if (ScalarType.isImplicitlyCastable(fromOperator.getType(), getType(), true)) {
return fromOperator.isNullable();
}
return true;
}

Expand Down
Expand Up @@ -62,7 +62,7 @@ public static void setUp() throws Exception {
starRocksAssert.withDatabase("db1").useDatabase("db1");
starRocksAssert.withTable(createTblStmtStr);
starRocksAssert.withTable("CREATE TABLE `test_decimal_type6` (\n" +
" `dec_1_2` decimal32(2, 1) NOT NULL COMMENT \"\",\n" +
" `dec_2_1` decimal32(2, 1) NOT NULL COMMENT \"\",\n" +
" `dec_18_0` decimal64(18, 0) NOT NULL COMMENT \"\",\n" +
" `dec_18_18` decimal64(18, 18) NOT NULL COMMENT \"\",\n" +
" `dec_10_2` decimal64(10, 2) NOT NULL COMMENT \"\",\n" +
Expand All @@ -72,9 +72,9 @@ public static void setUp() throws Exception {
" dec_4_2 decimal64(4, 2),\n" +
" dec_5_1 decimal64(5, 1)\n" +
") ENGINE=OLAP\n" +
"DUPLICATE KEY(`dec_1_2`)\n" +
"DUPLICATE KEY(`dec_2_1`)\n" +
"COMMENT \"OLAP\"\n" +
"DISTRIBUTED BY HASH(`dec_1_2`) BUCKETS 10\n" +
"DISTRIBUTED BY HASH(`dec_2_1`) BUCKETS 10\n" +
"PROPERTIES (\n" +
"\"replication_num\" = \"1\",\n" +
"\"in_memory\" = \"false\",\n" +
Expand Down Expand Up @@ -317,7 +317,7 @@ public void testDecimal32Stddev() throws Exception {
String sql = "select stddev(col_decimal32p9s2) from db1.decimal_table";
String plan = UtFrameUtils.getVerboseFragmentPlan(ctx, sql);
String snippet = "aggregate: stddev[(cast([2: col_decimal32p9s2, DECIMAL32(9,2), false] as DOUBLE)); " +
"args: DOUBLE; result: DOUBLE; args nullable: true; result nullable: true]";
"args: DOUBLE; result: DOUBLE; args nullable: false; result nullable: true]";
Assert.assertTrue(plan.contains(snippet));
}

Expand All @@ -326,7 +326,7 @@ public void testDecimal32Variance() throws Exception {
String sql = "select variance(col_decimal32p9s2) from db1.decimal_table";
String plan = UtFrameUtils.getVerboseFragmentPlan(ctx, sql);
String snippet = "aggregate: variance[(cast([2: col_decimal32p9s2, DECIMAL32(9,2), false] as DOUBLE)); " +
"args: DOUBLE; result: DOUBLE; args nullable: true; result nullable: true]";
"args: DOUBLE; result: DOUBLE; args nullable: false; result nullable: true]";
Assert.assertTrue(plan.contains(snippet));
}

Expand Down Expand Up @@ -402,7 +402,7 @@ public void testDecimalAddRule() throws Exception {
// decimal(10, 2) + decimal(10, 2) = decimal(11, 2)
sql = "select count(`dec_10_2` + dec_10_2) from `test_decimal_type6`;";
plan = UtFrameUtils.getVerboseFragmentPlan(ctx, sql);
Assert.assertTrue(plan.contains("[12: cast, DECIMAL64(11,2), true] + [12: cast, DECIMAL64(11,2), true]"));
Assert.assertTrue(plan.contains("[12: cast, DECIMAL64(11,2), false] + [12: cast, DECIMAL64(11,2), false]"));

sql = "select count(`dec_10_2` + dec_12_10) from `test_decimal_type6`;";
plan = UtFrameUtils.getVerboseFragmentPlan(ctx, sql);
Expand All @@ -419,9 +419,9 @@ public void testDecimalAddRule() throws Exception {
plan = UtFrameUtils.getVerboseFragmentPlan(ctx, sql);
Assert.assertTrue(plan.contains(" 2 <-> 1000000000000000000.000000000000000001"));

sql = "select dec_1_2 + dec_1_2 from test_decimal_type6";
sql = "select dec_2_1 + dec_2_1 from test_decimal_type6";
plan = UtFrameUtils.getVerboseFragmentPlan(ctx, sql);
Assert.assertTrue(plan.contains("[11: cast, DECIMAL32(3,1), true] + [11: cast, DECIMAL32(3,1), true]"));
Assert.assertTrue(plan.contains("[11: cast, DECIMAL32(3,1), false] + [11: cast, DECIMAL32(3,1), false]"));
}

@Test
Expand Down Expand Up @@ -494,7 +494,7 @@ public void testDecimalNullableProperties() throws Exception {
plan = UtFrameUtils.getVerboseFragmentPlan(ctx, sql);
System.out.println("plan = " + plan);
Assert.assertTrue(plan.contains(
"round[(cast([2: dec_18_0, DECIMAL64(18,0), false] as DECIMAL128(18,0))); args: DECIMAL128; result: DECIMAL128(38,0); args nullable: true; result nullable: true]"));
"round[(cast([2: dec_18_0, DECIMAL64(18,0), false] as DECIMAL128(18,0))); args: DECIMAL128; result: DECIMAL128(38,0); args nullable: false; result nullable: true]"));
}

@Test
Expand Down
Expand Up @@ -539,7 +539,7 @@ public void testJoinOnExpression() throws Exception {
String plan = getCostExplain(sql);
assertContains(plan, "6:HASH JOIN\n" +
" | join op: INNER JOIN (PARTITIONED)\n" +
" | equal join conjunct: [29: cast, BIGINT, true] = [30: add, BIGINT, true]\n" +
" | equal join conjunct: [29: cast, BIGINT, false] = [30: add, BIGINT, false]\n" +
" | output columns: 14, 15, 20, 22\n" +
" | cardinality: 600000000");
assertContains(plan, "|----5:EXCHANGE\n" +
Expand All @@ -552,7 +552,7 @@ public void testJoinOnExpression() throws Exception {
"GROUP BY l_shipmode, l_shipinstruct, o_orderdate, o_orderstatus;";
plan = getCostExplain(sql);
Assert.assertTrue(
plan.contains("equal join conjunct: [29: multiply, BIGINT, true] = [30: add, BIGINT, true]\n" +
plan.contains("equal join conjunct: [29: multiply, BIGINT, false] = [30: add, BIGINT, false]\n" +
" | output columns: 14, 15, 20, 22\n" +
" | cardinality: 600000000"));
}
Expand Down
Expand Up @@ -1182,6 +1182,7 @@ public void runFileUnitTest(String filename, boolean debug) {
e.printStackTrace();
}
System.out.println("DEBUG MODE!");
System.out.println("DEBUG FILE: " + debugFile.getPath());
}

Pattern regex = Pattern.compile("\\[plan-(\\d+)]");
Expand Down
8 changes: 4 additions & 4 deletions fe/fe-core/src/test/java/com/starrocks/sql/plan/SetTest.java
Expand Up @@ -303,8 +303,8 @@ public void testUnionNullConstant() throws Exception {
" | output exprs:\n" +
" | [7, DECIMAL64(12,2), true]\n" +
" | child exprs:\n" +
" | [3, DECIMAL64(12,2), true]\n" +
" | [6, DECIMAL64(12,2), true]\n");
" | [3, DECIMAL64(12,2), false]\n" +
" | [6, DECIMAL64(12,2), false]\n");

sql = "select count(*) from (select cast('1.2' as decimal(5,2)) as c1 union all " +
"select cast('1.2' as decimal(10,0)) as c1) t group by t.c1";
Expand All @@ -313,8 +313,8 @@ public void testUnionNullConstant() throws Exception {
" | output exprs:\n" +
" | [7, DECIMAL64(12,2), true]\n" +
" | child exprs:\n" +
" | [3, DECIMAL64(12,2), true]\n" +
" | [6, DECIMAL64(12,2), true]");
" | [3, DECIMAL64(12,2), false]\n" +
" | [6, DECIMAL64(12,2), false]");
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions fe/fe-core/src/test/resources/sql/tpch-histogram-cost/q11.sql
Expand Up @@ -78,7 +78,7 @@ OutPut Exchange Id: 30
| cardinality: 1
|
10:AGGREGATE (update finalize)
| aggregate: sum[([20: expr, DOUBLE, true]); args: DOUBLE; result: DOUBLE; args nullable: true; result nullable: true]
| aggregate: sum[([20: expr, DOUBLE, false]); args: DOUBLE; result: DOUBLE; args nullable: false; result nullable: true]
| group by: [1: PS_PARTKEY, INT, false]
| cardinality: 3200000
| probe runtime filters:
Expand Down Expand Up @@ -165,7 +165,7 @@ OutPut Partition: UNPARTITIONED
OutPut Exchange Id: 22

21:AGGREGATE (update serialize)
| aggregate: sum[([41: expr, DOUBLE, true]); args: DOUBLE; result: DOUBLE; args nullable: true; result nullable: true]
| aggregate: sum[([41: expr, DOUBLE, false]); args: DOUBLE; result: DOUBLE; args nullable: false; result nullable: true]
| cardinality: 1
| column statistics:
| * sum-->[1.0, 9999000.0, 0.0, 8.0, 1.0] ESTIMATE
Expand Down