/** * 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; }
@Override public Boolean matchesPkColumn(int pkIndex, Expression ex) { if (ex instanceof Function) { Function f = (Function)ex; ex = f.getArg(0); } return (pkColumns[pkIndex].equals(ex)); }
public Expression replaceExpression(Expression element) { if (element instanceof Function && !(element instanceof AggregateSymbol) && ((Function) element).isAggregate()) { Function f = (Function)element; AggregateSymbol as = new AggregateSymbol(f.getName(), false, f.getArgs(), null); as.setType(f.getType()); as.setFunctionDescriptor(f.getFunctionDescriptor()); return as; } return element; } };
/** * @see org.teiid.query.sql.visitor.ExpressionMappingVisitor#replaceExpression(org.teiid.query.sql.symbol.Expression) */ public Expression replaceExpression(Expression exp) { if (exp instanceof Function){ Function function = (Function)exp; if (function.getName().equalsIgnoreCase(FunctionLibrary.CONTEXT)){ this.contextFunctions.add(function); //return 2nd argument to 'context' return function.getArg(1); } } return exp; }
String functionName = function.getName(); String actualName =ALIASED_FUNCTIONS.get(functionName); FunctionLibrary funcLibrary = this.metadata.getFunctionLibrary(); function.setName(actualName); Expression[] args = function.getArgs(); Class<?>[] types = new Class[args.length]; for(int i=0; i<args.length; i++) { function.setFunctionDescriptor(descriptor); 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) { String format = "yyyy-MM-dd"; //$NON-NLS-1$ int length = 10; 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())) {
protected Object evaluatePushdown(Function function, List<?> tuple, Object[] values) throws TeiidComponentException, TeiidProcessingException { final FunctionDescriptor fd = function.getFunctionDescriptor(); if (fd.getMethod() == null) { throw new FunctionExecutionException(QueryPlugin.Event.TEIID30341, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30341, fd.getFullName())); Select select = new Select(); command.setSelect(select); Function f = new Function(function.getName(), functionArgs); f.setType(function.getType()); f.setFunctionDescriptor(fd); select.addSymbol(f); ss = new ScalarSubquery(command); ((Function)((ExpressionSymbol)ss.getCommand().getProjectedSymbols().get(0)).getExpression()).setArgs(functionArgs);
/** * 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(); }
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); }
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())); } }
Function f = (Function)passing.getExpression(); if (f.getName().equalsIgnoreCase(SourceSystemFunctions.JSONTOXML)) { String rootName = (String)this.evaluate(f.getArg(0), tuple); Object lob = this.evaluate(f.getArg(1), tuple); if (rootName == null || lob == null) { return null; throw new FunctionExecutionException(QueryPlugin.Event.TEIID30384, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30384, f.getFunctionDescriptor().getName())); } catch (SQLException e) { throw new FunctionExecutionException(QueryPlugin.Event.TEIID30384, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30384, f.getFunctionDescriptor().getName())); } catch (TeiidProcessingException e) { throw new FunctionExecutionException(QueryPlugin.Event.TEIID30384, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30384, f.getFunctionDescriptor().getName()));
public void visit(Function obj) { if (!this.getMetadata().isEnvAllowed() && (obj.getName().equalsIgnoreCase(FunctionLibrary.ENV) || obj.getName().equalsIgnoreCase(FunctionLibrary.SYS_PROP) || obj.getName().equalsIgnoreCase(FunctionLibrary.ENV_VAR))) { handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.env_not_allowed", obj, obj.getName()), obj); //$NON-NLS-1$ } else if(FunctionLibrary.LOOKUP.equalsIgnoreCase(obj.getName())) { try { ResolverUtil.ResolvedLookup resolvedLookup = ResolverUtil.resolveLookup(obj, getMetadata()); } else if(obj.getName().equalsIgnoreCase(SourceSystemFunctions.XPATHVALUE)) { if(obj.getArgs()[1] instanceof Constant) { Constant xpathConst = (Constant) obj.getArgs()[1]; try { XMLSystemFunctions.validateXpath((String)xpathConst.getValue()); } else if(obj.getName().equalsIgnoreCase(SourceSystemFunctions.TO_BYTES) || obj.getName().equalsIgnoreCase(SourceSystemFunctions.TO_CHARS)) { try { CharsetUtils.getCharset((String)((Constant)obj.getArg(1)).getValue()); } catch (IllegalArgumentException e) { handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.invalid_encoding", obj.getArg(1)), obj); //$NON-NLS-1$ } else if (obj.isAggregate()) { handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.user_defined_aggregate_as_function", obj, obj.getName()), obj); //$NON-NLS-1$ } else if (FunctionLibrary.JSONARRAY.equalsIgnoreCase(obj.getName())) { Expression[] args = obj.getArgs(); for (Expression expression : args) { validateJSONValue(obj, expression);
@Test public void testUser() throws Exception { Function func = new Function("user", new Expression[] {}); //$NON-NLS-1$ FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("user", new Class[] {} ); //$NON-NLS-1$ func.setFunctionDescriptor(desc); FakeDataManager dataMgr = new FakeDataManager(); CommandContext context = new CommandContext(new Long(1), null, null, null, 0); context.setUserName("logon"); //$NON-NLS-1$ assertEquals(context.getUserName(), new Evaluator(Collections.emptyMap(), dataMgr, context).evaluate(func, Collections.emptyList()) ); }
private org.teiid.query.sql.symbol.Expression minusOne( org.teiid.query.sql.symbol.Expression expr) { return new Function("-", new org.teiid.query.sql.symbol.Expression[] { expr, new Constant(1) }); }
public void visit(Function obj) { try { if(obj.getFunctionDescriptor().getPushdown() == PushDown.CANNOT_PUSHDOWN) { return; markInvalid(obj, (obj.isImplicit()?"(implicit) ":"") + obj.getName() + " function not supported by source"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ return; String name = obj.getName(); if (CapabilitiesUtil.supports(Capability.ONLY_FORMAT_LITERALS, modelID, metadata, capFinder) && parseFormat.contains(name)) { return; if (!(obj.getArg(1) instanceof Constant)) { markInvalid(obj, obj.getName() + " non-literal parse format function not supported by source"); //$NON-NLS-1$ return; Constant c = (Constant)obj.getArg(1); if (c.isMultiValued()) { markInvalid(obj, obj.getName() + " non-literal parse format function not supported by source"); //$NON-NLS-1$ return; markInvalid(obj, obj.getName() + " literal parse " + c + " not supported by source"); //$NON-NLS-1$ //$NON-NLS-2$ return;
ConversionResult cr = library.determineNecessaryConversions(function.getName(), function.getType(), args, types, hasArgWithoutType); if (cr.method == null) { return Collections.emptyList(); args[i] = ResolverUtil.getProperlyTypedConstant(((Constant)args[i]).getValue(), newType); } else { function.insertConversion(i, conversions[i]);
public void helpTestCommandPayload(Serializable payload, String property, String expectedValue) throws Exception { Function func = new Function("commandpayload", new Expression[] {}); //$NON-NLS-1$ Class[] parameterSignature = null; if(property == null) { parameterSignature = new Class[] {}; } else { parameterSignature = new Class[] { String.class }; } FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("commandpayload", parameterSignature ); //$NON-NLS-1$ func.setFunctionDescriptor(desc); FakeDataManager dataMgr = new FakeDataManager(); CommandContext context = new CommandContext(null, "user", payload, "vdb", 1, false); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ if(property != null) { func.setArgs(new Expression[] {new Constant(property)}); } String actual = (String) new Evaluator(Collections.emptyMap(), dataMgr, context).evaluate(func, Collections.emptyList()); assertEquals(expectedValue, actual); }
@Test public void testResolveConvertReference() throws Exception { Function function = new Function("convert", new Expression[] {new Reference(0), new Constant(DataTypeManager.DefaultDataTypes.BOOLEAN)}); //$NON-NLS-1$ ResolverVisitor.resolveLanguageObject(function, RealMetadataFactory.example1Cached()); assertEquals(DataTypeManager.DefaultDataClasses.BOOLEAN, function.getType()); assertEquals(DataTypeManager.DefaultDataClasses.BOOLEAN, function.getArgs()[0].getType()); }
public void testExecParamNestedFunction() { StoredProcedure exec = new StoredProcedure(); exec.setProcedureName("pm1.proc1"); //$NON-NLS-1$ exec.setProcedureID("proc"); //$NON-NLS-1$ Function f = new Function("length", new Expression[] { exampleElement(true, 1) }); //$NON-NLS-1$ Function f2 = new Function("+", new Expression[] { f, new Constant(new Integer(1)) }); //$NON-NLS-1$ SPParameter param1 = new SPParameter(1, f2); exec.setParameter(param1); // Run symbol mapper StaticSymbolMappingVisitor visitor = new StaticSymbolMappingVisitor(getSymbolMap()); DeepPreOrderNavigator.doVisit(exec, visitor); // Check that element got switched Function afterFunc = (Function) param1.getExpression(); Function innerFunc = (Function) afterFunc.getArgs()[0]; assertEquals("Stored proc param did not get mapped correctly: ", exampleElement(false, 1), innerFunc.getArg(0)); //$NON-NLS-1$ }
private void collectNotNull(Expression leftExpr, ArrayList<ElementSymbol> notNull) { if (leftExpr instanceof ElementSymbol) { notNull.add((ElementSymbol)leftExpr); } else if (leftExpr instanceof Function) { Function f = (Function)leftExpr; if (!f.getFunctionDescriptor().isNullDependent()) { for (Expression arg : f.getArgs()) { collectNotNull(arg, notNull); } } } }