Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2802,13 +2802,18 @@ func (node *SubstrExpr) walkSubtree(visit Visit) error {
// ConvertExpr represents a call to CONVERT(expr, type)
// or it's equivalent CAST(expr AS type). Both are rewritten to the former.
type ConvertExpr struct {
IsCast bool
Expr Expr
Type *ConvertType
}

// Format formats the node.
func (node *ConvertExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("convert(%v, %v)", node.Expr, node.Type)
if node.IsCast {
buf.Myprintf("CAST(%v AS %v)", node.Expr, node.Type)
} else {
buf.Myprintf("convert(%v, %v)", node.Expr, node.Type)
}
}

func (node *ConvertExpr) walkSubtree(visit Visit) error {
Expand Down
18 changes: 17 additions & 1 deletion parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,23 @@ func TestConvert(t *testing.T) {
output string
}{{
input: "select cast('abc' as date) from t",
output: "select convert('abc', date) from t",
output: "select CAST('abc' AS date) from t",
},{
input: "select cast('abc' as int) from t",
output: "select CAST('abc' AS int) from t",
},{
input: "select cast('abc' as integer) from t",
output: "select CAST('abc' AS integer) from t",
},{
input: "select cast('abc' as bool) from t",
output: "select CAST('abc' AS bool) from t",
},{
input: "select cast('abc' as boolean) from t",
output: "select CAST('abc' AS boolean) from t",
}, {
input: "select convert('abc', int) from t",
}, {
input: "select convert('abc', integer) from t",
}, {
input: "select convert('abc', binary(4)) from t",
}, {
Expand Down
Loading