Extended constraint exception with constraint name and expression

This commit is contained in:
Gleb Novikov 2019-06-05 10:33:34 +03:00
parent 108b4c6a51
commit b63623d014
2 changed files with 11 additions and 7 deletions

View File

@ -1,14 +1,18 @@
#include <DataStreams/CheckConstraintsBlockOutputStream.h> #include <DataStreams/CheckConstraintsBlockOutputStream.h>
#include <Parsers/formatAST.h>
namespace DB namespace DB
{ {
void CheckConstraintsBlockOutputStream::write(const Block & block) void CheckConstraintsBlockOutputStream::write(const Block & block)
{ {
for (auto & constraint_expr: expressions) for (size_t i = 0; i < expressions.size(); ++i)
{
auto constraint_expr = expressions[i];
if (!checkConstraintOnBlock(block, constraint_expr)) if (!checkConstraintOnBlock(block, constraint_expr))
throw Exception{"Some constraints are not satisfied", ErrorCodes::QUERY_WAS_CANCELLED}; throw Exception{"Constraint " + constraints.constraints[i]->name + " is not satisfied at, constraint expression: " +
serializeAST(*(constraints.constraints[i]->expr), true), ErrorCodes::LOGICAL_ERROR};
}
output->write(block); output->write(block);
} }

View File

@ -4,7 +4,7 @@ CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. $CURDIR/../shell_config.sh . $CURDIR/../shell_config.sh
EXCEPTION_SUCCESS_TEXT=ok EXCEPTION_SUCCESS_TEXT=ok
$CLICKHOUSE_CLIENT --query="CREATE DATABASE IF NOT EXISTS test;"
$CLICKHOUSE_CLIENT --query="DROP TABLE IF EXISTS test_constraints;" $CLICKHOUSE_CLIENT --query="DROP TABLE IF EXISTS test_constraints;"
$CLICKHOUSE_CLIENT --query="CREATE TABLE test_constraints $CLICKHOUSE_CLIENT --query="CREATE TABLE test_constraints
@ -20,7 +20,7 @@ $CLICKHOUSE_CLIENT --query="INSERT INTO test_constraints VALUES (1, 2);"
$CLICKHOUSE_CLIENT --query="SELECT * FROM test_constraints;" $CLICKHOUSE_CLIENT --query="SELECT * FROM test_constraints;"
# This one must throw and exception # This one must throw and exception
EXCEPTION_TEXT="Some constraints are not satisfied" EXCEPTION_TEXT="Constraint b_constraint is not satisfied"
$CLICKHOUSE_CLIENT --query="INSERT INTO test_constraints VALUES (3, 4), (1, 0);" 2>&1 \ $CLICKHOUSE_CLIENT --query="INSERT INTO test_constraints VALUES (3, 4), (1, 0);" 2>&1 \
| grep -q "$EXCEPTION_TEXT" && echo "$EXCEPTION_SUCCESS_TEXT" || echo "Did not thrown an exception" | grep -q "$EXCEPTION_TEXT" && echo "$EXCEPTION_SUCCESS_TEXT" || echo "Did not thrown an exception"
$CLICKHOUSE_CLIENT --query="SELECT * FROM test_constraints;" $CLICKHOUSE_CLIENT --query="SELECT * FROM test_constraints;"
@ -38,13 +38,13 @@ $CLICKHOUSE_CLIENT --query="CREATE TABLE test_constraints
ENGINE = MergeTree ORDER BY (a);" ENGINE = MergeTree ORDER BY (a);"
# This one must throw an exception # This one must throw an exception
EXCEPTION_TEXT="Some constraints are not satisfied" EXCEPTION_TEXT="Constraint b_constraint is not satisfied"
$CLICKHOUSE_CLIENT --query="INSERT INTO test_constraints VALUES (1, 2);" 2>&1 \ $CLICKHOUSE_CLIENT --query="INSERT INTO test_constraints VALUES (1, 2);" 2>&1 \
| grep -q "$EXCEPTION_TEXT" && echo "$EXCEPTION_SUCCESS_TEXT" || echo "Did not thrown an exception" | grep -q "$EXCEPTION_TEXT" && echo "$EXCEPTION_SUCCESS_TEXT" || echo "Did not thrown an exception"
$CLICKHOUSE_CLIENT --query="SELECT * FROM test_constraints;" $CLICKHOUSE_CLIENT --query="SELECT * FROM test_constraints;"
# This one must throw an exception # This one must throw an exception
EXCEPTION_TEXT="Some constraints are not satisfied" EXCEPTION_TEXT="Constraint a_constraint is not satisfied"
$CLICKHOUSE_CLIENT --query="INSERT INTO test_constraints VALUES (5, 16), (10, 11);" 2>&1 \ $CLICKHOUSE_CLIENT --query="INSERT INTO test_constraints VALUES (5, 16), (10, 11);" 2>&1 \
| grep -q "$EXCEPTION_TEXT" && echo "$EXCEPTION_SUCCESS_TEXT" || echo "Did not thrown an exception" | grep -q "$EXCEPTION_TEXT" && echo "$EXCEPTION_SUCCESS_TEXT" || echo "Did not thrown an exception"
$CLICKHOUSE_CLIENT --query="SELECT * FROM test_constraints;" $CLICKHOUSE_CLIENT --query="SELECT * FROM test_constraints;"