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 619b39617b0 Add test cases on PostgreSQLIndexSQLGenerator (#33426)
619b39617b0 is described below
commit 619b39617b06177bb51585a6ebf20f4b9aceb79a
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Oct 27 20:28:19 2024 +0800
Add test cases on PostgreSQLIndexSQLGenerator (#33426)
* Add test cases on PostgreSQLIndexSQLGenerator
* Add test cases on PostgreSQLIndexSQLGenerator
---
.../ddl/index/PostgreSQLIndexSQLGenerator.java | 50 +++------
.../ddl/index/PostgreSQLIndexSQLGeneratorTest.java | 123 +++++++++++++++++++++
2 files changed, 139 insertions(+), 34 deletions(-)
diff --git
a/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/ddl/index/PostgreSQLIndexSQLGenerator.java
b/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/ddl/index/PostgreSQLIndexSQLGenerator.java
index a793f19c102..f6d36fff2ee 100644
---
a/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/ddl/index/PostgreSQLIndexSQLGenerator.java
+++
b/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/ddl/index/PostgreSQLIndexSQLGenerator.java
@@ -24,9 +24,11 @@ import org.postgresql.jdbc.PgArray;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
+import java.util.stream.Collectors;
/**
* Index SQL generator for PostgreSQL.
@@ -50,36 +52,27 @@ public final class PostgreSQLIndexSQLGenerator {
*/
public String generate(final Map<String, Object> context) throws
SQLException {
StringBuilder result = new StringBuilder();
- Collection<Map<String, Object>> indexNodes = getIndexNodes(context);
- for (Map<String, Object> each : indexNodes) {
+ for (Map<String, Object> each : getIndexNodes(context)) {
if (each.containsKey("is_inherited") && (Boolean)
each.get("is_inherited")) {
continue;
}
- result.append(getIndexSql(context, each));
+ result.append(getIndexSQL(context, each));
}
return result.toString().trim();
}
private Collection<Map<String, Object>> getIndexNodes(final Map<String,
Object> context) {
- Map<String, Object> param = new LinkedHashMap<>();
- param.put("tid", context.get("tid"));
- return templateExecutor.executeByTemplate(param,
"component/indexes/%s/nodes.ftl");
+ return
templateExecutor.executeByTemplate(Collections.singletonMap("tid",
context.get("tid")), "component/indexes/%s/nodes.ftl");
}
- private String getIndexSql(final Map<String, Object> context, final
Map<String, Object> indexNode) throws SQLException {
+ private String getIndexSQL(final Map<String, Object> context, final
Map<String, Object> indexNode) throws SQLException {
Map<String, Object> indexData = getIndexData(context, indexNode);
appendColumnDetails(indexData, (Long) indexNode.get("oid"));
if (templateExecutor.getMajorVersion() >= PG_INDEX_INCLUDE_VERSION) {
- appendIncludeDetails(indexData, (Long) indexNode.get("oid"));
+ Collection<Map<String, Object>> includeDetails =
templateExecutor.executeByTemplate(Collections.singletonMap("idx",
indexNode.get("oid")), "component/indexes/%s/include_details.ftl");
+ indexData.put("include", includeDetails.stream().map(each ->
each.get("colname")).collect(Collectors.toList()));
}
- return doGenerateIndexSql(indexData);
- }
-
- private String doGenerateIndexSql(final Map<String, Object> indexData) {
- String result =
PostgreSQLPipelineFreemarkerManager.getSQLByVersion(indexData,
"component/indexes/%s/create.ftl", templateExecutor.getMajorVersion(),
templateExecutor.getMinorVersion());
- result += System.lineSeparator();
- result +=
PostgreSQLPipelineFreemarkerManager.getSQLByVersion(indexData,
"component/indexes/%s/alter.ftl", templateExecutor.getMajorVersion(),
templateExecutor.getMinorVersion());
- return result;
+ return doGenerateIndexSQL(indexData);
}
private Map<String, Object> getIndexData(final Map<String, Object>
context, final Map<String, Object> indexNode) {
@@ -91,7 +84,7 @@ public final class PostgreSQLIndexSQLGenerator {
}
private Collection<Map<String, Object>> fetchIndexProperties(final
Map<String, Object> context, final Map<String, Object> indexNode) {
- Map<String, Object> param = new LinkedHashMap<>();
+ Map<String, Object> param = new LinkedHashMap<>(4, 1F);
param.put("did", context.get("did"));
param.put("tid", context.get("tid"));
param.put("idx", indexNode.get("oid"));
@@ -100,10 +93,9 @@ public final class PostgreSQLIndexSQLGenerator {
}
private void appendColumnDetails(final Map<String, Object> indexData,
final Long indexId) throws SQLException {
- Collection<Map<String, Object>> columnDetails =
fetchColumnDetails(indexId);
Collection<Map<String, Object>> columns = new LinkedList<>();
Collection<String> columnDisplays = new LinkedList<>();
- for (Map<String, Object> each : columnDetails) {
+ for (Map<String, Object> each :
templateExecutor.executeByTemplate(Collections.singletonMap("idx", indexId),
"component/indexes/%s/column_details.ftl")) {
columns.add(getColumnData(indexData, each));
columnDisplays.add(getColumnPropertyDisplayData(each, indexData));
}
@@ -112,7 +104,7 @@ public final class PostgreSQLIndexSQLGenerator {
}
private Map<String, Object> getColumnData(final Map<String, Object>
indexData, final Map<String, Object> columnDetail) throws SQLException {
- Map<String, Object> result = new LinkedHashMap<>();
+ Map<String, Object> result = new LinkedHashMap<>(5, 1F);
result.put("colname", columnDetail.get("attdef"));
result.put("collspcname", columnDetail.get("collnspname"));
result.put("op_class", columnDetail.get("opcname"));
@@ -139,12 +131,6 @@ public final class PostgreSQLIndexSQLGenerator {
return options.length > 1 && options[1].split(" ").length > 1 &&
"FIRST".equals(options[1].split(" ")[1]);
}
- private Collection<Map<String, Object>> fetchColumnDetails(final Long
indexId) {
- Map<String, Object> param = new LinkedHashMap<>();
- param.put("idx", indexId);
- return templateExecutor.executeByTemplate(param,
"component/indexes/%s/column_details.ftl");
- }
-
private String getColumnPropertyDisplayData(final Map<String, Object>
columnDetail, final Map<String, Object> indexData) throws SQLException {
String result = (String) columnDetail.get("attdef");
if (null != columnDetail.get("collnspname")) {
@@ -165,13 +151,9 @@ public final class PostgreSQLIndexSQLGenerator {
return result;
}
- private void appendIncludeDetails(final Map<String, Object> indexData,
final Long oid) {
- Map<String, Object> param = new LinkedHashMap<>();
- param.put("idx", oid);
- Collection<Object> includes = new LinkedList<>();
- for (Map<String, Object> each :
templateExecutor.executeByTemplate(param,
"component/indexes/%s/include_details.ftl")) {
- includes.add(each.get("colname"));
- }
- indexData.put("include", includes);
+ private String doGenerateIndexSQL(final Map<String, Object> indexData) {
+ return String.join(System.lineSeparator(),
+ PostgreSQLPipelineFreemarkerManager.getSQLByVersion(indexData,
"component/indexes/%s/create.ftl", templateExecutor.getMajorVersion(),
templateExecutor.getMinorVersion()),
+ PostgreSQLPipelineFreemarkerManager.getSQLByVersion(indexData,
"component/indexes/%s/alter.ftl", templateExecutor.getMajorVersion(),
templateExecutor.getMinorVersion()));
}
}
diff --git
a/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/ddl/index/PostgreSQLIndexSQLGeneratorTest.java
b/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/ddl/index/PostgreSQLIndexSQLGeneratorTest.java
new file mode 100644
index 00000000000..9691bd61f82
--- /dev/null
+++
b/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/sqlbuilder/ddl/index/PostgreSQLIndexSQLGeneratorTest.java
@@ -0,0 +1,123 @@
+/*
+ * 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.data.pipeline.postgresql.sqlbuilder.ddl.index;
+
+import org.junit.jupiter.api.Test;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.ArgumentMatchers.contains;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class PostgreSQLIndexSQLGeneratorTest {
+
+ @Test
+ void assertGenerate() throws SQLException {
+ Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+ ResultSet getNodesResultSet = mockGetNodesResultSet();
+ when(connection.createStatement().executeQuery(
+ contains("SELECT DISTINCT ON(cls.relname) cls.oid, cls.relname
as name," + System.lineSeparator() + "(SELECT (CASE WHEN count(i.inhrelid) > 0
THEN true ELSE false END)")))
+ .thenReturn(getNodesResultSet);
+ ResultSet getPropertiesResultSet = mockGetPropertiesResultSet();
+ when(connection.createStatement().executeQuery(contains("SELECT
DISTINCT ON(cls.relname) cls.oid, cls.relname as name, indrelid, indkey,
indisclustered"))).thenReturn(getPropertiesResultSet);
+ Map<String, Object> context = new HashMap<>(5, 1F);
+ context.put("did", 1);
+ context.put("datlastsysoid", 10);
+ context.put("tid", 20);
+ context.put("schema", "foo_schema");
+ context.put("name", "foo_tbl");
+ String actual = new PostgreSQLIndexSQLGenerator(connection, 10,
0).generate(context);
+ String expected = "CREATE INDEX IF NOT EXISTS foo_tbl" +
System.lineSeparator() + "ON foo_schema.foo_tbl USING foo_am_name"
+ + System.lineSeparator() + "()" + System.lineSeparator() +
"WITH (FILLFACTOR=90)" + System.lineSeparator() + "TABLESPACE default" +
System.lineSeparator() + "WHERE NULL;";
+ assertThat(actual, is(expected));
+ }
+
+ private ResultSet mockGetNodesResultSet() throws SQLException {
+ ResultSet result = mock(ResultSet.class, RETURNS_DEEP_STUBS);
+ when(result.getMetaData().getColumnCount()).thenReturn(3);
+ when(result.next()).thenReturn(true, false);
+ when(result.getMetaData().getColumnName(1)).thenReturn("oid");
+ when(result.getObject(1)).thenReturn(1L);
+ when(result.getMetaData().getColumnName(2)).thenReturn("name");
+ when(result.getObject(2)).thenReturn("foo_tbl");
+ when(result.getMetaData().getColumnName(3)).thenReturn("is_inherited");
+ when(result.getObject(3)).thenReturn(false);
+ return result;
+ }
+
+ private ResultSet mockGetPropertiesResultSet() throws SQLException {
+ ResultSet result = mock(ResultSet.class, RETURNS_DEEP_STUBS);
+ when(result.getMetaData().getColumnCount()).thenReturn(23);
+ when(result.next()).thenReturn(true, false);
+ when(result.getMetaData().getColumnName(1)).thenReturn("oid");
+ when(result.getObject(1)).thenReturn(1L);
+ when(result.getMetaData().getColumnName(2)).thenReturn("name");
+ when(result.getObject(2)).thenReturn("foo_tbl");
+ when(result.getMetaData().getColumnName(3)).thenReturn("indrelid");
+ when(result.getObject(3)).thenReturn(20);
+ when(result.getMetaData().getColumnName(4)).thenReturn("indkey");
+ when(result.getObject(4)).thenReturn("{1,2}");
+
when(result.getMetaData().getColumnName(5)).thenReturn("indisclustered");
+ when(result.getObject(5)).thenReturn(false);
+ when(result.getMetaData().getColumnName(6)).thenReturn("indisvalid");
+ when(result.getObject(6)).thenReturn(true);
+ when(result.getMetaData().getColumnName(7)).thenReturn("indisunique");
+ when(result.getObject(7)).thenReturn(false);
+ when(result.getMetaData().getColumnName(8)).thenReturn("indisprimary");
+ when(result.getObject(8)).thenReturn(false);
+ when(result.getMetaData().getColumnName(9)).thenReturn("nspname");
+ when(result.getObject(9)).thenReturn("foo_schema");
+ when(result.getMetaData().getColumnName(10)).thenReturn("indnatts");
+ when(result.getObject(10)).thenReturn(3);
+ when(result.getMetaData().getColumnName(11)).thenReturn("spcoid");
+ when(result.getObject(11)).thenReturn("10101");
+ when(result.getMetaData().getColumnName(12)).thenReturn("spcname");
+ when(result.getObject(12)).thenReturn("default");
+ when(result.getMetaData().getColumnName(13)).thenReturn("tabname");
+ when(result.getObject(13)).thenReturn("foo_tbl");
+ when(result.getMetaData().getColumnName(14)).thenReturn("indclass");
+ when(result.getObject(14)).thenReturn("{pg_am_oid}");
+ when(result.getMetaData().getColumnName(15)).thenReturn("conoid");
+ when(result.getObject(15)).thenReturn(23456);
+ when(result.getMetaData().getColumnName(16)).thenReturn("description");
+ when(result.getObject(16)).thenReturn("");
+
when(result.getMetaData().getColumnName(17)).thenReturn("indconstraint");
+ when(result.getObject(17)).thenReturn("NULL");
+ when(result.getMetaData().getColumnName(18)).thenReturn("contype");
+ when(result.getObject(18)).thenReturn("p");
+
when(result.getMetaData().getColumnName(19)).thenReturn("condeferrable");
+ when(result.getObject(19)).thenReturn(true);
+ when(result.getMetaData().getColumnName(20)).thenReturn("condeferred");
+ when(result.getObject(20)).thenReturn(false);
+ when(result.getMetaData().getColumnName(21)).thenReturn("amname");
+ when(result.getObject(21)).thenReturn("foo_am_name");
+ when(result.getMetaData().getColumnName(22)).thenReturn("fillfactor");
+ when(result.getObject(22)).thenReturn(90);
+ when(result.getMetaData().getColumnName(23)).thenReturn("is_sys_idx");
+ when(result.getObject(23)).thenReturn(true);
+ return result;
+ }
+}