Search in sources :

Example 61 with Procedure

use of org.teiid.metadata.Procedure in project teiid by teiid.

the class TestWSTranslator method testHeaders.

@Test
public void testHeaders() throws Exception {
    WSExecutionFactory ef = new WSExecutionFactory();
    WSConnection mockConnection = Mockito.mock(WSConnection.class);
    MetadataFactory mf = new MetadataFactory("vdb", 1, "x", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
    ef.getMetadata(mf, mockConnection);
    Procedure p = mf.getSchema().getProcedure(WSExecutionFactory.INVOKE_HTTP);
    TransformationMetadata tm = RealMetadataFactory.createTransformationMetadata(mf.asMetadataStore(), "vdb");
    RuntimeMetadataImpl rm = new RuntimeMetadataImpl(tm);
    Dispatch<Object> mockDispatch = mockDispatch();
    DataSource mock = Mockito.mock(DataSource.class);
    ByteArrayInputStream baos = new ByteArrayInputStream(new byte[100]);
    Mockito.stub(mock.getInputStream()).toReturn(baos);
    Mockito.stub(mockDispatch.invoke(Mockito.any(DataSource.class))).toReturn(mock);
    Mockito.stub(mockConnection.createDispatch(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(Class.class), Mockito.any(Service.Mode.class))).toReturn(mockDispatch);
    CommandBuilder cb = new CommandBuilder(tm);
    Call call = (Call) cb.getCommand("call invokeHttp('GET', null, null, false, '{\"ContentType\":\"application/json\"}')");
    BinaryWSProcedureExecution pe = new BinaryWSProcedureExecution(call, rm, Mockito.mock(ExecutionContext.class), ef, mockConnection);
    pe.execute();
    Map<String, List<String>> headers = (Map<String, List<String>>) mockDispatch.getRequestContext().get(MessageContext.HTTP_REQUEST_HEADERS);
    assertEquals(Arrays.asList("application/json"), headers.get("ContentType"));
}
Also used : WSConnection(org.teiid.translator.WSConnection) Call(org.teiid.language.Call) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Properties(java.util.Properties) DataSource(javax.activation.DataSource) ExecutionContext(org.teiid.translator.ExecutionContext) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) RuntimeMetadataImpl(org.teiid.dqp.internal.datamgr.RuntimeMetadataImpl) Procedure(org.teiid.metadata.Procedure) List(java.util.List) CommandBuilder(org.teiid.cdk.CommandBuilder) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 62 with Procedure

use of org.teiid.metadata.Procedure in project teiid by teiid.

the class RelationalPlanner method addNestedProcedure.

private boolean addNestedProcedure(PlanNode sourceNode, ProcedureContainer container, Object metadataId) throws TeiidComponentException, QueryMetadataException, TeiidProcessingException {
    if (container instanceof StoredProcedure) {
        StoredProcedure sp = (StoredProcedure) container;
        if (sp.getProcedureID() instanceof Procedure) {
            context.accessedPlanningObject(sp.getProcedureID());
        }
    }
    for (SubqueryContainer<?> subqueryContainer : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(container)) {
        if (subqueryContainer.getCommand().getCorrelatedReferences() != null) {
            continue;
        }
        List<Reference> correlatedReferences = new ArrayList<Reference>();
        CorrelatedReferenceCollectorVisitor.collectReferences(subqueryContainer.getCommand(), Arrays.asList(container.getGroup()), correlatedReferences, metadata);
        setCorrelatedReferences(subqueryContainer, correlatedReferences);
    }
    // $NON-NLS-1$
    String cacheString = "transformation/" + container.getClass().getSimpleName().toUpperCase();
    Command c = (Command) metadata.getFromMetadataCache(metadataId, cacheString);
    if (c == null) {
        c = QueryResolver.expandCommand(container, metadata, analysisRecord);
        if (c != null) {
            if (c instanceof CreateProcedureCommand) {
                // TODO: find a better way to do this
                ((CreateProcedureCommand) c).setProjectedSymbols(container.getProjectedSymbols());
            }
            Request.validateWithVisitor(new ValidationVisitor(), metadata, c);
            metadata.addToMetadataCache(metadataId, cacheString, c.clone());
        }
    } else {
        c = (Command) c.clone();
        if (c instanceof CreateProcedureCommand) {
            // TODO: find a better way to do this
            ((CreateProcedureCommand) c).setProjectedSymbols(container.getProjectedSymbols());
        }
    }
    boolean checkRowBasedSecurity = true;
    if (!container.getGroup().isProcedure() && !metadata.isVirtualGroup(metadataId)) {
        Set<PlanningStackEntry> entries = planningStack.get();
        if (entries.contains(new PlanningStackEntry(container, container.getGroup()))) {
            checkRowBasedSecurity = false;
        }
    }
    if (checkRowBasedSecurity) {
        c = RowBasedSecurityHelper.checkUpdateRowBasedFilters(container, c, this);
    }
    if (c != null) {
        if (c instanceof TriggerAction) {
            TriggerAction ta = (TriggerAction) c;
            ProcessorPlan plan = new TriggerActionPlanner().optimize((ProcedureContainer) container.clone(), ta, idGenerator, metadata, capFinder, analysisRecord, context);
            sourceNode.setProperty(NodeConstants.Info.PROCESSOR_PLAN, plan);
            return true;
        }
        if (c.getCacheHint() != null) {
            if (container instanceof StoredProcedure) {
                StoredProcedure sp = (StoredProcedure) container;
                boolean noCache = isNoCacheGroup(metadata, sp.getProcedureID(), option);
                if (!noCache) {
                    if (!context.isResultSetCacheEnabled()) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        recordAnnotation(analysisRecord, Annotation.CACHED_PROCEDURE, Priority.MEDIUM, "SimpleQueryResolver.procedure_cache_not_usable", container.getGroup(), "result set cache disabled");
                    } else if (!container.areResultsCachable()) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        recordAnnotation(analysisRecord, Annotation.CACHED_PROCEDURE, Priority.MEDIUM, "SimpleQueryResolver.procedure_cache_not_usable", container.getGroup(), "procedure performs updates");
                    } else if (LobManager.getLobIndexes(new ArrayList<ElementSymbol>(sp.getProcedureParameters().keySet())) != null) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        recordAnnotation(analysisRecord, Annotation.CACHED_PROCEDURE, Priority.MEDIUM, "SimpleQueryResolver.procedure_cache_not_usable", container.getGroup(), "lob parameters");
                    }
                    container.getGroup().setGlobalTable(true);
                    container.setCacheHint(c.getCacheHint());
                    // $NON-NLS-1$*/
                    recordAnnotation(analysisRecord, Annotation.CACHED_PROCEDURE, Priority.LOW, "SimpleQueryResolver.procedure_cache_used", container.getGroup());
                    return false;
                }
                // $NON-NLS-1$
                recordAnnotation(analysisRecord, Annotation.CACHED_PROCEDURE, Priority.LOW, "SimpleQueryResolver.procedure_cache_not_used", container.getGroup());
            }
        }
        // skip the rewrite here, we'll do that in the optimizer
        // so that we know what the determinism level is.
        addNestedCommand(sourceNode, container.getGroup(), container, c, false, true);
    }
    List<SubqueryContainer<?>> subqueries = ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(container);
    if (c == null && container instanceof FilteredCommand) {
        // we force the evaluation of procedure params - TODO: inserts are fine except for nonpushdown functions on columns
        // for non-temp source queries, we must pre-plan subqueries to know if they can be pushed down
        boolean compensate = false;
        boolean isTemp = container.getGroup().isTempTable() && metadata.getModelID(container.getGroup().getMetadataID()) == TempMetadataAdapter.TEMP_MODEL;
        try {
            planSubqueries(container, c, subqueries, true);
        } catch (QueryPlannerException e) {
            if (!isTemp) {
                throw e;
            }
            compensate = true;
        }
        if (!isTemp && !CriteriaCapabilityValidatorVisitor.canPushLanguageObject(container, metadata.getModelID(container.getGroup().getMetadataID()), metadata, capFinder, analysisRecord)) {
            compensate = true;
        }
        if (compensate) {
            // do a workaround of row-by-row processing for update/delete
            validateRowProcessing(container);
            // treat this as an update procedure
            if (container instanceof Update) {
                c = QueryRewriter.createUpdateProcedure((Update) container, metadata, context);
            } else {
                c = QueryRewriter.createDeleteProcedure((Delete) container, metadata, context);
            }
            addNestedCommand(sourceNode, container.getGroup(), container, c, false, true);
            return false;
        }
    }
    // plan any subqueries in criteria/parameters/values
    planSubqueries(container, c, subqueries, false);
    return false;
}
Also used : ValidationVisitor(org.teiid.query.validator.ValidationVisitor) TriggerAction(org.teiid.query.sql.proc.TriggerAction) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) Procedure(org.teiid.metadata.Procedure) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) QueryPlannerException(org.teiid.api.exception.query.QueryPlannerException) TriggerActionPlanner(org.teiid.query.optimizer.TriggerActionPlanner)

Example 63 with Procedure

use of org.teiid.metadata.Procedure 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 64 with Procedure

use of org.teiid.metadata.Procedure in project teiid by teiid.

the class S3ExecutionFactory method saveFile.

private void saveFile(MetadataFactory metadataFactory) {
    Procedure p = metadataFactory.addProcedure(SAVEFILE);
    // $NON-NLS-1$
    p.setAnnotation("Saves the given value to the given bucket.  Any existing file will be overriden.");
    // $NON-NLS-1$
    ProcedureParameter param = metadataFactory.addProcedureParameter("name", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p);
    // $NON-NLS-1$
    param.setAnnotation("The name of the object to save");
    addCommonParameters(metadataFactory, p);
    // $NON-NLS-1$
    param = metadataFactory.addProcedureParameter("contents", TypeFacility.RUNTIME_NAMES.OBJECT, Type.In, p);
    // $NON-NLS-1$
    param.setAnnotation("The contents to save.  Can be one of CLOB, BLOB, or XML");
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) Procedure(org.teiid.metadata.Procedure)

Example 65 with Procedure

use of org.teiid.metadata.Procedure in project teiid by teiid.

the class S3ExecutionFactory method listBucket.

private void listBucket(MetadataFactory metadataFactory) {
    Procedure p = metadataFactory.addProcedure(LISTBUCKET);
    // $NON-NLS-1$
    p.setAnnotation("Lists the Objects in a given bucket.");
    ProcedureParameter param;
    // $NON-NLS-1$
    param = metadataFactory.addProcedureParameter("bucket", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p);
    // $NON-NLS-1$
    param.setAnnotation("The name of the bucket in Amazon S3");
    param.setNullType(NullType.Nullable);
    // $NON-NLS-1$
    param = metadataFactory.addProcedureParameter("region", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p);
    // $NON-NLS-1$
    param.setAnnotation("region in which the bucket exists on Amazon S3");
    param.setNullType(NullType.Nullable);
    // $NON-NLS-1$
    param = metadataFactory.addProcedureParameter("accesskey", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p);
    // $NON-NLS-1$
    param.setAnnotation("Security Access Key, if not provided will use translator configured keys.");
    param.setNullType(NullType.Nullable);
    // $NON-NLS-1$
    param = metadataFactory.addProcedureParameter("secretkey", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p);
    // $NON-NLS-1$
    param.setAnnotation("Security Secret Key, if not provided will use translator configured keys.");
    param.setNullType(NullType.Nullable);
    param = // $NON-NLS-1$
    metadataFactory.addProcedureParameter(// $NON-NLS-1$
    "nexttoken", // $NON-NLS-1$
    TypeFacility.RUNTIME_NAMES.STRING, Type.In, p);
    param.setAnnotation("If the response is truncated, Amazon S3 returns this parameter with a " + "continuation token that you can specify as the continuation-token in your next request " + // $NON-NLS-1$
    "to retrieve the next set of keys");
    param.setNullType(NullType.Nullable);
    // $NON-NLS-1$
    metadataFactory.addProcedureResultSetColumn("result", TypeFacility.RUNTIME_NAMES.CLOB, p);
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) Procedure(org.teiid.metadata.Procedure)

Aggregations

Procedure (org.teiid.metadata.Procedure)82 ProcedureParameter (org.teiid.metadata.ProcedureParameter)36 Test (org.junit.Test)35 MetadataFactory (org.teiid.metadata.MetadataFactory)30 RealMetadataFactory (org.teiid.query.unittest.RealMetadataFactory)27 Schema (org.teiid.metadata.Schema)16 Column (org.teiid.metadata.Column)14 MetadataStore (org.teiid.metadata.MetadataStore)11 Properties (java.util.Properties)10 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)10 Table (org.teiid.metadata.Table)9 QueryNode (org.teiid.query.mapping.relational.QueryNode)8 List (java.util.List)6 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)6 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)5 TranslatorException (org.teiid.translator.TranslatorException)5 ODataType (org.teiid.translator.odata4.ODataMetadataProcessor.ODataType)5 ArrayList (java.util.ArrayList)4 Call (org.teiid.language.Call)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3