Skip to content

Commit 8f01b76

Browse files
committed
fix(compiler): Improved type inference for concat operator
close #2734 Fixed sqlite engine treating "||" as OR. MySQL does not have a concat operator, so there is no need to support it.
1 parent b3c3474 commit 8f01b76

File tree

15 files changed

+206
-8
lines changed

15 files changed

+206
-8
lines changed

internal/compiler/output_columns.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
163163
cols = append(cols, &Column{Name: name, DataType: "bool", NotNull: true})
164164
case lang.IsMathematicalOperator(astutils.Join(n.Name, "")):
165165
cols = append(cols, &Column{Name: name, DataType: "int", NotNull: true})
166+
case astutils.Join(n.Name, "") == "||":
167+
cols = append(cols, &Column{Name: name, DataType: "text", NotNull: true})
166168
default:
167169
cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false})
168170
}

internal/compiler/resolve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
141141
// TODO: Move this to database-specific engine package
142142
dataType := "any"
143143
if astutils.Join(n.Name, ".") == "||" {
144-
dataType = "string"
144+
dataType = "text"
145145
}
146146

147147
defaultP := named.NewParam("")

internal/endtoend/testdata/concat_operator/postgresql/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/concat_operator/postgresql/go/models.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/concat_operator/postgresql/go/query.sql.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE foo(bar TEXT, baz TEXT);
2+
3+
-- name: Concat :many
4+
SELECT bar || ' ' || baz FROM foo;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/endtoend/testdata/concat_operator/sqlite/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/concat_operator/sqlite/go/models.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/concat_operator/sqlite/go/query.sql.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE foo(bar TEXT, baz TEXT);
2+
3+
-- name: Concat :many
4+
SELECT bar || ' ' || baz FROM foo;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "sqlite",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/engine/sqlite/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ func (c *cc) convertMathOperationNode(n *parser.Expr_math_opContext) ast.Node {
768768
return &ast.A_Expr{
769769
Name: &ast.List{
770770
Items: []ast.Node{
771-
&ast.String{Str: "+"}, // todo: Convert operation types
771+
&ast.String{Str: n.GetChild(1).(antlr.TerminalNode).GetText()}, // todo: Convert operation types
772772
},
773773
},
774774
Lexpr: c.convert(n.Expr(0)),

internal/engine/sqlite/parser/SQLiteParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ expr:
281281
| NAMED_BIND_PARAMETER #expr_bind
282282
| ((schema_name DOT)? table_name DOT)? column_name #expr_qualified_column_name
283283
| unary_operator expr #expr_unary
284-
| expr PIPE2 expr #expr_binary
284+
| expr PIPE2 expr #expr_math_op
285285
| expr ( STAR | DIV | MOD) expr #expr_math_op
286286
| expr ( PLUS | MINUS) expr #expr_math_op
287287
| expr ( LT2 | GT2 | AMP | PIPE) expr #expr_comparison

internal/engine/sqlite/parser/sqlite_parser.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)