Skip to content

Commit

Permalink
[Fix](Nereids) fix query sql-cache for nereids.
Browse files Browse the repository at this point in the history
  • Loading branch information
王翔宇 committed Aug 10, 2023
1 parent 36f7b4b commit d44c17b
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.catalog.DatabaseIf;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.View;
import org.apache.doris.common.Pair;
import org.apache.doris.datasource.CatalogIf;
import org.apache.doris.nereids.analyzer.Scope;
Expand Down Expand Up @@ -61,6 +62,7 @@

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -97,6 +99,7 @@ public class CascadesContext implements ScheduleContext {
private final RuntimeFilterContext runtimeFilterContext;
private Optional<Scope> outerScope = Optional.empty();
private List<TableIf> tables = null;
private Set<View> views = Sets.newHashSet();

private boolean isRewriteRoot;
private volatile boolean isTimeout = false;
Expand Down Expand Up @@ -355,6 +358,14 @@ public TableIf getTableInMinidumpCache(String tableName) {
return null;
}

public void addView(View view) {
this.views.add(view);
}

public List<View> getViews() {
return ImmutableList.copyOf(views);
}

public List<TableIf> getTables() {
return tables;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void plan(StatementBase queryStmt, org.apache.doris.thrift.TQueryOptions
ArrayList<String> columnLabelList = physicalPlan.getOutput().stream().map(NamedExpression::getName)
.collect(Collectors.toCollection(ArrayList::new));
logicalPlanAdapter.setColLabels(columnLabelList);
logicalPlanAdapter.setTables(cascadesContext.getTables());
logicalPlanAdapter.setViews(cascadesContext.getViews());
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.doris.analysis.Queriable;
import org.apache.doris.analysis.RedirectStatus;
import org.apache.doris.analysis.StatementBase;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.View;
import org.apache.doris.nereids.StatementContext;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand;
Expand All @@ -44,7 +44,7 @@ public class LogicalPlanAdapter extends StatementBase implements Queriable {
private final LogicalPlan logicalPlan;
private List<Expr> resultExprs;
private ArrayList<String> colLabels;
private List<TableIf> tables;
private List<View> views;

public LogicalPlanAdapter(LogicalPlan logicalPlan, StatementContext statementContext) {
this.logicalPlan = logicalPlan;
Expand Down Expand Up @@ -81,8 +81,8 @@ public ArrayList<String> getColLabels() {
return colLabels;
}

public List<TableIf> getTables() {
return tables;
public List<View> getViews() {
return views;
}

@Override
Expand All @@ -98,8 +98,8 @@ public void setColLabels(ArrayList<String> colLabels) {
this.colLabels = colLabels;
}

public void setTables(List<TableIf> tables) {
this.tables = tables;
public void setViews(List<View> views) {
this.views = views;
}

public StatementContext getStatementContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ private LogicalPlan getLogicalPlan(TableIf table, UnboundRelation unboundRelatio
case OLAP:
return makeOlapScan(table, unboundRelation, tableQualifier);
case VIEW:
cascadesContext.addView((View) table);
Plan viewPlan = parseAndAnalyzeView(((View) table).getDdlSql(), cascadesContext);
return new LogicalSubQueryAlias<>(tableQualifier, viewPlan);
case HMS_EXTERNAL_TABLE:
Expand Down Expand Up @@ -252,6 +253,9 @@ private Plan parseAndAnalyzeView(String viewSql, CascadesContext parentContext)
parentContext.getStatementContext(), parsedViewPlan, PhysicalProperties.ANY);
viewContext.newAnalyzer().analyze();

for (View view : viewContext.getViews()) {
parentContext.addView(view);
}
// we should remove all group expression of the plan which in other memo, so the groupId would not conflict
return viewContext.getRewritePlan();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.doris.catalog.Partition;
import org.apache.doris.catalog.PartitionType;
import org.apache.doris.catalog.RangePartitionInfo;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.View;
import org.apache.doris.common.Config;
import org.apache.doris.common.Status;
Expand All @@ -50,7 +49,6 @@
import org.apache.doris.thrift.TUniqueId;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -61,6 +59,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Analyze which caching mode a SQL is suitable for
Expand Down Expand Up @@ -191,6 +190,10 @@ public void checkCacheMode(long now) {
cacheMode = innerCheckCacheMode(now);
}

public void checkCacheModeForNereids(long now) {
cacheMode = innerCheckCacheModeForNereids(now);
}

private CacheMode innerCheckCacheMode(long now) {
if (!enableCache()) {
LOG.debug("cache is disabled. queryid {}", DebugUtil.printId(queryId));
Expand Down Expand Up @@ -385,8 +388,8 @@ private CacheMode innerCheckCacheModeForNereids(long now) {
return CacheMode.NoNeed;
}


addAllViewStmtForTblIfs(((LogicalPlanAdapter) parsedStmt).getTables());
allViewStmtSet.addAll(((LogicalPlanAdapter) parsedStmt).getViews()
.stream().map(view -> view.getDdlSql()).collect(Collectors.toSet()));
String allViewExpandStmtListStr = StringUtils.join(allViewStmtSet, "|");

if (now == 0) {
Expand All @@ -395,7 +398,7 @@ private CacheMode innerCheckCacheModeForNereids(long now) {
if (enableSqlCache()
&& (now - latestTable.latestTime) >= Config.cache_last_version_interval_second * 1000L) {
if (LOG.isDebugEnabled()) {
LOG.debug("TIME:{},{},{}", now, latestTable.latestTime,
LOG.debug("Query cache time :{},{},{}", now, latestTable.latestTime,
Config.cache_last_version_interval_second * 1000);
}
cache = new SqlCache(this.queryId, ((LogicalPlanAdapter) parsedStmt).getStatementContext()
Expand Down Expand Up @@ -584,22 +587,7 @@ private CacheTable getSelectedPartitionLastUpdateTime(OlapScanNode node) {
return cacheTable;
}

private void addAllViewStmtForTblIfs(List<TableIf> tblIfs) {
if (CollectionUtils.isEmpty(tblIfs)) {
return;
}
for (TableIf tblIf : tblIfs) {
if (tblIf instanceof View) {
View view = (View) tblIf;
if (!view.isLocalView()) {
allViewStmtSet.add(view.getInlineViewDef());
}
addAllViewStmt(view.getQueryStmt());
}
}
}

private void addAllViewStmtForTblRefs(List<TableRef> tblRefs) {
private void addAllViewStmt(List<TableRef> tblRefs) {
for (TableRef tblRef : tblRefs) {
if (tblRef instanceof InlineViewRef) {
InlineViewRef inlineViewRef = (InlineViewRef) tblRef;
Expand All @@ -619,7 +607,7 @@ private void addAllViewStmtForTblRefs(List<TableRef> tblRefs) {

private void addAllViewStmt(QueryStmt queryStmt) {
if (queryStmt instanceof SelectStmt) {
addAllViewStmtForTblRefs(((SelectStmt) queryStmt).getTableRefs());
addAllViewStmt(((SelectStmt) queryStmt).getTableRefs());
} else if (queryStmt instanceof SetOperationStmt) {
for (SetOperationStmt.SetOperand operand : ((SetOperationStmt) queryStmt).getOperands()) {
addAllViewStmt(operand.getQueryStmt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public void setCacheInfo(CacheAnalyzer.CacheTable latestTable, String allViewExp

public String getSqlWithViewStmt() {
String originSql = selectStmt != null ? selectStmt.toSql() : this.originSql;
return originSql + "|" + allViewExpandStmtListStr;
String cacheKey = originSql + "|" + allViewExpandStmtListStr;
LOG.debug("Cache key: {}", cacheKey);
return cacheKey;
}

public InternalService.PFetchCacheResult getCacheData(Status status) {
Expand Down
112 changes: 110 additions & 2 deletions fe/fe-core/src/test/java/org/apache/doris/qe/PartitionCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.apache.doris.common.Config;
import org.apache.doris.common.UserException;
import org.apache.doris.common.jmockit.Deencapsulation;
import org.apache.doris.common.telemetry.Telemetry;
import org.apache.doris.common.util.SqlParserUtils;
import org.apache.doris.common.util.Util;
import org.apache.doris.datasource.CatalogMgr;
Expand All @@ -54,6 +55,11 @@
import org.apache.doris.mysql.MysqlSerializer;
import org.apache.doris.mysql.privilege.AccessControllerManager;
import org.apache.doris.mysql.privilege.MockedAuth;
import org.apache.doris.nereids.NereidsPlanner;
import org.apache.doris.nereids.StatementContext;
import org.apache.doris.nereids.glue.LogicalPlanAdapter;
import org.apache.doris.nereids.parser.NereidsParser;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.planner.OlapScanNode;
import org.apache.doris.planner.PlanNodeId;
import org.apache.doris.planner.ScanNode;
Expand Down Expand Up @@ -235,6 +241,9 @@ public SystemInfoService getCurrentSystemInfo() {
QueryState state = new QueryState();
channel.reset();

SessionVariable sessionVariable = new SessionVariable();
Deencapsulation.setField(sessionVariable, "beNumber", 1);

new Expectations(channel) {
{
channel.getSerializer();
Expand Down Expand Up @@ -296,7 +305,6 @@ public SystemInfoService getCurrentSystemInfo() {
minTimes = 0;
result = dbName;

SessionVariable sessionVariable = new SessionVariable();
ctx.getSessionVariable();
minTimes = 0;
result = sessionVariable;
Expand All @@ -307,6 +315,18 @@ public SystemInfoService getCurrentSystemInfo() {
ctx.getStmtId();
minTimes = 0;
result = 1L;

ctx.getTracer();
minTimes = 0;
result = Telemetry.getNoopTracer();

ctx.getCurrentCatalog();
minTimes = 0;
result = catalog;

ctx.getCatalog(anyString);
minTimes = 0;
result = catalog;
}
};

Expand Down Expand Up @@ -466,10 +486,14 @@ private OlapTable createEventTable() {

List<Column> column = Lists.newArrayList();
column.add(column1);
column.add(column2);
column.add(column3);
column.add(column4);
column.add(column5);

table.setIndexMeta(new Long(2), "test", column, 1, 1, shortKeyColumnCount, TStorageType.COLUMN,
KeysType.AGG_KEYS);
Deencapsulation.setField(table, "baseIndexId", 1000);
Deencapsulation.setField(table, "baseIndexId", 2);

table.addPartition(part12);
table.addPartition(part13);
Expand Down Expand Up @@ -519,6 +543,24 @@ private ScanNode createEventScanNode(Collection<Long> selectedPartitionIds) {
return node;
}

private StatementBase parseSqlByNereids(String sql) {
StatementBase stmt = null;
try {
LogicalPlan plan = new NereidsParser().parseSingle(sql);
OriginStatement originStatement = new OriginStatement(sql, 0);
StatementContext statementContext = new StatementContext(ctx, originStatement);
NereidsPlanner nereidsPlanner = new NereidsPlanner(statementContext);
LogicalPlanAdapter adapter = new LogicalPlanAdapter(plan, statementContext);
nereidsPlanner.plan(adapter);
statementContext.setParsedStatement(adapter);
stmt = adapter;
} catch (Throwable throwable) {
LOG.warn("Part,an_ex={}", throwable);
Assert.fail(throwable.getMessage());
}
return stmt;
}

private StatementBase parseSql(String sql) {
SqlParser parser = new SqlParser(new SqlScanner(new StringReader(sql)));
StatementBase parseStmt = null;
Expand Down Expand Up @@ -1099,6 +1141,24 @@ public void testSqlCacheKeyWithView() {
+ "FROM appevent WHERE eventdate>=\"2020-01-12\" and eventdate<=\"2020-01-14\" GROUP BY eventdate");
}

@Test
public void testSqlCacheKeyWithViewForNereids() {
Env.getCurrentSystemInfo();
StatementBase parseStmt = parseSql("SELECT * from testDb.view1");
ArrayList<Long> selectedPartitionIds
= Lists.newArrayList(20200112L, 20200113L, 20200114L);
List<ScanNode> scanNodes = Lists.newArrayList(createEventScanNode(selectedPartitionIds));
CacheAnalyzer ca = new CacheAnalyzer(context, parseStmt, scanNodes);
ca.checkCacheModeForNereids(1579053661000L); //2020-1-15 10:01:01
Assert.assertEquals(ca.getCacheMode(), CacheMode.Sql);

SqlCache sqlCache = (SqlCache) ca.getCache();
String cacheKey = sqlCache.getSqlWithViewStmt();
Assert.assertEquals(cacheKey, "SELECT * from testDb.view1"
+ "|select eventdate, COUNT(userid) FROM appevent "
+ "WHERE eventdate>=\"2020-01-12\" and eventdate<=\"2020-01-14\" GROUP BY eventdate");
}

@Test
public void testSqlCacheKeyWithSubSelectView() {
Env.getCurrentSystemInfo();
Expand All @@ -1125,6 +1185,35 @@ public void testSqlCacheKeyWithSubSelectView() {
+ "<= '2020-01-14') origin|select eventdate, userid FROM appevent");
}


@Test
public void testSqlCacheKeyWithSubSelectViewForNereids() {
Env.getCurrentSystemInfo();
StatementBase parseStmt = parseSql(
"select origin.eventdate as eventdate, origin.userid as userid\n"
+ "from (\n"
+ " select view2.eventdate as eventdate, view2.userid as userid \n"
+ " from testDb.view2 view2 \n"
+ " where view2.eventdate >=\"2020-01-12\" and view2.eventdate <= \"2020-01-14\"\n"
+ ") origin"
);
ArrayList<Long> selectedPartitionIds
= Lists.newArrayList(20200112L, 20200113L, 20200114L);
List<ScanNode> scanNodes = Lists.newArrayList(createEventScanNode(selectedPartitionIds));
CacheAnalyzer ca = new CacheAnalyzer(context, parseStmt, scanNodes);
ca.checkCacheModeForNereids(1579053661000L); //2020-1-15 10:01:01
Assert.assertEquals(ca.getCacheMode(), CacheMode.Sql);

SqlCache sqlCache = (SqlCache) ca.getCache();
String cacheKey = sqlCache.getSqlWithViewStmt();
Assert.assertEquals(cacheKey, "select origin.eventdate as eventdate, origin.userid as userid\n"
+ "from (\n"
+ " select view2.eventdate as eventdate, view2.userid as userid \n"
+ " from testDb.view2 view2 \n"
+ " where view2.eventdate >=\"2020-01-12\" and view2.eventdate <= \"2020-01-14\"\n"
+ ") origin" + "|select eventdate, userid FROM appevent");
}

@Test
public void testPartitionCacheKeyWithView() {
Env.getCurrentSystemInfo();
Expand Down Expand Up @@ -1203,6 +1292,25 @@ public void testSqlCacheKeyWithNestedView() {
+ "eventdate<=\"2020-01-14\" GROUP BY eventdate|select eventdate, userid FROM appevent");
}

@Test
public void testSqlCacheKeyWithNestedViewByNereids() {
Env.getCurrentSystemInfo();
StatementBase parseStmt = parseSqlByNereids("SELECT * from testDb.view4");
ArrayList<Long> selectedPartitionIds
= Lists.newArrayList(20200112L, 20200113L, 20200114L);
List<ScanNode> scanNodes = Lists.newArrayList(createEventScanNode(selectedPartitionIds));
CacheAnalyzer ca = new CacheAnalyzer(context, parseStmt, scanNodes);
ca.checkCacheModeForNereids(1579053661000L); //2020-1-15 10:01:01
Assert.assertEquals(ca.getCacheMode(), CacheMode.Sql);

SqlCache sqlCache = (SqlCache) ca.getCache();
String cacheKey = sqlCache.getSqlWithViewStmt();
Assert.assertEquals(cacheKey, "SELECT * from testDb.view4"
+ "|select eventdate, COUNT(userid) FROM view2 " +
"WHERE eventdate>=\"2020-01-12\" and eventdate<=\"2020-01-14\" GROUP BY eventdate"
+ "|select eventdate, userid FROM appevent");
}

@Test
public void testCacheLocalViewMultiOperand() {
Env.getCurrentSystemInfo();
Expand Down

0 comments on commit d44c17b

Please sign in to comment.