|
22 | 22 | import org.apache.doris.analysis.LiteralExpr; |
23 | 23 | import org.apache.doris.analysis.StatementBase; |
24 | 24 | import org.apache.doris.catalog.Column; |
| 25 | +import org.apache.doris.catalog.Env; |
25 | 26 | import org.apache.doris.catalog.MTMV; |
26 | 27 | import org.apache.doris.common.NereidsException; |
27 | 28 | import org.apache.doris.common.Pair; |
|
54 | 55 | import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; |
55 | 56 | import org.apache.doris.nereids.trees.plans.logical.LogicalSqlCache; |
56 | 57 | import org.apache.doris.nereids.trees.plans.physical.PhysicalCatalogRelation; |
| 58 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalEmptyRelation; |
57 | 59 | import org.apache.doris.nereids.trees.plans.physical.PhysicalOneRowRelation; |
58 | 60 | import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan; |
59 | 61 | import org.apache.doris.nereids.trees.plans.physical.PhysicalResultSink; |
|
66 | 68 | import org.apache.doris.qe.ConnectContext; |
67 | 69 | import org.apache.doris.qe.ResultSet; |
68 | 70 | import org.apache.doris.qe.ResultSetMetaData; |
| 71 | +import org.apache.doris.qe.cache.CacheAnalyzer; |
69 | 72 |
|
70 | 73 | import com.google.common.annotations.VisibleForTesting; |
| 74 | +import com.google.common.collect.ImmutableList; |
71 | 75 | import com.google.common.collect.Lists; |
72 | 76 | import org.apache.logging.log4j.LogManager; |
73 | 77 | import org.apache.logging.log4j.Logger; |
@@ -168,8 +172,9 @@ public Plan plan(LogicalPlan plan, PhysicalProperties requireProperties, |
168 | 172 | LogicalSqlCache logicalSqlCache = (LogicalSqlCache) plan; |
169 | 173 | physicalPlan = new PhysicalSqlCache( |
170 | 174 | logicalSqlCache.getQueryId(), logicalSqlCache.getColumnLabels(), |
171 | | - logicalSqlCache.getResultExprs(), logicalSqlCache.getCacheValues(), |
172 | | - logicalSqlCache.getBackendAddress(), logicalSqlCache.getPlanBody() |
| 175 | + logicalSqlCache.getResultExprs(), logicalSqlCache.getResultSetInFe(), |
| 176 | + logicalSqlCache.getCacheValues(), logicalSqlCache.getBackendAddress(), |
| 177 | + logicalSqlCache.getPlanBody() |
173 | 178 | ); |
174 | 179 | return physicalPlan; |
175 | 180 | } |
@@ -528,31 +533,66 @@ public Optional<ResultSet> handleQueryInFe(StatementBase parsedStmt) { |
528 | 533 | if (!(parsedStmt instanceof LogicalPlanAdapter)) { |
529 | 534 | return Optional.empty(); |
530 | 535 | } |
531 | | - if (!(physicalPlan instanceof PhysicalResultSink)) { |
532 | | - return Optional.empty(); |
| 536 | + if (physicalPlan instanceof PhysicalSqlCache |
| 537 | + && ((PhysicalSqlCache) physicalPlan).getResultSet().isPresent()) { |
| 538 | + return Optional.of(((PhysicalSqlCache) physicalPlan).getResultSet().get()); |
533 | 539 | } |
534 | | - if (!(((PhysicalResultSink<?>) physicalPlan).child() instanceof PhysicalOneRowRelation)) { |
| 540 | + if (!(physicalPlan instanceof PhysicalResultSink)) { |
535 | 541 | return Optional.empty(); |
536 | 542 | } |
537 | | - PhysicalOneRowRelation physicalOneRowRelation |
538 | | - = (PhysicalOneRowRelation) ((PhysicalResultSink<?>) physicalPlan).child(); |
539 | | - List<Column> columns = Lists.newArrayList(); |
540 | | - List<String> data = Lists.newArrayList(); |
541 | | - for (int i = 0; i < physicalOneRowRelation.getProjects().size(); i++) { |
542 | | - NamedExpression item = physicalOneRowRelation.getProjects().get(i); |
543 | | - NamedExpression output = physicalPlan.getOutput().get(i); |
544 | | - Expression expr = item.child(0); |
545 | | - if (expr instanceof Literal) { |
546 | | - LiteralExpr legacyExpr = ((Literal) expr).toLegacyLiteral(); |
| 543 | + |
| 544 | + Optional<SqlCacheContext> sqlCacheContext = statementContext.getSqlCacheContext(); |
| 545 | + boolean enableSqlCache |
| 546 | + = CacheAnalyzer.canUseSqlCache(statementContext.getConnectContext().getSessionVariable()); |
| 547 | + Plan child = physicalPlan.child(0); |
| 548 | + if (child instanceof PhysicalOneRowRelation) { |
| 549 | + PhysicalOneRowRelation physicalOneRowRelation = (PhysicalOneRowRelation) physicalPlan.child(0); |
| 550 | + List<Column> columns = Lists.newArrayList(); |
| 551 | + List<String> data = Lists.newArrayList(); |
| 552 | + for (int i = 0; i < physicalOneRowRelation.getProjects().size(); i++) { |
| 553 | + NamedExpression item = physicalOneRowRelation.getProjects().get(i); |
| 554 | + NamedExpression output = physicalPlan.getOutput().get(i); |
| 555 | + Expression expr = item.child(0); |
| 556 | + if (expr instanceof Literal) { |
| 557 | + LiteralExpr legacyExpr = ((Literal) expr).toLegacyLiteral(); |
| 558 | + columns.add(new Column(output.getName(), output.getDataType().toCatalogDataType())); |
| 559 | + data.add(legacyExpr.getStringValueInFe()); |
| 560 | + } else { |
| 561 | + return Optional.empty(); |
| 562 | + } |
| 563 | + } |
| 564 | + |
| 565 | + ResultSetMetaData metadata = new CommonResultSet.CommonResultSetMetaData(columns); |
| 566 | + ResultSet resultSet = new CommonResultSet(metadata, Collections.singletonList(data)); |
| 567 | + if (sqlCacheContext.isPresent() && enableSqlCache) { |
| 568 | + sqlCacheContext.get().setResultSetInFe(resultSet); |
| 569 | + Env.getCurrentEnv().getSqlCacheManager().tryAddFeSqlCache( |
| 570 | + statementContext.getConnectContext(), |
| 571 | + statementContext.getOriginStatement().originStmt |
| 572 | + ); |
| 573 | + } |
| 574 | + return Optional.of(resultSet); |
| 575 | + } else if (child instanceof PhysicalEmptyRelation) { |
| 576 | + List<Column> columns = Lists.newArrayList(); |
| 577 | + PhysicalEmptyRelation physicalEmptyRelation = (PhysicalEmptyRelation) physicalPlan.child(0); |
| 578 | + for (int i = 0; i < physicalEmptyRelation.getProjects().size(); i++) { |
| 579 | + NamedExpression output = physicalPlan.getOutput().get(i); |
547 | 580 | columns.add(new Column(output.getName(), output.getDataType().toCatalogDataType())); |
548 | | - data.add(legacyExpr.getStringValueInFe()); |
549 | | - } else { |
550 | | - return Optional.empty(); |
551 | 581 | } |
| 582 | + |
| 583 | + ResultSetMetaData metadata = new CommonResultSet.CommonResultSetMetaData(columns); |
| 584 | + ResultSet resultSet = new CommonResultSet(metadata, ImmutableList.of()); |
| 585 | + if (sqlCacheContext.isPresent() && enableSqlCache) { |
| 586 | + sqlCacheContext.get().setResultSetInFe(resultSet); |
| 587 | + Env.getCurrentEnv().getSqlCacheManager().tryAddFeSqlCache( |
| 588 | + statementContext.getConnectContext(), |
| 589 | + statementContext.getOriginStatement().originStmt |
| 590 | + ); |
| 591 | + } |
| 592 | + return Optional.of(resultSet); |
| 593 | + } else { |
| 594 | + return Optional.empty(); |
552 | 595 | } |
553 | | - ResultSetMetaData metadata = new CommonResultSet.CommonResultSetMetaData(columns); |
554 | | - ResultSet resultSet = new CommonResultSet(metadata, Collections.singletonList(data)); |
555 | | - return Optional.of(resultSet); |
556 | 596 | } |
557 | 597 |
|
558 | 598 | @VisibleForTesting |
|
0 commit comments