def collectFields(node, keys=None, condition=False):
    """
    Collects all fields accessed by the given root node and all children.

    Returns a set of field names.

    """

    if keys is None:
        keys = set()

    if getattr(node, "rel", None) == "condition":
        condition = True

    elif node.type == "command" and node.name == "field":
        keys.add(node.params[0].value)

    elif condition and node.type == "identifier":
        keys.add(node.value)

    # Process children
    for child in reversed(node):
        if child is not None:
            collectFields(child, keys, condition)

    return keys