Skip to content

Commit fb195bb

Browse files
Merge pull request ClickHouse#87639 from ClickHouse/backport/25.8/86967
Backport ClickHouse#86967 to 25.8: fix CTE deterministic functions
2 parents 89021a1 + 9caa1c4 commit fb195bb

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

src/Analyzer/Resolve/IdentifierResolveScope.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,6 @@ struct IdentifierResolveScope
198198
/// Node hash to mask id map
199199
std::shared_ptr<std::map<IQueryTreeNode::Hash, size_t>> projection_mask_map;
200200

201-
struct ResolvedFunctionsCache
202-
{
203-
FunctionOverloadResolverPtr resolver;
204-
FunctionBasePtr function_base;
205-
};
206-
207-
std::map<IQueryTreeNode::Hash, ResolvedFunctionsCache> functions_cache;
208-
209201
[[maybe_unused]] const IdentifierResolveScope * getNearestQueryScope() const;
210202

211203
IdentifierResolveScope * getNearestQueryScope();

src/Analyzer/Resolve/QueryAnalyzer.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3511,19 +3511,26 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi
35113511
FunctionOverloadResolverPtr function = UserDefinedExecutableFunctionFactory::instance().tryGet(function_name, scope.context, parameters); /// NOLINT(readability-static-accessed-through-instance)
35123512
bool is_executable_udf = true;
35133513

3514-
IdentifierResolveScope::ResolvedFunctionsCache * function_cache = nullptr;
3514+
ResolvedFunctionsCache * function_cache = nullptr;
35153515

35163516
if (!function)
35173517
{
35183518
/// This is a hack to allow a query like `select randConstant(), randConstant(), randConstant()`.
35193519
/// Function randConstant() would return the same value for the same arguments (in scope).
3520+
/// But we need to exclude getSetting() function because SETTINGS can change its result for every scope.
35203521

3521-
auto hash = function_node_ptr->getTreeHash();
3522-
function_cache = &scope.functions_cache[hash];
3523-
if (!function_cache->resolver)
3524-
function_cache->resolver = FunctionFactory::instance().tryGet(function_name, scope.context);
3522+
if (function_name != "getSetting" && function_name != "rowNumberInAllBlocks")
3523+
{
3524+
auto hash = function_node_ptr->getTreeHash();
3525+
3526+
function_cache = &functions_cache[hash];
3527+
if (!function_cache->resolver)
3528+
function_cache->resolver = FunctionFactory::instance().tryGet(function_name, scope.context);
35253529

3526-
function = function_cache->resolver;
3530+
function = function_cache->resolver;
3531+
}
3532+
else
3533+
function = FunctionFactory::instance().tryGet(function_name, scope.context);
35273534

35283535
is_executable_udf = false;
35293536
}

src/Analyzer/Resolve/QueryAnalyzer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <Core/NamesAndTypes.h>
1313

1414
#include <Parsers/NullsAction.h>
15+
#include <Functions/IFunction.h>
1516

1617
namespace DB
1718
{
@@ -296,6 +297,14 @@ class QueryAnalyzer
296297

297298
std::unordered_map<QueryTreeNodePtr, IdentifierResolveScope> node_to_scope_map;
298299

300+
struct ResolvedFunctionsCache
301+
{
302+
FunctionOverloadResolverPtr resolver;
303+
FunctionBasePtr function_base;
304+
};
305+
306+
std::map<IQueryTreeNode::Hash, ResolvedFunctionsCache> functions_cache;
307+
299308
const bool only_analyze;
300309
};
301310

src/Functions/getSetting.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class FunctionGetSetting : public IFunction, WithContext
3737

3838
String getName() const override { return name; }
3939
bool isDeterministic() const override { return false; }
40+
bool isDeterministicInScopeOfQuery() const override { return false; }
4041
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
4142
size_t getNumberOfArguments() const override { return (mode == ErrorHandlingMode::Default) ? 2 : 1 ; }
4243
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0, 1}; }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
1
3+
1 1 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set enable_analyzer = 1;
2+
3+
with (select randConstant()) as a select a = a;
4+
with (select now() + sleep(1)) as a select a = a;
5+
with (select randConstant()) as b select b = b, a = b, `a=a` from (with (select randConstant()) as a select a, a = a as `a=a`);

0 commit comments

Comments
 (0)