code: def _match_categorical_dtypes_both(
      lcol: CategoricalColumn, rcol: CategoricalColumn, how: str
  ) -> Tuple[ColumnBase, ColumnBase]:
      ltype, rtype = lcol.dtype, rcol.dtype
  
      # when both are ordered and both have the same categories,
      # no casting required:
      if ltype == rtype:
          return lcol, rcol
  
      # Merging categorical variables when only one side is ordered is
      # ambiguous and not allowed.
      if ltype.ordered != rtype.ordered:
          raise TypeError(
              "Merging on categorical variables with mismatched"
              " ordering is ambiguous"
          )
  
      if ltype.ordered and rtype.ordered:
          # if we get to here, categories must be what causes the
          # dtype equality check to fail. And we can never merge
          # two ordered categoricals with different categories
          raise TypeError(
              f"{how} merge between categoricals with "
              "different categories is only valid when "
              "neither side is ordered"
          )
  
      if how == "inner":
          # cast to category types -- we must cast them back later
          return _match_join_keys(
              lcol._get_decategorized_column(),
              rcol._get_decategorized_column(),
              how,
          )
      elif how in {"left", "leftanti", "leftsemi"}:
          # always cast to left type
          return lcol, rcol.astype(ltype)
      else:
          # merge categories
          merged_categories = cudf.concat(
              [ltype.categories, rtype.categories]
          ).unique()
          common_type = cudf.CategoricalDtype(
              categories=merged_categories, ordered=False
          )
          return lcol.astype(common_type), rcol.astype(common_type)
  
imports: from __future__ import annotations
  from collections import abc
  from typing import TYPE_CHECKING, Any, Tuple, cast
  import numpy
  import cudf
  from cudf.api.types import is_decimal_dtype, is_dtype_equal
  from cudf.core.column import CategoricalColumn
  from cudf.core.dtypes import CategoricalDtype
label_desc: Use image node_id 2 for calling a global function with example usage: _match_categorical_dtypes_both(lcol, rcol, how) and returns: lcol,  rcol, _match_join_keys, lcol,  rcol, lcol,  rcol