|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.arrow.adapter.jdbc; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.sql.Connection; |
| 26 | +import java.sql.DatabaseMetaData; |
| 27 | +import java.sql.DriverManager; |
| 28 | +import java.sql.ResultSet; |
| 29 | +import java.sql.ResultSetMetaData; |
| 30 | +import java.sql.SQLException; |
| 31 | +import java.sql.Statement; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.HashSet; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.Objects; |
| 37 | +import java.util.Set; |
| 38 | + |
| 39 | +import org.apache.arrow.memory.RootAllocator; |
| 40 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 41 | +import org.apache.arrow.vector.util.ObjectMapperFactory; |
| 42 | +import org.junit.After; |
| 43 | +import org.junit.Before; |
| 44 | +import org.junit.Test; |
| 45 | + |
| 46 | +import com.fasterxml.jackson.databind.ObjectWriter; |
| 47 | + |
| 48 | +public class JdbcToArrowCommentMetadataTest { |
| 49 | + |
| 50 | + private static final String COMMENT = "comment"; //use this metadata key for interoperability with Spark StructType |
| 51 | + private final ObjectWriter schemaSerializer = ObjectMapperFactory.newObjectMapper().writerWithDefaultPrettyPrinter(); |
| 52 | + private Connection conn = null; |
| 53 | + |
| 54 | + /** |
| 55 | + * This method creates Connection object and DB table and also populate data into table for test. |
| 56 | + * |
| 57 | + * @throws SQLException on error |
| 58 | + * @throws ClassNotFoundException on error |
| 59 | + */ |
| 60 | + @Before |
| 61 | + public void setUp() throws SQLException, ClassNotFoundException { |
| 62 | + String url = "jdbc:h2:mem:JdbcToArrowTest?characterEncoding=UTF-8;INIT=runscript from 'classpath:/h2/comment.sql'"; |
| 63 | + String driver = "org.h2.Driver"; |
| 64 | + Class.forName(driver); |
| 65 | + conn = DriverManager.getConnection(url); |
| 66 | + } |
| 67 | + |
| 68 | + @After |
| 69 | + public void tearDown() throws SQLException { |
| 70 | + if (conn != null) { |
| 71 | + conn.close(); |
| 72 | + conn = null; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void schemaComment() throws Exception { |
| 78 | + boolean includeMetadata = false; |
| 79 | + String schemaJson = schemaSerializer.writeValueAsString(getSchemaWithCommentFromQuery(includeMetadata)); |
| 80 | + String expectedSchema = getExpectedSchema("/h2/expectedSchemaWithComments.json"); |
| 81 | + assertThat(schemaJson).isEqualTo(expectedSchema); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void schemaCommentWithDatabaseMetadata() throws Exception { |
| 86 | + boolean includeMetadata = true; |
| 87 | + String schemaJson = schemaSerializer.writeValueAsString(getSchemaWithCommentFromQuery(includeMetadata)); |
| 88 | + String expectedSchema = getExpectedSchema("/h2/expectedSchemaWithCommentsAndJdbcMeta.json"); |
| 89 | + /* corresponding Apache Spark DDL after conversion: |
| 90 | + ID BIGINT NOT NULL COMMENT 'Record identifier', |
| 91 | + NAME STRING COMMENT 'Name of record', |
| 92 | + COLUMN1 BOOLEAN, |
| 93 | + COLUMNN INT COMMENT 'Informative description of columnN' |
| 94 | + */ |
| 95 | + assertThat(schemaJson).isEqualTo(expectedSchema); |
| 96 | + } |
| 97 | + |
| 98 | + private Schema getSchemaWithCommentFromQuery(boolean includeMetadata) throws SQLException { |
| 99 | + DatabaseMetaData metaData = conn.getMetaData(); |
| 100 | + try (Statement statement = conn.createStatement()) { |
| 101 | + try (ResultSet resultSet = statement.executeQuery("select * from table1")) { |
| 102 | + ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); |
| 103 | + Map<Integer, Map<String, String>> columnCommentByColumnIndex = getColumnComments(metaData, resultSetMetaData); |
| 104 | + |
| 105 | + String tableName = getTableNameFromResultSetMetaData(resultSetMetaData); |
| 106 | + String tableComment = getTableComment(metaData, tableName); |
| 107 | + JdbcToArrowConfig config = new JdbcToArrowConfigBuilder() |
| 108 | + .setAllocator(new RootAllocator()).setSchemaMetadata(Collections.singletonMap(COMMENT, tableComment)) |
| 109 | + .setColumnMetadataByColumnIndex(columnCommentByColumnIndex).setIncludeMetadata(includeMetadata).build(); |
| 110 | + return JdbcToArrowUtils.jdbcToArrowSchema(resultSetMetaData, config); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private String getTableNameFromResultSetMetaData(ResultSetMetaData resultSetMetaData) throws SQLException { |
| 116 | + Set<String> tablesFromQuery = new HashSet<>(); |
| 117 | + for (int idx = 1, columnCount = resultSetMetaData.getColumnCount(); idx <= columnCount; idx++) { |
| 118 | + String tableName = resultSetMetaData.getTableName(idx); |
| 119 | + if (tableName != null && !tableName.isEmpty()) { |
| 120 | + tablesFromQuery.add(tableName); |
| 121 | + } |
| 122 | + } |
| 123 | + if (tablesFromQuery.size() == 1) { |
| 124 | + return tablesFromQuery.iterator().next(); |
| 125 | + } |
| 126 | + throw new RuntimeException("Table metadata is absent or ambiguous"); |
| 127 | + } |
| 128 | + |
| 129 | + private Map<Integer, Map<String, String>> getColumnComments(DatabaseMetaData metaData, |
| 130 | + ResultSetMetaData resultSetMetaData) throws SQLException { |
| 131 | + Map<Integer, Map<String, String>> columnCommentByColumnIndex = new HashMap<>(); |
| 132 | + for (int columnIdx = 1, columnCount = resultSetMetaData.getColumnCount(); columnIdx <= columnCount; columnIdx++) { |
| 133 | + String columnComment = getColumnComment(metaData, resultSetMetaData.getTableName(columnIdx), |
| 134 | + resultSetMetaData.getColumnName(columnIdx)); |
| 135 | + if (columnComment != null && !columnComment.isEmpty()) { |
| 136 | + columnCommentByColumnIndex.put(columnIdx, Collections.singletonMap(COMMENT, columnComment)); |
| 137 | + } |
| 138 | + } |
| 139 | + return columnCommentByColumnIndex; |
| 140 | + } |
| 141 | + |
| 142 | + private String getTableComment(DatabaseMetaData metaData, String tableName) throws SQLException { |
| 143 | + if (tableName == null || tableName.isEmpty()) { |
| 144 | + return null; |
| 145 | + } |
| 146 | + String comment = null; |
| 147 | + int rowCount = 0; |
| 148 | + try (ResultSet tableMetadata = metaData.getTables("%", "%", tableName, null)) { |
| 149 | + if (tableMetadata.next()) { |
| 150 | + comment = tableMetadata.getString("REMARKS"); |
| 151 | + rowCount++; |
| 152 | + } |
| 153 | + } |
| 154 | + if (rowCount == 1) { |
| 155 | + return comment; |
| 156 | + } |
| 157 | + if (rowCount > 1) { |
| 158 | + throw new RuntimeException("Multiple tables found for table name"); |
| 159 | + } |
| 160 | + throw new RuntimeException("Table comment not found"); |
| 161 | + } |
| 162 | + |
| 163 | + private String getColumnComment(DatabaseMetaData metaData, String tableName, String columnName) throws SQLException { |
| 164 | + try (ResultSet tableMetadata = metaData.getColumns("%", "%", tableName, columnName)) { |
| 165 | + if (tableMetadata.next()) { |
| 166 | + return tableMetadata.getString("REMARKS"); |
| 167 | + } |
| 168 | + } |
| 169 | + return null; |
| 170 | + } |
| 171 | + |
| 172 | + private String getExpectedSchema(String expectedResource) throws java.io.IOException, java.net.URISyntaxException { |
| 173 | + return new String(Files.readAllBytes(Paths.get(Objects.requireNonNull( |
| 174 | + JdbcToArrowCommentMetadataTest.class.getResource(expectedResource)).toURI())), StandardCharsets.UTF_8); |
| 175 | + } |
| 176 | +} |
0 commit comments