perf(native): Avoid LIKE rewrites for prefix/suffix patterns in native execution#27363
perf(native): Avoid LIKE rewrites for prefix/suffix patterns in native execution#27363pramodsatya merged 1 commit intoprestodb:masterfrom
LIKE rewrites for prefix/suffix patterns in native execution#27363Conversation
Reviewer's GuideMakes SQL-to-row-expression translation aware of native execution mode and disables LIKE prefix/suffix rewrites when native execution is enabled so that Velox can apply its own optimized LIKE evaluation. Sequence diagram for LIKE translation with nativeExecutionEnabled flagsequenceDiagram
actor Client
participant Coordinator
participant SqlToRowExpressionTranslator
participant Visitor
participant VeloxEngine
Client->>Coordinator: Submit query with LIKE predicate
Coordinator->>Coordinator: Determine nativeExecutionEnabled
Coordinator->>SqlToRowExpressionTranslator: translate(expression, types, layout, functionAndTypeManager, session, context)
SqlToRowExpressionTranslator->>SqlToRowExpressionTranslator: translate(..., user, transactionId, sqlFunctionProperties, sessionFunctions, context, nativeExecutionEnabled)
SqlToRowExpressionTranslator->>Visitor: new Visitor(..., nativeExecutionEnabled)
SqlToRowExpressionTranslator->>Visitor: process(expression, context)
Visitor->>Visitor: visitLikePredicate(node, context)
alt nativeExecutionEnabled == false
Visitor->>Visitor: generateLikePrefixOrSuffixMatch(value, pattern)
Visitor-->>SqlToRowExpressionTranslator: rewritten SUBSTR/STRPOS RowExpression
else nativeExecutionEnabled == true
Visitor->>Visitor: skip generateLikePrefixOrSuffixMatch
Visitor-->>SqlToRowExpressionTranslator: LIKE RowExpression without rewrite
SqlToRowExpressionTranslator-->>VeloxEngine: pass LIKE RowExpression
VeloxEngine-->>Coordinator: evaluate LIKE via OptimizedLike
end
Coordinator-->>Client: Return query results
Class diagram for SqlToRowExpressionTranslator nativeExecutionEnabled wiringclassDiagram
class SqlToRowExpressionTranslator {
+RowExpression translate(Expression expression, Map~NodeRef_Expression_, Type~ types, Map~VariableReferenceExpression, Integer~ layout, FunctionAndTypeManager functionAndTypeManager, Session session, Context context)
+RowExpression translate(Expression expression, Map~NodeRef_Expression_, Type~ types, Map~VariableReferenceExpression, Integer~ layout, FunctionAndTypeManager functionAndTypeManager, Optional~String~ user, Optional~TransactionId~ transactionId, SqlFunctionProperties sqlFunctionProperties, Map~SqlFunctionId, SqlInvokedFunction~ sessionFunctions, Context context)
-RowExpression translate(Expression expression, Map~NodeRef_Expression_, Type~ types, Map~VariableReferenceExpression, Integer~ layout, FunctionAndTypeManager functionAndTypeManager, Optional~String~ user, Optional~TransactionId~ transactionId, SqlFunctionProperties sqlFunctionProperties, Map~SqlFunctionId, SqlInvokedFunction~ sessionFunctions, Context context, boolean nativeExecutionEnabled)
}
class Visitor {
-Map~NodeRef_Expression_, Type~ types
-Map~VariableReferenceExpression, Integer~ layout
-FunctionAndTypeResolver functionAndTypeResolver
-Optional~String~ user
-Optional~TransactionId~ transactionId
-SqlFunctionProperties sqlFunctionProperties
-Map~SqlFunctionId, SqlInvokedFunction~ sessionFunctions
-FunctionResolution functionResolution
-boolean nativeExecutionEnabled
+Visitor(Map~NodeRef_Expression_, Type~ types, Map~VariableReferenceExpression, Integer~ layout, FunctionAndTypeManager functionAndTypeManager, Optional~String~ user, Optional~TransactionId~ transactionId, SqlFunctionProperties sqlFunctionProperties, Map~SqlFunctionId, SqlInvokedFunction~ sessionFunctions, boolean nativeExecutionEnabled)
+RowExpression visitLikePredicate(LikePredicate node, Context context)
-RowExpression generateLikePrefixOrSuffixMatch(RowExpression value, RowExpression pattern)
}
SqlToRowExpressionTranslator ..> Visitor : creates
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider avoiding the extra boolean parameter on the private
translate/Visitorconstructor by threading native-execution awareness throughContextor a small options object, which will scale better if additional execution-mode flags are added later. - It would be helpful to document in the
visitLikePredicatelogic (e.g., a short comment) that disabling the prefix/suffix rewrite under native execution intentionally defers to Velox'sOptimizedLikeimplementation, so future maintainers understand why this early-return is gated.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider avoiding the extra boolean parameter on the private `translate`/`Visitor` constructor by threading native-execution awareness through `Context` or a small options object, which will scale better if additional execution-mode flags are added later.
- It would be helpful to document in the `visitLikePredicate` logic (e.g., a short comment) that disabling the prefix/suffix rewrite under native execution intentionally defers to Velox's `OptimizedLike` implementation, so future maintainers understand why this early-return is gated.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
@majetideepak, sharing results from standalone microbenchmark comparing Velox's native LIKE evaluation ( ResultsAnalysis
Suffix matching is the dominant win — Velox's O(needle_len) |
aditi-pandit
left a comment
There was a problem hiding this comment.
Thanks @pramodsatya. Looks good.
Description
This change conditionally disables Presto's coordinator-side LIKE pattern rewrites when
native-execution-enabled=true. Velox'sOptimizedLikeimplementation provides superior performance for simple LIKE patterns compared to Presto's rewrites that decompose LIKE into SUBSTR/STRPOS calls.Motivation and Context
Presto currently optimizes SQL LIKE patterns at the coordinator level:
'foo%'→SUBSTR(x, 1, len) = 'foo''%foo'→SUBSTR(x, -len) = 'foo''%foo%'→STRPOS(x, 'foo') != 0When native execution is enabled, Velox's engine natively evaluates LIKE expressions using the
OptimizedLiketemplate class, which provides fast-path implementations:memcmpon byte ranges (no character indexing overhead)std::string_view::findfor O(n) scanningIn contrast, Presto's SUBSTR/STRPOS rewrites require:
For constant patterns, Velox's optimized paths are better performant. By letting Velox evaluate LIKE natively, we unlock Velox's optimization by eliminating unnecessary rewriting.
Impact
Test Plan
TestRowExpressionTranslatortest suiteRelease Notes
Summary by Sourcery
Guard LIKE prefix/suffix rewrite logic in the SQL-to-row-expression translator with the native execution enablement flag to preserve expected behavior under native execution.
Enhancements:
Summary by Sourcery
Gate coordinator-side LIKE prefix/suffix pattern rewrites on the native execution flag so that native execution uses Velox’s built-in LIKE evaluation.
Enhancements: