add short circuit setting and test

This commit is contained in:
jsc0218 2023-12-26 16:25:16 +00:00
parent e25eda91b0
commit 0e400ca7ae
5 changed files with 68 additions and 0 deletions

View File

@ -331,6 +331,7 @@ public:
settings.enable_lazy_execution_for_first_argument = false;
settings.enable_lazy_execution_for_common_descendants_of_arguments = false;
settings.force_enable_lazy_execution = false;
settings.enable_lazy_execution_for_third_argument = false;
return true;
}
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }

View File

@ -258,6 +258,9 @@ public:
/// Example: toTypeName(expr), even if expr contains functions that are not suitable for
/// lazy execution (because of their simplicity), we shouldn't execute them at all.
bool force_enable_lazy_execution;
/// Currently only useful for dictGetOrDefault, the 3rd argument of it should always be
/// calculated, so set it to false in dictGetOrDefault, while true in other cases.
bool enable_lazy_execution_for_third_argument = true;
};
/** Function is called "short-circuit" if it's arguments can be evaluated lazily

View File

@ -190,6 +190,18 @@ static void setLazyExecutionInfo(
lazy_execution_info.can_be_lazy_executed = false;
}
/// Currently only used in dictGetOrDefault.
if (parent->children.size() == 4 && node == parent->children[2] &&
!short_circuit_nodes.at(parent).enable_lazy_execution_for_third_argument)
{
/// We shouldn't add 2 index in node info in this case.
indexes.erase(2);
/// Disable lazy execution for current node only if it's disabled for short-circuit node,
/// because we can have nested short-circuit nodes.
if (!lazy_execution_infos[parent].can_be_lazy_executed)
lazy_execution_info.can_be_lazy_executed = false;
}
lazy_execution_info.short_circuit_ancestors_info[parent].insert(indexes.begin(), indexes.end());
}
else

View File

@ -0,0 +1,6 @@
Flat dictionary
('zero','zero')
('zero','zero')
Hashed dictionary
('zero','zero')
('zero','zero')

View File

@ -0,0 +1,46 @@
-- Tags: no-parallel
DROP TABLE IF EXISTS dictionary_source_table;
CREATE TABLE dictionary_source_table
(
id UInt64,
v1 String,
v2 Nullable(String)
) ENGINE=TinyLog;
INSERT INTO dictionary_source_table VALUES (0, 'zero', 'zero'), (1, 'one', NULL);
DROP DICTIONARY IF EXISTS flat_dictionary;
CREATE DICTIONARY flat_dictionary
(
id UInt64,
v1 String,
v2 Nullable(String) DEFAULT NULL
)
PRIMARY KEY id
SOURCE(CLICKHOUSE(TABLE 'dictionary_source_table'))
LIFETIME(MIN 0 MAX 0)
LAYOUT(FLAT());
SELECT 'Flat dictionary';
SELECT dictGetOrDefault('flat_dictionary', ('v1', 'v2'), 0, (intDiv(1, id), intDiv(1, id)))
FROM dictionary_source_table;
DROP DICTIONARY flat_dictionary;
DROP DICTIONARY IF EXISTS hashed_dictionary;
CREATE DICTIONARY hashed_dictionary
(
id UInt64,
v1 String,
v2 Nullable(String) DEFAULT NULL
)
PRIMARY KEY id
SOURCE(CLICKHOUSE(TABLE 'dictionary_source_table'))
LIFETIME(MIN 0 MAX 0)
LAYOUT(HASHED());
SELECT 'Hashed dictionary';
SELECT dictGetOrDefault('hashed_dictionary', ('v1', 'v2'), 0, (intDiv(1, id), intDiv(1, id)))
FROM dictionary_source_table;
DROP DICTIONARY hashed_dictionary;