chamikaramj commented on code in PR #41186:
URL: https://github.com/apache/beam/pull/41186#discussion_r4662451597


##########
sdks/python/apache_beam/yaml/yaml_io.py:
##########
@@ -99,6 +104,260 @@ def write_to_text(pcoll, path: str):
       lambda x: str(getattr(x, sole_field_name))) | beam.io.WriteToText(path)
 
 
+def read_from_csv(
+    path: str,
+    *,
+    sep: str = ',',
+    comment: Optional[str] = None,
+    filename_column: Optional[str] = None,
+    error_handling: Optional[Mapping[str, Any]] = None,
+    **kwargs):
+  """Reads comma-separated values (csv) files into a PCollection of rows.
+
+  Without ``error_handling``, this is ``apache_beam.io.ReadFromCsv``.
+
+  With ``error_handling``, each record is parsed on its own, and records
+  that fail to parse go to the error output instead of failing the pipeline.
+  Each error row has the source file path and the raw record text.
+  The column types come from the first 100 records of the first file.

Review Comment:
   Why 100 ?



##########
sdks/python/apache_beam/yaml/yaml_io.py:
##########
@@ -99,6 +104,260 @@ def write_to_text(pcoll, path: str):
       lambda x: str(getattr(x, sole_field_name))) | beam.io.WriteToText(path)
 
 
+def read_from_csv(
+    path: str,
+    *,
+    sep: str = ',',
+    comment: Optional[str] = None,
+    filename_column: Optional[str] = None,
+    error_handling: Optional[Mapping[str, Any]] = None,
+    **kwargs):
+  """Reads comma-separated values (csv) files into a PCollection of rows.
+
+  Without ``error_handling``, this is ``apache_beam.io.ReadFromCsv``.
+
+  With ``error_handling``, each record is parsed on its own, and records
+  that fail to parse go to the error output instead of failing the pipeline.
+  Each error row has the source file path and the raw record text.
+  The column types come from the first 100 records of the first file.
+
+  Args:
+    path (str): The file path to read from.  The path can contain glob
+      characters such as ``*`` and ``?``.
+    sep (str): The field delimiter.
+    comment (str): Lines that start with this character are skipped.
+    filename_column (str): If set, the name of a column to add to each
+      record, containing the path of the source file.
+    error_handling: If specified, should be a mapping giving an output into
+      which to emit records that failed to parse, as described at
+      https://beam.apache.org/documentation/sdks/yaml-errors/
+  """
+  if not error_handling:
+    return beam_io.ReadFromCsv(
+        path,
+        sep=sep,
+        comment=comment,
+        filename_column=filename_column,
+        **kwargs)
+  if kwargs:
+    raise ValueError(
+        f'ReadFromCsv does not support {sorted(kwargs)} with error_handling.')
+
+  def sample(handle):
+    return pd.read_csv(
+        handle,
+        sep=sep,
+        comment=comment,
+        nrows=_SAMPLE_SIZE,
+        on_bad_lines='skip')
+
+  def parse_record(header, record, dtypes):
+    columns = list(dtypes)
+    fields = pd.read_csv(
+        io.StringIO(record), header=None, sep=sep, comment=comment, dtype=str)
+    if len(fields.columns) != len(columns):
+      raise ValueError(
+          f'Expected {len(columns)} fields, got {len(fields.columns)}.')
+    df = pd.read_csv(
+        io.StringIO(header + record), sep=sep, comment=comment, dtype=dtypes)
+    if list(df.columns) != columns:
+      raise ValueError(
+          f'The file header {list(df.columns)} does not match {columns}.')
+    return df
+
+  def parse_file(lines, proxy):
+    dtypes = proxy.dtypes.drop(filename_column, errors='ignore').to_dict()
+    header = None
+    for record in _join_quoted_lines(lines, comment):
+      if not record.strip():
+        continue
+      if header is None:
+        header = record
+      else:
+        yield record, functools.partial(parse_record, header, record, dtypes)
+
+  return _ReadFilesWithErrors(
+      path, sample, parse_file, filename_column, error_handling)
+
+
+def read_from_json(
+    path: str, *, error_handling: Optional[Mapping[str, Any]] = None, 
**kwargs):
+  """Reads newline-delimited JSON files into a PCollection of rows.
+
+  Without ``error_handling``, this is ``apache_beam.io.ReadFromJson``.
+
+  With ``error_handling``, each line is parsed on its own, and lines
+  that fail to parse go to the error output instead of failing the pipeline.
+  Each error row has the source file path and the raw line.
+  The column types come from the first 100 records of the first file.
+
+  Args:
+    path (str): The file path to read from.  The path can contain glob
+      characters such as ``*`` and ``?``.
+    error_handling: If specified, should be a mapping giving an output into
+      which to emit lines that failed to parse, as described at
+      https://beam.apache.org/documentation/sdks/yaml-errors/
+  """
+  if not error_handling:
+    return beam_io.ReadFromJson(path, **kwargs)
+  if kwargs:
+    raise ValueError(
+        f'ReadFromJson does not support {sorted(kwargs)} with error_handling.')
+
+  def read_json(text):
+    return pd.read_json(
+        io.StringIO(text), orient='records', lines=True, dtype=False)
+
+  def sample(handle):
+    good_lines = []
+    for line in itertools.islice(handle, _SAMPLE_SIZE):
+      try:
+        if isinstance(json.loads(line), dict):
+          good_lines.append(line)
+      except ValueError:
+        pass
+    if not good_lines:
+      raise ValueError(
+          f'Found no JSON objects in the first {_SAMPLE_SIZE} lines '
+          'of the first file, so the column types are unknown.')
+    return read_json(''.join(good_lines))
+
+  def parse_record(line, proxy):
+    value = json.loads(line)
+    if not isinstance(value, dict):
+      raise ValueError('Expected a JSON object.')
+    unknown_fields = set(value) - set(proxy.columns)
+    if unknown_fields:
+      raise ValueError(f'Unknown fields {sorted(unknown_fields)}.')
+    return read_json(line).reindex(columns=proxy.columns)
+
+  def parse_file(lines, proxy):
+    for line in lines:
+      if line.strip():
+        yield line, functools.partial(parse_record, line, proxy)
+
+  return _ReadFilesWithErrors(path, sample, parse_file, None, error_handling)
+
+
+# The number of records used to infer column types when error_handling is set.
+_SAMPLE_SIZE = 100

Review Comment:
   Ditto. Any reason to justify 100 as the sample size here ?



##########
sdks/python/apache_beam/yaml/yaml_io.py:
##########
@@ -99,6 +104,260 @@ def write_to_text(pcoll, path: str):
       lambda x: str(getattr(x, sole_field_name))) | beam.io.WriteToText(path)
 
 
+def read_from_csv(
+    path: str,
+    *,
+    sep: str = ',',
+    comment: Optional[str] = None,
+    filename_column: Optional[str] = None,
+    error_handling: Optional[Mapping[str, Any]] = None,
+    **kwargs):
+  """Reads comma-separated values (csv) files into a PCollection of rows.
+
+  Without ``error_handling``, this is ``apache_beam.io.ReadFromCsv``.
+
+  With ``error_handling``, each record is parsed on its own, and records
+  that fail to parse go to the error output instead of failing the pipeline.
+  Each error row has the source file path and the raw record text.
+  The column types come from the first 100 records of the first file.
+
+  Args:
+    path (str): The file path to read from.  The path can contain glob
+      characters such as ``*`` and ``?``.
+    sep (str): The field delimiter.
+    comment (str): Lines that start with this character are skipped.
+    filename_column (str): If set, the name of a column to add to each
+      record, containing the path of the source file.
+    error_handling: If specified, should be a mapping giving an output into
+      which to emit records that failed to parse, as described at
+      https://beam.apache.org/documentation/sdks/yaml-errors/
+  """
+  if not error_handling:
+    return beam_io.ReadFromCsv(
+        path,
+        sep=sep,
+        comment=comment,
+        filename_column=filename_column,
+        **kwargs)
+  if kwargs:
+    raise ValueError(
+        f'ReadFromCsv does not support {sorted(kwargs)} with error_handling.')
+
+  def sample(handle):
+    return pd.read_csv(
+        handle,
+        sep=sep,
+        comment=comment,
+        nrows=_SAMPLE_SIZE,
+        on_bad_lines='skip')
+
+  def parse_record(header, record, dtypes):
+    columns = list(dtypes)
+    fields = pd.read_csv(
+        io.StringIO(record), header=None, sep=sep, comment=comment, dtype=str)
+    if len(fields.columns) != len(columns):
+      raise ValueError(
+          f'Expected {len(columns)} fields, got {len(fields.columns)}.')
+    df = pd.read_csv(
+        io.StringIO(header + record), sep=sep, comment=comment, dtype=dtypes)
+    if list(df.columns) != columns:
+      raise ValueError(
+          f'The file header {list(df.columns)} does not match {columns}.')
+    return df
+
+  def parse_file(lines, proxy):
+    dtypes = proxy.dtypes.drop(filename_column, errors='ignore').to_dict()
+    header = None
+    for record in _join_quoted_lines(lines, comment):
+      if not record.strip():
+        continue
+      if header is None:
+        header = record
+      else:
+        yield record, functools.partial(parse_record, header, record, dtypes)
+
+  return _ReadFilesWithErrors(
+      path, sample, parse_file, filename_column, error_handling)
+
+
+def read_from_json(
+    path: str, *, error_handling: Optional[Mapping[str, Any]] = None, 
**kwargs):
+  """Reads newline-delimited JSON files into a PCollection of rows.
+
+  Without ``error_handling``, this is ``apache_beam.io.ReadFromJson``.
+
+  With ``error_handling``, each line is parsed on its own, and lines
+  that fail to parse go to the error output instead of failing the pipeline.
+  Each error row has the source file path and the raw line.
+  The column types come from the first 100 records of the first file.
+
+  Args:
+    path (str): The file path to read from.  The path can contain glob
+      characters such as ``*`` and ``?``.
+    error_handling: If specified, should be a mapping giving an output into
+      which to emit lines that failed to parse, as described at
+      https://beam.apache.org/documentation/sdks/yaml-errors/
+  """
+  if not error_handling:
+    return beam_io.ReadFromJson(path, **kwargs)
+  if kwargs:
+    raise ValueError(
+        f'ReadFromJson does not support {sorted(kwargs)} with error_handling.')
+
+  def read_json(text):
+    return pd.read_json(
+        io.StringIO(text), orient='records', lines=True, dtype=False)
+
+  def sample(handle):
+    good_lines = []
+    for line in itertools.islice(handle, _SAMPLE_SIZE):
+      try:
+        if isinstance(json.loads(line), dict):
+          good_lines.append(line)
+      except ValueError:
+        pass
+    if not good_lines:
+      raise ValueError(
+          f'Found no JSON objects in the first {_SAMPLE_SIZE} lines '
+          'of the first file, so the column types are unknown.')
+    return read_json(''.join(good_lines))
+
+  def parse_record(line, proxy):
+    value = json.loads(line)
+    if not isinstance(value, dict):
+      raise ValueError('Expected a JSON object.')
+    unknown_fields = set(value) - set(proxy.columns)
+    if unknown_fields:
+      raise ValueError(f'Unknown fields {sorted(unknown_fields)}.')
+    return read_json(line).reindex(columns=proxy.columns)
+
+  def parse_file(lines, proxy):
+    for line in lines:
+      if line.strip():
+        yield line, functools.partial(parse_record, line, proxy)
+
+  return _ReadFilesWithErrors(path, sample, parse_file, None, error_handling)
+
+
+# The number of records used to infer column types when error_handling is set.
+_SAMPLE_SIZE = 100
+
+
+def _join_quoted_lines(lines, comment=None):

Review Comment:
   Quote joining is quadratic and misattributes records
   
   record.count('"') rescans the whole growing record for every line. One 
unclosed quote (a truncated line, one of the issue's main examples) makes it 
absorb the rest of the file.
   
   Also,
   Good rows end up in the error output. pandas treats a quote in the middle of 
a field as a literal character, but the parity check doesn't.



##########
sdks/python/apache_beam/yaml/yaml_io.py:
##########
@@ -99,6 +104,260 @@ def write_to_text(pcoll, path: str):
       lambda x: str(getattr(x, sole_field_name))) | beam.io.WriteToText(path)
 
 
+def read_from_csv(
+    path: str,
+    *,
+    sep: str = ',',
+    comment: Optional[str] = None,
+    filename_column: Optional[str] = None,
+    error_handling: Optional[Mapping[str, Any]] = None,
+    **kwargs):
+  """Reads comma-separated values (csv) files into a PCollection of rows.
+
+  Without ``error_handling``, this is ``apache_beam.io.ReadFromCsv``.
+
+  With ``error_handling``, each record is parsed on its own, and records
+  that fail to parse go to the error output instead of failing the pipeline.
+  Each error row has the source file path and the raw record text.
+  The column types come from the first 100 records of the first file.
+
+  Args:
+    path (str): The file path to read from.  The path can contain glob
+      characters such as ``*`` and ``?``.
+    sep (str): The field delimiter.
+    comment (str): Lines that start with this character are skipped.
+    filename_column (str): If set, the name of a column to add to each
+      record, containing the path of the source file.
+    error_handling: If specified, should be a mapping giving an output into
+      which to emit records that failed to parse, as described at
+      https://beam.apache.org/documentation/sdks/yaml-errors/
+  """
+  if not error_handling:
+    return beam_io.ReadFromCsv(
+        path,
+        sep=sep,
+        comment=comment,
+        filename_column=filename_column,
+        **kwargs)
+  if kwargs:
+    raise ValueError(
+        f'ReadFromCsv does not support {sorted(kwargs)} with error_handling.')
+
+  def sample(handle):
+    return pd.read_csv(
+        handle,
+        sep=sep,
+        comment=comment,
+        nrows=_SAMPLE_SIZE,
+        on_bad_lines='skip')
+
+  def parse_record(header, record, dtypes):
+    columns = list(dtypes)
+    fields = pd.read_csv(
+        io.StringIO(record), header=None, sep=sep, comment=comment, dtype=str)
+    if len(fields.columns) != len(columns):
+      raise ValueError(
+          f'Expected {len(columns)} fields, got {len(fields.columns)}.')
+    df = pd.read_csv(
+        io.StringIO(header + record), sep=sep, comment=comment, dtype=dtypes)
+    if list(df.columns) != columns:
+      raise ValueError(
+          f'The file header {list(df.columns)} does not match {columns}.')
+    return df
+
+  def parse_file(lines, proxy):
+    dtypes = proxy.dtypes.drop(filename_column, errors='ignore').to_dict()
+    header = None
+    for record in _join_quoted_lines(lines, comment):
+      if not record.strip():
+        continue
+      if header is None:
+        header = record
+      else:
+        yield record, functools.partial(parse_record, header, record, dtypes)
+
+  return _ReadFilesWithErrors(
+      path, sample, parse_file, filename_column, error_handling)
+
+
+def read_from_json(
+    path: str, *, error_handling: Optional[Mapping[str, Any]] = None, 
**kwargs):
+  """Reads newline-delimited JSON files into a PCollection of rows.
+
+  Without ``error_handling``, this is ``apache_beam.io.ReadFromJson``.
+
+  With ``error_handling``, each line is parsed on its own, and lines
+  that fail to parse go to the error output instead of failing the pipeline.
+  Each error row has the source file path and the raw line.
+  The column types come from the first 100 records of the first file.
+
+  Args:
+    path (str): The file path to read from.  The path can contain glob
+      characters such as ``*`` and ``?``.
+    error_handling: If specified, should be a mapping giving an output into
+      which to emit lines that failed to parse, as described at
+      https://beam.apache.org/documentation/sdks/yaml-errors/
+  """
+  if not error_handling:
+    return beam_io.ReadFromJson(path, **kwargs)
+  if kwargs:
+    raise ValueError(
+        f'ReadFromJson does not support {sorted(kwargs)} with error_handling.')
+
+  def read_json(text):
+    return pd.read_json(
+        io.StringIO(text), orient='records', lines=True, dtype=False)
+
+  def sample(handle):
+    good_lines = []
+    for line in itertools.islice(handle, _SAMPLE_SIZE):
+      try:
+        if isinstance(json.loads(line), dict):
+          good_lines.append(line)
+      except ValueError:
+        pass
+    if not good_lines:
+      raise ValueError(
+          f'Found no JSON objects in the first {_SAMPLE_SIZE} lines '
+          'of the first file, so the column types are unknown.')
+    return read_json(''.join(good_lines))
+
+  def parse_record(line, proxy):
+    value = json.loads(line)
+    if not isinstance(value, dict):
+      raise ValueError('Expected a JSON object.')
+    unknown_fields = set(value) - set(proxy.columns)
+    if unknown_fields:
+      raise ValueError(f'Unknown fields {sorted(unknown_fields)}.')
+    return read_json(line).reindex(columns=proxy.columns)
+
+  def parse_file(lines, proxy):
+    for line in lines:
+      if line.strip():
+        yield line, functools.partial(parse_record, line, proxy)
+
+  return _ReadFilesWithErrors(path, sample, parse_file, None, error_handling)
+
+
+# The number of records used to infer column types when error_handling is set.
+_SAMPLE_SIZE = 100
+
+
+def _join_quoted_lines(lines, comment=None):
+  """Joins lines so that a quoted csv field with newlines stays in one record.
+
+  A record is complete when it has an even number of quote characters.
+  Comment lines between records are skipped.
+  """
+  record = ''
+  for line in lines:
+    if not record and comment and line.startswith(comment):
+      continue
+    record += line
+    if record.count('"') % 2 == 0:
+      yield record
+      record = ''
+  if record:
+    yield record
+
+
+# The type of the error rows from _ReadFilesWithErrors.
+_FILE_ERROR_TYPE = RowTypeConstraint.from_fields([
+    (
+        'element',
+        RowTypeConstraint.from_fields([('path', str), ('record', str)])),
+    ('msg', str),
+    ('stack', str),
+])
+
+
+class _ReadFilesWithErrors(beam.PTransform):
+  """Reads files record by record, and sends bad records to an error output.
+
+  Args:
+    path: The file pattern to read.
+    sample: A function that takes a text file handle and returns a
+      pandas DataFrame. Its column types are the output column types.
+    parse_file: A function that takes the lines of one file and the sample.
+      It yields (record, parse) pairs. Calling parse() returns a DataFrame
+      with zero or one rows, or raises an exception for a bad record.
+    filename_column: If set, a column to add with the source file path.
+    error_handling: The error_handling config.
+  """
+  def __init__(self, path, sample, parse_file, filename_column, 
error_handling):
+    self._path = path
+    self._sample = sample
+    self._parse_file = parse_file
+    self._filename_column = filename_column
+    self._error_output = yaml_utils.SafeLineLoader.strip_metadata(
+        error_handling)['output']
+
+  def expand(self, root):
+    match = beam_io.filesystems.FileSystems.match([self._path], limits=[1])[0]
+    if not match.metadata_list:
+      raise FileNotFoundError(f'Found no files that match {self._path!r}')
+    with beam_io.filesystems.FileSystems.open(

Review Comment:
   Compressed input breaks with error_handling.
   
   .csv.gz with error_handling fails at construction: 'CompressedFile' object 
has no attribute 'writable' (from io.TextIOWrapper).
   Without error_handling, the same file reads. So this is a regression for CSV.
   After that, for line in handle would fail too: TypeError: 'CompressedFile' 
object is not iterable



##########
sdks/python/apache_beam/yaml/yaml_io.py:
##########
@@ -99,6 +104,260 @@ def write_to_text(pcoll, path: str):
       lambda x: str(getattr(x, sole_field_name))) | beam.io.WriteToText(path)
 
 
+def read_from_csv(
+    path: str,
+    *,
+    sep: str = ',',
+    comment: Optional[str] = None,
+    filename_column: Optional[str] = None,
+    error_handling: Optional[Mapping[str, Any]] = None,
+    **kwargs):
+  """Reads comma-separated values (csv) files into a PCollection of rows.
+
+  Without ``error_handling``, this is ``apache_beam.io.ReadFromCsv``.
+
+  With ``error_handling``, each record is parsed on its own, and records
+  that fail to parse go to the error output instead of failing the pipeline.
+  Each error row has the source file path and the raw record text.
+  The column types come from the first 100 records of the first file.
+
+  Args:
+    path (str): The file path to read from.  The path can contain glob
+      characters such as ``*`` and ``?``.
+    sep (str): The field delimiter.
+    comment (str): Lines that start with this character are skipped.
+    filename_column (str): If set, the name of a column to add to each
+      record, containing the path of the source file.
+    error_handling: If specified, should be a mapping giving an output into
+      which to emit records that failed to parse, as described at
+      https://beam.apache.org/documentation/sdks/yaml-errors/
+  """
+  if not error_handling:
+    return beam_io.ReadFromCsv(
+        path,
+        sep=sep,
+        comment=comment,
+        filename_column=filename_column,
+        **kwargs)
+  if kwargs:
+    raise ValueError(
+        f'ReadFromCsv does not support {sorted(kwargs)} with error_handling.')
+
+  def sample(handle):
+    return pd.read_csv(
+        handle,
+        sep=sep,
+        comment=comment,
+        nrows=_SAMPLE_SIZE,
+        on_bad_lines='skip')
+
+  def parse_record(header, record, dtypes):
+    columns = list(dtypes)
+    fields = pd.read_csv(
+        io.StringIO(record), header=None, sep=sep, comment=comment, dtype=str)
+    if len(fields.columns) != len(columns):
+      raise ValueError(
+          f'Expected {len(columns)} fields, got {len(fields.columns)}.')
+    df = pd.read_csv(
+        io.StringIO(header + record), sep=sep, comment=comment, dtype=dtypes)
+    if list(df.columns) != columns:
+      raise ValueError(
+          f'The file header {list(df.columns)} does not match {columns}.')
+    return df
+
+  def parse_file(lines, proxy):
+    dtypes = proxy.dtypes.drop(filename_column, errors='ignore').to_dict()
+    header = None
+    for record in _join_quoted_lines(lines, comment):
+      if not record.strip():
+        continue
+      if header is None:
+        header = record
+      else:
+        yield record, functools.partial(parse_record, header, record, dtypes)
+
+  return _ReadFilesWithErrors(
+      path, sample, parse_file, filename_column, error_handling)
+
+
+def read_from_json(
+    path: str, *, error_handling: Optional[Mapping[str, Any]] = None, 
**kwargs):
+  """Reads newline-delimited JSON files into a PCollection of rows.
+
+  Without ``error_handling``, this is ``apache_beam.io.ReadFromJson``.
+
+  With ``error_handling``, each line is parsed on its own, and lines
+  that fail to parse go to the error output instead of failing the pipeline.
+  Each error row has the source file path and the raw line.
+  The column types come from the first 100 records of the first file.
+
+  Args:
+    path (str): The file path to read from.  The path can contain glob
+      characters such as ``*`` and ``?``.
+    error_handling: If specified, should be a mapping giving an output into
+      which to emit lines that failed to parse, as described at
+      https://beam.apache.org/documentation/sdks/yaml-errors/
+  """
+  if not error_handling:
+    return beam_io.ReadFromJson(path, **kwargs)
+  if kwargs:
+    raise ValueError(
+        f'ReadFromJson does not support {sorted(kwargs)} with error_handling.')
+
+  def read_json(text):
+    return pd.read_json(
+        io.StringIO(text), orient='records', lines=True, dtype=False)
+
+  def sample(handle):
+    good_lines = []
+    for line in itertools.islice(handle, _SAMPLE_SIZE):
+      try:
+        if isinstance(json.loads(line), dict):
+          good_lines.append(line)
+      except ValueError:
+        pass
+    if not good_lines:
+      raise ValueError(
+          f'Found no JSON objects in the first {_SAMPLE_SIZE} lines '
+          'of the first file, so the column types are unknown.')
+    return read_json(''.join(good_lines))
+
+  def parse_record(line, proxy):
+    value = json.loads(line)
+    if not isinstance(value, dict):
+      raise ValueError('Expected a JSON object.')
+    unknown_fields = set(value) - set(proxy.columns)
+    if unknown_fields:
+      raise ValueError(f'Unknown fields {sorted(unknown_fields)}.')
+    return read_json(line).reindex(columns=proxy.columns)
+
+  def parse_file(lines, proxy):
+    for line in lines:
+      if line.strip():
+        yield line, functools.partial(parse_record, line, proxy)
+
+  return _ReadFilesWithErrors(path, sample, parse_file, None, error_handling)
+
+
+# The number of records used to infer column types when error_handling is set.
+_SAMPLE_SIZE = 100
+
+
+def _join_quoted_lines(lines, comment=None):
+  """Joins lines so that a quoted csv field with newlines stays in one record.
+
+  A record is complete when it has an even number of quote characters.
+  Comment lines between records are skipped.
+  """
+  record = ''
+  for line in lines:
+    if not record and comment and line.startswith(comment):
+      continue
+    record += line
+    if record.count('"') % 2 == 0:
+      yield record
+      record = ''
+  if record:
+    yield record
+
+
+# The type of the error rows from _ReadFilesWithErrors.
+_FILE_ERROR_TYPE = RowTypeConstraint.from_fields([
+    (
+        'element',
+        RowTypeConstraint.from_fields([('path', str), ('record', str)])),
+    ('msg', str),
+    ('stack', str),
+])
+
+
+class _ReadFilesWithErrors(beam.PTransform):
+  """Reads files record by record, and sends bad records to an error output.
+
+  Args:
+    path: The file pattern to read.
+    sample: A function that takes a text file handle and returns a
+      pandas DataFrame. Its column types are the output column types.
+    parse_file: A function that takes the lines of one file and the sample.
+      It yields (record, parse) pairs. Calling parse() returns a DataFrame
+      with zero or one rows, or raises an exception for a bad record.
+    filename_column: If set, a column to add with the source file path.
+    error_handling: The error_handling config.
+  """
+  def __init__(self, path, sample, parse_file, filename_column, 
error_handling):
+    self._path = path
+    self._sample = sample
+    self._parse_file = parse_file
+    self._filename_column = filename_column
+    self._error_output = yaml_utils.SafeLineLoader.strip_metadata(
+        error_handling)['output']
+
+  def expand(self, root):
+    match = beam_io.filesystems.FileSystems.match([self._path], limits=[1])[0]
+    if not match.metadata_list:
+      raise FileNotFoundError(f'Found no files that match {self._path!r}')
+    with beam_io.filesystems.FileSystems.open(
+        match.metadata_list[0].path) as handle:
+      proxy = self._sample(
+          io.TextIOWrapper(handle, encoding='utf-8', errors='replace'))[:0]
+    if self._filename_column:
+      proxy[self._filename_column] = ''
+    # Use the string dtype for text columns, as ReadFromCsv and ReadFromJson
+    # do, so that they get a str type in the output schema.
+    for column, dtype in proxy.dtypes.items():
+      if dtype == object:
+        proxy[column] = proxy[column].astype(pd.StringDtype())
+
+    results = (
+        root
+        | beam_io.fileio.MatchFiles(self._path)
+        | beam.Reshuffle()
+        | beam_io.fileio.ReadMatches()
+        | beam.ParDo(self._read_file, proxy).with_outputs(
+            'errors', main='good'))
+    return {
+        'good': results.good | UnbatchPandas(proxy),
+        # This Map only sets the schema of the error rows.
+        self._error_output: results.errors
+        | beam.Map(lambda error: error).with_output_types(_FILE_ERROR_TYPE),
+    }
+
+  def _read_file(self, readable_file, proxy):
+    path = readable_file.metadata.path
+    decode_errors = []
+
+    def error(record, exn):
+      return beam.pvalue.TaggedOutput(
+          'errors',
+          beam.Row(
+              element=beam.Row(path=path, record=record),
+              msg=str(exn),
+              stack=''.join(
+                  traceback.format_exception(type(exn), exn,
+                                             exn.__traceback__))))
+
+    def decoded_lines():
+      # Lines that are not valid UTF-8 are skipped and reported as errors.
+      with readable_file.open() as handle:
+        for line in handle:
+          try:
+            yield line.decode('utf-8')
+          except UnicodeDecodeError as exn:
+            decode_errors.append(error(line.decode('utf-8', 'replace'), exn))
+
+    for record, parse in self._parse_file(decoded_lines(), proxy):
+      try:
+        df = parse()
+        if self._filename_column:
+          df[self._filename_column] = path
+        df = df.astype(proxy.dtypes)

Review Comment:
   JSON path silently coerces bad values into "good" rows.
   
   df.astype(proxy.dtypes) converts values instead of checking them. Sample 
types are count: int64, flag: bool.
   
   Turning on a dead-letter queue should never make data less correct.



##########
sdks/python/apache_beam/yaml/yaml_io.py:
##########
@@ -99,6 +104,260 @@ def write_to_text(pcoll, path: str):
       lambda x: str(getattr(x, sole_field_name))) | beam.io.WriteToText(path)
 
 
+def read_from_csv(
+    path: str,
+    *,
+    sep: str = ',',
+    comment: Optional[str] = None,
+    filename_column: Optional[str] = None,
+    error_handling: Optional[Mapping[str, Any]] = None,
+    **kwargs):
+  """Reads comma-separated values (csv) files into a PCollection of rows.
+
+  Without ``error_handling``, this is ``apache_beam.io.ReadFromCsv``.
+
+  With ``error_handling``, each record is parsed on its own, and records
+  that fail to parse go to the error output instead of failing the pipeline.
+  Each error row has the source file path and the raw record text.
+  The column types come from the first 100 records of the first file.
+
+  Args:
+    path (str): The file path to read from.  The path can contain glob
+      characters such as ``*`` and ``?``.
+    sep (str): The field delimiter.
+    comment (str): Lines that start with this character are skipped.
+    filename_column (str): If set, the name of a column to add to each
+      record, containing the path of the source file.
+    error_handling: If specified, should be a mapping giving an output into
+      which to emit records that failed to parse, as described at
+      https://beam.apache.org/documentation/sdks/yaml-errors/
+  """
+  if not error_handling:
+    return beam_io.ReadFromCsv(
+        path,
+        sep=sep,
+        comment=comment,
+        filename_column=filename_column,
+        **kwargs)
+  if kwargs:
+    raise ValueError(
+        f'ReadFromCsv does not support {sorted(kwargs)} with error_handling.')
+
+  def sample(handle):
+    return pd.read_csv(
+        handle,
+        sep=sep,
+        comment=comment,
+        nrows=_SAMPLE_SIZE,
+        on_bad_lines='skip')
+
+  def parse_record(header, record, dtypes):
+    columns = list(dtypes)
+    fields = pd.read_csv(
+        io.StringIO(record), header=None, sep=sep, comment=comment, dtype=str)
+    if len(fields.columns) != len(columns):
+      raise ValueError(
+          f'Expected {len(columns)} fields, got {len(fields.columns)}.')
+    df = pd.read_csv(
+        io.StringIO(header + record), sep=sep, comment=comment, dtype=dtypes)
+    if list(df.columns) != columns:
+      raise ValueError(
+          f'The file header {list(df.columns)} does not match {columns}.')
+    return df
+
+  def parse_file(lines, proxy):
+    dtypes = proxy.dtypes.drop(filename_column, errors='ignore').to_dict()

Review Comment:
   CSV error path crashes when filename_column is unset.
   
   Series.drop(None) always raises ValueError: Need to specify at least one of 
'labels', 'index' or 'columns'. The error comes from inside the DoFn, so the 
pipeline fails on workers. Every CSV probe without filename_column failed this 
way.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to