Extract function serializeCompoundQuery