Skip to content

Commit e05283d

Browse files
Merge pull request #12060 from ClickHouse/fix-12053
Check wrong type for filter.
2 parents cf1967b + 3f51419 commit e05283d

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

src/DataTypes/DataTypeNullable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class DataTypeNullable final : public IDataType
9090
bool canBeComparedWithCollation() const override { return nested_data_type->canBeComparedWithCollation(); }
9191
bool canBeUsedAsVersion() const override { return false; }
9292
bool isSummable() const override { return nested_data_type->isSummable(); }
93-
bool canBeUsedInBooleanContext() const override { return nested_data_type->canBeUsedInBooleanContext(); }
93+
bool canBeUsedInBooleanContext() const override { return nested_data_type->canBeUsedInBooleanContext() || onlyNull(); }
9494
bool haveMaximumSizeOfValue() const override { return nested_data_type->haveMaximumSizeOfValue(); }
9595
size_t getMaximumSizeOfValueInMemory() const override { return 1 + nested_data_type->getMaximumSizeOfValueInMemory(); }
9696
bool isNullable() const override { return true; }

src/Interpreters/ExpressionAnalyzer.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ namespace ErrorCodes
7878
extern const int ILLEGAL_PREWHERE;
7979
extern const int LOGICAL_ERROR;
8080
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
81+
extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER;
8182
}
8283

8384
namespace
@@ -636,6 +637,11 @@ bool SelectQueryExpressionAnalyzer::appendPrewhere(
636637
step.required_output.push_back(prewhere_column_name);
637638
step.can_remove_required_output.push_back(true);
638639

640+
auto filter_type = step.actions->getSampleBlock().getByName(prewhere_column_name).type;
641+
if (!filter_type->canBeUsedInBooleanContext())
642+
throw Exception("Invalid type for filter in PREWHERE: " + filter_type->getName(),
643+
ErrorCodes::ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER);
644+
639645
{
640646
/// Remove unused source_columns from prewhere actions.
641647
auto tmp_actions = std::make_shared<ExpressionActions>(sourceColumns(), context);
@@ -716,11 +722,17 @@ bool SelectQueryExpressionAnalyzer::appendWhere(ExpressionActionsChain & chain,
716722

717723
ExpressionActionsChain::Step & step = chain.lastStep(sourceColumns());
718724

719-
step.required_output.push_back(select_query->where()->getColumnName());
725+
auto where_column_name = select_query->where()->getColumnName();
726+
step.required_output.push_back(where_column_name);
720727
step.can_remove_required_output = {true};
721728

722729
getRootActions(select_query->where(), only_types, step.actions);
723730

731+
auto filter_type = step.actions->getSampleBlock().getByName(where_column_name).type;
732+
if (!filter_type->canBeUsedInBooleanContext())
733+
throw Exception("Invalid type for filter in WHERE: " + filter_type->getName(),
734+
ErrorCodes::ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER);
735+
724736
return true;
725737
}
726738

src/Storages/MergeTree/MergeTreeBaseSelectProcessor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <Columns/FilterDescription.h>
77
#include <Common/typeid_cast.h>
88
#include <DataTypes/DataTypeNothing.h>
9+
#include <DataTypes/DataTypeNullable.h>
910

1011

1112
namespace DB
@@ -316,6 +317,12 @@ void MergeTreeBaseSelectProcessor::executePrewhereActions(Block & block, const P
316317
prewhere_info->alias_actions->execute(block);
317318

318319
prewhere_info->prewhere_actions->execute(block);
320+
auto & prewhere_column = block.getByName(prewhere_info->prewhere_column_name);
321+
322+
if (!prewhere_column.type->canBeUsedInBooleanContext())
323+
throw Exception("Invalid type for filter in PREWHERE: " + prewhere_column.type->getName(),
324+
ErrorCodes::LOGICAL_ERROR);
325+
319326
if (prewhere_info->remove_prewhere_column)
320327
block.erase(prewhere_info->prewhere_column_name);
321328
else

tests/queries/0_stateless/01356_wrong_filter-type_bug.reference

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
drop table if exists t0;
2+
3+
CREATE TABLE t0 (`c0` String, `c1` Int32 CODEC(NONE), `c2` Int32) ENGINE = MergeTree() ORDER BY tuple();
4+
insert into t0 values ('a', 1, 2);
5+
6+
SELECT t0.c2, t0.c1, t0.c0 FROM t0 PREWHERE t0.c0 ORDER BY ((t0.c2)>=(t0.c1)), (((- (((t0.c0)>(t0.c0))))) IS NULL) FORMAT TabSeparatedWithNamesAndTypes; -- {serverError 59}
7+
SELECT t0.c2, t0.c1, t0.c0 FROM t0 WHERE t0.c0 ORDER BY ((t0.c2)>=(t0.c1)), (((- (((t0.c0)>(t0.c0))))) IS NULL) FORMAT TabSeparatedWithNamesAndTypes settings optimize_move_to_prewhere=0; -- {serverError 59}
8+
9+
drop table if exists t0;

0 commit comments

Comments
 (0)