Skip to content

Commit

Permalink
Add GraalVM Reachability Metadata and corresponding nativeTest for Pr…
Browse files Browse the repository at this point in the history
…esto
  • Loading branch information
linghengqian committed Jan 22, 2025
1 parent 485153d commit d1b882f
Show file tree
Hide file tree
Showing 21 changed files with 827 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,6 @@ ShardingSphere 的单元测试仅使用 Maven 模块 `io.github.linghengqian:hiv
9. 由于 https://github.com/apache/doris/issues/9426 的影响,当通过 Shardinghere JDBC 连接至 Apache Doris FE,
用户需自行提供 `apache/doris` 集成模块相关的 GraalVM Reachability Metadata。

10. 由于 https://github.com/prestodb/presto/issues/23226 的影响,当通过 Shardinghere JDBC 连接至 Presto Server,
用户需自行提供 `com.facebook.presto:presto-jdbc` 和 `prestodb/presto` 集成模块相关的 GraalVM Reachability Metadata。

## 贡献 GraalVM Reachability Metadata

ShardingSphere 对在 GraalVM Native Image 下的可用性的验证,是通过 GraalVM Native Build Tools 的 Maven Plugin 子项目来完成的。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,6 @@ the Oracle JDBC Driver corresponding to the `com.oracle.database.jdbc:ojdbc8` Ma
9. Due to https://github.com/apache/doris/issues/9426, when connecting to Apache Doris FE via Shardinghere JDBC,
users need to provide GraalVM Reachability Metadata related to the `apache/doris` integration module.

10. Due to https://github.com/prestodb/presto/issues/23226, when connecting to Presto Server via Shardinghere JDBC,
users need to provide GraalVM Reachability Metadata related to the `com.facebook.presto:presto-jdbc` and `prestodb/presto` integration module.

## Contribute GraalVM Reachability Metadata

The verification of ShardingSphere's availability under GraalVM Native Image is completed through the Maven Plugin subproject
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.shardingsphere.infra.database.presto.metadata.data.loader;

import org.apache.shardingsphere.infra.database.core.metadata.data.loader.DialectMetaDataLoader;
import org.apache.shardingsphere.infra.database.core.metadata.data.loader.MetaDataLoaderMaterial;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeRegistry;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Meta data loader for Presto.
* As of prestodb/presto 0.290 of the Memory Connector, the table `INFORMATION_SCHEMA.INDEXES` does not exist,
* and `INFORMATION_SCHEMA.COLUMNS` does not have a column `IS_VISIBLE`.
* This may change in the future.
*/
public final class PrestoMetaDataLoader implements DialectMetaDataLoader {

@Override
public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial material) throws SQLException {
Collection<TableMetaData> tableMetaDataList = new LinkedList<>();
try (Connection connection = material.getDataSource().getConnection()) {
Map<String, Collection<ColumnMetaData>> columnMetaDataMap = loadColumnMetaDataMap(connection, material.getActualTableNames());
for (Map.Entry<String, Collection<ColumnMetaData>> entry : columnMetaDataMap.entrySet()) {
tableMetaDataList.add(new TableMetaData(entry.getKey(), entry.getValue(), Collections.emptyList(), Collections.emptyList()));
}
}
return Collections.singleton(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList));
}

@SuppressWarnings("SqlSourceToSinkFlow")
private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final Connection connection, final Collection<String> tables) throws SQLException {
Map<String, Collection<ColumnMetaData>> result = new HashMap<>();
try (PreparedStatement preparedStatement = connection.prepareStatement(getTableMetaDataSQL(tables))) {
preparedStatement.setString(1, connection.getCatalog());
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
ColumnMetaData columnMetaData = loadColumnMetaData(resultSet);
if (!result.containsKey(tableName)) {
result.put(tableName, new LinkedList<>());
}
result.get(tableName).add(columnMetaData);
}
}
}
return result;
}

private String getTableMetaDataSQL(final Collection<String> tables) {
if (tables.isEmpty()) {
return "SELECT TABLE_CATALOG,\n"
+ " TABLE_NAME,\n"
+ " COLUMN_NAME,\n"
+ " DATA_TYPE,\n"
+ " ORDINAL_POSITION,\n"
+ " IS_NULLABLE\n"
+ "FROM INFORMATION_SCHEMA.COLUMNS\n"
+ "WHERE TABLE_CATALOG = ?\n"
+ "ORDER BY ORDINAL_POSITION";
}
String collect = tables.stream().map(each -> String.format("'%s'", each).toUpperCase()).collect(Collectors.joining(","));
return String.format("SELECT TABLE_CATALOG,\n"
+ " TABLE_NAME,\n"
+ " COLUMN_NAME,\n"
+ " DATA_TYPE,\n"
+ " ORDINAL_POSITION,\n"
+ " IS_NULLABLE\n"
+ "FROM INFORMATION_SCHEMA.COLUMNS\n"
+ "WHERE TABLE_CATALOG = ?\n"
+ " AND UPPER(TABLE_NAME) IN (%s)\n"
+ "ORDER BY ORDINAL_POSITION", collect);
}

private ColumnMetaData loadColumnMetaData(final ResultSet resultSet) throws SQLException {
String columnName = resultSet.getString("COLUMN_NAME");
String dataType = resultSet.getString("DATA_TYPE");
boolean isNullable = "YES".equals(resultSet.getString("IS_NULLABLE"));
return new ColumnMetaData(columnName, DataTypeRegistry.getDataType(getDatabaseType(), dataType).orElse(Types.OTHER), Boolean.FALSE, Boolean.FALSE, false, Boolean.TRUE, false, isNullable);
}

@Override
public String getDatabaseType() {
return "Presto";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

org.apache.shardingsphere.infra.database.presto.metadata.data.loader.PrestoMetaDataLoader
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.shardingsphere.infra.database.presto.database;

import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class PrestoDatabaseMetaDataTest {

private final DialectDatabaseMetaData dialectDatabaseMetaData = DatabaseTypedSPILoader.getService(DialectDatabaseMetaData.class, TypedSPILoader.getService(DatabaseType.class, "Presto"));

@Test
void assertGetQuoteCharacter() {
assertThat(dialectDatabaseMetaData.getQuoteCharacter(), is(QuoteCharacter.QUOTE));
}

@Test
void assertGetDefaultNullsOrderType() {
assertThat(dialectDatabaseMetaData.getDefaultNullsOrderType(), is(NullsOrderType.LOW));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.guava.reflect.Reflection"},
"interfaces":["java.lang.reflect.TypeVariable"]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
[
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.client.StatementClientV1"},
"name":"com.facebook.presto.jdbc.internal.client.ClientTypeSignature",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.util.List","java.util.List","java.util.List"] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.client.ClientTypeSignature",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.deser.DefaultDeserializationContext"},
"name":"com.facebook.presto.jdbc.internal.client.ClientTypeSignatureParameter$ClientTypeSignatureParameterDeserializer",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.client.StatementClientV1"},
"name":"com.facebook.presto.jdbc.internal.client.Column",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","com.facebook.presto.jdbc.internal.client.ClientTypeSignature"] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.client.Column",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.client.ErrorLocation",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.client.FailureInfo",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"allPublicConstructors": true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.client.QueryData",
"queryAllDeclaredMethods":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.client.QueryError",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"allPublicConstructors": true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.client.QueryResults",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.net.URI","java.net.URI","java.net.URI","java.util.List","java.util.List","java.util.List","com.facebook.presto.jdbc.internal.client.StatementStats","com.facebook.presto.jdbc.internal.client.QueryError","java.util.List","java.lang.String","java.lang.Long"] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.client.QueryStatusInfo",
"queryAllDeclaredMethods":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.PrestoPreparedStatement"},
"name":"com.facebook.presto.jdbc.internal.client.StageStats",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","boolean","int","int","int","int","int","long","long","long","long","java.util.List"] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.PrestoResultSet$ResultsPageIterator"},
"name":"com.facebook.presto.jdbc.internal.client.StageStats",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","boolean","int","int","int","int","int","long","long","long","long","java.util.List"] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.PrestoStatement"},
"name":"com.facebook.presto.jdbc.internal.client.StageStats",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","boolean","int","int","int","int","int","long","long","long","long","java.util.List"] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.client.StageStats",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.client.StatementStats",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","boolean","boolean","boolean","int","int","int","int","int","long","long","long","long","long","long","long","long","long","long","long","com.facebook.presto.jdbc.internal.client.StageStats","com.facebook.presto.jdbc.internal.common.RuntimeStats"] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.deser.std.MapDeserializer"},
"name":"com.facebook.presto.jdbc.internal.common.RuntimeMetric",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","com.facebook.presto.jdbc.internal.common.RuntimeUnit","long","long","long","long"] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.common.RuntimeStats",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.introspect.AnnotatedConstructor"},
"name":"com.facebook.presto.jdbc.internal.common.RuntimeStats",
"methods":[{"name":"<init>","parameterTypes":["java.util.Map"] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.deser.std.MapDeserializer"},
"name":"com.facebook.presto.jdbc.internal.common.RuntimeUnit",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.introspect.JacksonAnnotationIntrospector"},
"name":"com.facebook.presto.jdbc.internal.common.RuntimeUnit",
"allDeclaredFields":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.util.ClassUtil"},
"name":"com.facebook.presto.jdbc.internal.common.RuntimeUnit",
"allDeclaredFields":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectMapper"},
"name":"com.facebook.presto.jdbc.internal.common.type.ParameterKind",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.introspect.AnnotatedMethod"},
"name":"com.facebook.presto.jdbc.internal.common.type.ParameterKind",
"methods":[{"name":"fromJsonValue","parameterTypes":["java.lang.String"] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.guava.reflect.Types$TypeVariableInvocationHandler"},
"name":"com.facebook.presto.jdbc.internal.guava.reflect.Types$TypeVariableImpl",
"queryAllPublicMethods":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ext.Java7Handlers"},
"name":"com.facebook.presto.jdbc.internal.jackson.databind.ext.Java7HandlersImpl",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ext.Java7Support"},
"name":"com.facebook.presto.jdbc.internal.jackson.databind.ext.Java7SupportImpl",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.com.facebook.airlift.json.ObjectMapperProvider"},
"name":"com.facebook.presto.jdbc.internal.joda.time.DateTime"
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.spi.PrestoWarning",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true
},
{
"condition":{"typeReachable":"com.facebook.presto.jdbc.internal.jackson.databind.ObjectReader"},
"name":"com.facebook.presto.jdbc.internal.spi.WarningCode",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true
}
]
Loading

0 comments on commit d1b882f

Please sign in to comment.