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

[Enhancement] improve sql digest for massive compound predicates #53207

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about Expr#flattenPredicate;

// Only record de-duplicated slots if there are too many compounds
Set<SlotRef> exprs = Sets.newHashSet();
traverse(node, x -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

why not use node.collectAllSlotRefs() directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ public static void afterClass() {
"|INSERT INTO `test`.`part_t1` PARTITION (p1) VALUES(?, ?, ?)",
"insert overwrite part_t1 partition (p1) values(1,2,3) " +
"|INSERT OVERWRITE `test`.`part_t1` PARTITION (p1) VALUES(?, ?, ?)",

// massive compounds
"select * from t1 where v4=1 or v4=2 or v4=3 or v4=4 or v4=5 or v4=6 or v4=7 or v4=8 or v4=9 or v4=10 " +
"or v4=11 or v4=12 or v4=13 or v4=14 or v4=15 or v4=16 or v4=17 or v4=18 " +
"or v4=19 or v4=20| " +
"SELECT * FROM test.t1 WHERE $massive_compounds[`test`.`t1`.`v4`]$",
"select * from t1 where v5 = 123 and (v4=1 or v4=2 or v4=3 or v4=4 or v4=5 or v4=6 or v4=7 or v4=8 or " +
"v4=9 or v4=10 " +
"or v4=11 or v4=12 or v4=13 or v4=14 or v4=15 or v4=16 or v4=17 or v4=18 " +
"or v4=19 or v4=20)| " +
"SELECT * FROM test.t1 WHERE $massive_compounds[`test`.`t1`.`v5`,`test`.`t1`.`v4`]$",
})
public void testBuild(String sql, String expectedDigest) throws Exception {
StatementBase stmt = UtFrameUtils.parseStmtWithNewParser(sql, connectContext);
Expand Down
Loading