Skip to content
Merged
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 @@ -456,6 +456,45 @@ public void partialInsertSQLTest() throws IoTDBConnectionException, StatementExe
}
}

@Test
public void disableAutoCreateSchemaForTableSessionTest()
throws IoTDBConnectionException, StatementExecutionException {
final DataNodeWrapper dataNode = EnvFactory.getEnv().getDataNodeWrapper(0);
try (ITableSession session =
new TableSessionBuilder()
.nodeUrls(Collections.singletonList(dataNode.getIpAndPortString()))
.database("db1")
.enableAutoCreateSchema(false)
.build()) {
final List<IMeasurementSchema> schemaList = new ArrayList<>();
schemaList.add(new MeasurementSchema("tag1", TSDataType.STRING));
schemaList.add(new MeasurementSchema("s1", TSDataType.INT64));
schemaList.add(new MeasurementSchema("s2", TSDataType.INT64));
final Tablet tablet =
new Tablet(
"session_auto_create",
IMeasurementSchema.getMeasurementNameList(schemaList),
IMeasurementSchema.getDataTypeList(schemaList),
Arrays.asList(ColumnCategory.TAG, ColumnCategory.FIELD, ColumnCategory.FIELD),
1);
tablet.addTimestamp(0, 1);
tablet.addValue("tag1", 0, "d1");
tablet.addValue("s1", 0, 1L);
tablet.addValue("s2", 0, 2L);

assertThrows(StatementExecutionException.class, () -> session.insert(tablet));

session.executeNonQueryStatement(
"CREATE TABLE session_auto_create (tag1 string tag, s1 int64 field)");
assertThrows(StatementExecutionException.class, () -> session.insert(tablet));

try (SessionDataSet dataSet =
session.executeQueryStatement("SELECT * FROM session_auto_create")) {
assertEquals(Arrays.asList("time", "tag1", "s1"), dataSet.getColumnNames());
}
}
}

@Test
public void partialInsertRelationalTabletTest()
throws IoTDBConnectionException, StatementExecutionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public abstract class AbstractSessionBuilder {

public String database;

public boolean enableAutoCreateSchema = true;

public Boolean isIoTDBRpcCompressionEnabled = true;

public Boolean isThriftRpcCompactionEnabled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ public class Session implements ISession {
// may be null
protected volatile String database;

protected volatile boolean enableAutoCreateSchema = true;

private static final String VALUES_SIZE_SHOULD_BE_EQUAL =
"times, measurementsList and valuesList's size should be equal";

Expand Down Expand Up @@ -484,6 +486,7 @@ public Session(AbstractSessionBuilder builder) {
this.sqlDialect = builder.sqlDialect;
this.queryTimeoutInMs = builder.timeOut;
this.database = builder.database;
this.enableAutoCreateSchema = builder.enableAutoCreateSchema;
}

@Override
Expand Down Expand Up @@ -4523,6 +4526,11 @@ public Builder database(String database) {
return this;
}

public Builder enableAutoCreateSchema(boolean enableAutoCreateSchema) {
this.enableAutoCreateSchema = enableAutoCreateSchema;
return this;
}

public Builder useSSL(boolean useSSL) {
this.useSSL = useSSL;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public class SessionConnection {

private static final Logger logger = LoggerFactory.getLogger(SessionConnection.class);
private static final String USE_ENCRYPTED_PASSWORD_KEY = "use_encrypted_password";
private static final String ENABLE_AUTO_CREATE_SCHEMA_KEY = "enable_auto_create_schema";
public static final String MSG_RECONNECTION_FAIL =
"Fail to reconnect to server. Please check server status.";
protected Session session;
Expand Down Expand Up @@ -244,6 +245,9 @@ private void init(
if (session.useEncryptedPassword) {
openReq.putToConfiguration(USE_ENCRYPTED_PASSWORD_KEY, Boolean.TRUE.toString());
}
if (!session.enableAutoCreateSchema) {
openReq.putToConfiguration(ENABLE_AUTO_CREATE_SCHEMA_KEY, Boolean.FALSE.toString());
}
if (database != null) {
openReq.putToConfiguration("db", database);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ public TableSessionBuilder enableAutoFetch(boolean enableAutoFetch) {
return this;
}

/**
* Enables or disables schema auto-creation for this session's write requests.
*
* @param enableAutoCreateSchema whether to allow auto creating tables and columns.
* @return the current {@link TableSessionBuilder} instance.
* @defaultValue true
*/
public TableSessionBuilder enableAutoCreateSchema(boolean enableAutoCreateSchema) {
this.enableAutoCreateSchema = enableAutoCreateSchema;
return this;
}

/**
* Sets the maximum number of retries for connection attempts.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ public class SessionPool implements ISessionPool {
// may be null
protected String database;

protected boolean enableAutoCreateSchema = true;

private static final String INSERT_RECORD_FAIL = SessionMessages.INSERT_RECORD_FAILED;

private static final String INSERT_RECORD_ERROR_MSG =
Expand Down Expand Up @@ -553,6 +555,7 @@ public SessionPool(AbstractSessionPoolBuilder builder) {
this.sqlDialect = builder.sqlDialect;
this.database = builder.database;
this.queryTimeoutInMs = builder.timeOut;
this.enableAutoCreateSchema = builder.enableAutoCreateSchema;

if (enableAutoFetch) {
initThreadPool();
Expand Down Expand Up @@ -612,6 +615,7 @@ private Session constructNewSession() {
.retryIntervalInMs(retryIntervalInMs)
.sqlDialect(sqlDialect)
.database(database)
.enableAutoCreateSchema(enableAutoCreateSchema)
.timeOut(queryTimeoutInMs)
.enableIoTDBRpcCompression(enableIoTDBRpcCompression)
.enableThriftRpcCompression(enableThriftCompression)
Expand Down Expand Up @@ -640,6 +644,7 @@ private Session constructNewSession() {
.retryIntervalInMs(retryIntervalInMs)
.sqlDialect(sqlDialect)
.database(database)
.enableAutoCreateSchema(enableAutoCreateSchema)
.timeOut(queryTimeoutInMs)
.enableIoTDBRpcCompression(enableIoTDBRpcCompression)
.enableThriftRpcCompression(enableThriftCompression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,18 @@ public TableSessionPoolBuilder sslProtocol(String sslProtocol) {
return this;
}

/**
* Enables or disables schema auto-creation for this pool's write requests.
*
* @param enableAutoCreateSchema whether to allow auto creating tables and columns.
* @return the current {@link TableSessionPoolBuilder} instance.
* @defaultValue true
*/
public TableSessionPoolBuilder enableAutoCreateSchema(boolean enableAutoCreateSchema) {
this.enableAutoCreateSchema = enableAutoCreateSchema;
return this;
}

/**
* Builds and returns a configured {@link ITableSessionPool} instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public abstract class IClientSession {

@Nullable private String databaseName;

private boolean enableAutoCreateSchema = true;

private long lastActiveTime = CommonDateTimeUtils.currentTime();

public abstract String getClientAddress();
Expand Down Expand Up @@ -201,6 +203,14 @@ public void setDatabaseName(@Nullable String databaseName) {
}
}

public boolean isEnableAutoCreateSchema() {
return enableAutoCreateSchema;
}

public void setEnableAutoCreateSchema(boolean enableAutoCreateSchema) {
this.enableAutoCreateSchema = enableAutoCreateSchema;
}

/**
* Add a prepared statement to this session.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ public SessionInfo getSessionInfo(IClientSession session) {
session.getZoneId(),
session.getClientVersion(),
session.getDatabaseName(),
session.getSqlDialect());
session.getSqlDialect(),
session.isEnableAutoCreateSchema());
}

// Sometimes we need to switch from table model to tree model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ public class ClientRPCServiceImpl implements IClientRPCServiceWithHandler {

public static final String ERROR_CODE = DataNodeMiscMessages.ERROR_CODE;
private static final String USE_ENCRYPTED_PASSWORD_KEY = "use_encrypted_password";
private static final String ENABLE_AUTO_CREATE_SCHEMA_KEY = "enable_auto_create_schema";

private static final TSProtocolVersion CURRENT_RPC_VERSION =
TSProtocolVersion.IOTDB_SERVICE_PROTOCOL_V3;
Expand Down Expand Up @@ -1633,8 +1634,11 @@ public TSOpenSessionResp openSession(TSOpenSessionReq req) throws TException {
parseUseEncryptedPassword(req));
TSStatus tsStatus = RpcUtils.getStatus(openSessionResp.getCode(), openSessionResp.getMessage());

if (tsStatus.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode() && database.isPresent()) {
clientSession.setDatabaseName(database.get().toLowerCase(Locale.ENGLISH));
if (tsStatus.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
if (database.isPresent()) {
clientSession.setDatabaseName(database.get().toLowerCase(Locale.ENGLISH));
}
clientSession.setEnableAutoCreateSchema(parseEnableAutoCreateSchema(req));
}
TSOpenSessionResp resp = new TSOpenSessionResp(tsStatus, CURRENT_RPC_VERSION);
Map<String, String> configuration = new HashMap<>();
Expand Down Expand Up @@ -1678,6 +1682,13 @@ private boolean parseUseEncryptedPassword(TSOpenSessionReq req) {
&& Boolean.parseBoolean(configuration.get(USE_ENCRYPTED_PASSWORD_KEY));
}

private boolean parseEnableAutoCreateSchema(TSOpenSessionReq req) {
Map<String, String> configuration = req.configuration;
return configuration == null
|| !configuration.containsKey(ENABLE_AUTO_CREATE_SCHEMA_KEY)
|| Boolean.parseBoolean(configuration.get(ENABLE_AUTO_CREATE_SCHEMA_KEY));
}

@Override
public TSStatus closeSession(TSCloseSessionReq req) {
return new TSStatus(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ public void validateInsertNodeMeasurements(
final List<Integer> missingMeasurementIndices = new ArrayList<>();

final boolean isAutoCreateSchemaEnabled =
IoTDBDescriptor.getInstance().getConfig().isAutoCreateSchemaEnabled();
IoTDBDescriptor.getInstance().getConfig().isAutoCreateSchemaEnabled()
&& context.getSession().isEnableAutoCreateSchema();

// First round validate, check existing schema
if (table == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ public class SessionInfo {

private ClientVersion version = ClientVersion.V_1_0;

// Only used during statement analysis on the coordinator. Analysis is complete before the
// SessionInfo is sent to execution nodes, so this field does not need serialization.
private final transient boolean enableAutoCreateSchema;

public SessionInfo(long sessionId, UserEntity userEntity, ZoneId zoneId) {
this.sessionId = sessionId;
this.userEntity = userEntity;
this.zoneId = zoneId;
this.databaseName = null;
this.sqlDialect = SqlDialect.TREE;
this.enableAutoCreateSchema = true;
}

public SessionInfo(
Expand All @@ -60,7 +65,7 @@ public SessionInfo(
ZoneId zoneId,
@Nullable String databaseName,
SqlDialect sqlDialect) {
this(sessionId, userEntity, zoneId, ClientVersion.V_1_0, databaseName, sqlDialect);
this(sessionId, userEntity, zoneId, ClientVersion.V_1_0, databaseName, sqlDialect, true);
}

public SessionInfo(
Expand All @@ -70,12 +75,24 @@ public SessionInfo(
ClientVersion version,
@Nullable String databaseName,
SqlDialect sqlDialect) {
this(sessionId, userEntity, zoneId, version, databaseName, sqlDialect, true);
}

public SessionInfo(
long sessionId,
UserEntity userEntity,
ZoneId zoneId,
ClientVersion version,
@Nullable String databaseName,
SqlDialect sqlDialect,
boolean enableAutoCreateSchema) {
this.sessionId = sessionId;
this.userEntity = userEntity;
this.zoneId = zoneId;
this.version = version;
this.databaseName = databaseName;
this.sqlDialect = sqlDialect;
this.enableAutoCreateSchema = enableAutoCreateSchema;
}

@TestOnly
Expand Down Expand Up @@ -144,6 +161,10 @@ public SqlDialect getSqlDialect() {
return sqlDialect;
}

public boolean isEnableAutoCreateSchema() {
return enableAutoCreateSchema;
}

public static SessionInfo deserializeFrom(final ByteBuffer buffer) {
final long sessionId = ReadWriteIOUtils.readLong(buffer);
final long userId = ReadWriteIOUtils.readLong(buffer);
Expand Down
Loading