Skip to content

Commit 0260358

Browse files
authored
Merge pull request ClickHouse#11934 from ClickHouse/fix_some_logical_errors
Fix some logical errors
2 parents e5fc792 + 64aebb2 commit 0260358

File tree

8 files changed

+37
-3
lines changed

8 files changed

+37
-3
lines changed

src/Functions/hasColumnInTable.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace ErrorCodes
1717
{
1818
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
1919
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
20+
extern const int UNKNOWN_TABLE;
2021
}
2122

2223

@@ -110,6 +111,9 @@ void FunctionHasColumnInTable::executeImpl(Block & block, const ColumnNumbers &
110111
String table_name = get_string_from_block(arguments[arg++]);
111112
String column_name = get_string_from_block(arguments[arg++]);
112113

114+
if (table_name.empty())
115+
throw Exception("Table name is empty", ErrorCodes::UNKNOWN_TABLE);
116+
113117
bool has_column;
114118
if (host_name.empty())
115119
{

src/Interpreters/convertFieldToType.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,16 @@ Field convertFieldToType(const Field & from_value, const IDataType & to_type, co
329329
return convertFieldToTypeImpl(from_value, to_type, from_type_hint);
330330
}
331331

332+
Field convertFieldToTypeOrThrow(const Field & from_value, const IDataType & to_type, const IDataType * from_type_hint)
333+
{
334+
bool is_null = from_value.isNull();
335+
if (is_null && !to_type.isNullable())
336+
throw Exception(ErrorCodes::TYPE_MISMATCH, "Cannot convert NULL to {}", to_type.getName());
337+
Field converted = convertFieldToType(from_value, to_type, from_type_hint);
338+
if (!is_null && converted.isNull())
339+
throw Exception(ErrorCodes::ARGUMENT_OUT_OF_BOUND, "Cannot convert value{}: it cannot be represented as {}",
340+
from_type_hint ? " from " + from_type_hint->getName() : "", to_type.getName());
341+
return converted;
342+
}
332343

333344
}

src/Interpreters/convertFieldToType.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ class IDataType;
1717
*/
1818
Field convertFieldToType(const Field & from_value, const IDataType & to_type, const IDataType * from_type_hint = nullptr);
1919

20+
/// Does the same, but throws ARGUMENT_OUT_OF_BOUND if value does not fall into the range.
21+
Field convertFieldToTypeOrThrow(const Field & from_value, const IDataType & to_type, const IDataType * from_type_hint = nullptr);
22+
2023
}

src/TableFunctions/TableFunctionValues.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static void parseAndInsertValues(MutableColumns & res_columns, const ASTs & args
3838
{
3939
const auto & [value_field, value_type_ptr] = evaluateConstantExpression(args[i], context);
4040

41-
Field value = convertFieldToType(value_field, *sample_block.getByPosition(0).type, value_type_ptr.get());
41+
Field value = convertFieldToTypeOrThrow(value_field, *sample_block.getByPosition(0).type, value_type_ptr.get());
4242
res_columns[0]->insert(value);
4343
}
4444
}
@@ -51,11 +51,11 @@ static void parseAndInsertValues(MutableColumns & res_columns, const ASTs & args
5151
const Tuple & value_tuple = value_field.safeGet<Tuple>();
5252

5353
if (value_tuple.size() != sample_block.columns())
54-
throw Exception("Values size should match with number of columns", ErrorCodes::LOGICAL_ERROR);
54+
throw Exception("Values size should match with number of columns", ErrorCodes::BAD_ARGUMENTS);
5555

5656
for (size_t j = 0; j < value_tuple.size(); ++j)
5757
{
58-
Field value = convertFieldToType(value_tuple[j], *sample_block.getByPosition(j).type, value_types_tuple[j].get());
58+
Field value = convertFieldToTypeOrThrow(value_tuple[j], *sample_block.getByPosition(j).type, value_types_tuple[j].get());
5959
res_columns[j]->insert(value);
6060
}
6161
}

tests/queries/0_stateless/00386_has_column_in_table.reference

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
0
1313
0
1414
0
15+
0

tests/queries/0_stateless/00386_has_column_in_table.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,14 @@ SELECT hasColumnInTable(currentDatabase(), 'has_column_in_table', 'nest.not_exis
1818
SELECT hasColumnInTable('localhost', currentDatabase(), 'has_column_in_table', 'nest.not_existing');
1919
SELECT hasColumnInTable(currentDatabase(), 'has_column_in_table', 'not_existing');
2020
SELECT hasColumnInTable('localhost', currentDatabase(), 'has_column_in_table', 'not_existing');
21+
SELECT hasColumnInTable('system', 'one', '');
22+
23+
/* bad queries */
24+
SELECT hasColumnInTable('', '', ''); -- { serverError 60; }
25+
SELECT hasColumnInTable('', 't', 'c'); -- { serverError 81; }
26+
SELECT hasColumnInTable(currentDatabase(), '', 'c'); -- { serverError 60; }
27+
SELECT hasColumnInTable('d', 't', 's'); -- { serverError 81; }
28+
SELECT hasColumnInTable(currentDatabase(), 't', 's'); -- { serverError 60; }
29+
2130

2231
DROP TABLE has_column_in_table;

tests/queries/0_stateless/00975_values_list.reference

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ abracadabra
1111
23 23 23
1212
24 24 24
1313
1.6660 a b
14+
\N

tests/queries/0_stateless/00975_values_list.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ SELECT * FROM VALUES('s String', ('abra'), ('cadabra'), ('abracadabra'));
1111
SELECT * FROM VALUES('n UInt64, s String, ss String', (1 + 22, '23', toString(23)), (toUInt64('24'), '24', concat('2', '4')));
1212

1313
SELECT * FROM VALUES('a Decimal(4, 4), b String, c String', (divide(toDecimal32(5, 3), 3), 'a', 'b'));
14+
15+
SELECT * FROM VALUES('x Float64', toUInt64(-1)); -- { serverError 69; }
16+
SELECT * FROM VALUES('x Float64', NULL); -- { serverError 53; }
17+
SELECT * FROM VALUES('x Nullable(Float64)', NULL);
18+
1419
DROP TABLE values_list;

0 commit comments

Comments
 (0)