Skip to content

Commit

Permalink
separate errors and add test case errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lmatz committed Dec 27, 2024
1 parent 3a567d8 commit bdbc790
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
53 changes: 52 additions & 1 deletion e2e_test/batch/order/test_limit.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,45 @@ SELECT * FROM generate_series(0,3,1) as t(v) order by v limit NULL + '1'::bigint
2
3


statement error
SELECT * FROM generate_series(0,3,1) as t(v) order by v limit NULL + '1';
----
db error: ERROR: Failed to run the query

Caused by these errors (recent errors listed first):
1: Failed to bind expression: NULL + '1'
2: Bind error: function add(unknown, unknown) is not unique
HINT: Could not choose a best candidate function. You might need to add explicit type casts.


statement error
SELECT * FROM generate_series(0,3,1) as t(v) order by v limit NULL + '1'::jsonb;
----
db error: ERROR: Failed to run the query

Caused by these errors (recent errors listed first):
1: Failed to bind expression: NULL + CAST('1' AS JSONB)
2: function add(unknown, jsonb) does not exist



query I
SELECT * FROM generate_series(0,3,1) as t(v) order by v limit 2.1::Decimal;
----
0
1


statement error
SELECT * FROM generate_series(0,3,1) as t(v) order by v limit '1'::jsonb;
----
db error: ERROR: Failed to run the query

Caused by these errors (recent errors listed first):
1: Expr error
2: expects an integer or expression that can be evaluated to an integer after LIMIT



query I
Expand All @@ -247,11 +272,37 @@ SELECT * FROM generate_series(0,3,1) as t(v) order by v limit '2';
0
1

statement error

statement error
SELECT * FROM generate_series(0,3,1) as t(v) order by v limit '-2';
----
db error: ERROR: Failed to run the query

Caused by these errors (recent errors listed first):
1: Expr error
2: LIMIT must not be negative, but found: -2


statement error
SELECT * FROM generate_series(0,3,1) as t(v) order by v limit '2.2';
----
db error: ERROR: Failed to run the query

Caused by these errors (recent errors listed first):
1: Expr error
2: expects an integer or expression that can be evaluated to an integer after LIMIT,
but the evaluation of the expression returns error:Expr error: error while evaluating expression `str_parse('2.2')`


statement error
select 1 limit generate_series(1, 2);
----
db error: ERROR: Failed to run the query

Caused by these errors (recent errors listed first):
1: Expr error
2: expects an integer or expression that can be evaluated to an integer after LIMIT, but found non-const expression


# Subqueries that return negative values
statement error
Expand Down
22 changes: 15 additions & 7 deletions src/frontend/src/binder/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl Binder {
};
let limit_expr = limit.map(|expr| self.bind_expr(expr)).transpose()?;
let limit = if let Some(limit_expr) = limit_expr {
// wrong type error is handled here
let limit_cast_to_bigint = limit_expr.cast_assign(DataType::Int64).map_err(|_| {
RwError::from(ErrorCode::ExprError(
"expects an integer or expression that can be evaluated to an integer after LIMIT"
Expand All @@ -191,24 +192,31 @@ impl Binder {
})?;
let limit = match limit_cast_to_bigint.try_fold_const() {
Some(Ok(Some(datum))) => {
let value = datum.as_integral();
if value < 0 {
let value = datum.as_int64();
if *value < 0 {
return Err(ErrorCode::ExprError(
format!("LIMIT must not be negative, but found: {}", value).into(),
format!("LIMIT must not be negative, but found: {}", *value).into(),
)
.into());
}
value as u64
*value as u64
}
// If evaluated to NULL, we follow PG to treat NULL as no limit
Some(Ok(None)) => {
u64::MAX
}
// wrong type, not const, eval error all belongs to this branch
_ => return Err(ErrorCode::ExprError(
"expects an integer or expression that can be evaluated to an integer after LIMIT"
// not const error
None => return Err(ErrorCode::ExprError(
"expects an integer or expression that can be evaluated to an integer after LIMIT, but found non-const expression"
.into(),
).into()),
// eval error
Some(Err(e)) => {
return Err(ErrorCode::ExprError(
format!("expects an integer or expression that can be evaluated to an integer after LIMIT,\nbut the evaluation of the expression returns error:{}", e
).into(),
).into())
}
};
Some(limit)
} else {
Expand Down

0 comments on commit bdbc790

Please sign in to comment.