Skip to content

Commit

Permalink
[Fix](Planner) fix varchar does not show real length apache#25171 (ap…
Browse files Browse the repository at this point in the history
  • Loading branch information
LiBinfeng-01 authored and gnehil committed Dec 4, 2023
1 parent 268aee3 commit 181c01b
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ public String toString() {
return "TIMEV2(" + scale + ")";
} else if (type == PrimitiveType.VARCHAR) {
if (isWildcardVarchar()) {
return "VARCHAR(*)";
return "VARCHAR(" + MAX_VARCHAR_LENGTH + ")";
}
return "VARCHAR(" + len + ")";
} else if (type == PrimitiveType.STRING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
import org.apache.doris.nereids.trees.plans.logical.LogicalUnion;
import org.apache.doris.nereids.trees.plans.logical.UsingJoin;
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.nereids.types.VarcharType;
import org.apache.doris.nereids.types.coercion.CharacterType;
import org.apache.doris.nereids.util.ExpressionUtils;
import org.apache.doris.policy.FilterType;
Expand Down Expand Up @@ -1119,6 +1120,9 @@ public Expression visitCast(DorisParser.CastContext ctx) {
Expression cast = ParserUtils.withOrigin(ctx, () ->
new Cast(getExpression(ctx.expression()), dataType, true));
if (dataType.isStringLikeType() && ((CharacterType) dataType).getLen() >= 0) {
if (dataType.isVarcharType() && ((VarcharType) dataType).isWildcardVarchar()) {
return cast;
}
List<Expression> args = ImmutableList.of(
cast,
new TinyIntLiteral((byte) 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public class VarcharType extends CharacterType {

public static final VarcharType SYSTEM_DEFAULT = new VarcharType(-1);
public static final int MAX_VARCHAR_LENGTH = ScalarType.MAX_VARCHAR_LENGTH;

public VarcharType(int len) {
super(len);
Expand Down Expand Up @@ -66,7 +67,7 @@ public DataType defaultConcreteType() {
@Override
public String toSql() {
if (len == -1) {
return "VARCHAR(*)";
return "VARCHAR(" + MAX_VARCHAR_LENGTH + ")";
}
return "VARCHAR(" + len + ")";
}
Expand All @@ -84,4 +85,8 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(super.hashCode(), len);
}

public boolean isWildcardVarchar() {
return len == -1 || len == MAX_VARCHAR_LENGTH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void test() throws Exception {

queryStr = "select db1.id_masking(k1) from db1.tbl1";
Assert.assertTrue(
dorisAssert.query(queryStr).explainQuery().contains("concat(left(CAST(CAST(k1 AS BIGINT) AS VARCHAR(*)), 3), '****', right(CAST(CAST(k1 AS BIGINT) AS VARCHAR(*)), 4))"));
dorisAssert.query(queryStr).explainQuery().contains("concat(left(CAST(CAST(k1 AS BIGINT) AS VARCHAR(65533)), 3), '****', right(CAST(CAST(k1 AS BIGINT) AS VARCHAR(65533)), 4))"));

// create alias function with cast
// cast any type to decimal with specific precision and scale
Expand Down Expand Up @@ -149,7 +149,7 @@ public void test() throws Exception {

// cast any type to varchar with fixed length
createFuncStr = "create alias function db1.varchar(all) with parameter(text) as "
+ "cast(text as varchar(*));";
+ "cast(text as varchar(65533));";
createFunctionStmt = (CreateFunctionStmt) UtFrameUtils.parseAndAnalyzeStmt(createFuncStr, ctx);
Env.getCurrentEnv().createFunction(createFunctionStmt);

Expand All @@ -172,7 +172,7 @@ public void test() throws Exception {
Assert.assertTrue(constExprLists.get(0).get(0) instanceof StringLiteral);

queryStr = "select db1.varchar(k1, 4) from db1.tbl1;";
Assert.assertTrue(dorisAssert.query(queryStr).explainQuery().contains("CAST(`k1` AS VARCHAR(*))"));
Assert.assertTrue(dorisAssert.query(queryStr).explainQuery().contains("CAST(`k1` AS VARCHAR(65533))"));

// cast any type to char with fixed length
createFuncStr = "create alias function db1.to_char(all, int) with parameter(text, length) as "
Expand Down Expand Up @@ -235,7 +235,7 @@ public void testCreateGlobalFunction() throws Exception {

queryStr = "select id_masking(k1) from db2.tbl1";
Assert.assertTrue(
dorisAssert.query(queryStr).explainQuery().contains("concat(left(CAST(CAST(k1 AS BIGINT) AS VARCHAR(*)), 3), '****', right(CAST(CAST(k1 AS BIGINT) AS VARCHAR(*)), 4))"));
dorisAssert.query(queryStr).explainQuery().contains("concat(left(CAST(CAST(k1 AS BIGINT) AS VARCHAR(65533)), 3), '****', right(CAST(CAST(k1 AS BIGINT) AS VARCHAR(65533)), 4))"));

// 4. create alias function with cast
// cast any type to decimal with specific precision and scale
Expand Down Expand Up @@ -270,7 +270,7 @@ public void testCreateGlobalFunction() throws Exception {
testFunctionQuery(ctx, queryStr, true);

queryStr = "select varchar(k1, 4) from db2.tbl1;";
Assert.assertTrue(dorisAssert.query(queryStr).explainQuery().contains("CAST(`k1` AS VARCHAR(*))"));
Assert.assertTrue(dorisAssert.query(queryStr).explainQuery().contains("CAST(`k1` AS VARCHAR(65533))"));

// 6. cast any type to char with fixed length
createFuncStr = "create global alias function db2.to_char(all, int) with parameter(text, length) as "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ public void testNDVToHll() throws Exception {
String createUserTagMVSql = "create materialized view " + USER_TAG_MV_NAME + " as select user_id, "
+ "`" + FunctionSet.HLL_UNION + "`(" + FunctionSet.HLL_HASH + "(tag_id)) from " + USER_TAG_TABLE_NAME + " group by user_id;";
dorisAssert.withMaterializedView(createUserTagMVSql);
String query = "select ndv(tag_id) from " + USER_TAG_TABLE_NAME + ";";
String query = "select /*+ SET_VAR(enable_fallback_to_original_planner=false) */ndv(tag_id) from " + USER_TAG_TABLE_NAME + ";";
dorisAssert.query(query).explainContains(USER_TAG_MV_NAME, "hll_union_agg");
}

Expand All @@ -782,7 +782,7 @@ public void testApproxCountDistinctToHll() throws Exception {
String createUserTagMVSql = "create materialized view " + USER_TAG_MV_NAME + " as select user_id, "
+ "`" + FunctionSet.HLL_UNION + "`(" + FunctionSet.HLL_HASH + "(tag_id)) from " + USER_TAG_TABLE_NAME + " group by user_id;";
dorisAssert.withMaterializedView(createUserTagMVSql);
String query = "select approx_count_distinct(tag_id) from " + USER_TAG_TABLE_NAME + ";";
String query = "select /*+ SET_VAR(enable_fallback_to_original_planner=false) */ approx_count_distinct(tag_id) from " + USER_TAG_TABLE_NAME + ";";
dorisAssert.query(query).explainContains(USER_TAG_MV_NAME, "hll_union_agg");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,11 @@ public void testTypeCast() throws Exception {
// disable cast hll/bitmap to string
assertSQLPlanOrErrorMsgContains(
"select cast(id2 as varchar) from test.hll_table;",
"Invalid type cast of `id2` from HLL to VARCHAR(*)"
"Invalid type cast of `id2` from HLL to VARCHAR(65533)"
);
assertSQLPlanOrErrorMsgContains(
"select cast(id2 as varchar) from test.bitmap_table;",
"Invalid type cast of `id2` from BITMAP to VARCHAR(*)"
"Invalid type cast of `id2` from BITMAP to VARCHAR(65533)"
);
// disable implicit cast hll/bitmap to string
assertSQLPlanOrErrorMsgContains(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,27 +273,27 @@ public void testRewriteLikePredicate() throws Exception {
// tinyint
String sql = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from tb3 where k1 like '%4%';";
LOG.info("EXPLAIN:{}", dorisAssert.query(sql).explainQuery());
dorisAssert.query(sql).explainContains("CAST(`k1` AS VARCHAR(*)) LIKE '%4%'");
dorisAssert.query(sql).explainContains("CAST(`k1` AS VARCHAR(65533)) LIKE '%4%'");

// smallint
sql = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from tb3 where k2 like '%4%';";
LOG.info("EXPLAIN:{}", dorisAssert.query(sql).explainQuery());
dorisAssert.query(sql).explainContains("CAST(`k2` AS VARCHAR(*)) LIKE '%4%'");
dorisAssert.query(sql).explainContains("CAST(`k2` AS VARCHAR(65533)) LIKE '%4%'");

// int
sql = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from tb3 where k3 like '%4%';";
LOG.info("EXPLAIN:{}", dorisAssert.query(sql).explainQuery());
dorisAssert.query(sql).explainContains("CAST(`k3` AS VARCHAR(*)) LIKE '%4%'");
dorisAssert.query(sql).explainContains("CAST(`k3` AS VARCHAR(65533)) LIKE '%4%'");

// bigint
sql = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from tb3 where k4 like '%4%';";
LOG.info("EXPLAIN:{}", dorisAssert.query(sql).explainQuery());
dorisAssert.query(sql).explainContains("CAST(`k4` AS VARCHAR(*)) LIKE '%4%'");
dorisAssert.query(sql).explainContains("CAST(`k4` AS VARCHAR(65533)) LIKE '%4%'");

// largeint
sql = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from tb3 where k5 like '%4%';";
LOG.info("EXPLAIN:{}", dorisAssert.query(sql).explainQuery());
dorisAssert.query(sql).explainContains("CAST(`k5` AS VARCHAR(*)) LIKE '%4%'");
dorisAssert.query(sql).explainContains("CAST(`k5` AS VARCHAR(65533)) LIKE '%4%'");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ bigint_key BIGINT No true \N BLOOM_FILTER
char_50_key CHAR(50) No true \N BLOOM_FILTER
character_key VARCHAR(500) No true \N BLOOM_FILTER
char_key CHAR(1) No true \N BLOOM_FILTER
character_most_key VARCHAR(*) No true \N BLOOM_FILTER
character_most_key VARCHAR(65533) No true \N BLOOM_FILTER
decimal_key DECIMAL(20, 6) No true \N BLOOM_FILTER
decimal_most_key DECIMAL(27, 9) No true \N BLOOM_FILTER
decimal32_key DECIMAL(5, 1) No true \N BLOOM_FILTER
Expand All @@ -29,7 +29,7 @@ bigint_value BIGINT No false \N SUM
char_50_value CHAR(50) No false \N REPLACE
character_value VARCHAR(500) No false \N REPLACE
char_value CHAR(1) No false \N REPLACE
character_most_value VARCHAR(*) No false \N REPLACE
character_most_value VARCHAR(65533) No false \N REPLACE
decimal_value DECIMAL(20, 6) No false \N SUM
decimal_most_value DECIMAL(27, 9) No false \N SUM
decimal32_value DECIMAL(5, 1) No false \N SUM
Expand Down
4 changes: 2 additions & 2 deletions regression-test/data/ddl_p0/test_create_table_like.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !desc_create_table --
decimal_test DUP_KEYS name VARCHAR(*) VARCHAR(*) Yes true \N true
decimal_test DUP_KEYS name VARCHAR(65533) VARCHAR(65533) Yes true \N true
id SMALLINT SMALLINT Yes false \N NONE true
timestamp0 DECIMAL DECIMALV3(9, 0) Yes false \N NONE true
timestamp1 DECIMAL DECIMALV3(10, 0) Yes false \N NONE true
Expand All @@ -9,7 +9,7 @@ decimal_test DUP_KEYS name VARCHAR(*) VARCHAR(*) Yes true \N true
timestamp4 DECIMAL(10, 1) DECIMALV3(10, 1) Yes false \N NONE true

-- !desc_create_table_like --
decimal_test_like DUP_KEYS name VARCHAR(*) VARCHAR(*) Yes true \N true
decimal_test_like DUP_KEYS name VARCHAR(65533) VARCHAR(65533) Yes true \N true
id SMALLINT SMALLINT Yes false \N NONE true
timestamp0 DECIMAL DECIMALV3(9, 0) Yes false \N NONE true
timestamp1 DECIMAL DECIMALV3(10, 0) Yes false \N NONE true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ binary TEXT Yes false \N NONE
bit TEXT Yes false \N NONE
blob TEXT Yes false \N NONE
boolean TINYINT Yes false \N NONE
char VARCHAR(*) Yes false \N NONE
char VARCHAR(65533) Yes false \N NONE
date DATE Yes false \N NONE
datetime DATETIME Yes false \N NONE
decimal DECIMAL(12, 4) Yes false \N NONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,7 @@ bit_value BOOLEAN Yes false \N NONE
bitn_value TEXT Yes false \N NONE
bitnv_value TEXT Yes false \N NONE
box_value TEXT Yes false \N NONE
char_value VARCHAR(*) Yes true \N
char_value VARCHAR(65533) Yes true \N
cidr_value TEXT Yes false \N NONE
circle_value TEXT Yes false \N NONE
date_value DATE Yes false \N NONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ test1 UNIQUE_KEYS vid VARCHAR(1) VARCHAR(1) No true \N true
report_time INT INT No true \N true

mv_test UNIQUE_KEYS mv_report_time INT INT No true \N true `report_time`
mv_vid VARCHAR(*) VARCHAR(*) No true \N REPLACE true `vid`
mv_vid VARCHAR(65533) VARCHAR(65533) No true \N REPLACE true `vid`

2 changes: 1 addition & 1 deletion regression-test/data/nereids_syntax_p0/rollup/hll/hll.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ test_materialized_view_hll1 DUP_KEYS record_id INT INT Yes true \N true
sale_amt BIGINT BIGINT Yes false \N NONE true

amt_count AGG_KEYS mv_store_id INT INT Yes true \N true `store_id`
mva_HLL_UNION__hll_hash(CAST(`sale_amt` AS VARCHAR(*))) HLL HLL No false \N HLL_UNION true hll_hash(CAST(`sale_amt` AS VARCHAR(*)))
mva_HLL_UNION__hll_hash(CAST(`sale_amt` AS VARCHAR(65533))) HLL HLL No false \N HLL_UNION true hll_hash(CAST(`sale_amt` AS VARCHAR(65533)))

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_materialized_view_hll_with_light_sc1 DUP_KEYS record_id INT INT Yes true \N
sale_amt BIGINT BIGINT Yes false \N NONE true

amt_count1 AGG_KEYS mv_store_id INT INT Yes true \N true `store_id`
mva_HLL_UNION__hll_hash(CAST(`sale_amt` AS VARCHAR(*))) HLL HLL No false \N HLL_UNION true hll_hash(CAST(`sale_amt` AS VARCHAR(*)))
mva_HLL_UNION__hll_hash(CAST(`sale_amt` AS VARCHAR(65533))) HLL HLL No false \N HLL_UNION true hll_hash(CAST(`sale_amt` AS VARCHAR(65533)))

-- !sql --
1 1
Expand Down
2 changes: 1 addition & 1 deletion regression-test/data/rollup/test_materialized_view_hll.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_materialized_view_hll DUP_KEYS record_id INT INT Yes true \N true
sale_amt BIGINT BIGINT Yes false \N NONE true

amt_count AGG_KEYS mv_store_id INT INT Yes true \N true `store_id`
mva_HLL_UNION__hll_hash(CAST(`sale_amt` AS VARCHAR(*))) HLL HLL No false \N HLL_UNION true hll_hash(CAST(`sale_amt` AS VARCHAR(*)))
mva_HLL_UNION__hll_hash(CAST(`sale_amt` AS VARCHAR(65533))) HLL HLL No false \N HLL_UNION true hll_hash(CAST(`sale_amt` AS VARCHAR(65533)))

-- !sql --
1 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_materialized_view_hll_with_light_sc DUP_KEYS record_id INT INT Yes true \N
sale_amt BIGINT BIGINT Yes false \N NONE true

amt_count1 AGG_KEYS mv_store_id INT INT Yes true \N true `store_id`
mva_HLL_UNION__hll_hash(CAST(`sale_amt` AS VARCHAR(*))) HLL HLL No false \N HLL_UNION true hll_hash(CAST(`sale_amt` AS VARCHAR(*)))
mva_HLL_UNION__hll_hash(CAST(`sale_amt` AS VARCHAR(65533))) HLL HLL No false \N HLL_UNION true hll_hash(CAST(`sale_amt` AS VARCHAR(65533)))

-- !sql --
1 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.codehaus.groovy.runtime.IOGroovyMethods

suite ("testNDVToHll") {
sql """set enable_nereids_planner=true;"""
sql """set enable_fallback_to_original_planner=false;"""
sql """ DROP TABLE IF EXISTS user_tags; """

sql """ create table user_tags (
Expand Down
2 changes: 1 addition & 1 deletion regression-test/suites/view_p0/view_p0.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ suite("view_p0") {
"""

qt_sql "select * from test_varchar_view;"
qt_sql "select cast( id as varchar(*)) from test_view_table;"
qt_sql "select cast( id as varchar(65533)) from test_view_table;"

// array view
sql """DROP TABLE IF EXISTS test_array_tbl_1"""
Expand Down

0 comments on commit 181c01b

Please sign in to comment.