Merge generic query parsing logic