Search in sources :

Example 11 with StoredProcedure

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

the class PreparedStatementRequest method generatePlan.

/**
 * @throws TeiidComponentException
 * @throws TeiidProcessingException
 * @see org.teiid.dqp.internal.process.Request#generatePlan()
 */
@Override
protected void generatePlan(boolean addLimit) throws TeiidComponentException, TeiidProcessingException {
    createCommandContext();
    String sqlQuery = requestMsg.getCommands()[0];
    if (this.preParser != null) {
        sqlQuery = this.preParser.preParse(sqlQuery, this.context);
    }
    CacheID id = new CacheID(this.workContext, Request.createParseInfo(this.requestMsg, this.workContext.getSession()), sqlQuery);
    prepPlan = prepPlanCache.get(id);
    if (prepPlan != null) {
        // already in cache. obtain the values from cache
        analysisRecord = prepPlan.getAnalysisRecord();
        ProcessorPlan cachedPlan = prepPlan.getPlan();
        this.userCommand = prepPlan.getCommand();
        if (validateAccess(requestMsg.getCommands(), userCommand, CommandType.PREPARED)) {
            // $NON-NLS-1$
            LogManager.logDetail(LogConstants.CTX_DQP, requestId, "AuthorizationValidator indicates that the prepared plan for command will not be used");
            prepPlan = null;
            analysisRecord = null;
        } else {
            // $NON-NLS-1$
            LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Query exist in cache: ", sqlQuery });
            processPlan = cachedPlan.clone();
        }
    }
    if (prepPlan == null) {
        // if prepared plan does not exist, create one
        prepPlan = new PreparedPlan();
        // $NON-NLS-1$
        LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Query does not exist in cache: ", sqlQuery });
        super.generatePlan(true);
        prepPlan.setCommand(this.userCommand);
        // there's no need to cache the plan if it's a stored procedure, since we already do that in the optimizer
        boolean cache = !(this.userCommand instanceof StoredProcedure);
        // Defect 13751: Clone the plan in its current state (i.e. before processing) so that it can be used for later queries
        prepPlan.setPlan(cache ? processPlan.clone() : processPlan, this.context);
        prepPlan.setAnalysisRecord(analysisRecord);
        if (cache) {
            Determinism determinismLevel = this.context.getDeterminismLevel();
            if (userCommand.getCacheHint() != null && userCommand.getCacheHint().getDeterminism() != null) {
                // $NON-NLS-1$ //$NON-NLS-2$
                LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Cache hint modified the query determinism from ", this.context.getDeterminismLevel(), " to ", determinismLevel });
                determinismLevel = userCommand.getCacheHint().getDeterminism();
            }
            this.prepPlanCache.put(id, determinismLevel, prepPlan, userCommand.getCacheHint() != null ? userCommand.getCacheHint().getTtl() : null);
        }
    }
    if (requestMsg.isBatchedUpdate()) {
        handlePreparedBatchUpdate();
    } else {
        List<Reference> params = prepPlan.getReferences();
        List<?> values = requestMsg.getParameterValues();
        PreparedStatementRequest.resolveParameterValues(params, values, this.context, this.metadata);
    }
}
Also used : Determinism(org.teiid.metadata.FunctionMethod.Determinism) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) Reference(org.teiid.query.sql.symbol.Reference) ProcessorPlan(org.teiid.query.processor.ProcessorPlan)

Example 12 with StoredProcedure

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

the class TestProcedureResolving method testDotInName.

@Test
public void testDotInName() throws Exception {
    // $NON-NLS-1$
    String ddl = "CREATE FOREIGN PROCEDURE \"my.proc\" (param STRING) RETURNS TABLE (a INTEGER, b STRING);";
    TransformationMetadata tm = RealMetadataFactory.fromDDL(ddl, "x", "y");
    StoredProcedure sp = (StoredProcedure) TestResolver.helpResolve("exec \"my.proc\"()", tm);
    assertEquals(2, sp.getProjectedSymbols().size());
    assertEquals("y.my.proc.b", sp.getProjectedSymbols().get(1).toString());
    TestValidator.helpValidate("begin exec proc(); end", new String[] {}, tm);
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Test(org.junit.Test)

Example 13 with StoredProcedure

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

the class TestProcedureResolving method testVarArgs.

@Test
public void testVarArgs() throws Exception {
    String ddl = "create foreign procedure proc (x integer, VARIADIC z integer) returns (x string);\n";
    TransformationMetadata tm = createMetadata(ddl);
    // $NON-NLS-1$
    String sql = "call proc (1, 2, 3)";
    StoredProcedure sp = (StoredProcedure) TestResolver.helpResolve(sql, tm);
    assertEquals("EXEC proc(1, 2, 3)", sp.toString());
    assertEquals(new Constant(1), sp.getParameter(1).getExpression());
    assertEquals(new Array(DataTypeManager.DefaultDataClasses.INTEGER, Arrays.asList((Expression) new Constant(2), new Constant(3))), sp.getParameter(2).getExpression());
    assertEquals(SPParameter.RESULT_SET, sp.getParameter(3).getParameterType());
    // $NON-NLS-1$
    sql = "call proc (1)";
    sp = (StoredProcedure) TestResolver.helpResolve(sql, tm);
    assertEquals("EXEC proc(1)", sp.toString());
    assertEquals(new Array(DataTypeManager.DefaultDataClasses.INTEGER, new ArrayList<Expression>(0)), sp.getParameter(2).getExpression());
    sp = (StoredProcedure) QueryRewriter.evaluateAndRewrite(sp, new Evaluator(null, null, null), null, tm);
    LanguageBridgeFactory lbf = new LanguageBridgeFactory(tm);
    Call call = (Call) lbf.translate(sp);
    assertEquals("EXEC proc(1)", call.toString());
    // $NON-NLS-1$
    sql = "call proc (1, (2, 3))";
    sp = (StoredProcedure) TestResolver.helpResolve(sql, tm);
    assertEquals("EXEC proc(1, (2, 3))", sp.toString());
    assertEquals(new Constant(1), sp.getParameter(1).getExpression());
    assertEquals(new Array(DataTypeManager.DefaultDataClasses.INTEGER, Arrays.asList((Expression) new Constant(2), new Constant(3))), sp.getParameter(2).getExpression());
    assertEquals(SPParameter.RESULT_SET, sp.getParameter(3).getParameterType());
}
Also used : Array(org.teiid.query.sql.symbol.Array) Call(org.teiid.language.Call) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) Evaluator(org.teiid.query.eval.Evaluator) LanguageBridgeFactory(org.teiid.dqp.internal.datamgr.LanguageBridgeFactory) Test(org.junit.Test)

Example 14 with StoredProcedure

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

the class TestProcedureResolving method testOptionalParams.

@Test
public void testOptionalParams() throws Exception {
    String ddl = "create foreign procedure proc (x integer, y string);\n";
    TransformationMetadata tm = createMetadata(ddl);
    // $NON-NLS-1$
    String sql = "call proc (1)";
    StoredProcedure sp = (StoredProcedure) TestResolver.helpResolve(sql, tm);
    assertEquals(new Constant(null, DataTypeManager.DefaultDataClasses.STRING), sp.getParameter(2).getExpression());
    // $NON-NLS-1$
    sql = "call proc (1, 'a')";
    sp = (StoredProcedure) TestResolver.helpResolve(sql, tm);
    assertEquals(new Constant("a", DataTypeManager.DefaultDataClasses.STRING), sp.getParameter(2).getExpression());
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Constant(org.teiid.query.sql.symbol.Constant) Test(org.junit.Test)

Example 15 with StoredProcedure

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

the class TestProcedureResolving method testVarArgs2.

@Test
public void testVarArgs2() throws Exception {
    String ddl = "create foreign procedure proc (VARIADIC z object) returns (x string);\n";
    TransformationMetadata tm = createMetadata(ddl);
    // $NON-NLS-1$
    String sql = "call proc ()";
    StoredProcedure sp = (StoredProcedure) TestResolver.helpResolve(sql, tm);
    assertEquals("EXEC proc()", sp.toString());
    assertEquals(new Array(DataTypeManager.DefaultDataClasses.OBJECT, new ArrayList<Expression>(0)), sp.getParameter(1).getExpression());
    // $NON-NLS-1$
    sql = "call proc (1, (2, 3))";
    sp = (StoredProcedure) TestResolver.helpResolve(sql, tm);
    assertEquals("EXEC proc(1, (2, 3))", sp.toString());
    ArrayList<Expression> expressions = new ArrayList<Expression>();
    expressions.add(new Constant(1));
    expressions.add(new Array(DataTypeManager.DefaultDataClasses.INTEGER, Arrays.asList((Expression) new Constant(2), new Constant(3))));
    assertEquals(new Array(DataTypeManager.DefaultDataClasses.OBJECT, expressions), sp.getParameter(1).getExpression());
}
Also used : Array(org.teiid.query.sql.symbol.Array) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Expression(org.teiid.query.sql.symbol.Expression) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)34 Test (org.junit.Test)10 SPParameter (org.teiid.query.sql.lang.SPParameter)10 Command (org.teiid.query.sql.lang.Command)9 Constant (org.teiid.query.sql.symbol.Constant)9 Expression (org.teiid.query.sql.symbol.Expression)9 ArrayList (java.util.ArrayList)8 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)8 List (java.util.List)7 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)7 QueryCommand (org.teiid.query.sql.lang.QueryCommand)5 Reference (org.teiid.query.sql.symbol.Reference)5 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)4 TeiidProcessingException (org.teiid.core.TeiidProcessingException)4 Call (org.teiid.language.Call)4 Insert (org.teiid.query.sql.lang.Insert)4 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)3 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)3