-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
remove input argument for Expression.ConstItem
#49464
Comments
For the cache inside expression sig, I think we can distinguish two different usages of the cache:
The layer 2 cache should be prefered: if the layer 2 cache is nil, try to read from layer 1 cache. For the first situation, we can simple return For example
If |
I prefer this one since
I prefer this one( |
@bb7133 Let me try to explain in a clearer way. Now, the cache in expression can take effect in two different scopes:
The requirement of the second cache is stronger than the first one. A However, we only have a single cache currently. Therefore, the current implementation has an assumption: the prepared statement can only use the second kind of cache. It's unreasonable and can harm the performance. For example, the statement I have discussed with @lcwangchao, and we agreed that our plans don't have much difference technically, and he'd like to only take care of the first kind of cache for the simplicity at first. It may cause some performance degradation (for prepared statement), but it'll be easier for us to take the first step 👍 . |
@bb7133 In PR #49584, I'm using ContextID way to separate caches because its performance is better. The stmtCache in Any way, the cache logic is in |
Enhancement
The method
Expression.ConstItem
returns a bool to indicate whether the expression returns a constant ignoring the input row. However, it has an input argument with type*stmtctx.StatementContext
.When we take a closer look at its implementation. We found the input stmtctx is required by
Constant
:tidb/pkg/expression/constant.go
Lines 426 to 428 in d076bc1
So this function has two meanings:
UseCache
is true. This returns whether the expression is constant ignoring theEvalContext
which means it will return a constant value at any time.UseCache
is false. This returns whether the expression is constant only with the sameEvalContext
.The concern of
UseCache
field is to make sure some intermediate states inScalarExpression
can be cached correctly. See implement of regexp: https://github.com/pingcap/tidb/blob/master/pkg/expression/builtin_regexp.go . When an expression will be cached in plan cache, it should not cache the intermediate state because some other statements may use it later.However, it will has some problems:
ConstItem
is confusing, and the expression is coupled with plan cache heavily.UseCache
and just want to know whether the expression returns a constant with same context. But the current implement forces us to check it:approx_percentile
report error some time in prepare/execute #49343Some possible refactors
Remove input argument in
ConstItem
to decouple with plan cacheMake it clear whether the expression is a pure constant or context-level constant.
First option, introduce a new enum
ConstLevel
to tell the expression's level of constAnother option, introduce two methods to indicate the const property seperately
Make
ScalarFunction
stateless and move cache to contextTo make sure we can also cache some states when plan cache enabled, we can move the expression cache from scalar function to context:
The method
cacheKey()
should return a unique string for each scaclar function and we can use a global uint64 to generate it.Another option is to still cache some states in the scalar function but use a contextID to tell which context it belongs to. For example:
Is seems a little wired, but more efficient than the first way
Tasks
Expression.ConstItem
fromStatementContext
tobool
#49492regexp_substr
#49583regexp_like
/regexp_instr
/regexp_replace
#49641like
andilike
#49674regexpBaseFuncSig
#49684ConstItem
to be a constant bool #49790The text was updated successfully, but these errors were encountered: