Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MS SQL in Jdbc SQL connector [HZ-2694] #25071

Merged
merged 30 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1be42c7
add MSSQLAllTypesInsertJdbcSqlConnectorTest
Edizc Jul 25, 2023
d3be4ed
Add MSSQLGenericMapStoreTest
Edizc Aug 1, 2023
490204e
Add HazelcastMSSQlDialect and Update, SelectJdbcConnectorTest for Mic…
Edizc Aug 2, 2023
e5bb270
Unnecessary import and a comment corrected in HazelcastMSSQLDialect
Edizc Aug 2, 2023
7001339
Add the rest of the tests
Edizc Aug 4, 2023
7a43387
Add MSSQL's LEN function
Edizc Aug 7, 2023
c055b2d
Fixed Checkstyle Issues
Edizc Aug 7, 2023
2529e52
Make Length to LEN translation
TomaszGaweda Aug 7, 2023
149c3fe
Removed commented out tests and unused import
Edizc Aug 8, 2023
96419a8
Changed the upsert statement from IF to MERGE
Edizc Aug 9, 2023
a12e698
Merge branch 'hazelcast:master' into add-mssqlTests
Edizc Aug 9, 2023
764b40c
Updated MSSQLUpsertQueryBuilder and removed unnecessary code
Edizc Aug 9, 2023
ac78843
Merge branch 'add-mssqlTests' of github.com:Edizc/hazelcast into add-…
Edizc Aug 9, 2023
fa1b867
Add After annotation to close mocks
Edizc Aug 9, 2023
51c6ede
added GETTERS to serialization in readData and writeData
Edizc Aug 9, 2023
5277e6a
updated initializeGetters function
Edizc Aug 9, 2023
b6f8f6e
Add missing predicate tests
frant-hartm Aug 14, 2023
0c9fa89
Fix test for TIME data type
frant-hartm Aug 14, 2023
38f23f6
Cleanup bit/boolean support
frant-hartm Aug 16, 2023
b37d4ea
Fix CreateDataConnectionJdbcSqlConnectorTest
frant-hartm Aug 16, 2023
1dd1464
Cleanup duplication
frant-hartm Aug 16, 2023
c7add0a
Add BIT back - needed for MySQL boolean support
frant-hartm Aug 16, 2023
075e21b
Organized MSSQLUpsertQueryBuilder
Edizc Aug 16, 2023
50fc63d
Merge branch 'add-mssqlTests' of github.com:Edizc/hazelcast into add-…
Edizc Aug 16, 2023
46f4df2
add GettersProvider
Edizc Aug 16, 2023
19728c0
fixed GettersProvider
Edizc Aug 16, 2023
5294505
removed commented code in SelectProcessorSupplier
Edizc Aug 16, 2023
b954cec
Sorted the supported Sql dialect names
Edizc Aug 16, 2023
1a42b7f
Add BIT back (needed for MySQL boolean support)
frant-hartm Aug 16, 2023
9160aa7
Cleanup getters
frant-hartm Aug 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.hazelcast.test.annotation.QuickTest;
import com.hazelcast.test.jdbc.H2DatabaseProvider;
import com.hazelcast.test.jdbc.MySQLDatabaseProvider;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -560,7 +561,7 @@ public void givenTableNameProperty_whenCreateMapLoader_thenUseTableNameWithCusto
}

private static void createSchema(String schemaName) throws SQLException {
executeJdbc("CREATE SCHEMA IF NOT EXISTS " + schemaName + " ");
executeJdbc(JdbcSqlTestSupport.databaseProvider.createSchemaQuery(schemaName));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
executeJdbc(JdbcSqlTestSupport.databaseProvider.createSchemaQuery(schemaName));
executeJdbc(databaseProvider.createSchemaQuery(schemaName));

}

private <K> GenericMapLoader<K> createMapLoader() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2023 Hazelcast Inc.
*
* Licensed under the Hazelcast Community License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://hazelcast.com/hazelcast-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hazelcast.mapstore.mssql;

import com.hazelcast.mapstore.GenericMapStoreTest;
import com.hazelcast.nio.serialization.genericrecord.GenericRecord;
import com.hazelcast.test.annotation.NightlyTest;
import com.hazelcast.test.jdbc.MSSQLDatabaseProvider;
import com.hazelcast.test.jdbc.MySQLDatabaseProvider;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.util.Properties;

import static com.hazelcast.mapstore.GenericMapLoader.DATA_CONNECTION_REF_PROPERTY;
import static com.hazelcast.mapstore.GenericMapLoader.EXTERNAL_NAME_PROPERTY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeFalse;

@Category(NightlyTest.class)
public class MSSQLGenericMapStoreTest extends GenericMapStoreTest {

@BeforeClass
public static void beforeClass() {
initialize(new MSSQLDatabaseProvider());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.hazelcast.jet.impl.util.Util;
import com.hazelcast.jet.sql.impl.connector.HazelcastRexNode;
import com.hazelcast.jet.sql.impl.connector.SqlConnector;
import com.hazelcast.jet.sql.impl.connector.jdbc.mssql.HazelcastMSSQLDialect;
import com.hazelcast.jet.sql.impl.connector.jdbc.mysql.HazelcastMySqlDialect;
import com.hazelcast.spi.impl.NodeEngine;
import com.hazelcast.sql.impl.QueryException;
Expand Down Expand Up @@ -292,6 +293,8 @@ private static SqlDialect resolveDialect(DatabaseMetaData databaseMetaData) thro
switch (databaseMetaData.getDatabaseProductName().toUpperCase(Locale.ROOT).trim()) {
case "MYSQL":
return new HazelcastMySqlDialect(SqlDialects.createContext(databaseMetaData));
case "MICROSOFT SQL SERVER":
return new HazelcastMSSQLDialect(SqlDialects.createContext(databaseMetaData));

default:
return SqlDialectFactoryImpl.INSTANCE.create(databaseMetaData);
Expand Down Expand Up @@ -549,9 +552,11 @@ private static QueryDataType resolveType(String columnTypeName) {
return QueryDataType.TIME;

case "TIMESTAMP":
case "DATETIME":
return QueryDataType.TIMESTAMP;

case "TIMESTAMP WITH TIME ZONE":
case "DATETIMEOFFSET":
return QueryDataType.TIMESTAMP_WITH_TZ_OFFSET_DATE_TIME;

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2023 Hazelcast Inc.
*
* Licensed under the Hazelcast Community License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://hazelcast.com/hazelcast-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hazelcast.jet.sql.impl.connector.jdbc.mssql;

import com.hazelcast.jet.sql.impl.validate.operators.string.HazelcastConcatOperator;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.SqlWriter.Frame;
import org.apache.calcite.sql.SqlWriter.FrameTypeEnum;
import org.apache.calcite.sql.dialect.MssqlSqlDialect;

/**
* Custom dialect for MSSQL which allows correct unparsing of MSSQL specific operators, like CONCAT
*/
public class HazelcastMSSQLDialect extends MssqlSqlDialect {

/**
* Creates a HazelcastMSSQLDialect.
*/
public HazelcastMSSQLDialect(Context context) {
super(context);
}

@Override
public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
switch (call.getKind()) {
case OTHER:
if (call.getOperator() instanceof HazelcastConcatOperator) {
unparseConcat(writer, call);
break;
} else {
super.unparseCall(writer, call, leftPrec, rightPrec);
break;
}

default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
}

private void unparseConcat(SqlWriter writer, SqlCall call) {
writer.print("CONCAT");
Frame frame = writer.startList(FrameTypeEnum.PARENTHESES, "(", ")");
for (SqlNode operand : call.getOperandList()) {
writer.sep(",");
operand.unparse(writer, 0, 0);
}
writer.endList(frame);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.time.ZoneOffset;
import java.util.Collection;

import static org.assertj.core.util.Lists.newArrayList;
import static java.util.Arrays.asList;

@RunWith(HazelcastParametrizedRunner.class)
Expand Down Expand Up @@ -107,6 +108,7 @@ public void insertRowWithAllTypes() throws Exception {
execute("INSERT INTO " + mappingName + " VALUES(1, ?)", javaValue);

assertJdbcRowsAnyOrder(tableName,
newArrayList(Integer.class, jdbcValue.getClass()),
new Row(0, jdbcValue),
new Row(1, jdbcValue)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ protected static void assertJdbcRowsAnyOrder(String tableName, Row... rows) {
assertThat(actualRows).containsExactlyInAnyOrderElementsOf(Arrays.asList(rows));
}

protected static void assertJdbcRowsAnyOrder(String tableName, List<Class<?>> columnType,Row... rows) {
List<Row> actualRows = jdbcRowsTable(tableName, columnType);
assertThat(actualRows).containsExactlyInAnyOrderElementsOf(Arrays.asList(rows));
}

protected static void assertJdbcQueryRowsAnyOrder(String query, Row... rows) {
List<Row> actualRows = jdbcRows(query);
assertThat(actualRows).containsExactlyInAnyOrderElementsOf(Arrays.asList(rows));
Expand All @@ -193,12 +198,19 @@ protected static void assertJdbcQueryRowsAnyOrder(String query, Row... rows) {
protected static List<Row> jdbcRowsTable(String tableName) {
return jdbcRows("SELECT * FROM " + tableName);
}
protected static List<Row> jdbcRowsTable(String tableName, List<Class<?>> columnType) {
frant-hartm marked this conversation as resolved.
Show resolved Hide resolved
return jdbcRows("SELECT * FROM " + tableName, columnType);
}

@Nonnull
protected static List<Row> jdbcRows(String query) {
return jdbcRows(query, dbConnectionUrl);
}

protected static List<Row> jdbcRows(String query, List<Class<?>> columnType) {
return jdbcRows(query, dbConnectionUrl, columnType);
}

public static List<Row> jdbcRows(String query, String connectionUrl) {
List<Row> rows = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(connectionUrl);
Expand All @@ -218,4 +230,23 @@ public static List<Row> jdbcRows(String query, String connectionUrl) {
throw new RuntimeException(e);
}
}
public static List<Row> jdbcRows(String query, String connectionUrl, List<Class<?>> columnType) {
List<Row> rows = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(connectionUrl);
Statement stmt = conn.createStatement()
) {
stmt.execute(query);
ResultSet resultSet = stmt.getResultSet();
while (resultSet.next()) {
Object[] values = new Object[resultSet.getMetaData().getColumnCount()];
for (int i = 0; i < values.length; i++) {
values[i] = resultSet.getObject(i + 1, columnType.get(i));
frant-hartm marked this conversation as resolved.
Show resolved Hide resolved
}
rows.add(new Row(values));
}
return rows;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2023 Hazelcast Inc.
*
* Licensed under the Hazelcast Community License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://hazelcast.com/hazelcast-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hazelcast.jet.sql.impl.connector.jdbc.mssql;

import com.hazelcast.jet.sql.impl.connector.jdbc.AllTypesInsertJdbcSqlConnectorTest;
import com.hazelcast.test.annotation.NightlyTest;
import com.hazelcast.test.jdbc.MSSQLDatabaseProvider;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.experimental.categories.Category;

import static org.assertj.core.api.Assumptions.assumeThat;

@Category(NightlyTest.class)
public class MSSQLAllTypesInsertJdbcSqlConnectorTest extends AllTypesInsertJdbcSqlConnectorTest {

@BeforeClass
public static void beforeClass() {
initialize(new MSSQLDatabaseProvider());
}


@Before
public void setUp() throws Exception {
// MSSQL doesn't support BOOLEAN but BIT can be used instead and, uses FLOAT as 8-byte floating point type
frant-hartm marked this conversation as resolved.
Show resolved Hide resolved
// For Float in MSSQL see https://learn.microsoft.com/en-us/sql/t-sql/data-types/float-and-real-transact-sql?view=sql-server-ver16
// For BIT in MSSQL see https://learn.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql?view=sql-server-ver16
// For TIMESTAMP in MSSQL see https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2005/ms182776(v=sql.90)?redirectedfrom=MSDN
// For DATETIMEOFFSET in MSSQL see https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetimeoffset-transact-sql?view=sql-server-ver16
if (type.equals("BOOLEAN")) {
type = "BIT";
}
if (type.equals("DOUBLE")) {
type = "FLOAT";
}
if (type.equals("TIMESTAMP")) {
type = "DATETIME";
}
if (type.equals("TIMESTAMP WITH TIME ZONE")) {
type = "DATETIMEOFFSET";
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 Hazelcast Inc.
*
* Licensed under the Hazelcast Community License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://hazelcast.com/hazelcast-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hazelcast.jet.sql.impl.connector.jdbc.mssql;

import com.hazelcast.jet.sql.impl.connector.jdbc.SelectJdbcSqlConnectorTest;
import com.hazelcast.test.annotation.NightlyTest;
import com.hazelcast.test.jdbc.MSSQLDatabaseProvider;
import org.junit.BeforeClass;
import org.junit.experimental.categories.Category;

@Category(NightlyTest.class)
public class MSSQLSelectJdbcSqlConnectorTest extends SelectJdbcSqlConnectorTest {

@BeforeClass
public static void beforeClass() {
initialize(new MSSQLDatabaseProvider());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 Hazelcast Inc.
*
* Licensed under the Hazelcast Community License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://hazelcast.com/hazelcast-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hazelcast.jet.sql.impl.connector.jdbc.mssql;

import com.hazelcast.jet.sql.impl.connector.jdbc.UpdateJdbcSqlConnectorTest;
import com.hazelcast.test.annotation.NightlyTest;
import com.hazelcast.test.jdbc.MSSQLDatabaseProvider;
import org.junit.BeforeClass;
import org.junit.experimental.categories.Category;

@Category(NightlyTest.class)
public class MSSQLUpdateJdbcSqlConnectorTest extends UpdateJdbcSqlConnectorTest {

@BeforeClass
public static void beforeClass() {
initialize(new MSSQLDatabaseProvider());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ public void shutdown() {
container = null;
}
}

@Override
public String createSchemaQuery(String schemaName) {
return "IF NOT EXISTS (SELECT 0 FROM information_schema.schemata WHERE schema_name = '" + schemaName
+ "') BEGIN EXEC sp_executesql N'CREATE SCHEMA " + schemaName + "'; END";
frant-hartm marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ default String quote(String[] parts) {
.map(part -> '\"' + part.replaceAll("\"", "\"\"") + '\"')
.collect(joining("."));
};

default String createSchemaQuery(String schemaName) {
return "CREATE SCHEMA IF NOT EXISTS " + schemaName;
}
}