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

Fix to ignore computed columns during BulkCopy #1562

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java
Original file line number Diff line number Diff line change
Expand Up @@ -1729,40 +1729,36 @@ private void getDestinationMetadata() throws SQLServerException {
if (null != destinationTableMetadata) {
rs = (SQLServerResultSet) destinationTableMetadata;
} else {
stmt = (SQLServerStatement) connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY, connection.getHoldability(), stmtColumnEncriptionSetting);
stmt = (SQLServerStatement) connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, connection.getHoldability(), stmtColumnEncriptionSetting);

// Get destination metadata
rs = stmt.executeQueryInternal(
"sp_executesql N'SET FMTONLY ON SELECT * FROM " + escapedDestinationTableName + " '");
rs = stmt.executeQueryInternal("sp_executesql N'SET FMTONLY ON SELECT * FROM " + escapedDestinationTableName + " '");
}

destColumnCount = rs.getMetaData().getColumnCount();
int destColumnMetadataCount = rs.getMetaData().getColumnCount();
destColumnMetadata = new HashMap<>();
destCekTable = rs.getCekTable();

if (!connection.getServerSupportsColumnEncryption()) {
metaDataQuery = "select collation_name from sys.columns where " + "object_id=OBJECT_ID('"
+ escapedDestinationTableName + "') " + "order by column_id ASC";
} else {
metaDataQuery = "select collation_name, encryption_type from sys.columns where "
+ "object_id=OBJECT_ID('" + escapedDestinationTableName + "') " + "order by column_id ASC";
}
metaDataQuery = "select * from sys.columns where " + "object_id=OBJECT_ID('" + escapedDestinationTableName + "') " + "order by column_id ASC";

try (SQLServerStatement statementMoreMetadata = (SQLServerStatement) connection.createStatement();
SQLServerResultSet rsMoreMetaData = statementMoreMetadata.executeQueryInternal(metaDataQuery)) {
for (int i = 1; i <= destColumnCount; ++i) {
SQLServerResultSet rsMoreMetaData = statementMoreMetadata.executeQueryInternal(metaDataQuery)) {
for (int i = 1; i <= destColumnMetadataCount; ++i) {
if (rsMoreMetaData.next()) {
String bulkCopyEncryptionType = null;
if (connection.getServerSupportsColumnEncryption()) {
bulkCopyEncryptionType = rsMoreMetaData.getString("encryption_type");
}
destColumnMetadata.put(i, new BulkColumnMetaData(rs.getColumn(i),
rsMoreMetaData.getString("collation_name"), bulkCopyEncryptionType));
// Skip computed columns
if (!rsMoreMetaData.getBoolean("is_computed")) {
destColumnMetadata.put(i, new BulkColumnMetaData(rs.getColumn(i), rsMoreMetaData.getString("collation_name"),
bulkCopyEncryptionType));
}
} else {
destColumnMetadata.put(i, new BulkColumnMetaData(rs.getColumn(i)));
}
}
destColumnCount = destColumnMetadata.size();
}
} catch (SQLException e) {
// Unable to retrieve metadata for destination
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,9 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL
CryptoMetadata cryptoMetadata = c.getCryptoMetadata();
int jdbctype;
TypeInfo ti = c.getTypeInfo();
if (ti.getUpdatability() == 0) { // Skip read only columns
continue;
}
checkValidColumns(ti);
if (null != cryptoMetadata) {
jdbctype = cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType().getIntValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class BatchExecutionWithBulkCopyTest extends AbstractTest {
static String squareBracketTableName = RandomUtil.getIdentifier("BulkCopy]]]]test'");
static String doubleQuoteTableName = RandomUtil.getIdentifier("\"BulkCopy\"\"\"\"test\"");
static String schemaTableName = "\"dbo\" . /*some comment */ " + squareBracketTableName;
static String tableNameBulkComputedCols = RandomUtil.getIdentifier("BulkCopyComputedCols");

private Object[] generateExpectedValues() {
float randomFloat = RandomData.generateReal(false);
Expand Down Expand Up @@ -760,6 +761,45 @@ public void testReverseColumnOrder() throws Exception {
}
}

@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xSQLv12)
public void testComputedCols() throws Exception {
String valid = "insert into " + AbstractSQLGenerator.escapeIdentifier(tableNameBulkComputedCols) + " (id, json)"
+ " values (?, ?)";

try (Connection connection = PrepUtil.getConnection(connectionString + ";useBulkCopyForBatchInsert=true;");
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection.prepareStatement(valid);
Statement stmt = (SQLServerStatement) connection.createStatement();) {
Field f1 = SQLServerConnection.class.getDeclaredField("isAzureDW");
f1.setAccessible(true);
f1.set(connection, true);

TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableNameBulkComputedCols), stmt);
String createTable = "create table " + AbstractSQLGenerator.escapeIdentifier(tableNameBulkComputedCols)
+ " (id nvarchar(100) not null, json nvarchar(max) not null,"
+ " vcol1 as json_value([json], '$.vcol1'), vcol2 as json_value([json], '$.vcol2'))";
stmt.execute(createTable);

String jsonValue =
"{\"vcol1\":\"" + UUID.randomUUID().toString() + "\",\"vcol2\":\"" + UUID.randomUUID().toString()
+ "\" }";
String idValue = UUID.randomUUID().toString();
pstmt.setString(1, idValue);
pstmt.setString(2, jsonValue);
pstmt.addBatch();
pstmt.executeBatch();

try (ResultSet rs = stmt.executeQuery(
"select * from " + AbstractSQLGenerator.escapeIdentifier(tableNameBulkComputedCols))) {
rs.next();

assertEquals(idValue, rs.getObject(1));
assertEquals(jsonValue, rs.getObject(2));
}
}
}

@BeforeAll
public static void setupTests() throws Exception {
setConnection();
Expand Down
Loading