Skip to content

Commit

Permalink
[BugFix] Fix error message when substr/substring meets illegal interg…
Browse files Browse the repository at this point in the history
…er (#34921)

Signed-off-by: shuming.li <ming.moriarty@gmail.com>
  • Loading branch information
LiShuMing authored Nov 14, 2023
1 parent 31b9a33 commit 368c135
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public IntLiteral(long longValue, Type type) {
}

if (!valid) {
throw new ArithmeticException("Number out of range[" + value + "]. type: " + type);
throw new ArithmeticException("Number out of range[" + longValue + "]. type: " + type);
}

this.value = longValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5488,10 +5488,14 @@ private static List<Expr> getArgumentsForTimeSlice(Expr time, Expr value, String
private static void addArgumentUseTypeInt(Expr value, List<Expr> exprs) {
// IntLiteral may use TINYINT/SMALLINT/INT/BIGINT type
// but time_slice only support INT type when executed in BE
if (value instanceof IntLiteral) {
exprs.add(new IntLiteral(((IntLiteral) value).getValue(), Type.INT));
} else {
exprs.add(value);
try {
if (value instanceof IntLiteral) {
exprs.add(new IntLiteral(((IntLiteral) value).getValue(), Type.INT));
} else {
exprs.add(value);
}
} catch (Exception e) {
throw new IllegalArgumentException(String.format("Cast argument %s to int type failed.", value.toSql()));
}
}

Expand Down
75 changes: 75 additions & 0 deletions test/sql/test_function/R/test_substr
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
-- name: test_substr
create table t1 (id int, v bigint) distributed by hash(id) properties ("replication_num" = "1");
-- result:
-- !result
select SUBSTR('', 9223372036854775807) ;
-- result:
E: (1064, 'Unexpected exception: Cast argument 9223372036854775807 to int type failed.')
-- !result
select SUBSTR('', 9223372036854775807, 465254298) ;
-- result:
E: (1064, 'Unexpected exception: Cast argument 9223372036854775807 to int type failed.')
-- !result
select SUBSTR('', -9223372036854775807, 465254298) ;
-- result:
E: (1064, 'Unexpected exception: Cast argument -9223372036854775807 to int type failed.')
-- !result
select SUBSTR('', 9223372036854775806, 465254298) ;
-- result:
E: (1064, 'Unexpected exception: Cast argument 9223372036854775806 to int type failed.')
-- !result
select SUBSTRING('', 9223372036854775807) ;
-- result:
E: (1064, 'Unexpected exception: Cast argument 9223372036854775807 to int type failed.')
-- !result
select SUBSTRING('', 9223372036854775807, 465254298) ;
-- result:
E: (1064, 'Unexpected exception: Cast argument 9223372036854775807 to int type failed.')
-- !result
select SUBSTRING('', -9223372036854775807, 465254298) ;
-- result:
E: (1064, 'Unexpected exception: Cast argument -9223372036854775807 to int type failed.')
-- !result
select SUBSTRING('', 9223372036854775806, 465254298) ;
-- result:
E: (1064, 'Unexpected exception: Cast argument 9223372036854775806 to int type failed.')
-- !result
insert into t1 values(1, 9223372036854775807), (2, -9223372036854775807), (3, 9223372036854775806);
-- result:
-- !result
select SUBSTR('', v) from t1;
-- result:
None
None
None
-- !result
select SUBSTR('', v, id) from t1;
-- result:
None
None
None
-- !result
select SUBSTR('STARROCKS', v, id) from t1;
-- result:
None
None
None
-- !result
select SUBSTRING('', v) from t1;
-- result:
None
None
None
-- !result
select SUBSTRING('', v, id) from t1;
-- result:
None
None
None
-- !result
select SUBSTRING('STARROCKS', v, id) from t1;
-- result:
None
None
None
-- !result
23 changes: 23 additions & 0 deletions test/sql/test_function/T/test_substr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- name: test_substr

create table t1 (id int, v bigint) distributed by hash(id) properties ("replication_num" = "1");

select SUBSTR('', 9223372036854775807) ;
select SUBSTR('', 9223372036854775807, 465254298) ;
select SUBSTR('', -9223372036854775807, 465254298) ;
select SUBSTR('', 9223372036854775806, 465254298) ;

select SUBSTRING('', 9223372036854775807) ;
select SUBSTRING('', 9223372036854775807, 465254298) ;
select SUBSTRING('', -9223372036854775807, 465254298) ;
select SUBSTRING('', 9223372036854775806, 465254298) ;

insert into t1 values(1, 9223372036854775807), (2, -9223372036854775807), (3, 9223372036854775806);

select SUBSTR('', v) from t1;
select SUBSTR('', v, id) from t1;
select SUBSTR('STARROCKS', v, id) from t1;

select SUBSTRING('', v) from t1;
select SUBSTRING('', v, id) from t1;
select SUBSTRING('STARROCKS', v, id) from t1;

0 comments on commit 368c135

Please sign in to comment.