Copilot commented on code in PR #6019: URL: https://github.com/apache/paimon/pull/6019#discussion_r2253472748
########## paimon-core/src/test/java/org/apache/paimon/table/TableTestBase.java: ########## @@ -87,8 +87,8 @@ public void beforeEach() throws Catalog.DatabaseAlreadyExistException { public void after() throws IOException { // assert all connections are closed Predicate<Path> pathPredicate = path -> path.toString().contains(tempPath.toString()); - assertThat(TraceableFileIO.openInputStreams(pathPredicate)).isEmpty(); - assertThat(TraceableFileIO.openOutputStreams(pathPredicate)).isEmpty(); + assertThat(TraceableFileIO.openInputStreams(pathPredicate).isEmpty()); Review Comment: The assertion should use assertThat().isEmpty() instead of assertThat(collection.isEmpty()). The current form tests the boolean result rather than the collection itself. ```suggestion assertThat(TraceableFileIO.openInputStreams(pathPredicate)).isEmpty(); ``` ########## paimon-format/src/main/java/org/apache/paimon/format/json/JsonFormatWriter.java: ########## @@ -0,0 +1,82 @@ +/* + * 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.paimon.format.json; + +import org.apache.paimon.data.InternalRow; +import org.apache.paimon.format.FormatWriter; +import org.apache.paimon.fs.PositionOutputStream; +import org.apache.paimon.options.Options; +import org.apache.paimon.types.RowType; + +import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; + +/** A {@link FormatWriter} implementation for JSON format. */ +public class JsonFormatWriter implements FormatWriter { + + private final PositionOutputStream outputStream; + private final Writer writer; + private final RowToJsonConverter converter; + private final ObjectMapper objectMapper; + + public JsonFormatWriter(PositionOutputStream outputStream, RowType rowType, Options options) { + this.outputStream = outputStream; + this.writer = + new OutputStreamWriter( + new CloseShieldOutputStream(outputStream), StandardCharsets.UTF_8); + this.converter = new RowToJsonConverter(rowType, options); + this.objectMapper = new ObjectMapper(); + } + + @Override + public void addElement(InternalRow element) throws IOException { + Object jsonObject = converter.convert(element); + String jsonString = objectMapper.writeValueAsString(jsonObject); + writer.write(jsonString); + writer.write('\n'); // JSON lines format - one JSON object per line + } + + @Override + public void close() throws IOException { + if (writer != null) { + try { + writer.flush(); + writer.close(); + } catch (IOException e) { + // If the underlying stream is already closed, ignore the exception + if (!e.getMessage().contains("Already closed")) { + throw e; + } Review Comment: String-based exception matching is fragile and error-prone. Consider checking the exception type or using a more robust method to detect already-closed streams. ```suggestion // Ignore exceptions during close, as CloseShieldOutputStream prevents closing the underlying stream. // If you want to log the exception, you can do so here. // e.g., log.warn("Exception during writer close", e); ``` ########## paimon-format/src/main/java/org/apache/paimon/format/csv/CloseShieldOutputStream.java: ########## @@ -0,0 +1,65 @@ +/* + * 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.paimon.format.csv; + +import java.io.IOException; +import java.io.OutputStream; + +/** A proxy output stream that prevents the underlying output stream from being closed. */ +public class CloseShieldOutputStream extends OutputStream { + private final OutputStream out; + + public CloseShieldOutputStream(OutputStream out) { + this.out = out; + } + + @Override + public void write(int b) throws IOException { + out.write(b); + } + + @Override + public void write(byte[] buffer) throws IOException { + out.write(buffer); + } + + @Override + public void write(byte[] buffer, int off, int len) throws IOException { + out.write(buffer, off, len); + } + + @Override + public void flush() throws IOException { + try { + out.flush(); + } catch (IOException e) { + // If the underlying stream is already closed, ignore the exception + if (e.getMessage() != null && e.getMessage().contains("Already closed")) { + // Silently ignore already closed exception + return; + } Review Comment: String-based exception matching is fragile and error-prone. Consider checking the exception type or using a more robust method to detect already-closed streams. ```suggestion // Do not attempt to match exception messages; propagate the exception. ``` ########## paimon-format/src/main/java/org/apache/paimon/format/json/CloseShieldOutputStream.java: ########## @@ -0,0 +1,65 @@ +/* + * 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.paimon.format.json; + +import java.io.IOException; +import java.io.OutputStream; + +/** A proxy output stream that prevents the underlying output stream from being closed. */ +public class CloseShieldOutputStream extends OutputStream { + private final OutputStream out; + + public CloseShieldOutputStream(OutputStream out) { + this.out = out; + } + + @Override + public void write(int b) throws IOException { + out.write(b); + } + + @Override + public void write(byte[] buffer) throws IOException { + out.write(buffer); + } + + @Override + public void write(byte[] buffer, int off, int len) throws IOException { + out.write(buffer, off, len); + } + + @Override + public void flush() throws IOException { + try { + out.flush(); + } catch (IOException e) { + // If the underlying stream is already closed, ignore the exception + if (e.getMessage() != null && e.getMessage().contains("Already closed")) { + // Silently ignore already closed exception + return; + } + throw e; + } + } + + @Override + public void close() throws IOException { Review Comment: String-based exception matching is fragile and error-prone. Consider checking the exception type or using a more robust method to detect already-closed streams. ```suggestion if (closed) { // Silently ignore flush if stream is closed return; } out.flush(); } @Override public void close() throws IOException { closed = true; ``` ########## paimon-core/src/test/java/org/apache/paimon/table/TableTestBase.java: ########## @@ -87,8 +87,8 @@ public void beforeEach() throws Catalog.DatabaseAlreadyExistException { public void after() throws IOException { // assert all connections are closed Predicate<Path> pathPredicate = path -> path.toString().contains(tempPath.toString()); - assertThat(TraceableFileIO.openInputStreams(pathPredicate)).isEmpty(); - assertThat(TraceableFileIO.openOutputStreams(pathPredicate)).isEmpty(); + assertThat(TraceableFileIO.openInputStreams(pathPredicate).isEmpty()); + assertThat(TraceableFileIO.openOutputStreams(pathPredicate).isEmpty()); Review Comment: The assertion should use assertThat().isEmpty() instead of assertThat(collection.isEmpty()). The current form tests the boolean result rather than the collection itself. ```suggestion assertThat(TraceableFileIO.openOutputStreams(pathPredicate)).isEmpty(); ``` ########## paimon-format/src/main/java/org/apache/paimon/format/csv/CloseShieldPositionOutputStream.java: ########## @@ -0,0 +1,74 @@ +/* + * 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.paimon.format.csv; + +import org.apache.paimon.fs.PositionOutputStream; + +import java.io.IOException; + +/** + * A proxy position output stream that prevents the underlying output stream from being closed or + * flushed when already closed. + */ +public class CloseShieldPositionOutputStream extends PositionOutputStream { + private final PositionOutputStream out; + + public CloseShieldPositionOutputStream(PositionOutputStream out) { + this.out = out; + } + + @Override + public long getPos() throws IOException { + return out.getPos(); + } + + @Override + public void write(int b) throws IOException { + out.write(b); + } + + @Override + public void write(byte[] b) throws IOException { + out.write(b); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + out.write(b, off, len); + } + + @Override + public void flush() throws IOException { + try { + out.flush(); + } catch (IOException e) { + // If the underlying stream is already closed, ignore the exception + if (e.getMessage() != null && e.getMessage().contains("Already closed")) { + // Silently ignore already closed exception + return; + } Review Comment: String-based exception matching is fragile and error-prone. Consider checking the exception type or using a more robust method to detect already-closed streams. ```suggestion // String-based exception matching is fragile and error-prone. // Consider catching a specific exception type if available. ``` ########## paimon-format/src/main/java/org/apache/paimon/format/csv/CsvFormatWriter.java: ########## @@ -0,0 +1,185 @@ +/* + * 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.paimon.format.csv; + +import org.apache.paimon.data.BinaryString; +import org.apache.paimon.data.Decimal; +import org.apache.paimon.data.InternalRow; +import org.apache.paimon.data.Timestamp; +import org.apache.paimon.format.FormatWriter; +import org.apache.paimon.fs.PositionOutputStream; +import org.apache.paimon.options.Options; +import org.apache.paimon.types.DataType; +import org.apache.paimon.types.DataTypeRoot; +import org.apache.paimon.types.RowType; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** CSV format writer implementation. */ +public class CsvFormatWriter implements FormatWriter { + + private final RowType rowType; + private final String fieldDelimiter; + private final String lineDelimiter; + private final String quoteCharacter; + private final String escapeCharacter; + private final String nullLiteral; + private final boolean includeHeader; + + private final BufferedWriter writer; + private final PositionOutputStream outputStream; + private boolean headerWritten = false; + + private static final DateTimeFormatter DATE_FORMATTER = + DateTimeFormatter.ofPattern("yyyy-MM-dd"); + private static final DateTimeFormatter TIMESTAMP_FORMATTER = + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + public CsvFormatWriter(PositionOutputStream out, RowType rowType, Options options) + throws IOException { + this.rowType = rowType; + this.fieldDelimiter = options.getString("csv.field-delimiter", ","); + this.lineDelimiter = options.getString("csv.line-delimiter", "\n"); + this.quoteCharacter = options.getString("csv.quote-character", "\""); + this.escapeCharacter = options.getString("csv.escape-character", "\\"); + this.nullLiteral = options.getString("csv.null-literal", ""); + this.includeHeader = options.getBoolean("csv.include-header", false); + + this.outputStream = out; + this.writer = + new BufferedWriter( + new OutputStreamWriter( + new CloseShieldOutputStream(out), StandardCharsets.UTF_8)); + } + + @Override + public void addElement(InternalRow element) throws IOException { + // Write header if needed + if (includeHeader && !headerWritten) { + writeHeader(); + headerWritten = true; + } + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < rowType.getFieldCount(); i++) { + if (i > 0) { + sb.append(fieldDelimiter); + } + + Object value = + InternalRow.createFieldGetter(rowType.getTypeAt(i), i).getFieldOrNull(element); + String fieldValue = formatField(value, rowType.getTypeAt(i)); + sb.append(escapeField(fieldValue)); + } + sb.append(lineDelimiter); + + writer.write(sb.toString()); + } + + @Override + public void close() throws IOException { + if (writer != null) { + try { + writer.close(); + } catch (IOException e) { + // If the underlying stream is already closed, ignore the exception + if (!e.getMessage().contains("Already closed")) { + throw e; + } + } + } Review Comment: String-based exception matching is fragile and error-prone. Consider checking the exception type or using a more robust method to detect already-closed streams. ```suggestion if (closed) { return; } if (writer != null) { writer.close(); } closed = true; ``` -- 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: issues-unsubscr...@paimon.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org