| 27167 | */ |
| 27168 | |
| 27169 | static int test_if_order_by_key(JOIN *join, ORDER *order, TABLE *table, |
| 27170 | uint idx, uint *used_key_parts) |
| 27171 | { |
| 27172 | KEY_PART_INFO *key_part,*key_part_end; |
| 27173 | key_part=table->key_info[idx].key_part; |
| 27174 | key_part_end=key_part + table->key_info[idx].ext_key_parts; |
| 27175 | key_part_map const_key_parts=table->const_key_parts[idx]; |
| 27176 | uint user_defined_kp= table->key_info[idx].user_defined_key_parts; |
| 27177 | int reverse=0; |
| 27178 | uint key_parts; |
| 27179 | bool have_pk_suffix= false; |
| 27180 | uint pk= table->s->primary_key; |
| 27181 | ORDER::enum_order keypart_order; |
| 27182 | DBUG_ENTER("test_if_order_by_key"); |
| 27183 | |
| 27184 | if ((table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) && |
| 27185 | idx < table->s->keys && |
| 27186 | table->key_info[idx].ext_key_part_map && pk != MAX_KEY && pk != idx) |
| 27187 | { |
| 27188 | have_pk_suffix= true; |
| 27189 | } |
| 27190 | |
| 27191 | if ((*order->item)->real_item()->type() != Item::FIELD_ITEM) |
| 27192 | { |
| 27193 | if (order->next || order->direction != ORDER::ORDER_ASC) |
| 27194 | DBUG_RETURN(0); |
| 27195 | |
| 27196 | DBUG_RETURN((*order->item)->part_of_sortkey().is_set(idx)); |
| 27197 | } |
| 27198 | |
| 27199 | for (; order ; order=order->next, const_key_parts>>=1) |
| 27200 | { |
| 27201 | Item_field *item_field= ((Item_field*) (*order->item)->real_item()); |
| 27202 | int flag; |
| 27203 | |
| 27204 | /* |
| 27205 | Skip key parts that are constants in the WHERE clause. |
| 27206 | These are already skipped in the ORDER BY by const_expression_in_where() |
| 27207 | for top level queries. |
| 27208 | */ |
| 27209 | for (; const_key_parts & 1 ; const_key_parts>>= 1) |
| 27210 | { |
| 27211 | if (item_field->contains(key_part->field)) |
| 27212 | { |
| 27213 | /* Subquery with ORDER BY, continue with next field */ |
| 27214 | goto next_order_field; |
| 27215 | } |
| 27216 | key_part++; |
| 27217 | } |
| 27218 | |
| 27219 | /* |
| 27220 | This check was in this function historically (although I think it's |
| 27221 | better to check it outside of this function): |
| 27222 | |
| 27223 | "Test if the primary key parts were all const (i.e. there's one row). |
| 27224 | The sorting doesn't matter" |
| 27225 | |
| 27226 | So, we're checking that |
no test coverage detected