code: def closest_ds_partition(
      table,
      ds,
      before=True,
      schema="default",
      metastore_conn_id="metastore_default",
  ) -> str | None:
      """
      Find the date in a list closest to the target date.
  
      An optional parameter can be given to get the closest before or after.
  
      :param table: A hive table name
      :param ds: A datestamp ``%Y-%m-%d`` e.g. ``yyyy-mm-dd``
      :param before: closest before (True), after (False) or either side of ds
      :param schema: table schema
      :param metastore_conn_id: which metastore connection to use
      :returns: The closest date
  
      >>> tbl = "airflow.static_babynames_partitioned"
      >>> closest_ds_partition(tbl, "2015-01-02")
      '2015-01-01'
      """
      from airflow.providers.apache.hive.hooks.hive import (
          HiveMetastoreHook,
      )
  
      if "." in table:
          schema, table = table.split(".")
      hive_hook = HiveMetastoreHook(metastore_conn_id=metastore_conn_id)
      partitions = hive_hook.get_partitions(
          schema=schema, table_name=table
      )
      if not partitions:
          return None
      part_vals = [next(iter(p.values())) for p in partitions]
      if ds in part_vals:
          return ds
      else:
          parts = [
              datetime.datetime.strptime(pv, "%Y-%m-%d")
              for pv in part_vals
          ]
          target_dt = datetime.datetime.strptime(ds, "%Y-%m-%d")
          closest_ds = _closest_date(
              target_dt, parts, before_target=before
          )
          if closest_ds is not None:
              return closest_ds.isoformat()
      return None
  
imports: from __future__ import annotations
  import datetime
label_desc: Use image node_id 3 for calling a global function with example usage: closest_ds_partition(table, ds, before, schema, metastore_conn_id) and returns: None, None, ds, closest_ds