Skip to content

Commit e7d6828

Browse files
committed
refactor(cast): remove dead code and duplicate unknown check
Remove inline cast logic from ExplicitCast.Eval since it's now being handled by getRecordCast() in cast.go, and called from GetExplicitCast before returning nil. Also, remove duplicate UnknownLiteralCast fallback in GetImplicitCast and unused core import from explicit_cast.go. Last, clean up test name; don't include GH issue number. Refs: #1648
1 parent af77007 commit e7d6828

3 files changed

Lines changed: 6 additions & 53 deletions

File tree

server/expression/explicit_cast.go

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/dolthub/go-mysql-server/sql/expression"
2424
vitess "github.com/dolthub/vitess/go/vt/sqlparser"
2525

26-
"github.com/dolthub/doltgresql/core"
2726
"github.com/dolthub/doltgresql/server/functions/framework"
2827
pgtypes "github.com/dolthub/doltgresql/server/types"
2928
)
@@ -97,53 +96,11 @@ func (c *ExplicitCast) Eval(ctx *sql.Context, row sql.Row) (any, error) {
9796
baseCastToType := checkForDomainType(c.castToType)
9897
castFunction := framework.GetExplicitCast(fromType, baseCastToType)
9998
if castFunction == nil {
100-
if fromType.IsRecordType() && c.castToType.IsCompositeType() { // TODO: should this only be in explicit, or assignment and implicit too?
101-
// Casting to a record type will always work for any composite type.
102-
// TODO: is the above statement true for all cases?
103-
// When casting to a composite type, then we must match the arity and have valid casts for every position.
104-
if c.castToType.IsRecordType() {
105-
castFunction = framework.IdentityCast
106-
} else {
107-
castFunction = func(ctx *sql.Context, val any, targetType *pgtypes.DoltgresType) (any, error) {
108-
vals, ok := val.([]pgtypes.RecordValue)
109-
if !ok {
110-
// TODO: better error message
111-
return nil, errors.New("casting input error from record type")
112-
}
113-
if len(targetType.CompositeAttrs) != len(vals) {
114-
return nil, errors.Newf("cannot cast type %s to %s", "", targetType.Name())
115-
}
116-
typeCollection, err := core.GetTypesCollectionFromContext(ctx)
117-
if err != nil {
118-
return nil, err
119-
}
120-
outputVals := make([]pgtypes.RecordValue, len(vals))
121-
for i := range vals {
122-
valType, ok := vals[i].Type.(*pgtypes.DoltgresType)
123-
if !ok {
124-
// TODO: if this is a GMS type, then we should cast to a Doltgres type here
125-
return nil, errors.New("cannot cast record containing GMS type")
126-
}
127-
outputVals[i].Type, err = typeCollection.GetType(ctx, targetType.CompositeAttrs[i].TypeID)
128-
if err != nil {
129-
return nil, err
130-
}
131-
innerExplicit := ExplicitCast{
132-
sqlChild: NewUnsafeLiteral(vals[i].Value, valType),
133-
castToType: outputVals[i].Type.(*pgtypes.DoltgresType),
134-
}
135-
outputVals[i].Value, err = innerExplicit.Eval(ctx, nil)
136-
if err != nil {
137-
return nil, err
138-
}
139-
}
140-
return outputVals, nil
141-
}
142-
}
143-
} else {
144-
return nil, errors.Errorf("EXPLICIT CAST: cast from `%s` to `%s` does not exist: %s",
145-
fromType.String(), c.castToType.String(), c.sqlChild.String())
146-
}
99+
return nil, errors.Errorf(
100+
"EXPLICIT CAST: cast from `%s` to `%s` does not exist: %s",
101+
fromType.String(), c.castToType.String(), c.sqlChild.String(),
102+
)
103+
147104
}
148105
castResult, err := castFunction(ctx, val, c.castToType)
149106
if err != nil {

server/functions/framework/cast.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,6 @@ func GetImplicitCast(fromType *pgtypes.DoltgresType, toType *pgtypes.DoltgresTyp
224224
if fromType.ID == pgtypes.Unknown.ID {
225225
return UnknownLiteralCast
226226
}
227-
// It is always valid to convert from the `unknown` type
228-
if fromType.ID == pgtypes.Unknown.ID {
229-
return UnknownLiteralCast
230-
}
231227
return nil
232228
}
233229

testing/go/values_statement_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var ValuesStatementTests = []ScriptTest{
5454
},
5555
},
5656
{
57-
Name: "VALUES with mixed int and decimal - issue 1648",
57+
Name: "VALUES with mixed int and decimal",
5858
SetUpScript: []string{},
5959
Assertions: []ScriptTestAssertion{
6060
{

0 commit comments

Comments
 (0)