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

Add filter operation #1588

Merged
merged 7 commits into from Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/yqlib/doc/operators/filter.md
@@ -0,0 +1,18 @@

## Filter array
Given a sample.yml file of:
```yaml
- 1
- 2
- 3
```
then
```bash
yq 'filter(. < 3)' sample.yml
```
will output
```yaml
- 1
- 2
```

1 change: 1 addition & 0 deletions pkg/yqlib/lexer_participle.go
Expand Up @@ -37,6 +37,7 @@ var participleYqRules = []*participleYqRule{

{"MapValues", `map_?values`, opToken(mapValuesOpType), 0},
simpleOp("map", mapOpType),
simpleOp("filter", filterOpType),
simpleOp("pick", pickOpType),

{"FlattenWithDepth", `flatten\([0-9]+\)`, flattenWithDepth(), 0},
Expand Down
1 change: 1 addition & 0 deletions pkg/yqlib/lib.go
Expand Up @@ -84,6 +84,7 @@ var expressionOpType = &operationType{Type: "EXP", NumArgs: 0, Precedence: 50, H

var collectOpType = &operationType{Type: "COLLECT", NumArgs: 1, Precedence: 50, Handler: collectOperator}
var mapOpType = &operationType{Type: "MAP", NumArgs: 1, Precedence: 50, Handler: mapOperator}
var filterOpType = &operationType{Type: "FILTER", NumArgs: 1, Precedence: 50, Handler: filterOperator}
var errorOpType = &operationType{Type: "ERROR", NumArgs: 1, Precedence: 50, Handler: errorOperator}
var pickOpType = &operationType{Type: "PICK", NumArgs: 1, Precedence: 50, Handler: pickOperator}
var evalOpType = &operationType{Type: "EVAL", NumArgs: 1, Precedence: 50, Handler: evalOperator}
Expand Down
56 changes: 56 additions & 0 deletions pkg/yqlib/operator_filter.go
@@ -0,0 +1,56 @@
package yqlib

import (
"errors"
"container/list"
)

func filterOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- filterOperation")
var results = list.New()

for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
children := context.SingleChildContext(candidate)
splatted, err := splat(children, traversePreferences{})
rbren marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return Context{}, err
}

if err != nil {
rbren marked this conversation as resolved.
Show resolved Hide resolved
return Context{}, err
}

for resultEl := splatted.MatchingNodes.Front(); resultEl != nil; resultEl = resultEl.Next() {
result := resultEl.Value.(*CandidateNode)
childCtx := context.SingleReadonlyChildContext(result)
include, err := d.GetMatchingNodes(childCtx, expressionNode.RHS)
if err != nil {
return Context{}, err
}
var includeResult bool
var errDecoding error
includeEl := include.MatchingNodes.Front()
if includeEl.Next() != nil {
return Context{}, errors.New("Only expected one child")
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the best way I could find to turn each value into a truthy value to determine if it should be included. LMK if there's a better way

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, yeah there should be a cleaner way. What this function is basically doing is a map(select(X)).

You could create a new select expression node and give the expression.RHS to it.

something like:

    selectNodesOp := &ExpressionNode{
      Operation: selectOp,
      RHS:       expression.RHS,
      }
      
      includedNodes, err := d.GetMatchingNodes(splatted, selectNodesOp.RHS)

Pretty sure this could be extended to create a map(select) expression and just delegate the logic to those functions entirely...

includeVal := includeEl.Value.(*CandidateNode)
includeResult, errDecoding = isTruthy(includeVal)
if errDecoding != nil {
return Context{}, errDecoding
}
log.Debug("isTruthy %v", includeResult)
if includeResult {
selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}}
collected, err := collectTogether(d, childCtx, selfExpression)
if err != nil {
return Context{}, err
}
collected.Node.Style = unwrapDoc(result.Node).Style
results.PushBack(collected)
}
}
}
return context.ChildContext(results), nil
}

49 changes: 49 additions & 0 deletions pkg/yqlib/operator_filter_test.go
@@ -0,0 +1,49 @@
package yqlib

import (
"testing"
)

var filterOperatorScenarios = []expressionScenario{
{
description: "Filter array",
document: `[1,2,3]`,
expression: `filter(. < 3)`,
expected: []string{
"D0, P[], (!!seq)::- 1\n",
"D0, P[], (!!seq)::- 2\n",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't actually the right result - this means the filter function is return two arrays - each with a single element. It should return a single array with two elements

"D0, P[], (!!seq)::- 1\n- 2\n",

I think if you refactor to use map and select under the hood - it would do the right thing.

},
},
{
skipDoc: true,
document: `[1,2,3]`,
expression: `filter(. > 1)`,
expected: []string{
"D0, P[], (!!seq)::- 2\n",
"D0, P[], (!!seq)::- 3\n",
},
},
{
skipDoc: true,
description: "Filter array to empty",
document: `[1,2,3]`,
expression: `filter(. > 4)`,
expected: []string{
},
},
{
skipDoc: true,
description: "Filter empty array",
document: `[]`,
expression: `filter(. > 1)`,
expected: []string{
},
},
}

func TestFilterOperatorScenarios(t *testing.T) {
for _, tt := range filterOperatorScenarios {
testScenario(t, &tt)
}
documentOperatorScenarios(t, "filter", filterOperatorScenarios)
}