This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 9dd0d3990c8 Implements openGauss version function by calcite (#19327)
9dd0d3990c8 is described below
commit 9dd0d3990c849d50c17c6dc7c92ec2d4ce0ad7e5
Author: 吴伟杰 <[email protected]>
AuthorDate: Mon Jul 18 19:19:11 2022 +0800
Implements openGauss version function by calcite (#19327)
* Implements openGauss version function by calcite
* Rename methods and classes
---
.../opengauss/OpenGaussAdminExecutorCreator.java | 21 +++++-
... OpenGaussSystemCatalogAdminQueryExecutor.java} | 17 ++++-
.../OpenGaussAdminExecutorCreatorTest.java | 71 +++++++++++++++++++
.../OpenGaussAdminExecutorFactoryTest.java | 2 +-
.../OpenGaussSelectDatabaseExecutorTest.java | 66 ------------------
...enGaussSystemCatalogAdminQueryExecutorTest.java | 79 ++++++++++++++++++++++
6 files changed, 185 insertions(+), 71 deletions(-)
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorCreator.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorCreator.java
index ae44f33b1ae..2afe36b65c0 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorCreator.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorCreator.java
@@ -21,7 +21,11 @@ import
org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import
org.apache.shardingsphere.proxy.backend.text.admin.executor.DatabaseAdminExecutor;
import
org.apache.shardingsphere.proxy.backend.text.admin.executor.DatabaseAdminExecutorCreator;
import
org.apache.shardingsphere.proxy.backend.text.admin.postgresql.PostgreSQLAdminExecutorCreator;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+import java.util.Collection;
import java.util.Optional;
/**
@@ -40,12 +44,25 @@ public final class OpenGaussAdminExecutorCreator implements
DatabaseAdminExecuto
@Override
public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?>
sqlStatementContext, final String sql, final String databaseName) {
- if
(sqlStatementContext.getTablesContext().getTableNames().contains(OG_DATABASE)) {
- return Optional.of(new OpenGaussSelectDatabaseExecutor(sql));
+ if (isSystemCatalogQuery(sqlStatementContext)) {
+ return Optional.of(new
OpenGaussSystemCatalogAdminQueryExecutor(sql));
}
return delegated.create(sqlStatementContext, sql, databaseName);
}
+ private boolean isSystemCatalogQuery(final SQLStatementContext<?>
sqlStatementContext) {
+ if
(sqlStatementContext.getTablesContext().getTableNames().contains(OG_DATABASE)) {
+ return true;
+ }
+ if (!(sqlStatementContext.getSqlStatement() instanceof
SelectStatement)) {
+ return false;
+ }
+ SelectStatement selectStatement = (SelectStatement)
sqlStatementContext.getSqlStatement();
+ Collection<ProjectionSegment> projections =
selectStatement.getProjections().getProjections();
+ return 1 == projections.size() && projections.iterator().next()
instanceof ExpressionProjectionSegment
+ && "VERSION()".equalsIgnoreCase(((ExpressionProjectionSegment)
projections.iterator().next()).getText());
+ }
+
@Override
public String getType() {
return "openGauss";
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSelectDatabaseExecutor.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutor.java
similarity index 82%
rename from
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSelectDatabaseExecutor.java
rename to
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutor.java
index dd02cde3d09..990cdce90bc 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSelectDatabaseExecutor.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutor.java
@@ -20,6 +20,8 @@ package
org.apache.shardingsphere.proxy.backend.text.admin.opengauss;
import lombok.Getter;
import org.apache.calcite.adapter.java.ReflectiveSchema;
import org.apache.calcite.jdbc.CalciteConnection;
+import org.apache.calcite.schema.impl.ScalarFunctionImpl;
+import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.metadata.JDBCQueryResultMetaData;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.type.memory.JDBCMemoryQueryResult;
@@ -42,7 +44,7 @@ import java.util.Collections;
/**
* Select database executor for openGauss.
*/
-public final class OpenGaussSelectDatabaseExecutor implements
DatabaseAdminQueryExecutor {
+public final class OpenGaussSystemCatalogAdminQueryExecutor implements
DatabaseAdminQueryExecutor {
private static final String PG_CATALOG = "pg_catalog";
@@ -56,7 +58,7 @@ public final class OpenGaussSelectDatabaseExecutor implements
DatabaseAdminQuery
@Getter
private MergedResult mergedResult;
- public OpenGaussSelectDatabaseExecutor(final String sql) {
+ public OpenGaussSystemCatalogAdminQueryExecutor(final String sql) {
this.sql = SQLUtil.trimSemicolon(sql);
}
@@ -64,6 +66,7 @@ public final class OpenGaussSelectDatabaseExecutor implements
DatabaseAdminQuery
public void execute(final ConnectionSession connectionSession) throws
SQLException {
try (CalciteConnection connection =
DriverManager.getConnection("jdbc:calcite:caseSensitive=false").unwrap(CalciteConnection.class))
{
connection.getRootSchema().add(PG_CATALOG, new
ReflectiveSchema(constructOgCatalog()));
+ connection.getRootSchema().add("version",
ScalarFunctionImpl.create(getClass(), "version"));
connection.setSchema(PG_CATALOG);
try (Statement statement = connection.createStatement(); ResultSet
resultSet = statement.executeQuery(sql)) {
queryResultMetaData = new
JDBCQueryResultMetaData(resultSet.getMetaData());
@@ -81,4 +84,14 @@ public final class OpenGaussSelectDatabaseExecutor
implements DatabaseAdminQuery
}
return new OgCatalog(ogDatabases);
}
+
+ /**
+ * Get version of ShardingSphere-Proxy.
+ *
+ * @return version message
+ */
+ @SuppressWarnings("unused")
+ public static String version() {
+ return "ShardingSphere-Proxy " + ShardingSphereVersion.VERSION + ("-"
+ ShardingSphereVersion.BUILD_GIT_COMMIT_ID_ABBREV) +
(ShardingSphereVersion.BUILD_GIT_DIRTY ? "-dirty" : "");
+ }
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorCreatorTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorCreatorTest.java
new file mode 100644
index 00000000000..c990313d2af
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorCreatorTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.proxy.backend.text.admin.opengauss;
+
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import
org.apache.shardingsphere.proxy.backend.text.admin.executor.DatabaseAdminExecutor;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
+import org.junit.Test;
+
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class OpenGaussAdminExecutorCreatorTest {
+
+ @Test
+ public void assertCreateExecutorForSelectDatabase() {
+ SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+
when(selectStatementContext.getTablesContext().getTableNames().contains("pg_database")).thenReturn(true);
+ Optional<DatabaseAdminExecutor> actual = new
OpenGaussAdminExecutorCreator()
+ .create(selectStatementContext, "select datname,
datcompatibility from pg_database where datname = 'sharding_db'", "postgres");
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(),
instanceOf(OpenGaussSystemCatalogAdminQueryExecutor.class));
+ }
+
+ @Test
+ public void assertCreateExecutorForSelectVersion() {
+ SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+
when(selectStatementContext.getSqlStatement().getProjections().getProjections()).thenReturn(Collections.singletonList(new
ExpressionProjectionSegment(-1, -1, "VERSION()")));
+ Optional<DatabaseAdminExecutor> actual = new
OpenGaussAdminExecutorCreator().create(selectStatementContext, "select
VERSION()", "postgres");
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(),
instanceOf(OpenGaussSystemCatalogAdminQueryExecutor.class));
+ }
+
+ @Test
+ public void assertCreateOtherExecutor() {
+ OpenGaussAdminExecutorCreator creator = new
OpenGaussAdminExecutorCreator();
+ SQLStatementContext<?> sqlStatementContext =
mock(SQLStatementContext.class, RETURNS_DEEP_STUBS);
+
when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.emptyList());
+ assertThat(creator.create(sqlStatementContext), is(Optional.empty()));
+ assertThat(creator.create(sqlStatementContext, "", ""),
is(Optional.empty()));
+ }
+
+ @Test
+ public void assertGetType() {
+ assertThat(new OpenGaussAdminExecutorCreator().getType(),
is("openGauss"));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorFactoryTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorFactoryTest.java
index ff3aa62f08a..a7b66b8992f 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorFactoryTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorFactoryTest.java
@@ -70,7 +70,7 @@ public final class OpenGaussAdminExecutorFactoryTest {
when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singletonList("pg_database"));
Optional<DatabaseAdminExecutor> actual =
openGaussAdminExecutorFactory.create(sqlStatementContext, "select
datcompatibility from pg_database where datname = 'sharding_db'", "");
assertTrue(actual.isPresent());
- assertThat(actual.get(),
instanceOf(OpenGaussSelectDatabaseExecutor.class));
+ assertThat(actual.get(),
instanceOf(OpenGaussSystemCatalogAdminQueryExecutor.class));
}
@Test
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSelectDatabaseExecutorTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSelectDatabaseExecutorTest.java
deleted file mode 100644
index 3311865469b..00000000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSelectDatabaseExecutorTest.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * 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.proxy.backend.text.admin.opengauss;
-
-import
org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseType;
-import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
-import org.apache.shardingsphere.infra.merge.result.MergedResult;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
-import org.junit.Test;
-import org.mockito.MockedStatic;
-
-import java.sql.SQLException;
-import java.util.Arrays;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.mockStatic;
-import static org.mockito.Mockito.when;
-
-public final class OpenGaussSelectDatabaseExecutorTest {
-
- private static final String SQL = "select datname, datcompatibility from
pg_database where datname = 'sharding_db'";
-
- @Test
- public void assertExecute() throws SQLException {
- try (MockedStatic<ProxyContext> mockedStatic =
mockStatic(ProxyContext.class)) {
-
mockedStatic.when(ProxyContext::getInstance).thenReturn(mock(ProxyContext.class,
RETURNS_DEEP_STUBS));
- assertExecute0();
- }
- }
-
- private void assertExecute0() throws SQLException {
-
when(ProxyContext.getInstance().getAllDatabaseNames()).thenReturn(Arrays.asList("foo",
"bar", "sharding_db", "other_db"));
- OpenGaussSelectDatabaseExecutor executor = new
OpenGaussSelectDatabaseExecutor(SQL);
- ConnectionSession connectionSession = mock(ConnectionSession.class);
- when(connectionSession.getDatabaseType()).thenReturn(new
OpenGaussDatabaseType());
- executor.execute(connectionSession);
- QueryResultMetaData actualMetaData = executor.getQueryResultMetaData();
- assertThat(actualMetaData.getColumnCount(), is(2));
- assertThat(actualMetaData.getColumnName(1), is("datname"));
- assertThat(actualMetaData.getColumnName(2), is("datcompatibility"));
- MergedResult actualResult = executor.getMergedResult();
- assertTrue(actualResult.next());
- assertThat(actualResult.getValue(1, String.class), is("sharding_db"));
- assertThat(actualResult.getValue(2, String.class), is("PG"));
- }
-}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutorTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutorTest.java
new file mode 100644
index 00000000000..54770bbb16d
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutorTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.proxy.backend.text.admin.opengauss;
+
+import
org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseType;
+import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
+import org.apache.shardingsphere.infra.merge.result.MergedResult;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.junit.Test;
+import org.mockito.MockedStatic;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Arrays;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+public final class OpenGaussSystemCatalogAdminQueryExecutorTest {
+
+ @Test
+ public void assertExecuteSelectFromPgDatabase() throws SQLException {
+ try (MockedStatic<ProxyContext> mockedStatic =
mockStatic(ProxyContext.class)) {
+
mockedStatic.when(ProxyContext::getInstance).thenReturn(mock(ProxyContext.class,
RETURNS_DEEP_STUBS));
+
when(ProxyContext.getInstance().getAllDatabaseNames()).thenReturn(Arrays.asList("foo",
"bar", "sharding_db", "other_db"));
+ OpenGaussSystemCatalogAdminQueryExecutor executor = new
OpenGaussSystemCatalogAdminQueryExecutor("select datname, datcompatibility from
pg_database where datname = 'sharding_db'");
+ ConnectionSession connectionSession =
mock(ConnectionSession.class);
+ when(connectionSession.getDatabaseType()).thenReturn(new
OpenGaussDatabaseType());
+ executor.execute(connectionSession);
+ QueryResultMetaData actualMetaData =
executor.getQueryResultMetaData();
+ assertThat(actualMetaData.getColumnCount(), is(2));
+ assertThat(actualMetaData.getColumnName(1), is("datname"));
+ assertThat(actualMetaData.getColumnName(2),
is("datcompatibility"));
+ MergedResult actualResult = executor.getMergedResult();
+ assertTrue(actualResult.next());
+ assertThat(actualResult.getValue(1, String.class),
is("sharding_db"));
+ assertThat(actualResult.getValue(2, String.class), is("PG"));
+ }
+ }
+
+ @Test
+ public void assertExecuteSelectVersion() throws SQLException {
+ try (MockedStatic<ProxyContext> mockedStatic =
mockStatic(ProxyContext.class)) {
+
mockedStatic.when(ProxyContext::getInstance).thenReturn(mock(ProxyContext.class,
RETURNS_DEEP_STUBS));
+ OpenGaussSystemCatalogAdminQueryExecutor executor = new
OpenGaussSystemCatalogAdminQueryExecutor("select VERSION()");
+ ConnectionSession connectionSession =
mock(ConnectionSession.class);
+ when(connectionSession.getDatabaseType()).thenReturn(new
OpenGaussDatabaseType());
+ executor.execute(connectionSession);
+ QueryResultMetaData actualMetaData =
executor.getQueryResultMetaData();
+ assertThat(actualMetaData.getColumnCount(), is(1));
+ assertThat(actualMetaData.getColumnType(1), is(Types.VARCHAR));
+ MergedResult actualResult = executor.getMergedResult();
+ assertTrue(actualResult.next());
+ assertThat((String) actualResult.getValue(1, String.class),
containsString("ShardingSphere-Proxy"));
+ }
+ }
+}