Search in sources :

Example 11 with VariableContext

use of org.teiid.query.sql.util.VariableContext in project teiid by teiid.

the class QueryProcessorFactoryImpl method createQueryProcessor.

@Override
public QueryProcessor createQueryProcessor(String query, String recursionGroup, CommandContext commandContext, Object... params) throws TeiidProcessingException, TeiidComponentException {
    CommandContext copy = commandContext.clone();
    copy.resetDeterminismLevel(true);
    copy.setDataObjects(null);
    QueryMetadataInterface metadata = commandContext.getMetadata();
    if (metadata == null) {
        metadata = defaultMetadata;
    }
    PreparedPlan pp = getPreparedPlan(query, recursionGroup, copy, metadata);
    copy.pushVariableContext(new VariableContext());
    PreparedStatementRequest.resolveParameterValues(pp.getReferences(), Arrays.asList(params), copy, metadata);
    return new QueryProcessor(pp.getPlan().clone(), copy, bufferMgr, dataMgr);
}
Also used : CommandContext(org.teiid.query.util.CommandContext) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) VariableContext(org.teiid.query.sql.util.VariableContext) QueryProcessor(org.teiid.query.processor.QueryProcessor)

Example 12 with VariableContext

use of org.teiid.query.sql.util.VariableContext in project teiid by teiid.

the class TestProcessor method setParameterValues.

public static void setParameterValues(List<?> values, Command command, CommandContext context) {
    VariableContext vc = new VariableContext();
    Iterator<?> valIter = values.iterator();
    for (Reference ref : ReferenceCollectorVisitor.getReferences(command)) {
        if (!ref.isPositional()) {
            continue;
        }
        // $NON-NLS-1$
        vc.setGlobalValue(ref.getContextSymbol(), valIter.next());
    }
    context.setVariableContext(vc);
}
Also used : Reference(org.teiid.query.sql.symbol.Reference) VariableContext(org.teiid.query.sql.util.VariableContext)

Example 13 with VariableContext

use of org.teiid.query.sql.util.VariableContext in project teiid by teiid.

the class PreparedStatementRequest method handlePreparedBatchUpdate.

/**
 * There are two cases
 *   if
 *     The source supports preparedBatchUpdate -> just let the command and values pass to the source
 *   else
 *     create a batchedupdatecommand that represents the batch operation
 * @param command
 * @throws QueryMetadataException
 * @throws TeiidComponentException
 * @throws QueryResolverException
 * @throws QueryPlannerException
 * @throws QueryValidatorException
 */
private void handlePreparedBatchUpdate() throws QueryMetadataException, TeiidComponentException, QueryResolverException, QueryPlannerException, QueryValidatorException {
    List<List<?>> paramValues = (List<List<?>>) requestMsg.getParameterValues();
    if (paramValues.isEmpty()) {
        throw new QueryValidatorException(QueryPlugin.Event.TEIID30555, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30555));
    }
    boolean supportPreparedBatchUpdate = false;
    Command command = null;
    if (this.processPlan instanceof RelationalPlan) {
        RelationalPlan rPlan = (RelationalPlan) this.processPlan;
        if (rPlan.getRootNode() instanceof AccessNode) {
            AccessNode aNode = (AccessNode) rPlan.getRootNode();
            String modelName = aNode.getModelName();
            command = aNode.getCommand();
            SourceCapabilities caps = capabilitiesFinder.findCapabilities(modelName);
            supportPreparedBatchUpdate = caps.supportsCapability(SourceCapabilities.Capability.BULK_UPDATE);
            if (supportPreparedBatchUpdate && // only allow the plan if the multi-valued references result in expressions that can be pushed
            !CriteriaCapabilityValidatorVisitor.canPushLanguageObject(command, metadata.getModelID(modelName), metadata, capabilitiesFinder, analysisRecord, false, false, true)) {
                supportPreparedBatchUpdate = false;
            }
        }
    }
    List<Command> commands = new LinkedList<Command>();
    List<VariableContext> contexts = new LinkedList<VariableContext>();
    List<List<Object>> multiValues = new ArrayList<List<Object>>(this.prepPlan.getReferences().size());
    for (List<?> values : paramValues) {
        PreparedStatementRequest.resolveParameterValues(this.prepPlan.getReferences(), values, this.context, this.metadata);
        contexts.add(this.context.getVariableContext());
        if (supportPreparedBatchUpdate) {
            if (multiValues.isEmpty()) {
                for (int i = 0; i < values.size(); i++) {
                    multiValues.add(new ArrayList<Object>(paramValues.size()));
                }
            }
            for (int i = 0; i < values.size(); i++) {
                List<Object> multiValue = multiValues.get(i);
                Object value = this.context.getVariableContext().getGlobalValue(this.prepPlan.getReferences().get(i).getContextSymbol());
                multiValue.add(value);
            }
        } else {
            // just accumulate copies of the command/plan - clones are not necessary
            if (command == null) {
                command = this.prepPlan.getCommand();
            }
            command.setProcessorPlan(this.processPlan);
            commands.add(command);
        }
    }
    if (paramValues.size() > 1) {
        this.context.setVariableContext(new VariableContext());
    }
    if (paramValues.size() == 1) {
        // just use the existing plan, and global reference evaluation
        return;
    }
    if (supportPreparedBatchUpdate) {
        for (int i = 0; i < this.prepPlan.getReferences().size(); i++) {
            Constant c = new Constant(null, this.prepPlan.getReferences().get(i).getType());
            c.setMultiValued(multiValues.get(i));
            this.context.getVariableContext().setGlobalValue(this.prepPlan.getReferences().get(i).getContextSymbol(), c);
        }
        return;
    }
    BatchedUpdateCommand buc = new BatchedUpdateCommand(commands);
    buc.setVariableContexts(contexts);
    BatchedUpdatePlanner planner = new BatchedUpdatePlanner();
    this.processPlan = planner.optimize(buc, idGenerator, metadata, capabilitiesFinder, analysisRecord, context);
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) RelationalPlan(org.teiid.query.processor.relational.RelationalPlan) VariableContext(org.teiid.query.sql.util.VariableContext) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) LinkedList(java.util.LinkedList) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) Command(org.teiid.query.sql.lang.Command) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AccessNode(org.teiid.query.processor.relational.AccessNode) SourceCapabilities(org.teiid.query.optimizer.capabilities.SourceCapabilities) BatchedUpdatePlanner(org.teiid.query.optimizer.BatchedUpdatePlanner)

Example 14 with VariableContext

use of org.teiid.query.sql.util.VariableContext in project teiid by teiid.

the class PreparedStatementRequest method resolveParameterValues.

/**
 * @param params
 * @param values
 * @throws QueryResolverException
 * @throws QueryValidatorException
 */
public static void resolveParameterValues(List<Reference> params, List values, CommandContext context, QueryMetadataInterface metadata) throws QueryResolverException, TeiidComponentException, QueryValidatorException {
    VariableContext result = new VariableContext();
    // the size of the values must be the same as that of the parameters
    if (params.size() != values.size()) {
        // $NON-NLS-1$
        String msg = QueryPlugin.Util.getString("QueryUtil.wrong_number_of_values", new Object[] { new Integer(values.size()), new Integer(params.size()) });
        throw new QueryResolverException(QueryPlugin.Event.TEIID30556, msg);
    }
    boolean embedded = context != null && context.getSession() != null && context.getSession().isEmbedded();
    // to that of the reference
    for (int i = 0; i < params.size(); i++) {
        Reference param = params.get(i);
        Object value = values.get(i);
        if (value != null) {
            if (embedded && value instanceof BaseLob) {
                createStreamCopy(context, i, param, value);
            }
            try {
                String targetTypeName = DataTypeManager.getDataTypeName(param.getType());
                Expression expr = ResolverUtil.convertExpression(new Constant(DataTypeManager.convertToRuntimeType(value, param.getType() != DataTypeManager.DefaultDataClasses.OBJECT)), targetTypeName, metadata);
                value = Evaluator.evaluate(expr);
            } catch (ExpressionEvaluationException e) {
                // $NON-NLS-1$
                String msg = QueryPlugin.Util.getString("QueryUtil.Error_executing_conversion_function_to_convert_value", i + 1, value, value.getClass(), DataTypeManager.getDataTypeName(param.getType()));
                throw new QueryResolverException(QueryPlugin.Event.TEIID30557, e, msg);
            } catch (QueryResolverException e) {
                // $NON-NLS-1$
                String msg = QueryPlugin.Util.getString("QueryUtil.Error_executing_conversion_function_to_convert_value", i + 1, value, value.getClass(), DataTypeManager.getDataTypeName(param.getType()));
                throw new QueryResolverException(QueryPlugin.Event.TEIID30558, e, msg);
            }
        }
        if (param.getConstraint() != null) {
            param.getConstraint().validate(value);
        }
        // bind variable
        result.setGlobalValue(param.getContextSymbol(), value);
    }
    context.setVariableContext(result);
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) Expression(org.teiid.query.sql.symbol.Expression) Reference(org.teiid.query.sql.symbol.Reference) Constant(org.teiid.query.sql.symbol.Constant) BaseLob(org.teiid.core.types.BaseLob) VariableContext(org.teiid.query.sql.util.VariableContext) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 15 with VariableContext

use of org.teiid.query.sql.util.VariableContext in project teiid by teiid.

the class Evaluator method getRowValue.

private RowValue getRowValue(final List<?> tuple, final LanguageObject lo) {
    if (lo instanceof GroupSymbol) {
        GroupSymbol leftRowValue = (GroupSymbol) lo;
        TempMetadataID id = (TempMetadataID) leftRowValue.getMetadataID();
        VariableContext vc = this.context.getVariableContext();
        List<TempMetadataID> cols = id.getElements();
        return new RowValue() {

            @Override
            public int length() {
                return cols.size();
            }

            @Override
            public Object get(int index) {
                return vc.getValue(new ElementSymbol(cols.get(index).getName(), leftRowValue));
            }
        };
    }
    return new RowValue() {

        @Override
        public int length() {
            return 1;
        }

        @Override
        public Object get(int index) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
            return internalEvaluate((Expression) lo, tuple);
        }
    };
}
Also used : TempMetadataID(org.teiid.query.metadata.TempMetadataID) VariableContext(org.teiid.query.sql.util.VariableContext)

Aggregations

VariableContext (org.teiid.query.sql.util.VariableContext)22 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)6 Expression (org.teiid.query.sql.symbol.Expression)6 List (java.util.List)5 TransactionContext (org.teiid.dqp.service.TransactionContext)5 CommandContext (org.teiid.query.util.CommandContext)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)3 TeiidProcessingException (org.teiid.core.TeiidProcessingException)3 QueryProcessor (org.teiid.query.processor.QueryProcessor)3 Command (org.teiid.query.sql.lang.Command)3 Constant (org.teiid.query.sql.symbol.Constant)3 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)2 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)2 XATransactionException (org.teiid.client.xa.XATransactionException)2 BlockedException (org.teiid.common.buffer.BlockedException)2 TupleBatch (org.teiid.common.buffer.TupleBatch)2 TeiidComponentException (org.teiid.core.TeiidComponentException)2