From 8c20367b401021da10ebcb892cf891fe4678e549 Mon Sep 17 00:00:00 2001 From: Daylon Wilkins Date: Mon, 5 Feb 2024 03:38:10 -0800 Subject: [PATCH 1/2] Added a way for Doltgres to inject expressions --- go/vt/sqlparser/ast.go | 46 +++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/go/vt/sqlparser/ast.go b/go/vt/sqlparser/ast.go index b6a9d0449d3..5a4888d16d2 100644 --- a/go/vt/sqlparser/ast.go +++ b/go/vt/sqlparser/ast.go @@ -2613,6 +2613,9 @@ type ColumnType struct { // The base type string Type string + // The base type if it has already been resolved + ResolvedType any + // Generic field options. Null BoolVal NotNull BoolVal @@ -2771,17 +2774,19 @@ func (ct *ColumnType) merge(other ColumnType) error { // Format returns a canonical string representation of the type and all relevant options func (ct *ColumnType) Format(buf *TrackedBuffer) { - buf.Myprintf("%s", ct.Type) - - if ct.Length != nil && ct.Scale != nil { - buf.Myprintf("(%v,%v)", ct.Length, ct.Scale) - - } else if ct.Length != nil { - buf.Myprintf("(%v)", ct.Length) - } + if stringer, ok := ct.ResolvedType.(fmt.Stringer); ok { + buf.WriteString(stringer.String()) + } else { + buf.Myprintf("%s", ct.Type) + if ct.Length != nil && ct.Scale != nil { + buf.Myprintf("(%v,%v)", ct.Length, ct.Scale) - if len(ct.EnumValues) > 0 { - buf.Myprintf("('%s')", strings.Join(ct.EnumValues, "', '")) + } else if ct.Length != nil { + buf.Myprintf("(%v)", ct.Length) + } + if len(ct.EnumValues) > 0 { + buf.Myprintf("('%s')", strings.Join(ct.EnumValues, "', '")) + } } opts := make([]string, 0, 16) @@ -7224,3 +7229,24 @@ func (node *SrsAttribute) Format(buf *TrackedBuffer) { buf.Myprintf("organization '%s' identified by %v\n", node.Organization, node.OrgID) buf.Myprintf("description '%s'", node.Description) } + +// DoltgresInjectedExpr allows DoltgreSQL to bypass AST analysis, which is currently specific to MySQL. +type DoltgresInjectedExpr struct { + Expression any +} + +var _ Expr = DoltgresInjectedExpr{} + +func (d DoltgresInjectedExpr) iExpr() {} + +func (d DoltgresInjectedExpr) replace(from, to Expr) bool { + return false +} + +func (d DoltgresInjectedExpr) Format(buf *TrackedBuffer) { + if stringer, ok := d.Expression.(fmt.Stringer); ok { + buf.WriteString(stringer.String()) + } else { + buf.WriteString("DoltgresInjectedExpr") + } +} From aec3497d9d8399cf490d8911a3c88b5d06127be5 Mon Sep 17 00:00:00 2001 From: Daylon Wilkins Date: Wed, 7 Feb 2024 04:07:35 -0800 Subject: [PATCH 2/2] PR feedback --- go/vt/sqlparser/ast.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/go/vt/sqlparser/ast.go b/go/vt/sqlparser/ast.go index 5a4888d16d2..660806e997b 100644 --- a/go/vt/sqlparser/ast.go +++ b/go/vt/sqlparser/ast.go @@ -7230,23 +7230,24 @@ func (node *SrsAttribute) Format(buf *TrackedBuffer) { buf.Myprintf("description '%s'", node.Description) } -// DoltgresInjectedExpr allows DoltgreSQL to bypass AST analysis, which is currently specific to MySQL. -type DoltgresInjectedExpr struct { +// InjectedExpr allows bypassing AST analysis. This is used by projects that rely on Vitess, but may not implement +// MySQL's dialect. +type InjectedExpr struct { Expression any } -var _ Expr = DoltgresInjectedExpr{} +var _ Expr = InjectedExpr{} -func (d DoltgresInjectedExpr) iExpr() {} +func (d InjectedExpr) iExpr() {} -func (d DoltgresInjectedExpr) replace(from, to Expr) bool { +func (d InjectedExpr) replace(from, to Expr) bool { return false } -func (d DoltgresInjectedExpr) Format(buf *TrackedBuffer) { +func (d InjectedExpr) Format(buf *TrackedBuffer) { if stringer, ok := d.Expression.(fmt.Stringer); ok { buf.WriteString(stringer.String()) } else { - buf.WriteString("DoltgresInjectedExpr") + buf.WriteString("InjectedExpr") } }