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

[feature](schema) support temporary table like mysql #40680

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,28 @@ supportedCreateStatement
properties=propertyClause?
(BROKER extProperties=propertyClause)?
(AS query)? #createTable
| CREATE (TEMPORARY)? TABLE (IF NOT EXISTS)? name=multipartIdentifier
((ctasCols=identifierList)? | (LEFT_PAREN columnDefs (COMMA indexDefs)? COMMA? RIGHT_PAREN))
(ENGINE EQ engine=identifier)?
((AGGREGATE | UNIQUE | DUPLICATE) KEY keys=identifierList
(CLUSTER BY clusterKeys=identifierList)?)?
(COMMENT STRING_LITERAL)?
(partition=partitionTable)?
(DISTRIBUTED BY (HASH hashKeys=identifierList | RANDOM)
(BUCKETS (INTEGER_VALUE | autoBucket=AUTO))?)?
(ROLLUP LEFT_PAREN rollupDefs RIGHT_PAREN)?
properties=propertyClause?
(BROKER extProperties=propertyClause)?
(AS query)? #createTable
| CREATE (OR REPLACE)? VIEW (IF NOT EXISTS)? name=multipartIdentifier
(LEFT_PAREN cols=simpleColumnDefs RIGHT_PAREN)?
(COMMENT STRING_LITERAL)? AS query #createView
| CREATE (EXTERNAL)? TABLE (IF NOT EXISTS)? name=multipartIdentifier
LIKE existedTable=multipartIdentifier
(WITH ROLLUP (rollupNames=identifierList)?)? #createTableLike
| CREATE (TEMPORARY)? TABLE (IF NOT EXISTS)? name=multipartIdentifier
LIKE existedTable=multipartIdentifier
(WITH ROLLUP (rollupNames=identifierList)?)? #createTableLike
| CREATE ROW POLICY (IF NOT EXISTS)? name=identifier
ON table=multipartIdentifier
AS type=(RESTRICTIVE | PERMISSIVE)
Expand Down
2 changes: 2 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/DorisFE.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ public static void start(String dorisHomeDir, String pidDir, String[] args, Star
Env.getCurrentEnv().initialize(args);
Env.getCurrentEnv().waitForReady();

Env.getCurrentEnv().cleanPhantomTempTable();

// init and start:
// 1. HttpServer for HTTP Server
// 2. FeServer for Thrift Server
Expand Down
2 changes: 1 addition & 1 deletion fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void processCreateMaterializedView(CreateMaterializedViewStmt stmt)
Database db = Env.getCurrentInternalCatalog().getDbOrDdlException(dbName);
Env.getCurrentInternalCatalog().checkAvailableCapacity(db);

OlapTable olapTable = (OlapTable) db.getTableOrMetaException(tableName, TableType.OLAP);
OlapTable olapTable = (OlapTable) db.getNonTempTableOrMetaException(tableName, TableType.OLAP);
((MaterializedViewHandler) materializedViewHandler).processCreateMaterializedView(stmt, db, olapTable);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public CreateMTMVStmt(boolean ifNotExists, TableName mvName, List<Column> column
MTMVRefreshInfo refreshInfo, KeysDesc keyDesc, DistributionDesc distributionDesc,
Map<String, String> properties, Map<String, String> mvProperties, String querySql, String comment,
PartitionDesc partitionDesc, MTMVPartitionInfo mvPartitionInfo, MTMVRelation relation) {
super(ifNotExists, false, mvName, columns, new ArrayList<Index>(), DEFAULT_ENGINE_NAME, keyDesc, partitionDesc,
distributionDesc, properties, null, comment, null, null);
super(ifNotExists, false, false, mvName, columns, new ArrayList<Index>(), DEFAULT_ENGINE_NAME, keyDesc,
partitionDesc, distributionDesc, properties, null, comment, null, null);
this.refreshInfo = refreshInfo;
this.querySql = querySql;
this.mvProperties = mvProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,21 @@ public class CreateTableLikeStmt extends DdlStmt implements NotFallbackInParser
private static final Logger LOG = LogManager.getLogger(CreateTableLikeStmt.class);

private final boolean ifNotExists;
private final boolean isTemp;
private final TableName tableName;
private final TableName existedTableName;
private final ArrayList<String> rollupNames;
private final boolean withAllRollup;

public CreateTableLikeStmt(boolean ifNotExists, TableName tableName, TableName existedTableName,
ArrayList<String> rollupNames, boolean withAllRollup) throws DdlException {
this(ifNotExists, false, tableName, existedTableName, rollupNames, withAllRollup);
}

public CreateTableLikeStmt(boolean ifNotExists, boolean isTemp, TableName tableName, TableName existedTableName,
ArrayList<String> rollupNames, boolean withAllRollup) throws DdlException {
this.ifNotExists = ifNotExists;
this.isTemp = isTemp;
this.tableName = tableName;
this.existedTableName = existedTableName;
if (!CollectionUtils.isEmpty(rollupNames) && withAllRollup) {
Expand All @@ -65,6 +72,10 @@ public boolean isIfNotExists() {
return ifNotExists;
}

public boolean isTemp() {
return isTemp;
}

public String getDbName() {
return tableName.getDb();
}
Expand Down Expand Up @@ -115,7 +126,11 @@ public void analyze(Analyzer analyzer) throws UserException {
@Override
public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append("CREATE TABLE ").append(tableName.toSql()).append(" LIKE ").append(existedTableName.toSql());
sb.append("CREATE ");
if (isTemp) {
sb.append("TEMPORARY ");
}
sb.append("TABLE ").append(tableName.toSql()).append(" LIKE ").append(existedTableName.toSql());
if (withAllRollup && CollectionUtils.isEmpty(rollupNames)) {
sb.append(" WITH ROLLUP");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class CreateTableStmt extends DdlStmt implements NotFallbackInParser {

protected boolean ifNotExists;
private boolean isExternal;
private boolean isTemp;
protected TableName tableName;
protected List<ColumnDef> columnDefs;
private List<IndexDef> indexDefs;
Expand Down Expand Up @@ -187,12 +188,14 @@ public CreateTableStmt(boolean ifNotExists, boolean isExternal, TableName tableN
}

// for Nereids
public CreateTableStmt(boolean ifNotExists, boolean isExternal, TableName tableName, List<Column> columns,
List<Index> indexes, String engineName, KeysDesc keysDesc, PartitionDesc partitionDesc,
DistributionDesc distributionDesc, Map<String, String> properties, Map<String, String> extProperties,
String comment, List<AlterClause> rollupAlterClauseList, Void unused) {
public CreateTableStmt(boolean ifNotExists, boolean isExternal, boolean isTemp, TableName tableName,
List<Column> columns, List<Index> indexes, String engineName, KeysDesc keysDesc,
PartitionDesc partitionDesc, DistributionDesc distributionDesc, Map<String, String> properties,
Map<String, String> extProperties, String comment,
List<AlterClause> rollupAlterClauseList, Void unused) {
this.ifNotExists = ifNotExists;
this.isExternal = isExternal;
this.isTemp = isTemp;
this.tableName = tableName;
this.columns = columns;
this.indexes = indexes;
Expand Down Expand Up @@ -224,6 +227,14 @@ public boolean isExternal() {
return isExternal;
}

public boolean isTemp() {
return isTemp;
}

public void setTemp(boolean temp) {
isTemp = temp;
}

public TableName getDbTbl() {
return tableName;
}
Expand Down Expand Up @@ -652,6 +663,9 @@ public String toSql() {
StringBuilder sb = new StringBuilder();

sb.append("CREATE ");
if (isTemp) {
sb.append("TEMPORARY ");
}
if (isExternal) {
sb.append("EXTERNAL ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ public int compare(Table t1, Table t2) {
PrivPredicate.SHOW)) {
continue;
}
if (table.getType() == TableType.TEMP && Util.getTempTableConnectionId(table.getName())
!= ConnectContext.get().getConnectionId()) {
continue;
}
sortedTables.add(table);
}

Expand All @@ -196,7 +200,13 @@ public int compare(Table t1, Table t2) {
remoteSize = olapTable.getRemoteDataSize();

//|TableName|Size|ReplicaCount|RemoteSize
List<Object> row = Arrays.asList(table.getName(), tableSize, replicaCount, remoteSize);
List<Object> row;
if (table.getType() == TableType.TEMP) {
row = Arrays.asList(Util.getTempTableOuterName(table.getName()), tableSize,
replicaCount, remoteSize);
} else {
row = Arrays.asList(table.getName(), tableSize, replicaCount, remoteSize);
}
totalRowsObject.add(row);

totalSize += tableSize;
Expand Down Expand Up @@ -299,7 +309,9 @@ public int compare(Table t1, Table t2) {
String indexName = olapTable.getIndexNameById(indexId);
// .add("TableName").add("IndexName").add("Size").add("ReplicaCount").add("RowCount")
// .add("RemoteSize")
List<Object> row = Arrays.asList(tableName, indexName, indexSize, indexReplicaCount,
String tableShowName = olapTable.getType() == TableType.TEMP
? Util.getTempTableOuterName(tableName.getTbl()) : tableName.getTbl();
List<Object> row = Arrays.asList(tableShowName, indexName, indexSize, indexReplicaCount,
indexRowCount, indexRemoteSize);
totalRowsObject.add(row);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.UserException;
import org.apache.doris.common.util.Util;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.ShowResultSetMetaData;
Expand Down Expand Up @@ -137,7 +138,14 @@ public void analyze(Analyzer analyzer) throws UserException {
stats.forEach((tableName, queryHit) -> {
if (Env.getCurrentEnv().getAccessManager()
.checkTblPriv(ConnectContext.get(), ctlName, dbName, tableName, PrivPredicate.SHOW)) {
totalRows.add(Arrays.asList(tableName, String.valueOf(queryHit)));
if (Util.isTempTable(tableName)) {
if (Util.isTempTableInCurrentSession(tableName)) {
totalRows.add(Arrays.asList(Util.getTempTableOuterName(tableName),
String.valueOf(queryHit)));
}
} else {
totalRows.add(Arrays.asList(tableName, String.valueOf(queryHit)));
}
}
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.Table;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
Expand Down Expand Up @@ -146,7 +147,7 @@ private void analyzeTargetTable(Analyzer analyzer) throws UserException {

// step2: resolve table name with catalog, only unique olap table could be updated
targetTable = targetTableRef.getTable();
if (targetTable.getType() != Table.TableType.OLAP
if ((targetTable.getType() != Table.TableType.OLAP && targetTable.getType() != TableType.TEMP)
|| ((OlapTable) targetTable).getKeysType() != KeysType.UNIQUE_KEYS) {
throw new AnalysisException("Only unique table could be updated.");
}
Expand Down
54 changes: 51 additions & 3 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.doris.persist.CreateTableInfo;
import org.apache.doris.persist.gson.GsonPostProcessable;
import org.apache.doris.persist.gson.GsonUtils;
import org.apache.doris.qe.ConnectContext;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -378,6 +379,19 @@ public void checkQuota() throws DdlException {
checkReplicaQuota();
}

public boolean isTableExist(String tableName, TableType tableType) {
if (Env.isTableNamesCaseInsensitive()) {
tableName = tableName.toLowerCase();
}

if (tableType == TableType.TEMP) {
Set<String> tableSet = ConnectContext.get().getDbToTempTableNamesMap().get(fullQualifiedName);
return tableSet != null && tableSet.contains(tableName);
} else {
return nameToTable.containsKey(tableName);
}
}

public boolean isTableExist(String tableName) {
if (Env.isTableNamesCaseInsensitive()) {
tableName = lowerCaseToTableName.get(tableName.toLowerCase());
Expand Down Expand Up @@ -407,8 +421,16 @@ public Pair<Boolean, Boolean> createTableWithLock(
isTableExist = true;
} else {
idToTable.put(table.getId(), table);
nameToTable.put(table.getName(), table);
lowerCaseToTableName.put(tableName.toLowerCase(), tableName);
nameToTable.put(table.getName(), table);
if (table.getType() == TableType.TEMP) {
if (isReplay) {
// add to to-deleted list, and delete it after catalog is ready
Env.getCurrentEnv().addPhantomTempTable(table);
} else {
ConnectContext.get().addTempTableToDB(table.getQualifiedDbName(), table.getName());
}
}

if (!isReplay) {
// Write edit log
Expand Down Expand Up @@ -454,8 +476,8 @@ public void unregisterTable(String tableName) {
Table table = getTableNullable(tableName);
if (table != null) {
this.nameToTable.remove(tableName);
this.idToTable.remove(table.getId());
this.lowerCaseToTableName.remove(tableName.toLowerCase());
this.idToTable.remove(table.getId());
table.markDropped();
}
}
Expand Down Expand Up @@ -549,7 +571,33 @@ public Table getTableNullable(String tableName) {
return null;
}
}
return nameToTable.get(tableName);

// return temp table first
Table table = nameToTable.get(Util.generateTempTableInnerName(tableName));
if (table == null) {
table = nameToTable.get(tableName);
}

return table;
}

/**
* This is a thread-safe method when nameToTable is a concurrent hash map
*/
@Override
public Table getNonTempTableNullable(String tableName) {
if (Env.isStoredTableNamesLowerCase()) {
tableName = tableName.toLowerCase();
}
if (Env.isTableNamesCaseInsensitive()) {
tableName = lowerCaseToTableName.get(tableName.toLowerCase());
if (tableName == null) {
return null;
}
}

Table table = nameToTable.get(tableName);
return table;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ default Set<String> getTableNamesOrEmptyWithLock() {

T getTableNullable(String tableName);

T getNonTempTableNullable(String tableName);

default T getTableNullableIfException(String tableName) {
try {
return getTableNullable(tableName);
Expand Down Expand Up @@ -139,6 +141,15 @@ default <E extends Exception> T getTableOrException(String tableName, java.util.
return table;
}

default <E extends Exception> T getNonTempTableOrException(String tableName,
java.util.function.Function<String, E> e) throws E {
T table = getNonTempTableNullable(tableName);
if (table == null) {
throw e.apply(tableName);
}
return table;
}

default <E extends Exception> T getTableOrException(long tableId, Function<Long, E> e) throws E {
T table = getTableNullable(tableId);
if (table == null) {
Expand All @@ -152,6 +163,11 @@ default T getTableOrMetaException(String tableName) throws MetaNotFoundException
ErrorCode.ERR_BAD_TABLE_ERROR));
}

default T getNonTempTableOrMetaException(String tableName) throws MetaNotFoundException {
return getNonTempTableOrException(tableName, t -> new MetaNotFoundException("table not found, tableName=" + t,
ErrorCode.ERR_BAD_TABLE_ERROR));
}

default T getTableOrMetaException(long tableId) throws MetaNotFoundException {
return getTableOrException(tableId, t -> new MetaNotFoundException("table not found, tableId=" + t,
ErrorCode.ERR_BAD_TABLE_ERROR));
Expand All @@ -167,6 +183,17 @@ default T getTableOrMetaException(String tableName, TableIf.TableType tableType)
return table;
}

default T getNonTempTableOrMetaException(String tableName, TableIf.TableType tableType)
throws MetaNotFoundException {
T table = getNonTempTableOrMetaException(tableName);
TableType type = Objects.requireNonNull(table.getType());
if (type != tableType && type.getParentType() != tableType) {
throw new MetaNotFoundException(
"table type is not " + tableType + ", tableName=" + tableName + ", type=" + type);
}
return table;
}

default T getTableOrMetaException(String tableName, List<TableIf.TableType> tableTypes)
throws MetaNotFoundException {
T table = getTableOrMetaException(tableName);
Expand Down Expand Up @@ -256,7 +283,7 @@ default OlapTable getOlapTableOrDdlException(String tableName) throws DdlExcepti

default OlapTable getOlapTableOrAnalysisException(String tableName) throws AnalysisException {
T table = getTableOrAnalysisException(tableName);
if (!(table instanceof OlapTable)) {
if (!(table instanceof OlapTable) && !(table.getType() == TableType.TEMP)) {
throw new AnalysisException(ErrorCode.ERR_NOT_OLAP_TABLE.formatErrorMsg(tableName));
}
return (OlapTable) table;
Expand Down
Loading
Loading