github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/structs/searchnodestructs_test.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package structs
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/siglens/siglens/pkg/segment/utils"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func Test_extractSearchNodeType(t *testing.T) {
    27  
    28  	batch0, err := utils.CreateDtypeEnclosure("batch-0", 0)
    29  	assert.Nil(t, err)
    30  	batchOneAllCols := &SearchExpression{
    31  		LeftSearchInput:  &SearchExpressionInput{ColumnName: "*"},
    32  		FilterOp:         utils.Equals,
    33  		RightSearchInput: &SearchExpressionInput{ColumnValue: batch0},
    34  	}
    35  
    36  	query := &SearchQuery{
    37  		ExpressionFilter: &SearchExpression{
    38  			LeftSearchInput:  &SearchExpressionInput{ColumnName: "col3"},
    39  			FilterOp:         utils.Equals,
    40  			RightSearchInput: &SearchExpressionInput{ColumnValue: batch0},
    41  		},
    42  		SearchType: SimpleExpression,
    43  	}
    44  
    45  	node := &SearchNode{
    46  		AndSearchConditions: &SearchCondition{
    47  			SearchQueries: []*SearchQuery{query},
    48  		},
    49  	}
    50  	node.AddQueryInfoForNode()
    51  	assert.Equal(t, node.NodeType, ColumnValueQuery)
    52  
    53  	node = &SearchNode{
    54  		AndSearchConditions: &SearchCondition{
    55  			SearchQueries: []*SearchQuery{{
    56  				ExpressionFilter: batchOneAllCols,
    57  				SearchType:       SimpleExpression,
    58  			}},
    59  		},
    60  	}
    61  	node.AddQueryInfoForNode()
    62  	assert.Equal(t, node.NodeType, ColumnValueQuery)
    63  
    64  	wildcard, err := utils.CreateDtypeEnclosure("*", 0)
    65  	assert.Nil(t, err)
    66  	node = &SearchNode{
    67  		AndSearchConditions: &SearchCondition{
    68  			SearchQueries: []*SearchQuery{{
    69  				ExpressionFilter: &SearchExpression{
    70  					LeftSearchInput:  &SearchExpressionInput{ColumnName: "*"},
    71  					FilterOp:         utils.Equals,
    72  					RightSearchInput: &SearchExpressionInput{ColumnValue: wildcard},
    73  				},
    74  				SearchType: SimpleExpression,
    75  			}},
    76  		},
    77  	}
    78  	node.AddQueryInfoForNode()
    79  	assert.Equal(t, node.NodeType, MatchAllQuery)
    80  
    81  	assert.Nil(t, err)
    82  	mf := &MatchFilter{
    83  		MatchColumn:   "*",
    84  		MatchWords:    [][]byte{[]byte("*")},
    85  		MatchOperator: utils.And,
    86  	}
    87  
    88  	node = &SearchNode{
    89  		AndSearchConditions: &SearchCondition{
    90  			SearchQueries: []*SearchQuery{{
    91  				MatchFilter: mf,
    92  				SearchType:  SimpleExpression,
    93  			}},
    94  		},
    95  	}
    96  	node.AddQueryInfoForNode()
    97  	assert.Equal(t, node.NodeType, MatchAllQuery)
    98  
    99  	assert.Nil(t, err)
   100  	mf = &MatchFilter{
   101  		MatchColumn:   "*",
   102  		MatchWords:    [][]byte{[]byte("*"), []byte("abc")},
   103  		MatchOperator: utils.And,
   104  	}
   105  
   106  	node = &SearchNode{
   107  		AndSearchConditions: &SearchCondition{
   108  			SearchQueries: []*SearchQuery{{
   109  				MatchFilter: mf,
   110  				SearchType:  SimpleExpression,
   111  			}},
   112  		},
   113  	}
   114  	node.AddQueryInfoForNode()
   115  	assert.Equal(t, node.NodeType, ColumnValueQuery, "no longer match all")
   116  
   117  	wordMatch := &MatchFilter{
   118  		MatchColumn:   "*",
   119  		MatchWords:    [][]byte{[]byte("def"), []byte("abc")},
   120  		MatchOperator: utils.And,
   121  	}
   122  
   123  	node = &SearchNode{
   124  		AndSearchConditions: &SearchCondition{
   125  			SearchQueries: []*SearchQuery{{
   126  				MatchFilter: wordMatch,
   127  				SearchType:  SimpleExpression,
   128  			}},
   129  		},
   130  	}
   131  	node.AddQueryInfoForNode()
   132  	assert.Equal(t, node.NodeType, ColumnValueQuery, "no longer match all")
   133  
   134  	mf = &MatchFilter{
   135  		MatchColumn:   "*",
   136  		MatchWords:    [][]byte{[]byte("*"), []byte("abc")},
   137  		MatchOperator: utils.Or,
   138  	}
   139  
   140  	node = &SearchNode{
   141  		AndSearchConditions: &SearchCondition{
   142  			SearchQueries: []*SearchQuery{{
   143  				MatchFilter: mf,
   144  				SearchType:  SimpleExpression,
   145  			}},
   146  		},
   147  	}
   148  	node.AddQueryInfoForNode()
   149  	assert.Equal(t, node.NodeType, MatchAllQuery, "no longer match all")
   150  
   151  	nestNode := &SearchNode{
   152  		AndSearchConditions: &SearchCondition{
   153  			SearchQueries: []*SearchQuery{
   154  				{
   155  					MatchFilter: mf,
   156  					SearchType:  SimpleExpression,
   157  				},
   158  				{
   159  					ExpressionFilter: batchOneAllCols,
   160  					SearchType:       SimpleExpression,
   161  				},
   162  			},
   163  		},
   164  	}
   165  
   166  	nestNode.AddQueryInfoForNode()
   167  	assert.Equal(t, nestNode.NodeType, ColumnValueQuery, "has a nested non match all")
   168  
   169  	matchAllExp := &SearchExpression{
   170  		LeftSearchInput:  &SearchExpressionInput{ColumnName: "*"},
   171  		FilterOp:         utils.Equals,
   172  		RightSearchInput: &SearchExpressionInput{ColumnValue: wildcard},
   173  	}
   174  	parentNode := &SearchNode{
   175  		AndSearchConditions: &SearchCondition{
   176  			SearchNode: []*SearchNode{nestNode},
   177  			SearchQueries: []*SearchQuery{
   178  				{
   179  					ExpressionFilter: matchAllExp,
   180  					SearchType:       SimpleExpression,
   181  				},
   182  			},
   183  		},
   184  	}
   185  	parentNode.AddQueryInfoForNode()
   186  	assert.Equal(t, parentNode.NodeType, ColumnValueQuery, "has a nested non match all")
   187  }