Skip to content

fix(compiler): Improved type inference for concat operator #2740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
cols = append(cols, &Column{Name: name, DataType: "bool", NotNull: true})
case lang.IsMathematicalOperator(astutils.Join(n.Name, "")):
cols = append(cols, &Column{Name: name, DataType: "int", NotNull: true})
case astutils.Join(n.Name, "") == "||":
cols = append(cols, &Column{Name: name, DataType: "text", NotNull: true})
default:
cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
// TODO: Move this to database-specific engine package
dataType := "any"
if astutils.Join(n.Name, ".") == "||" {
dataType = "string"
dataType = "text"
}

defaultP := named.NewParam("")
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/concat_operator/postgresql/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions internal/endtoend/testdata/concat_operator/postgresql/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE foo(bar TEXT, baz TEXT);

-- name: Concat :many
SELECT bar || ' ' || baz FROM foo;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/concat_operator/postgresql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/concat_operator/sqlite/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions internal/endtoend/testdata/concat_operator/sqlite/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions internal/endtoend/testdata/concat_operator/sqlite/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions internal/endtoend/testdata/concat_operator/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE foo(bar TEXT, baz TEXT);

-- name: Concat :many
SELECT bar || ' ' || baz FROM foo;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/concat_operator/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "sqlite",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
2 changes: 1 addition & 1 deletion internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ func (c *cc) convertMathOperationNode(n *parser.Expr_math_opContext) ast.Node {
return &ast.A_Expr{
Name: &ast.List{
Items: []ast.Node{
&ast.String{Str: "+"}, // todo: Convert operation types
&ast.String{Str: n.GetChild(1).(antlr.TerminalNode).GetText()},
},
},
Lexpr: c.convert(n.Expr(0)),
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/sqlite/parser/SQLiteParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ expr:
| NAMED_BIND_PARAMETER #expr_bind
| ((schema_name DOT)? table_name DOT)? column_name #expr_qualified_column_name
| unary_operator expr #expr_unary
| expr PIPE2 expr #expr_binary
| expr PIPE2 expr #expr_math_op
| expr ( STAR | DIV | MOD) expr #expr_math_op
| expr ( PLUS | MINUS) expr #expr_math_op
| expr ( LT2 | GT2 | AMP | PIPE) expr #expr_comparison
Expand Down
10 changes: 5 additions & 5 deletions internal/engine/sqlite/parser/sqlite_parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.