def METHOD_NAME(collectionid: Optional[int], userid: int, resource: str, action: str) -> QueryResult:
    cursor = connection.cursor()
    cursor.execute("""
    select collection_id, specifyuser_id, resource, action
    from spuserpolicy
    where (collection_id = %(collectionid)s or collection_id is null)
    and (specifyuser_id = %(userid)s or specifyuser_id is null)
    and %(resource)s like resource
    and %(action)s like action
    """, {
        'collectionid': collectionid,
        'userid': userid,
        'resource': resource,
        'action': action
    })
    ups = [
        dict(zip(("collectionid", "userid", "resource", "action"), r))
        for r in cursor.fetchall()
    ]
    cursor.execute("""
    select r.id, r.name, resource, action
    from spuserrole ur
    join sprole r on r.id = ur.role_id
    join sprolepolicy rp on rp.role_id = r.id
    where ur.specifyuser_id = %(userid)s
    and collection_id = %(collectionid)s
    and %(resource)s like resource
    and %(action)s like action
    """, {
        'collectionid': collectionid,
        'userid': userid,
        'resource': resource,
        'action': action
    })
    rps = [
        dict(zip(("roleid", "rolename", "resource", "action"), r))
        for r in cursor.fetchall()
    ]
    return QueryResult(
        allowed=bool(ups) or bool(rps),
        matching_user_policies=ups,
        matching_role_policies=rps,
    )