Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d7a3947
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 13, 2024
0f67d8f
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 20, 2024
fa35f3f
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 21, 2024
ffd501f
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 22, 2024
2f2bc30
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 23, 2024
b5ebeeb
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 23, 2024
a3c470d
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 24, 2024
4cf6fdd
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 25, 2024
4922b79
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 25, 2024
ba0a2ab
Merge branch 'master' into create_view
feiniaofeiafei Mar 25, 2024
29a8a3d
Merge branch 'master' into create_view
feiniaofeiafei Mar 29, 2024
6bb25b1
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 26, 2024
3b9a8ac
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 29, 2024
2c66c18
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 31, 2024
4a941dd
[feat](nereids) support create view in nereids
feiniaofeiafei Mar 31, 2024
2e40f0f
[feat](nereids) support create view in nereids
feiniaofeiafei Apr 1, 2024
9bb9a7e
Merge branch 'master' into create_view
feiniaofeiafei Apr 1, 2024
32ba0a7
[feat](nereids) support create view in nereids
feiniaofeiafei Apr 1, 2024
730f4fb
Merge branch 'master' into create_view
feiniaofeiafei Apr 1, 2024
c8ba826
[feat](nereids) support create view in nereids
feiniaofeiafei Apr 1, 2024
c9ab703
[feat](nereids) support create view in nereids
feiniaofeiafei Apr 1, 2024
1f6a10e
[feat](nereids) support create view in nereids
feiniaofeiafei Apr 2, 2024
2441c00
[feat](nereids) support create view in nereids
feiniaofeiafei Apr 3, 2024
129c8ee
Merge branch 'master' into create_view
feiniaofeiafei Apr 3, 2024
2d75e08
[feat](nereids) support create view in nereids
feiniaofeiafei Apr 3, 2024
65aaab7
[feat](nereids) support create view in nereids
feiniaofeiafei Apr 8, 2024
ab8ade1
[feat](nereids) support create view in nereids
feiniaofeiafei Apr 8, 2024
bf7dee4
[feat](nereids) support create view in nereids
feiniaofeiafei Apr 8, 2024
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 @@ -60,6 +60,9 @@ statementBase
properties=propertyClause?
(BROKER extProperties=propertyClause)?
(AS query)? #createTable
| CREATE VIEW (IF NOT EXISTS)? name=multipartIdentifier
(LEFT_PAREN cols=simpleColumnDefs RIGHT_PAREN)?
(COMMENT STRING_LITERAL)? AS query #createView
| explain? INSERT (INTO | OVERWRITE TABLE)
(tableName=multipartIdentifier | DORIS_INTERNAL_TABLE_ID LEFT_PAREN tableId=INTEGER_VALUE RIGHT_PAREN)
partitionSpec? // partition define
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.doris.common.UserException;
import org.apache.doris.common.util.ToSqlContext;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.logging.log4j.LogManager;
Expand All @@ -50,7 +49,6 @@ public class BaseViewStmt extends DdlStmt {
protected QueryStmt cloneStmt;

public BaseViewStmt(TableName tableName, List<ColWithComment> cols, QueryStmt queryStmt) {
Preconditions.checkNotNull(queryStmt);
this.tableName = tableName;
this.cols = cols;
this.viewDefStmt = queryStmt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.doris.analysis;

import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.Env;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
Expand Down Expand Up @@ -92,4 +93,12 @@ public void analyze(Analyzer analyzer) throws UserException {
}
}
}

public void setInlineViewDef(String querySql) {
inlineViewDef = querySql;
}

public void setFinalColumns(List<Column> columns) {
finalCols.addAll(columns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.annotation.concurrent.GuardedBy;

/**
Expand Down Expand Up @@ -121,6 +122,11 @@ public class StatementContext {

private BitSet disableRules;

// for create view support in nereids
// key is the start and end position of the sql substring that needs to be replaced,
// and value is the new string used for replacement.
private TreeMap<Pair<Integer, Integer>, String> indexInSqlToString = new TreeMap<>(new Pair.PairComparator<>());

public StatementContext() {
this.connectContext = ConnectContext.get();
}
Expand Down Expand Up @@ -354,4 +360,12 @@ public boolean isHasUnknownColStats() {
public void setHasUnknownColStats(boolean hasUnknownColStats) {
this.hasUnknownColStats = hasUnknownColStats;
}

public TreeMap<Pair<Integer, Integer>, String> getIndexInSqlToString() {
return indexInSqlToString;
}

public void addIndexInSqlToString(Pair<Integer, Integer> pair, String replacement) {
indexInSqlToString.put(pair, replacement);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.doris.nereids.analyzer;

import org.apache.doris.analysis.TableScanParams;
import org.apache.doris.common.Pair;
import org.apache.doris.nereids.exceptions.UnboundException;
import org.apache.doris.nereids.memo.GroupExpression;
import org.apache.doris.nereids.properties.LogicalProperties;
Expand Down Expand Up @@ -55,35 +56,44 @@ public class UnboundRelation extends LogicalRelation implements Unbound, BlockFu
private final Optional<TableSample> tableSample;
private final Optional<String> indexName;
private TableScanParams scanParams;
// the start and end position of the sql substring(e.g. "t1", "db1.t1", "ctl1.db1.t1")
private final Optional<Pair<Integer, Integer>> indexInSqlString;

public UnboundRelation(RelationId id, List<String> nameParts) {
this(id, nameParts, Optional.empty(), Optional.empty(), ImmutableList.of(), false, ImmutableList.of(),
ImmutableList.of(), Optional.empty(), Optional.empty(), null);
ImmutableList.of(), Optional.empty(), Optional.empty(), null, Optional.empty());
}

public UnboundRelation(RelationId id, List<String> nameParts, List<String> partNames, boolean isTempPart) {
this(id, nameParts, Optional.empty(), Optional.empty(), partNames, isTempPart, ImmutableList.of(),
ImmutableList.of(), Optional.empty(), Optional.empty(), null);
ImmutableList.of(), Optional.empty(), Optional.empty(), null, Optional.empty());
}

public UnboundRelation(RelationId id, List<String> nameParts, List<String> partNames, boolean isTempPart,
List<Long> tabletIds, List<String> hints, Optional<TableSample> tableSample, Optional<String> indexName) {
this(id, nameParts, Optional.empty(), Optional.empty(),
partNames, isTempPart, tabletIds, hints, tableSample, indexName, null);
partNames, isTempPart, tabletIds, hints, tableSample, indexName, null, Optional.empty());
}

public UnboundRelation(RelationId id, List<String> nameParts, List<String> partNames, boolean isTempPart,
List<Long> tabletIds, List<String> hints, Optional<TableSample> tableSample, Optional<String> indexName,
TableScanParams scanParams) {
this(id, nameParts, Optional.empty(), Optional.empty(),
partNames, isTempPart, tabletIds, hints, tableSample, indexName, scanParams);
partNames, isTempPart, tabletIds, hints, tableSample, indexName, scanParams, Optional.empty());
}

public UnboundRelation(RelationId id, List<String> nameParts, Optional<GroupExpression> groupExpression,
Optional<LogicalProperties> logicalProperties, List<String> partNames, boolean isTempPart,
List<Long> tabletIds, List<String> hints, Optional<TableSample> tableSample, Optional<String> indexName) {
this(id, nameParts, groupExpression, logicalProperties, partNames,
isTempPart, tabletIds, hints, tableSample, indexName, null);
isTempPart, tabletIds, hints, tableSample, indexName, null, Optional.empty());
}

public UnboundRelation(RelationId id, List<String> nameParts, List<String> partNames, boolean isTempPart,
List<Long> tabletIds, List<String> hints, Optional<TableSample> tableSample, Optional<String> indexName,
TableScanParams scanParams, Optional<Pair<Integer, Integer>> indexInSqlString) {
this(id, nameParts, Optional.empty(), Optional.empty(),
partNames, isTempPart, tabletIds, hints, tableSample, indexName, scanParams, indexInSqlString);
}

/**
Expand All @@ -92,7 +102,7 @@ public UnboundRelation(RelationId id, List<String> nameParts, Optional<GroupExpr
public UnboundRelation(RelationId id, List<String> nameParts, Optional<GroupExpression> groupExpression,
Optional<LogicalProperties> logicalProperties, List<String> partNames, boolean isTempPart,
List<Long> tabletIds, List<String> hints, Optional<TableSample> tableSample, Optional<String> indexName,
TableScanParams scanParams) {
TableScanParams scanParams, Optional<Pair<Integer, Integer>> indexInSqlString) {
super(id, PlanType.LOGICAL_UNBOUND_RELATION, groupExpression, logicalProperties);
this.nameParts = ImmutableList.copyOf(Objects.requireNonNull(nameParts, "nameParts should not null"));
this.partNames = ImmutableList.copyOf(Objects.requireNonNull(partNames, "partNames should not null"));
Expand All @@ -102,6 +112,7 @@ public UnboundRelation(RelationId id, List<String> nameParts, Optional<GroupExpr
this.tableSample = tableSample;
this.indexName = indexName;
this.scanParams = scanParams;
this.indexInSqlString = indexInSqlString;
}

public List<String> getNameParts() {
Expand All @@ -122,14 +133,14 @@ public LogicalProperties computeLogicalProperties() {
public Plan withGroupExpression(Optional<GroupExpression> groupExpression) {
return new UnboundRelation(relationId, nameParts,
groupExpression, Optional.of(getLogicalProperties()),
partNames, isTempPart, tabletIds, hints, tableSample, indexName, null);
partNames, isTempPart, tabletIds, hints, tableSample, indexName, null, indexInSqlString);
}

@Override
public Plan withGroupExprLogicalPropChildren(Optional<GroupExpression> groupExpression,
Optional<LogicalProperties> logicalProperties, List<Plan> children) {
return new UnboundRelation(relationId, nameParts, groupExpression, logicalProperties, partNames,
isTempPart, tabletIds, hints, tableSample, indexName, null);
isTempPart, tabletIds, hints, tableSample, indexName, null, indexInSqlString);
}

@Override
Expand Down Expand Up @@ -187,4 +198,8 @@ public Optional<TableSample> getTableSample() {
public TableScanParams getScanParams() {
return scanParams;
}

public Optional<Pair<Integer, Integer>> getIndexInSqlString() {
return indexInSqlString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.doris.nereids.analyzer;

import org.apache.doris.common.Pair;
import org.apache.doris.nereids.exceptions.UnboundException;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
Expand All @@ -28,17 +29,26 @@

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* Star expression.
*/
public class UnboundStar extends NamedExpression implements LeafExpression, Unbound, PropagateNullable {

private final List<String> qualifier;
// the start and end position of the sql substring(e.g. "*", "table.*")
private final Optional<Pair<Integer, Integer>> indexInSqlString;

public UnboundStar(List<String> qualifier) {
super(ImmutableList.of());
this.qualifier = Objects.requireNonNull(ImmutableList.copyOf(qualifier), "qualifier can not be null");
this.indexInSqlString = Optional.empty();
}

public UnboundStar(List<String> qualifier, Optional<Pair<Integer, Integer>> indexInSqlString) {
super(ImmutableList.of());
this.qualifier = Objects.requireNonNull(ImmutableList.copyOf(qualifier), "qualifier can not be null");
this.indexInSqlString = indexInSqlString;
}

@Override
Expand Down Expand Up @@ -71,6 +81,10 @@ public boolean equals(Object o) {
return qualifier.equals(that.qualifier);
}

public Optional<Pair<Integer, Integer>> getIndexInSqlString() {
return indexInSqlString;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), qualifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import org.apache.doris.nereids.DorisParser.CreateProcedureContext;
import org.apache.doris.nereids.DorisParser.CreateRowPolicyContext;
import org.apache.doris.nereids.DorisParser.CreateTableContext;
import org.apache.doris.nereids.DorisParser.CreateViewContext;
import org.apache.doris.nereids.DorisParser.CteContext;
import org.apache.doris.nereids.DorisParser.DataTypeWithNullableContext;
import org.apache.doris.nereids.DorisParser.DateCeilContext;
Expand Down Expand Up @@ -360,6 +361,7 @@
import org.apache.doris.nereids.trees.plans.commands.CreatePolicyCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateTableCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateViewCommand;
import org.apache.doris.nereids.trees.plans.commands.DeleteFromCommand;
import org.apache.doris.nereids.trees.plans.commands.DeleteFromUsingCommand;
import org.apache.doris.nereids.trees.plans.commands.DropConstraintCommand;
Expand All @@ -386,6 +388,7 @@
import org.apache.doris.nereids.trees.plans.commands.info.ColumnDefinition;
import org.apache.doris.nereids.trees.plans.commands.info.CreateMTMVInfo;
import org.apache.doris.nereids.trees.plans.commands.info.CreateTableInfo;
import org.apache.doris.nereids.trees.plans.commands.info.CreateViewInfo;
import org.apache.doris.nereids.trees.plans.commands.info.DMLCommandType;
import org.apache.doris.nereids.trees.plans.commands.info.DefaultValue;
import org.apache.doris.nereids.trees.plans.commands.info.DistributionDescriptor;
Expand Down Expand Up @@ -472,6 +475,15 @@
*/
@SuppressWarnings({"OptionalUsedAsFieldOrParameterType", "OptionalGetWithoutIsPresent"})
public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {
private final boolean forCreateView;

public LogicalPlanBuilder() {
forCreateView = false;
}

public LogicalPlanBuilder(boolean forCreateView) {
this.forCreateView = forCreateView;
}

@SuppressWarnings("unchecked")
protected <T> T typedVisit(ParseTree ctx) {
Expand Down Expand Up @@ -1329,11 +1341,16 @@ public LogicalPlan visitTableName(TableNameContext ctx) {
scanParams = new TableScanParams(ctx.optScanParams().funcName.getText(), map);
}

MultipartIdentifierContext identifier = ctx.multipartIdentifier();
TableSample tableSample = ctx.sample() == null ? null : (TableSample) visit(ctx.sample());
LogicalPlan checkedRelation = LogicalPlanBuilderAssistant.withCheckPolicy(
UnboundRelation relation = forCreateView ? new UnboundRelation(StatementScopeIdGenerator.newRelationId(),
tableId, partitionNames, isTempPart, tabletIdLists, relationHints,
Optional.ofNullable(tableSample), indexName, scanParams,
Optional.of(Pair.of(identifier.start.getStartIndex(), identifier.stop.getStopIndex()))) :
new UnboundRelation(StatementScopeIdGenerator.newRelationId(),
tableId, partitionNames, isTempPart, tabletIdLists, relationHints,
Optional.ofNullable(tableSample), indexName, scanParams));
Optional.ofNullable(tableSample), indexName, scanParams);
LogicalPlan checkedRelation = LogicalPlanBuilderAssistant.withCheckPolicy(relation);
LogicalPlan plan = withTableAlias(checkedRelation, ctx.tableAlias());
for (LateralViewContext lateralViewContext : ctx.lateralView()) {
plan = withGenerate(plan, lateralViewContext);
Expand Down Expand Up @@ -1382,7 +1399,9 @@ public Expression visitStar(StarContext ctx) {
} else {
target = ImmutableList.of();
}
return new UnboundStar(target);
return forCreateView
? new UnboundStar(target, Optional.of(Pair.of(ctx.start.getStartIndex(), ctx.stop.getStopIndex())))
: new UnboundStar(target);
});
}

Expand Down Expand Up @@ -2413,6 +2432,19 @@ private LogicalPlan plan(ParserRuleContext tree) {
* create table parsing
* ******************************************************************************************** */

@Override
public LogicalPlan visitCreateView(CreateViewContext ctx) {
List<String> nameParts = visitMultipartIdentifier(ctx.name);
String comment = ctx.STRING_LITERAL() == null ? "" : LogicalPlanBuilderAssistant.escapeBackSlash(
ctx.STRING_LITERAL().getText().substring(1, ctx.STRING_LITERAL().getText().length() - 1));
LogicalPlan logicalPlan = visitQuery(ctx.query());
String querySql = getOriginSql(ctx.query());
CreateViewInfo info = new CreateViewInfo(ctx.EXISTS() != null, new TableNameInfo(nameParts),
comment, logicalPlan, querySql,
ctx.cols == null ? Lists.newArrayList() : visitSimpleColumnDefs(ctx.cols));
return new CreateViewCommand(info);
}

@Override
public LogicalPlan visitCreateTable(CreateTableContext ctx) {
String ctlName = null;
Expand Down Expand Up @@ -2858,8 +2890,11 @@ private LogicalPlan withSelectQuerySpecification(
if (!expressions.stream().allMatch(UnboundSlot.class::isInstance)) {
throw new ParseException("only column name is supported in except clause", selectColumnCtx);
}
project = new LogicalProject<>(ImmutableList.of(new UnboundStar(ImmutableList.of())),
expressions, isDistinct, aggregate);
UnboundStar star = forCreateView ? new UnboundStar(ImmutableList.of(),
Optional.of(Pair.of(selectColumnCtx.start.getStartIndex(),
selectColumnCtx.stop.getStopIndex())))
: new UnboundStar(ImmutableList.of());
project = new LogicalProject<>(ImmutableList.of(star), expressions, isDistinct, aggregate);
} else {
List<NamedExpression> projects = getNamedExpressions(selectColumnCtx.namedExpressionSeq());
project = new LogicalProject<>(projects, ImmutableList.of(), isDistinct, aggregate);
Expand Down Expand Up @@ -3038,8 +3073,10 @@ private LogicalPlan withProjection(LogicalPlan input, SelectColumnClauseContext
if (!expressions.stream().allMatch(UnboundSlot.class::isInstance)) {
throw new ParseException("only column name is supported in except clause", selectCtx);
}
return new LogicalProject<>(ImmutableList.of(new UnboundStar(ImmutableList.of())),
expressions, isDistinct, input);
UnboundStar star = forCreateView ? new UnboundStar(ImmutableList.of(),
Optional.of(Pair.of(selectCtx.start.getStartIndex(), selectCtx.stop.getStopIndex()))) :
new UnboundStar(ImmutableList.of());
return new LogicalProject<>(ImmutableList.of(star), expressions, isDistinct, input);
} else {
List<NamedExpression> projects = getNamedExpressions(selectCtx.namedExpressionSeq());
return new LogicalProject<>(projects, Collections.emptyList(), isDistinct, input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,20 @@ private <T> T parse(String sql, @Nullable LogicalPlanBuilder logicalPlanBuilder,
return (T) realLogicalPlanBuilder.visit(tree);
}

private ParserRuleContext toAst(String sql, Function<DorisParser, ParserRuleContext> parseFunction) {
public LogicalPlan parseForCreateView(String sql) {
return parseForCreateViewInternal(sql, null, DorisParser::singleStatement);
}

private <T> T parseForCreateViewInternal(String sql, @Nullable LogicalPlanBuilder logicalPlanBuilder,
Function<DorisParser, ParserRuleContext> parseFunction) {
ParserRuleContext tree = toAst(sql, parseFunction);
LogicalPlanBuilder realLogicalPlanBuilder = logicalPlanBuilder == null
? new LogicalPlanBuilder(true) : logicalPlanBuilder;
return (T) realLogicalPlanBuilder.visit(tree);
}

/** toAst */
public static ParserRuleContext toAst(String sql, Function<DorisParser, ParserRuleContext> parseFunction) {
DorisLexer lexer = new DorisLexer(new CaseInsensitiveStream(CharStreams.fromString(sql)));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
DorisParser parser = new DorisParser(tokenStream);
Expand Down
Loading