Skip to content

Commit cb4833a

Browse files
authored
Merge pull request ClickHouse#79593 from yariks5s/caseWithExpression-arguments-fix
Forbid incorrect use-case for `CaseWithExpression`
2 parents 5aa7dee + f1a55f2 commit cb4833a

File tree

4 files changed

+8
-1
lines changed

4 files changed

+8
-1
lines changed

src/Functions/caseWithExpression.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace DB
1111
namespace ErrorCodes
1212
{
1313
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
14+
extern const int BAD_ARGUMENTS;
1415
}
1516

1617
namespace
@@ -38,6 +39,11 @@ class FunctionCaseWithExpression : public IFunction
3839
if (args.empty())
3940
throw Exception(ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION, "Function {} expects at least 1 arguments", getName());
4041

42+
/// We expect an even number of arguments, with the first argument being the expression,
43+
/// and the last argument being the ELSE branch, with the rest being pairs of WHEN and THEN branches.
44+
if (args.size() < 4 || args.size() % 2 != 0)
45+
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Function {} expects an even number of arguments: (expr, when1, then1, ..., else)", getName());
46+
4147
/// See the comments in executeImpl() to understand why we actually have to
4248
/// get the return type of a transform function.
4349

tests/queries/0_stateless/03390_non_constant_case.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ END = 1;
3535
SELECT DISTINCT caseWithExpression(1.1, toNullable(0.1), 'a', 1.1, 'b', materialize(2.1), toFixedString('c', 1), 'default' ) AS f;
3636

3737
SELECT
38-
caseWithExpression(NULL, materialize(NULL), NULL) AS f1,
38+
caseWithExpression(NULL, materialize(NULL), NULL, NULL) AS f1,
3939
if(NULL, toDateTimeOrZero(NULL), NULL) AS f2
4040
FROM numbers(1);
4141

tests/queries/0_stateless/03444_case_with_expression_exception.reference

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT caseWithExpression('C', 'A', true, 'B', false); -- { serverError BAD_ARGUMENTS }

0 commit comments

Comments
 (0)