BePPPower commented on code in PR #50225: URL: https://github.com/apache/doris/pull/50225#discussion_r2057855229
########## fe/fe-core/src/main/java/org/apache/doris/datasource/property/fileformat/CsvFileFormatProperties.java: ########## @@ -0,0 +1,195 @@ +// 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. + +package org.apache.doris.datasource.property.fileformat; + +import org.apache.doris.analysis.Separator; +import org.apache.doris.catalog.Column; +import org.apache.doris.common.util.FileFormatUtils; +import org.apache.doris.common.util.Util; +import org.apache.doris.datasource.property.constants.CsvProperties; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.proto.InternalService.PFetchTableSchemaRequest; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.thrift.TFileAttributes; +import org.apache.doris.thrift.TFileFormatType; +import org.apache.doris.thrift.TFileTextScanRangeParams; +import org.apache.doris.thrift.TResultFileSinkOptions; +import org.apache.doris.thrift.TTextSerdeType; + +import com.google.common.base.Strings; +import com.google.common.collect.Lists; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; +import java.util.Map; + +public class CsvFileFormatProperties extends FileFormatProperties { + public static final Logger LOG = LogManager.getLogger( + org.apache.doris.datasource.property.fileformat.CsvFileFormatProperties.class); + + private String headerType = ""; + private TTextSerdeType textSerdeType = TTextSerdeType.JSON_TEXT_SERDE; + private String columnSeparator = CsvProperties.DEFAULT_COLUMN_SEPARATOR; + private String lineDelimiter = CsvProperties.DEFAULT_LINE_DELIMITER; + private boolean trimDoubleQuotes; + private int skipLines; + private byte enclose; + + // used by tvf + // User specified csv columns, it will override columns got from file + private final List<Column> csvSchema = Lists.newArrayList(); + + String defaultColumnSeparator = CsvProperties.DEFAULT_COLUMN_SEPARATOR; + + public CsvFileFormatProperties(TFileFormatType fileFormatType) { + super(fileFormatType); + } + + public CsvFileFormatProperties(TFileFormatType fileFormatType, String defaultColumnSeparator, + TTextSerdeType textSerdeType) { + super(fileFormatType); + this.defaultColumnSeparator = defaultColumnSeparator; + this.textSerdeType = textSerdeType; + } + + public CsvFileFormatProperties(TFileFormatType fileFormatType, String headerType) { + super(fileFormatType); + this.headerType = headerType; + } + + + @Override + public void analyzeFileFormatProperties(Map<String, String> formatProperties, boolean isRemoveOriginProperty) + throws AnalysisException { + try { + // check properties specified by user -- formatProperties + columnSeparator = getOrDefaultAndRemove(formatProperties, CsvProperties.PROP_COLUMN_SEPARATOR, + defaultColumnSeparator, isRemoveOriginProperty); + if (Strings.isNullOrEmpty(columnSeparator)) { + throw new AnalysisException("column_separator can not be empty."); + } + columnSeparator = Separator.convertSeparator(columnSeparator); + + lineDelimiter = getOrDefaultAndRemove(formatProperties, CsvProperties.PROP_LINE_DELIMITER, + CsvProperties.DEFAULT_LINE_DELIMITER, isRemoveOriginProperty); + if (Strings.isNullOrEmpty(lineDelimiter)) { + throw new AnalysisException("line_delimiter can not be empty."); + } + lineDelimiter = Separator.convertSeparator(lineDelimiter); + + String enclosedString = getOrDefaultAndRemove(formatProperties, CsvProperties.PROP_ENCLOSE, + "", isRemoveOriginProperty); + if (!Strings.isNullOrEmpty(enclosedString)) { + if (enclosedString.length() > 1) { + throw new AnalysisException("enclose should not be longer than one byte."); + } + enclose = (byte) enclosedString.charAt(0); + if (enclose == 0) { + throw new AnalysisException("enclose should not be byte [0]."); + } + } + + trimDoubleQuotes = Boolean.valueOf(getOrDefaultAndRemove(formatProperties, + CsvProperties.PROP_TRIM_DOUBLE_QUOTES, "", isRemoveOriginProperty)) + .booleanValue(); + skipLines = Integer.valueOf(getOrDefaultAndRemove(formatProperties, + CsvProperties.PROP_SKIP_LINES, "0", isRemoveOriginProperty)).intValue(); + if (skipLines < 0) { + throw new AnalysisException("skipLines should not be less than 0."); + } + + String compressTypeStr = getOrDefaultAndRemove(formatProperties, + CsvProperties.PROP_COMPRESS_TYPE, "UNKNOWN", isRemoveOriginProperty); + try { + compressionType = Util.getFileCompressType(compressTypeStr); + } catch (IllegalArgumentException e) { + throw new AnalysisException("Compress type : " + compressTypeStr + " is not supported."); + } + + FileFormatUtils.parseCsvSchema(csvSchema, getOrDefaultAndRemove(formatProperties, + CsvProperties.PROP_CSV_SCHEMA, "", isRemoveOriginProperty)); + if (LOG.isDebugEnabled()) { + LOG.debug("get csv schema: {}", csvSchema); + } + } catch (org.apache.doris.common.AnalysisException e) { Review Comment: The method `Separator.convertSeparator` maybe throw this exception -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org