Search in sources :

Example 6 with Reference

use of org.teiid.query.sql.symbol.Reference in project teiid by teiid.

the class ExecResolver method findCommandMetadata.

/**
 * @see org.teiid.query.resolver.CommandResolver#findCommandMetadata(org.teiid.query.sql.lang.Command,
 * org.teiid.query.metadata.QueryMetadataInterface)
 */
private void findCommandMetadata(Command command, TempMetadataStore discoveredMetadata, QueryMetadataInterface metadata) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
    StoredProcedure storedProcedureCommand = (StoredProcedure) command;
    StoredProcedureInfo storedProcedureInfo = null;
    try {
        storedProcedureInfo = metadata.getStoredProcedureInfoForProcedure(storedProcedureCommand.getProcedureName());
    } catch (QueryMetadataException e) {
        // $NON-NLS-1$
        String[] parts = storedProcedureCommand.getProcedureName().split("\\.", 2);
        if (parts.length > 1 && parts[0].equalsIgnoreCase(metadata.getVirtualDatabaseName())) {
            try {
                storedProcedureInfo = metadata.getStoredProcedureInfoForProcedure(parts[1]);
                storedProcedureCommand.setProcedureName(parts[1]);
            } catch (QueryMetadataException e1) {
            }
        }
        if (storedProcedureInfo == null) {
            throw e;
        }
    }
    storedProcedureCommand.setUpdateCount(storedProcedureInfo.getUpdateCount());
    storedProcedureCommand.setModelID(storedProcedureInfo.getModelID());
    storedProcedureCommand.setProcedureID(storedProcedureInfo.getProcedureID());
    storedProcedureCommand.setProcedureCallableName(storedProcedureInfo.getProcedureCallableName());
    // Get old parameters as they may have expressions set on them - collect
    // those expressions to copy later into the resolved parameters
    Collection<SPParameter> oldParams = storedProcedureCommand.getParameters();
    boolean namedParameters = storedProcedureCommand.displayNamedParameters();
    // of relying on all default values of all optional parameters.
    if (oldParams.size() == 0 || (oldParams.size() == 1 && storedProcedureCommand.isCalledWithReturn())) {
        storedProcedureCommand.setDisplayNamedParameters(true);
        namedParameters = true;
    }
    // Cache original input parameter expressions.  Depending on whether
    // the procedure was parsed with named or unnamed parameters, the keys
    // for this map will either be the String names of the parameters or
    // the Integer indices, as entered in the user query
    Map<Integer, Expression> positionalExpressions = new TreeMap<Integer, Expression>();
    Map<String, Expression> namedExpressions = new TreeMap<String, Expression>(String.CASE_INSENSITIVE_ORDER);
    int adjustIndex = 0;
    for (SPParameter param : oldParams) {
        if (param.getExpression() == null) {
            if (param.getParameterType() == SPParameter.RESULT_SET) {
                // If this was already resolved, just pretend the result set param doesn't exist
                adjustIndex--;
            }
            continue;
        }
        if (namedParameters && param.getParameterType() != SPParameter.RETURN_VALUE) {
            if (namedExpressions.put(param.getParameterSymbol().getShortName(), param.getExpression()) != null) {
                throw new QueryResolverException(QueryPlugin.Event.TEIID30138, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30138, param.getName()));
            }
        } else {
            positionalExpressions.put(param.getIndex() + adjustIndex, param.getExpression());
        }
    }
    storedProcedureCommand.clearParameters();
    int origInputs = positionalExpressions.size() + namedExpressions.size();
    /*
         * Take the values set from the stored procedure implementation, and match up with the
         * types of parameter it is from the metadata and then reset the newly joined parameters
         * into the stored procedure command.  If it is a result set get those columns and place
         * them into the stored procedure command as well.
         */
    List<SPParameter> metadataParams = storedProcedureInfo.getParameters();
    List<SPParameter> clonedMetadataParams = new ArrayList<SPParameter>(metadataParams.size());
    int inputParams = 0;
    int optionalParams = 0;
    int outParams = 0;
    boolean hasReturnValue = false;
    boolean optional = false;
    boolean varargs = false;
    for (int i = 0; i < metadataParams.size(); i++) {
        SPParameter metadataParameter = metadataParams.get(i);
        if ((metadataParameter.getParameterType() == ParameterInfo.IN) || (metadataParameter.getParameterType() == ParameterInfo.INOUT)) {
            if (ResolverUtil.hasDefault(metadataParameter.getMetadataID(), metadata) || metadataParameter.isVarArg()) {
                optional = true;
                optionalParams++;
            } else {
                inputParams++;
                if (optional) {
                    optional = false;
                    inputParams += optionalParams;
                    optionalParams = 0;
                }
            }
            if (metadataParameter.isVarArg()) {
                varargs = true;
            }
        } else if (metadataParameter.getParameterType() == ParameterInfo.OUT) {
            outParams++;
        /*
            	 * TODO: it would consistent to do the following, but it is a breaking change for procedures that have intermixed out params with in.
            	 * we may need to revisit this later
            	 */
        // optional = true;
        // optionalParams++;
        } else if (metadataParameter.getParameterType() == ParameterInfo.RETURN_VALUE) {
            hasReturnValue = true;
        }
        SPParameter clonedParam = (SPParameter) metadataParameter.clone();
        clonedMetadataParams.add(clonedParam);
        storedProcedureCommand.setParameter(clonedParam);
    }
    if (storedProcedureCommand.isCalledWithReturn() && !hasReturnValue) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID30139, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30139, storedProcedureCommand.getGroup()));
    }
    if (!namedParameters && (inputParams > positionalExpressions.size())) {
        // $NON-NLS-1$ //$NON-NLS-2$
        throw new QueryResolverException(QueryPlugin.Event.TEIID30140, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30140, inputParams, inputParams + optionalParams + (varargs ? "+" : ""), origInputs, storedProcedureCommand.getGroup()));
    }
    // Walk through the resolved parameters and set the expressions from the
    // input parameters
    int exprIndex = 1;
    HashSet<String> expected = new HashSet<String>();
    if (storedProcedureCommand.isCalledWithReturn() && hasReturnValue) {
        for (SPParameter param : clonedMetadataParams) {
            if (param.getParameterType() == SPParameter.RETURN_VALUE) {
                Expression expr = positionalExpressions.remove(exprIndex++);
                param.setExpression(expr);
                break;
            }
        }
    }
    for (SPParameter param : clonedMetadataParams) {
        if (param.getParameterType() == SPParameter.RESULT_SET || param.getParameterType() == SPParameter.RETURN_VALUE) {
            continue;
        }
        if (namedParameters) {
            String nameKey = param.getParameterSymbol().getShortName();
            Expression expr = namedExpressions.remove(nameKey);
            // With named parameters, have to check on optional params and default values
            if (expr == null) {
                if (param.getParameterType() != ParameterInfo.OUT) {
                    param.setUsingDefault(true);
                    expected.add(nameKey);
                    if (!param.isVarArg()) {
                        expr = ResolverUtil.getDefault(param.getParameterSymbol(), metadata);
                    } else {
                        // zero length array
                        List<Expression> exprs = new ArrayList<Expression>(0);
                        Array array = new Array(exprs);
                        array.setImplicit(true);
                        array.setType(param.getClassType());
                        expr = array;
                    }
                }
            }
            param.setExpression(expr);
        } else {
            Expression expr = positionalExpressions.remove(exprIndex++);
            if (param.getParameterType() == SPParameter.OUT) {
                if (expr != null) {
                    boolean isRef = expr instanceof Reference;
                    if (!isRef || exprIndex <= inputParams + 1) {
                        // for backwards compatibility, this should be treated instead as an input
                        exprIndex--;
                        positionalExpressions.put(exprIndex, expr);
                    } else if (isRef) {
                        // mimics the hack that was in PreparedStatementRequest.
                        Reference ref = (Reference) expr;
                        // may be an out
                        ref.setOptional(true);
                    /*
	                		 * Note that there is a corner case here with out parameters intermixed with optional parameters
	                		 * there's not a good way around this.
	                		 */
                    }
                }
                continue;
            }
            if (expr == null) {
                if (!param.isVarArg()) {
                    expr = ResolverUtil.getDefault(param.getParameterSymbol(), metadata);
                }
                param.setUsingDefault(true);
            }
            if (param.isVarArg()) {
                List<Expression> exprs = new ArrayList<Expression>(positionalExpressions.size() + 1);
                if (expr != null) {
                    exprs.add(expr);
                }
                exprs.addAll(positionalExpressions.values());
                positionalExpressions.clear();
                Array array = new Array(exprs);
                array.setImplicit(true);
                array.setType(param.getClassType());
                expr = array;
            }
            param.setExpression(expr);
        }
    }
    // Check for leftovers, i.e. params entered by user w/ wrong/unknown names
    if (!namedExpressions.isEmpty()) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID30141, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30141, namedExpressions.keySet(), expected));
    }
    if (!positionalExpressions.isEmpty()) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID31113, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31113, positionalExpressions.size(), origInputs, storedProcedureCommand.getGroup().toString()));
    }
    // Create temporary metadata that defines a group based on either the stored proc
    // name or the stored query name - this will be used later during planning
    String procName = storedProcedureCommand.getProcedureName();
    List tempElements = storedProcedureCommand.getProjectedSymbols();
    boolean isVirtual = storedProcedureInfo.getQueryPlan() != null;
    discoveredMetadata.addTempGroup(procName, tempElements, isVirtual);
    // Resolve tempElements against new metadata
    GroupSymbol procGroup = new GroupSymbol(storedProcedureInfo.getProcedureCallableName());
    procGroup.setProcedure(true);
    TempMetadataID tid = discoveredMetadata.getTempGroupID(procName);
    tid.setOriginalMetadataID(storedProcedureCommand.getProcedureID());
    procGroup.setMetadataID(tid);
    storedProcedureCommand.setGroup(procGroup);
}
Also used : SPParameter(org.teiid.query.sql.lang.SPParameter) Reference(org.teiid.query.sql.symbol.Reference) ArrayList(java.util.ArrayList) TempMetadataID(org.teiid.query.metadata.TempMetadataID) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TreeMap(java.util.TreeMap) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) StoredProcedureInfo(org.teiid.query.metadata.StoredProcedureInfo) Array(org.teiid.query.sql.symbol.Array) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 7 with Reference

use of org.teiid.query.sql.symbol.Reference in project teiid by teiid.

the class TestLimit method testToString.

public void testToString() {
    // $NON-NLS-1$
    assertEquals("LIMIT 50", new Limit(null, new Constant(new Integer(50))).toString());
    // $NON-NLS-1$
    assertEquals("LIMIT 100, 50", new Limit(new Constant(new Integer(100)), new Constant(new Integer(50))).toString());
    // $NON-NLS-1$
    assertEquals("LIMIT ?, ?", new Limit(new Reference(0), new Reference(1)).toString());
    // $NON-NLS-1$
    assertEquals("LIMIT -1, ?", new Limit(new Constant(new Integer(-1)), new Reference(1)).toString());
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) Reference(org.teiid.query.sql.symbol.Reference)

Example 8 with Reference

use of org.teiid.query.sql.symbol.Reference in project teiid by teiid.

the class TestLimit method testEquals.

public void testEquals() {
    Limit limit1 = new Limit(new Constant(new Integer(100)), new Constant(new Integer(50)));
    Limit limit2 = new Limit(new Constant(new Integer(100)), new Constant(new Integer(50)));
    Limit limit3 = new Limit(new Constant(new Integer(99)), new Constant(new Integer(50)));
    Limit limit4 = new Limit(new Constant(new Integer(100)), new Constant(new Integer(49)));
    Limit limit5 = new Limit(null, new Constant(new Integer(50)));
    Limit limit6 = new Limit(new Constant(new Integer(0)), new Constant(new Integer(50)));
    Limit limit7 = new Limit(new Reference(0), new Constant(new Integer(50)));
    Limit limit8 = new Limit(new Reference(0), new Reference(1));
    Limit limit9 = new Limit(null, new Reference(1));
    assertTrue(limit1.equals(limit2));
    assertTrue(limit2.equals(limit1));
    assertTrue(limit1.equals(limit1));
    assertFalse(limit1.equals(null));
    assertFalse(limit1.equals(limit3));
    assertFalse(limit1.equals(limit4));
    assertFalse(limit6.equals(limit7));
    assertFalse(limit7.equals(limit6));
    assertFalse(limit5.equals(limit7));
    assertFalse(limit7.equals(limit5));
    assertFalse(limit7.equals(limit8));
    assertFalse(limit8.equals(limit9));
    assertFalse(limit5.equals(limit9));
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) Reference(org.teiid.query.sql.symbol.Reference)

Example 9 with Reference

use of org.teiid.query.sql.symbol.Reference in project teiid by teiid.

the class MetaDataProcessor method getMetadataForCommand.

// For each projected symbol, construct a metadata map
private MetadataResult getMetadataForCommand(Command originalCommand) throws TeiidComponentException {
    Map<Integer, Object>[] columnMetadata = null;
    switch(originalCommand.getType()) {
        case Command.TYPE_QUERY:
            if (originalCommand instanceof Query) {
                if (((Query) originalCommand).getInto() == null) {
                    columnMetadata = createProjectedSymbolMetadata(originalCommand);
                }
            } else {
                columnMetadata = createProjectedSymbolMetadata(originalCommand);
            }
            break;
        case Command.TYPE_STORED_PROCEDURE:
            columnMetadata = createProjectedSymbolMetadata(originalCommand);
            break;
        case Command.TYPE_INSERT:
        case Command.TYPE_UPDATE:
        case Command.TYPE_DELETE:
        case Command.TYPE_CREATE:
        case Command.TYPE_DROP:
            break;
        default:
            if (originalCommand.returnsResultSet()) {
                columnMetadata = createProjectedSymbolMetadata(originalCommand);
            }
    }
    Map<Reference, String> paramMap = Collections.emptyMap();
    if (originalCommand instanceof StoredProcedure) {
        StoredProcedure sp = (StoredProcedure) originalCommand;
        paramMap = new HashMap<Reference, String>();
        Collection<SPParameter> params = sp.getParameters();
        for (SPParameter spParameter : params) {
            if (spParameter.getParameterType() != SPParameter.INOUT && spParameter.getParameterType() != SPParameter.IN && spParameter.getParameterType() != SPParameter.RETURN_VALUE) {
                continue;
            }
            Expression ex = spParameter.getExpression();
            if (ex instanceof Function && FunctionLibrary.isConvert((Function) ex)) {
                ex = ((Function) ex).getArg(0);
            }
            if (ex instanceof Reference) {
                paramMap.put((Reference) ex, spParameter.getParameterSymbol().getShortName());
            }
        }
    }
    List<Reference> params = ReferenceCollectorVisitor.getReferences(originalCommand);
    Map<Integer, Object>[] paramMetadata = new Map[params.size()];
    for (int i = 0; i < params.size(); i++) {
        Reference param = params.get(i);
        paramMetadata[i] = getDefaultColumn(null, paramMap.get(param), param.getType());
    }
    return new MetadataResult(columnMetadata, paramMetadata);
}
Also used : Query(org.teiid.query.sql.lang.Query) Reference(org.teiid.query.sql.symbol.Reference) SPParameter(org.teiid.query.sql.lang.SPParameter) MetadataResult(org.teiid.client.metadata.MetadataResult) Function(org.teiid.query.sql.symbol.Function) WindowFunction(org.teiid.query.sql.symbol.WindowFunction) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Expression(org.teiid.query.sql.symbol.Expression) HashMap(java.util.HashMap) Map(java.util.Map) SymbolMap(org.teiid.query.sql.util.SymbolMap)

Example 10 with Reference

use of org.teiid.query.sql.symbol.Reference in project teiid by teiid.

the class PreparedStatementRequest method generatePlan.

/**
 * @throws TeiidComponentException
 * @throws TeiidProcessingException
 * @see org.teiid.dqp.internal.process.Request#generatePlan()
 */
@Override
protected void generatePlan(boolean addLimit) throws TeiidComponentException, TeiidProcessingException {
    createCommandContext();
    String sqlQuery = requestMsg.getCommands()[0];
    if (this.preParser != null) {
        sqlQuery = this.preParser.preParse(sqlQuery, this.context);
    }
    CacheID id = new CacheID(this.workContext, Request.createParseInfo(this.requestMsg, this.workContext.getSession()), sqlQuery);
    prepPlan = prepPlanCache.get(id);
    if (prepPlan != null) {
        // already in cache. obtain the values from cache
        analysisRecord = prepPlan.getAnalysisRecord();
        ProcessorPlan cachedPlan = prepPlan.getPlan();
        this.userCommand = prepPlan.getCommand();
        if (validateAccess(requestMsg.getCommands(), userCommand, CommandType.PREPARED)) {
            // $NON-NLS-1$
            LogManager.logDetail(LogConstants.CTX_DQP, requestId, "AuthorizationValidator indicates that the prepared plan for command will not be used");
            prepPlan = null;
            analysisRecord = null;
        } else {
            // $NON-NLS-1$
            LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Query exist in cache: ", sqlQuery });
            processPlan = cachedPlan.clone();
        }
    }
    if (prepPlan == null) {
        // if prepared plan does not exist, create one
        prepPlan = new PreparedPlan();
        // $NON-NLS-1$
        LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Query does not exist in cache: ", sqlQuery });
        super.generatePlan(true);
        prepPlan.setCommand(this.userCommand);
        // there's no need to cache the plan if it's a stored procedure, since we already do that in the optimizer
        boolean cache = !(this.userCommand instanceof StoredProcedure);
        // Defect 13751: Clone the plan in its current state (i.e. before processing) so that it can be used for later queries
        prepPlan.setPlan(cache ? processPlan.clone() : processPlan, this.context);
        prepPlan.setAnalysisRecord(analysisRecord);
        if (cache) {
            Determinism determinismLevel = this.context.getDeterminismLevel();
            if (userCommand.getCacheHint() != null && userCommand.getCacheHint().getDeterminism() != null) {
                // $NON-NLS-1$ //$NON-NLS-2$
                LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Cache hint modified the query determinism from ", this.context.getDeterminismLevel(), " to ", determinismLevel });
                determinismLevel = userCommand.getCacheHint().getDeterminism();
            }
            this.prepPlanCache.put(id, determinismLevel, prepPlan, userCommand.getCacheHint() != null ? userCommand.getCacheHint().getTtl() : null);
        }
    }
    if (requestMsg.isBatchedUpdate()) {
        handlePreparedBatchUpdate();
    } else {
        List<Reference> params = prepPlan.getReferences();
        List<?> values = requestMsg.getParameterValues();
        PreparedStatementRequest.resolveParameterValues(params, values, this.context, this.metadata);
    }
}
Also used : Determinism(org.teiid.metadata.FunctionMethod.Determinism) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) Reference(org.teiid.query.sql.symbol.Reference) ProcessorPlan(org.teiid.query.processor.ProcessorPlan)

Aggregations

Reference (org.teiid.query.sql.symbol.Reference)42 Test (org.junit.Test)15 Constant (org.teiid.query.sql.symbol.Constant)14 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)14 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)12 Expression (org.teiid.query.sql.symbol.Expression)11 Limit (org.teiid.query.sql.lang.Limit)7 Query (org.teiid.query.sql.lang.Query)7 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)7 ArrayList (java.util.ArrayList)6 Criteria (org.teiid.query.sql.lang.Criteria)6 LinkedList (java.util.LinkedList)5 From (org.teiid.query.sql.lang.From)5 Select (org.teiid.query.sql.lang.Select)5 SetQuery (org.teiid.query.sql.lang.SetQuery)5 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)5 UnaryFromClause (org.teiid.query.sql.lang.UnaryFromClause)5 Column (org.teiid.metadata.Column)4 Command (org.teiid.query.sql.lang.Command)4 SPParameter (org.teiid.query.sql.lang.SPParameter)4