| 1128 | } |
| 1129 | |
| 1130 | String MergeTreeMetaBase::getPartitionIDFromQuery(const ASTPtr & ast, ContextPtr local_context) const |
| 1131 | { |
| 1132 | if (const auto & ast_literal = ast->as<ASTLiteral>()) |
| 1133 | { |
| 1134 | return ast_literal->value.get<String>(); |
| 1135 | } |
| 1136 | |
| 1137 | const auto & partition_ast = ast->as<ASTPartition &>(); |
| 1138 | |
| 1139 | if (!partition_ast.value) |
| 1140 | { |
| 1141 | MergeTreePartInfo::validatePartitionID(partition_ast.id, format_version); |
| 1142 | return partition_ast.id; |
| 1143 | } |
| 1144 | |
| 1145 | if (format_version < MERGE_TREE_DATA_MIN_FORMAT_VERSION_WITH_CUSTOM_PARTITIONING) |
| 1146 | { |
| 1147 | /// Month-partitioning specific - partition ID can be passed in the partition value. |
| 1148 | const auto * partition_lit = partition_ast.value->as<ASTLiteral>(); |
| 1149 | if (partition_lit && partition_lit->value.getType() == Field::Types::String) |
| 1150 | { |
| 1151 | String partition_id = partition_lit->value.get<String>(); |
| 1152 | MergeTreePartInfo::validatePartitionID(partition_id, format_version); |
| 1153 | return partition_id; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | /// Re-parse partition key fields using the information about expected field types. |
| 1158 | |
| 1159 | auto metadata_snapshot = getInMemoryMetadataPtr(); |
| 1160 | size_t fields_count = metadata_snapshot->getPartitionKey().sample_block.columns(); |
| 1161 | if (partition_ast.fields_count != fields_count) |
| 1162 | throw Exception( |
| 1163 | "Wrong number of fields in the partition expression: " + toString(partition_ast.fields_count) + |
| 1164 | ", must be: " + toString(fields_count), |
| 1165 | ErrorCodes::INVALID_PARTITION_VALUE); |
| 1166 | |
| 1167 | const FormatSettings format_settings; |
| 1168 | Row partition_row(fields_count); |
| 1169 | |
| 1170 | if (fields_count) |
| 1171 | { |
| 1172 | ReadBufferFromMemory left_paren_buf("(", 1); |
| 1173 | ReadBufferFromMemory fields_buf(partition_ast.fields_str.data(), partition_ast.fields_str.size()); |
| 1174 | ReadBufferFromMemory right_paren_buf(")", 1); |
| 1175 | ConcatReadBuffer buf({&left_paren_buf, &fields_buf, &right_paren_buf}); |
| 1176 | |
| 1177 | auto input_format = FormatFactory::instance().getInput( |
| 1178 | "Values", |
| 1179 | buf, |
| 1180 | metadata_snapshot->getPartitionKey().sample_block, |
| 1181 | local_context, |
| 1182 | local_context->getSettingsRef().max_block_size); |
| 1183 | auto input_stream = std::make_shared<InputStreamFromInputFormat>(input_format); |
| 1184 | |
| 1185 | auto block = input_stream->read(); |
| 1186 | if (!block || !block.rows()) |
| 1187 | throw Exception( |
no test coverage detected