def _build_metadata_condition(
    relative_path: str,
    condition_spec: Any,
    param_helper: ParamHelper,
    json_column: str,
) -> str:
    """
    Builds SQL condition for a potentially nested field within a JSONB column.
    This function acts as a dispatcher, figuring out if the condition_spec
    is a direct operator application or a further nested path definition.

    Args:
        relative_path (str): The path to the field *within* the JSONB column
                             (e.g., "key", "nested.key"). Can be empty if
                             the top-level filter targets the json_column itself.
        condition_spec (Any): The condition to apply (e.g., "value", {"$gt": 5},
                              {"nested": "val"}, {"path.to.key": {"$in": [...]}}).
        param_helper (ParamHelper): The parameter helper instance.
        json_column (str): The name of the JSONB column (e.g., 'metadata').

    Returns:
        str: The generated SQL condition string.

    Raises:
        FilterError: If the condition specification is invalid.
    """

    # Handle complex condition_spec (nested paths or operators)
    # Check if condition_spec is a dictionary containing a single key
    if isinstance(condition_spec, dict) and len(condition_spec) == 1:
        key, value = next(iter(condition_spec.items()))

        # Case 1: The key is a recognized operator (starts with '$')
        if key.startswith("$") and key in vars(FilterOperator).values():
            # Apply the operator 'key' with 'value' to the 'relative_path'
            # Requires the helper function _build_metadata_operator_condition
            # Ensure relative_path is valid (not empty for direct operator)
            if not relative_path:
                raise FilterError(
                    f"Operator '{key}' cannot be applied directly to the root of '{json_column}'. Specify a path."
                )
            return _build_metadata_operator_condition(
                relative_path, key, value, param_helper, json_column
            )

        # Case 2: The key is NOT an operator - assume it's a nested path segment
        else:
            # It's a nested path like {"inner": "value"} applied relative to relative_path
            # Combine the current relative_path with the new key
            # Handle the case where relative_path might be initially empty (shouldn't happen if called from _process_field_condition correctly)
            new_relative_path = (
                f"{relative_path}.{key}" if relative_path else key
            )
            # Recursively call _build_metadata_condition with the combined path and the inner value
            return _build_metadata_condition(
                new_relative_path, value, param_helper, json_column
            )

    # Handle condition_spec being a direct value (shorthand for EQ)
    elif not isinstance(condition_spec, dict):
        # It's a direct value comparison like "value", 123, True
        # Apply EQ operator to the relative_path
        # Requires the helper function _build_metadata_operator_condition
        if not relative_path:
            raise FilterError(
                f"Direct value comparison cannot be applied to the root of '{json_column}'. Specify a path."
            )
        return _build_metadata_operator_condition(
            relative_path,
            FilterOperator.EQ,  # Apply Equality operator
            condition_spec,  # The value itself
            param_helper,
            json_column,
        )

    # Handle condition_spec being a dictionary but with multiple keys or zero keys (invalid structure at this level)
    # This case usually happens when the filter is like:
    # {"metadata": {"path1": "val1", "path2": {"$gt": 5}}}
    # which should have been handled by the loop in _process_field_condition
    # when the field name was just "metadata". If we reach here with such a structure,
    # it implies an unexpected filter format deeper down.
    else:  # It's a dict with 0 or multiple keys, or something else unexpected
        # If relative_path is empty, it might be the multi-key dict case from the caller
        if not relative_path and isinstance(condition_spec, dict):
            raise FilterError(
                f"Internal Error: Multi-key dictionary for '{json_column}' root should be handled by caller loop."
            )
        # Otherwise, it's an invalid structure nested under a path
        raise FilterError(
            f"Invalid filter structure for metadata path '{relative_path}'. "
            f"Expected a value or a single-key dictionary with an operator or nested path. Found: {condition_spec}"
        )

    Builds SQL condition for a potentially nested field within a JSONB column.
    This function acts as a dispatcher, figuring out if the condition_spec
    is a direct operator application or a further nested path definition.

    Args:
        relative_path (str): The path to the field *within* the JSONB column
                             (e.g., "key", "nested.key"). Can be empty if
                             the top-level filter targets the json_column itself.
        condition_spec (Any): The condition to apply (e.g., "value", {"$gt": 5},
                              {"nested": "val"}, {"path.to.key": {"$in": [...]}}).
        param_helper (ParamHelper): The parameter helper instance.
        json_column (str): The name of the JSONB column (e.g., 'metadata').

    Returns:
        str: The generated SQL condition string.

    Raises:
        FilterError: If the condition specification is invalid.
    
py/core/providers/database/filters.py