github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/structs/expressionstructs_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  // TODO: uncomment after zero-copy complex relation
    20  // func Test_evaluateExpression(t *testing.T) {
    21  
    22  // 	literalEnc, err := CreateDtypeEnclosure(0.6)
    23  // 	if err != nil {
    24  // 		t.Fatal(err)
    25  // 	}
    26  // 	simpleExpression := &Expression{
    27  // 		LeftInput:    &ExpressionInput{ColumnValue: literalEnc},
    28  // 		ExpressionOp: Add,
    29  // 		RightInput:   &ExpressionInput{ColumnName: "col1"},
    30  // 	}
    31  
    32  // 	valueMap := make([]*RecordEntry, 0)
    33  
    34  // 	entry1 := &RecordEntry{
    35  // 		ColumnName:      []byte("col1"),
    36  // 		ColumnValueType: VALTYPE_ENC_FLOAT64[0],
    37  // 		ColumnValue:     utils.Float64ToBytesLittleEndian(float64(0.5)),
    38  // 	}
    39  // 	valueMap = append(valueMap, entry1)
    40  
    41  // 	retVal, valid := simpleExpression.Evaluate(valueMap)
    42  // 	assert.Equal(t, retVal.(float64), float64(1.1), "test substitution of map values for similar types")
    43  // 	assert.Nil(t, valid)
    44  
    45  // 	valueMap[0].ColumnValue = []byte("invalid")
    46  // 	valueMap[0].ColumnValueType = VALTYPE_ENC_SMALL_STRING[0]
    47  // 	_, inValid := simpleExpression.Evaluate(valueMap) // will fail as string addition is not supported
    48  // 	assert.NotNil(t, inValid, "test invalid operation for converted datatype")
    49  
    50  // 	valueMap[0].ColumnValue = utils.Uint64ToBytesLittleEndian(10)
    51  // 	valueMap[0].ColumnValueType = VALTYPE_ENC_UINT64[0]
    52  // 	res, inValid := simpleExpression.Evaluate(valueMap) // will fail as 0.6 can't get converted to uint
    53  // 	assert.Nil(t, inValid)
    54  // 	assert.Equal(t, 10.6, res)
    55  
    56  // 	missingColumn := &Expression{
    57  // 		LeftInput:    &ExpressionInput{ColumnValue: literalEnc},
    58  // 		ExpressionOp: Add,
    59  // 		RightInput:   &ExpressionInput{ColumnName: "col2"},
    60  // 	}
    61  // 	_, missing := missingColumn.Evaluate(valueMap)
    62  // 	assert.NotNil(t, missing, "cannot evaluate expression if column does not exist in map")
    63  // }