Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: support column store for get_database_metadata (#16388)

rebelice2b65d873b794
diff --git a/backend/plugin/schema/mssql/get_database_metadata.go b/backend/plugin/schema/mssql/get_database_metadata.go
index ca0adfcd33..1d7cf60600 100644
--- a/backend/plugin/schema/mssql/get_database_metadata.go
+++ b/backend/plugin/schema/mssql/get_database_metadata.go
@@ -161,6 +161,13 @@ func (e *metadataExtractor) EnterCreate_table(ctx *parser.Create_tableContext) {
if columnDefList := ctx.Column_def_table_constraints(); columnDefList != nil {
e.extractTableElements(columnDefList, tableMetadata, schema)
}
+
+ // Extract table indices (including columnstore indexes)
+ if tableIndices := ctx.AllTable_indices(); tableIndices != nil {
+ for _, tableIndex := range tableIndices {
+ e.extractTableIndex(tableIndex, tableMetadata)
+ }
+ }
}
// EnterCreate_index is called when entering a create index parse tree node
@@ -292,6 +299,73 @@ func (e *metadataExtractor) EnterCreate_or_alter_function(ctx *parser.Create_or_
schemaMetadata.Functions = append(schemaMetadata.Functions, functionMetadata)
}
+// EnterCreate_columnstore_index is called when entering a create clustered columnstore index parse tree node
+func (e *metadataExtractor) EnterCreate_columnstore_index(ctx *parser.Create_columnstore_indexContext) {
+ if e.err != nil {
+ return
+ }
+
+ // Extract table reference
+ if ctx.Table_name() == nil {
+ return
+ }
+
+ _, schema, table := e.normalizeTableNameSeparated(ctx.Table_name(), e.currentDatabase, e.currentSchema)
+
+ tableMetadata := e.getOrCreateTable(schema, table)
+
+ // Extract index metadata
+ index := &storepb.IndexMetadata{
+ Type: "CLUSTERED COLUMNSTORE",
+ Expressions: []string{}, // Clustered columnstore indexes don't have specific columns
+ Descending: []bool{},
+ IsConstraint: false,
+ }
+
+ // Index name
+ if ctx.Id_(0) != nil {
+ index.Name, _ = tsql.NormalizeTSQLIdentifier(ctx.Id_(0))
+ }
+
+ tableMetadata.Indexes = append(tableMetadata.Indexes, index)
+}
+
+// EnterCreate_nonclustered_columnstore_index is called when entering a create nonclustered columnstore index parse tree node
+func (e *metadataExtractor) EnterCreate_nonclustered_columnstore_index(ctx *parser.Create_nonclustered_columnstore_indexContext) {
+ if e.err != nil {
+ return
+ }
+
+ // Extract table reference
+ if ctx.Table_name() == nil {
+ return
+ }
+
+ _, schema, table := e.normalizeTableNameSeparated(ctx.Table_name(), e.currentDatabase, e.currentSchema)
+
+ tableMetadata := e.getOrCreateTable(schema, table)
+
+ // Extract index metadata
+ index := &storepb.IndexMetadata{
+ Type: "NONCLUSTERED COLUMNSTORE",
+ Expressions: []string{},
+ Descending: []bool{},
+ IsConstraint: false,
+ }
+
+ // Index name
+ if ctx.Id_(0) != nil {
+ index.Name, _ = tsql.NormalizeTSQLIdentifier(ctx.Id_(0))
+ }
+
+ // Extract columns
+ if columnList := ctx.Column_name_list_with_order(); columnList != nil {
+ e.extractIndexColumns(columnList, index)
+ }
+
+ tableMetadata.Indexes = append(tableMetadata.Indexes, index)
+}
+
// EnterCreate_sequence is called when entering a create sequence parse tree node
func (e *metadataExtractor) EnterCreate_sequence(ctx *parser.Create_sequenceContext) {
if e.err != nil {
@@ -653,6 +727,80 @@ func (*metadataExtractor) extractIndexColumns(ctx parser.IColumn_name_list_with_
}
}
+// extractTableIndex extracts index information from table_indices context
+func (e *metadataExtractor) extractTableIndex(ctx parser.ITable_indicesContext, table *storepb.TableMetadata) {
+ if ctx == nil {
+ return
+ }
+
+ index := &storepb.IndexMetadata{
+ Expressions: []string{},
+ Descending: []bool{},
+ IsConstraint: false,
+ }
+
+ // Index name
+ idList := ctx.AllId_()
+ if len(idList) > 0 {
+ index.Name, _ = tsql.NormalizeTSQLIdentifier(idList[0])
+ }
+
+ // Check for UNIQUE
+ if ctx.UNIQUE() != nil {
+ index.Unique = true
+ }
+
+ // Check for index type
+ if ctx.CLUSTERED() != nil && ctx.COLUMNSTORE() != nil {
+ index.Type = "CLUSTERED COLUMNSTORE"
+ // Clustered columnstore indexes don't have specific columns
+ } else if ctx.NONCLUSTERED() != nil && ctx.COLUMNSTORE() != nil {
+ index.Type = "NONCLUSTERED COLUMNSTORE"
+ // Extract columns for nonclustered columnstore
+ if columnList := ctx.Column_name_list(); columnList != nil {
+ e.extractColumnNameList(columnList, index)
+ }
+ } else if ctx.COLUMNSTORE() != nil {
+ // COLUMNSTORE without NONCLUSTERED means nonclustered by default
+ index.Type = "NONCLUSTERED COLUMNSTORE"
+ // Extract columns
+ if columnList := ctx.Column_name_list(); columnList != nil {
+ e.extractColumnNameList(columnList, index)
+ }
+ } else {
+ // Regular index
+ if clustered := ctx.Clustered(); clustered != nil {
+ if clustered.CLUSTERED() != nil {
+ index.Type = "CLUSTERED"
+ } else if clustered.NONCLUSTERED() != nil {
+ index.Type = "NONCLUSTERED"
+ }
+ }
+ // Extract columns
+ if columnList := ctx.Column_name_list_with_order(); columnList != nil {
+ e.extractIndexColumns(columnList, index)
+ }
+ }
+
+ table.Indexes = append(table.Indexes, index)
+}
+
+// extractColumnNameList extracts column names without order for columnstore indexes
+func (*metadataExtractor) extractColumnNameList(ctx parser.IColumn_name_listContext, index *storepb.IndexMetadata) {
+ if ctx == nil {
+ return
+ }
+
+ if idList := ctx.AllId_(); idList != nil {
+ for _, id := range idList {
+ colName, _ := tsql.NormalizeTSQLIdentifier(id)
+ index.Expressions = append(index.Expressions, colName)
+ // Columnstore indexes don't have DESC/ASC specification
+ index.Descending = append(index.Descending, false)
+ }
+ }
+}
+
// Helper functions
func extractDataType(ctx parser.IData_typeContext) string {
diff --git a/backend/plugin/schema/mssql/test-data/test_get_database_metadata.yaml b/backend/plugin/schema/mssql/test-data/test_get_database_metadata.yaml
index 47e3bb7598..0508df9ca7 100644
--- a/backend/plugin/schema/mssql/test-data/test_get_database_metadata.yaml
+++ b/backend/plugin/schema/mssql/test-data/test_get_database_metadata.yaml
@@ -1767,3 +1767,286 @@
}
]
}
+- input: |
+ CREATE TABLE dbo.sales_data (
+ sale_id INT IDENTITY(1,1) PRIMARY KEY,
+ product_id INT NOT NULL,
+ sale_date DATE NOT NULL,
+ quantity INT NOT NULL,
+ amount DECIMAL(10,2) NOT NULL
+ );
+
+ CREATE CLUSTERED COLUMNSTORE INDEX CCI_sales_data ON dbo.sales_data;
+ result: |-
+ {
+ "schemas": [
+ {
+ "name": "dbo",
+ "tables": [
+ {
+ "name": "sales_data",
+ "columns": [
+ {
+ "name": "sale_id",
+ "DefaultValue": null,
+ "type": "INT",
+ "is_identity": true,
+ "identity_seed": 1,
+ "identity_increment": 1
+ },
+ {
+ "name": "product_id",
+ "DefaultValue": null,
+ "type": "INT"
+ },
+ {
+ "name": "sale_date",
+ "DefaultValue": null,
+ "type": "DATE"
+ },
+ {
+ "name": "quantity",
+ "DefaultValue": null,
+ "type": "INT"
+ },
+ {
+ "name": "amount",
+ "DefaultValue": null,
+ "type": "DECIMAL(10,2)"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PK_sales_data_1",
+ "expressions": [
+ "sale_id"
+ ],
+ "descending": [
+ false
+ ],
+ "unique": true,
+ "primary": true,
+ "is_constraint": true
+ },
+ {
+ "name": "CCI_sales_data",
+ "type": "CLUSTERED COLUMNSTORE"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+- input: |
+ CREATE TABLE dbo.analytics_data (
+ id INT NOT NULL,
+ year INT NOT NULL,
+ month INT NOT NULL,
+ revenue DECIMAL(15,2),
+ expenses DECIMAL(15,2)
+ );
+
+ CREATE NONCLUSTERED COLUMNSTORE INDEX NCCI_analytics ON dbo.analytics_data (year, month, revenue, expenses);
+ result: |-
+ {
+ "schemas": [
+ {
+ "name": "dbo",
+ "tables": [
+ {
+ "name": "analytics_data",
+ "columns": [
+ {
+ "name": "id",
+ "DefaultValue": null,
+ "type": "INT"
+ },
+ {
+ "name": "year",
+ "DefaultValue": null,
+ "type": "INT"
+ },
+ {
+ "name": "month",
+ "DefaultValue": null,
+ "type": "INT"
+ },
+ {
+ "name": "revenue",
+ "DefaultValue": null,
+ "nullable": true,
+ "type": "DECIMAL(15,2)"
+ },
+ {
+ "name": "expenses",
+ "DefaultValue": null,
+ "nullable": true,
+ "type": "DECIMAL(15,2)"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "NCCI_analytics",
+ "expressions": [
+ "year",
+ "month",
+ "revenue",
+ "expenses"
+ ],
+ "descending": [
+ false,
+ false,
+ false,
+ false
+ ],
+ "type": "NONCLUSTERED COLUMNSTORE"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+- input: |
+ CREATE TABLE dbo.fact_sales (
+ sale_id INT NOT NULL,
+ product_id INT NOT NULL,
+ customer_id INT NOT NULL,
+ sale_date DATE NOT NULL,
+ amount DECIMAL(10,2) NOT NULL,
+ INDEX IX_fact_sales CLUSTERED COLUMNSTORE
+ );
+ result: |-
+ {
+ "schemas": [
+ {
+ "name": "dbo",
+ "tables": [
+ {
+ "name": "fact_sales",
+ "columns": [
+ {
+ "name": "sale_id",
+ "DefaultValue": null,
+ "type": "INT"
+ },
+ {
+ "name": "product_id",
+ "DefaultValue": null,
+ "type": "INT"
+ },
+ {
+ "name": "customer_id",
+ "DefaultValue": null,
+ "type": "INT"
+ },
+ {
+ "name": "sale_date",
+ "DefaultValue": null,
+ "type": "DATE"
+ },
+ {
+ "name": "amount",
+ "DefaultValue": null,
+ "type": "DECIMAL(10,2)"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "IX_fact_sales",
+ "type": "CLUSTERED COLUMNSTORE"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+- input: |
+ CREATE TABLE dbo.hybrid_table (
+ id INT PRIMARY KEY NONCLUSTERED,
+ category VARCHAR(50) NOT NULL,
+ value DECIMAL(10,2),
+ created_date DATETIME,
+ INDEX IX_hybrid_columnstore NONCLUSTERED COLUMNSTORE (category, value, created_date),
+ INDEX IX_hybrid_regular NONCLUSTERED (category, created_date DESC)
+ );
+ result: |-
+ {
+ "schemas": [
+ {
+ "name": "dbo",
+ "tables": [
+ {
+ "name": "hybrid_table",
+ "columns": [
+ {
+ "name": "id",
+ "DefaultValue": null,
+ "type": "INT"
+ },
+ {
+ "name": "category",
+ "DefaultValue": null,
+ "type": "VARCHAR(50)"
+ },
+ {
+ "name": "value",
+ "DefaultValue": null,
+ "nullable": true,
+ "type": "DECIMAL(10,2)"
+ },
+ {
+ "name": "created_date",
+ "DefaultValue": null,
+ "nullable": true,
+ "type": "DATETIME"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PK_hybrid_table_1",
+ "expressions": [
+ "id"
+ ],
+ "descending": [
+ false
+ ],
+ "type": "NONCLUSTERED",
+ "unique": true,
+ "primary": true,
+ "is_constraint": true
+ },
+ {
+ "name": "IX_hybrid_columnstore",
+ "expressions": [
+ "category",
+ "value",
+ "created_date"
+ ],
+ "descending": [
+ false,
+ false,
+ false
+ ],
+ "type": "NONCLUSTERED COLUMNSTORE"
+ },
+ {
+ "name": "IX_hybrid_regular",
+ "expressions": [
+ "category",
+ "created_date"
+ ],
+ "descending": [
+ false,
+ true
+ ],
+ "type": "NONCLUSTERED"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }