Skip to content

Commit 511d191

Browse files
nickitatzvonand
authored andcommitted
Merge pull request ClickHouse#78819 from ClickHouse/bring_back_76914
Bring back ClickHouse#76914
1 parent 82b3d5f commit 511d191

7 files changed

Lines changed: 62 additions & 16 deletions

src/Functions/multiIf.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,6 @@ class FunctionMultiIf final : public FunctionIfBase
157157

158158
ColumnPtr executeImpl(const ColumnsWithTypeAndName & args, const DataTypePtr & result_type, size_t input_rows_count) const override
159159
{
160-
/// Fast path when data is empty
161-
if (input_rows_count == 0)
162-
return result_type->createColumn();
163-
164160
ColumnsWithTypeAndName arguments = args;
165161
executeShortCircuitArguments(arguments);
166162
/** We will gather values from columns in branches to result column,
@@ -241,27 +237,24 @@ class FunctionMultiIf final : public FunctionIfBase
241237
}
242238

243239
/// Special case if first instruction condition is always true and source is constant
244-
if (instructions.size() == 1 && instructions.front().source_is_constant
245-
&& instructions.front().condition_always_true)
240+
if (instructions.size() == 1 && instructions.front().source_is_constant && instructions.front().condition_always_true)
246241
{
247242
MutableColumnPtr res = return_type->createColumn();
248243
auto & instruction = instructions.front();
249244
res->insertFrom(assert_cast<const ColumnConst &>(*instruction.source).getDataColumn(), 0);
250245
return ColumnConst::create(std::move(res), instruction.source->size());
251246
}
252247

253-
bool contains_short = false;
254-
for (const auto & instruction : instructions)
255-
{
256-
if (instruction.condition_is_short || instruction.source_is_short)
257-
{
258-
contains_short = true;
259-
break;
260-
}
261-
}
248+
/// Fast path when data is empty.
249+
/// We should account for the above case when the result is constant, otherwise we might produce a full column instead of a
250+
/// ColumnConst. But only for the header (while building pipeline, i.e. when input_rows_count==0), then we will get on with a
251+
/// normal execution and return ColumnConst from the happy path.
252+
/// It will break code that creates some data structures based on the input header column types.
253+
if (input_rows_count == 0)
254+
return result_type->createColumn();
262255

263256
const WhichDataType which(removeNullable(result_type));
264-
bool execute_multiif_columnar = allow_execute_multiif_columnar && !contains_short
257+
bool execute_multiif_columnar = allow_execute_multiif_columnar
265258
&& instructions.size() <= std::numeric_limits<UInt8>::max()
266259
&& (which.isInt() || which.isUInt() || which.isFloat() || which.isDecimal() || which.isDateOrDate32OrDateTimeOrDateTime64()
267260
|| which.isEnum() || which.isIPv4() || which.isIPv6());
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
2 1
2+
1 3

tests/queries/0_stateless/03352_distinct_sorted_bug.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ INSERT INTO t0 VALUES (1);
99
-- vptr for 'DB::ColumnConst'
1010
-- SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../src/Interpreters/AggregationCommon.h:90:35
1111
SELECT DISTINCT multiIf(1, 2, 1, materialize(toInt128(3)), 4), c0 FROM t0;
12+
13+
DROP TABLE IF EXISTS t0__fuzz_41;
14+
CREATE TABLE t0__fuzz_41 (c0 DateTime) ENGINE = MergeTree ORDER BY c0;
15+
INSERT INTO t0__fuzz_41 FORMAT Values (1) (2) (3) (4) (5) (6) (7) (8) (9) (10);
16+
17+
SELECT multiIf(1, 2, 1, materialize(3), 4), c0 FROM t0__fuzz_41 FORMAT Null;
18+
SELECT multiIf(0, 2, 1, materialize(3), 4), c0 FROM t0__fuzz_41 FORMAT Null;
19+
20+
SELECT DISTINCT subquery_1.id, subquery_2.id FROM (SELECT 1 AS id, 2 AS value) AS subquery_1, (SELECT 3 AS id, 4) AS subquery_2;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1 \N
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
CREATE TABLE t_having
2+
(
3+
c0 Int32,
4+
c1 UInt64
5+
)
6+
ENGINE = MergeTree
7+
ORDER BY c0
8+
SETTINGS index_granularity = 18398, min_bytes_for_wide_part = 985217954, ratio_of_defaults_for_sparse_serialization = 0.5019329786300659, replace_long_file_name_to_hash = true, max_file_name_length = 0, min_bytes_for_full_part_storage = 536870912, compact_parts_max_bytes_to_buffer = 3894513, compact_parts_max_granules_to_buffer = 216, compact_parts_merge_max_bytes_to_prefetch_part = 27835806, merge_max_block_size = 17883, old_parts_lifetime = 196., prefer_fetch_merged_part_size_threshold = 10737418240, vertical_merge_algorithm_min_rows_to_activate = 1, vertical_merge_algorithm_min_columns_to_activate = 100, min_merge_bytes_to_use_direct_io = 6973717094, index_granularity_bytes = 26319752, use_const_adaptive_granularity = true, enable_index_granularity_compression = true, concurrent_part_removal_threshold = 31, allow_vertical_merges_from_compact_to_wide_parts = true, enable_block_number_column = false, enable_block_offset_column = false, cache_populated_by_fetch = false, marks_compress_block_size = 64286, primary_key_compress_block_size = 40716, use_primary_key_cache = false, prewarm_primary_key_cache = true, prewarm_mark_cache = false;
9+
10+
INSERT INTO t_having SELECT number, number FROM numbers(1e3);
11+
12+
SET enable_optimize_predicate_expression = 0, min_compress_block_size = 1013937, max_compress_block_size = 2211670, max_block_size = 97680, min_external_table_block_size_bytes = 1, max_joined_block_size_rows = 98012, max_insert_threads = 2, max_threads = 32, max_parsing_threads = 10, max_read_buffer_size = 740736, connect_timeout_with_failover_ms = 2000, connect_timeout_with_failover_secure_ms = 3000, idle_connection_timeout = 36000, s3_max_get_rps = 1000000, s3_max_get_burst = 2000000, s3_max_put_rps = 1000000, s3_max_put_burst = 2000000, s3_check_objects_after_upload = true, use_uncompressed_cache = true, max_remote_read_network_bandwidth = 1000000000000, max_remote_write_network_bandwidth = 1000000000000, max_local_read_bandwidth = 1000000000000, max_local_write_bandwidth = 1000000000000, stream_like_engine_allow_direct_select = true, enable_multiple_prewhere_read_steps = false, replication_wait_for_inactive_replica_timeout = 30, min_count_to_compile_expression = 0, group_by_two_level_threshold = 1000000, group_by_two_level_threshold_bytes = 32424819, distributed_aggregation_memory_efficient = false, enable_positional_arguments = false, allow_nonconst_timezone_arguments = true, group_by_use_nulls = true, min_chunk_bytes_for_parallel_parsing = 5815363, merge_tree_coarse_index_granularity = 6, min_bytes_to_use_direct_io = 10737418240, min_bytes_to_use_mmap_io = 1, log_queries = true, insert_quorum_timeout = 60000, table_function_remote_max_addresses = 200, merge_tree_read_split_ranges_into_intersecting_and_non_intersecting_injection_probability = 0.6000000238418579, http_wait_end_of_query = true, http_response_buffer_size = 7985738, fsync_metadata = true, join_use_nulls = true, query_plan_join_swap_table = false, http_send_timeout = 60., http_receive_timeout = 60., use_index_for_in_with_subqueries_max_values = 1000000000, opentelemetry_start_trace_probability = 0.10000000149011612, enable_vertical_final = false, max_rows_to_read = 20000000, max_bytes_to_read = 1000000000000, max_bytes_to_read_leaf = 1000000000000, max_rows_to_group_by = 10000000000, max_bytes_ratio_before_external_group_by = 0.2, max_rows_to_sort = 10000000000, max_bytes_to_sort = 10000000000, prefer_external_sort_block_bytes = 100000000, max_bytes_ratio_before_external_sort = 0.19, max_bytes_before_remerge_sort = 2087479364, max_result_rows = 1000000000, max_result_bytes = 1000000000, max_execution_time = 60., max_execution_time_leaf = 600., max_execution_speed = 100000000000, max_execution_speed_bytes = 10000000000000, timeout_before_checking_execution_speed = 300., max_estimated_execution_time = 600., max_columns_to_read = 20000, max_temporary_columns = 20000, max_temporary_non_const_columns = 20000, max_rows_in_set = 10000000000, max_bytes_in_set = 10000000000, max_rows_in_join = 10000000000, max_bytes_in_join = 10000000000, join_algorithm = 'grace_hash', cross_join_min_rows_to_compress = 100000000, cross_join_min_bytes_to_compress = 0, max_rows_to_transfer = 1000000000, max_bytes_to_transfer = 1000000000, max_rows_in_distinct = 10000000000, max_bytes_in_distinct = 10000000000, max_memory_usage = 10000000000, max_memory_usage_for_user = 19896741888, max_untracked_memory = 1048576, memory_profiler_step = 1048576, max_network_bandwidth = 100000000000, max_network_bytes = 1000000000000, max_network_bandwidth_for_user = 100000000000, max_network_bandwidth_for_all_users = 100000000000, max_temporary_data_on_disk_size_for_user = 100000000000, max_temporary_data_on_disk_size_for_query = 100000000000, max_backup_bandwidth = 100000000000, log_comment = '01798_having_push_down.sql', send_logs_level = 'warning', enable_optimize_predicate_expression = false, optimize_aggregation_in_order = true, aggregation_in_order_max_block_bytes = 49246335, read_in_order_two_level_merge_threshold = 48, max_hyperscan_regexp_length = 1000000, max_hyperscan_regexp_total_length = 10000000, allow_introspection_functions = true, database_atomic_wait_for_drop_and_detach_synchronously = true, optimize_or_like_chain = true, optimize_if_chain_to_multiif = true, optimize_functions_to_subcolumns = false, optimize_append_index = true, use_query_cache = true, query_cache_nondeterministic_function_handling = 'ignore', query_cache_system_table_handling = 'ignore', query_cache_max_size_in_bytes = 10000000, query_cache_max_entries = 100000, database_replicated_allow_replicated_engine_arguments = 1, distributed_ddl_entry_format_version = 6, external_storage_max_read_rows = 10000000000, external_storage_max_read_bytes = 10000000000, local_filesystem_read_method = 'pread', remote_filesystem_read_method = 'read', local_filesystem_read_prefetch = true, remote_filesystem_read_prefetch = false, merge_tree_min_bytes_per_task_for_remote_reading = 16777216, merge_tree_compact_parts_min_granules_to_multibuffer_read = 55, async_insert_busy_timeout_max_ms = 5000, enable_filesystem_cache = true, enable_filesystem_cache_on_write_operations = true, filesystem_cache_segments_batch_size = 10, use_page_cache_for_disks_without_file_cache = true, load_marks_asynchronously = true, allow_prefetched_read_pool_for_remote_filesystem = true, allow_prefetched_read_pool_for_local_filesystem = false, filesystem_prefetch_step_bytes = 104857600, filesystem_prefetch_max_memory_usage = 134217728, filesystem_prefetches_limit = 10, allow_deprecated_database_ordinary = true, max_streams_for_merge_tree_reading = 1000, insert_keeper_max_retries = 100, insert_keeper_retry_initial_backoff_ms = 1, insert_keeper_retry_max_backoff_ms = 10, insert_keeper_fault_injection_probability = 0.009999999776482582, ignore_drop_queries_probability = 0.20000000298023224, optimize_distinct_in_order = false, allow_experimental_parallel_reading_from_replicas = 1, max_parallel_replicas = 3, cluster_for_parallel_replicas = 'parallel_replicas', parallel_replicas_for_non_replicated_merge_tree = true, session_timezone = 'Africa/Juba';
13+
14+
SELECT
15+
c0 + -1,
16+
sum(intDivOrZero(intDivOrZero(NULL, NULL), '2'), intDivOrZero(10000000000., intDivOrZero(intDivOrZero(intDivOrZero(NULL, NULL), 10), NULL)))
17+
FROM t_having
18+
GROUP BY
19+
c0 = 2,
20+
c0 = 10,
21+
intDivOrZero(intDivOrZero(intDivOrZero(NULL, NULL), NULL), NULL),
22+
c0
23+
HAVING c0 = 2;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[(1, \'a\')]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
SELECT DISTINCT t
2+
FROM
3+
(
4+
SELECT CAST('[(1, \'a\')]', 'String') AS t
5+
GROUP BY
6+
GROUPING SETS (
7+
(1),
8+
(printf(printf(NULL, 7, printf(isNullable(7), 7, '%%d: %d', 7, 7, 7, NULL), 7, materialize(toUInt256(7)), '%%d: %d', toNullable(7) IS NULL, 7), materialize(toNullable(NULL)))),
9+
(isZeroOrNull(isNullable(7)) IS NULL))
10+
) AS na,
11+
(
12+
SELECT CAST(toNullable('[(1, \'a\')]'), 'String') AS t
13+
GROUP BY
14+
1,
15+
toNullable(toUInt256(123)),
16+
printf(printf(NULL, *, printf(NULL, 7, 7, 7, '%%d: %d', 7, 7, materialize(7), 7, 7), 7, 7, '%%d: %d', 7, 7, 7))
17+
)
18+
SETTINGS joined_subquery_requires_alias = 0;

0 commit comments

Comments
 (0)