mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-12 16:58:59 +08:00
fix: parser updated to differentiate between params and query string (#1763)
This commit is contained in:
parent
2771d2e774
commit
d06d41af87
@ -309,9 +309,16 @@ func GenerateSQLWhere(allFields *model.GetFieldsResponse, params *model.LogsFilt
|
|||||||
filterTokens = append(filterTokens, filter)
|
filterTokens = append(filterTokens, filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(filterTokens) > 0 {
|
lenFilterTokens := len(filterTokens)
|
||||||
if len(tokens) > 0 {
|
if lenFilterTokens > 0 {
|
||||||
tokens[0] = fmt.Sprintf("and %s", tokens[0])
|
// add parenthesis
|
||||||
|
filterTokens[0] = fmt.Sprintf("( %s", filterTokens[0])
|
||||||
|
filterTokens[lenFilterTokens-1] = fmt.Sprintf("%s) ", filterTokens[lenFilterTokens-1])
|
||||||
|
|
||||||
|
lenTokens := len(tokens)
|
||||||
|
if lenTokens > 0 {
|
||||||
|
tokens[0] = fmt.Sprintf("and ( %s", tokens[0])
|
||||||
|
tokens[lenTokens-1] = fmt.Sprintf("%s) ", tokens[lenTokens-1])
|
||||||
}
|
}
|
||||||
filterTokens = append(filterTokens, tokens...)
|
filterTokens = append(filterTokens, tokens...)
|
||||||
tokens = filterTokens
|
tokens = filterTokens
|
||||||
|
@ -271,32 +271,65 @@ func TestCheckIfPrevousPaginateAndModifyOrder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGenerateSQLQuery(t *testing.T) {
|
var generateSQLQueryFields = model.GetFieldsResponse{
|
||||||
allFields := model.GetFieldsResponse{
|
Selected: []model.LogField{
|
||||||
Selected: []model.LogField{
|
{
|
||||||
{
|
Name: "field1",
|
||||||
Name: "id",
|
DataType: "int64",
|
||||||
DataType: "int64",
|
Type: "attributes",
|
||||||
Type: "attributes",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Interesting: []model.LogField{
|
{
|
||||||
{
|
Name: "field2",
|
||||||
Name: "code",
|
DataType: "double64",
|
||||||
DataType: "int64",
|
Type: "attributes",
|
||||||
Type: "attributes",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
{
|
||||||
|
Name: "field2",
|
||||||
query := "id lt 100 and id gt 50 and code lte 500 and code gte 400"
|
DataType: "string",
|
||||||
tsStart := uint64(1657689292000)
|
Type: "attributes",
|
||||||
tsEnd := uint64(1657689294000)
|
},
|
||||||
idStart := "2BsKLKv8cZrLCn6rkOcRGkdjBdM"
|
},
|
||||||
idEnd := "2BsKG6tRpFWjYMcWsAGKfSxoQdU"
|
Interesting: []model.LogField{
|
||||||
sqlWhere := "timestamp >= '1657689292000' and timestamp <= '1657689294000' and id > '2BsKLKv8cZrLCn6rkOcRGkdjBdM' and id < '2BsKG6tRpFWjYMcWsAGKfSxoQdU' and id < 100 and id > 50 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] <= 500 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] >= 400 "
|
{
|
||||||
Convey("testGenerateSQL", t, func() {
|
Name: "code",
|
||||||
res, _ := GenerateSQLWhere(&allFields, &model.LogsFilterParams{Query: query, TimestampStart: tsStart, TimestampEnd: tsEnd, IdGt: idStart, IdLT: idEnd})
|
DataType: "int64",
|
||||||
So(res, ShouldEqual, sqlWhere)
|
Type: "attributes",
|
||||||
})
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var generateSQLQueryTestCases = []struct {
|
||||||
|
Name string
|
||||||
|
Filter model.LogsFilterParams
|
||||||
|
SqlFilter string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Name: "first query with more than 1 compulsory filters",
|
||||||
|
Filter: model.LogsFilterParams{
|
||||||
|
Query: "field1 lt 100 and field1 gt 50 and code lte 500 and code gte 400",
|
||||||
|
TimestampStart: uint64(1657689292000),
|
||||||
|
TimestampEnd: uint64(1657689294000),
|
||||||
|
IdGt: "2BsKLKv8cZrLCn6rkOcRGkdjBdM",
|
||||||
|
IdLT: "2BsKG6tRpFWjYMcWsAGKfSxoQdU",
|
||||||
|
},
|
||||||
|
SqlFilter: "( timestamp >= '1657689292000' and timestamp <= '1657689294000' and id > '2BsKLKv8cZrLCn6rkOcRGkdjBdM' and id < '2BsKG6tRpFWjYMcWsAGKfSxoQdU' ) and ( field1 < 100 and field1 > 50 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] <= 500 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] >= 400 ) ",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "second query with only timestamp range",
|
||||||
|
Filter: model.LogsFilterParams{
|
||||||
|
Query: "field1 lt 100 and field1 gt 50 and code lte 500 and code gte 400",
|
||||||
|
TimestampStart: uint64(1657689292000),
|
||||||
|
TimestampEnd: uint64(1657689294000),
|
||||||
|
},
|
||||||
|
SqlFilter: "( timestamp >= '1657689292000' and timestamp <= '1657689294000' ) and ( field1 < 100 and field1 > 50 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] <= 500 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] >= 400 ) ",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenerateSQLQuery(t *testing.T) {
|
||||||
|
for _, test := range generateSQLQueryTestCases {
|
||||||
|
Convey("testGenerateSQL", t, func() {
|
||||||
|
res, _ := GenerateSQLWhere(&generateSQLQueryFields, &test.Filter)
|
||||||
|
So(res, ShouldEqual, test.SqlFilter)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user