Search in sources :

Example 1 with QueryCommand

use of org.teiid.query.sql.lang.QueryCommand in project teiid by teiid.

the class SetQueryResolver method setProjectedTypes.

private void setProjectedTypes(SetQuery setQuery, List<Class<?>> firstProjectTypes, QueryMetadataInterface metadata) throws QueryResolverException {
    for (QueryCommand subCommand : setQuery.getQueryCommands()) {
        if (!(subCommand instanceof SetQuery)) {
            continue;
        }
        SetQuery child = (SetQuery) subCommand;
        List projectedSymbols = child.getProjectedSymbols();
        if (child.getOrderBy() != null) {
            for (int j = 0; j < projectedSymbols.size(); j++) {
                Expression ses = (Expression) projectedSymbols.get(j);
                Class<?> targetType = firstProjectTypes.get(j);
                if (ses.getType() != targetType && orderByContainsVariable(child.getOrderBy(), ses, j)) {
                    String sourceTypeName = DataTypeManager.getDataTypeName(ses.getType());
                    String targetTypeName = DataTypeManager.getDataTypeName(targetType);
                    throw new QueryResolverException(// $NON-NLS-1$
                    QueryPlugin.Util.getString(// $NON-NLS-1$
                    "UnionQueryResolver.type_conversion", new Object[] { ses, sourceTypeName, targetTypeName }));
                }
            }
        }
        child.setProjectedTypes(firstProjectTypes, metadata);
        setProjectedTypes(child, firstProjectTypes, metadata);
    }
}
Also used : SetQuery(org.teiid.query.sql.lang.SetQuery) Expression(org.teiid.query.sql.symbol.Expression) QueryCommand(org.teiid.query.sql.lang.QueryCommand) ArrayList(java.util.ArrayList) List(java.util.List) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 2 with QueryCommand

use of org.teiid.query.sql.lang.QueryCommand in project teiid by teiid.

the class AccessNode method nextCommand.

@SuppressWarnings("unused")
protected Command nextCommand() throws TeiidProcessingException, TeiidComponentException {
    // to ensure that the subquery ids remain stable
    if (nextCommand == null) {
        nextCommand = (Command) processingCommand.clone();
        if (evaluatedPlans != null) {
            for (WithQueryCommand with : ((QueryCommand) nextCommand).getWith()) {
                TupleBuffer tb = evaluatedPlans.get(with.getGroupSymbol()).collector.getTupleBuffer();
                with.setTupleBuffer(tb);
            }
        }
    }
    return nextCommand;
}
Also used : TupleBuffer(org.teiid.common.buffer.TupleBuffer) QueryCommand(org.teiid.query.sql.lang.QueryCommand) WithQueryCommand(org.teiid.query.sql.lang.WithQueryCommand) WithQueryCommand(org.teiid.query.sql.lang.WithQueryCommand)

Example 3 with QueryCommand

use of org.teiid.query.sql.lang.QueryCommand in project teiid by teiid.

the class ScalarSubquery method clone.

/**
 * Returns a safe clone
 * @see java.lang.Object#clone()
 */
public Object clone() {
    QueryCommand copyCommand = null;
    if (getCommand() != null) {
        copyCommand = (QueryCommand) getCommand().clone();
    }
    ScalarSubquery clone = new ScalarSubquery(copyCommand);
    // Don't invoke the lazy-loading getType()
    clone.setType(this.type);
    clone.shouldEvaluate = this.shouldEvaluate;
    if (this.subqueryHint != null) {
        clone.subqueryHint = subqueryHint.clone();
    }
    return clone;
}
Also used : QueryCommand(org.teiid.query.sql.lang.QueryCommand)

Example 4 with QueryCommand

use of org.teiid.query.sql.lang.QueryCommand in project teiid by teiid.

the class MetadataValidator method validate.

private void validate(VDBMetaData vdb, ModelMetaData model, AbstractMetadataRecord record, ValidatorReport report, QueryMetadataInterface metadata, MetadataFactory mf) {
    ValidatorReport resolverReport = null;
    try {
        if (record instanceof Procedure) {
            Procedure p = (Procedure) record;
            Command command = parser.parseProcedure(p.getQueryPlan(), false);
            validateNoReferences(command, report, model);
            QueryResolver.resolveCommand(command, new GroupSymbol(p.getFullName()), Command.TYPE_STORED_PROCEDURE, metadata, false);
            resolverReport = Validator.validate(command, metadata);
            determineDependencies(p, command);
        } else if (record instanceof Table) {
            Table t = (Table) record;
            GroupSymbol symbol = new GroupSymbol(t.getFullName());
            ResolverUtil.resolveGroup(symbol, metadata);
            String selectTransformation = t.getSelectTransformation();
            QueryNode node = null;
            if (t.isVirtual()) {
                QueryCommand command = (QueryCommand) parser.parseCommand(selectTransformation);
                validateNoReferences(command, report, model);
                QueryResolver.resolveCommand(command, metadata);
                resolverReport = Validator.validate(command, metadata);
                if (!resolverReport.hasItems() && (t.getColumns() == null || t.getColumns().isEmpty())) {
                    List<Expression> symbols = command.getProjectedSymbols();
                    for (Expression column : symbols) {
                        try {
                            addColumn(Symbol.getShortName(column), column.getType(), t, mf);
                        } catch (TranslatorException e) {
                            log(report, model, e.getMessage());
                        }
                    }
                }
                node = QueryResolver.resolveView(symbol, new QueryNode(selectTransformation), SQLConstants.Reserved.SELECT, metadata, true);
                if (t.getColumns() != null && !t.getColumns().isEmpty()) {
                    determineDependencies(t, command);
                    if (t.getInsertPlan() != null && t.isInsertPlanEnabled()) {
                        validateUpdatePlan(model, report, metadata, t, t.getInsertPlan(), Command.TYPE_INSERT);
                    }
                    if (t.getUpdatePlan() != null && t.isUpdatePlanEnabled()) {
                        validateUpdatePlan(model, report, metadata, t, t.getUpdatePlan(), Command.TYPE_UPDATE);
                    }
                    if (t.getDeletePlan() != null && t.isDeletePlanEnabled()) {
                        validateUpdatePlan(model, report, metadata, t, t.getDeletePlan(), Command.TYPE_DELETE);
                    }
                }
            }
            boolean addCacheHint = false;
            if (t.isVirtual() && t.isMaterialized() && t.getMaterializedTable() == null) {
                List<KeyRecord> fbis = t.getFunctionBasedIndexes();
                List<GroupSymbol> groups = Arrays.asList(symbol);
                if (fbis != null && !fbis.isEmpty()) {
                    for (KeyRecord fbi : fbis) {
                        for (int j = 0; j < fbi.getColumns().size(); j++) {
                            Column c = fbi.getColumns().get(j);
                            if (c.getParent() != fbi) {
                                continue;
                            }
                            String exprString = c.getNameInSource();
                            try {
                                Expression ex = parser.parseExpression(exprString);
                                validateNoReferences(ex, report, model);
                                ResolverVisitor.resolveLanguageObject(ex, groups, metadata);
                                if (!ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(ex).isEmpty()) {
                                    log(report, model, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31114, exprString, fbi.getFullName()));
                                }
                                EvaluatableVisitor ev = new EvaluatableVisitor();
                                PreOrPostOrderNavigator.doVisit(ex, ev, PreOrPostOrderNavigator.PRE_ORDER);
                                if (ev.getDeterminismLevel().compareTo(Determinism.VDB_DETERMINISTIC) < 0) {
                                    log(report, model, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31115, exprString, fbi.getFullName()));
                                }
                            } catch (QueryResolverException e) {
                                log(report, model, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31116, exprString, fbi.getFullName(), e.getMessage()));
                            }
                        }
                    }
                }
            } else {
                addCacheHint = true;
            }
            if (node != null && addCacheHint && t.isMaterialized()) {
                CacheHint cacheHint = node.getCommand().getCacheHint();
                Long ttl = -1L;
                if (cacheHint != null) {
                    if (cacheHint.getTtl() != null && t.getProperty(MaterializationMetadataRepository.MATVIEW_TTL, false) == null) {
                        ttl = cacheHint.getTtl();
                        t.setProperty(MaterializationMetadataRepository.MATVIEW_TTL, String.valueOf(ttl));
                    }
                    if (cacheHint.getUpdatable() != null && t.getProperty(MaterializationMetadataRepository.MATVIEW_UPDATABLE, false) == null) {
                        t.setProperty(MaterializationMetadataRepository.MATVIEW_UPDATABLE, String.valueOf(cacheHint.getUpdatable()));
                    }
                    if (cacheHint.getPrefersMemory() != null && t.getProperty(MaterializationMetadataRepository.MATVIEW_PREFER_MEMORY, false) == null) {
                        t.setProperty(MaterializationMetadataRepository.MATVIEW_PREFER_MEMORY, String.valueOf(cacheHint.getPrefersMemory()));
                    }
                    if (cacheHint.getScope() != null && t.getProperty(MaterializationMetadataRepository.MATVIEW_SHARE_SCOPE, false) == null) {
                        log(report, model, Severity.WARNING, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31252, t.getName(), cacheHint.getScope().name()));
                        t.setProperty(MaterializationMetadataRepository.MATVIEW_SHARE_SCOPE, MaterializationMetadataRepository.Scope.IMPORTED.name());
                    }
                }
            }
        }
        processReport(model, record, report, resolverReport);
    } catch (TeiidException e) {
        log(report, model, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31080, record.getFullName(), e.getMessage()));
    }
}
Also used : CacheHint(org.teiid.query.sql.lang.CacheHint) ValidatorReport(org.teiid.query.validator.ValidatorReport) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) TeiidException(org.teiid.core.TeiidException) EvaluatableVisitor(org.teiid.query.sql.visitor.EvaluatableVisitor) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) Command(org.teiid.query.sql.lang.Command) QueryCommand(org.teiid.query.sql.lang.QueryCommand) Expression(org.teiid.query.sql.symbol.Expression) QueryNode(org.teiid.query.mapping.relational.QueryNode) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryCommand(org.teiid.query.sql.lang.QueryCommand) TranslatorException(org.teiid.translator.TranslatorException)

Example 5 with QueryCommand

use of org.teiid.query.sql.lang.QueryCommand in project teiid by teiid.

the class Request method generatePlan.

/**
 * state side effects:
 *      creates the analysis record
 * 		creates the command context
 * 		sets the pre-rewrite command on the request
 * 		adds a limit clause if the row limit is specified
 * 		sets the processor plan
 *
 * @throws TeiidComponentException
 * @throws TeiidProcessingException
 */
protected void generatePlan(boolean prepared) throws TeiidComponentException, TeiidProcessingException {
    createCommandContext();
    Command command = parseCommand();
    List<Reference> references = ReferenceCollectorVisitor.getReferences(command);
    getAnalysisRecord();
    resolveCommand(command);
    checkReferences(references);
    validateAccess(requestMsg.getCommands(), command, CommandType.USER);
    this.userCommand = (Command) command.clone();
    Collection<GroupSymbol> groups = GroupCollectorVisitor.getGroups(command, true);
    for (GroupSymbol groupSymbol : groups) {
        if (groupSymbol.isTempTable()) {
            this.context.setDeterminismLevel(Determinism.SESSION_DETERMINISTIC);
            break;
        }
    }
    validateQuery(command);
    command = QueryRewriter.rewrite(command, metadata, context);
    /*
         * Adds a row limit to a query if Statement.setMaxRows has been called and the command
         * doesn't already have a limit clause.
         */
    if (!prepared && requestMsg.getRowLimit() > 0 && command instanceof QueryCommand) {
        QueryCommand query = (QueryCommand) command;
        if (query.getLimit() == null) {
            query.setLimit(new Limit(null, new Constant(new Integer(requestMsg.getRowLimit()), DataTypeManager.DefaultDataClasses.INTEGER)));
            this.addedLimit = true;
        }
    }
    boolean debug = analysisRecord.recordDebug();
    if (debug) {
        // $NON-NLS-1$
        analysisRecord.println("\n============================================================================");
        // $NON-NLS-1$
        analysisRecord.println("USER COMMAND:\n" + command);
    }
    // Run the optimizer
    try {
        CommandContext.pushThreadLocalContext(context);
        processPlan = QueryOptimizer.optimizePlan(command, metadata, idGenerator, capabilitiesFinder, analysisRecord, context);
    } finally {
        CommandContext.popThreadLocalContext();
        String debugLog = analysisRecord.getDebugLog();
        if (debugLog != null && debugLog.length() > 0) {
            LogManager.log(requestMsg.getShowPlan() == ShowPlan.DEBUG ? MessageLevel.INFO : MessageLevel.TRACE, LogConstants.CTX_QUERY_PLANNER, debugLog);
        }
        if (analysisRecord.recordAnnotations() && analysisRecord.getAnnotations() != null && !analysisRecord.getAnnotations().isEmpty()) {
            LogManager.logDetail(LogConstants.CTX_QUERY_PLANNER, analysisRecord.getAnnotations());
        }
    }
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_DQP, new Object[] { QueryPlugin.Util.getString("BasicInterceptor.ProcessTree_for__4"), requestId, processPlan });
}
Also used : CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) Command(org.teiid.query.sql.lang.Command) QueryCommand(org.teiid.query.sql.lang.QueryCommand) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) Reference(org.teiid.query.sql.symbol.Reference) Constant(org.teiid.query.sql.symbol.Constant) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryCommand(org.teiid.query.sql.lang.QueryCommand) Limit(org.teiid.query.sql.lang.Limit)

Aggregations

QueryCommand (org.teiid.query.sql.lang.QueryCommand)7 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)2 Command (org.teiid.query.sql.lang.Command)2 SetQuery (org.teiid.query.sql.lang.SetQuery)2 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)2 Expression (org.teiid.query.sql.symbol.Expression)2 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TupleBuffer (org.teiid.common.buffer.TupleBuffer)1 TeiidException (org.teiid.core.TeiidException)1 BatchedUpdates (org.teiid.language.BatchedUpdates)1 BulkCommand (org.teiid.language.BulkCommand)1 Call (org.teiid.language.Call)1 QueryNode (org.teiid.query.mapping.relational.QueryNode)1 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)1 CacheHint (org.teiid.query.sql.lang.CacheHint)1 Limit (org.teiid.query.sql.lang.Limit)1 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)1 WithQueryCommand (org.teiid.query.sql.lang.WithQueryCommand)1