Skip to content

Commit

Permalink
[Enhancement] Select into outfile support invisible character column …
Browse files Browse the repository at this point in the history
…separator and line delimiter (#29410)

---------

Signed-off-by: wyb <wybb86@gmail.com>
(cherry picked from commit 4264907)
  • Loading branch information
wyb authored and mergify[bot] committed Aug 18, 2023
1 parent b90db85 commit 4cc4bb2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ private void analyzeProperties() throws AnalysisException {
if (!isCsvFormat()) {
throw new AnalysisException(PROP_COLUMN_SEPARATOR + " is only for CSV format");
}
columnSeparator = properties.get(PROP_COLUMN_SEPARATOR);
columnSeparator = Delimiter.convertDelimiter(properties.get(PROP_COLUMN_SEPARATOR));
}

if (properties.containsKey(PROP_LINE_DELIMITER)) {
if (!isCsvFormat()) {
throw new AnalysisException(PROP_LINE_DELIMITER + " is only for CSV format");
}
rowDelimiter = properties.get(PROP_LINE_DELIMITER);
rowDelimiter = Delimiter.convertDelimiter(properties.get(PROP_LINE_DELIMITER));
}

if (properties.containsKey(PARQUET_COMPRESSION_TYPE)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed 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
//
// https://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 com.starrocks.analysis;

import com.google.common.collect.Maps;
import com.starrocks.sql.analyzer.RelationFields;
import com.starrocks.sql.analyzer.RelationId;
import com.starrocks.sql.analyzer.Scope;
import org.junit.Assert;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.Map;

public class OutFileClauseTest {

@Test
public void testAnalyzeProperties() throws UnsupportedEncodingException {
Scope scope = new Scope(RelationId.anonymous(), new RelationFields());
Map<String, String> properties = Maps.newHashMap();

// default column separator and line delimiter
OutFileClause clause = new OutFileClause("file_path", "csv", properties);
clause.analyze(scope);
Assert.assertEquals("\t", clause.getColumnSeparator());
Assert.assertEquals("\n", clause.getRowDelimiter());

// column separator: | and line delimiter: ,
properties.clear();
properties.put("column_separator", "|");
properties.put("line_delimiter", ",");
clause = new OutFileClause("file_path", "csv", properties);
clause.analyze(scope);
Assert.assertEquals("|", clause.getColumnSeparator());
Assert.assertEquals(",", clause.getRowDelimiter());

// invisible character column separator and line delimiter
properties.clear();
properties.put("column_separator", "\\x01");
properties.put("line_delimiter", "\\x02");
clause = new OutFileClause("file_path", "csv", properties);
clause.analyze(scope);
Assert.assertEquals(new String(new byte[] {1}, "UTF-8"), clause.getColumnSeparator());
Assert.assertEquals(new String(new byte[] {2}, "UTF-8"), clause.getRowDelimiter());
}
}

0 comments on commit 4cc4bb2

Please sign in to comment.