influxdb/predicate/logical.go

61 lines
1.4 KiB
Go
Raw Normal View History

2019-09-23 16:00:03 +00:00
package predicate
import (
"fmt"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
"github.com/influxdata/influxdb/v2/storage/reads/datatypes"
2019-09-23 16:00:03 +00:00
)
// LogicalOperator is a string type of logical operator.
2019-10-14 21:33:50 +00:00
type LogicalOperator int
2019-09-23 16:00:03 +00:00
// LogicalOperators
var (
2019-10-14 21:33:50 +00:00
LogicalAnd LogicalOperator = 1
2019-09-23 16:00:03 +00:00
)
// Value returns the node logical type.
func (op LogicalOperator) Value() (datatypes.Node_Logical, error) {
switch op {
case LogicalAnd:
return datatypes.Node_LogicalAnd, nil
2019-09-23 16:00:03 +00:00
default:
return 0, &errors.Error{
Code: errors.EInvalid,
2019-09-23 16:00:03 +00:00
Msg: fmt.Sprintf("the logical operator %q is invalid", op),
}
}
}
// LogicalNode is a node type includes a logical expression with other nodes.
type LogicalNode struct {
Operator LogicalOperator `json:"operator"`
Children [2]Node `json:"children"`
2019-09-23 16:00:03 +00:00
}
// ToDataType convert a LogicalNode to datatypes.Node.
func (n LogicalNode) ToDataType() (*datatypes.Node, error) {
logicalOp, err := n.Operator.Value()
if err != nil {
return nil, err
}
children := make([]*datatypes.Node, len(n.Children))
for k, node := range n.Children {
children[k], err = node.ToDataType()
if err != nil {
return nil, &errors.Error{
Code: errors.EInvalid,
2019-09-23 16:00:03 +00:00
Msg: fmt.Sprintf("Err in Child %d, err: %s", k, err.Error()),
}
}
}
return &datatypes.Node{
NodeType: datatypes.Node_TypeLogicalExpression,
2019-09-23 16:00:03 +00:00
Value: &datatypes.Node_Logical_{
Logical: logicalOp,
},
Children: children,
}, nil
}