Search in sources :

Example 6 with SPParameter

use of org.teiid.query.sql.lang.SPParameter 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 7 with SPParameter

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

the class TestProcessor method getProcResultSetSymbols.

static List getProcResultSetSymbols(List params) {
    List result = new ArrayList();
    Iterator iter = params.iterator();
    while (iter.hasNext()) {
        SPParameter param = (SPParameter) iter.next();
        if (param.getResultSetColumns() != null) {
            result.addAll(param.getResultSetColumns());
        }
    }
    iter = params.iterator();
    while (iter.hasNext()) {
        SPParameter param = (SPParameter) iter.next();
        if (param.getParameterType() == ParameterInfo.INOUT || param.getParameterType() == ParameterInfo.RETURN_VALUE) {
            result.add(param.getParameterSymbol());
        }
    }
    return result;
}
Also used : SPParameter(org.teiid.query.sql.lang.SPParameter)

Example 8 with SPParameter

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

the class TestProcedureRelational method testIssue119.

/*
     * The following are tests that were removed from the validator.  We are no longer trying to validate a priori whether 
     * procedure input criteria is valid.  This can be addressed later more generally when we do up front validation of
     * access patterns and access patterns have a wider range of semantics.
     * 
    @Test public void testProcInVirtualGroupDefect14609_1() throws Exception{
        helpValidate("select ve3 from vm1.vgvp1 where ve1=1.1 and ve2='a'", new String[] {"ve1 = 1.1"}, RealMetadataFactory.example1Cached()); //$NON-NLS-1$ //$NON-NLS-2$
    }
   
    @Test public void testProcInVirtualGroupDefect14609_2() throws Exception{
        helpValidate("select ve3 from vm1.vgvp1 where convert(ve1, integer)=1 and ve2='a'", new String[] {"convert(ve1, integer) = 1" }, RealMetadataFactory.example1Cached()); //$NON-NLS-1$ //$NON-NLS-2$
    }
    
    @Test public void testProcInVirtualGroupDefect14609_3() throws Exception{
        helpValidate("select ve3 from vm1.vgvp1 where 1.1=ve1 and ve2='a'", new String[] {"1.1 = ve1" }, RealMetadataFactory.example1Cached()); //$NON-NLS-1$ //$NON-NLS-2$
    }
    
    @Test public void testProcInVirtualGroupDefect14609_4() throws Exception{
        helpValidate("select ve3 from vm1.vgvp1 where 1=convert(ve1, integer) and ve2='a'", new String[] {"1 = convert(ve1, integer)" }, RealMetadataFactory.example1Cached()); //$NON-NLS-1$ //$NON-NLS-2$
    }    

    @Test public void testDefect15861() throws Exception{
        helpValidate("select ve3 from vm1.vgvp1 where (ve1=1 or ve1=2) and ve2='a'", new String[] {"(ve1 = 1) OR (ve1 = 2)", "ve1 = 2"}, RealMetadataFactory.example1Cached()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }

    @Test public void testProcInVirtualGroup1_Defect20164() {
        helpFailProcedure("select ve3 from vm1.vgvp2 where (ve1=1 and ve2='a') or ve3='c'", RealMetadataFactory.example1Cached()); //$NON-NLS-1$
    }

    @Test public void testProcInVirtualGroup2_Defect20164() {
        helpFailProcedure("select ve3 from vm1.vgvp2 where ve1=1 or ve2='a'", RealMetadataFactory.example1Cached()); //$NON-NLS-1$
    }

    @Test public void testProcInVirtualGroup3_Defect20164() {
        helpFailProcedure("select ve3 from vm1.vgvp2, pm1.g1 where ve1=pm1.g1.e2 and ve2='a'", RealMetadataFactory.example1Cached()); //$NON-NLS-1$
    }

    @Test public void testProcInVirtualGroup4_Defect20164() {
        helpValidate("select ve3 from vm1.vgvp2 where (ve1=1 and ve2='a') and (ve3='a' OR ve3='c')", new String[0], RealMetadataFactory.example1Cached()); //$NON-NLS-1$
    }

    @Test public void testProcInVirtualGroup5_Defect20164() {
        helpFailProcedure("select ve3 from vm1.vgvp2 where ve1=1 and NOT(ve2='a')", RealMetadataFactory.example1Cached()); //$NON-NLS-1$
    }

    @Test public void testProcInVirtualGroup6_Defect20164() {
        helpValidate("select ve3 from vm1.vgvp2 where ve1=1 and ve2 is null", new String[0], RealMetadataFactory.example1Cached()); //$NON-NLS-1$
    }

    @Test public void testProcInVirtualGroup7_Defect20164() {
        helpFailProcedure("select ve3 from vm1.vgvp2 where ve1=1 and ve2 is not null", RealMetadataFactory.example1Cached()); //$NON-NLS-1$
    }*/
/**
 * Ensures that dependent procedures are processed 1 at a time so that projected input values
 * are set correctly.
 */
@Test
public void testIssue119() throws Exception {
    MetadataStore metadataStore = new MetadataStore();
    // $NON-NLS-1$
    Schema v1 = RealMetadataFactory.createVirtualModel("v1", metadataStore);
    // $NON-NLS-1$
    Schema pm1 = RealMetadataFactory.createPhysicalModel("pm1", metadataStore);
    // $NON-NLS-1$
    ProcedureParameter in = RealMetadataFactory.createParameter("in1", SPParameter.IN, DataTypeManager.DefaultDataTypes.INTEGER);
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
    ColumnSet<Procedure> rs1 = RealMetadataFactory.createResultSet("v1.vp1.rs1", new String[] { "e1", "e2", "e3", "e4", "e5" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.INTEGER });
    // $NON-NLS-1$
    QueryNode n1 = new QueryNode("CREATE VIRTUAL PROCEDURE BEGIN SELECT vp1.in1 e1, x.in1 e2, x.e1 e3, y.in1 e4, y.e1 e5 FROM pm1.sp119 x, pm1.sp119 y where x.in1 = vp1.in1 and y.in1 = x.e1; END");
    // $NON-NLS-1$
    Procedure vt1 = RealMetadataFactory.createVirtualProcedure("vp1", v1, Arrays.asList(in), n1);
    vt1.setResultSet(rs1);
    // $NON-NLS-1$
    ProcedureParameter in1 = RealMetadataFactory.createParameter("in1", SPParameter.IN, DataTypeManager.DefaultDataTypes.INTEGER);
    // $NON-NLS-1$ //$NON-NLS-2$
    ColumnSet<Procedure> rs3 = RealMetadataFactory.createResultSet("pm1.sp119.rs1", new String[] { "e1" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER });
    // $NON-NLS-1$
    Procedure sp1 = RealMetadataFactory.createStoredProcedure("sp119", pm1, Arrays.asList(in1));
    sp1.setResultSet(rs3);
    // $NON-NLS-1$
    String sql = "select * from (exec v1.vp1(1)) foo order by e4, e5";
    List<?>[] expected = new List[] { Arrays.asList(1, 1, 3, 3, 5), Arrays.asList(1, 1, 3, 3, 8), Arrays.asList(1, 1, 6, 6, 8), Arrays.asList(1, 1, 6, 6, 11) };
    QueryMetadataInterface metadata = RealMetadataFactory.createTransformationMetadata(metadataStore, "foo");
    // Construct data manager with data
    // Plan query
    ProcessorPlan plan = TestProcedureProcessor.getProcedurePlan(sql, metadata);
    // Run query
    HardcodedDataManager dataManager = new HardcodedDataManager() {

        @Override
        public TupleSource registerRequest(CommandContext context, Command command, String modelName, RegisterRequestParameter parameterObject) throws TeiidComponentException {
            if (command instanceof StoredProcedure) {
                StoredProcedure proc = (StoredProcedure) command;
                List<SPParameter> params = proc.getInputParameters();
                assertEquals(1, params.size());
                int value = (Integer) ((Constant) params.get(0).getExpression()).getValue();
                return new FakeTupleSource(command.getProjectedSymbols(), new List[] { Arrays.asList(value + 2), Arrays.asList(value + 5) });
            }
            return super.registerRequest(context, command, modelName, parameterObject);
        }
    };
    TestProcedureProcessor.helpTestProcess(plan, expected, dataManager, metadata);
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) CommandContext(org.teiid.query.util.CommandContext) SPParameter(org.teiid.query.sql.lang.SPParameter) Schema(org.teiid.metadata.Schema) MetadataStore(org.teiid.metadata.MetadataStore) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Command(org.teiid.query.sql.lang.Command) QueryNode(org.teiid.query.mapping.relational.QueryNode) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Procedure(org.teiid.metadata.Procedure) List(java.util.List) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) Test(org.junit.Test)

Example 9 with SPParameter

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

the class RulePlanProcedures method execute.

/**
 * @see org.teiid.query.optimizer.relational.OptimizerRule#execute(org.teiid.query.optimizer.relational.plantree.PlanNode, org.teiid.query.metadata.QueryMetadataInterface, org.teiid.query.optimizer.capabilities.CapabilitiesFinder, org.teiid.query.optimizer.relational.RuleStack, org.teiid.query.analysis.AnalysisRecord, org.teiid.query.util.CommandContext)
 */
public PlanNode execute(PlanNode plan, final QueryMetadataInterface metadata, CapabilitiesFinder capabilitiesFinder, RuleStack rules, AnalysisRecord analysisRecord, CommandContext context) throws QueryPlannerException, QueryMetadataException, TeiidComponentException {
    for (PlanNode node : NodeEditor.findAllNodes(plan, NodeConstants.Types.SOURCE, NodeConstants.Types.ACCESS)) {
        if (!FrameUtil.isProcedure(node.getFirstChild())) {
            continue;
        }
        StoredProcedure proc = (StoredProcedure) node.getProperty(NodeConstants.Info.NESTED_COMMAND);
        if (!proc.isProcedureRelational()) {
            continue;
        }
        HashSet<ElementSymbol> inputSymbols = new HashSet<ElementSymbol>();
        List<Reference> inputReferences = new LinkedList<Reference>();
        PlanNode critNode = node.getParent();
        List<Criteria> conjuncts = new LinkedList<Criteria>();
        HashSet<ElementSymbol> coveredParams = new HashSet<ElementSymbol>();
        for (Iterator<SPParameter> params = proc.getInputParameters().iterator(); params.hasNext(); ) {
            SPParameter param = params.next();
            ElementSymbol symbol = param.getParameterSymbol();
            Expression input = param.getExpression();
            inputReferences.add((Reference) input);
            inputSymbols.add(symbol);
        }
        findInputNodes(inputSymbols, critNode, conjuncts, coveredParams);
        List<Expression> defaults = new LinkedList<Expression>();
        for (Reference ref : inputReferences) {
            ElementSymbol symbol = ref.getExpression();
            Expression defaultValue = null;
            /*try {
                    defaultValue = ResolverUtil.getDefault(symbol, metadata);
                } catch (QueryResolverException qre) {
                    //Just ignore
                }*/
            defaults.add(defaultValue);
            if (defaultValue == null && !coveredParams.contains(symbol)) {
                throw new QueryPlannerException(QueryPlugin.Event.TEIID30270, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30270, symbol));
            }
        }
        /*if (conjuncts.isEmpty()) {
                for (int j = 0; j < inputReferences.size(); j++) {
                    Reference ref = (Reference)inputReferences.get(j);
                    ref.setValue(defaults.get(j));
                }
                continue;
            }*/
        PlanNode accessNode = NodeEditor.findNodePreOrder(node, NodeConstants.Types.ACCESS);
        Criteria crit = Criteria.combineCriteria(conjuncts);
        if (crit != null) {
            accessNode.setProperty(NodeConstants.Info.PROCEDURE_CRITERIA, crit);
            accessNode.setProperty(NodeConstants.Info.PROCEDURE_INPUTS, inputReferences);
            accessNode.setProperty(NodeConstants.Info.PROCEDURE_DEFAULTS, defaults);
            accessNode.setProperty(NodeConstants.Info.IS_DEPENDENT_SET, Boolean.TRUE);
        }
    }
    return plan;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Reference(org.teiid.query.sql.symbol.Reference) SPParameter(org.teiid.query.sql.lang.SPParameter) DependentSetCriteria(org.teiid.query.sql.lang.DependentSetCriteria) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) SetCriteria(org.teiid.query.sql.lang.SetCriteria) LinkedList(java.util.LinkedList) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Expression(org.teiid.query.sql.symbol.Expression) QueryPlannerException(org.teiid.api.exception.query.QueryPlannerException) HashSet(java.util.HashSet)

Example 10 with SPParameter

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

the class TransformationMetadata method getStoredProcInfoDirect.

private StoredProcedureInfo getStoredProcInfoDirect(final String name) throws TeiidComponentException, QueryMetadataException {
    ArgCheck.isNotEmpty(name);
    String canonicalName = name.toUpperCase();
    Collection<StoredProcedureInfo> results = this.procedureCache.get(canonicalName);
    if (results == null) {
        Collection<Procedure> procRecords = getMetadataStore().getStoredProcedure(canonicalName);
        if (procRecords.isEmpty()) {
            return null;
        }
        results = new ArrayList<StoredProcedureInfo>(procRecords.size());
        for (Procedure procRecord : procRecords) {
            String procedureFullName = procRecord.getFullName();
            // create the storedProcedure info object that would hold procedure's metadata
            StoredProcedureInfo procInfo = new StoredProcedureInfo();
            procInfo.setProcedureCallableName(procedureFullName);
            procInfo.setProcedureID(procRecord);
            // modelID for the procedure
            procInfo.setModelID(procRecord.getParent());
            // get the parameter metadata info
            for (ProcedureParameter paramRecord : procRecord.getParameters()) {
                String runtimeType = paramRecord.getRuntimeType();
                int direction = this.convertParamRecordTypeToStoredProcedureType(paramRecord.getType());
                // create a parameter and add it to the procedure object
                SPParameter spParam = new SPParameter(paramRecord.getPosition(), direction, paramRecord.getFullName());
                spParam.setMetadataID(paramRecord);
                spParam.setClassType(DataTypeManager.getDataTypeClass(runtimeType));
                if (paramRecord.isVarArg()) {
                    spParam.setVarArg(true);
                    spParam.setClassType(DataTypeManager.getArrayType(spParam.getClassType()));
                }
                procInfo.addParameter(spParam);
            }
            // if the procedure returns a resultSet, obtain resultSet metadata
            if (procRecord.getResultSet() != null) {
                ColumnSet<Procedure> resultRecord = procRecord.getResultSet();
                // resultSet is the last parameter in the procedure
                int lastParamIndex = procInfo.getParameters().size() + 1;
                SPParameter param = new SPParameter(lastParamIndex, SPParameter.RESULT_SET, resultRecord.getFullName());
                param.setClassType(java.sql.ResultSet.class);
                param.setMetadataID(resultRecord);
                for (Column columnRecord : resultRecord.getColumns()) {
                    String colType = columnRecord.getRuntimeType();
                    param.addResultSetColumn(columnRecord.getFullName(), DataTypeManager.getDataTypeClass(colType), columnRecord);
                }
                procInfo.addParameter(param);
            }
            // if this is a virtual procedure get the procedure plan
            if (procRecord.isVirtual()) {
                QueryNode queryNode = new LiveQueryNode(procRecord);
                procInfo.setQueryPlan(queryNode);
            }
            procInfo.setUpdateCount(procRecord.getUpdateCount());
            results.add(procInfo);
        }
        this.procedureCache.put(canonicalName, results);
    }
    StoredProcedureInfo result = null;
    for (StoredProcedureInfo storedProcedureInfo : results) {
        Schema schema = (Schema) storedProcedureInfo.getModelID();
        if (name.equalsIgnoreCase(storedProcedureInfo.getProcedureCallableName()) || vdbMetaData == null || vdbMetaData.isVisible(schema.getName())) {
            if (result != null) {
                throw new QueryMetadataException(QueryPlugin.Event.TEIID30358, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30358, name));
            }
            result = storedProcedureInfo;
        }
    }
    return result;
}
Also used : SPParameter(org.teiid.query.sql.lang.SPParameter) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) QueryNode(org.teiid.query.mapping.relational.QueryNode)

Aggregations

SPParameter (org.teiid.query.sql.lang.SPParameter)14 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)10 Expression (org.teiid.query.sql.symbol.Expression)5 Reference (org.teiid.query.sql.symbol.Reference)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)3 GroupContext (org.teiid.query.sql.lang.GroupContext)3 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)3 HashSet (java.util.HashSet)2 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)2 Procedure (org.teiid.metadata.Procedure)2 Schema (org.teiid.metadata.Schema)2 QueryNode (org.teiid.query.mapping.relational.QueryNode)2 StoredProcedureInfo (org.teiid.query.metadata.StoredProcedureInfo)2 TempMetadataAdapter (org.teiid.query.metadata.TempMetadataAdapter)2 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)2 Command (org.teiid.query.sql.lang.Command)2 Criteria (org.teiid.query.sql.lang.Criteria)2 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)2