From 8979f7f424fa57811408ea82eeafd18e4ca86f9f Mon Sep 17 00:00:00 2001 From: Asdine El Hrychy Date: Thu, 25 Jun 2020 21:45:42 +0400 Subject: [PATCH] Delete IsEqual method from Node type --- sql/planner/deletion.go | 9 ----- sql/planner/input.go | 18 --------- sql/planner/optimizer_test.go | 8 ++-- sql/planner/projection.go | 41 ------------------- sql/planner/replacement.go | 9 ----- sql/planner/sort.go | 9 ----- sql/planner/tree.go | 75 ----------------------------------- sql/planner/tree_test.go | 27 ------------- 8 files changed, 4 insertions(+), 192 deletions(-) delete mode 100644 sql/planner/tree_test.go diff --git a/sql/planner/deletion.go b/sql/planner/deletion.go index 0335bef9c..ecca9494f 100644 --- a/sql/planner/deletion.go +++ b/sql/planner/deletion.go @@ -34,15 +34,6 @@ func NewDeletionNode(n Node, tableName string) Node { } } -func (n *deletionNode) IsEqual(other Node) bool { - if !n.node.IsEqual(other) { - return false - } - - on := other.(*deletionNode) - return n.tableName == on.tableName -} - func (n *deletionNode) Bind(tx *database.Transaction, params []expr.Param) (err error) { n.table, err = tx.GetTable(n.tableName) return diff --git a/sql/planner/input.go b/sql/planner/input.go index 971355df1..3e002f187 100644 --- a/sql/planner/input.go +++ b/sql/planner/input.go @@ -33,15 +33,6 @@ func NewTableInputNode(tableName string) Node { } } -func (n *tableInputNode) IsEqual(other Node) bool { - if !n.node.IsEqual(other) { - return false - } - - on := other.(*tableInputNode) - return n.tableName == on.tableName -} - func (n *tableInputNode) Bind(tx *database.Transaction, params []expr.Param) (err error) { n.tx = tx n.params = params @@ -88,15 +79,6 @@ func NewIndexInputNode(tableName, indexName string, iop IndexIteratorOperator, f } } -func (n *indexInputNode) IsEqual(other Node) bool { - if !n.node.IsEqual(other) { - return false - } - - on := other.(*indexInputNode) - return n.tableName == on.tableName && n.indexName == on.indexName -} - func (n *indexInputNode) Bind(tx *database.Transaction, params []expr.Param) (err error) { if n.table == nil { n.table, err = tx.GetTable(n.tableName) diff --git a/sql/planner/optimizer_test.go b/sql/planner/optimizer_test.go index de374a161..1c0949bc2 100644 --- a/sql/planner/optimizer_test.go +++ b/sql/planner/optimizer_test.go @@ -86,7 +86,7 @@ func TestSplitANDConditionRule(t *testing.T) { t.Run(test.name, func(t *testing.T) { res, err := planner.SplitANDConditionRule(planner.NewTree(test.root)) require.NoError(t, err) - require.True(t, res.Root.IsEqual(test.expected)) + require.Equal(t, res.String(), planner.NewTree(test.expected).String()) }) } } @@ -165,7 +165,7 @@ func TestPrecalculateExprRule(t *testing.T) { t.Run(test.name, func(t *testing.T) { res, err := planner.PrecalculateExprRule(planner.NewTree(planner.NewSelectionNode(planner.NewTableInputNode("foo"), test.e))) require.NoError(t, err) - require.True(t, res.Root.IsEqual(planner.NewSelectionNode(planner.NewTableInputNode("foo"), test.expected))) + require.Equal(t, res.String(), planner.NewTree(planner.NewSelectionNode(planner.NewTableInputNode("foo"), test.expected)).String()) }) } } @@ -197,7 +197,7 @@ func TestRemoveUnnecessarySelectionNodesRule(t *testing.T) { res, err := planner.RemoveUnnecessarySelectionNodesRule(planner.NewTree(test.root)) require.NoError(t, err) if test.expected != nil { - require.True(t, test.expected.IsEqual(res.Root)) + require.Equal(t, planner.NewTree(test.expected).String(), res.String()) } else { require.Equal(t, test.expected, res.Root) } @@ -367,7 +367,7 @@ func TestUseIndexBasedOnSelectionNodeRule(t *testing.T) { res, err := planner.UseIndexBasedOnSelectionNodeRule(planner.NewTree(test.root)) require.NoError(t, err) if test.expected != nil { - require.True(t, test.expected.IsEqual(res.Root)) + require.Equal(t, planner.NewTree(test.expected).String(), res.String()) } else { require.Equal(t, res.Root, res.Root) } diff --git a/sql/planner/projection.go b/sql/planner/projection.go index 4ac1c09ac..8a4e96384 100644 --- a/sql/planner/projection.go +++ b/sql/planner/projection.go @@ -40,47 +40,6 @@ func NewProjectionNode(n Node, expressions []ResultField, tableName string) Node } } -// IsEqual returns true if other is a *ProjectionNode and contains the -// same information. -func (n *ProjectionNode) IsEqual(other Node) bool { - if !n.node.IsEqual(other) { - return false - } - - on := other.(*ProjectionNode) - if n.tableName != on.tableName { - return false - } - - if len(n.Expressions) != len(on.Expressions) { - return false - } - - for i := range n.Expressions { - switch t := n.Expressions[i].(type) { - case Wildcard: - if _, ok := on.Expressions[i].(Wildcard); !ok { - return false - } - case ResultFieldExpr: - rf, ok := on.Expressions[i].(ResultFieldExpr) - if !ok { - return false - } - - if t.ExprName != rf.ExprName { - return false - } - - if !expr.Equal(t.Expr, rf.Expr) { - return false - } - } - } - - return true -} - // Bind database resources to this node. func (n *ProjectionNode) Bind(tx *database.Transaction, params []expr.Param) (err error) { n.tx = tx diff --git a/sql/planner/replacement.go b/sql/planner/replacement.go index eb9d2079a..605e47b05 100644 --- a/sql/planner/replacement.go +++ b/sql/planner/replacement.go @@ -36,15 +36,6 @@ func NewReplacementNode(n Node, tableName string) Node { } } -func (n *replacementNode) IsEqual(other Node) bool { - if !n.node.IsEqual(other) { - return false - } - - on := other.(*replacementNode) - return n.tableName == on.tableName -} - func (n *replacementNode) Bind(tx *database.Transaction, params []expr.Param) (err error) { n.table, err = tx.GetTable(n.tableName) return diff --git a/sql/planner/sort.go b/sql/planner/sort.go index 6abf12989..9b3ba15f8 100644 --- a/sql/planner/sort.go +++ b/sql/planner/sort.go @@ -39,15 +39,6 @@ func NewSortNode(n Node, sortField expr.FieldSelector, direction scanner.Token) } } -func (n *sortNode) IsEqual(other Node) bool { - if !n.node.IsEqual(other) { - return false - } - - on := other.(*sortNode) - return expr.Equal(n.sortField, on.sortField) && n.direction == on.direction -} - func (n *sortNode) Bind(tx *database.Transaction, params []expr.Param) (err error) { return } diff --git a/sql/planner/tree.go b/sql/planner/tree.go index 8447dab7c..57dd0ac8a 100644 --- a/sql/planner/tree.go +++ b/sql/planner/tree.go @@ -141,7 +141,6 @@ type Node interface { Right() Node SetLeft(Node) SetRight(Node) - IsEqual(Node) bool Bind(tx *database.Transaction, params []expr.Param) error } @@ -188,38 +187,6 @@ func (n *node) SetRight(rn Node) { n.right = rn } -func (n *node) IsEqual(other Node) bool { - if n == nil { - return other == nil - } - - if other == nil { - return false - } - - if n.op != other.Operation() { - return false - } - - if n.left == nil { - if other.Left() != nil { - return false - } - } else if !n.left.IsEqual(other.Left()) { - return false - } - - if n.right == nil { - if other.Right() != nil { - return false - } - } else if !n.right.IsEqual(other.Right()) { - return false - } - - return true -} - type selectionNode struct { node @@ -242,14 +209,6 @@ func NewSelectionNode(n Node, cond expr.Expr) Node { } } -func (n *selectionNode) IsEqual(other Node) bool { - if !n.node.IsEqual(other) { - return false - } - - return expr.Equal(n.cond, other.(*selectionNode).cond) -} - func (n *selectionNode) Bind(tx *database.Transaction, params []expr.Param) (err error) { n.tx = tx n.params = params @@ -306,14 +265,6 @@ func NewLimitNode(n Node, limit int) Node { } } -func (n *limitNode) IsEqual(other Node) bool { - if !n.node.IsEqual(other) { - return false - } - - return n.limit == other.(*limitNode).limit -} - func (n *limitNode) Bind(tx *database.Transaction, params []expr.Param) (err error) { n.tx = tx n.params = params @@ -349,14 +300,6 @@ func NewOffsetNode(n Node, offset int) Node { } } -func (n *offsetNode) IsEqual(other Node) bool { - if !n.node.IsEqual(other) { - return false - } - - return n.offset == other.(*offsetNode).offset -} - func (n *offsetNode) String() string { return fmt.Sprintf("Offset(%d)", n.offset) } @@ -395,15 +338,6 @@ func NewSetNode(n Node, field string, e expr.Expr) Node { } } -func (n *setNode) IsEqual(other Node) bool { - if !n.node.IsEqual(other) { - return false - } - - on := other.(*setNode) - return n.field == on.field && expr.Equal(n.e, on.e) -} - func (n *setNode) Bind(tx *database.Transaction, params []expr.Param) (err error) { n.tx = tx n.params = params @@ -472,15 +406,6 @@ func NewUnsetNode(n Node, field string) Node { } } -func (n *unsetNode) IsEqual(other Node) bool { - if !n.node.IsEqual(other) { - return false - } - - on := other.(*unsetNode) - return n.field == on.field -} - func (n *unsetNode) Bind(tx *database.Transaction, params []expr.Param) error { return nil } diff --git a/sql/planner/tree_test.go b/sql/planner/tree_test.go deleted file mode 100644 index 7f7457003..000000000 --- a/sql/planner/tree_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package planner - -import ( - "testing" - - "github.com/genjidb/genji/sql/query/expr" -) - -func TestNodeEqual(t *testing.T) { - tests := []struct { - name string - a *node - b Node - equal bool - }{ - {"two nils", (*node)(nil), nil, true}, - {"a nil", (*node)(nil), NewSelectionNode(nil, expr.BoolValue(true)), false}, - {"b nil", &node{op: Selection}, nil, false}, - {"b nil", &node{op: Selection}, NewSelectionNode(nil, nil), true}, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - test.a.IsEqual(test.b) - }) - } -}