Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use exists instead of missing and support eq empty string. #40

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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