Query pattern

@imlucas 
Parse can not parse the query pattern field.
+ A bit of refactoring
- moving portions of code into their own function
- spacing
- renaming sort_shape to sortShape