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

Updated getIndexInfo() to include Columnstore indexes by using custom query. #2563

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
70f07ce
Updated getIndexInfo() to include Columnstore indexes by using custom…
Ananya2 Dec 13, 2024
c23fb4e
dded unit tests to validate that the updated getIndexInfo() includes …
Ananya2 Dec 13, 2024
2b1eff6
Updated test cases.
Ananya2 Dec 13, 2024
5226956
Fixed pipeline failure to create unique table name.
Ananya2 Dec 13, 2024
2753615
Update DatabaseMetadataTest.java
Ananya2 Dec 13, 2024
0b52da7
Updated DatabaseMetadataTest.java - resolved test case failure
Ananya2 Dec 13, 2024
72cac86
Update SQLServerDatabaseMetaData.java
Ananya2 Dec 13, 2024
c793f84
Update SQLServerDatabaseMetaData.java - throws SQLException
Ananya2 Dec 13, 2024
fd74d8e
Update SQLServerDatabaseMetaData.java
Ananya2 Dec 13, 2024
38d1350
Update DatabaseMetadataTest.java - testing
Ananya2 Dec 13, 2024
e09631b
Update DatabaseMetadataTest.java
Ananya2 Dec 13, 2024
d8c2879
Update DatabaseMetadataTest.java
Ananya2 Dec 13, 2024
b8f6bf5
Update DatabaseMetadataTest.java - testing
Ananya2 Dec 16, 2024
0099ad8
Update DatabaseMetadataTest.java
Ananya2 Dec 16, 2024
d486ddf
Update DatabaseMetadataTest.java
Ananya2 Dec 16, 2024
4d34588
Update DatabaseMetadataTest.java
Ananya2 Dec 16, 2024
cc108dd
Update DatabaseMetadataTest.java
Ananya2 Dec 16, 2024
b367585
Update DatabaseMetadataTest.java
Ananya2 Dec 16, 2024
c0388c6
Update DatabaseMetadataTest.java
Ananya2 Dec 16, 2024
3df9342
Update DatabaseMetadataTest.java
Ananya2 Dec 16, 2024
29c4c33
Update DatabaseMetadataTest.java
Ananya2 Dec 16, 2024
f5631a1
Update DatabaseMetadataTest.java
Ananya2 Dec 16, 2024
84b19a0
Update DatabaseMetadataTest.java
Ananya2 Dec 16, 2024
cd29254
Update DatabaseMetadataTest.java
Ananya2 Dec 16, 2024
d08fb90
Fixed test case failures
Ananya2 Dec 17, 2024
4cbcd3d
fixed test case failure
Ananya2 Dec 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -1197,39 +1197,31 @@ private ResultSet executeSPFkeys(String[] procParams) throws SQLException {
}
}

private static final String[] getIndexInfoColumnNames = { /* 1 */ TABLE_CAT, /* 2 */ TABLE_SCHEM,
/* 3 */ TABLE_NAME, /* 4 */ NON_UNIQUE, /* 5 */ INDEX_QUALIFIER, /* 6 */ INDEX_NAME, /* 7 */ TYPE,
/* 8 */ ORDINAL_POSITION, /* 9 */ COLUMN_NAME, /* 10 */ ASC_OR_DESC, /* 11 */ CARDINALITY, /* 12 */ PAGES,
/* 13 */ FILTER_CONDITION};

@Override
public java.sql.ResultSet getIndexInfo(String cat, String schema, String table, boolean unique,
boolean approximate) throws SQLServerException, SQLTimeoutException {
boolean approximate) throws SQLServerException, SQLTimeoutException, SQLException {
if (loggerExternal.isLoggable(Level.FINER) && Util.isActivityTraceOn()) {
loggerExternal.finer(toString() + ACTIVITY_ID + ActivityCorrelator.getCurrent().toString());
}
checkClosed();
/*
* sp_statistics [ @table_name = ] 'table_name' [ , [ @table_owner = ] 'owner' ] [ , [ @table_qualifier = ]
* 'qualifier' ] [ , [ @index_name = ] 'index_name' ] [ , [ @is_unique = ] 'is_unique' ] [ , [ @accuracy = ]
* 'accuracy' ]
*/
String[] arguments = new String[6];
arguments[0] = table;
arguments[1] = schema;
arguments[2] = cat;
// use default for index name
arguments[3] = "%"; // index name % is default
if (unique)
arguments[4] = "Y"; // is_unique
else
arguments[4] = "N";
if (approximate)
arguments[5] = "Q";
else
arguments[5] = "E";
return getResultSetWithProvidedColumnNames(cat, CallableHandles.SP_STATISTICS, arguments,
getIndexInfoColumnNames);
String query = "SELECT " +
"db_name() AS CatalogName, " +
"sch.name AS SchemaName, " +
"t.name AS TableName, " +
"i.name AS IndexName, " +
"i.type_desc AS IndexType, " +
"i.is_unique AS IsUnique, " +
"c.name AS ColumnName, " +
"ic.key_ordinal AS ColumnOrder " +
"FROM sys.indexes i " +
"INNER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id " +
"INNER JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id " +
"INNER JOIN sys.tables t ON i.object_id = t.object_id " +
"INNER JOIN sys.schemas sch ON t.schema_id = sch.schema_id " +
"WHERE t.name = '" + table + "' " +
Ananya2 marked this conversation as resolved.
Show resolved Hide resolved
"AND sch.name = '" + schema + "' " +
"ORDER BY t.name, i.name, ic.key_ordinal";
return getResultSetFromInternalQueries(cat, query);
}

@Override
Expand Down
103 changes: 103 additions & 0 deletions src/test/java/com/microsoft/sqlserver/jdbc/DatabaseMetadataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.microsoft.sqlserver.jdbc;

import static org.junit.jupiter.api.Assertions.*;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;

public class DatabaseMetadataTest extends AbstractTest {

private static String tableName = AbstractSQLGenerator.escapeIdentifier("DBMetadataTestTable");
private static String col1Name = AbstractSQLGenerator.escapeIdentifier("p1");
private static String col2Name = AbstractSQLGenerator.escapeIdentifier("p2");
private static String col3Name = AbstractSQLGenerator.escapeIdentifier("p3");

@BeforeAll
public static void setupTests() throws Exception {
setConnection();
}

@Test
public void testGetIndexInfo() throws SQLException, SQLServerException {
ResultSet rs = null;
try (Connection connection = getConnection(); Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(tableName, stmt);
String createTableSQL = "CREATE TABLE " + tableName + " (" +
Ananya2 marked this conversation as resolved.
Show resolved Hide resolved
col1Name + " INT, " +
col2Name + " INT, " +
col3Name + " INT)";
stmt.executeUpdate(createTableSQL);

String createClusteredIndexSQL = "CREATE CLUSTERED INDEX IDX_Clustered ON " + tableName + "(" + col1Name + ")";
stmt.executeUpdate(createClusteredIndexSQL);

String createNonClusteredIndexSQL = "CREATE NONCLUSTERED INDEX IDX_NonClustered ON " + tableName + "(" + col2Name + ")";
stmt.executeUpdate(createNonClusteredIndexSQL);

String createColumnstoreIndexSQL = "CREATE NONCLUSTERED COLUMNSTORE INDEX IDX_Columnstore ON " + tableName + "(" + col3Name + ")";
stmt.executeUpdate(createColumnstoreIndexSQL);
String catalog = connection.getCatalog();
String schema = "dbo";

DatabaseMetaData dbMetadata = connection.getMetaData();
rs = dbMetadata.getIndexInfo(catalog, schema, tableName, false, false);

boolean hasClusteredIndex = false;
boolean hasNonClusteredIndex = false;
boolean hasColumnstoreIndex = false;
System.out.println("Testing getIndexInfo " + rs + " " + rs.next());
String query =
"SELECT " +
" db_name() AS CatalogName, " +
" sch.name AS SchemaName, " +
" t.name AS TableName, " +
" i.name AS IndexName, " +
" i.type_desc AS IndexType, " +
" i.is_unique AS IsUnique, " +
" c.name AS ColumnName, " +
" ic.key_ordinal AS ColumnOrder " +
"FROM " +
" sys.indexes i " +
"INNER JOIN " +
" sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id " +
"INNER JOIN " +
" sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id " +
"INNER JOIN " +
" sys.tables t ON i.object_id = t.object_id " +
"INNER JOIN " +
" sys.schemas sch ON t.schema_id = sch.schema_id " +

"WHERE t.name = '" + tableName + "' " +
"AND sch.name = '" + schema + "' " +
"ORDER BY " +
" t.name, i.name, ic.key_ordinal;";
rs = stmt.executeQuery(query);
System.out.println("Testing query " + rs + " " + rs.next());
while (rs.next()) {
String indexType = rs.getString("IndexType");
Ananya2 marked this conversation as resolved.
Show resolved Hide resolved
String indexName = rs.getString("IndexName");
System.out.println(indexType + " " + indexName);

if (indexType.contains("COLUMNSTORE")) {
hasColumnstoreIndex = true;
} else if (indexType.equals("CLUSTERED")) {
hasClusteredIndex = true;
} else if (indexType.equals("NONCLUSTERED")) {
hasNonClusteredIndex = true;
}
}

assertTrue(hasClusteredIndex, "CLUSTERED index found.");
Ananya2 marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(hasNonClusteredIndex, "NONCLUSTERED index found.");
Ananya2 marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(hasColumnstoreIndex, "COLUMNSTORE index found.");
Ananya2 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading