Skip to content

Commit 413f546

Browse files
committed
Fix query translator.
1 parent 15542d1 commit 413f546

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

internal/knowledge/query_sql.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ func (sqt *SQLQueryTranslator) Translate(query *query.QueryCypher) (*SQLTranslat
321321
var from []string
322322

323323
filterExpressions := AndOrExpression{And: true}
324+
whereExpressions := AndOrExpression{And: true}
324325
for _, x := range query.QuerySinglePartQuery.QueryMatches {
325326
parser := NewPatternParser(&sqt.QueryGraph)
326327
for _, y := range x.PatternElements {
@@ -330,17 +331,6 @@ func (sqt *SQLQueryTranslator) Translate(query *query.QueryCypher) (*SQLTranslat
330331
}
331332
}
332333

333-
// Build the constraints for the patterns in MATCH clause
334-
expr, f, err := buildSQLConstraintsFromPatterns(&sqt.QueryGraph, constrainedNodes, MatchScope)
335-
if err != nil {
336-
return nil, fmt.Errorf("Unable to build SQL constraints from patterns in the MATCH clause: %v", err)
337-
}
338-
from = f
339-
340-
if expr.Expression != "" || len(expr.Children) > 0 {
341-
filterExpressions.Children = append(filterExpressions.Children, *expr)
342-
}
343-
344334
if x.Where != nil {
345335
whereVisitor := NewQueryWhereVisitor(&sqt.QueryGraph)
346336
whereExpression, err := whereVisitor.ParseExpression(x.Where)
@@ -357,12 +347,27 @@ func (sqt *SQLQueryTranslator) Translate(query *query.QueryCypher) (*SQLTranslat
357347

358348
// We only append the expression if it's not empty
359349
if whereExpression != "" {
360-
filterExpressions.Children = append(filterExpressions.Children,
361-
AndOrExpression{Expression: whereExpression})
350+
whereExpressions.Children = append(whereExpressions.Children,
351+
AndOrExpression{And: true, Expression: whereExpression})
362352
}
363353
}
364354
}
365355

356+
// Build the constraints for the patterns in MATCH clause
357+
expr, f, err := buildSQLConstraintsFromPatterns(&sqt.QueryGraph, constrainedNodes, MatchScope)
358+
if err != nil {
359+
return nil, fmt.Errorf("Unable to build SQL constraints from patterns in the MATCH clause: %v", err)
360+
}
361+
from = f
362+
363+
if expr.Expression != "" || len(expr.Children) > 0 {
364+
filterExpressions.Children = append(filterExpressions.Children, *expr)
365+
}
366+
367+
if whereExpressions.Expression != "" || len(whereExpressions.Children) > 0 {
368+
filterExpressions.Children = append(filterExpressions.Children, whereExpressions)
369+
}
370+
366371
projections := make([]string, 0)
367372
projectionTypes := make([]Projection, 0)
368373

internal/knowledge/query_sql_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ RETURN ip`,
243243
SELECT a0.id, a0.value, a0.type FROM assets a0, assets a1, relations r0
244244
WHERE ((((a0.type = 'ip' AND a1.type = 'device') AND r0.type = 'observed') AND (r0.from_id = a1.id AND r0.to_id = a0.id)) AND NOT EXISTS (SELECT 1 FROM assets aw0, assets aw2, relations rw1
245245
WHERE (aw0.id = a0.id AND aw2.type = 'task' AND rw1.type = 'scanned' AND (rw1.from_id = aw2.id AND rw1.to_id = aw0.id))))`,
246-
Selected: true,
247246
},
248247
{
249248
Description: "Combine a pattern and a simple comparison expression in the WHERE clause",
@@ -255,7 +254,6 @@ RETURN ip`,
255254
SELECT a0.id, a0.value, a0.type FROM assets a0, assets a1, relations r0
256255
WHERE ((((a0.type = 'ip' AND a1.type = 'device') AND r0.type = 'observed') AND (r0.from_id = a1.id AND r0.to_id = a0.id)) AND EXISTS (SELECT 1 FROM assets aw0, assets aw2, relations rw1
257256
WHERE (aw0.id = a0.id AND aw2.type = 'task' AND rw1.type = 'scanned' AND (rw1.from_id = aw2.id AND rw1.to_id = aw0.id))) AND a0.value = '127.0.0.1')`,
258-
Selected: true,
259257
},
260258
}
261259

internal/query/query_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
package query
22

3-
import "testing"
3+
import (
4+
"testing"
45

5-
import "github.com/stretchr/testify/require"
6+
"github.com/stretchr/testify/require"
7+
)
68

79
type TestCase struct {
810
Query string
911
Error string
1012
}
1113

1214
var testCases = []TestCase{
13-
TestCase{
15+
{
1416
Query: "MATCH",
1517
Error: "Parsing errors detected: line 1:5 - no viable alternative at input 'MATCH'",
1618
},
17-
TestCase{
19+
{
1820
Query: "MATCH (n) RETURN ",
1921
Error: "Parsing errors detected: line 1:17 - no viable alternative at input ' '",
2022
},
21-
TestCase{
23+
{
2224
Query: "MATCH (n) RETURN c, n,",
2325
Error: "Parsing errors detected: line 1:22 - mismatched input '<EOF>' expecting {'(', '[', '+', '-', '{', '$', ALL, NOT, NULL, COUNT, ANY, NONE, SINGLE, TRUE, FALSE, EXISTS, CASE, StringLiteral, HexInteger, DecimalInteger, OctalInteger, HexLetter, ExponentDecimalReal, RegularDecimalReal, FILTER, EXTRACT, UnescapedSymbolicName, EscapedSymbolicName, SP}",
2426
},
25-
TestCase{
27+
{
2628
Query: "MATCH (n)-[r]-> RETURN c",
2729
Error: "Parsing errors detected: line 1:16 - no viable alternative at input 'MATCH (n)-[r]-> RETURN'",
2830
},
29-
TestCase{
31+
{
3032
Query: "MATCH (n)->[r]-(n) RETURN c",
3133
Error: "Parsing errors detected: line 1:10 - no viable alternative at input 'MATCH (n)->'",
3234
},

0 commit comments

Comments
 (0)