Search in sources :

Example 36 with TempMetadataStore

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

the class UpdateProcedureResolver method resolveStatement.

private void resolveStatement(CreateProcedureCommand command, Statement statement, GroupContext externalGroups, GroupSymbol variables, TempMetadataAdapter metadata) throws QueryResolverException, QueryMetadataException, TeiidComponentException {
    // $NON-NLS-1$
    LogManager.logTrace(org.teiid.logging.LogConstants.CTX_QUERY_RESOLVER, new Object[] { "Resolving statement", statement });
    switch(statement.getType()) {
        case Statement.TYPE_IF:
            IfStatement ifStmt = (IfStatement) statement;
            Criteria ifCrit = ifStmt.getCondition();
            for (SubqueryContainer container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(ifCrit)) {
                resolveEmbeddedCommand(metadata, externalGroups, container.getCommand());
            }
            ResolverVisitor.resolveLanguageObject(ifCrit, null, externalGroups, metadata);
            resolveBlock(command, ifStmt.getIfBlock(), externalGroups, metadata);
            if (ifStmt.hasElseBlock()) {
                resolveBlock(command, ifStmt.getElseBlock(), externalGroups, metadata);
            }
            break;
        case Statement.TYPE_COMMAND:
            CommandStatement cmdStmt = (CommandStatement) statement;
            Command subCommand = cmdStmt.getCommand();
            TempMetadataStore discoveredMetadata = resolveEmbeddedCommand(metadata, externalGroups, subCommand);
            if (subCommand instanceof StoredProcedure) {
                StoredProcedure sp = (StoredProcedure) subCommand;
                for (SPParameter param : sp.getParameters()) {
                    switch(param.getParameterType()) {
                        case ParameterInfo.OUT:
                        case ParameterInfo.RETURN_VALUE:
                            if (param.getExpression() != null) {
                                if (!isAssignable(metadata, param)) {
                                    throw new QueryResolverException(QueryPlugin.Event.TEIID30121, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30121, param.getExpression()));
                                }
                                sp.setCallableStatement(true);
                            }
                            break;
                        case ParameterInfo.INOUT:
                            if (!isAssignable(metadata, param)) {
                                continue;
                            }
                            sp.setCallableStatement(true);
                            break;
                    }
                }
            }
            if (discoveredMetadata != null) {
                metadata.getMetadataStore().getData().putAll(discoveredMetadata.getData());
            }
            // dynamic commands need to be updated as to their implicitly expected projected symbols
            if (subCommand instanceof DynamicCommand) {
                DynamicCommand dynCommand = (DynamicCommand) subCommand;
                if (dynCommand.getIntoGroup() == null && !dynCommand.isAsClauseSet()) {
                    if ((command.getResultSetColumns() != null && command.getResultSetColumns().isEmpty()) || !cmdStmt.isReturnable() || command.getResultSetColumns() == null) {
                        // we're not interested in the resultset
                        dynCommand.setAsColumns(Collections.EMPTY_LIST);
                    } else {
                        // should match the procedure
                        dynCommand.setAsColumns(command.getResultSetColumns());
                    }
                }
            }
            if (command.getResultSetColumns() == null && cmdStmt.isReturnable() && subCommand.returnsResultSet() && subCommand.getResultSetColumns() != null && !subCommand.getResultSetColumns().isEmpty()) {
                command.setResultSetColumns(subCommand.getResultSetColumns());
                if (command.getProjectedSymbols().isEmpty()) {
                    command.setProjectedSymbols(subCommand.getResultSetColumns());
                }
            }
            break;
        case Statement.TYPE_ERROR:
        case Statement.TYPE_ASSIGNMENT:
        case Statement.TYPE_DECLARE:
        case Statement.TYPE_RETURN:
            ExpressionStatement exprStmt = (ExpressionStatement) statement;
            // first resolve the value.  this ensures the value cannot use the variable being defined
            if (exprStmt.getExpression() != null) {
                Expression expr = exprStmt.getExpression();
                for (SubqueryContainer container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(expr)) {
                    resolveEmbeddedCommand(metadata, externalGroups, container.getCommand());
                }
                ResolverVisitor.resolveLanguageObject(expr, null, externalGroups, metadata);
            }
            // second resolve the variable
            switch(statement.getType()) {
                case Statement.TYPE_DECLARE:
                    collectDeclareVariable((DeclareStatement) statement, variables, metadata, externalGroups);
                    break;
                case Statement.TYPE_ASSIGNMENT:
                    AssignmentStatement assStmt = (AssignmentStatement) statement;
                    ResolverVisitor.resolveLanguageObject(assStmt.getVariable(), null, externalGroups, metadata);
                    if (!metadata.elementSupports(assStmt.getVariable().getMetadataID(), SupportConstants.Element.UPDATE)) {
                        throw new QueryResolverException(QueryPlugin.Event.TEIID30121, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30121, assStmt.getVariable()));
                    }
                    // don't allow variable assignments to be external
                    assStmt.getVariable().setIsExternalReference(false);
                    break;
                case Statement.TYPE_RETURN:
                    ReturnStatement rs = (ReturnStatement) statement;
                    if (rs.getExpression() != null) {
                        if (command.getReturnVariable() == null) {
                            throw new QueryResolverException(QueryPlugin.Event.TEIID31125, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31125, rs));
                        }
                        rs.setVariable(command.getReturnVariable().clone());
                    }
                    // else - we don't currently require the use of return for backwards compatibility
                    break;
            }
            // third ensure the type matches
            if (exprStmt.getExpression() != null) {
                Class<?> varType = exprStmt.getExpectedType();
                Class<?> exprType = exprStmt.getExpression().getType();
                if (exprType == null) {
                    throw new QueryResolverException(QueryPlugin.Event.TEIID30123, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30123));
                }
                String varTypeName = DataTypeManager.getDataTypeName(varType);
                exprStmt.setExpression(ResolverUtil.convertExpression(exprStmt.getExpression(), varTypeName, metadata));
                if (statement.getType() == Statement.TYPE_ERROR) {
                    ResolverVisitor.checkException(exprStmt.getExpression());
                }
            }
            break;
        case Statement.TYPE_WHILE:
            WhileStatement whileStmt = (WhileStatement) statement;
            Criteria whileCrit = whileStmt.getCondition();
            for (SubqueryContainer container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(whileCrit)) {
                resolveEmbeddedCommand(metadata, externalGroups, container.getCommand());
            }
            ResolverVisitor.resolveLanguageObject(whileCrit, null, externalGroups, metadata);
            resolveBlock(command, whileStmt.getBlock(), externalGroups, metadata);
            break;
        case Statement.TYPE_LOOP:
            LoopStatement loopStmt = (LoopStatement) statement;
            String groupName = loopStmt.getCursorName();
            isValidGroup(metadata, groupName);
            Command cmd = loopStmt.getCommand();
            resolveEmbeddedCommand(metadata, externalGroups, cmd);
            List<Expression> symbols = cmd.getProjectedSymbols();
            // add the loop cursor group into its own context
            TempMetadataStore store = metadata.getMetadataStore().clone();
            metadata = new TempMetadataAdapter(metadata.getMetadata(), store);
            externalGroups = new GroupContext(externalGroups, null);
            ProcedureContainerResolver.addScalarGroup(groupName, store, externalGroups, symbols, false);
            resolveBlock(command, loopStmt.getBlock(), externalGroups, metadata);
            break;
        case Statement.TYPE_COMPOUND:
            resolveBlock(command, (Block) statement, externalGroups, metadata);
            break;
    }
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) SubqueryContainer(org.teiid.query.sql.lang.SubqueryContainer) SPParameter(org.teiid.query.sql.lang.SPParameter) Criteria(org.teiid.query.sql.lang.Criteria) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) DynamicCommand(org.teiid.query.sql.lang.DynamicCommand) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) DynamicCommand(org.teiid.query.sql.lang.DynamicCommand) Command(org.teiid.query.sql.lang.Command) Expression(org.teiid.query.sql.symbol.Expression) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) GroupContext(org.teiid.query.sql.lang.GroupContext)

Example 37 with TempMetadataStore

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

the class TestMultiSourceMetadataWrapper method testMultiSourcePseudoElement.

@Test
public void testMultiSourcePseudoElement() throws Exception {
    HashSet<String> multiSourceModels = new HashSet<String>();
    multiSourceModels.add("BQT1");
    MultiSourceMetadataWrapper wrapper = new MultiSourceMetadataWrapper(RealMetadataFactory.exampleBQTCached(), multiSourceModels);
    // $NON-NLS-1$
    Object groupID = wrapper.getGroupID("BQT1.SmallA");
    List<?> elements = wrapper.getElementIDsInGroupID(groupID);
    assertEquals(18, elements.size());
    Object instanceElementID = elements.get(elements.size() - 1);
    String fullName = wrapper.getFullName(instanceElementID);
    // $NON-NLS-1$
    assertEquals("BQT1.SmallA." + MultiSourceElement.DEFAULT_MULTI_SOURCE_ELEMENT_NAME, fullName);
    assertEquals(instanceElementID, wrapper.getElementID(fullName));
    assertEquals(groupID, wrapper.getGroupIDForElementID(instanceElementID));
    assertEquals(null, wrapper.getMaximumValue(instanceElementID));
    assertEquals(null, wrapper.getMinimumValue(instanceElementID));
    assertEquals(wrapper.getModelID(groupID), wrapper.getModelID(instanceElementID));
    assertEquals(null, wrapper.getDefaultValue(instanceElementID));
    assertEquals(255, wrapper.getElementLength(instanceElementID));
    assertEquals(DataTypeManager.DefaultDataTypes.STRING, wrapper.getElementRuntimeTypeName(instanceElementID));
    assertEquals(new Properties(), wrapper.getExtensionProperties(instanceElementID));
    assertEquals(null, wrapper.getNameInSource(instanceElementID));
    assertEquals(null, wrapper.getNativeType(instanceElementID));
    assertEquals(18, wrapper.getPosition(instanceElementID));
    assertEquals(0, wrapper.getPrecision(instanceElementID));
    assertEquals(0, wrapper.getScale(instanceElementID));
    assertEquals(0, wrapper.getRadix(instanceElementID));
    assertEquals(MultiSourceElement.DEFAULT_MULTI_SOURCE_ELEMENT_NAME, Symbol.getShortName(fullName));
    assertEquals(fullName, wrapper.getFullName(groupID) + Symbol.SEPARATOR + MultiSourceElement.DEFAULT_MULTI_SOURCE_ELEMENT_NAME);
    TempMetadataAdapter tma = new TempMetadataAdapter(wrapper, new TempMetadataStore());
    ElementSymbol elementSymbol = new ElementSymbol("y");
    elementSymbol.setType(DataTypeManager.DefaultDataClasses.STRING);
    TempMetadataID id = tma.getMetadataStore().addTempGroup("x", Arrays.asList(elementSymbol));
    assertFalse(tma.isMultiSourceElement(id.getElements().get(0)));
    assertTrue(tma.isMultiSourceElement(instanceElementID));
    assertTrue(tma.isPseudo(instanceElementID));
    assertEquals(17, tma.getElementIDsInGroupID(tma.getGroupID("VQT.Smalla")).size());
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TempMetadataID(org.teiid.query.metadata.TempMetadataID) Properties(java.util.Properties) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 38 with TempMetadataStore

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

the class TestMultiSourcePlanToProcessConverter method helpTestMultiSourcePlan.

public ProcessorPlan helpTestMultiSourcePlan(QueryMetadataInterface metadata, String userSql, String multiModel, int sourceCount, ProcessorDataManager dataMgr, List<?>[] expectedResults, VDBMetaData vdb, List<?> params, Options options, SourceCapabilities bsc) throws Exception {
    Map<String, String> multiSourceModels = MultiSourceMetadataWrapper.getMultiSourceModels(vdb);
    for (String model : multiSourceModels.keySet()) {
        char sourceID = 'a';
        // by default every model has one binding associated, but for multi-source there were none assigned.
        ModelMetaData m = vdb.getModel(model);
        int x = m.getSourceNames().size();
        for (int i = x; i < sourceCount; i++, sourceID++) {
            // $NON-NLS-1$ //$NON-NLS-2$
            m.addSourceMapping("" + sourceID, "translator", null);
        }
    }
    QueryMetadataInterface wrapper = new MultiSourceMetadataWrapper(metadata, multiSourceModels);
    wrapper = new TempMetadataAdapter(wrapper, new TempMetadataStore());
    DQPWorkContext workContext = RealMetadataFactory.buildWorkContext(wrapper, vdb);
    AnalysisRecord analysis = new AnalysisRecord(DEBUG, DEBUG);
    Command command = TestResolver.helpResolve(userSql, wrapper);
    ValidatorReport report = Validator.validate(command, metadata);
    if (report.hasItems()) {
        fail(report.toString());
    }
    // Plan
    command = QueryRewriter.rewrite(command, wrapper, null);
    DefaultCapabilitiesFinder fakeFinder = new DefaultCapabilitiesFinder(bsc);
    CapabilitiesFinder finder = new TempCapabilitiesFinder(fakeFinder);
    IDGenerator idGenerator = new IDGenerator();
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    CommandContext context = new CommandContext("test", "user", null, vdb.getName(), vdb.getVersion(), false);
    context.setDQPWorkContext(workContext);
    context.setOptions(options);
    ProcessorPlan plan = QueryOptimizer.optimizePlan(command, wrapper, idGenerator, finder, analysis, context);
    if (DEBUG) {
        System.out.println(analysis.getDebugLog());
        // $NON-NLS-1$
        System.out.println("\nMultiSource Plan:");
        System.out.println(plan);
    }
    if (params != null) {
        TestProcessor.setParameterValues(params, command, context);
    }
    TestProcessor.helpProcess(plan, context, dataMgr, expectedResults);
    return plan;
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) DQPWorkContext(org.teiid.dqp.internal.process.DQPWorkContext) AnalysisRecord(org.teiid.query.analysis.AnalysisRecord) CommandContext(org.teiid.query.util.CommandContext) TempCapabilitiesFinder(org.teiid.query.metadata.TempCapabilitiesFinder) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) ValidatorReport(org.teiid.query.validator.ValidatorReport) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) TempCapabilitiesFinder(org.teiid.query.metadata.TempCapabilitiesFinder) CapabilitiesFinder(org.teiid.query.optimizer.capabilities.CapabilitiesFinder) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Command(org.teiid.query.sql.lang.Command) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) IDGenerator(org.teiid.core.id.IDGenerator) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore)

Example 39 with TempMetadataStore

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

the class TestStoredProcedurePlanning method testStoredQuery15.

/**
 * Test planning stored queries. GeminiStoredQueryTestPlan - 6d
 */
@Ignore("stored procedure wrapper removal logic has been removed")
@Test
public void testStoredQuery15() {
    ProcessorPlan plan = // $NON-NLS-1$
    TestOptimizer.helpPlan(// $NON-NLS-1$
    "EXEC pm1.sq10('1', 2)", // $NON-NLS-1$
    new TempMetadataAdapter(RealMetadataFactory.example1Cached(), new TempMetadataStore()), // $NON-NLS-1$
    new String[] { "SELECT e1 FROM pm1.g1 WHERE (e1 = '1') AND (e2 = 2)" });
    TestOptimizer.checkNodeTypes(plan, TestOptimizer.FULL_PUSHDOWN);
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 40 with TempMetadataStore

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

the class TestStoredProcedurePlanning method testStoredQuery10.

/**
 * Test planning stored queries. GeminiStoredQueryTestPlan - 5b
 */
@Ignore("stored procedure wrapper removal logic has been removed")
@Test
public void testStoredQuery10() {
    ProcessorPlan plan = // $NON-NLS-1$
    TestOptimizer.helpPlan(// $NON-NLS-1$
    "EXEC pm1.sq5('1')", // $NON-NLS-1$
    new TempMetadataAdapter(RealMetadataFactory.example1Cached(), new TempMetadataStore()), // $NON-NLS-1$
    new String[] { "SELECT e1, e2 FROM pm1.g1 WHERE e1 = '1'" });
    TestOptimizer.checkNodeTypes(plan, TestOptimizer.FULL_PUSHDOWN);
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)52 TempMetadataAdapter (org.teiid.query.metadata.TempMetadataAdapter)45 Test (org.junit.Test)33 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)32 Ignore (org.junit.Ignore)21 TempMetadataID (org.teiid.query.metadata.TempMetadataID)6 CommandContext (org.teiid.query.util.CommandContext)6 List (java.util.List)5 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)5 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)5 Command (org.teiid.query.sql.lang.Command)5 GroupContext (org.teiid.query.sql.lang.GroupContext)5 Expression (org.teiid.query.sql.symbol.Expression)5 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)5 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)4 ArrayList (java.util.ArrayList)3 TeiidProcessingException (org.teiid.core.TeiidProcessingException)3 AnalysisRecord (org.teiid.query.analysis.AnalysisRecord)3 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)3 LinkedList (java.util.LinkedList)2