Skip to content

Commit

Permalink
1.use exists instead of missing.
Browse files Browse the repository at this point in the history
2.support eq empty string.
  • Loading branch information
lemonyxk committed Aug 4, 2023
1 parent 2bc2add commit 49dfb30
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions select_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func buildComparisonExprRightStr(expr sqlparser.Expr) (string, bool, error) {
return "", missingCheck, err
}
case *sqlparser.ColName:
if sqlparser.String(expr) == "missing" {
if strings.ToLower(sqlparser.String(expr)) == "missing" {
missingCheck = true
return "", missingCheck, nil
}
Expand Down Expand Up @@ -251,15 +251,20 @@ func handleSelectWhereComparisonExpr(expr *sqlparser.Expr, topLevel bool, parent
rightStr = unescapeSql(rightStr, escapeStr)
resultStr := ""

// allow eq empty string
if rightStr == `<nil>` {
rightStr = ""
}

switch comparisonExpr.Operator {
case ">=":
resultStr = fmt.Sprintf(`{"range" : {"%v" : {"from" : "%v"}}}`, colNameStr, rightStr)
case "<=":
resultStr = fmt.Sprintf(`{"range" : {"%v" : {"to" : "%v"}}}`, colNameStr, rightStr)
case "=":
// field is missing
if missingCheck {
resultStr = fmt.Sprintf(`{"missing":{"field":"%v"}}`, colNameStr)
if missingCheck { // missing was deprecated in 2.2, use exists instead.
resultStr = fmt.Sprintf(`{"bool" : {"must_not" : [{"exists":{"field":"%v"}}]}}`, colNameStr)
} else {
resultStr = fmt.Sprintf(`{"match_phrase" : {"%v" : {"query" : "%v"}}}`, colNameStr, rightStr)
}
Expand All @@ -268,8 +273,8 @@ func handleSelectWhereComparisonExpr(expr *sqlparser.Expr, topLevel bool, parent
case "<":
resultStr = fmt.Sprintf(`{"range" : {"%v" : {"lt" : "%v"}}}`, colNameStr, rightStr)
case "!=":
if missingCheck {
resultStr = fmt.Sprintf(`{"bool" : {"must_not" : [{"missing":{"field":"%v"}}]}}`, colNameStr)
if missingCheck { // missing was deprecated in 2.2, use exists instead.
resultStr = fmt.Sprintf(`{"bool" : {"must" : [{"exists":{"field":"%v"}}]}}`, colNameStr)
} else {
resultStr = fmt.Sprintf(`{"bool" : {"must_not" : [{"match_phrase" : {"%v" : {"query" : "%v"}}}]}}`, colNameStr, rightStr)
}
Expand Down
4 changes: 2 additions & 2 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ var selectCaseMap = map[string]string{
"select * from abc limit 10": `{"query" : {"bool" : {"must": [{"match_all" : {}}]}},"from" : 0,"size" : 10}`,
"select count(*), id from ark group by id": `{"query" : {"bool" : {"must": [{"match_all" : {}}]}},"from" : 0,"size" : 0,"aggregations" : {"id":{"aggregations":{"COUNT(*)":{"value_count":{"field":"_index"}}},"terms":{"field":"id","size":200}}}}`,
"SELECT COUNT(distinct age) FROM bank GROUP BY range(age, 20,25,30,35,40)": `{"query" : {"bool" : {"must": [{"match_all" : {}}]}},"from" : 0,"size" : 0,"aggregations" : {"range(age,20,25,30,35,40)":{"aggregations":{"COUNT(age)":{"cardinality":{"field":"age"}}},"range":{"field":"age","ranges":[{"from":"20","to":"25"},{"from":"25","to":"30"},{"from":"30","to":"35"},{"from":"35","to":"40"}]}}}}`,
"select * from a where id != missing": `{"query" : {"bool" : {"must" : [{"bool" : {"must_not" : [{"missing":{"field":"id"}}]}}]}},"from" : 0,"size" : 1}`,
"select * from a where id = missing": `{"query" : {"bool" : {"must" : [{"missing":{"field":"id"}}]}},"from" : 0,"size" : 1}`,
"select * from a where id != missing": `{"query" : {"bool" : {"must" : [{"bool" : {"must" : [{"exists":{"field":"id"}}]}}]}},"from" : 0,"size" : 1} `,
"select * from a where id = missing": `{"query" : {"bool" : {"must" : [{"bool" : {"must_not" : [{"exists":{"field":"id"}}]}}]}},"from" : 0,"size" : 1} `,
"select count(*) from a": `{"query" : {"bool" : {"must": [{"match_all" : {}}]}},"from" : 0,"size" : 0,"aggregations" : {"COUNT(*)":{"value_count":{"field":"_index"}}}}`,
"SELECT online FROM online GROUP BY date_range(field='insert_time' , format='yyyy-MM-dd', '2014-08-18','2014-08-17','now-8d','now-7d','now-6d','now')": `{"query" : {"bool" : {"must": [{"match_all" : {}}]}},"from" : 0,"size" : 0,"aggregations" : {"date_range(field=insert_time,format=yyyy-MM-dd,2014-08-18,2014-08-17,now-8d,now-7d,now-6d,now)":{"date_range":{"field":"insert_time","format":"yyyy-MM-dd","ranges":[{"from":"2014-08-18","to":"2014-08-17"},{"from":"2014-08-17","to":"now-8d"},{"from":"now-8d","to":"now-7d"},{"from":"now-7d","to":"now-6d"},{"from":"now-6d","to":"now"}]}}}}`,
"select count(id), sum(age) from a group by id": `{"query" : {"bool" : {"must": [{"match_all" : {}}]}},"from" : 0,"size" : 0,"aggregations" : {"id":{"aggregations":{"COUNT(id)":{"value_count":{"field":"id"}},"SUM(age)":{"sum":{"field":"age"}}},"terms":{"field":"id","size":200}}}}`,
Expand Down

0 comments on commit 49dfb30

Please sign in to comment.