Skip to content

Commit 8d6e6d6

Browse files
Merge branch 'fix-inconsistent-formatting-of-explain-in-subqueries' into lazy-primary-key-loading
2 parents c083498 + 427caf8 commit 8d6e6d6

21 files changed

+113
-46
lines changed

docker/test/base/setup_export_logs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function setup_logs_replication
190190
echo -e "Creating remote destination table ${table}_${hash} with statement:\n${statement}" >&2
191191

192192
echo "$statement" | clickhouse-client --database_replicated_initial_query_timeout_sec=10 \
193-
--distributed_ddl_task_timeout=30 \
193+
--distributed_ddl_task_timeout=30 --distributed_ddl_output_mode=throw_only_active \
194194
"${CONNECTION_ARGS[@]}" || continue
195195

196196
echo "Creating table system.${table}_sender" >&2

src/Analyzer/QueryNode.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,8 @@ ASTPtr QueryNode::toASTImpl(const ConvertToASTOptions & options) const
421421

422422
if (is_subquery)
423423
{
424-
auto subquery = std::make_shared<ASTSubquery>();
425-
424+
auto subquery = std::make_shared<ASTSubquery>(std::move(result_select_query));
426425
subquery->cte_name = cte_name;
427-
subquery->children.push_back(std::move(result_select_query));
428-
429426
return subquery;
430427
}
431428

src/Analyzer/UnionNode.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,8 @@ ASTPtr UnionNode::toASTImpl(const ConvertToASTOptions & options) const
185185

186186
if (is_subquery)
187187
{
188-
auto subquery = std::make_shared<ASTSubquery>();
189-
188+
auto subquery = std::make_shared<ASTSubquery>(std::move(select_with_union_query));
190189
subquery->cte_name = cte_name;
191-
subquery->children.push_back(std::move(select_with_union_query));
192-
193190
return subquery;
194191
}
195192

src/Common/FieldVisitorConvertToNumber.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class FieldVisitorConvertToNumber : public StaticVisitor<T>
9191
if constexpr (std::is_floating_point_v<T>)
9292
return x.getValue().template convertTo<T>() / x.getScaleMultiplier().template convertTo<T>();
9393
else
94-
return (x.getValue() / x.getScaleMultiplier()). template convertTo<T>();
94+
return (x.getValue() / x.getScaleMultiplier()).template convertTo<T>();
9595
}
9696

9797
T operator() (const AggregateFunctionStateData &) const

src/Core/AccurateComparison.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ bool notEqualsOp(A a, B b)
152152
}
153153

154154
/// Converts numeric to an equal numeric of other type.
155-
/// When `strict` is `true` check that result exactly same as input, otherwise just check overflow
155+
/// When `strict` is `true` check that result exactly the same as input, otherwise just check overflow
156156
template <typename From, typename To, bool strict = true>
157157
inline bool NO_SANITIZE_UNDEFINED convertNumeric(From value, To & result)
158158
{

src/Core/SettingsFields.cpp

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include <Core/SettingsFields.h>
2-
32
#include <Core/Field.h>
3+
#include <Core/AccurateComparison.h>
44
#include <Common/getNumberOfPhysicalCPUCores.h>
5-
#include <Common/FieldVisitorConvertToNumber.h>
65
#include <Common/logger_useful.h>
76
#include <DataTypes/DataTypeMap.h>
87
#include <DataTypes/DataTypeString.h>
@@ -13,13 +12,15 @@
1312

1413
#include <cmath>
1514

15+
1616
namespace DB
1717
{
1818
namespace ErrorCodes
1919
{
2020
extern const int SIZE_OF_FIXED_STRING_DOESNT_MATCH;
2121
extern const int CANNOT_PARSE_BOOL;
2222
extern const int CANNOT_PARSE_NUMBER;
23+
extern const int CANNOT_CONVERT_TYPE;
2324
}
2425

2526

@@ -48,9 +49,51 @@ namespace
4849
T fieldToNumber(const Field & f)
4950
{
5051
if (f.getType() == Field::Types::String)
52+
{
5153
return stringToNumber<T>(f.get<const String &>());
54+
}
55+
else if (f.getType() == Field::Types::UInt64)
56+
{
57+
T result;
58+
if (!accurate::convertNumeric(f.get<UInt64>(), result))
59+
throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Field value {} is out of range of {} type", f, demangle(typeid(T).name()));
60+
return result;
61+
}
62+
else if (f.getType() == Field::Types::Int64)
63+
{
64+
T result;
65+
if (!accurate::convertNumeric(f.get<Int64>(), result))
66+
throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Field value {} is out of range of {} type", f, demangle(typeid(T).name()));
67+
return result;
68+
}
69+
else if (f.getType() == Field::Types::Bool)
70+
{
71+
return T(f.get<bool>());
72+
}
73+
else if (f.getType() == Field::Types::Float64)
74+
{
75+
Float64 x = f.get<Float64>();
76+
if constexpr (std::is_floating_point_v<T>)
77+
{
78+
return T(x);
79+
}
80+
else
81+
{
82+
if (!isFinite(x))
83+
{
84+
/// Conversion of infinite values to integer is undefined.
85+
throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Cannot convert infinite value to integer type");
86+
}
87+
else if (x > Float64(std::numeric_limits<T>::max()) || x < Float64(std::numeric_limits<T>::lowest()))
88+
{
89+
throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Cannot convert out of range floating point value to integer type");
90+
}
91+
else
92+
return T(x);
93+
}
94+
}
5295
else
53-
return applyVisitor(FieldVisitorConvertToNumber<T>(), f);
96+
throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Invalid value {} of the setting, which needs {}", f, demangle(typeid(T).name()));
5497
}
5598

5699
Map stringToMap(const String & str)
@@ -174,7 +217,7 @@ namespace
174217
if (f.getType() == Field::Types::String)
175218
return stringToMaxThreads(f.get<const String &>());
176219
else
177-
return applyVisitor(FieldVisitorConvertToNumber<UInt64>(), f);
220+
return fieldToNumber<UInt64>(f);
178221
}
179222
}
180223

src/Interpreters/TreeRewriter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,7 @@ struct ExistsExpressionData
262262
select_with_union_query->list_of_selects->children.push_back(std::move(select_query));
263263
select_with_union_query->children.push_back(select_with_union_query->list_of_selects);
264264

265-
auto new_subquery = std::make_shared<ASTSubquery>();
266-
new_subquery->children.push_back(select_with_union_query);
265+
auto new_subquery = std::make_shared<ASTSubquery>(std::move(select_with_union_query));
267266

268267
auto function = makeASTFunction("in", std::make_shared<ASTLiteral>(1u), new_subquery);
269268
func = *function;

src/Interpreters/executeQuery.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ static std::tuple<ASTPtr, BlockIO> executeQueryImpl(
721721
/// Verify that AST formatting is consistent:
722722
/// If you format AST, parse it back, and format it again, you get the same string.
723723

724-
String formatted1 = ast->formatForErrorMessage();
724+
String formatted1 = ast->formatWithPossiblyHidingSensitiveData(0, true, true);
725725

726726
ASTPtr ast2 = parseQuery(parser,
727727
formatted1.data(),
@@ -730,7 +730,7 @@ static std::tuple<ASTPtr, BlockIO> executeQueryImpl(
730730

731731
chassert(ast2);
732732

733-
String formatted2 = ast2->formatForErrorMessage();
733+
String formatted2 = ast2->formatWithPossiblyHidingSensitiveData(0, true, true);
734734

735735
if (formatted1 != formatted2)
736736
throw Exception(ErrorCodes::LOGICAL_ERROR,

src/Parsers/ASTSelectWithUnionQuery.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ void ASTSelectWithUnionQuery::formatQueryImpl(const FormatSettings & settings, F
7171
}
7272
else
7373
{
74-
auto sub_query = std::make_shared<ASTSubquery>();
75-
sub_query->children.push_back(*it);
74+
auto sub_query = std::make_shared<ASTSubquery>(*it);
7675
sub_query->formatImpl(settings, state, frame);
7776
}
7877
}

src/Parsers/ASTSubquery.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class ASTSubquery : public ASTWithAlias
2626
return clone;
2727
}
2828

29+
ASTSubquery() = default;
30+
31+
ASTSubquery(ASTPtr child)
32+
{
33+
children.emplace_back(std::move(child));
34+
}
35+
2936
void updateTreeHashImpl(SipHash & hash_state, bool ignore_aliases) const override;
3037
String getAliasOrColumnName() const override;
3138
String tryGetAlias() const override;

0 commit comments

Comments
 (0)