-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[Enhancement] improve sql digest for massive compound predicates #53207
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,28 +15,70 @@ | |
package com.starrocks.sql.common; | ||
|
||
import com.google.common.base.Joiner; | ||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Sets; | ||
import com.starrocks.analysis.CompoundPredicate; | ||
import com.starrocks.analysis.Expr; | ||
import com.starrocks.analysis.InPredicate; | ||
import com.starrocks.analysis.LimitElement; | ||
import com.starrocks.analysis.LiteralExpr; | ||
import com.starrocks.analysis.SlotRef; | ||
import com.starrocks.common.Pair; | ||
import com.starrocks.sql.analyzer.AstToStringBuilder; | ||
import com.starrocks.sql.ast.StatementBase; | ||
import com.starrocks.sql.ast.ValuesRelation; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.List; | ||
import java.util.Queue; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Used to build sql digest(string without any dynamic parameters in it) | ||
*/ | ||
public class SqlDigestBuilder { | ||
|
||
private static final int MASSIVE_COMPOUND_LIMIT = 16; | ||
|
||
public static String build(StatementBase statement) { | ||
return new SqlDigestBuilderVisitor().visit(statement); | ||
} | ||
|
||
private static class SqlDigestBuilderVisitor extends AstToStringBuilder.AST2StringBuilderVisitor { | ||
|
||
private Pair<Integer, Integer> countCompound(CompoundPredicate node) { | ||
int andCount = 0; | ||
int orCount = 0; | ||
Queue<Expr> q = Lists.newLinkedList(); | ||
q.add(node); | ||
while (!q.isEmpty()) { | ||
Expr head = q.poll(); | ||
if (head instanceof CompoundPredicate) { | ||
CompoundPredicate.Operator op = ((CompoundPredicate) head).getOp(); | ||
if (op == CompoundPredicate.Operator.AND) { | ||
andCount++; | ||
} else if (op == CompoundPredicate.Operator.OR) { | ||
orCount++; | ||
} | ||
} | ||
q.addAll(head.getChildren()); | ||
} | ||
|
||
return Pair.create(andCount, orCount); | ||
} | ||
|
||
private void traverse(CompoundPredicate node, Consumer<Expr> consumer) { | ||
Queue<Expr> q = Lists.newLinkedList(); | ||
q.add(node); | ||
while (!q.isEmpty()) { | ||
Expr head = q.poll(); | ||
consumer.accept(head); | ||
q.addAll(head.getChildren()); | ||
} | ||
} | ||
|
||
@Override | ||
public String visitInPredicate(InPredicate node, Void context) { | ||
if (!node.isLiteralChildren()) { | ||
|
@@ -50,6 +92,26 @@ public String visitInPredicate(InPredicate node, Void context) { | |
} | ||
} | ||
|
||
@Override | ||
public String visitCompoundPredicate(CompoundPredicate node, Void context) { | ||
Pair<Integer, Integer> pair = countCompound(node); | ||
if (pair.first + pair.second >= MASSIVE_COMPOUND_LIMIT) { | ||
// Only record de-duplicated slots if there are too many compounds | ||
Set<SlotRef> exprs = Sets.newHashSet(); | ||
traverse(node, x -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well, I should use it. but it seems I should improve the performance of that method first. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it constructs a lot of intermediate list,, i'm afraid the performance can be terrible |
||
if (x instanceof SlotRef) { | ||
exprs.add((SlotRef) x); | ||
} | ||
}); | ||
return "$massive_compounds[" + exprs.stream().map(SlotRef::toSqlImpl) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SlotRefs are query-specific so unstable, suggest to use original SlotRefs(slotref references to table column) and convert original slotrefs into table columns then sort these columns lexicographically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea. |
||
.collect(Collectors.joining(",")) + "]$"; | ||
} else { | ||
// TODO: it will introduce a little bit overhead in top-down visiting, in which the countCompound is | ||
// duplicated. it's better to eliminate this overhead | ||
return super.visitCompoundPredicate(node, context); | ||
} | ||
} | ||
|
||
@Override | ||
public String visitValues(ValuesRelation node, Void scope) { | ||
if (node.isNullValues()) { | ||
murphyatwork marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
Expr#flattenPredicate;