def METHOD_NAME(query, database):
    mongoToAst = MongoToAst()
    collection = [database, query['find']]
    if not query.get('singleBatch') and 'collection' in query.get('filter'):
        # JOIN mode
        # upper query
        ast_query = mongoToAst.find(
            collection=collection,
            projection=query.get('projection'),
            sort=query.get('sort'),
            limit=query.get('limit'),
            skip=query.get('skip'),
        )
        # table_query
        collection = query['filter']['collection']
        filter = query['filter'].get('query', {})
        table_select = mongoToAst.find(
            collection=collection,
            filter=filter,
        )
        table_select.parentheses = True
        table_select.alias = Identifier(query['filter']['collection'])
        table_select.alias.parts = [table_select.alias.parts[-1]]
        if 'limit' in query:
            table_select.limit = Constant(query['limit'])
        modifiers = query['filter'].get('modifiers')
        if modifiers is not None and hasattr(ast_query, 'modifiers'):
            for modifier in modifiers:
                table_select.modifiers.append(modifier)
        # convert to join
        right_table = ast_query.from_table
        ast_join = Join(
            left=table_select,
            right=right_table,
            join_type='join'
        )
        ast_query.from_table = ast_join
    else:
        # is single table
        ast_query = mongoToAst.find(
            collection=collection,
            filter=query.get('filter'),
            projection=query.get('projection'),
            sort=query.get('sort'),
            limit=query.get('limit'),
            skip=query.get('skip'),
        )
        modifiers = query['filter'].get('modifiers')
        if modifiers is not None and hasattr(ast_query, 'modifiers'):
            for modifier in modifiers:
                ast_query.modifiers.append(modifier)
    return ast_query