use of io.mycat.calcite.physical.MycatInsertRel in project Mycat2 by MyCATApache.
the class DrdsSqlCompiler method dispatch.
public MycatRel dispatch(OptimizationContext optimizationContext, DrdsSql drdsSql, SchemaPlus plus) {
SQLStatement sqlStatement = drdsSql.getParameterizedStatement();
if (sqlStatement instanceof SQLSelectStatement) {
return compileQuery(optimizationContext, plus, drdsSql);
}
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
if (sqlStatement instanceof MySqlInsertStatement) {
MySqlInsertStatement insertStatement = (MySqlInsertStatement) sqlStatement;
String schemaName = SQLUtils.normalize(insertStatement.getTableSource().getSchema());
String tableName = SQLUtils.normalize(insertStatement.getTableName().getSimpleName());
TableHandler logicTable = Objects.requireNonNull(metadataManager.getTable(schemaName, tableName));
switch(logicTable.getType()) {
case SHARDING:
MycatInsertRel mycatInsertRel = new MycatInsertRel(sqlStatement, false);
optimizationContext.saveAlways();
return mycatInsertRel;
case GLOBAL:
return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
case NORMAL:
return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
case CUSTOM:
throw new UnsupportedOperationException();
}
} else if (sqlStatement instanceof MySqlUpdateStatement) {
SQLExprTableSource tableSource = (SQLExprTableSource) ((MySqlUpdateStatement) sqlStatement).getTableSource();
String schemaName = SQLUtils.normalize(tableSource.getSchema());
String tableName = SQLUtils.normalize(((MySqlUpdateStatement) sqlStatement).getTableName().getSimpleName());
TableHandler logicTable = metadataManager.getTable(schemaName, tableName);
switch(logicTable.getType()) {
case SHARDING:
return compileUpdate(logicTable, optimizationContext, drdsSql, plus);
case GLOBAL:
{
return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
}
case NORMAL:
{
return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
}
case CUSTOM:
throw new UnsupportedOperationException();
}
} else if (sqlStatement instanceof SQLReplaceStatement) {
SQLExprTableSource tableSource = (SQLExprTableSource) ((SQLReplaceStatement) sqlStatement).getTableSource();
String schemaName = SQLUtils.normalize(Optional.ofNullable(tableSource).map(i -> i.getSchema()).orElse(null));
String tableName = SQLUtils.normalize(((SQLReplaceStatement) sqlStatement).getTableName().getSimpleName());
TableHandler logicTable = metadataManager.getTable(schemaName, tableName);
switch(logicTable.getType()) {
case SHARDING:
return compileUpdate(logicTable, optimizationContext, drdsSql, plus);
case GLOBAL:
{
return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
}
case NORMAL:
{
return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
}
case CUSTOM:
throw new UnsupportedOperationException();
}
} else if (sqlStatement instanceof MySqlDeleteStatement) {
SQLExprTableSource tableSource = (SQLExprTableSource) ((MySqlDeleteStatement) sqlStatement).getTableSource();
String schemaName = SQLUtils.normalize(Optional.ofNullable(tableSource).map(i -> i.getSchema()).orElse(null));
String tableName = SQLUtils.normalize(((MySqlDeleteStatement) sqlStatement).getTableName().getSimpleName());
TableHandler logicTable = metadataManager.getTable(schemaName, tableName);
switch(logicTable.getType()) {
case SHARDING:
return compileDelete(logicTable, optimizationContext, drdsSql, plus);
case GLOBAL:
{
return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
}
case NORMAL:
{
return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
}
case CUSTOM:
throw new UnsupportedOperationException();
}
}
return null;
}
use of io.mycat.calcite.physical.MycatInsertRel in project Mycat2 by MyCATApache.
the class ObservablePlanImplementorImpl method executeInsert.
@Override
public Future<Void> executeInsert(Plan logical) {
MycatInsertRel mycatRel = (MycatInsertRel) logical.getMycatRel();
List<VertxExecuter.EachSQL> insertSqls = VertxExecuter.explainInsert((SQLInsertStatement) mycatRel.getSqlStatement(), drdsSqlWithParams.getParams());
assert !insertSqls.isEmpty();
Future<long[]> future;
if (insertSqls.size() > 1) {
future = VertxExecuter.simpleUpdate(context, true, true, mycatRel.isGlobal(), VertxExecuter.rewriteInsertBatchedStatements(insertSqls));
} else {
future = VertxExecuter.simpleUpdate(context, true, false, mycatRel.isGlobal(), insertSqls);
}
return future.eventually(u -> context.getTransactionSession().closeStatementState()).flatMap(result -> response.sendOk(result[0], result[1]));
}
use of io.mycat.calcite.physical.MycatInsertRel in project Mycat2 by MyCATApache.
the class HintHandler method loaddata.
@NotNull
private PromiseInternal<Void> loaddata(MycatDataContext dataContext, Response response, String body, MetadataManager metadataManager) throws IOException {
Map<String, Object> map = JsonUtil.from(body, Map.class);
String schemaName = Objects.requireNonNull((String) map.get("schemaName"));
String tableName = Objects.requireNonNull((String) map.get("tableName"));
final String fileName = Objects.toString(map.get("fileName"));
CSVFormat format = CSVFormat.MYSQL;
final Character delimiter = map.getOrDefault("delimiter", format.getDelimiter() + "").toString().charAt(0);
final Character quoteChar = map.getOrDefault("quoteChar", format.getQuoteCharacter() + "").toString().charAt(0);
final QuoteMode quoteMode = QuoteMode.valueOf(map.getOrDefault("quoteMode", format.getQuoteMode() + "").toString());
final Character commentStart = Optional.ofNullable(map.get("commentStart")).map(c -> c.toString().charAt(0)).orElse(format.getCommentMarker());
final Character escape = map.getOrDefault("escape", format.getEscapeCharacter()).toString().charAt(0);
final Boolean ignoreSurroundingSpaces = Boolean.parseBoolean(map.getOrDefault("escape", format.getIgnoreSurroundingSpaces()).toString());
final Boolean ignoreEmptyLines = Boolean.parseBoolean(map.getOrDefault("ignoreEmptyLines", format.getIgnoreEmptyLines()).toString());
final String recordSeparator = map.getOrDefault("recordSeparator", format.getRecordSeparator()).toString();
final String nullString = map.getOrDefault("nullString", format.getNullString()).toString();
final List headerComments = (List) map.getOrDefault("headerComments", Arrays.asList(Optional.ofNullable(format.getHeaderComments()).orElse(new String[] {})));
final List<String> header = Optional.ofNullable((List<String>) map.get("header")).orElse(null);
final Boolean skipHeaderRecord = Boolean.parseBoolean(map.getOrDefault("skipHeaderRecord", format.getSkipHeaderRecord()).toString());
final Boolean allowMissingColumnNames = Boolean.parseBoolean(map.getOrDefault("allowMissingColumnNames", format.getSkipHeaderRecord()).toString());
final Boolean ignoreHeaderCase = Boolean.parseBoolean(map.getOrDefault("ignoreHeaderCase", format.getSkipHeaderRecord()).toString());
final Boolean trim = Boolean.parseBoolean(map.getOrDefault("trim", format.getSkipHeaderRecord()).toString());
final Boolean trailingDelimiter = Boolean.parseBoolean(map.getOrDefault("trailingDelimiter", format.getSkipHeaderRecord()).toString());
final Boolean autoFlush = Boolean.parseBoolean(map.getOrDefault("autoFlush", format.getSkipHeaderRecord()).toString());
final Boolean allowDuplicateHeaderNames = Boolean.parseBoolean(map.getOrDefault("allowDuplicateHeaderNames", format.getSkipHeaderRecord()).toString());
format = CSVFormat.newFormat(delimiter).withQuote(quoteChar).withQuoteMode(quoteMode).withCommentMarker(commentStart).withEscape(escape).withIgnoreSurroundingSpaces(ignoreSurroundingSpaces).withIgnoreEmptyLines(ignoreEmptyLines).withRecordSeparator(recordSeparator).withNullString(nullString).withHeaderComments(headerComments).withHeader(Optional.ofNullable(header).map(n -> n.toArray(new String[] {})).orElse(null)).withSkipHeaderRecord(skipHeaderRecord).withAllowMissingColumnNames(allowMissingColumnNames).withIgnoreHeaderCase(ignoreHeaderCase).withTrim(trim).withTrailingDelimiter(trailingDelimiter).withAutoFlush(autoFlush).withAllowDuplicateHeaderNames(allowDuplicateHeaderNames);
MySqlInsertStatement mySqlInsertStatement = new MySqlInsertStatement();
mySqlInsertStatement.setTableName(new SQLIdentifierExpr(tableName));
mySqlInsertStatement.getTableSource().setSchema(schemaName);
List<String> columnNames;
if (header == null || header.isEmpty()) {
TableHandler table = Objects.requireNonNull(metadataManager.getTable(schemaName, tableName));
columnNames = table.getColumns().stream().map(i -> (i.getColumnName())).collect(Collectors.toList());
} else {
columnNames = header;
}
for (SQLIdentifierExpr columnName : columnNames.stream().map(i -> new SQLIdentifierExpr("`" + i + "`")).collect(Collectors.toList())) {
mySqlInsertStatement.addColumn(columnName);
}
int batch = 1000;
Reader in = new FileReader(fileName);
Iterable<CSVRecord> records = format.parse(in);
Stream<SQLInsertStatement> insertStatementStream = StreamSupport.stream(records.spliterator(), false).map(strings -> {
SQLInsertStatement sqlInsertStatement = mySqlInsertStatement.clone();
SQLInsertStatement.ValuesClause valuesClause = new SQLInsertStatement.ValuesClause();
for (String string : strings) {
valuesClause.addValue(new SQLCharExpr(string));
}
sqlInsertStatement.addValueCause(valuesClause);
return sqlInsertStatement;
});
UnmodifiableIterator<List<VertxExecuter.EachSQL>> iterator = Iterators.partition(insertStatementStream.flatMap(statement -> {
DrdsSqlWithParams drdsSql = DrdsRunnerHelper.preParse(statement, dataContext.getDefaultSchema());
Plan plan = UpdateSQLHandler.getPlan(drdsSql);
MycatInsertRel mycatRel = (MycatInsertRel) plan.getMycatRel();
Iterable<VertxExecuter.EachSQL> eachSQLS1 = VertxExecuter.explainInsert((SQLInsertStatement) mycatRel.getSqlStatement(), drdsSql.getParams());
return StreamSupport.stream(eachSQLS1.spliterator(), false);
}).iterator(), batch);
Future<long[]> continution = Future.succeededFuture(new long[] { 0, 0 });
while (iterator.hasNext()) {
Iterable<VertxExecuter.EachSQL> eachSQL = VertxExecuter.rewriteInsertBatchedStatements(iterator.next());
continution = continution.flatMap(o -> {
Future<long[]> future = VertxExecuter.simpleUpdate(dataContext, true, true, false, eachSQL);
return future.map(o2 -> new long[] { o[0] + o2[0], Math.max(o[1], o2[1]) });
});
}
continution.onComplete(event -> JdbcUtils.close(in));
return VertxUtil.castPromise(continution.flatMap(result -> {
return response.sendOk(result[0], result[1]);
}));
}
Aggregations