Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: cch123/elasticsql
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: ruang-guru/elasticsql
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 3 commits
  • 3 files changed
  • 2 contributors

Commits on Jul 4, 2022

  1. is null DSL

    agungwk committed Jul 4, 2022
    Copy the full SHA
    af6e702 View commit details

Commits on Jul 11, 2022

  1. Merge pull request #1 from ruang-guru/add-features-is-clause

    Add is null DSL
    agungwk authored Jul 11, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8616b75 View commit details
  2. Update go.mod (#2)

    agungwk authored Jul 11, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    3a5b9c3 View commit details
Showing with 36 additions and 2 deletions.
  1. +1 −1 go.mod
  2. +33 −1 select_handler.go
  3. +2 −0 select_test.go
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/cch123/elasticsql
module github.com/ruang-guru/elasticsql

go 1.13

34 changes: 33 additions & 1 deletion select_handler.go
Original file line number Diff line number Diff line change
@@ -303,6 +303,38 @@ func handleSelectWhereComparisonExpr(expr *sqlparser.Expr, topLevel bool, parent
return resultStr, nil
}

func handleIsExpr(expr *sqlparser.Expr, topLevel bool, parent *sqlparser.Expr) (string, error) {
isExpr := (*expr).(*sqlparser.IsExpr)
colName, ok := isExpr.Expr.(*sqlparser.ColName)

if !ok {
return "", errors.New("elasticsql: invalid is expression, the left must be a column name")
}

colNameStr := sqlparser.String(colName)
colNameStr = strings.Replace(colNameStr, "`", "", -1)

escapeStr := sqlparser.String(isExpr.Expr)
escapeStr = strings.Trim(escapeStr, "'") // remove quote both sides
resultStr := ""

switch isExpr.Operator {
case "is null":
resultStr = fmt.Sprintf(`{"bool" : {"must_not" : [{"exists" : {"field" : "%v"}}]}}`, colNameStr)
case "is not null":
resultStr = fmt.Sprintf(`{"bool": {"must" : [{"exists" : {"field" : "%v"}}]}}`, colNameStr)
default:
return "", errors.New("elasticsql: invalid is expression, the operator is not supported")
}

// the root node need to have bool and must
if topLevel {
resultStr = fmt.Sprintf(`{"bool" : {"must" : [%v]}}`, resultStr)
}

return resultStr, nil
}

func handleSelectWhere(expr *sqlparser.Expr, topLevel bool, parent *sqlparser.Expr) (string, error) {
if expr == nil {
return "", errors.New("elasticsql: error expression cannot be nil here")
@@ -318,7 +350,7 @@ func handleSelectWhere(expr *sqlparser.Expr, topLevel bool, parent *sqlparser.Ex
return handleSelectWhereComparisonExpr(expr, topLevel, parent)

case *sqlparser.IsExpr:
return "", errors.New("elasticsql: is expression currently not supported")
return handleIsExpr(expr, topLevel, parent)
case *sqlparser.RangeCond:
// between a and b
// the meaning is equal to range query
2 changes: 2 additions & 0 deletions select_test.go
Original file line number Diff line number Diff line change
@@ -77,6 +77,8 @@ var selectCaseMap = map[string]string{
"select * from aaa where a= 1 and multi_match(query='this is a test', fields=(title,title.origin), type=phrase)": `{"query" : {"bool" : {"must" : [{"match_phrase" : {"a" : {"query" : "1"}}},{"multi_match" : {"query" : "this is a test", "type" : "phrase", "fields" : ["title","title.origin"]}}]}},"from" : 0,"size" : 1}`,
"SELECT `id`, `name` FROM `student` WHERE (`name` LIKE '%aaa!_bbb!!ccc%' ESCAPE '!') ORDER BY `updateTime` DESC LIMIT 10": `{"query" : {"bool" : {"must" : [{"match_phrase" : {"name" : {"query" : "aaa_bbb!ccc"}}}]}},"from" : 0,"size" : 10,"sort" : [{"updateTime": "desc"}]}`,
"SELECT `id`, `name` FROM `student` WHERE (`name` LIKE '%aaa!_bbb!!ccc%') ORDER BY `updateTime` DESC LIMIT 10": `{"query" : {"bool" : {"must" : [{"match_phrase" : {"name" : {"query" : "aaa!_bbb!!ccc"}}}]}},"from" : 0,"size" : 10,"sort" : [{"updateTime": "desc"}]}`,
"SELECT `id`, `name` FROM `student` WHERE `name` is not null": `{"query" : {"bool" : {"must" : [{"bool": {"must" : [{"exists" : {"field" : "name"}}]}}]}},"from" : 0,"size" : 1}`,
"SELECT `id`, `name` FROM `student` WHERE `name` is null": `{"query" : {"bool" : {"must" : [{"bool": {"must_not" : [{"exists" : {"field" : "name"}}]}}]}},"from" : 0,"size" : 1}`,
}

func TestSupported(t *testing.T) {