use of io.mycat.calcite.resultset.CalciteRowMetaData in project Mycat2 by MyCATApache.
the class PlanImpl method getMetaData.
@Override
public MycatRowMetaData getMetaData() {
MycatRel mycatRel = (MycatRel) relNode;
List<RelDataTypeField> fieldList = mycatRel.getRowType().getFieldList();
return new CalciteRowMetaData(fieldList, aliasList);
}
use of io.mycat.calcite.resultset.CalciteRowMetaData in project Mycat2 by MyCATApache.
the class MycatSQLTableLookup method dispatchRightObservable.
public static Observable<Object[]> dispatchRightObservable(NewMycatDataContext context, MycatSQLTableLookup tableLookup, Observable<Object[]> leftInput) {
MycatView rightView = (MycatView) tableLookup.getRight();
CopyMycatRowMetaData rightRowMetaData = new CopyMycatRowMetaData(new CalciteRowMetaData(rightView.getRowType().getFieldList()));
AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = (AsyncMycatDataContextImpl.SqlMycatDataContextImpl) context;
Observable<@NonNull List<Object[]>> buffer;
if (tableLookup.getType() == Type.BACK) {
// semi 可以分解
buffer = leftInput.buffer(300, 50);
} else {
// NONE 该运算不能分解
buffer = leftInput.toList().toObservable();
}
Observable<Object[]> rightObservable = buffer.flatMap(argsList -> {
if (argsList.isEmpty()) {
return Observable.empty();
}
RexShuttle rexShuttle = argSolver(argsList);
RelNode mycatInnerRelNode = rightView.getRelNode().accept(new RelShuttleImpl() {
@Override
public RelNode visit(RelNode other) {
if (other instanceof Filter) {
Filter filter = (Filter) other;
return filter.accept(rexShuttle);
} else if (other instanceof MycatTableLookupValues) {
MycatTableLookupValues mycatTableLookupValues = (MycatTableLookupValues) other;
return mycatTableLookupValues.apply(argsList);
}
return super.visit(other);
}
});
MycatView newRightView = new MycatView(rightView.getTraitSet(), mycatInnerRelNode, rightView.getDistribution(), rightView.getCondition().map(c -> c.accept(rexShuttle)).orElse(null));
DrdsSqlWithParams drdsSql = context.getDrdsSql();
SqlNode sqlTemplate = newRightView.getSQLTemplate(DrdsSqlWithParams.isForUpdate(drdsSql.getParameterizedSQL()));
ImmutableMultimap<String, SqlString> apply1 = newRightView.apply(context.getContext().getMergeUnionSize(), sqlTemplate, sqlMycatDataContext.getSqlMap(Collections.emptyMap(), newRightView, drdsSql, drdsSql.getHintDataNodeFilter()), drdsSql.getParams());
return Observable.merge(sqlMycatDataContext.getObservables(apply1, rightRowMetaData, (sessionConnection, sql, extractParams, calciteRowMetaData) -> VertxExecuter.runQuery(sessionConnection, sql, extractParams, calciteRowMetaData)));
});
return rightObservable;
}
use of io.mycat.calcite.resultset.CalciteRowMetaData 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);
}
use of io.mycat.calcite.resultset.CalciteRowMetaData in project Mycat2 by MyCATApache.
the class DrdsExecutorCompiler method getCodeExecuterContext.
@NotNull
@SneakyThrows
public static CodeExecuterContext getCodeExecuterContext(Map<RexNode, RexNode> constantMap, MycatRel relNode, boolean forUpdate) {
HashMap<String, Object> varContext = new HashMap<>(2);
StreamMycatEnumerableRelImplementor mycatEnumerableRelImplementor = new StreamMycatEnumerableRelImplementor(varContext);
HashMap<String, MycatRelDatasourceSourceInfo> stat = new HashMap<>();
relNode.accept(new RelShuttleImpl() {
@Override
public RelNode visit(RelNode other) {
CopyMycatRowMetaData rowMetaData = new CopyMycatRowMetaData(new CalciteRowMetaData(other.getRowType().getFieldList()));
if (other instanceof MycatView) {
MycatView view = (MycatView) other;
MycatRelDatasourceSourceInfo rel = stat.computeIfAbsent(other.getDigest(), s -> {
return new MycatRelDatasourceSourceInfo(rowMetaData, view.getSQLTemplate(forUpdate), view);
});
rel.refCount += 1;
}
if (other instanceof MycatTransientSQLTableScan) {
MycatTransientSQLTableScan tableScan = (MycatTransientSQLTableScan) other;
MycatRelDatasourceSourceInfo rel = stat.computeIfAbsent(other.getDigest(), s -> {
return new MycatRelDatasourceSourceInfo(rowMetaData, new TextSqlNode(tableScan.getSql()), tableScan);
});
rel.refCount += 1;
}
if (other instanceof MycatSQLTableLookup) {
MycatView right = ((MycatSQLTableLookup) other).getRight();
right.accept(this);
}
return super.visit(other);
}
});
ClassDeclaration classDeclaration = mycatEnumerableRelImplementor.implementHybridRoot(relNode, EnumerableRel.Prefer.ARRAY);
String code = Expressions.toString(classDeclaration.memberDeclarations, "\n", false);
if (log.isDebugEnabled()) {
log.debug("----------------------------------------code----------------------------------------");
log.debug(code);
}
CodeContext codeContext = new CodeContext(classDeclaration.name, code);
CodeExecuterContext executerContext = CodeExecuterContext.of(constantMap, stat, varContext, relNode, codeContext);
return executerContext;
}
Aggregations