code: def pyarrow_schema_to_select_replace(
      schema: pa.Schema, table_name: str
  ) -> str:
      """
      Build `SELECT * REPLACE(...) from table_name` query that casts columns
      to match the provided pyarrow schema.
  
      This is needed because sometimes the resulting DuckDB column types won't exactly
      match those that DataFusion expects (e.g. DuckDB returning a DECIMAL(5) instead of
      and int64). Types that are not covered by `pyarrow_type_to_duckdb_type_name` above
      are passed through as-is.
      """
      replaces = []
      for field_index in range(len(schema)):
          field = schema.field(field_index)
          field_name = field.name
          field_type = field.type
  
          quoted_column = quote_column(field_name)
          duckdb_type = pyarrow_type_to_duckdb_type_name(field_type)
          if duckdb_type:
              replaces.append(
                  f"{quoted_column}::{duckdb_type} as {quoted_column}"
              )
  
      if replaces:
          replace_csv = ", ".join(replaces)
          return f"SELECT * REPLACE({replace_csv}) FROM {table_name}"
      else:
          return f"SELECT * FROM {table_name}"
  
imports: import re
  import warnings
  from .None import SqlConnection, CsvReadOptions
  from typing import Dict, Optional
  from distutils.version import LooseVersion
  import duckdb
  import pyarrow
  import pyarrow.feather
  import pandas
  import logging
  import uuid
label_desc: Use image node_id 4 for calling a global function with example usage: pyarrow_schema_to_select_replace(schema, table_name) and returns: str, replace_csv, str, table_name, str, table_name