Don't unfold non deterministic expressions

This commit is contained in:
alesapin 2018-10-24 14:31:23 +03:00
parent 949ff9e346
commit 070d75c7c9
3 changed files with 31 additions and 1 deletions

View File

@ -206,8 +206,14 @@ void ExpressionAction::prepare(Block & sample_block, const Settings & settings)
if (auto * prepared_function = dynamic_cast<PreparedFunctionImpl *>(function.get()))
prepared_function->createLowCardinalityResultCache(settings.max_threads);
bool compile_expressions = false;
#if USE_EMBEDDED_COMPILER
compile_expressions = settings.compile_expressions;
#endif
/// If all arguments are constants, and function is suitable to be executed in 'prepare' stage - execute function.
if (all_const && function_base->isSuitableForConstantFolding())
/// But if we compile expressions compiled version of this function maybe placed in cache,
/// so we don't want to unfold non deterministic functions
if (all_const && function_base->isSuitableForConstantFolding() && (!compile_expressions || function_base->isDeterministic()))
{
function->execute(sample_block, arguments, result_position, sample_block.rows());

View File

@ -0,0 +1,20 @@
SET compile_expressions = 1;
SET min_count_to_compile = 1;
DROP TABLE IF EXISTS test.time_table;
CREATE TABLE test.time_table(timecol DateTime, value Int32) ENGINE = MergeTree order by tuple();
INSERT INTO test.time_table VALUES (now() - 5, 5), (now() - 3, 3);
SELECT COUNT() from test.time_table WHERE value < now() - 1 AND value != 0 AND modulo(value, 2) != 0 AND timecol < now() - 1;
SELECT sleep(3);
INSERT INTO test.time_table VALUES (now(), 101);
SELECT sleep(3);
SELECT COUNT() from test.time_table WHERE value < now() - 1 AND value != 0 AND modulo(value, 2) != 0 AND timecol < now() - 1;
DROP TABLE IF EXISTS test.time_table;