Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,4 @@ public Schema snapshot(SchemaVersion schemaVersion) {
return this;
}

public void applyDiff(SchemaDiff schemaDiff) {
if (is == null) {
return;
}
InfoSchemaInTxnBuilder schemaInTxnBuilder = new InfoSchemaInTxnBuilder(is);
schemaInTxnBuilder.applyDiff(InfoSchemaService.root(), schemaDiff);
this.is = schemaInTxnBuilder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import com.google.common.collect.ImmutableSortedSet;
import io.dingodb.calcite.DingoTable;
import io.dingodb.common.log.LogUtils;
import io.dingodb.common.mysql.DingoErrUtil;
import io.dingodb.common.util.Optional;
import io.dingodb.meta.DdlService;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.apache.calcite.jdbc.CalciteSchema;
Expand All @@ -36,6 +38,8 @@

import java.util.List;

import static io.dingodb.common.mysql.error.ErrorCode.ErrNoSuchTable;

@Slf4j
public class SubCalciteSchema extends CalciteSchema {
RootCalciteSchema rootCalciteSchema;
Expand Down Expand Up @@ -72,7 +76,20 @@ protected SubCalciteSchema(
boolean inTxn = subSnapshotSchema.inTransaction();
Table table = schema.getTable(tableName);
if (table != null && inTxn) {
// Add schemaVersion consistency check
DingoTable dingoTable = (DingoTable) table;
long schemaVersion = dingoTable.getTable().getSchemaVersion();
int status = DdlService.root().checkTableSchemaVersion(dingoTable.getTableId(), schemaVersion);
if (status == 3) {
// table not exists
throw DingoErrUtil.newStdErr(ErrNoSuchTable, dingoTable.getTable().getName());
} else if (status == 2) {
// refresh schema
io.dingodb.meta.entity.Table table1 = DdlService.root().getIsLatest().getTable(dingoTable.getTableId().seq);
table = new DingoTable(dingoTable.getContext(), dingoTable.getNames(), table1);
}
// check method
// throw exception
rootCalciteSchema.putRelatedTable(dingoTable.getTableId().seq,
((SubSnapshotSchema) schema).getSchemaVer(), subSnapshotSchema.txnId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,46 +96,6 @@ public SubSnapshotSchema(CommonId txnId, InfoSchema is, String schemaName,
);
}

public @Nullable DingoTable getValidateTable(String tableName) {
tableName = convertName(tableName);
SchemaTables schemaTables;
if (is == null) {
InfoSchema isTmp = DdlService.root().getIsLatest();
if (isTmp == null) {
return null;
}
schemaTables = isTmp.schemaMap.get(schemaName);
} else {
schemaTables = is.schemaMap.get(schemaName);;
}
if (schemaTables == null) {
return null;
}
Table table = schemaTables.getTables().get(tableName);
if (table == null) {
return null;
}
boolean hasHidden = table.getColumns()
.stream().anyMatch(column -> column.getState() != 1
||
(column.getSchemaState() != SchemaState.SCHEMA_PUBLIC && column.getSchemaState() != null)
);
if (hasHidden) {
List<Column> columnList = table.getColumns()
.stream()
.filter(column -> (column.getSchemaState() == null
|| column.getSchemaState() == SchemaState.SCHEMA_PUBLIC)
&& column.getState() == 1)
.collect(Collectors.toList());
table = table.copyWithColumns(columnList);
}
return new DingoTable(
context,
ImmutableList.<String>builder().addAll(names).add(tableName).build(),
table
);
}

@Override
public Set<String> getTableNames() {
SchemaTables schemaTables;
Expand Down
13 changes: 12 additions & 1 deletion dingo-driver/host/src/main/java/io/dingodb/driver/DingoMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -194,7 +195,17 @@ private Collection<CalciteSchema.TableEntry> getMatchedTables(
.flatMap(s -> s.getTableNames().stream()
.filter(filter)
.filter(name -> verifyPrivilege((SubSnapshotSchema) s.schema, name, "getTables"))
.map(name -> s.getImplicitTable(name, caseSensitive())))
.map(name -> {
try {
return s.getImplicitTable(name, caseSensitive());
} catch (DingoSqlException e) {
if (e.getSqlCode() == 1146 && "42S02".equalsIgnoreCase(e.getSqlState())) {
return null;
} else {
throw e;
}
}
}).filter(Objects::nonNull))
.collect(Collectors.toList());
}

Expand Down
2 changes: 2 additions & 0 deletions dingo-meta-api/src/main/java/io/dingodb/meta/DdlService.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,6 @@ default void alterTableTruncatePart(SchemaInfo schemaInfo, Table table, String p
void refreshMeta(SchemaInfo schemaInfo, String tableName);

void addPrimaryKey(SchemaInfo schemaInfo, Table table, List<String> primaryKeyList);

int checkTableSchemaVersion(CommonId tableId, long schemaVersion);
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public class Table {
@JsonProperty
public boolean visible;

public long schemaVersion;

public TupleType tupleType() {
return DingoTypeFactory.tuple(columns.stream()
.map(col -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,22 @@ public void addPrimaryKey(SchemaInfo schemaInfo, Table table, List<String> prima
DdlHandler.doDdlJob(job);
}

public int checkTableSchemaVersion(CommonId tableId, long schemaVersion) {
InfoSchema infoSchema = InfoCache.infoCache.getLatest();
if (infoSchema == null) {
return 0;
} else {
Table table = infoSchema.getTable(tableId.seq);
if (table != null) {
if (table.getSchemaVersion() == schemaVersion) {
return 1;
} else {
return 2;
}
} else {
return 3;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,9 @@ public Object getTable(long schemaId, String tableName, long tenantId) {
? tableDefinitionWithId.getTableDefinition().getName().equalsIgnoreCase(table)
: tableDefinitionWithId.getTableDefinition().getName().equals(table))
.filter(tableDefinitionWithId -> {
return tableDefinitionWithId.getTableDefinition().getSchemaState()
return tableDefinitionWithId.getTableDefinition().getSchemaState()
== SchemaState.SCHEMA_PUBLIC;
}
)
})
.findFirst().orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,8 @@ public void refreshMeta(SchemaInfo schemaInfo, String tableName) {
public void addPrimaryKey(SchemaInfo schemaInfo, Table table, List<String> primaryKeyList) {

}

public int checkTableSchemaVersion(CommonId tableId, long schemaVersion) {
return 1;
}
}
Loading