use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestFunction method testFunction5.
public void testFunction5() {
// $NON-NLS-1$
Function f1 = new Function("f1", new Expression[] { null });
// $NON-NLS-1$ //$NON-NLS-2$
Function f2 = new Function("f1", new Expression[] { new Constant("xyz") });
UnitTestUtil.helpTestEquivalence(1, f1, f2);
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class MetaDataProcessor method getMetadataForCommand.
// For each projected symbol, construct a metadata map
private MetadataResult getMetadataForCommand(Command originalCommand) throws TeiidComponentException {
Map<Integer, Object>[] columnMetadata = null;
switch(originalCommand.getType()) {
case Command.TYPE_QUERY:
if (originalCommand instanceof Query) {
if (((Query) originalCommand).getInto() == null) {
columnMetadata = createProjectedSymbolMetadata(originalCommand);
}
} else {
columnMetadata = createProjectedSymbolMetadata(originalCommand);
}
break;
case Command.TYPE_STORED_PROCEDURE:
columnMetadata = createProjectedSymbolMetadata(originalCommand);
break;
case Command.TYPE_INSERT:
case Command.TYPE_UPDATE:
case Command.TYPE_DELETE:
case Command.TYPE_CREATE:
case Command.TYPE_DROP:
break;
default:
if (originalCommand.returnsResultSet()) {
columnMetadata = createProjectedSymbolMetadata(originalCommand);
}
}
Map<Reference, String> paramMap = Collections.emptyMap();
if (originalCommand instanceof StoredProcedure) {
StoredProcedure sp = (StoredProcedure) originalCommand;
paramMap = new HashMap<Reference, String>();
Collection<SPParameter> params = sp.getParameters();
for (SPParameter spParameter : params) {
if (spParameter.getParameterType() != SPParameter.INOUT && spParameter.getParameterType() != SPParameter.IN && spParameter.getParameterType() != SPParameter.RETURN_VALUE) {
continue;
}
Expression ex = spParameter.getExpression();
if (ex instanceof Function && FunctionLibrary.isConvert((Function) ex)) {
ex = ((Function) ex).getArg(0);
}
if (ex instanceof Reference) {
paramMap.put((Reference) ex, spParameter.getParameterSymbol().getShortName());
}
}
}
List<Reference> params = ReferenceCollectorVisitor.getReferences(originalCommand);
Map<Integer, Object>[] paramMetadata = new Map[params.size()];
for (int i = 0; i < params.size(); i++) {
Reference param = params.get(i);
paramMetadata[i] = getDefaultColumn(null, paramMap.get(param), param.getType());
}
return new MetadataResult(columnMetadata, paramMetadata);
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class LanguageBridgeFactory method translate.
org.teiid.language.Expression translate(Function function) {
Expression[] args = function.getArgs();
List<org.teiid.language.Expression> params = new ArrayList<org.teiid.language.Expression>(args.length);
for (int i = 0; i < args.length; i++) {
params.add(translate(args[i]));
}
String name = function.getName();
if (function.getFunctionDescriptor() != null) {
name = function.getFunctionDescriptor().getName();
if (!supportsConcat2 && function.getFunctionDescriptor().getMethod().getParent() == null && name.equalsIgnoreCase(SourceSystemFunctions.CONCAT2)) {
Expression[] newArgs = new Expression[args.length];
boolean useCase = true;
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof Constant) {
newArgs[i] = args[i];
useCase = false;
} else {
// $NON-NLS-1$
Function f = new Function(SourceSystemFunctions.IFNULL, new Expression[] { args[i], new Constant("") });
newArgs[i] = f;
f.setType(args[i].getType());
FunctionDescriptor descriptor = metadataFactory.getMetadata().getFunctionLibrary().findFunction(SourceSystemFunctions.IFNULL, new Class[] { args[i].getType(), DataTypeManager.DefaultDataClasses.STRING });
f.setFunctionDescriptor(descriptor);
}
}
Function concat = new Function(SourceSystemFunctions.CONCAT, newArgs);
concat.setType(DataTypeManager.DefaultDataClasses.STRING);
if (!useCase) {
return translate(concat);
}
FunctionDescriptor descriptor = metadataFactory.getMetadata().getFunctionLibrary().findFunction(SourceSystemFunctions.CONCAT, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING });
concat.setFunctionDescriptor(descriptor);
List<CompoundCriteria> when = Arrays.asList(new CompoundCriteria(CompoundCriteria.AND, new IsNullCriteria(args[0]), new IsNullCriteria(args[1])));
Constant nullConstant = new Constant(null, DataTypeManager.DefaultDataClasses.STRING);
List<Constant> then = Arrays.asList(nullConstant);
SearchedCaseExpression caseExpr = new SearchedCaseExpression(when, then);
caseExpr.setElseExpression(concat);
caseExpr.setType(DataTypeManager.DefaultDataClasses.STRING);
return translate(caseExpr);
}
// check for translator pushdown functions, and use the name in source if possible
if (function.getFunctionDescriptor().getMethod().getNameInSource() != null && (CoreConstants.SYSTEM_MODEL.equals(function.getFunctionDescriptor().getSchema()) || (function.getFunctionDescriptor().getMethod().getParent() != null && function.getFunctionDescriptor().getMethod().getParent().isPhysical()))) {
name = function.getFunctionDescriptor().getMethod().getNameInSource();
}
} else {
name = Symbol.getShortName(name);
}
// if there is any ambiguity in the function name it will be up to the translator logic to check the
// metadata
org.teiid.language.Function result = new org.teiid.language.Function(name, params, function.getType());
if (function.getFunctionDescriptor() != null) {
result.setMetadataObject(function.getFunctionDescriptor().getMethod());
}
return result;
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestResolver method testUser.
/**
* test jdbc USER method
*/
@Test
public void testUser() {
// String sql = "select intkey from SmallA where user() = 'bqt2'";
// Expected left expression
FunctionLibrary library = RealMetadataFactory.SFM.getSystemFunctionLibrary();
FunctionDescriptor fd = library.findFunction(FunctionLibrary.USER, new Class[] {});
Function user = new Function(fd.getName(), new Expression[] {});
user.setFunctionDescriptor(fd);
// Expected criteria
CompareCriteria expected = new CompareCriteria();
// Expected right expression
// $NON-NLS-1$
Expression e1 = new Constant("bqt2", String.class);
// Expected left expression
expected.setLeftExpression(user);
expected.setOperator(CompareCriteria.EQ);
expected.setRightExpression(e1);
// Resolve the query and check against expected objects
// $NON-NLS-1$
CompareCriteria actual = (CompareCriteria) helpResolveCriteria("user()='bqt2'");
// $NON-NLS-1$
assertEquals("Did not match expected criteria", expected, actual);
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestResolver method testStringConversion2.
@Test
public void testStringConversion2() {
// Expected left expression
// $NON-NLS-1$
ElementSymbol e1 = new ElementSymbol("pm3.g1.e2");
e1.setType(DataTypeManager.DefaultDataClasses.DATE);
// Expected right expression
Class srcType = DataTypeManager.DefaultDataClasses.STRING;
String tgtTypeName = DataTypeManager.DefaultDataTypes.DATE;
// $NON-NLS-1$
Expression expression = new Constant("2003-02-27");
FunctionLibrary library = RealMetadataFactory.SFM.getSystemFunctionLibrary();
FunctionDescriptor fd = library.findFunction(FunctionLibrary.CONVERT, new Class[] { srcType, DataTypeManager.DefaultDataClasses.STRING });
Function conversion = new Function(fd.getName(), new Expression[] { expression, new Constant(tgtTypeName) });
conversion.setType(DataTypeManager.getDataTypeClass(tgtTypeName));
conversion.setFunctionDescriptor(fd);
conversion.makeImplicit();
// Expected criteria
CompareCriteria expected = new CompareCriteria();
expected.setLeftExpression(conversion);
expected.setOperator(CompareCriteria.EQ);
expected.setRightExpression(e1);
// Resolve the query and check against expected objects
// $NON-NLS-1$
CompareCriteria actual = (CompareCriteria) helpResolveCriteria("'2003-02-27'=pm3.g1.e2");
// if (! actual.getLeftExpression().equals(expected.getLeftExpression())) {
// fail("Left expressions not equal");
// } else if (!actual.getRightExpression().equals(expected.getRightExpression())) {
// fail("Right expressions not equal");
// }
// $NON-NLS-1$
assertEquals("Did not match expected criteria", expected, actual);
}
Aggregations