public static org.teiid.query.sql.symbol.Function helpExample(String name) { Constant c1 = new Constant(new Integer(100)); Constant c2 = new Constant(new Integer(200)); org.teiid.query.sql.symbol.Function f = new org.teiid.query.sql.symbol.Function(name, new org.teiid.query.sql.symbol.Expression[] {c1, c2}); f.setType(Integer.class); return f; }
static Expression op(String op, Expression expr1, Expression expr2, FunctionLibrary functionLibrary) { if (expr1 == null) { return expr2; } if (expr2 == null) { return expr1; } Function newExpr = new Function(op, new Expression[] {expr1, expr2}); newExpr.setFunctionDescriptor(functionLibrary.findFunction(op, new Class[] {DataTypeManager.DefaultDataClasses.INTEGER, DataTypeManager.DefaultDataClasses.INTEGER})); newExpr.setType(newExpr.getFunctionDescriptor().getReturnType()); return evaluateIfPossible(newExpr); }
static Expression op(String op, Expression expr1, Expression expr2, FunctionLibrary functionLibrary) { if (expr1 == null) { return expr2; } if (expr2 == null) { return expr1; } Function newExpr = new Function(op, new Expression[] {expr1, expr2}); newExpr.setFunctionDescriptor(functionLibrary.findFunction(op, new Class[] {DataTypeManager.DefaultDataClasses.INTEGER, DataTypeManager.DefaultDataClasses.INTEGER})); newExpr.setType(newExpr.getFunctionDescriptor().getReturnType()); return evaluateIfPossible(newExpr); }
static Expression op(String op, Expression expr1, Expression expr2, FunctionLibrary functionLibrary) { if (expr1 == null) { return expr2; } if (expr2 == null) { return expr1; } Function newExpr = new Function(op, new Expression[] {expr1, expr2}); newExpr.setFunctionDescriptor(functionLibrary.findFunction(op, new Class[] {DataTypeManager.DefaultDataClasses.INTEGER, DataTypeManager.DefaultDataClasses.INTEGER})); newExpr.setType(newExpr.getFunctionDescriptor().getReturnType()); return evaluateIfPossible(newExpr); }
/** * Utility to set the type of an expression if it is a Reference and has a null type. * @param expression the expression to test * @param targetType the target type, if the expression's type is null. * @throws QueryResolverException if unable to set the reference type to the target type. */ public static void setDesiredType(Expression expression, Class<?> targetType, LanguageObject surroundingExpression) throws QueryResolverException { if (expression instanceof Reference) { Reference ref = (Reference)expression; if (ref.isPositional() && ref.getType() == null) { if (targetType == null) { throw new QueryResolverException(QueryPlugin.Event.TEIID30083, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30083, surroundingExpression)); } ref.setType(targetType); } } else if (expression instanceof Function) { Function f = (Function)expression; if (f.getType() == null) { f.setType(targetType); } } }
/** * Utility to set the type of an expression if it is a Reference and has a null type. * @param expression the expression to test * @param targetType the target type, if the expression's type is null. * @throws QueryResolverException if unable to set the reference type to the target type. */ public static void setDesiredType(Expression expression, Class<?> targetType, LanguageObject surroundingExpression) throws QueryResolverException { if (expression instanceof Reference) { Reference ref = (Reference)expression; if (ref.isPositional() && ref.getType() == null) { if (targetType == null) { throw new QueryResolverException(QueryPlugin.Event.TEIID30083, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30083, surroundingExpression)); } ref.setType(targetType); } } else if (expression instanceof Function) { Function f = (Function)expression; if (f.getType() == null) { f.setType(targetType); } } }
/** * Utility to set the type of an expression if it is a Reference and has a null type. * @param expression the expression to test * @param targetType the target type, if the expression's type is null. * @throws QueryResolverException if unable to set the reference type to the target type. */ public static void setDesiredType(Expression expression, Class<?> targetType, LanguageObject surroundingExpression) throws QueryResolverException { if (expression instanceof Reference) { Reference ref = (Reference)expression; if (ref.isPositional() && ref.getType() == null) { if (targetType == null) { throw new QueryResolverException(QueryPlugin.Event.TEIID30083, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30083, surroundingExpression)); } ref.setType(targetType); } } else if (expression instanceof Function) { Function f = (Function)expression; if (f.getType() == null) { f.setType(targetType); } } }
/** * Return a deep copy of this object. * @return Deep copy of the object */ public Object clone() { Expression[] copyArgs = LanguageObject.Util.deepClone(this.args); Function copy = new Function(getName(), copyArgs); copy.setType(getType()); copy.setFunctionDescriptor(getFunctionDescriptor()); if(this.isImplicit()) { copy.makeImplicit(); } copy.eval = this.eval; return copy; }
/** * Insert a conversion function at specified index. This is a convenience * method to insert a conversion into the function tree. * @param index Argument index to insert conversion function at * @param functionDescriptor Conversion function descriptor */ public void insertConversion(int index, FunctionDescriptor functionDescriptor) { // Get target type for conversion Class<?> t = functionDescriptor.getReturnType(); String typeName = DataTypeManager.getDataTypeName(t); // Pull old expression at index Expression newArg[] = new Expression[] { args[index], new Constant(typeName) }; // Replace old expression with new expression, using old as arg Function func = new Function(functionDescriptor.getName(), newArg); args[index] = func; // Set function descriptor and type of new function func.setFunctionDescriptor(functionDescriptor); func.setType(t); func.makeImplicit(); }
/** * Insert a conversion function at specified index. This is a convenience * method to insert a conversion into the function tree. * @param index Argument index to insert conversion function at * @param functionDescriptor Conversion function descriptor */ public void insertConversion(int index, FunctionDescriptor functionDescriptor) { // Get target type for conversion Class<?> t = functionDescriptor.getReturnType(); String typeName = DataTypeManager.getDataTypeName(t); // Pull old expression at index Expression newArg[] = new Expression[] { args[index], new Constant(typeName) }; // Replace old expression with new expression, using old as arg Function func = new Function(functionDescriptor.getName(), newArg); args[index] = func; // Set function descriptor and type of new function func.setFunctionDescriptor(functionDescriptor); func.setType(t); func.makeImplicit(); }
/** * Return a deep copy of this object. * @return Deep copy of the object */ public Object clone() { Expression[] copyArgs = LanguageObject.Util.deepClone(this.args); Function copy = new Function(getName(), copyArgs); copy.setType(getType()); copy.setFunctionDescriptor(getFunctionDescriptor()); if(this.isImplicit()) { copy.makeImplicit(); } copy.eval = this.eval; return copy; }
/** * Return a deep copy of this object. * @return Deep copy of the object */ public Object clone() { Expression[] copyArgs = LanguageObject.Util.deepClone(this.args); Function copy = new Function(getName(), copyArgs); copy.setType(getType()); copy.setFunctionDescriptor(getFunctionDescriptor()); if(this.isImplicit()) { copy.makeImplicit(); } copy.eval = this.eval; return copy; }
/** * Insert a conversion function at specified index. This is a convenience * method to insert a conversion into the function tree. * @param index Argument index to insert conversion function at * @param functionDescriptor Conversion function descriptor */ public void insertConversion(int index, FunctionDescriptor functionDescriptor) { // Get target type for conversion Class<?> t = functionDescriptor.getReturnType(); String typeName = DataTypeManager.getDataTypeName(t); // Pull old expression at index Expression newArg[] = new Expression[] { args[index], new Constant(typeName) }; // Replace old expression with new expression, using old as arg Function func = new Function(functionDescriptor.getName(), newArg); args[index] = func; // Set function descriptor and type of new function func.setFunctionDescriptor(functionDescriptor); func.setType(t); func.makeImplicit(); }
@Test public void testProjectExpression() throws Exception { ElementSymbol es1 = new ElementSymbol("e1"); //$NON-NLS-1$ es1.setType(DataTypeManager.DefaultDataClasses.STRING); List elements = new ArrayList(); elements.add(es1); Function func = new Function("concat", new Expression[] { es1, new Constant("abc")}); //$NON-NLS-1$ //$NON-NLS-2$ FunctionDescriptor fd = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("concat", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING }); //$NON-NLS-1$ func.setFunctionDescriptor(fd); func.setType(DataTypeManager.DefaultDataClasses.STRING); ExpressionSymbol expr = new ExpressionSymbol("expr", func); //$NON-NLS-1$ List projectElements = new ArrayList(); projectElements.add(expr); List[] data = new List[] { Arrays.asList(new Object[] { "1" }), //$NON-NLS-1$ Arrays.asList(new Object[] { "2" }) }; //$NON-NLS-1$ List[] expected = new List[] { Arrays.asList(new Object[] { "1abc" }), //$NON-NLS-1$ Arrays.asList(new Object[] { "2abc" }) }; //$NON-NLS-1$ helpTestProject(projectElements, data, elements, expected, null); }
@Test public void testProjectExpressionFunctionFails() throws Exception { ElementSymbol es1 = new ElementSymbol("e1"); //$NON-NLS-1$ es1.setType(DataTypeManager.DefaultDataClasses.STRING); List elements = new ArrayList(); elements.add(es1); Function func = new Function("convert", new Expression[] { es1, new Constant("integer")}); //$NON-NLS-1$ //$NON-NLS-2$ FunctionDescriptor fd = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("convert", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING }); //$NON-NLS-1$ func.setFunctionDescriptor(fd); func.setType(DataTypeManager.DefaultDataClasses.INTEGER); ExpressionSymbol expr = new ExpressionSymbol("expr", func); //$NON-NLS-1$ List projectElements = new ArrayList(); projectElements.add(expr); List[] data = new List[] { Arrays.asList(new Object[] { "1" }), //$NON-NLS-1$ Arrays.asList(new Object[] { "2x" }) }; //$NON-NLS-1$ String expectedMessage = "TEIID30328 UNABLE TO EVALUATE CONVERT(E1, INTEGER): TEIID30384 ERROR WHILE EVALUATING FUNCTION CONVERT"; //$NON-NLS-1$ helpTestProjectFails(projectElements, data, elements, expectedMessage); }
FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("lookup", new Class[] { String.class, String.class, String.class, String.class } ); //$NON-NLS-1$ func.setFunctionDescriptor(desc); func.setType(DataTypeManager.DefaultDataClasses.STRING);
/** * IMPORTANT: source and target must be basic runtime types * @param sourceExpression * @param sourceTypeName * @param targetTypeName * @param implicit * @param library * @return */ public static Function getConversion(Expression sourceExpression, String sourceTypeName, String targetTypeName, boolean implicit, FunctionLibrary library) { Class<?> srcType = DataTypeManager.getDataTypeClass(sourceTypeName); Class<?> targetType = DataTypeManager.getDataTypeClass(targetTypeName); try { setDesiredType(sourceExpression, targetType, sourceExpression); } catch (QueryResolverException e) { } FunctionDescriptor fd = library.findTypedConversionFunction(srcType, DataTypeManager.getDataTypeClass(targetTypeName)); Function conversion = new Function(fd.getName(), new Expression[] { sourceExpression, new Constant(targetTypeName) }); conversion.setType(DataTypeManager.getDataTypeClass(targetTypeName)); conversion.setFunctionDescriptor(fd); if (implicit) { conversion.makeImplicit(); } return conversion; }
private void modifyToCheckMatViewStatus(QueryMetadataInterface metadata, QueryCommand queryCommand, Set<Object> ids) throws QueryMetadataException, TeiidComponentException { for (Object viewMatadataId : ids) { String schemaName = metadata.getName(metadata.getModelID(viewMatadataId)); String viewName = metadata.getName(viewMatadataId); Expression expr1 = new Constant(schemaName); Expression expr2 = new Constant(viewName); Function status = new Function("mvstatus", new Expression[] {expr1, expr2}); //$NON-NLS-1$ status.setType(DataTypeManager.DefaultDataClasses.INTEGER); FunctionDescriptor descriptor = metadata.getFunctionLibrary().findFunction("mvstatus", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING }); //$NON-NLS-1$ status.setFunctionDescriptor(descriptor); Query query = queryCommand.getProjectedQuery(); //insert first so that it gets evaluated ahead of any false predicate query.setCriteria(Criteria.combineCriteria(new CompareCriteria(status, CompareCriteria.EQ, new Constant(1)), query.getCriteria())); } }
private void modifyToCheckMatViewStatus(QueryMetadataInterface metadata, QueryCommand queryCommand, Set<Object> ids) throws QueryMetadataException, TeiidComponentException { for (Object viewMatadataId : ids) { String schemaName = metadata.getName(metadata.getModelID(viewMatadataId)); String viewName = metadata.getName(viewMatadataId); Expression expr1 = new Constant(schemaName); Expression expr2 = new Constant(viewName); Function status = new Function("mvstatus", new Expression[] {expr1, expr2}); //$NON-NLS-1$ status.setType(DataTypeManager.DefaultDataClasses.INTEGER); FunctionDescriptor descriptor = metadata.getFunctionLibrary().findFunction("mvstatus", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING }); //$NON-NLS-1$ status.setFunctionDescriptor(descriptor); Query query = queryCommand.getProjectedQuery(); //insert first so that it gets evaluated ahead of any false predicate query.setCriteria(Criteria.combineCriteria(new CompareCriteria(status, CompareCriteria.EQ, new Constant(1)), query.getCriteria())); } }
private void modifyToCheckMatViewStatus(QueryMetadataInterface metadata, QueryCommand queryCommand, Set<Object> ids) throws QueryMetadataException, TeiidComponentException { for (Object viewMatadataId : ids) { String schemaName = metadata.getName(metadata.getModelID(viewMatadataId)); String viewName = metadata.getName(viewMatadataId); Expression expr1 = new Constant(schemaName); Expression expr2 = new Constant(viewName); Function status = new Function("mvstatus", new Expression[] {expr1, expr2}); //$NON-NLS-1$ status.setType(DataTypeManager.DefaultDataClasses.INTEGER); FunctionDescriptor descriptor = metadata.getFunctionLibrary().findFunction("mvstatus", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING }); //$NON-NLS-1$ status.setFunctionDescriptor(descriptor); Query query = queryCommand.getProjectedQuery(); //insert first so that it gets evaluated ahead of any false predicate query.setCriteria(Criteria.combineCriteria(new CompareCriteria(status, CompareCriteria.EQ, new Constant(1)), query.getCriteria())); } }