use of org.teiid.query.mapping.relational.QueryNode in project teiid by teiid.
the class TestProcedureProcessor method addProc.
private void addProc(TransformationMetadata metadata, String name, String query, String[] rsCols, String[] rsTypes, String[] params, String[] paramTypes) {
// $NON-NLS-1$
Schema pm1 = metadata.getMetadataStore().getSchema("PM1");
pm1.getProcedures().remove(name.toUpperCase());
ColumnSet<Procedure> rs2 = RealMetadataFactory.createResultSet("rs1", rsCols, rsTypes);
// $NON-NLS-1$
QueryNode sq2n1 = new QueryNode(query);
ArrayList<ProcedureParameter> procParams = new ArrayList<ProcedureParameter>(params.length);
for (int i = 0; i < params.length; i++) {
procParams.add(RealMetadataFactory.createParameter(params[i], SPParameter.IN, paramTypes[i]));
}
// $NON-NLS-1$
Procedure sq1 = RealMetadataFactory.createVirtualProcedure(name, pm1, procParams, sq2n1);
sq1.setResultSet(rs2);
}
use of org.teiid.query.mapping.relational.QueryNode in project teiid by teiid.
the class TestResolver method helpResolveWithBindings.
public static Command helpResolveWithBindings(String sql, QueryMetadataInterface metadata, List bindings) throws QueryResolverException, TeiidComponentException {
// parse
Command command = helpParse(sql);
QueryNode qn = new QueryNode(sql);
qn.setBindings(bindings);
// resolve
QueryResolver.resolveWithBindingMetadata(command, metadata, qn, true);
CheckSymbolsAreResolvedVisitor vis = new CheckSymbolsAreResolvedVisitor();
DeepPreOrderNavigator.doVisit(command, vis);
Collection<LanguageObject> unresolvedSymbols = vis.getUnresolvedSymbols();
// $NON-NLS-1$
assertTrue("Found unresolved symbols: " + unresolvedSymbols, unresolvedSymbols.isEmpty());
return command;
}
use of org.teiid.query.mapping.relational.QueryNode 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);
}
use of org.teiid.query.mapping.relational.QueryNode in project teiid by teiid.
the class GlobalTableStoreImpl method getGlobalTempTableMetadataId.
@Override
public TempMetadataID getGlobalTempTableMetadataId(Object viewId) throws TeiidProcessingException, TeiidComponentException {
String matViewName = metadata.getFullName(viewId);
String matTableName = RelationalPlanner.MAT_PREFIX + matViewName.toUpperCase();
GroupSymbol group = new GroupSymbol(matViewName);
group.setMetadataID(viewId);
TempMetadataID id = tableStore.getMetadataStore().getTempGroupID(matTableName);
// define the table preserving the key/index information and ensure that only a single instance exists
if (id == null) {
synchronized (viewId) {
id = tableStore.getMetadataStore().getTempGroupID(matTableName);
LinkedHashMap<Expression, Integer> newExprs = null;
if (id == null) {
List<ElementSymbol> allCols = ResolverUtil.resolveElementsInGroup(group, metadata);
QueryNode qnode = metadata.getVirtualPlan(viewId);
if (viewId instanceof Table) {
Table t = (Table) viewId;
List<KeyRecord> fbis = t.getFunctionBasedIndexes();
if (!fbis.isEmpty()) {
List<GroupSymbol> groups = Arrays.asList(group);
int i = 0;
newExprs = new LinkedHashMap<Expression, Integer>();
for (KeyRecord keyRecord : fbis) {
for (int j = 0; j < keyRecord.getColumns().size(); j++) {
Column c = keyRecord.getColumns().get(j);
if (c.getParent() != keyRecord) {
continue;
}
String exprString = c.getNameInSource();
Expression ex = QueryParser.getQueryParser().parseExpression(exprString);
Integer index = newExprs.get(ex);
if (index == null) {
ResolverVisitor.resolveLanguageObject(ex, groups, metadata);
ex = QueryRewriter.rewriteExpression(ex, null, metadata);
String colName = TEIID_FBI + i;
while (t.getColumnByName(colName) != null) {
colName = TEIID_FBI + (++i);
}
ElementSymbol es = new ElementSymbol(colName);
es.setType(ex.getType());
allCols.add(es);
c.setPosition(allCols.size());
newExprs.put(ex, allCols.size());
ex = (Expression) ex.clone();
} else {
c.setPosition(index);
}
}
}
ResolverUtil.clearGroupInfo(group, metadata);
// $NON-NLS-1$
StringBuilder query = new StringBuilder("SELECT ");
// $NON-NLS-1$
query.append(group).append(".*, ");
for (Iterator<Expression> iter = newExprs.keySet().iterator(); iter.hasNext(); ) {
query.append(iter.next());
if (iter.hasNext()) {
// $NON-NLS-1$
query.append(", ");
}
}
// $NON-NLS-1$ //$NON-NLS-2$
query.append(" FROM ").append(group).append(" option nocache ").append(group);
qnode = new QueryNode(query.toString());
}
}
id = tableStore.getMetadataStore().addTempGroup(matTableName, allCols, false, true);
id.setQueryNode(qnode);
id.setCardinality((int) metadata.getCardinality(viewId));
id.setOriginalMetadataID(viewId);
Object pk = metadata.getPrimaryKey(viewId);
if (pk != null) {
ArrayList<TempMetadataID> primaryKey = resolveIndex(metadata, id, pk);
id.setPrimaryKey(primaryKey);
}
Collection keys = metadata.getUniqueKeysInGroup(viewId);
for (Object key : keys) {
id.addUniqueKey(resolveIndex(metadata, id, key));
}
Collection indexes = metadata.getIndexesInGroup(viewId);
for (Object index : indexes) {
id.addIndex(index, resolveIndex(metadata, id, index));
}
if (newExprs != null) {
Table table = (Table) viewId;
List<KeyRecord> fbis = table.getFunctionBasedIndexes();
for (KeyRecord keyRecord : fbis) {
id.addIndex(keyRecord, resolveIndex(metadata, id, keyRecord));
}
GroupSymbol gs = new GroupSymbol(matTableName);
gs.setMetadataID(id);
SymbolMap map = SymbolMap.createSymbolMap(group, ResolverUtil.resolveElementsInGroup(gs, metadata).subList(0, allCols.size() - newExprs.size()), metadata);
LinkedHashMap<Expression, Integer> mappedExprs = new LinkedHashMap<Expression, Integer>();
for (Map.Entry<Expression, Integer> entry : newExprs.entrySet()) {
Expression ex = (Expression) entry.getKey().clone();
ExpressionMappingVisitor.mapExpressions(ex, map.asMap());
mappedExprs.put(ex, entry.getValue());
}
id.getTableData().setFunctionBasedExpressions(mappedExprs);
}
}
}
}
updateCacheHint(viewId, group, id);
return id;
}
use of org.teiid.query.mapping.relational.QueryNode in project teiid by teiid.
the class GlobalTableStoreImpl method getCodeTableMetadataId.
@Override
public TempMetadataID getCodeTableMetadataId(String codeTableName, String returnElementName, String keyElementName, String matTableName) throws TeiidComponentException, QueryMetadataException {
ElementSymbol keyElement = new ElementSymbol(matTableName + ElementSymbol.SEPARATOR + keyElementName);
ElementSymbol returnElement = new ElementSymbol(matTableName + ElementSymbol.SEPARATOR + returnElementName);
keyElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + keyElementName))));
returnElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + returnElementName))));
TempMetadataID id = this.tableStore.getMetadataStore().getTempGroupID(matTableName);
if (id == null) {
synchronized (this) {
id = this.tableStore.getMetadataStore().addTempGroup(matTableName, Arrays.asList(keyElement, returnElement), false, true);
// $NON-NLS-1$
String queryString = Reserved.SELECT + ' ' + new ElementSymbol(keyElementName) + " ," + new ElementSymbol(returnElementName) + ' ' + Reserved.FROM + ' ' + new GroupSymbol(codeTableName);
id.setQueryNode(new QueryNode(queryString));
id.setPrimaryKey(id.getElements().subList(0, 1));
CacheHint hint = new CacheHint(true, null);
id.setCacheHint(hint);
}
}
return id;
}
Aggregations