use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.
the class ShardingJoinRBOMegreTest method parse.
public static Explain parse(String sql) {
DrdsSqlCompiler drds = getDrds();
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sql, null);
OptimizationContext optimizationContext = new OptimizationContext();
MycatRel dispatch = drds.dispatch(optimizationContext, drdsSqlWithParams);
Plan plan = new PlanImpl(dispatch, DrdsExecutorCompiler.getCodeExecuterContext(optimizationContext.relNodeContext.getConstantMap(), dispatch, false), drdsSqlWithParams.getAliasList());
return new Explain(plan, drdsSqlWithParams);
}
use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.
the class ClientTest method testDBeaver.
@Test
public void testDBeaver() throws Exception {
/**
* SELECT *
* FROM information_schema.COLUMNS
* WHERE TABLE_SCHEMA = 'db1'
* AND TABLE_NAME = 'www'
* ORDER BY ORDINAL_POSITION
*/
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse("SELECT @@global.character_set_server, @@global.collation_server", null);
Assert.assertEquals("SELECT @@global.character_set_server, @@global.collation_server", drdsSqlWithParams.getParameterizedSQL());
try (Connection mySQLConnection = getMySQLConnection(DB_MYCAT)) {
// Statement statement = mySQLConnection.createStatement();
// ResultSet resultSet = statement.executeQuery("SELECT * FROM `information_schema`.`CHARACTER_SETS` LIMIT 0, 1000; ");
// List<String> res = new ArrayList<>();
// while (resultSet.next()) {
// int columnCount = resultSet.getMetaData().getColumnCount();
// Object[] objects = new Object[columnCount];
// for (int i = 0; i < columnCount; i++) {
// objects[i] = resultSet.getObject(i + 1);
// }
// String collect = Stream.of(objects).map(i -> {
// if (i instanceof String) {
// return "\"" + i + "\"";
// }
// return i.toString();
// }).collect(Collectors.joining(","));
// res.add("r.add(new Object[]{"+collect+"});");
// }
// System.out.println(String.join("",res));
// List<Map<String, Object>> show_collation = JdbcUtils.executeQuery(mySQLConnection, "SHOW COLLATION", Collections.emptyList());
execute(mySQLConnection, "/* ApplicationName=DBeaver 21.2.5 - Main */ SET autocommit=1");
executeQuery(mySQLConnection, "/* ApplicationName=DBeaver 21.2.5 - Metadata */ SELECT DATABASE()");
executeQuery(mySQLConnection, "/* ApplicationName=DBeaver 21.2.5 - Metadata */ SHOW ENGINES");
executeQuery(mySQLConnection, "/* ApplicationName=DBeaver 21.2.5 - Metadata */ SHOW CHARSET");
executeQuery(mySQLConnection, "/* ApplicationName=DBeaver 21.2.5 - Metadata */ SHOW COLLATION");
executeQuery(mySQLConnection, "/* ApplicationName=DBeaver 21.2.5 - Metadata */ SELECT @@GLOBAL.character_set_server,@@GLOBAL.collation_server");
executeQuery(mySQLConnection, "/* ApplicationName=DBeaver 21.2.5 - Metadata */ SHOW PLUGINS");
executeQuery(mySQLConnection, "/* ApplicationName=DBeaver 21.2.5 - Metadata */ SHOW VARIABLES LIKE 'lower_case_table_names'");
executeQuery(mySQLConnection, "/* ApplicationName=DBeaver 21.2.5 - Metadata */ show databases");
executeQuery(mySQLConnection, "/* ApplicationName=DBeaver 21.2.5 - Metadata */ SELECT * FROM information_schema.TABLES t\n" + "WHERE\n" + "\tt.TABLE_SCHEMA = 'information_schema'\n" + "\tAND t.TABLE_NAME = 'CHECK_CONSTRAINTS'");
}
}
use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.
the class ExecutorProviderImpl method prepare.
@Override
public PrepareExecutor prepare(Plan plan) {
CodeExecuterContext codeExecuterContext = plan.getCodeExecuterContext();
PrepareExecutor bindable = codeExecuterContext.bindable;
if (bindable != null)
return bindable;
try {
return codeExecuterContext.bindable = PrepareExecutor.of((newMycatDataContext, mycatRowMetaData) -> {
DrdsSqlWithParams drdsSql = newMycatDataContext.getDrdsSql();
CalciteCompiler mycatCalciteCompiler = new CalciteCompiler();
PhysicalPlan factory = mycatCalciteCompiler.convert(plan.getMycatRel());
factory = fixCast(factory);
RexConverter rexConverter = mycatCalciteCompiler.getRexConverter();
Map<Integer, IndexedParameterLinkFunction> indexedMap = rexConverter.getIndexedParameterLinkFunctionMap();
List<Object> params = drdsSql.getParams();
if (!indexedMap.isEmpty()) {
for (int i = 0; i < params.size(); i++) {
Object o = params.get(i);
IndexedParameterLinkFunction indexedParameterLinkFunction = indexedMap.get(i);
if (indexedParameterLinkFunction != null) {
BindVariable base = (BindVariable) indexedParameterLinkFunction.getBase();
base.setObject(o);
}
}
}
List<SessionVariable> sessionMap = rexConverter.getSessionVariableFunctionMap();
for (SessionVariable sessionVariable : sessionMap) {
sessionVariable.setSession(newMycatDataContext.getContext());
}
AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = new AsyncMycatDataContextImpl.SqlMycatDataContextImpl(newMycatDataContext.getContext(), plan.getCodeExecuterContext(), drdsSql);
RootContext rootContext = new RootContext(sqlMycatDataContext);
Observable<VectorSchemaRoot> schemaRootObservable = factory.execute(rootContext);
return PrepareExecutor.ArrowObservable.of(mycatRowMetaData, schemaRootObservable);
}, getArrayBindable(codeExecuterContext));
} catch (Exception exception) {
LOGGER.error("", exception);
}
return null;
}
use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.
the class ShardingSQLHandler method onExecute.
@Override
protected Future<Void> onExecute(SQLRequest<SQLSelectStatement> request, MycatDataContext dataContext, Response response) {
Optional<Future<Void>> op = Optional.empty();
if (dataContext.isDebug()) {
op = testExample(request, dataContext, response);
if (op.isPresent())
return op.get();
}
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(request.getAst(), dataContext.getDefaultSchema());
HackRouter hackRouter = new HackRouter(drdsSqlWithParams.getParameterizedStatement(), dataContext);
try {
if (hackRouter.analyse()) {
Pair<String, String> plan = hackRouter.getPlan();
return response.proxySelect(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
} else {
return DrdsRunnerHelper.runOnDrds(dataContext, drdsSqlWithParams, response);
}
} catch (Throwable throwable) {
LOGGER.error(request.getAst().toString(), throwable);
return Future.failedFuture(throwable);
}
}
use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.
the class MycatViewPlan method execute.
@Override
public Observable<VectorSchemaRoot> execute(RootContext rootContext) {
RootAllocator rootAllocator = new RootAllocator();
NewMycatDataContext context = (NewMycatDataContext) rootContext.getContext();
DrdsSqlWithParams drdsSql = context.getDrdsSql();
MycatView view = (MycatView) mycatView;
CopyMycatRowMetaData rowMetaData = new CopyMycatRowMetaData(new CalciteRowMetaData(view.getRowType().getFieldList()));
MycatRelDatasourceSourceInfo mycatRelDatasourceSourceInfo = new MycatRelDatasourceSourceInfo(rowMetaData, view.getSQLTemplate(DrdsSql.isForUpdate(drdsSql.getParameterizedStatement())), view);
SqlNode sqlTemplate = mycatRelDatasourceSourceInfo.getSqlTemplate();
List<PartitionGroup> partitionGroups = AsyncMycatDataContextImpl.getSqlMap(Collections.emptyMap(), view, drdsSql, drdsSql.getHintDataNodeFilter());
ImmutableMultimap<String, SqlString> stringSqlStringImmutableMultimap = view.apply(-1, sqlTemplate, partitionGroups, drdsSql.getParams());
List<Observable<VectorSchemaRoot>> observableList = new ArrayList<>();
for (Map.Entry<String, SqlString> entry : stringSqlStringImmutableMultimap.entries()) {
String key = entry.getKey();
SqlString sqlString = entry.getValue();
observableList.add(Observable.create(emitter -> {
Future<NewMycatConnection> connectionFuture = context.getConnection(context.getContext().resolveDatasourceTargetName(key));
Future<Observable<VectorSchemaRoot>> observableFuture = connectionFuture.map(connection -> {
Observable<VectorSchemaRoot> observable = connection.prepareQuery(sqlString.getSql(), MycatPreparedStatementUtil.extractParams(drdsSql.getParams(), sqlString.getDynamicParameters()), MycatRelDataType.getMycatRelType(MycatViewPlan.this.schema()), rootAllocator);
return observable.doOnComplete(() -> context.recycleConnection(context.getContext().resolveDatasourceTargetName(key), Future.succeededFuture(connection)));
});
observableFuture.onFailure(event -> emitter.tryOnError(event));
observableFuture.onSuccess(event -> {
event = event.doOnComplete(() -> emitter.onComplete());
event = event.doOnError(throwable -> emitter.tryOnError(throwable));
event.forEach(vectorSchemaRoot -> emitter.onNext(vectorSchemaRoot));
});
}));
}
return Observable.fromIterable(observableList).flatMap(i -> i);
}
Aggregations