Skip to content

Commit

Permalink
Merge pull request #7589 from planetscale/online-ddl-vrepl-cleanup
Browse files Browse the repository at this point in the history
Online DDL: code cleanup
  • Loading branch information
shlomi-noach authored Apr 22, 2021
2 parents 70642d1 + 50b566b commit 34439fd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 163 deletions.
23 changes: 0 additions & 23 deletions go/vt/vttablet/onlineddl/vrepl/encoding.go

This file was deleted.

46 changes: 9 additions & 37 deletions go/vt/vttablet/onlineddl/vrepl/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,16 @@ import (
"regexp"
"strconv"
"strings"

"vitess.io/vitess/go/vt/schema"
)

var (
sanitizeQuotesRegexp = regexp.MustCompile("('[^']*')")
renameColumnRegexp = regexp.MustCompile(`(?i)\bchange\s+(column\s+|)([\S]+)\s+([\S]+)\s+`)
dropColumnRegexp = regexp.MustCompile(`(?i)\bdrop\s+(column\s+|)([\S]+)$`)
renameTableRegexp = regexp.MustCompile(`(?i)\brename\s+(to|as)\s+`)
autoIncrementRegexp = regexp.MustCompile(`(?i)\bauto_increment[\s]*=[\s]*([0-9]+)`)
alterTableExplicitSchemaTableRegexps = []*regexp.Regexp{
// ALTER TABLE `scm`.`tbl` something
regexp.MustCompile(`(?i)\balter\s+table\s+` + "`" + `([^` + "`" + `]+)` + "`" + `[.]` + "`" + `([^` + "`" + `]+)` + "`" + `\s+(.*$)`),
// ALTER TABLE `scm`.tbl something
regexp.MustCompile(`(?i)\balter\s+table\s+` + "`" + `([^` + "`" + `]+)` + "`" + `[.]([\S]+)\s+(.*$)`),
// ALTER TABLE scm.`tbl` something
regexp.MustCompile(`(?i)\balter\s+table\s+([\S]+)[.]` + "`" + `([^` + "`" + `]+)` + "`" + `\s+(.*$)`),
// ALTER TABLE scm.tbl something
regexp.MustCompile(`(?i)\balter\s+table\s+([\S]+)[.]([\S]+)\s+(.*$)`),
}
alterTableExplicitTableRegexps = []*regexp.Regexp{
// ALTER TABLE `tbl` something
regexp.MustCompile(`(?i)\balter\s+table\s+` + "`" + `([^` + "`" + `]+)` + "`" + `\s+(.*$)`),
// ALTER TABLE tbl something
regexp.MustCompile(`(?i)\balter\s+table\s+([\S]+)\s+(.*$)`),
}
sanitizeQuotesRegexp = regexp.MustCompile("('[^']*')")
renameColumnRegexp = regexp.MustCompile(`(?i)\bchange\s+(column\s+|)([\S]+)\s+([\S]+)\s+`)
dropColumnRegexp = regexp.MustCompile(`(?i)\bdrop\s+(column\s+|)([\S]+)$`)
renameTableRegexp = regexp.MustCompile(`(?i)\brename\s+(to|as)\s+`)
autoIncrementRegexp = regexp.MustCompile(`(?i)\bauto_increment[\s]*=[\s]*([0-9]+)`)
)

// AlterTableParser is a parser tool for ALTER TABLE statements
Expand Down Expand Up @@ -141,22 +127,8 @@ func (p *AlterTableParser) parseAlterToken(alterToken string) (err error) {

// ParseAlterStatement is the main function of th eparser, and parses an ALTER TABLE statement
func (p *AlterTableParser) ParseAlterStatement(alterStatement string) (err error) {
p.alterStatementOptions = alterStatement
for _, alterTableRegexp := range alterTableExplicitSchemaTableRegexps {
if submatch := alterTableRegexp.FindStringSubmatch(p.alterStatementOptions); len(submatch) > 0 {
p.explicitSchema = submatch[1]
p.explicitTable = submatch[2]
p.alterStatementOptions = submatch[3]
break
}
}
for _, alterTableRegexp := range alterTableExplicitTableRegexps {
if submatch := alterTableRegexp.FindStringSubmatch(p.alterStatementOptions); len(submatch) > 0 {
p.explicitTable = submatch[1]
p.alterStatementOptions = submatch[2]
break
}
}
p.explicitSchema, p.explicitTable, p.alterStatementOptions = schema.ParseAlterTableOptions(alterStatement)

alterTokens, _ := p.tokenizeAlterStatement(p.alterStatementOptions)
for _, alterToken := range alterTokens {
alterToken = p.sanitizeQuotesFromAlterStatement(alterToken)
Expand Down
104 changes: 1 addition & 103 deletions go/vt/vttablet/onlineddl/vrepl/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,74 +26,12 @@ package vrepl
import (
"fmt"
"reflect"
"strconv"
"strings"
)

// ColumnType enumerates some important column types
type ColumnType int

const (
UnknownColumnType ColumnType = iota
TimestampColumnType
DateTimeColumnType
EnumColumnType
MediumIntColumnType
JSONColumnType
FloatColumnType
)

const maxMediumintUnsigned int32 = 16777215

// TimezoneConversion indicates how to convert a timezone value
type TimezoneConversion struct {
ToTimezone string
}

// Column represents a table column
type Column struct {
Name string
IsUnsigned bool
Charset string
Type ColumnType
timezoneConversion *TimezoneConversion
}

func (c *Column) convertArg(arg interface{}) interface{} {
if s, ok := arg.(string); ok {
// string, charset conversion
if encoding, ok := charsetEncodingMap[c.Charset]; ok {
arg, _ = encoding.NewDecoder().String(s)
}
return arg
}

if c.IsUnsigned {
if i, ok := arg.(int8); ok {
return uint8(i)
}
if i, ok := arg.(int16); ok {
return uint16(i)
}
if i, ok := arg.(int32); ok {
if c.Type == MediumIntColumnType {
// problem with mediumint is that it's a 3-byte type. There is no compatible golang type to match that.
// So to convert from negative to positive we'd need to convert the value manually
if i >= 0 {
return i
}
return uint32(maxMediumintUnsigned + i + 1)
}
return uint32(i)
}
if i, ok := arg.(int64); ok {
return strconv.FormatUint(uint64(i), 10)
}
if i, ok := arg.(int); ok {
return uint(i)
}
}
return arg
Name string
}

// NewColumns creates a new column array from non empty names
Expand Down Expand Up @@ -178,46 +116,6 @@ func (l *ColumnList) GetColumn(columnName string) *Column {
return nil
}

// SetUnsigned toggles on the unsigned property
func (l *ColumnList) SetUnsigned(columnName string) {
l.GetColumn(columnName).IsUnsigned = true
}

// IsUnsigned returns true when the column is an unsigned numeral
func (l *ColumnList) IsUnsigned(columnName string) bool {
return l.GetColumn(columnName).IsUnsigned
}

// SetCharset sets the charset property
func (l *ColumnList) SetCharset(columnName string, charset string) {
l.GetColumn(columnName).Charset = charset
}

// GetCharset returns the hcarset property
func (l *ColumnList) GetCharset(columnName string) string {
return l.GetColumn(columnName).Charset
}

// SetColumnType sets the type of the column (for interesting types)
func (l *ColumnList) SetColumnType(columnName string, columnType ColumnType) {
l.GetColumn(columnName).Type = columnType
}

// GetColumnType gets type of column, for interesting types
func (l *ColumnList) GetColumnType(columnName string) ColumnType {
return l.GetColumn(columnName).Type
}

// SetConvertDatetimeToTimestamp sets the timezone conversion
func (l *ColumnList) SetConvertDatetimeToTimestamp(columnName string, toTimezone string) {
l.GetColumn(columnName).timezoneConversion = &TimezoneConversion{ToTimezone: toTimezone}
}

// HasTimezoneConversion sees if there's timezone conversion defined (only applicable to temporal values)
func (l *ColumnList) HasTimezoneConversion(columnName string) bool {
return l.GetColumn(columnName).timezoneConversion != nil
}

// String returns a comma separated list of column names
func (l *ColumnList) String() string {
return strings.Join(l.Names(), ",")
Expand Down

0 comments on commit 34439fd

Please sign in to comment.