Skip to content

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

chore: support spatial index for mssql (#16800)

rebelice1a173c50a5bf
diff --git a/backend/plugin/schema/mssql/generate_migration.go b/backend/plugin/schema/mssql/generate_migration.go
index 61b1abf3a6..577521f3df 100644
--- a/backend/plugin/schema/mssql/generate_migration.go
+++ b/backend/plugin/schema/mssql/generate_migration.go
@@ -853,6 +853,27 @@ func generateCreateIndex(schemaName, tableName string, index *storepb.IndexMetad
_, _ = buf.WriteString(")")
}
return buf.String()
+ case "SPATIAL":
+ _, _ = buf.WriteString(" SPATIAL INDEX [")
+ _, _ = buf.WriteString(index.Name)
+ _, _ = buf.WriteString("] ON [")
+ _, _ = buf.WriteString(schemaName)
+ _, _ = buf.WriteString("].[")
+ _, _ = buf.WriteString(tableName)
+ _, _ = buf.WriteString("] (")
+ for i, expr := range index.Expressions {
+ if i > 0 {
+ _, _ = buf.WriteString(", ")
+ }
+ _, _ = buf.WriteString("[")
+ _, _ = buf.WriteString(expr)
+ _, _ = buf.WriteString("]")
+ }
+ _, _ = buf.WriteString(")")
+ // Note: SQL Server requires additional parameters for spatial indexes (USING clause and
+ // BOUNDING_BOX for GEOMETRY columns). Since we don't have column type information here,
+ // the generated DDL may need manual adjustment for spatial indexes.
+ return buf.String()
default:
// Regular indexes
if index.Unique {
diff --git a/backend/plugin/schema/mssql/get_database_definition.go b/backend/plugin/schema/mssql/get_database_definition.go
index f870d46169..b3cd30e6c1 100644
--- a/backend/plugin/schema/mssql/get_database_definition.go
+++ b/backend/plugin/schema/mssql/get_database_definition.go
@@ -362,6 +362,21 @@ func writeNonClusteredColumnStoreIndex(out *strings.Builder, schemaName string,
_, _ = out.WriteString("\n);\n\n")
}
+func writeSpatialIndex(out *strings.Builder, schemaName string, tableName string, index *storepb.IndexMetadata) {
+ _, _ = fmt.Fprintf(out, "CREATE SPATIAL INDEX [%s] ON [%s].[%s] (\n", index.Name, schemaName, tableName)
+ for i, column := range index.Expressions {
+ if i != 0 {
+ _, _ = out.WriteString(",\n")
+ }
+ _, _ = fmt.Fprintf(out, " [%s]", column)
+ }
+ _, _ = out.WriteString("\n)")
+ // Note: SQL Server requires additional parameters for spatial indexes (USING clause and
+ // BOUNDING_BOX for GEOMETRY columns). Since we don't have column type information here,
+ // the generated DDL may need manual adjustment for spatial indexes.
+ _, _ = out.WriteString(";\n\n")
+}
+
func writeNormalIndex(out *strings.Builder, schemaName string, tableName string, index *storepb.IndexMetadata) {
_, _ = out.WriteString("CREATE")
if index.Unique {
@@ -391,6 +406,8 @@ func writeIndex(out *strings.Builder, schemaName string, tableName string, index
writeClusteredColumnStoreIndex(out, schemaName, tableName, index)
case "NONCLUSTERED COLUMNSTORE":
writeNonClusteredColumnStoreIndex(out, schemaName, tableName, index)
+ case "SPATIAL":
+ writeSpatialIndex(out, schemaName, tableName, index)
default:
writeNormalIndex(out, schemaName, tableName, index)
}
diff --git a/backend/plugin/schema/mssql/get_database_metadata.go b/backend/plugin/schema/mssql/get_database_metadata.go
index 1328329e95..371d24b4d9 100644
--- a/backend/plugin/schema/mssql/get_database_metadata.go
+++ b/backend/plugin/schema/mssql/get_database_metadata.go
@@ -218,6 +218,45 @@ func (e *metadataExtractor) EnterCreate_index(ctx *parser.Create_indexContext) {
tableMetadata.Indexes = append(tableMetadata.Indexes, index)
}
+// EnterCreate_spatial_index is called when entering a create spatial index parse tree node
+func (e *metadataExtractor) EnterCreate_spatial_index(ctx *parser.Create_spatial_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: "SPATIAL",
+ Expressions: []string{},
+ Descending: []bool{},
+ IsConstraint: false,
+ }
+
+ // Index name
+ idList := ctx.AllId_()
+ if len(idList) > 0 {
+ index.Name, _ = tsql.NormalizeTSQLIdentifier(idList[0])
+ }
+
+ // Extract column - spatial indexes have a single column
+ if len(idList) > 1 {
+ colName, _ := tsql.NormalizeTSQLIdentifier(idList[1])
+ index.Expressions = append(index.Expressions, colName)
+ index.Descending = append(index.Descending, false)
+ }
+
+ tableMetadata.Indexes = append(tableMetadata.Indexes, index)
+}
+
// EnterCreate_view is called when entering a create view parse tree node
func (e *metadataExtractor) EnterCreate_view(ctx *parser.Create_viewContext) {
if e.err != nil {
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 e3d246b912..c0e24f208a 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
@@ -1944,3 +1944,245 @@
}
]
}
+- input: |
+ CREATE TABLE dbo.locations (
+ id INT IDENTITY(1,1) PRIMARY KEY,
+ name NVARCHAR(100) NOT NULL,
+ position GEOMETRY NOT NULL,
+ area GEOGRAPHY
+ );
+
+ CREATE SPATIAL INDEX idx_locations_position ON dbo.locations (position);
+ CREATE SPATIAL INDEX idx_locations_area ON dbo.locations (area);
+ result: |-
+ {
+ "schemas": [
+ {
+ "name": "dbo",
+ "tables": [
+ {
+ "name": "locations",
+ "columns": [
+ {
+ "name": "id",
+ "type": "INT",
+ "is_identity": true,
+ "identity_seed": 1,
+ "identity_increment": 1
+ },
+ {
+ "name": "name",
+ "type": "NVARCHAR(100)"
+ },
+ {
+ "name": "position",
+ "type": "GEOMETRY"
+ },
+ {
+ "name": "area",
+ "nullable": true,
+ "type": "GEOGRAPHY"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PK_locations_1",
+ "expressions": [
+ "id"
+ ],
+ "descending": [
+ false
+ ],
+ "unique": true,
+ "primary": true,
+ "is_constraint": true
+ },
+ {
+ "name": "idx_locations_position",
+ "expressions": [
+ "position"
+ ],
+ "descending": [
+ false
+ ],
+ "type": "SPATIAL"
+ },
+ {
+ "name": "idx_locations_area",
+ "expressions": [
+ "area"
+ ],
+ "descending": [
+ false
+ ],
+ "type": "SPATIAL"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+- input: |
+ CREATE SCHEMA geo;
+
+ -- Multiple spatial indexes on same table
+ CREATE TABLE geo.world_map (
+ feature_id INT IDENTITY(1,1) PRIMARY KEY,
+ feature_name NVARCHAR(200) NOT NULL,
+ feature_type NVARCHAR(50),
+ geometry_data GEOMETRY NOT NULL,
+ geography_data GEOGRAPHY NOT NULL,
+ bbox_geometry GEOMETRY,
+ center_point GEOMETRY NOT NULL,
+ area_polygon GEOGRAPHY
+ );
+
+ -- Create multiple spatial indexes
+ CREATE SPATIAL INDEX idx_geometry ON geo.world_map (geometry_data);
+ CREATE SPATIAL INDEX idx_geography ON geo.world_map (geography_data);
+ CREATE SPATIAL INDEX idx_bbox ON geo.world_map (bbox_geometry);
+ CREATE SPATIAL INDEX idx_center ON geo.world_map (center_point);
+ CREATE SPATIAL INDEX idx_area ON geo.world_map (area_polygon);
+
+ -- Also create regular indexes
+ CREATE INDEX idx_feature_name ON geo.world_map (feature_name);
+ CREATE INDEX idx_feature_type ON geo.world_map (feature_type);
+ result: |-
+ {
+ "schemas": [
+ {
+ "name": "geo",
+ "tables": [
+ {
+ "name": "world_map",
+ "columns": [
+ {
+ "name": "feature_id",
+ "type": "INT",
+ "is_identity": true,
+ "identity_seed": 1,
+ "identity_increment": 1
+ },
+ {
+ "name": "feature_name",
+ "type": "NVARCHAR(200)"
+ },
+ {
+ "name": "feature_type",
+ "nullable": true,
+ "type": "NVARCHAR(50)"
+ },
+ {
+ "name": "geometry_data",
+ "type": "GEOMETRY"
+ },
+ {
+ "name": "geography_data",
+ "type": "GEOGRAPHY"
+ },
+ {
+ "name": "bbox_geometry",
+ "nullable": true,
+ "type": "GEOMETRY"
+ },
+ {
+ "name": "center_point",
+ "type": "GEOMETRY"
+ },
+ {
+ "name": "area_polygon",
+ "nullable": true,
+ "type": "GEOGRAPHY"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PK_world_map_1",
+ "expressions": [
+ "feature_id"
+ ],
+ "descending": [
+ false
+ ],
+ "unique": true,
+ "primary": true,
+ "is_constraint": true
+ },
+ {
+ "name": "idx_geometry",
+ "expressions": [
+ "geometry_data"
+ ],
+ "descending": [
+ false
+ ],
+ "type": "SPATIAL"
+ },
+ {
+ "name": "idx_geography",
+ "expressions": [
+ "geography_data"
+ ],
+ "descending": [
+ false
+ ],
+ "type": "SPATIAL"
+ },
+ {
+ "name": "idx_bbox",
+ "expressions": [
+ "bbox_geometry"
+ ],
+ "descending": [
+ false
+ ],
+ "type": "SPATIAL"
+ },
+ {
+ "name": "idx_center",
+ "expressions": [
+ "center_point"
+ ],
+ "descending": [
+ false
+ ],
+ "type": "SPATIAL"
+ },
+ {
+ "name": "idx_area",
+ "expressions": [
+ "area_polygon"
+ ],
+ "descending": [
+ false
+ ],
+ "type": "SPATIAL"
+ },
+ {
+ "name": "idx_feature_name",
+ "expressions": [
+ "feature_name"
+ ],
+ "descending": [
+ false
+ ]
+ },
+ {
+ "name": "idx_feature_type",
+ "expressions": [
+ "feature_type"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+# Test case removed: SQL Server doesn't support inline spatial index definitions in CREATE TABLE
+# The correct syntax is to create spatial indexes separately using CREATE SPATIAL INDEX
+
diff --git a/go.mod b/go.mod
index 6814ca3c24..91e6939cef 100644
--- a/go.mod
+++ b/go.mod
@@ -298,7 +298,7 @@ require (
github.com/aws/smithy-go v1.22.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytebase/partiql-parser v0.0.0-20240531101102-1962ff456f2c
- github.com/bytebase/tsql-parser v0.0.0-20250603072002-329b1b7f99a7
+ github.com/bytebase/tsql-parser v0.0.0-20250704100832-7b20f2173b45
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cncf/xds/go v0.0.0-20250326154945-ae57f3c0d45f // indirect
github.com/cockroachdb/cockroach-go/v2 v2.4.0
diff --git a/go.sum b/go.sum
index fedc456b30..2626a350a2 100644
--- a/go.sum
+++ b/go.sum
@@ -847,8 +847,8 @@ github.com/bytebase/tidb2/pkg/parser v0.0.0-20231129002249-5bbb6bb83940 h1:PZ22F
github.com/bytebase/tidb2/pkg/parser v0.0.0-20231129002249-5bbb6bb83940/go.mod h1:yRkiqLFwIqibYg2P7h4bclHjHcJiIFRLKhGRyBcKYus=
github.com/bytebase/trino-parser v0.0.0-20250502000119-9977709a63e8 h1:gSJFO4jHhIIDQjTnyrdDncsxMYhtILZip1B8mCSlJIg=
github.com/bytebase/trino-parser v0.0.0-20250502000119-9977709a63e8/go.mod h1:n0hMVu7KFEBzmYW0q6Kyd/dJ7cng8+YY0sZeDSWIX8g=
-github.com/bytebase/tsql-parser v0.0.0-20250603072002-329b1b7f99a7 h1:wnSjjBXMfE29+da0TUQUpYNa8A1FOZNKYh3jFY2zMl4=
-github.com/bytebase/tsql-parser v0.0.0-20250603072002-329b1b7f99a7/go.mod h1:L9qgmrnVenkm/SL/fmyUSXK5+iMT1ctn/EsMqItSGVk=
+github.com/bytebase/tsql-parser v0.0.0-20250704100832-7b20f2173b45 h1:YDQAmPoltpXNnTA/tOdBix5zfyG1lFumjUPkLaYLih4=
+github.com/bytebase/tsql-parser v0.0.0-20250704100832-7b20f2173b45/go.mod h1:L9qgmrnVenkm/SL/fmyUSXK5+iMT1ctn/EsMqItSGVk=
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
github.com/caarlos0/env/v11 v11.3.1/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U=
github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=