Skip to content

Commit 3b4e0fa

Browse files
elianddbclaude
andcommitted
Refactor validateAutoIncrementType to use switch statement
Improve code readability and maintainability by replacing multiple if statements with a comprehensive switch statement on t.Type(). - Add query import for type constants - Explicit case handling for all data types - Clear grouping of allowed vs disallowed types - Better performance with single switch evaluation 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent cb7c756 commit 3b4e0fa

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

sql/analyzer/validate_create_table.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package analyzer
1717
import (
1818
"strings"
1919

20+
"github.com/dolthub/vitess/go/vt/proto/query"
21+
2022
"github.com/dolthub/go-mysql-server/sql"
2123
"github.com/dolthub/go-mysql-server/sql/expression"
2224
"github.com/dolthub/go-mysql-server/sql/plan"
@@ -790,32 +792,44 @@ func removeInSchema(sch sql.Schema, colName, tableName string) sql.Schema {
790792

791793
// validateAutoIncrementType returns true if the given type can be used with AUTO_INCREMENT
792794
func validateAutoIncrementType(t sql.Type) bool {
793-
// Check for invalid types first
794-
if types.IsEnum(t) || types.IsSet(t) || types.IsBit(t) {
795+
if t == nil {
795796
return false
796797
}
797798

798-
// Check for text/string types - not allowed (includes TEXT, VARCHAR, CHAR, BLOB, BINARY, etc.)
799-
if types.IsText(t) {
799+
switch t.Type() {
800+
// Integer types - allowed
801+
case query.Type_INT8, query.Type_UINT8, query.Type_INT16, query.Type_UINT16,
802+
query.Type_INT24, query.Type_UINT24, query.Type_INT32, query.Type_UINT32,
803+
query.Type_INT64, query.Type_UINT64:
804+
return true
805+
806+
// Floating point types - allowed
807+
case query.Type_FLOAT32, query.Type_FLOAT64:
808+
return true
809+
810+
// Text/string types - not allowed
811+
case query.Type_CHAR, query.Type_VARCHAR, query.Type_TEXT,
812+
query.Type_BLOB, query.Type_BINARY, query.Type_VARBINARY:
800813
return false
801-
}
802814

803-
// Check for datetime/time types - not allowed
804-
if types.IsTime(t) || types.IsDateType(t) || types.IsDatetimeType(t) || types.IsTimestampType(t) || types.IsYear(t) {
815+
// Decimal - not allowed per MySQL behavior
816+
case query.Type_DECIMAL:
805817
return false
806-
}
807818

808-
// Check for numeric types - only these are potentially allowed
809-
if types.IsNumber(t) {
810-
// DECIMAL is not allowed for auto_increment per MySQL behavior
811-
if types.IsDecimal(t) {
812-
return false
813-
}
814-
return true
815-
}
819+
// Date/time types - not allowed
820+
case query.Type_DATE, query.Type_TIME, query.Type_DATETIME,
821+
query.Type_TIMESTAMP, query.Type_YEAR:
822+
return false
816823

817-
// Default to false for any other types (JSON, Geometry, etc.)
818-
return false
824+
// Other types - not allowed
825+
case query.Type_ENUM, query.Type_SET, query.Type_BIT,
826+
query.Type_JSON, query.Type_GEOMETRY:
827+
return false
828+
829+
// Default to false for any other types
830+
default:
831+
return false
832+
}
819833
}
820834

821835
func validateAutoIncrementModify(schema sql.Schema, keyedColumns map[string]bool) error {

0 commit comments

Comments
 (0)