Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Reduce amount of implemented code on Expressions #772

Closed
@ajnavarro

Description

@ajnavarro

To reduce the amount of implemented code on Expressions and Nodes, we can heavily use UnaryExpression, BinaryExpression, and so on.

Right now, these interfaces are just implementing Children(), Resolved() and IsNullable().

We could implement also TransformUp function as following:

  • Create a new interface in charge of return a new instance of the implemented expression:
type ExpressionInstanceGenerator interface {
	Generate(exprs ...sql.Expression) (sql.Expression, error)
}
  • With this, we will be able to implement this interface on Unary and Binary expressions:
type BinaryExpression struct {
	ExpressionInstanceGenerator
	Left  sql.Expression
	Right sql.Expression
}
  • After that, TransformUp method can be implemented on BinaryExpression. Generate method is the one that will be implemented on the other Expressions:
func (p *BinaryExpression) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) {
	var err error

	var l sql.Expression
	if p.Left != nil {
		l, err = p.Left.TransformUp(f)
		if err != nil {
			return nil, err
		}
	}
	var r sql.Expression
	if p.Left != nil {
		r, err = p.Right.TransformUp(f)
		if err != nil {
			return nil, err
		}
	}

	exp, err := p.Generate(l, r)
	if err != nil {
		return nil, err
	}

	return f(exp)
}

Doing this, we will reduce bugs on bad implemented TransformUp functions, also we can create generic Tests suites for this kind of expressions.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestproposalproposal for new additions or changes

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions