Skip to content

Commit

Permalink
opt: introduce VolatilitySet logical property
Browse files Browse the repository at this point in the history
This commit adds a VolatilitySet logical property that will replace
CanHaveSideEffects. For now, the property is not used for anything. The property
reflects the volatility of any functions; future changes will incorporate the
volatility of casts and scalar operators.

The reason for why we need a set (rather than the "max" volatility) is that we
will need to reoptimize a cached expression if it contains stable operators
(regardless of whether it also contains volatile operators).

Release note: None
  • Loading branch information
RaduBerinde committed Jun 1, 2020
1 parent c52e94c commit 107c2ae
Show file tree
Hide file tree
Showing 69 changed files with 1,057 additions and 764 deletions.
6 changes: 6 additions & 0 deletions pkg/sql/opt/memo/expr_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,9 @@ func (f *ExprFmtCtx) formatRelational(e RelExpr, tp treeprinter.Node) {
f.Buffer.WriteString(name)
}

if !relational.VolatilitySet.IsLeakProof() {
writeFlag(relational.VolatilitySet.String())
}
if relational.CanHaveSideEffects {
writeFlag("side-effects")
}
Expand Down Expand Up @@ -874,6 +877,9 @@ func (f *ExprFmtCtx) scalarPropsStrings(scalar opt.ScalarExpr) []string {
if !scalarProps.OuterCols.Empty() {
emitProp("outer=%s", scalarProps.OuterCols)
}
if !scalarProps.VolatilitySet.IsLeakProof() {
emitProp(scalarProps.VolatilitySet.String())
}
if scalarProps.CanHaveSideEffects {
emitProp("side-effects")
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/opt/memo/logical_props_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (b *logicalPropsBuilder) buildScanProps(scan *ScanExpr, rel *props.Relation
// A Locking option is a side-effect (we don't want to elide this scan).
if scan.Locking != nil {
rel.CanHaveSideEffects = true
rel.VolatilitySet.AddVolatile()
}

// Output Columns
Expand Down Expand Up @@ -911,6 +912,7 @@ func (b *logicalPropsBuilder) buildLimitProps(limit *LimitExpr, rel *props.Relat
// Negative limits can trigger a runtime error.
if constLimit < 0 || !haveConstLimit {
rel.CanHaveSideEffects = true
rel.VolatilitySet.AddImmutable()
}

// Output Columns
Expand Down Expand Up @@ -1383,6 +1385,7 @@ func BuildSharedProps(e opt.Expr, shared *props.Shared) {
}
if !nonZero {
shared.CanHaveSideEffects = true
shared.VolatilitySet.AddImmutable()
}

case *SubqueryExpr, *ExistsExpr, *AnyExpr, *ArrayFlattenExpr:
Expand All @@ -1399,11 +1402,13 @@ func BuildSharedProps(e opt.Expr, shared *props.Shared) {
// Impure functions can return different value on each call.
shared.CanHaveSideEffects = true
}
shared.VolatilitySet.Add(t.Overload.Volatility)

default:
if opt.IsMutationOp(e) {
shared.CanHaveSideEffects = true
shared.CanMutate = true
shared.VolatilitySet.AddVolatile()
}
}

Expand All @@ -1426,6 +1431,7 @@ func BuildSharedProps(e opt.Expr, shared *props.Shared) {
if cached.HasPlaceholder {
shared.HasPlaceholder = true
}
shared.VolatilitySet.UnionWith(cached.VolatilitySet)
if cached.CanHaveSideEffects {
shared.CanHaveSideEffects = true
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/sql/opt/memo/testdata/logprops/constraints-null
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ SELECT * FROM xy WHERE x > abs(y)
----
select
├── columns: x:1(int!null) y:2(int)
├── immutable
├── scan xy
│ ├── columns: x:1(int) y:2(int)
│ └── prune: (1,2)
└── filters
└── gt [type=bool, outer=(1,2), constraints=(/1: (/NULL - ])]
└── gt [type=bool, outer=(1,2), immutable, constraints=(/1: (/NULL - ])]
├── variable: x:1 [type=int]
└── function: abs [type=int]
└── variable: y:2 [type=int]
Expand All @@ -114,12 +115,13 @@ SELECT * FROM xy WHERE sin(x::float)::int < x
----
select
├── columns: x:1(int!null) y:2(int)
├── immutable
├── prune: (2)
├── scan xy
│ ├── columns: x:1(int) y:2(int)
│ └── prune: (1,2)
└── filters
└── gt [type=bool, outer=(1), constraints=(/1: (/NULL - ])]
└── gt [type=bool, outer=(1), immutable, constraints=(/1: (/NULL - ])]
├── variable: x:1 [type=int]
└── cast: INT8 [type=int]
└── function: sin [type=float]
Expand Down
14 changes: 7 additions & 7 deletions pkg/sql/opt/memo/testdata/logprops/delete
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ delete abcde
├── columns: <none>
├── fetch columns: a:7(int) b:8(int) c:9(int) d:10(int) rowid:11(int) e:12(int)
├── cardinality: [0 - 0]
├── side-effects, mutations
├── volatile, side-effects, mutations
└── select
├── columns: a:7(int!null) b:8(int) c:9(int!null) d:10(int) rowid:11(int!null) e:12(int)
├── key: (11)
Expand Down Expand Up @@ -55,13 +55,13 @@ DELETE FROM abcde WHERE a=1 RETURNING *
----
project
├── columns: a:1(int!null) b:2(int) c:3(int!null) d:4(int)
├── side-effects, mutations
├── volatile, side-effects, mutations
├── fd: ()-->(1)
├── prune: (1-4)
└── delete abcde
├── columns: a:1(int!null) b:2(int) c:3(int!null) d:4(int) rowid:5(int!null)
├── fetch columns: a:7(int) b:8(int) c:9(int) d:10(int) rowid:11(int) e:12(int)
├── side-effects, mutations
├── volatile, side-effects, mutations
├── key: (5)
├── fd: ()-->(1), (5)-->(2-4)
├── prune: (1-4)
Expand Down Expand Up @@ -96,15 +96,15 @@ DELETE FROM abcde WHERE rowid=1 RETURNING *
project
├── columns: a:1(int!null) b:2(int) c:3(int!null) d:4(int)
├── cardinality: [0 - 1]
├── side-effects, mutations
├── volatile, side-effects, mutations
├── key: ()
├── fd: ()-->(1-4)
├── prune: (1-4)
└── delete abcde
├── columns: a:1(int!null) b:2(int) c:3(int!null) d:4(int) rowid:5(int!null)
├── fetch columns: a:7(int) b:8(int) c:9(int) d:10(int) rowid:11(int) e:12(int)
├── cardinality: [0 - 1]
├── side-effects, mutations
├── volatile, side-effects, mutations
├── key: ()
├── fd: ()-->(1-5)
├── prune: (1-4)
Expand Down Expand Up @@ -139,13 +139,13 @@ DELETE FROM abcde WHERE b=c RETURNING *;
----
project
├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(int)
├── side-effects, mutations
├── volatile, side-effects, mutations
├── fd: (2)==(3), (3)==(2)
├── prune: (1-4)
└── delete abcde
├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(int) rowid:5(int!null)
├── fetch columns: a:7(int) b:8(int) c:9(int) d:10(int) rowid:11(int) e:12(int)
├── side-effects, mutations
├── volatile, side-effects, mutations
├── key: (5)
├── fd: (2)==(3), (3)==(2), (5)-->(1-4)
├── prune: (1-4)
Expand Down
46 changes: 23 additions & 23 deletions pkg/sql/opt/memo/testdata/logprops/insert
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ insert abcde
│ ├── column11:11 => rowid:5
│ └── column12:12 => e:6
├── cardinality: [0 - 0]
├── side-effects, mutations
├── volatile, side-effects, mutations
└── project
├── columns: column13:13(int!null) y:8(int!null) column10:10(int!null) column11:11(int) column12:12(int)
├── cardinality: [0 - 10]
├── side-effects
├── volatile, side-effects
├── fd: ()-->(10,12), (8)-->(13)
├── prune: (8,10-13)
├── interesting orderings: (+8)
├── project
│ ├── columns: column10:10(int!null) column11:11(int) column12:12(int) y:8(int!null)
│ ├── cardinality: [0 - 10]
│ ├── side-effects
│ ├── volatile, side-effects
│ ├── fd: ()-->(10,12)
│ ├── prune: (8,10-12)
│ ├── interesting orderings: (+8)
Expand All @@ -67,7 +67,7 @@ insert abcde
│ │ └── const: 10 [type=int]
│ └── projections
│ ├── const: 10 [as=column10:10, type=int]
│ ├── function: unique_rowid [as=column11:11, type=int, side-effects]
│ ├── function: unique_rowid [as=column11:11, type=int, volatile, side-effects]
│ └── cast: INT8 [as=column12:12, type=int]
│ └── null [type=unknown]
└── projections
Expand All @@ -84,7 +84,7 @@ INSERT INTO abcde (a, b) SELECT y, y FROM xyz ORDER BY y, z LIMIT 10 RETURNING *
project
├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(int!null)
├── cardinality: [0 - 10]
├── side-effects, mutations
├── volatile, side-effects, mutations
├── fd: ()-->(3), (1)==(2), (2)==(1), (1)-->(4)
├── prune: (1-4)
└── insert abcde
Expand All @@ -97,19 +97,19 @@ project
│ ├── column11:11 => rowid:5
│ └── column12:12 => e:6
├── cardinality: [0 - 10]
├── side-effects, mutations
├── volatile, side-effects, mutations
├── fd: ()-->(3), (1)==(2), (2)==(1), (1)-->(4)
└── project
├── columns: column13:13(int!null) y:8(int!null) column10:10(int!null) column11:11(int) column12:12(int)
├── cardinality: [0 - 10]
├── side-effects
├── volatile, side-effects
├── fd: ()-->(10,12), (8)-->(13)
├── prune: (8,10-13)
├── interesting orderings: (+8)
├── project
│ ├── columns: column10:10(int!null) column11:11(int) column12:12(int) y:8(int!null)
│ ├── cardinality: [0 - 10]
│ ├── side-effects
│ ├── volatile, side-effects
│ ├── fd: ()-->(10,12)
│ ├── prune: (8,10-12)
│ ├── interesting orderings: (+8)
Expand All @@ -135,7 +135,7 @@ project
│ │ └── const: 10 [type=int]
│ └── projections
│ ├── const: 10 [as=column10:10, type=int]
│ ├── function: unique_rowid [as=column11:11, type=int, side-effects]
│ ├── function: unique_rowid [as=column11:11, type=int, volatile, side-effects]
│ └── cast: INT8 [as=column12:12, type=int]
│ └── null [type=unknown]
└── projections
Expand All @@ -151,7 +151,7 @@ INSERT INTO abcde (a, b) SELECT y, y FROM xyz ORDER BY y, z RETURNING *
----
project
├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(int!null)
├── side-effects, mutations
├── volatile, side-effects, mutations
├── fd: ()-->(3), (1)==(2), (2)==(1), (1)-->(4)
├── prune: (1-4)
└── insert abcde
Expand All @@ -163,16 +163,16 @@ project
│ ├── column13:13 => d:4
│ ├── column11:11 => rowid:5
│ └── column12:12 => e:6
├── side-effects, mutations
├── volatile, side-effects, mutations
├── fd: ()-->(3), (1)==(2), (2)==(1), (1)-->(4)
└── project
├── columns: column13:13(int!null) y:8(int!null) column10:10(int!null) column11:11(int) column12:12(int)
├── side-effects
├── volatile, side-effects
├── fd: ()-->(10,12), (8)-->(13)
├── prune: (8,10-13)
├── project
│ ├── columns: column10:10(int!null) column11:11(int) column12:12(int) y:8(int!null)
│ ├── side-effects
│ ├── volatile, side-effects
│ ├── fd: ()-->(10,12)
│ ├── prune: (8,10-12)
│ ├── project
Expand All @@ -186,7 +186,7 @@ project
│ │ └── interesting orderings: (+7)
│ └── projections
│ ├── const: 10 [as=column10:10, type=int]
│ ├── function: unique_rowid [as=column11:11, type=int, side-effects]
│ ├── function: unique_rowid [as=column11:11, type=int, volatile, side-effects]
│ └── cast: INT8 [as=column12:12, type=int]
│ └── null [type=unknown]
└── projections
Expand All @@ -210,20 +210,20 @@ insert abcde
│ ├── column10:10 => rowid:5
│ └── column11:11 => e:6
├── cardinality: [1 - 1]
├── side-effects, mutations
├── volatile, side-effects, mutations
├── key: ()
├── fd: ()-->(1-5)
└── project
├── columns: column12:12(int!null) column1:7(int!null) column2:8(int!null) column9:9(int!null) column10:10(int) column11:11(int)
├── cardinality: [1 - 1]
├── side-effects
├── volatile, side-effects
├── key: ()
├── fd: ()-->(7-12)
├── prune: (7-12)
├── project
│ ├── columns: column9:9(int!null) column10:10(int) column11:11(int) column1:7(int!null) column2:8(int!null)
│ ├── cardinality: [1 - 1]
│ ├── side-effects
│ ├── volatile, side-effects
│ ├── key: ()
│ ├── fd: ()-->(7-11)
│ ├── prune: (7-11)
Expand All @@ -238,7 +238,7 @@ insert abcde
│ │ └── const: 2 [type=int]
│ └── projections
│ ├── const: 10 [as=column9:9, type=int]
│ ├── function: unique_rowid [as=column10:10, type=int, side-effects]
│ ├── function: unique_rowid [as=column10:10, type=int, volatile, side-effects]
│ └── cast: INT8 [as=column11:11, type=int]
│ └── null [type=unknown]
└── projections
Expand All @@ -254,7 +254,7 @@ INSERT INTO abcde (a, b) SELECT y, (z+1)::int FROM xyz WHERE y=1 RETURNING a, c;
----
project
├── columns: a:1(int!null) c:3(int!null)
├── side-effects, mutations
├── volatile, side-effects, mutations
├── fd: ()-->(1,3)
├── prune: (1,3)
└── insert abcde
Expand All @@ -266,16 +266,16 @@ project
│ ├── column14:14 => d:4
│ ├── column12:12 => rowid:5
│ └── column13:13 => e:6
├── side-effects, mutations
├── volatile, side-effects, mutations
├── fd: ()-->(1,3), (2)-->(4)
└── project
├── columns: column14:14(int) y:8(int!null) int8:10(int) column11:11(int!null) column12:12(int) column13:13(int)
├── side-effects
├── volatile, side-effects
├── fd: ()-->(8,11,13), (10)-->(14)
├── prune: (8,10-14)
├── project
│ ├── columns: column11:11(int!null) column12:12(int) column13:13(int) y:8(int!null) int8:10(int)
│ ├── side-effects
│ ├── volatile, side-effects
│ ├── fd: ()-->(8,11,13)
│ ├── prune: (8,10-13)
│ ├── project
Expand Down Expand Up @@ -305,7 +305,7 @@ project
│ │ └── const: 1.0 [type=float]
│ └── projections
│ ├── const: 10 [as=column11:11, type=int]
│ ├── function: unique_rowid [as=column12:12, type=int, side-effects]
│ ├── function: unique_rowid [as=column12:12, type=int, volatile, side-effects]
│ └── cast: INT8 [as=column13:13, type=int]
│ └── null [type=unknown]
└── projections
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/opt/memo/testdata/logprops/join
Original file line number Diff line number Diff line change
Expand Up @@ -1444,13 +1444,13 @@ SELECT (SELECT m FROM
----
with &1
├── columns: m:19(int)
├── side-effects, mutations
├── volatile, side-effects, mutations
├── fd: ()-->(19)
├── prune: (19)
├── project
│ ├── columns: uv.u:4(int!null) uv.v:5(int!null)
│ ├── cardinality: [1 - 1]
│ ├── side-effects, mutations
│ ├── volatile, side-effects, mutations
│ ├── key: ()
│ ├── fd: ()-->(4,5)
│ ├── prune: (4,5)
Expand All @@ -1461,13 +1461,13 @@ with &1
│ │ ├── column2:8 => uv.v:5
│ │ └── column9:9 => rowid:6
│ ├── cardinality: [1 - 1]
│ ├── side-effects, mutations
│ ├── volatile, side-effects, mutations
│ ├── key: ()
│ ├── fd: ()-->(4-6)
│ └── values
│ ├── columns: column1:7(int!null) column2:8(int!null) column9:9(int)
│ ├── cardinality: [1 - 1]
│ ├── side-effects
│ ├── volatile, side-effects
│ ├── key: ()
│ ├── fd: ()-->(7-9)
│ ├── prune: (7-9)
Expand Down
Loading

0 comments on commit 107c2ae

Please sign in to comment.