Skip to content

Commit

Permalink
bug: should not fail on unknown collations
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Feb 6, 2024
1 parent c635264 commit 3e5d494
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 42 deletions.
4 changes: 2 additions & 2 deletions go/vt/vtgate/semantics/derived_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ func (dt *DerivedTable) GetVindexTable() *vindexes.Table {
return nil
}

func (dt *DerivedTable) getColumns() ([]ColumnInfo, error) {
func (dt *DerivedTable) getColumns() []ColumnInfo {
cols := make([]ColumnInfo, 0, len(dt.columnNames))
for _, col := range dt.columnNames {
cols = append(cols, ColumnInfo{
Name: col,
})
}
return cols, nil
return cols
}

func (dt *DerivedTable) hasStar() bool {
Expand Down
18 changes: 3 additions & 15 deletions go/vt/vtgate/semantics/early_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,7 @@ func findOnlyOneTableInfoThatHasColumn(b *binder, tbl sqlparser.TableExpr, colum
case *sqlparser.AliasedTableExpr:
ts := b.tc.tableSetFor(tbl)
tblInfo := b.tc.Tables[ts.TableOffset()]
columns, err := tblInfo.getColumns()
if err != nil {
return nil, err
}
for _, info := range columns {
for _, info := range tblInfo.getColumns() {
if column.EqualString(info.Name) {
return []TableInfo{tblInfo}, nil
}
Expand Down Expand Up @@ -839,13 +835,9 @@ func (e *expanderState) processColumnsFor(tbl TableInfo) error {
From: https://dev.mysql.com/doc/refman/8.0/en/join.html
*/

columns, err := tbl.getColumns()
if err != nil {
return err
}
outer:
// in this first loop we just find columns used in any JOIN USING used on this table
for _, col := range columns {
for _, col := range tbl.getColumns() {
if col.Invisible {
continue
}
Expand All @@ -864,11 +856,7 @@ outer:
}

// and this time around we are printing any columns not involved in any JOIN USING
columns, err = tbl.getColumns()
if err != nil {
return err
}
for _, col := range columns {
for _, col := range tbl.getColumns() {
if col.Invisible {
continue
}
Expand Down
22 changes: 7 additions & 15 deletions go/vt/vtgate/semantics/real_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ var _ TableInfo = (*RealTable)(nil)
// dependencies implements the TableInfo interface
func (r *RealTable) dependencies(colName string, org originable) (dependencies, error) {
ts := org.tableSetFor(r.ASTNode)
columns, err := r.getColumns()
if err != nil {
return nil, err
}
for _, info := range columns {
for _, info := range r.getColumns() {
if strings.EqualFold(info.Name, colName) {
return createCertain(ts, ts, info.Type), nil
}
Expand Down Expand Up @@ -72,7 +68,7 @@ func (r *RealTable) IsInfSchema() bool {
}

// GetColumns implements the TableInfo interface
func (r *RealTable) getColumns() ([]ColumnInfo, error) {
func (r *RealTable) getColumns() []ColumnInfo {
return vindexTableToColumnInfo(r.Table, r.collationEnv)
}

Expand Down Expand Up @@ -121,18 +117,14 @@ func (r *RealTable) matches(name sqlparser.TableName) bool {
return (name.Qualifier.IsEmpty() || name.Qualifier.String() == r.dbName) && r.tableName == name.Name.String()
}

func vindexTableToColumnInfo(tbl *vindexes.Table, collationEnv *collations.Environment) ([]ColumnInfo, error) {
func vindexTableToColumnInfo(tbl *vindexes.Table, collationEnv *collations.Environment) []ColumnInfo {
if tbl == nil {
return nil, nil
return nil
}
nameMap := map[string]any{}
cols := make([]ColumnInfo, 0, len(tbl.Columns))
for _, col := range tbl.Columns {
tt, err := col.ToEvalengineType(collationEnv)
if err != nil {
return nil, err
}

tt := col.ToEvalengineType(collationEnv)
cols = append(cols, ColumnInfo{
Name: col.Name.String(),
Type: tt,
Expand All @@ -142,7 +134,7 @@ func vindexTableToColumnInfo(tbl *vindexes.Table, collationEnv *collations.Envir
}
// If table is authoritative, we do not need ColumnVindexes to help in resolving the unqualified columns.
if tbl.ColumnListAuthoritative {
return cols, nil
return cols
}
for _, vindex := range tbl.ColumnVindexes {
for _, column := range vindex.Columns {
Expand All @@ -156,5 +148,5 @@ func vindexTableToColumnInfo(tbl *vindexes.Table, collationEnv *collations.Envir
nameMap[name] = nil
}
}
return cols, nil
return cols
}
2 changes: 1 addition & 1 deletion go/vt/vtgate/semantics/semantic_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type (
canShortCut() shortCut

// getColumns returns the known column information for this table
getColumns() ([]ColumnInfo, error)
getColumns() []ColumnInfo

dependencies(colName string, org originable) (dependencies, error)
getExprFor(s string) (sqlparser.Expr, error)
Expand Down
3 changes: 2 additions & 1 deletion go/vt/vtgate/semantics/typer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestNormalizerAndSemanticAnalysisIntegration(t *testing.T) {
}
}

// Tests that the types correctly picks up and sets the collation on columns
func TestColumnCollations(t *testing.T) {
tests := []struct {
query, collation string
Expand All @@ -75,7 +76,7 @@ func TestColumnCollations(t *testing.T) {

st, err := Analyze(parse, "d", fakeSchemaInfo())
require.NoError(t, err)
col := parse.(*sqlparser.Select).SelectExprs[0].(*sqlparser.AliasedExpr).Expr
col := extract(parse.(*sqlparser.Select), 0)
typ, found := st.TypeForExpr(col)
require.True(t, found, "column was not typed")

Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/semantics/vindex_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (v *VindexTable) canShortCut() shortCut {
}

// GetColumns implements the TableInfo interface
func (v *VindexTable) getColumns() ([]ColumnInfo, error) {
func (v *VindexTable) getColumns() []ColumnInfo {
return v.Table.getColumns()
}

Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/semantics/vtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ func (v *vTableInfo) GetVindexTable() *vindexes.Table {
return nil
}

func (v *vTableInfo) getColumns() ([]ColumnInfo, error) {
func (v *vTableInfo) getColumns() []ColumnInfo {
cols := make([]ColumnInfo, 0, len(v.columnNames))
for _, col := range v.columnNames {
cols = append(cols, ColumnInfo{
Name: col,
})
}
return cols, nil
return cols
}

func (v *vTableInfo) hasStar() bool {
Expand Down
7 changes: 2 additions & 5 deletions go/vt/vtgate/vindexes/vschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,12 @@ func (col *Column) MarshalJSON() ([]byte, error) {
return json.Marshal(cj)
}

func (col *Column) ToEvalengineType(collationEnv *collations.Environment) (evalengine.Type, error) {
func (col *Column) ToEvalengineType(collationEnv *collations.Environment) evalengine.Type {
collation := collations.CollationForType(col.Type, collationEnv.DefaultConnectionCharset())
if sqltypes.IsText(col.Type) {
collation, _ = collationEnv.LookupID(col.CollationName)
if collation == collations.Unknown {
return evalengine.Type{}, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unknown collation name %q", col.CollationName)
}
}
return evalengine.NewTypeEx(col.Type, collation, col.Nullable, col.Size, col.Scale), nil
return evalengine.NewTypeEx(col.Type, collation, col.Nullable, col.Size, col.Scale)
}

// KeyspaceSchema contains the schema(table) for a keyspace.
Expand Down

0 comments on commit 3e5d494

Please sign in to comment.