Skip to content

Commit 9cd9139

Browse files
authored
fix(engine/sqlite): support -> and ->> operators (#2927)
* fix(engine/sqlite): support -> and ->> operators close #2919 There are still cases where the type inference part does not work properly, but this can be worked around with explicit CAST. * chore: remove infer
1 parent d9e233e commit 9cd9139

19 files changed

+3789
-3640
lines changed

internal/compiler/output_columns.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
158158
if res.Name != nil {
159159
name = *res.Name
160160
}
161-
switch {
162-
case lang.IsComparisonOperator(astutils.Join(n.Name, "")):
161+
switch op := astutils.Join(n.Name, ""); {
162+
case lang.IsComparisonOperator(op):
163163
// TODO: Generate a name for these operations
164164
cols = append(cols, &Column{Name: name, DataType: "bool", NotNull: true})
165-
case lang.IsMathematicalOperator(astutils.Join(n.Name, "")):
165+
case lang.IsMathematicalOperator(op):
166166
cols = append(cols, &Column{Name: name, DataType: "int", NotNull: true})
167167
default:
168168
cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false})

internal/compiler/resolve.go

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

151151
defaultP := named.NewParam("")

internal/endtoend/testdata/json_param_type/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/json_param_type/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/json_param_type/sqlite/go/query.sql.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: FindByAddress :one
2+
SELECT * FROM "user" WHERE "metadata"->>'address1' = ?1 LIMIT 1;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE "user" (
2+
"id" INT,
3+
"metadata" TEXT
4+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: "2"
2+
sql:
3+
- engine: "sqlite"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"

internal/engine/sqlite/convert.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -764,19 +764,19 @@ func (c *cc) convertLiteral(n *parser.Expr_literalContext) ast.Node {
764764
return todo("convertLiteral", n)
765765
}
766766

767-
func (c *cc) convertMathOperationNode(n *parser.Expr_math_opContext) ast.Node {
767+
func (c *cc) convertBinaryNode(n *parser.Expr_binaryContext) 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()},
772772
},
773773
},
774774
Lexpr: c.convert(n.Expr(0)),
775775
Rexpr: c.convert(n.Expr(1)),
776776
}
777777
}
778778

779-
func (c *cc) convertBinaryNode(n *parser.Expr_binaryContext) ast.Node {
779+
func (c *cc) convertBoolNode(n *parser.Expr_boolContext) ast.Node {
780780
return &ast.BoolExpr{
781781
// TODO: Set op
782782
Args: &ast.List{
@@ -1163,14 +1163,14 @@ func (c *cc) convert(node node) ast.Node {
11631163
case *parser.Expr_literalContext:
11641164
return c.convertLiteral(n)
11651165

1166-
case *parser.Expr_binaryContext:
1167-
return c.convertBinaryNode(n)
1166+
case *parser.Expr_boolContext:
1167+
return c.convertBoolNode(n)
11681168

11691169
case *parser.Expr_listContext:
11701170
return c.convertExprListContext(n)
11711171

1172-
case *parser.Expr_math_opContext:
1173-
return c.convertMathOperationNode(n)
1172+
case *parser.Expr_binaryContext:
1173+
return c.convertBinaryNode(n)
11741174

11751175
case *parser.Expr_in_selectContext:
11761176
return c.convertInSelectNode(n)

internal/engine/sqlite/parser/SQLiteLexer.g4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ COMMA: ',';
3535
ASSIGN: '=';
3636
STAR: '*';
3737
PLUS: '+';
38+
PTR2: '->>';
39+
PTR: '->';
3840
MINUS: '-';
3941
TILDE: '~';
4042
PIPE2: '||';

internal/engine/sqlite/parser/SQLiteLexer.interp

Lines changed: 7 additions & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)