Search in sources :

Example 36 with TempMetadataID

use of org.teiid.query.metadata.TempMetadataID 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)

Example 37 with TempMetadataID

use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.

the class QueryOptimizer method optimizePlan.

public static ProcessorPlan optimizePlan(Command command, QueryMetadataInterface metadata, IDGenerator idGenerator, CapabilitiesFinder capFinder, AnalysisRecord analysisRecord, CommandContext context) throws QueryMetadataException, TeiidComponentException, QueryPlannerException {
    if (analysisRecord == null) {
        analysisRecord = new AnalysisRecord(false, false);
    }
    if (context == null) {
        context = new CommandContext();
    }
    if (!(capFinder instanceof TempCapabilitiesFinder)) {
        capFinder = new TempCapabilitiesFinder(capFinder);
    }
    boolean debug = analysisRecord.recordDebug();
    if (!(metadata instanceof TempMetadataAdapter)) {
        metadata = new TempMetadataAdapter(metadata, new TempMetadataStore());
    }
    if (context.getMetadata() == null) {
        context.setMetadata(metadata);
    }
    // Create an ID generator that can be used for all plans to generate unique data node IDs
    if (idGenerator == null) {
        idGenerator = new IDGenerator();
    }
    if (debug) {
        // $NON-NLS-1$
        analysisRecord.println("\n----------------------------------------------------------------------------");
        // $NON-NLS-1$
        analysisRecord.println("OPTIMIZE: \n" + command);
    }
    if (command instanceof Insert) {
        Insert insert = (Insert) command;
        if (insert.isUpsert()) {
            // if not supported or there are trigger actions, then rewrite as a procedure
            // we do this here since we're changing the command type.
            // TODO: we could push this back into the rewrite, but it will need to be capabilities aware
            GroupSymbol group = insert.getGroup();
            Object modelId = metadata.getModelID(group.getMetadataID());
            boolean supportsUpsert = CapabilitiesUtil.supports(Capability.UPSERT, modelId, metadata, capFinder);
            if (!supportsUpsert) {
                try {
                    command = QueryRewriter.rewriteAsUpsertProcedure(insert, metadata, context);
                } catch (TeiidProcessingException e) {
                    throw new QueryPlannerException(e);
                }
                if (debug) {
                    // $NON-NLS-1$
                    analysisRecord.println("\n----------------------------------------------------------------------------");
                    // $NON-NLS-1$
                    analysisRecord.println("OPTIMIZE UPSERT PROCEDURE: \n" + command);
                }
            }
        }
    }
    ProcessorPlan result = null;
    switch(command.getType()) {
        case Command.TYPE_UPDATE_PROCEDURE:
            CreateProcedureCommand cupc = (CreateProcedureCommand) command;
            if (cupc.getUpdateType() != Command.TYPE_UNKNOWN || cupc.getVirtualGroup() == null) {
                // row update procedure or anon block
                result = planProcedure(command, metadata, idGenerator, capFinder, analysisRecord, context);
            } else {
                Object pid = cupc.getVirtualGroup().getMetadataID();
                if (pid instanceof TempMetadataID) {
                    TempMetadataID tid = (TempMetadataID) pid;
                    if (tid.getOriginalMetadataID() != null) {
                        pid = tid.getOriginalMetadataID();
                    }
                }
                String fullName = metadata.getFullName(pid);
                // $NON-NLS-1$
                fullName = "procedure cache:" + fullName;
                PreparedPlan pp = context.getPlan(fullName);
                if (pp == null) {
                    Determinism determinismLevel = context.resetDeterminismLevel();
                    try {
                        CommandContext clone = context.clone();
                        ProcessorPlan plan = planProcedure(command, metadata, idGenerator, capFinder, analysisRecord, clone);
                        // note that this is not a full prepared plan.  It is not usable by user queries.
                        if (pid instanceof Procedure) {
                            clone.accessedPlanningObject(pid);
                        }
                        pp = new PreparedPlan();
                        pp.setPlan(plan, clone);
                        context.putPlan(fullName, pp, context.getDeterminismLevel());
                    } finally {
                        context.setDeterminismLevel(determinismLevel);
                    }
                }
                result = pp.getPlan().clone();
                for (Object id : pp.getAccessInfo().getObjectsAccessed()) {
                    context.accessedPlanningObject(id);
                }
            }
            break;
        case Command.TYPE_BATCHED_UPDATE:
            result = BATCHED_UPDATE_PLANNER.optimize(command, idGenerator, metadata, capFinder, analysisRecord, context);
            break;
        case Command.TYPE_ALTER_PROC:
        case Command.TYPE_ALTER_TRIGGER:
        case Command.TYPE_ALTER_VIEW:
            result = DDL_PLANNER.optimize(command, idGenerator, metadata, capFinder, analysisRecord, context);
            break;
        case Command.TYPE_SOURCE_EVENT:
            result = SOURCE_EVENT_PLANNER.optimize(command, idGenerator, metadata, capFinder, analysisRecord, context);
            break;
        default:
            try {
                RelationalPlanner planner = new RelationalPlanner();
                planner.initialize(command, idGenerator, metadata, capFinder, analysisRecord, context);
                result = planner.optimize(command);
            } catch (QueryResolverException e) {
                throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30245, e);
            }
    }
    if (debug) {
        // $NON-NLS-1$
        analysisRecord.println("\n----------------------------------------------------------------------------");
        // $NON-NLS-1$
        analysisRecord.println("OPTIMIZATION COMPLETE:");
        // $NON-NLS-1$
        analysisRecord.println("PROCESSOR PLAN:\n" + result);
        // $NON-NLS-1$
        analysisRecord.println("============================================================================");
    }
    return result;
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) AnalysisRecord(org.teiid.query.analysis.AnalysisRecord) Determinism(org.teiid.metadata.FunctionMethod.Determinism) CommandContext(org.teiid.query.util.CommandContext) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) TempMetadataID(org.teiid.query.metadata.TempMetadataID) TempCapabilitiesFinder(org.teiid.query.metadata.TempCapabilitiesFinder) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) Insert(org.teiid.query.sql.lang.Insert) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) RelationalPlanner(org.teiid.query.optimizer.relational.RelationalPlanner) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) PreparedPlan(org.teiid.dqp.internal.process.PreparedPlan) Procedure(org.teiid.metadata.Procedure) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) IDGenerator(org.teiid.core.id.IDGenerator) QueryPlannerException(org.teiid.api.exception.query.QueryPlannerException) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore)

Example 38 with TempMetadataID

use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.

the class ResolverVisitor method checkException.

public static void checkException(Expression obj) throws QueryResolverException {
    if (obj == null || obj instanceof ExceptionExpression) {
        return;
    }
    if (obj instanceof ElementSymbol) {
        ElementSymbol es = (ElementSymbol) obj;
        if (!(es.getMetadataID() instanceof TempMetadataID)) {
            throw new QueryResolverException(QueryPlugin.Event.TEIID31120, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31120, obj));
        }
        TempMetadataID tid = (TempMetadataID) es.getMetadataID();
        if (tid.getType() != Exception.class) {
            throw new QueryResolverException(QueryPlugin.Event.TEIID31120, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31120, obj));
        }
    } else if (obj instanceof Constant) {
        Constant c = (Constant) obj;
        if (!(c.getValue() instanceof Exception)) {
            throw new QueryResolverException(QueryPlugin.Event.TEIID31120, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31120, obj));
        }
    } else {
        throw new QueryResolverException(QueryPlugin.Event.TEIID31120, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31120, obj));
    }
}
Also used : ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) TempMetadataID(org.teiid.query.metadata.TempMetadataID) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) InvalidFunctionException(org.teiid.api.exception.query.InvalidFunctionException)

Example 39 with TempMetadataID

use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.

the class QueryResolver method resolveCommand.

public static TempMetadataStore resolveCommand(Command currentCommand, QueryMetadataInterface metadata, boolean resolveNullLiterals) throws QueryResolverException, TeiidComponentException {
    // $NON-NLS-1$
    LogManager.logTrace(org.teiid.logging.LogConstants.CTX_QUERY_RESOLVER, new Object[] { "Resolving command", currentCommand });
    TempMetadataAdapter resolverMetadata = null;
    try {
        TempMetadataStore discoveredMetadata = currentCommand.getTemporaryMetadata();
        if (discoveredMetadata == null) {
            discoveredMetadata = new TempMetadataStore();
            currentCommand.setTemporaryMetadata(discoveredMetadata);
        }
        resolverMetadata = new TempMetadataAdapter(metadata, discoveredMetadata);
        // Resolve external groups for command
        Collection<GroupSymbol> externalGroups = currentCommand.getAllExternalGroups();
        for (GroupSymbol extGroup : externalGroups) {
            Object metadataID = extGroup.getMetadataID();
            // TODO: this is mainly for XML resolving since it sends external groups in unresolved
            if (metadataID == null || (!(extGroup.getMetadataID() instanceof TempMetadataID) && discoveredMetadata.getTempGroupID(extGroup.getName()) != null)) {
                boolean missing = metadataID == null;
                metadataID = resolverMetadata.getGroupID(extGroup.getName());
                if (missing) {
                    extGroup.setMetadataID(metadataID);
                } else {
                    // we shouldn't modify the existing, just add a shadow group
                    GroupSymbol gs = extGroup.clone();
                    gs.setMetadataID(metadataID);
                    currentCommand.getExternalGroupContexts().addGroup(gs);
                }
            }
        }
        CommandResolver resolver = chooseResolver(currentCommand, resolverMetadata);
        // Resolve this command
        resolver.resolveCommand(currentCommand, resolverMetadata, resolveNullLiterals);
    } catch (QueryMetadataException e) {
        throw new QueryResolverException(e);
    }
    // Flag that this command has been resolved.
    currentCommand.setIsResolved(true);
    return resolverMetadata.getMetadataStore();
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TempMetadataID(org.teiid.query.metadata.TempMetadataID) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 40 with TempMetadataID

use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.

the class TempTableStore method addTempTable.

/**
 * @param tempTableName
 * @param create
 * @param buffer
 * @param add
 * @param context may be null for mat views
 * @return
 * @throws TeiidProcessingException
 */
TempTable addTempTable(final String tempTableName, Create create, BufferManager buffer, boolean add, CommandContext context) throws TeiidProcessingException {
    List<ElementSymbol> columns = create.getColumnSymbols();
    TempMetadataID id = tempMetadataStore.getTempGroupID(tempTableName);
    getSynchronization(context);
    if (id == null) {
        // add metadata
        id = tempMetadataStore.addTempGroup(tempTableName, columns, false, true);
        TempTableResolver.addAdditionalMetadata(create, id);
    }
    for (int i = 0; i < id.getElements().size(); i++) {
        columns.get(i).setMetadataID(id.getElements().get(i));
    }
    columns = new ArrayList<ElementSymbol>(columns);
    if (!create.getPrimaryKey().isEmpty()) {
        // reorder the columns to put the key in front
        // retain the metadata as well by using the original column
        List<ElementSymbol> primaryKey = create.getPrimaryKey();
        for (int i = 0; i < primaryKey.size(); i++) {
            ElementSymbol es = primaryKey.get(i);
            int index = columns.indexOf(es);
            es = columns.remove(index);
            columns.add(i, es);
        }
    }
    final TempTable tempTable = new TempTable(id, buffer, columns, create.getPrimaryKey().size(), sessionID);
    if (add) {
        tempTables.put(tempTableName, tempTable);
    }
    return tempTable;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TempMetadataID(org.teiid.query.metadata.TempMetadataID)

Aggregations

TempMetadataID (org.teiid.query.metadata.TempMetadataID)48 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)21 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)16 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)12 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Test (org.junit.Test)7 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)6 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)6 HashSet (java.util.HashSet)5 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)5 TeiidComponentException (org.teiid.core.TeiidComponentException)5 TeiidProcessingException (org.teiid.core.TeiidProcessingException)5 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)5 LanguageObject (org.teiid.query.sql.LanguageObject)4 CacheHint (org.teiid.query.sql.lang.CacheHint)4 Expression (org.teiid.query.sql.symbol.Expression)4 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)3 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)3