lidavidm commented on code in PR #14081:
URL: https://github.com/apache/arrow/pull/14081#discussion_r967105833


##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/Constants.java:
##########
@@ -27,5 +27,6 @@ private Constants() {}
   public static final String SQL_TABLE_NAME_KEY = "SQL_TABLE_NAME";
   public static final String SQL_COLUMN_NAME_KEY = "SQL_COLUMN_NAME";
   public static final String SQL_TYPE_KEY = "SQL_TYPE";
+  public static final String COMMENT = "comment";

Review Comment:
   Also, since the JDBC name for this is REMARKS, should we stay consistent 
with that?



##########
java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcToArrowCommentMetadataTest.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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.arrow.adapter.jdbc;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.types.pojo.Schema;
+import org.apache.arrow.vector.util.ObjectMapperFactory;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.fasterxml.jackson.databind.ObjectWriter;
+
+public class JdbcToArrowCommentMetadataTest {
+
+  private final ObjectWriter schemaSerializer = 
ObjectMapperFactory.newObjectMapper().writerWithDefaultPrettyPrinter();
+  private Connection conn = null;
+
+  /**
+   * This method creates Connection object and DB table and also populate data 
into table for test.
+   *
+   * @throws SQLException on error
+   * @throws ClassNotFoundException on error
+   */
+  @Before
+  public void setUp() throws SQLException, ClassNotFoundException {
+    String url = 
"jdbc:h2:mem:JdbcToArrowTest?characterEncoding=UTF-8;INIT=runscript from 
'classpath:/h2/comment.sql'";
+    String driver = "org.h2.Driver";
+    Class.forName(driver);
+    conn = DriverManager.getConnection(url);
+  }
+
+  @After
+  public void tearDown() throws SQLException {
+    if (conn != null) {
+      conn.close();
+      conn = null;
+    }
+  }
+
+  @Test
+  public void schemaComment() throws Exception {
+    boolean includeMetadata = false;
+    String schemaJson = 
schemaSerializer.writeValueAsString(getSchemaWithCommentFromQuery(includeMetadata));
+    String expectedSchema = 
getExpectedSchema("/h2/expectedSchemaWithComments.json");
+    assertThat(schemaJson).isEqualTo(expectedSchema);
+  }
+
+  @Test
+  public void schemaCommentWithDatabaseMetadata() throws Exception {
+    boolean includeMetadata = true;
+    String schemaJson = 
schemaSerializer.writeValueAsString(getSchemaWithCommentFromQuery(includeMetadata));
+    String expectedSchema = 
getExpectedSchema("/h2/expectedSchemaWithCommentsAndJdbcMeta.json");
+    assertThat(schemaJson).isEqualTo(expectedSchema);
+  }
+
+  private Schema getSchemaWithCommentFromQuery(boolean includeMetadata) throws 
SQLException {
+    DatabaseMetaData metaData = conn.getMetaData();
+    try (Statement statement = conn.createStatement()) {
+      try (ResultSet resultSet = statement.executeQuery("select * from 
table1")) {
+        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
+        Map<Integer, String> columnCommentByColumnIndex = 
getColumnComments(metaData, resultSetMetaData);
+
+        String tableName = 
getTableNameFromResultSetMetaData(resultSetMetaData);
+        String tableComment = getTableComment(metaData, tableName);
+        JdbcToArrowConfig config = new JdbcToArrowConfigBuilder()
+                .setAllocator(new 
RootAllocator()).setSchemaComment(tableComment)
+                
.setColumnCommentByColumnIndex(columnCommentByColumnIndex).setIncludeMetadata(includeMetadata).build();
+        return JdbcToArrowUtils.jdbcToArrowSchema(resultSetMetaData, config);

Review Comment:
   Ah, for the other metadata, this method automatically extracts the metadata 
values and adds them here. But for REMARKS, it's non-trivial to extract it from 
the result set so it probably shouldn't be done automatically.
   
   At this point, I wonder if the API shouldn't just be: allow specifying extra 
metadata to attach to the schema and to each column, instead of special casing 
a single metadata value? 



##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/Constants.java:
##########
@@ -27,5 +27,6 @@ private Constants() {}
   public static final String SQL_TABLE_NAME_KEY = "SQL_TABLE_NAME";
   public static final String SQL_COLUMN_NAME_KEY = "SQL_COLUMN_NAME";
   public static final String SQL_TYPE_KEY = "SQL_TYPE";
+  public static final String COMMENT = "comment";

Review Comment:
   I think this should have the `SQL_` prefix (in both name and value) for 
consistency with the other constants



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to