Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: support ConstItem() for expression #10004

Merged
merged 6 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions expression/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ func (col *CorrelatedColumn) IsCorrelated() bool {
return true
}

// ReferTable implements Expression interface.
func (col *CorrelatedColumn) ReferTable() bool {
return true
}

// Decorrelate implements Expression interface.
func (col *CorrelatedColumn) Decorrelate(schema *Schema) Expression {
if !schema.Contains(&col.Column) {
Expand Down Expand Up @@ -297,6 +302,11 @@ func (col *Column) IsCorrelated() bool {
return false
}

// ReferTable implements Expression interface.
func (col *Column) ReferTable() bool {
return true
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
}

// Decorrelate implements Expression interface.
func (col *Column) Decorrelate(_ *Schema) Expression {
return col
Expand Down
1 change: 1 addition & 0 deletions expression/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (s *testEvaluatorSuite) TestColumn(c *C) {
c.Assert(corCol.Equal(nil, corCol), IsTrue)
c.Assert(corCol.Equal(nil, invalidCorCol), IsFalse)
c.Assert(corCol.IsCorrelated(), IsTrue)
c.Assert(corCol.ReferTable(), IsTrue)
c.Assert(corCol.Decorrelate(schema).Equal(nil, col), IsTrue)
c.Assert(invalidCorCol.Decorrelate(schema).Equal(nil, invalidCorCol), IsTrue)

Expand Down
5 changes: 5 additions & 0 deletions expression/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ func (c *Constant) IsCorrelated() bool {
return false
}

// ReferTable implements Expression interface.
func (c *Constant) ReferTable() bool {
return false
}

// Decorrelate implements Expression interface.
func (c *Constant) Decorrelate(_ *Schema) Expression {
return c
Expand Down
3 changes: 3 additions & 0 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ type Expression interface {
// IsCorrelated checks if this expression has correlated key.
IsCorrelated() bool

// ReferTable checks if this expression refers to a table.
ReferTable() bool
qw4990 marked this conversation as resolved.
Show resolved Hide resolved

// Decorrelate try to decorrelate the expression by schema.
Decorrelate(schema *Schema) Expression

Expand Down
1 change: 1 addition & 0 deletions expression/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (s *testEvaluatorSuite) TestConstant(c *C) {

sc := &stmtctx.StatementContext{TimeZone: time.Local}
c.Assert(Zero.IsCorrelated(), IsFalse)
c.Assert(Zero.ReferTable(), IsFalse)
c.Assert(Zero.Decorrelate(nil).Equal(s.ctx, Zero), IsTrue)
c.Assert(Zero.HashCode(sc), DeepEquals, []byte{0x0, 0x8, 0x0})
c.Assert(Zero.Equal(s.ctx, One), IsFalse)
Expand Down
10 changes: 10 additions & 0 deletions expression/scalar_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ func (sf *ScalarFunction) IsCorrelated() bool {
return false
}

// ReferTable implements Expression interface.
func (sf *ScalarFunction) ReferTable() bool {
for _, arg := range sf.GetArgs() {
if arg.ReferTable() {
return true
}
}
return false
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems ExtractColumns can provide what you want? CorrelatedColumn should be treated as constant actually?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, my fault. We need to check also non-deterministic functions and statement parameters

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh from this comment, you need to check if the expression is "purely" constant, i.e, CorrelatedColumn should not be treated as constant because we are not in executor, so ExtractColumns cannot work, sorry for the misleading.

Why would non-deterministic functions matter here? though their return value is non-deterministic, the return type is deterministic?

What does statement parameters mean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For non-deterministic functions, their return value is non-deterministic, so you can't use its value to computer the frac part or somethings else.
I believe statement parameters is the parameters used in prepare statement.

// Decorrelate implements Expression interface.
func (sf *ScalarFunction) Decorrelate(schema *Schema) Expression {
for i, arg := range sf.GetArgs() {
Expand Down
1 change: 1 addition & 0 deletions expression/scalar_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (s *testEvaluatorSuite) TestScalarFunction(c *C) {
c.Assert(err, IsNil)
c.Assert(res, DeepEquals, []byte{0x22, 0x6c, 0x74, 0x28, 0x66, 0x65, 0x69, 0x2e, 0x68, 0x61, 0x6e, 0x2c, 0x20, 0x31, 0x29, 0x22})
c.Assert(sf.IsCorrelated(), IsFalse)
c.Assert(sf.ReferTable(), IsTrue)
c.Assert(sf.Decorrelate(nil).Equal(s.ctx, sf), IsTrue)
c.Assert(sf.HashCode(sc), DeepEquals, []byte{0x3, 0x4, 0x6c, 0x74, 0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x5, 0xbf, 0xf0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})

Expand Down