use of org.teiid.query.metadata.CompositeMetadataStore in project teiid by teiid.
the class DataTierManagerImpl method processSystemQuery.
/**
* @param command
* @param workItem
* @return
* @throws TeiidComponentException
* @throws TeiidProcessingException
*/
private TupleSource processSystemQuery(CommandContext context, Command command, DQPWorkContext workContext) throws TeiidComponentException, TeiidProcessingException {
String vdbName = workContext.getVdbName();
String vdbVersion = workContext.getVdbVersion();
final VDBMetaData vdb = workContext.getVDB();
TransformationMetadata indexMetadata = vdb.getAttachment(TransformationMetadata.class);
CompositeMetadataStore metadata = indexMetadata.getMetadataStore();
if (command instanceof Query) {
Query query = (Query) command;
UnaryFromClause ufc = (UnaryFromClause) query.getFrom().getClauses().get(0);
GroupSymbol group = ufc.getGroup();
if (StringUtil.startsWithIgnoreCase(group.getNonCorrelationName(), CoreConstants.SYSTEM_ADMIN_MODEL)) {
final SystemAdminTables sysTable = SystemAdminTables.valueOf(group.getNonCorrelationName().substring(CoreConstants.SYSTEM_ADMIN_MODEL.length() + 1).toUpperCase());
BaseExtractionTable<?> et = systemAdminTables.get(sysTable);
return et.processQuery(query, vdb, indexMetadata, context);
}
final SystemTables sysTable = SystemTables.valueOf(group.getNonCorrelationName().substring(CoreConstants.SYSTEM_MODEL.length() + 1).toUpperCase());
BaseExtractionTable<?> et = systemTables.get(sysTable);
return et.processQuery(query, vdb, indexMetadata, context);
}
Collection<List<?>> rows = new ArrayList<List<?>>();
StoredProcedure proc = (StoredProcedure) command;
if (StringUtil.startsWithIgnoreCase(proc.getProcedureCallableName(), CoreConstants.SYSTEM_ADMIN_MODEL)) {
final SystemAdminProcs sysProc = SystemAdminProcs.valueOf(proc.getProcedureCallableName().substring(CoreConstants.SYSTEM_ADMIN_MODEL.length() + 1).toUpperCase());
switch(sysProc) {
case LOGMSG:
case ISLOGGABLE:
String level = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
String logContext = (String) ((Constant) proc.getParameter(3).getExpression()).getValue();
Object message = null;
if (sysProc == SystemAdminProcs.LOGMSG) {
message = ((Constant) proc.getParameter(4).getExpression()).getValue();
}
int msgLevel = getLevel(level);
boolean logged = false;
if (LogManager.isMessageToBeRecorded(logContext, msgLevel)) {
if (message != null) {
LogManager.log(msgLevel, logContext, message);
}
logged = true;
}
if (proc.returnParameters()) {
rows.add(Arrays.asList(logged));
}
return new CollectionTupleSource(rows.iterator());
case SETPROPERTY:
try {
String uuid = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
String key = (String) ((Constant) proc.getParameter(3).getExpression()).getValue();
Clob value = (Clob) ((Constant) proc.getParameter(4).getExpression()).getValue();
key = MetadataFactory.resolvePropertyKey(null, key);
String strVal = null;
String result = null;
if (value != null) {
if (value.length() > MAX_VALUE_LENGTH) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30548, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30548, MAX_VALUE_LENGTH));
}
strVal = ObjectConverterUtil.convertToString(value.getCharacterStream());
}
final AbstractMetadataRecord target = getByUuid(metadata, uuid);
if (target == null) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30549, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30549, uuid));
}
AbstractMetadataRecord schema = target;
while (!(schema instanceof Schema) && schema.getParent() != null) {
schema = schema.getParent();
}
if (schema instanceof Schema && vdb.getImportedModels().contains(((Schema) schema).getName())) {
// $NON-NLS-1$
throw new TeiidProcessingException(QueryPlugin.Event.TEIID31098, QueryPlugin.Util.getString("ValidationVisitor.invalid_alter", uuid));
}
if (getMetadataRepository(target, vdb) != null) {
getMetadataRepository(target, vdb).setProperty(vdbName, vdbVersion, target, key, strVal);
}
result = DdlPlan.setProperty(vdb, target, key, strVal);
if (eventDistributor != null) {
eventDistributor.setProperty(vdbName, vdbVersion, uuid, key, strVal);
}
// materialization depends upon the property values
// $NON-NLS-1$
indexMetadata.addToMetadataCache(target, "transformation/matview", null);
if (proc.returnParameters()) {
if (result == null) {
rows.add(Arrays.asList((Clob) null));
} else {
rows.add(Arrays.asList(new ClobType(new ClobImpl(result))));
}
}
return new CollectionTupleSource(rows.iterator());
} catch (SQLException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30550, e);
} catch (IOException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30551, e);
}
}
final Table table = indexMetadata.getGroupID((String) ((Constant) proc.getParameter(1).getExpression()).getValue());
switch(sysProc) {
case SETCOLUMNSTATS:
final String columnName = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
Column c = null;
for (Column col : table.getColumns()) {
if (col.getName().equalsIgnoreCase(columnName)) {
c = col;
break;
}
}
if (c == null) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30552, columnName + TransformationMetadata.NOT_EXISTS_MESSAGE);
}
Number distinctVals = (Number) ((Constant) proc.getParameter(3).getExpression()).getValue();
Number nullVals = (Number) ((Constant) proc.getParameter(4).getExpression()).getValue();
String max = (String) ((Constant) proc.getParameter(5).getExpression()).getValue();
String min = (String) ((Constant) proc.getParameter(6).getExpression()).getValue();
final ColumnStats columnStats = new ColumnStats();
columnStats.setDistinctValues(distinctVals);
columnStats.setNullValues(nullVals);
columnStats.setMaximumValue(max);
columnStats.setMinimumValue(min);
if (getMetadataRepository(table, vdb) != null) {
getMetadataRepository(table, vdb).setColumnStats(vdbName, vdbVersion, c, columnStats);
}
DdlPlan.setColumnStats(vdb, c, columnStats);
if (eventDistributor != null) {
eventDistributor.setColumnStats(vdbName, vdbVersion, table.getParent().getName(), table.getName(), columnName, columnStats);
}
break;
case SETTABLESTATS:
Constant val = (Constant) proc.getParameter(2).getExpression();
final Number cardinality = (Number) val.getValue();
TableStats tableStats = new TableStats();
tableStats.setCardinality(cardinality);
if (getMetadataRepository(table, vdb) != null) {
getMetadataRepository(table, vdb).setTableStats(vdbName, vdbVersion, table, tableStats);
}
DdlPlan.setTableStats(vdb, table, tableStats);
if (eventDistributor != null) {
eventDistributor.setTableStats(vdbName, vdbVersion, table.getParent().getName(), table.getName(), tableStats);
}
break;
}
return new CollectionTupleSource(rows.iterator());
}
final SystemProcs sysTable = SystemProcs.valueOf(proc.getProcedureCallableName().substring(CoreConstants.SYSTEM_MODEL.length() + 1).toUpperCase());
switch(sysTable) {
case GETXMLSCHEMAS:
try {
Object groupID = indexMetadata.getGroupID((String) ((Constant) proc.getParameter(1).getExpression()).getValue());
List<SQLXMLImpl> schemas = indexMetadata.getXMLSchemas(groupID);
for (SQLXMLImpl schema : schemas) {
rows.add(Arrays.asList(new XMLType(schema)));
}
} catch (QueryMetadataException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30553, e);
}
break;
case ARRAYITERATE:
Object array = ((Constant) proc.getParameter(1).getExpression()).getValue();
if (array != null) {
final Object[] vals;
if (array instanceof Object[]) {
vals = (Object[]) array;
} else {
ArrayImpl arrayImpl = (ArrayImpl) array;
vals = arrayImpl.getValues();
}
return new CollectionTupleSource(new Iterator<List<?>>() {
int index = 0;
@Override
public boolean hasNext() {
return index < vals.length;
}
@Override
public List<?> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return Arrays.asList(vals[index++]);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
});
}
}
return new CollectionTupleSource(rows.iterator());
}
use of org.teiid.query.metadata.CompositeMetadataStore in project teiid by teiid.
the class TestSqlServerConversionVisitor method testUniqueidentifier.
@Test
public void testUniqueidentifier() throws Exception {
MetadataStore metadataStore = new MetadataStore();
// $NON-NLS-1$
Schema foo = RealMetadataFactory.createPhysicalModel("foo", metadataStore);
// $NON-NLS-1$
Table table = RealMetadataFactory.createPhysicalGroup("bar", foo);
String[] elemNames = new String[] { // $NON-NLS-1$
"x" };
String[] elemTypes = new String[] { DataTypeManager.DefaultDataTypes.STRING };
List<Column> cols = RealMetadataFactory.createElements(table, elemNames, elemTypes);
Column obj = cols.get(0);
// $NON-NLS-1$
obj.setNativeType("uniqueidentifier");
CompositeMetadataStore store = new CompositeMetadataStore(metadataStore);
QueryMetadataInterface metadata = new TransformationMetadata(null, store, null, RealMetadataFactory.SFM.getSystemFunctions(), null);
TranslationUtility tu = new TranslationUtility(metadata);
// $NON-NLS-1$
Command command = tu.parseCommand("select max(x) from bar");
// $NON-NLS-1$
TranslationHelper.helpTestVisitor("SELECT MAX(bar.x) FROM bar", trans, command);
// $NON-NLS-1$
command = tu.parseCommand("select * from (select max(x) as max from bar) x");
// $NON-NLS-1$
TranslationHelper.helpTestVisitor("SELECT x.max FROM (SELECT MAX(bar.x) AS max FROM bar) x", trans, command);
// $NON-NLS-1$
command = tu.parseCommand("insert into bar (x) values ('a')");
// $NON-NLS-1$
TranslationHelper.helpTestVisitor("INSERT INTO bar (x) VALUES ('a')", trans, command);
trans = new SQLServerExecutionFactory();
trans.setDatabaseVersion(SQLServerExecutionFactory.V_2000);
trans.start();
// $NON-NLS-1$
command = tu.parseCommand("select max(x) from bar");
// $NON-NLS-1$
TranslationHelper.helpTestVisitor("SELECT MAX(cast(bar.x as char(36))) FROM bar", trans, command);
// $NON-NLS-1$
command = tu.parseCommand("select * from (select max(x) as max from bar) x");
// $NON-NLS-1$
TranslationHelper.helpTestVisitor("SELECT x.max FROM (SELECT MAX(cast(bar.x as char(36))) AS max FROM bar) x", trans, command);
}
use of org.teiid.query.metadata.CompositeMetadataStore in project teiid by teiid.
the class TestOracleTranslator method getOracleSpecificMetadata.
/**
* create fake BQT metadata to test this case, name in source is important
*/
private QueryMetadataInterface getOracleSpecificMetadata() {
MetadataStore metadataStore = new MetadataStore();
// $NON-NLS-1$
Schema foo = RealMetadataFactory.createPhysicalModel("BQT1", metadataStore);
// $NON-NLS-1$
Table table = RealMetadataFactory.createPhysicalGroup("SmallA", foo);
// $NON-NLS-1$
Table x = RealMetadataFactory.createPhysicalGroup("x", foo);
x.setProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, "select c from d");
// $NON-NLS-1$
Table dual = RealMetadataFactory.createPhysicalGroup("DUAL", foo);
// $NON-NLS-1$
table.setNameInSource("SmallishA");
String[] elemNames = new String[] { // $NON-NLS-1$
"DoubleNum", // $NON-NLS-1$
"ID", // $NON-NLS-1$
"timestampvalue", "description", "ndescription" };
String[] elemTypes = new String[] { DataTypeManager.DefaultDataTypes.DOUBLE, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.TIMESTAMP, DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.STRING };
RealMetadataFactory.createElements(x, elemNames, elemTypes);
List<Column> cols = RealMetadataFactory.createElements(table, elemNames, elemTypes);
cols.get(1).setAutoIncremented(true);
// $NON-NLS-1$
cols.get(1).setNameInSource("ID:SEQUENCE=MYSEQUENCE.nextVal");
// $NON-NLS-1$
cols.get(2).setNativeType("date");
cols.get(3).setNativeType("CHAR");
cols.get(4).setNativeType("NCHAR");
// $NON-NLS-1$
List<Column> dualCols = RealMetadataFactory.createElements(dual, new String[] { "something" }, new String[] { DataTypeManager.DefaultDataTypes.STRING });
dualCols.get(0).setNameInSource("seq.nextval");
// $NON-NLS-1$
ProcedureParameter in1 = RealMetadataFactory.createParameter("in1", SPParameter.IN, DataTypeManager.DefaultDataTypes.INTEGER);
// $NON-NLS-1$ //$NON-NLS-2$
ColumnSet<Procedure> rs3 = RealMetadataFactory.createResultSet("proc.rs1", new String[] { "e1" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER });
Procedure p = RealMetadataFactory.createStoredProcedure("proc", foo, Arrays.asList(in1));
p.setResultSet(rs3);
p.setProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, "select x from y where z = $1");
p = RealMetadataFactory.createStoredProcedure("proc1", foo, Arrays.asList(RealMetadataFactory.createParameter("in1", SPParameter.IN, DataTypeManager.DefaultDataTypes.STRING)));
// $NON-NLS-1$ //$NON-NLS-2$
p.setResultSet(RealMetadataFactory.createResultSet("proc.rs1", new String[] { "e1" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER }));
p.setProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, "select $1 from y");
p.setProperty(SQLConversionVisitor.TEIID_NON_PREPARED, "true");
CompositeMetadataStore store = new CompositeMetadataStore(metadataStore);
return new TransformationMetadata(null, store, null, RealMetadataFactory.SFM.getSystemFunctions(), null);
}
use of org.teiid.query.metadata.CompositeMetadataStore in project teiid by teiid.
the class EvaluatorIterator method createTransformationMetadata.
public static TransformationMetadata createTransformationMetadata(String ddl) {
MetadataStore mds = new MetadataStore();
MetadataFactory mf = new MetadataFactory("vdb", 1, IMPLICIT_MODEL_NAME, SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
QueryParser.getQueryParser().parseDDL(mf, ddl);
mf.mergeInto(mds);
CompositeMetadataStore store = new CompositeMetadataStore(mds);
VDBMetaData vdbMetaData = new VDBMetaData();
// $NON-NLS-1$
vdbMetaData.setName("vdb");
vdbMetaData.setVersion(1);
List<FunctionTree> udfs = new ArrayList<FunctionTree>();
for (Schema schema : store.getSchemas().values()) {
vdbMetaData.addModel(createModel(schema.getName(), schema.isPhysical()));
}
TransformationMetadata metadata = new TransformationMetadata(vdbMetaData, store, null, SFM.getSystemFunctions(), udfs);
vdbMetaData.addAttchment(TransformationMetadata.class, metadata);
vdbMetaData.addAttchment(QueryMetadataInterface.class, metadata);
return metadata;
}
use of org.teiid.query.metadata.CompositeMetadataStore in project teiid by teiid.
the class TestTeiidLanguageToCQL method cassandraMetadata.
private QueryMetadataInterface cassandraMetadata(Properties modelProperties) {
MetadataFactory factory = new MetadataFactory("", 1, "", SystemMetadata.getInstance().getRuntimeTypeMap(), modelProperties, "");
createFakeMetadata(factory);
return new TransformationMetadata(null, new CompositeMetadataStore(factory.asMetadataStore()), null, RealMetadataFactory.SFM.getSystemFunctions(), null);
}
Aggregations