diff --git a/src/Storages/MergeTree/MergeTreeBlockReadUtils.cpp b/src/Storages/MergeTree/MergeTreeBlockReadUtils.cpp index e300ceeb4fc..dac304fcdff 100644 --- a/src/Storages/MergeTree/MergeTreeBlockReadUtils.cpp +++ b/src/Storages/MergeTree/MergeTreeBlockReadUtils.cpp @@ -44,12 +44,11 @@ bool injectRequiredColumnsRecursively( if (alter_conversions.isColumnRenamed(column_name_in_part)) column_name_in_part = alter_conversions.getColumnOldName(column_name_in_part); - auto column_in_part = NameAndTypePair( - column_name_in_part, column_in_storage->getSubcolumnName(), - column_in_storage->getTypeInStorage(), column_in_storage->type); + auto column_in_part = part->getColumns().tryGetByName(column_name_in_part); - /// column has files and hence does not require evaluation - if (part->hasColumnFiles(column_in_part)) + if (column_in_part + && (!column_in_storage->isSubcolumn() + || column_in_part->type->tryGetSubcolumnType(column_in_storage->getSubcolumnName()))) { /// ensure each column is added only once if (!required_columns.contains(column_name)) diff --git a/tests/queries/0_stateless/01825_type_json_wide_parts_merge.reference b/tests/queries/0_stateless/01825_type_json_wide_parts_merge.reference new file mode 100644 index 00000000000..b41a6da16f5 --- /dev/null +++ b/tests/queries/0_stateless/01825_type_json_wide_parts_merge.reference @@ -0,0 +1,3 @@ +{"data":{"k1":0,"k2":2}} +{"data":{"k1":1,"k2":0}} +Tuple(k1 Int8, k2 Int8) diff --git a/tests/queries/0_stateless/01825_type_json_wide_parts_merge.sql b/tests/queries/0_stateless/01825_type_json_wide_parts_merge.sql new file mode 100644 index 00000000000..fd023e473a4 --- /dev/null +++ b/tests/queries/0_stateless/01825_type_json_wide_parts_merge.sql @@ -0,0 +1,25 @@ +-- Tags: no-fasttest + +SET allow_experimental_object_type = 1; +SET output_format_json_named_tuples_as_objects = 1; + +DROP TABLE IF EXISTS t_json_wide_parts; + +CREATE TABLE t_json_wide_parts (data JSON) +ENGINE MergeTree ORDER BY tuple() +SETTINGS min_bytes_for_wide_part = 0; + +SYSTEM STOP MERGES t_json_wide_parts; + +INSERT INTO t_json_wide_parts VALUES ('{"k1": 1}'); +INSERT INTO t_json_wide_parts VALUES ('{"k2": 2}'); + +SYSTEM START MERGES t_json_wide_parts; +OPTIMIZE TABLE t_json_wide_parts FINAL; + +SELECT data FROM t_json_wide_parts ORDER BY data.k1 FORMAT JSONEachRow; + +SELECT type FROM system.parts_columns +WHERE table = 't_json_wide_parts' AND database = currentDatabase() AND active; + +DROP TABLE t_json_wide_parts;