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


##########
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:
   Thanks, I could reproduce this for both `.csv.gz` and `.json.gz`. Fixed in 
af13592c93d153b7961a5277166819e6d5467311: the error path now passes the binary 
handle to pandas, the same as `ReadFromCsv` does, and reads lines with 
`readline()`, so compressed files work. Added tests for both.
   
   This is just a progress update, the PR is not ready for another review yet. 
I'm still working on the JSON type coercion and the quote handling, including 
an unclosed quote in the first 100 rows, which currently fails the pipeline 
instead of going to the error output.



-- 
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