You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to you under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */packageorg.apache.wayang.api.sql.sources.fs;
importau.com.bytecode.opencsv.CSVParser;
importorg.apache.calcite.avatica.util.DateTimeUtils;
importorg.apache.calcite.rel.type.RelDataType;
importorg.apache.commons.lang3.time.FastDateFormat;
importjava.io.IOException;
importjava.math.BigDecimal;
importjava.math.RoundingMode;
importjava.text.ParseException;
importjava.util.Date;
importjava.util.Locale;
importjava.util.TimeZone;
/** * Based on Calcite's CSV enumerator. * TODO: handle different variants * */publicclassCsvRowConverter {
privatestaticfinalCSVParserparser;
privatestaticfinalFastDateFormatTIME_FORMAT_DATE;
privatestaticfinalFastDateFormatTIME_FORMAT_TIME;
privatestaticfinalFastDateFormatTIME_FORMAT_TIMESTAMP;
static {
finalTimeZonegmt = TimeZone.getTimeZone("GMT");
TIME_FORMAT_DATE = FastDateFormat.getInstance("yyyy-MM-dd", gmt);
TIME_FORMAT_TIME = FastDateFormat.getInstance("HH:mm:ss", gmt);
TIME_FORMAT_TIMESTAMP = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss", gmt);
parser = newCSVParser();
}
publicstaticObjectconvert(RelDataTypefieldType, Stringstring) {
if (fieldType == null || string == null) {
returnstring;
}
switch (fieldType.getSqlTypeName()) {
caseBOOLEAN:
if (string.length() == 0) {
returnnull;
}
returnBoolean.parseBoolean(string);
caseTINYINT:
if (string.length() == 0) {
returnnull;
}
returnByte.parseByte(string);
caseSMALLINT:
if (string.length() == 0) {
returnnull;
}
returnShort.parseShort(string);
caseINTEGER:
if (string.length() == 0) {
returnnull;
}
returnInteger.parseInt(string);
caseBIGINT:
if (string.length() == 0) {
returnnull;
}
returnLong.parseLong(string);
caseFLOAT:
if (string.length() == 0) {
returnnull;
}
returnFloat.parseFloat(string);
caseDOUBLE:
if (string.length() == 0) {
returnnull;
}
returnDouble.parseDouble(string);
caseDECIMAL:
if (string.length() == 0) {
returnnull;
}
returnparseDecimal(fieldType.getPrecision(), fieldType.getScale(), string);
caseDATE:
if (string.length() == 0) {
returnnull;
}
try {
Datedate = TIME_FORMAT_DATE.parse(string);
return (int) (date.getTime() / DateTimeUtils.MILLIS_PER_DAY);
} catch (ParseExceptione) {
returnnull;
}
caseTIME:
if (string.length() == 0) {
returnnull;
}
try {
Datedate = TIME_FORMAT_TIME.parse(string);
return (int) date.getTime();
} catch (ParseExceptione) {
returnnull;
}
caseTIMESTAMP:
if (string.length() == 0) {
returnnull;
}
try {
Datedate = TIME_FORMAT_TIMESTAMP.parse(string);
returndate.getTime();
} catch (ParseExceptione) {
returnnull;
}
caseVARCHAR:
default:
returnstring;
}
}
privatestaticBigDecimalparseDecimal(intprecision, intscale, Stringstring) {
BigDecimalresult = newBigDecimal(string);
// If the parsed value has more fractional digits than the specified scale, round ties away// from 0.if (result.scale() > scale) {
//TODO: enable logging/*LOGGER.warn( "Decimal value {} exceeds declared scale ({}). Performing rounding to keep the " + "first {} fractional digits.", result, scale, scale);*/result = result.setScale(scale, RoundingMode.HALF_UP);
}
// Throws an exception if the parsed value has more digits to the left of the decimal point// than the specified value.if (result.precision() - result.scale() > precision - scale) {
thrownewIllegalArgumentException(String
.format(Locale.ROOT, "Decimal value %s exceeds declared precision (%d) and scale (%d).",
result, precision, scale));
}
returnresult;
}
publicstaticString[] parseLine(Strings) throwsIOException {
returnparser.parseLine(s);
}
/** * Parse line with a separator * @param s * @param separator * @return * @throws IOException */publicstaticString[] parseLine(Strings, charseparator) throwsIOException {
CSVParsercsvParser = newCSVParser(separator);
returncsvParser.parseLine(s);
}
}
c3fd38ac20a33309a09d22bf208ddedb82c295b9
The text was updated successfully, but these errors were encountered:
handle different variants
https://github.com/databloom-ai/incubator-wayang/blob/c51b95e414816ed2b6ac7f8f3cad5857b40fea3b/wayang-api/wayang-api-sql/src/main/java/org/apache/wayang/api/sql/sources/fs/CsvRowConverter.java#L35
c3fd38ac20a33309a09d22bf208ddedb82c295b9
The text was updated successfully, but these errors were encountered: