Search in sources :

Example 1 with SessionVariable

use of io.ordinate.engine.function.bind.SessionVariable in project Mycat2 by MyCATApache.

the class RexConverter method convertToFunction.

public Function convertToFunction(RexCall call, List<Function> aeOperands) {
    final Function ae;
    SqlKind kind = call.op.kind;
    String lowerName = kind.lowerName;
    switch(call.op.kind) {
        // Conjunction
        case AND:
            ae = executeCompiler.call(lowerName, aeOperands);
            break;
        case OR:
            if (aeOperands.size() == 2) {
                // Binary OR
                ae = executeCompiler.call(lowerName, aeOperands);
            } else {
                // COMPARE_IN
                ae = executeCompiler.call("in", aeOperands);
            }
            break;
        // Binary Comparison
        case EQUALS:
            ae = executeCompiler.call("=", aeOperands);
            break;
        case NOT_EQUALS:
            ae = executeCompiler.call("!=", aeOperands);
            break;
        case LESS_THAN:
            ae = executeCompiler.call("<", aeOperands);
            break;
        case GREATER_THAN:
            ae = executeCompiler.call(">", aeOperands);
            break;
        case LESS_THAN_OR_EQUAL:
            ae = executeCompiler.call("<=", aeOperands);
            break;
        case GREATER_THAN_OR_EQUAL:
            ae = executeCompiler.call(">=", aeOperands);
            break;
        case LIKE:
            ae = executeCompiler.call("like", aeOperands);
            break;
        // Arthimetic Operators
        case PLUS:
            // todo datetime
            ae = executeCompiler.call("+", aeOperands);
            break;
        case MINUS:
            // todo datetime
            // Check for DATETIME - INTERVAL expression first
            // For whatever reason Calcite treats + and - DATETIME operation differently
            ae = executeCompiler.call("-", aeOperands);
            break;
        case TIMES:
            ae = executeCompiler.call("*", aeOperands);
            break;
        case DIVIDE:
            ae = executeCompiler.call("/", aeOperands);
            break;
        case CAST:
            InnerType targetType = convertColumnType(call.getType());
            ae = executeCompiler.cast(aeOperands.get(0), targetType);
            break;
        case NOT:
            ae = executeCompiler.call("not", aeOperands.get(0));
            break;
        case IS_NULL:
            ae = executeCompiler.call("isNull", aeOperands.get(0));
            break;
        case IS_NOT_NULL:
            ae = executeCompiler.call("isNotNull", aeOperands.get(0));
            break;
        case EXISTS:
            ae = executeCompiler.call("exists", aeOperands.get(0));
            break;
        case CASE:
            ae = executeCompiler.call("case", aeOperands);
            break;
        case COALESCE:
            ae = executeCompiler.call("coalesce", aeOperands);
            break;
        case OTHER:
        case OTHER_FUNCTION:
        default:
            String callName = call.op.getName().toUpperCase();
            if (callName.contains("SESSIONVALUE")) {
                Function function = aeOperands.get(0);
                StringFunction stringFunction = (StringFunction) function;
                String name = stringFunction.getString(null).toString();
                SessionVariableFunction sessionVariableFunction = ExecuteCompiler.newSessionVariable(name);
                sessionVariableFunctionMap.add(sessionVariableFunction);
                ae = sessionVariableFunction;
            } else {
                ae = executeCompiler.call(callName, aeOperands);
                if (ae == null) {
                    throw new IllegalArgumentException("Unsupported Calcite expression type! " + call.op.kind.toString());
                }
                if (ae instanceof SessionVariable) {
                    sessionVariableFunctionMap.add((SessionVariable) ae);
                }
            }
    }
    Objects.requireNonNull(ae);
    return ae;
}
Also used : SessionVariableFunction(io.ordinate.engine.function.bind.SessionVariableFunction) ExtractFunction(io.mycat.calcite.sqlfunction.datefunction.ExtractFunction) VariableParameterFunction(io.ordinate.engine.function.bind.VariableParameterFunction) StringFunction(io.ordinate.engine.function.StringFunction) Function(io.ordinate.engine.function.Function) IndexedParameterLinkFunction(io.ordinate.engine.function.bind.IndexedParameterLinkFunction) SessionVariableFunction(io.ordinate.engine.function.bind.SessionVariableFunction) StringFunction(io.ordinate.engine.function.StringFunction) InnerType(io.ordinate.engine.schema.InnerType) NlsString(org.apache.calcite.util.NlsString) SessionVariable(io.ordinate.engine.function.bind.SessionVariable) SqlKind(org.apache.calcite.sql.SqlKind)

Example 2 with SessionVariable

use of io.ordinate.engine.function.bind.SessionVariable 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;
}
Also used : ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SneakyThrows(lombok.SneakyThrows) NewMycatDataContext(org.apache.calcite.runtime.NewMycatDataContext) LoggerFactory(org.slf4j.LoggerFactory) SessionVariable(io.ordinate.engine.function.bind.SessionVariable) CalciteCompiler(io.ordinate.engine.builder.CalciteCompiler) Utilities(org.apache.calcite.runtime.Utilities) Observable(io.reactivex.rxjava3.core.Observable) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) Map(java.util.Map) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) EnumerableInterpretable(org.apache.calcite.adapter.enumerable.EnumerableInterpretable) CalciteSystemProperty(org.apache.calcite.config.CalciteSystemProperty) IClassBodyEvaluator(org.codehaus.commons.compiler.IClassBodyEvaluator) FieldVector(org.apache.arrow.vector.FieldVector) Logger(org.slf4j.Logger) PhysicalPlan(io.ordinate.engine.physicalplan.PhysicalPlan) io.mycat.calcite(io.mycat.calcite) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) RexConverter(io.ordinate.engine.builder.RexConverter) Enumerable(org.apache.calcite.linq4j.Enumerable) BindVariable(io.ordinate.engine.function.bind.BindVariable) RootContext(io.ordinate.engine.record.RootContext) ArrayBindable(org.apache.calcite.runtime.ArrayBindable) List(java.util.List) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) CompilerFactoryFactory(org.codehaus.commons.compiler.CompilerFactoryFactory) StringReader(java.io.StringReader) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) IndexedParameterLinkFunction(io.ordinate.engine.function.bind.IndexedParameterLinkFunction) MycatDataContext(io.mycat.MycatDataContext) NotNull(org.jetbrains.annotations.NotNull) ICompilerFactory(org.codehaus.commons.compiler.ICompilerFactory) Plan(io.mycat.calcite.spm.Plan) PhysicalPlan(io.ordinate.engine.physicalplan.PhysicalPlan) CalciteCompiler(io.ordinate.engine.builder.CalciteCompiler) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) SessionVariable(io.ordinate.engine.function.bind.SessionVariable) Observable(io.reactivex.rxjava3.core.Observable) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) RootContext(io.ordinate.engine.record.RootContext) RexConverter(io.ordinate.engine.builder.RexConverter) List(java.util.List) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) BindVariable(io.ordinate.engine.function.bind.BindVariable) Map(java.util.Map) IndexedParameterLinkFunction(io.ordinate.engine.function.bind.IndexedParameterLinkFunction)

Aggregations

IndexedParameterLinkFunction (io.ordinate.engine.function.bind.IndexedParameterLinkFunction)2 SessionVariable (io.ordinate.engine.function.bind.SessionVariable)2 AsyncMycatDataContextImpl (io.mycat.AsyncMycatDataContextImpl)1 DrdsSqlWithParams (io.mycat.DrdsSqlWithParams)1 MycatDataContext (io.mycat.MycatDataContext)1 MysqlPayloadObject (io.mycat.api.collector.MysqlPayloadObject)1 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)1 ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)1 io.mycat.calcite (io.mycat.calcite)1 Plan (io.mycat.calcite.spm.Plan)1 ExtractFunction (io.mycat.calcite.sqlfunction.datefunction.ExtractFunction)1 CalciteCompiler (io.ordinate.engine.builder.CalciteCompiler)1 RexConverter (io.ordinate.engine.builder.RexConverter)1 Function (io.ordinate.engine.function.Function)1 StringFunction (io.ordinate.engine.function.StringFunction)1 BindVariable (io.ordinate.engine.function.bind.BindVariable)1 SessionVariableFunction (io.ordinate.engine.function.bind.SessionVariableFunction)1 VariableParameterFunction (io.ordinate.engine.function.bind.VariableParameterFunction)1 PhysicalPlan (io.ordinate.engine.physicalplan.PhysicalPlan)1 RootContext (io.ordinate.engine.record.RootContext)1