use of org.teiid.core.types.Transform in project teiid by teiid.
the class EntityCollectionResponse method getPropertyValueInternal.
private static Object getPropertyValueInternal(SingletonPrimitiveType expectedType, boolean isArray, Object value) throws TransformationException, SQLException, IOException {
Class<?> sourceType = DataTypeManager.getRuntimeType(value.getClass());
if (sourceType.isAssignableFrom(expectedType.getDefaultType())) {
return value;
}
if (expectedType instanceof EdmDate && sourceType == Date.class) {
return value;
} else if (expectedType instanceof EdmDateTimeOffset && sourceType == Timestamp.class) {
return value;
} else if (expectedType instanceof EdmTimeOfDay && sourceType == Time.class) {
return value;
} else if (expectedType instanceof EdmBinary) {
// there could be memory implications here, should have been modeled as EdmStream
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_ODATA, "Possible OOM when inlining the stream based values");
if (sourceType == ClobType.class) {
return ClobType.getString((Clob) value).getBytes();
}
if (sourceType == SQLXML.class) {
return ((SQLXML) value).getString().getBytes();
}
if (sourceType == BlobType.class) {
return ObjectConverterUtil.convertToByteArray(((Blob) value).getBinaryStream());
}
if (value instanceof Serializable) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(value);
oos.close();
bos.close();
return bos.toByteArray();
}
}
Class<?> targetType = DataTypeManager.getDataTypeClass(ODataTypeManager.teiidType(expectedType, isArray));
if (sourceType != targetType) {
Transform t = DataTypeManager.getTransform(sourceType, targetType);
value = t != null ? t.transform(value, targetType) : value;
}
return value;
}
use of org.teiid.core.types.Transform in project teiid by teiid.
the class ObjectToAnyTransform method transform.
@Override
public Object transform(Object value, Class<?> targetType) throws TransformationException {
if (value == null) {
return null;
}
if (targetType.isAssignableFrom(value.getClass())) {
return value;
}
Transform transform = DataTypeManager.getTransform(value.getClass(), targetType);
boolean valid = true;
if (transform instanceof ObjectToAnyTransform) {
Object v1 = DataTypeManager.convertToRuntimeType(value, true);
if (v1 != value) {
try {
return transform(v1, targetType);
} catch (TransformationException e) {
throw new TransformationException(CorePlugin.Event.TEIID10076, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10076, getSourceType(), targetClass, value));
}
}
if (targetType.isArray()) {
if (value instanceof Array) {
try {
// TODO: need to use the base type information for non-ArrayImpl values
Object array = ((Array) value).getArray();
if (targetType.isAssignableFrom(array.getClass())) {
if (!(value instanceof ArrayImpl)) {
return new ArrayImpl((Object[]) array);
}
return value;
}
value = array;
} catch (SQLException e) {
throw new TransformationException(e);
}
}
if (value.getClass().isArray()) {
if (value.getClass().getComponentType().isPrimitive() && targetType.getComponentType().isAssignableFrom(convertPrimitiveToObject(value.getClass().getComponentType()))) {
Object[] result = (Object[]) java.lang.reflect.Array.newInstance(targetType.getComponentType(), java.lang.reflect.Array.getLength(value));
for (int i = 0; i < result.length; i++) {
result[i] = java.lang.reflect.Array.get(value, i);
}
return new ArrayImpl(result);
}
Class<?> targetComponentType = targetType.getComponentType();
Object[] result = (Object[]) java.lang.reflect.Array.newInstance(targetComponentType, java.lang.reflect.Array.getLength(value));
for (int i = 0; i < result.length; i++) {
Object v = java.lang.reflect.Array.get(value, i);
if (v.getClass() == targetComponentType || DefaultDataClasses.OBJECT == targetComponentType) {
result[i] = v;
} else {
Transform subTransform = DataTypeManager.getTransform(v.getClass(), targetComponentType);
if (subTransform == null) {
valid = false;
break;
}
result[i] = subTransform.transform(v, targetComponentType);
}
}
if (valid) {
return new ArrayImpl(result);
}
}
}
valid = false;
}
if (transform == null || !valid) {
Object[] params = new Object[] { getSourceType(), targetType, value };
throw new TransformationException(CorePlugin.Event.TEIID10076, CorePlugin.Util.gs(CorePlugin.Event.TEIID10076, params));
}
try {
return transform.transform(value, targetType);
} catch (TransformationException e) {
Object[] params = new Object[] { getSourceType(), targetType, value };
throw new TransformationException(CorePlugin.Event.TEIID10076, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10076, params));
}
}
use of org.teiid.core.types.Transform in project teiid by teiid.
the class MongoDBExecutionFactory method retrieveValue.
/**
* @param field
* @param expectedClass
* @return
* @throws TranslatorException
*/
public Object retrieveValue(Object value, Class<?> expectedClass, DB mongoDB, String fqn, String colName) throws TranslatorException {
if (value == null) {
return null;
}
if (value.getClass().equals(expectedClass)) {
return value;
}
if (value instanceof DBRef) {
Object obj = ((DBRef) value).getId();
if (obj instanceof BasicDBObject) {
BasicDBObject bdb = (BasicDBObject) obj;
return bdb.get(colName);
}
return obj;
} else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Date.class)) {
return new java.sql.Date(((java.util.Date) value).getTime());
} else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Timestamp.class)) {
return new java.sql.Timestamp(((java.util.Date) value).getTime());
} else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Time.class)) {
return new java.sql.Time(((java.util.Date) value).getTime());
} else if (value instanceof String && expectedClass.equals(BigDecimal.class)) {
return new BigDecimal((String) value);
} else if (value instanceof String && expectedClass.equals(BigInteger.class)) {
return new BigInteger((String) value);
} else if (value instanceof String && expectedClass.equals(Character.class)) {
return new Character(((String) value).charAt(0));
} else if (value instanceof String && expectedClass.equals(BinaryType.class)) {
return new BinaryType(((String) value).getBytes());
} else if (value instanceof String && expectedClass.equals(Blob.class)) {
GridFS gfs = new GridFS(mongoDB, fqn);
final GridFSDBFile resource = gfs.findOne((String) value);
if (resource == null) {
return null;
}
return new BlobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return resource.getInputStream();
}
});
} else if (value instanceof String && expectedClass.equals(Clob.class)) {
GridFS gfs = new GridFS(mongoDB, fqn);
final GridFSDBFile resource = gfs.findOne((String) value);
if (resource == null) {
return null;
}
return new ClobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return resource.getInputStream();
}
}, -1);
} else if (value instanceof String && expectedClass.equals(SQLXML.class)) {
GridFS gfs = new GridFS(mongoDB, fqn);
final GridFSDBFile resource = gfs.findOne((String) value);
if (resource == null) {
return null;
}
return new SQLXMLImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return resource.getInputStream();
}
});
} else if (value instanceof BasicDBList) {
BasicDBList arrayValues = (BasicDBList) value;
// array
if (expectedClass.isArray() && !(arrayValues.get(0) instanceof BasicDBObject)) {
Class arrayType = expectedClass.getComponentType();
Object array = Array.newInstance(arrayType, arrayValues.size());
for (int i = 0; i < arrayValues.size(); i++) {
Object arrayItem = retrieveValue(arrayValues.get(i), arrayType, mongoDB, fqn, colName);
Array.set(array, i, arrayItem);
}
value = array;
}
} else if (value instanceof org.bson.types.ObjectId) {
org.bson.types.ObjectId id = (org.bson.types.ObjectId) value;
value = id.toHexString();
} else {
Transform transform = DataTypeManager.getTransform(value.getClass(), expectedClass);
if (transform != null) {
try {
value = transform.transform(value, expectedClass);
} catch (TransformationException e) {
throw new TranslatorException(e);
}
}
}
return value;
}
use of org.teiid.core.types.Transform in project teiid by teiid.
the class QueryRewriter method rewriteFunction.
private Expression rewriteFunction(Function function) throws TeiidComponentException, TeiidProcessingException {
// rewrite alias functions
String functionName = function.getName();
String actualName = ALIASED_FUNCTIONS.get(functionName);
FunctionLibrary funcLibrary = this.metadata.getFunctionLibrary();
if (actualName != null) {
function.setName(actualName);
Expression[] args = function.getArgs();
Class<?>[] types = new Class[args.length];
for (int i = 0; i < args.length; i++) {
types[i] = args[i].getType();
}
FunctionDescriptor descriptor = funcLibrary.findFunction(actualName, types);
function.setFunctionDescriptor(descriptor);
}
if (StringUtil.startsWithIgnoreCase(functionName, "parse")) {
// $NON-NLS-1$
String type = functionName.substring(5);
if (PARSE_FORMAT_TYPES.contains(type) && Number.class.isAssignableFrom(function.getType()) && !type.equals(DataTypeManager.DefaultDataTypes.BIG_DECIMAL)) {
Function result = new Function(SourceSystemFunctions.PARSEBIGDECIMAL, function.getArgs());
FunctionDescriptor descriptor = funcLibrary.findFunction(SourceSystemFunctions.PARSEBIGDECIMAL, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING });
result.setFunctionDescriptor(descriptor);
result.setType(DataTypeManager.DefaultDataClasses.BIG_DECIMAL);
return rewriteFunction(ResolverUtil.getConversion(result, DataTypeManager.DefaultDataTypes.BIG_DECIMAL, DataTypeManager.getDataTypeName(function.getType()), false, metadata.getFunctionLibrary()));
} else if ((DataTypeManager.DefaultDataTypes.DATE.equalsIgnoreCase(type) || DataTypeManager.DefaultDataTypes.TIME.equalsIgnoreCase(type)) && function.getArg(1) instanceof Constant) {
// $NON-NLS-1$
String format = "yyyy-MM-dd";
int length = 10;
if (DataTypeManager.DefaultDataTypes.TIME.equalsIgnoreCase(type)) {
// $NON-NLS-1$
format = "hh:mm:ss";
length = 8;
}
Constant c = (Constant) function.getArg(1);
if (format.equals(c.getValue())) {
Expression arg = function.getArg(0);
if ((arg instanceof Function) && FunctionLibrary.isConvert((Function) arg) && java.util.Date.class.isAssignableFrom(((Function) arg).getArg(0).getType())) {
return rewriteExpressionDirect(ResolverUtil.getConversion(arg, DataTypeManager.DefaultDataTypes.STRING, type, false, metadata.getFunctionLibrary()));
}
}
}
} else if (StringUtil.startsWithIgnoreCase(functionName, "format")) {
// $NON-NLS-1$
String type = functionName.substring(6);
if (PARSE_FORMAT_TYPES.contains(type) && Number.class.isAssignableFrom(function.getArg(0).getType()) && !type.equals(DataTypeManager.DefaultDataTypes.BIG_DECIMAL)) {
Function bigDecimalParam = ResolverUtil.getConversion(function.getArg(0), DataTypeManager.getDataTypeName(function.getArg(0).getType()), DataTypeManager.DefaultDataTypes.BIG_DECIMAL, false, metadata.getFunctionLibrary());
Function result = new Function(SourceSystemFunctions.FORMATBIGDECIMAL, new Expression[] { bigDecimalParam, function.getArg(1) });
FunctionDescriptor descriptor = funcLibrary.findFunction(SourceSystemFunctions.FORMATBIGDECIMAL, new Class[] { DataTypeManager.DefaultDataClasses.BIG_DECIMAL, DataTypeManager.DefaultDataClasses.STRING });
result.setFunctionDescriptor(descriptor);
result.setType(DataTypeManager.DefaultDataClasses.STRING);
return rewriteFunction(result);
} else if ((DataTypeManager.DefaultDataTypes.DATE.equalsIgnoreCase(type) || DataTypeManager.DefaultDataTypes.TIME.equalsIgnoreCase(type)) && function.getArg(1) instanceof Constant) {
// $NON-NLS-1$
String format = "yyyy-MM-dd";
if (DataTypeManager.DefaultDataTypes.TIME.equalsIgnoreCase(type)) {
// $NON-NLS-1$
format = "hh:mm:ss";
}
Constant c = (Constant) function.getArg(1);
if (format.equals(c.getValue())) {
return rewriteExpressionDirect(ResolverUtil.getConversion(function.getArg(0), DataTypeManager.getDataTypeName(function.getArg(0).getType()), DataTypeManager.DefaultDataTypes.STRING, false, metadata.getFunctionLibrary()));
}
}
}
boolean omitNull = false;
Integer code = FUNCTION_MAP.get(functionName);
if (code != null) {
switch(code) {
case 0:
{
// space(x) => repeat(' ', x)
Function result = new Function(SourceSystemFunctions.REPEAT, // $NON-NLS-1$
new Expression[] { new Constant(" "), function.getArg(0) });
// resolve the function
FunctionDescriptor descriptor = funcLibrary.findFunction(SourceSystemFunctions.REPEAT, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.INTEGER });
result.setFunctionDescriptor(descriptor);
result.setType(DataTypeManager.DefaultDataClasses.STRING);
function = result;
break;
}
case 1:
{
// TEIID-4455
break;
}
case 2:
{
// rewrite nullif(a, b) => case when (a = b) then null else a
List when = Arrays.asList(new Criteria[] { new CompareCriteria(function.getArg(0), CompareCriteria.EQ, function.getArg(1)) });
Constant nullConstant = new Constant(null, function.getType());
List then = Arrays.asList(new Expression[] { nullConstant });
SearchedCaseExpression caseExpr = new SearchedCaseExpression(when, then);
caseExpr.setElseExpression(function.getArg(0));
caseExpr.setType(function.getType());
return rewriteExpressionDirect(caseExpr);
}
case 3:
{
Expression[] args = function.getArgs();
if (args.length == 2) {
Function result = new Function(SourceSystemFunctions.IFNULL, new Expression[] { function.getArg(0), function.getArg(1) });
// resolve the function
FunctionDescriptor descriptor = funcLibrary.findFunction(SourceSystemFunctions.IFNULL, new Class[] { function.getType(), function.getType() });
result.setFunctionDescriptor(descriptor);
result.setType(function.getType());
function = result;
}
break;
}
case 4:
omitNull = true;
break;
case 5:
{
if (function.getType() != DataTypeManager.DefaultDataClasses.TIMESTAMP) {
FunctionDescriptor descriptor = funcLibrary.findFunction(SourceSystemFunctions.TIMESTAMPADD, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.INTEGER, DataTypeManager.DefaultDataClasses.TIMESTAMP });
function.setFunctionDescriptor(descriptor);
Class<?> type = function.getType();
function.setType(DataTypeManager.DefaultDataClasses.TIMESTAMP);
function.getArgs()[2] = ResolverUtil.getConversion(function.getArg(2), DataTypeManager.getDataTypeName(type), DataTypeManager.DefaultDataTypes.TIMESTAMP, false, funcLibrary);
function = ResolverUtil.getConversion(function, DataTypeManager.DefaultDataTypes.TIMESTAMP, DataTypeManager.getDataTypeName(type), false, funcLibrary);
}
break;
}
case 6:
case 7:
{
FunctionDescriptor descriptor = funcLibrary.findFunction(SourceSystemFunctions.PARSETIMESTAMP, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING });
function.setName(SourceSystemFunctions.PARSETIMESTAMP);
function.setFunctionDescriptor(descriptor);
Class<?> type = function.getType();
function.setType(DataTypeManager.DefaultDataClasses.TIMESTAMP);
function = ResolverUtil.getConversion(function, DataTypeManager.DefaultDataTypes.TIMESTAMP, DataTypeManager.getDataTypeName(type), false, funcLibrary);
break;
}
case 8:
case 9:
{
FunctionDescriptor descriptor = funcLibrary.findFunction(SourceSystemFunctions.FORMATTIMESTAMP, new Class[] { DataTypeManager.DefaultDataClasses.TIMESTAMP, DataTypeManager.DefaultDataClasses.STRING });
function.setName(SourceSystemFunctions.FORMATTIMESTAMP);
function.setFunctionDescriptor(descriptor);
function.getArgs()[0] = ResolverUtil.getConversion(function.getArg(0), DataTypeManager.getDataTypeName(function.getArg(0).getType()), DataTypeManager.DefaultDataTypes.TIMESTAMP, false, funcLibrary);
break;
}
case 10:
{
if (new Constant(" ").equals(function.getArg(1))) {
// $NON-NLS-1$
String spec = (String) ((Constant) function.getArg(0)).getValue();
Expression string = function.getArg(2);
if (!SQLConstants.Reserved.TRAILING.equalsIgnoreCase(spec)) {
function = new Function(SourceSystemFunctions.LTRIM, new Expression[] { string });
FunctionDescriptor descriptor = funcLibrary.findFunction(SourceSystemFunctions.LTRIM, new Class[] { DataTypeManager.DefaultDataClasses.STRING });
function.setFunctionDescriptor(descriptor);
function.setType(DataTypeManager.DefaultDataClasses.STRING);
string = function;
}
if (!SQLConstants.Reserved.LEADING.equalsIgnoreCase(spec)) {
function = new Function(SourceSystemFunctions.RTRIM, new Expression[] { string });
FunctionDescriptor descriptor = funcLibrary.findFunction(SourceSystemFunctions.RTRIM, new Class[] { DataTypeManager.DefaultDataClasses.STRING });
function.setFunctionDescriptor(descriptor);
function.setType(DataTypeManager.DefaultDataClasses.STRING);
}
}
break;
}
case 11:
{
if (function.getArg(1) instanceof Constant) {
Constant c = (Constant) function.getArg(1);
if (!c.isMultiValued() && !c.isNull()) {
int val = (Integer) c.getValue();
if (val == 0) {
function.getArgs()[1] = new Constant(1);
}
}
}
break;
}
}
}
Expression[] args = function.getArgs();
Expression[] newArgs = new Expression[args.length];
// Rewrite args
int j = 0;
for (int i = 0; i < args.length; i++) {
Expression ex = rewriteExpressionDirect(args[i]);
if (isNull(ex)) {
if (!function.getFunctionDescriptor().isNullDependent()) {
return new Constant(null, function.getType());
}
if (omitNull) {
continue;
}
}
newArgs[j++] = ex;
}
if (omitNull) {
if (j == 0) {
return new Constant(null, function.getType());
}
if (j == 1) {
return newArgs[0];
}
if (j != args.length) {
newArgs = Arrays.copyOf(newArgs, j);
}
}
function.setArgs(newArgs);
if (FunctionLibrary.isConvert(function)) {
Class<?> srcType = newArgs[0].getType();
Class<?> tgtType = function.getType();
if (srcType != null && tgtType != null && srcType.equals(tgtType)) {
// unnecessary conversion
return newArgs[0];
}
if (function.isImplicit()) {
function.setImplicit(false);
}
if (!(newArgs[0] instanceof Function) || tgtType == DataTypeManager.DefaultDataClasses.OBJECT) {
return function;
}
Function nested = (Function) newArgs[0];
if (!FunctionLibrary.isConvert(nested)) {
return function;
}
Class<?> nestedType = nested.getArgs()[0].getType();
Transform t = DataTypeManager.getTransform(nestedType, nested.getType());
if (t.isExplicit()) {
// explicit conversions are required
return function;
}
if (DataTypeManager.getTransform(nestedType, tgtType) == null) {
// no direct conversion exists
return function;
}
// can't remove a convert that would alter the lexical form
if (tgtType == DataTypeManager.DefaultDataClasses.STRING && (nestedType == DataTypeManager.DefaultDataClasses.BOOLEAN || nestedType == DataTypeManager.DefaultDataClasses.DATE || nestedType == DataTypeManager.DefaultDataClasses.TIME || tgtType == DataTypeManager.DefaultDataClasses.BIG_DECIMAL || tgtType == DataTypeManager.DefaultDataClasses.FLOAT || (tgtType == DataTypeManager.DefaultDataClasses.DOUBLE && srcType != DataTypeManager.DefaultDataClasses.FLOAT))) {
return function;
}
// nested implicit transform is not needed
return rewriteExpressionDirect(ResolverUtil.getConversion(nested.getArgs()[0], DataTypeManager.getDataTypeName(nestedType), DataTypeManager.getDataTypeName(tgtType), false, funcLibrary));
}
// convert DECODESTRING function to CASE expression
if (function.getName().equalsIgnoreCase(FunctionLibrary.DECODESTRING) || function.getName().equalsIgnoreCase(FunctionLibrary.DECODEINTEGER)) {
return convertDecodeFunction(function);
}
return function;
}
use of org.teiid.core.types.Transform in project teiid by teiid.
the class SwaggerTypeManager method convertTeiidRuntimeType.
static Object convertTeiidRuntimeType(Object value, Class<?> expectedType) throws TranslatorException {
if (value == null) {
return null;
}
if (expectedType.isAssignableFrom(value.getClass())) {
return value;
} else {
if (expectedType.isAssignableFrom(Timestamp.class) && value instanceof Long) {
return new Timestamp((Long) value);
} else if (expectedType.isAssignableFrom(java.sql.Timestamp.class) && value instanceof String) {
return formTimestamp((String) value);
} else if (expectedType.isAssignableFrom(java.util.Date.class) && value instanceof Long) {
return new java.util.Date((Long) value);
} else if (expectedType.isAssignableFrom(java.util.Date.class) && value instanceof String) {
return formDate((String) value);
} else if (expectedType.isArray() && value instanceof List) {
List<?> values = (List<?>) value;
Class<?> expectedArrayComponentType = expectedType.getComponentType();
Object array = Array.newInstance(expectedArrayComponentType, values.size());
for (int i = 0; i < values.size(); i++) {
Object arrayItem = convertTeiidRuntimeType(values.get(i), expectedArrayComponentType);
Array.set(array, i, arrayItem);
}
return array;
}
Transform transform = DataTypeManager.getTransform(value.getClass(), expectedType);
if (transform != null) {
try {
value = transform.transform(value, expectedType);
} catch (TransformationException e) {
throw new TranslatorException(e);
}
}
}
return value;
}
Aggregations