CsvReader extends FileReader implements DataReader<CsvReadOptions> { protected ColumnType[] detectColumnTypes(Reader reader, CsvReadOptions options) { boolean header = options.header(); int linesToSkip = header ? 1 : 0; CsvParser parser = csvParser(options); try { return getColumnTypes(reader, options, linesToSkip, parser); } finally { parser.stopParsing(); } } CsvReader(); static void register(ReaderRegistry registry); Table read(CsvReadOptions options); String printColumnTypes(CsvReadOptions options); @Override Table read(Source source);  }
@Test public void testDataTypeDetection() throws IOException { Reader reader = new FileReader("../data/bus_stop_test.csv"); CsvReadOptions options = CsvReadOptions.builder(reader) .header(true) .minimizeColumnSizes() .separator(',') .sample(false) .locale(Locale.getDefault()) .build(); ColumnType[] columnTypes = new CsvReader().detectColumnTypes(reader, options); assertArrayEquals(bus_types, columnTypes); }
@Test public void testNumberTypeDetectionIgnoreZeroDecimal() throws IOException { Reader reader = new FileReader("../data/immunization.csv"); CsvReadOptions options = CsvReadOptions.builder(reader).header(true).sample(false).ignoreZeroDecimal(true).build(); ColumnType[] columnTypes = new CsvReader().detectColumnTypes(reader, options); assertEquals(INTEGER, columnTypes[3]); assertEquals(INTEGER, columnTypes[7]); }
@Test public void testNumberTypeDetectionRetainZeroDecimal() throws IOException { Reader reader = new FileReader("../data/immunization.csv"); CsvReadOptions options = CsvReadOptions.builder(reader).header(true).sample(false).ignoreZeroDecimal(false).build(); ColumnType[] columnTypes = new CsvReader().detectColumnTypes(reader, options); assertEquals(DOUBLE, columnTypes[3]); assertEquals(DOUBLE, columnTypes[7]); }
@Test public void testLocalDateDetectionEnglish() { final Reader reader = new StringReader( "Date" + LINE_END + "\"Nov 1, 2017\"" + LINE_END + "\"Oct 1, 2017\"" + LINE_END + "\"Sep 1, 2017\"" + LINE_END + "\"Aug 1, 2017\"" + LINE_END + "\"Jul 1, 2017\"" + LINE_END + "\"Jun 1, 2017\"" + LINE_END); final boolean header = true; final char delimiter = ','; final boolean useSampling = true; CsvReadOptions options = CsvReadOptions.builder(reader) .header(header) .separator(delimiter) .sample(useSampling) .locale(Locale.ENGLISH) .build(); final List<ColumnType> actual = asList(new CsvReader().detectColumnTypes(reader, options)); assertEquals(Collections.singletonList(LOCAL_DATE), actual); }
@Test public void testDateTimeDetection() { final Reader reader = new StringReader( "Date" + LINE_END + "09-Nov-2014 13:03:04" + LINE_END + "09-Oct-2014 13:03:56" + LINE_END); final boolean header = true; CsvReadOptions options = CsvReadOptions.builder(reader) .header(header) .dateTimeFormat(DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss")) .build(); final List<ColumnType> actual = asList(new CsvReader().detectColumnTypes(reader, options)); assertEquals(Collections.singletonList(LOCAL_DATE_TIME), actual); }
@Test public void testDateTimeDetection2() { final Reader reader = new StringReader( "Date" + LINE_END + "09-Nov-2014 13:03:04" + LINE_END + "09-Oct-2014 13:03:56" + LINE_END); final boolean header = true; CsvReadOptions options = CsvReadOptions.builder(reader) .header(header) .dateTimeFormat(DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss")) .build(); final List<ColumnType> actual = asList(new CsvReader().detectColumnTypes(reader, options)); assertEquals(Collections.singletonList(LOCAL_DATE_TIME), actual); }
@Test public void testDateTimeDetection3() { final Reader reader = new StringReader( "Date" + LINE_END + "09-NOV-2014 13:03:04" + LINE_END + "09-OCT-2014 13:03:56" + LINE_END); final boolean header = true; CsvReadOptions options = CsvReadOptions.builder(reader) .header(header) .dateTimeFormat( new DateTimeFormatterBuilder() .parseCaseInsensitive() .appendPattern("dd-MMM-yyyy HH:mm:ss") .toFormatter()) .build(); final List<ColumnType> actual = asList(new CsvReader().detectColumnTypes(reader, options)); assertEquals(Collections.singletonList(LOCAL_DATE_TIME), actual); }
@Test public void testDateDetection1() { final Reader reader = new StringReader("Time" + LINE_END + "13.03.04" + LINE_END + "13.03.04" + LINE_END); final boolean header = true; CsvReadOptions options = CsvReadOptions.builder(reader) .header(header) .timeFormat( new DateTimeFormatterBuilder() .parseCaseInsensitive() .appendPattern("HH.mm.ss") .toFormatter()) .build(); final List<ColumnType> actual = asList(new CsvReader().detectColumnTypes(reader, options)); assertEquals(Collections.singletonList(LOCAL_TIME), actual); }
@Test public void testTimeDetection1() { final Reader reader = new StringReader("Date" + LINE_END + "09-NOV-2014" + LINE_END + "09-OCT-2014" + LINE_END); final boolean header = true; CsvReadOptions options = CsvReadOptions.builder(reader) .header(header) .dateFormat( new DateTimeFormatterBuilder() .parseCaseInsensitive() .appendPattern("dd-MMM-yyyy") .toFormatter()) .build(); final List<ColumnType> actual = asList(new CsvReader().detectColumnTypes(reader, options)); assertEquals(Collections.singletonList(LOCAL_DATE), actual); }
@Test public void testLocalDateDetectionFrench() { final Reader reader = new StringReader( "Date" + LINE_END + "\"nov. 1, 2017\"" + LINE_END + "\"oct. 1, 2017\"" + LINE_END + "\"sept. 1, 2017\"" + LINE_END + "\"août 1, 2017\"" + LINE_END + "\"juil. 1, 2017\"" + LINE_END + "\"juin 1, 2017\"" + LINE_END); final boolean header = true; final char delimiter = ','; final boolean useSampling = true; CsvReadOptions options = CsvReadOptions.builder(reader) .header(header) .separator(delimiter) .sample(useSampling) .locale(Locale.FRENCH) .build(); final List<ColumnType> actual = asList(new CsvReader().detectColumnTypes(reader, options)); assertEquals(Collections.singletonList(LOCAL_DATE), actual); }
@Test public void testLocalDateTimeDetectionFrench() { final Reader reader = new StringReader( "Date" + LINE_END + "09-nov.-2014 13:03" + LINE_END + "09-oct.-2014 13:03" + LINE_END + "09-sept.-2014 13:03" + LINE_END + "09-août-2014 13:03" + LINE_END + "09-juil.-2014 13:03" + LINE_END + "09-juin-2014 13:03" + LINE_END); final boolean header = true; final char delimiter = ','; final boolean useSampling = true; CsvReadOptions options = CsvReadOptions.builder(reader) .header(header) .separator(delimiter) .sample(useSampling) .locale(Locale.FRENCH) .build(); final List<ColumnType> actual = asList(new CsvReader().detectColumnTypes(reader, options)); assertEquals(Collections.singletonList(LOCAL_DATE_TIME), actual); }
@Test public void testDataTypeDetection2() throws IOException { Reader reader = new FileReader("../data/bush.csv"); CsvReadOptions options = CsvReadOptions.builder(reader) .header(true) .separator(',') .sample(false) .locale(Locale.getDefault()) .build(); ColumnType[] columnTypes = new CsvReader().detectColumnTypes(reader, options); assertEquals(LOCAL_DATE, columnTypes[0]); assertEquals(INTEGER, columnTypes[1]); assertEquals(STRING, columnTypes[2]); }