Class: Embulk::Guess::CsvGuessPlugin

Inherits:
LineGuessPlugin show all
Defined in:
lib/embulk/guess/csv.rb

Defined Under Namespace

Classes: TimestampMatch

Constant Summary collapse

DELIMITER_CANDIDATES =
[
  ",", "\t", "|"
]
QUOTE_CANDIDATES =
[
  "\"", "'"
]
TRUE_STRINGS =

CsvParserPlugin.TRUE_STRINGS

Instance Method Summary collapse

Methods inherited from LineGuessPlugin

#guess

Methods inherited from Embulk::GuessPlugin

from_java, #guess, new_java

Instance Method Details

#guess_lines(config, sample_lines) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/embulk/guess/csv.rb', line 25

def guess_lines(config, sample_lines)
  delim = guess_delimiter(sample_lines)
  unless delim
    # not CSV file
    return {}
  end

  parser_config = config["parser"] || {}
  parser_guessed = {"type" => "csv", "delimiter" => delim}

  quote = guess_quote(sample_lines, delim)
  parser_guessed["quote"] = quote ? quote : ''

  sample_records = sample_lines.map {|line| line.split(delim) }  # TODO use CsvTokenizer
  first_types = guess_field_types(sample_records[0, 1])
  other_types = guess_field_types(sample_records[1..-1])

  if first_types.size <= 1 || other_types.size <= 1
    # guess failed
    return {}
  end

  unless parser_config.has_key?("header_line")
    parser_guessed["header_line"] = (first_types != other_types && !first_types.any? {|t| t != ["string"] })
  end

  unless parser_config.has_key?("columns")
    if parser_guessed["header_line"] || parser_config["header_line"]
      column_names = sample_records.first
    else
      column_names = (0..other_types.size).to_a.map {|i| "c#{i}" }
    end
    schema = []
    column_names.zip(other_types).each do |name,(type,format)|
      if name && type
        if format
          schema << {"name" => name, "type" => type, "format" => format}
        else
          schema << {"name" => name, "type" => type}
        end
      end
    end
    parser_guessed["columns"] = schema
  end

  return {"parser" => parser_guessed}
end