Search in sources :

Example 76 with TransformationMetadata

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

the class TestTriggerActions method testUpdateWithNonConstant.

@Test
public void testUpdateWithNonConstant() throws Exception {
    TransformationMetadata metadata = TestUpdateValidator.example1();
    TestUpdateValidator.createView("select 1 as x, 2 as y", metadata, GX);
    Table t = metadata.getMetadataStore().getSchemas().get(VM1).getTables().get(GX);
    t.setDeletePlan("");
    t.setUpdatePlan("FOR EACH ROW BEGIN update pm1.g1 set e2 = new.y where e2 = old.y; END");
    t.setInsertPlan("");
    String sql = "update gx set y = x";
    FakeDataManager dm = new FakeDataManager();
    FakeDataStore.addTable("pm1.g1", dm, metadata);
    CommandContext context = createCommandContext();
    BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
    ProcessorPlan plan = TestProcessor.helpGetPlan(TestResolver.helpResolve(sql, metadata), metadata, new DefaultCapabilitiesFinder(caps), context);
    List<?>[] expected = new List[] { Arrays.asList(1) };
    helpProcess(plan, context, dm, expected);
    assertEquals("UPDATE pm1.g1 SET e2 = 1 WHERE e2 = 2", dm.getQueries().get(0));
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Table(org.teiid.metadata.Table) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) List(java.util.List) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Test(org.junit.Test)

Example 77 with TransformationMetadata

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

the class TestTriggerActions method testUpdateIfDistinct.

@Test
public void testUpdateIfDistinct() throws Exception {
    TransformationMetadata metadata = RealMetadataFactory.fromDDL("create foreign table g1 (e1 string, e2 integer) options (updatable true);" + " create view GX options (updatable true) as select '1' as x, 2 as y union all select '2' as x, 2 as y;" + " create trigger on GX instead of update as for each row begin if (\"new\" is distinct from \"old\") update g1 set e1 = new.x, e2 = new.y where e2 = old.y; END", "x", "y");
    String sql = "update gx set x = x || 'a' where y = 2";
    HardcodedDataManager dm = new HardcodedDataManager();
    dm.addData("UPDATE g1 SET e1 = '1a', e2 = 2 WHERE e2 = 2", new List[] { Arrays.asList(1) });
    dm.addData("UPDATE g1 SET e1 = '2a', e2 = 2 WHERE e2 = 2", new List[] { Arrays.asList(1) });
    CommandContext context = createCommandContext();
    BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
    ProcessorPlan plan = TestProcessor.helpGetPlan(TestResolver.helpResolve(sql, metadata), metadata, new DefaultCapabilitiesFinder(caps), context);
    List<?>[] expected = new List[] { Arrays.asList(2) };
    helpProcess(plan, context, dm, expected);
    metadata = RealMetadataFactory.fromDDL("create foreign table g1 (e1 string, e2 integer) options (updatable true);" + " create view GX options (updatable true) as select '1' as x, 2 as y union all select '2' as x, 2 as y;" + " create trigger on GX instead of update as for each row begin if (\"new\" is not distinct from \"old\") update g1 set e1 = new.x, e2 = new.y where e2 = old.y; END", "x", "y");
    // no updates expected
    dm.clearData();
    context = createCommandContext();
    plan = TestProcessor.helpGetPlan(TestResolver.helpResolve(sql, metadata), metadata, new DefaultCapabilitiesFinder(caps), context);
    expected = new List[] { Arrays.asList(2) };
    helpProcess(plan, context, dm, expected);
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) List(java.util.List) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Test(org.junit.Test)

Example 78 with TransformationMetadata

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

the class TestTriggerActions method testInsertRewrite.

/**
 * Ensure that we simplify expressions
 */
@Test
public void testInsertRewrite() throws Exception {
    TransformationMetadata metadata = RealMetadataFactory.fromDDL("create foreign table tablea (s string primary key) options (updatable true);\n" + "create view viewa (i integer) options (updatable true) as SELECT cast(s as integer) from tablea;\n" + "create trigger on viewa instead of insert as for each row begin atomic " + "INSERT INTO tablea (tablea.s) VALUES (\"NEW\".i); END;" + "create trigger on viewa instead of update as for each row begin atomic " + "update tablea set s = \"NEW\".i; END;", "x", "y");
    String sql = "insert into viewa (i) values (1)";
    HardcodedDataManager dm = new HardcodedDataManager();
    dm.addData("INSERT INTO tablea (s) VALUES ('1')", Arrays.asList(1));
    CommandContext context = createCommandContext();
    BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
    caps.setFunctionSupport(SourceSystemFunctions.CONVERT, true);
    ProcessorPlan plan = TestProcessor.helpGetPlan(TestResolver.helpResolve(sql, metadata), metadata, new DefaultCapabilitiesFinder(caps), context);
    List<?>[] expected = new List[] { Arrays.asList(1) };
    helpProcess(plan, context, dm, expected);
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) List(java.util.List) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Test(org.junit.Test)

Example 79 with TransformationMetadata

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

the class TestTriggerActions method testInsertWithDefault.

@Test
public void testInsertWithDefault() throws Exception {
    TransformationMetadata metadata = TestUpdateValidator.example1();
    TestUpdateValidator.createView("select 1 as x, 2 as y", metadata, GX);
    Table t = metadata.getMetadataStore().getSchemas().get(VM1).getTables().get(GX);
    t.setDeletePlan("");
    t.setUpdatePlan("");
    t.setInsertPlan("FOR EACH ROW BEGIN insert into pm1.g1 (e1) values (new.x); END");
    String sql = "insert into gx (x) values (1)";
    FakeDataManager dm = new FakeDataManager();
    FakeDataStore.addTable("pm1.g1", dm, metadata);
    CommandContext context = createCommandContext();
    BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
    ProcessorPlan plan = TestProcessor.helpGetPlan(TestResolver.helpResolve(sql, metadata), metadata, new DefaultCapabilitiesFinder(caps), context);
    List<?>[] expected = new List[] { Arrays.asList(1) };
    helpProcess(plan, context, dm, expected);
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Table(org.teiid.metadata.Table) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) List(java.util.List) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Test(org.junit.Test)

Example 80 with TransformationMetadata

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

the class TestTriggerActions method testUpdateSetExpression.

@Test
public void testUpdateSetExpression() throws Exception {
    TransformationMetadata metadata = RealMetadataFactory.fromDDL("create foreign table g1 (e1 string, e2 integer) options (updatable true);" + " create view GX options (updatable true) as select '1' as x, 2 as y;" + " create trigger on GX instead of update as for each row begin update g1 set e1 = new.x, e2 = new.y where e2 = old.y; END", "x", "y");
    String sql = "update gx set x = x || 'a' where y = 2";
    HardcodedDataManager dm = new HardcodedDataManager();
    dm.addData("UPDATE g1 SET e1 = '1a', e2 = 2 WHERE e2 = 2", new List[] { Arrays.asList(1) });
    CommandContext context = createCommandContext();
    BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
    ProcessorPlan plan = TestProcessor.helpGetPlan(TestResolver.helpResolve(sql, metadata), metadata, new DefaultCapabilitiesFinder(caps), context);
    List<?>[] expected = new List[] { Arrays.asList(1) };
    helpProcess(plan, context, dm, expected);
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) List(java.util.List) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Test(org.junit.Test)

Aggregations

TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)427 Test (org.junit.Test)352 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)163 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)108 FakeCapabilitiesFinder (org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder)97 DefaultCapabilitiesFinder (org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder)89 List (java.util.List)83 CommandContext (org.teiid.query.util.CommandContext)64 HardcodedDataManager (org.teiid.query.processor.HardcodedDataManager)63 ArrayList (java.util.ArrayList)56 TranslationUtility (org.teiid.cdk.api.TranslationUtility)35 MetadataFactory (org.teiid.metadata.MetadataFactory)35 FakeDataManager (org.teiid.query.processor.FakeDataManager)35 RealMetadataFactory (org.teiid.query.unittest.RealMetadataFactory)35 Schema (org.teiid.metadata.Schema)33 Table (org.teiid.metadata.Table)27 FunctionTree (org.teiid.query.function.FunctionTree)22 Column (org.teiid.metadata.Column)21 Command (org.teiid.language.Command)20 UDFSource (org.teiid.query.function.UDFSource)20