Search in sources :

Example 11 with Function

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);
}
Also used : Function(org.teiid.query.sql.symbol.Function) Constant(org.teiid.query.sql.symbol.Constant)

Example 12 with Function

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);
}
Also used : Query(org.teiid.query.sql.lang.Query) Reference(org.teiid.query.sql.symbol.Reference) SPParameter(org.teiid.query.sql.lang.SPParameter) MetadataResult(org.teiid.client.metadata.MetadataResult) Function(org.teiid.query.sql.symbol.Function) WindowFunction(org.teiid.query.sql.symbol.WindowFunction) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Expression(org.teiid.query.sql.symbol.Expression) HashMap(java.util.HashMap) Map(java.util.Map) SymbolMap(org.teiid.query.sql.util.SymbolMap)

Example 13 with Function

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;
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) org.teiid.language(org.teiid.language) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) WindowFunction(org.teiid.query.sql.symbol.WindowFunction) Function(org.teiid.query.sql.symbol.Function) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Expression(org.teiid.query.sql.symbol.Expression)

Example 14 with Function

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);
}
Also used : Function(org.teiid.query.sql.symbol.Function) Expression(org.teiid.query.sql.symbol.Expression) Constant(org.teiid.query.sql.symbol.Constant) FunctionLibrary(org.teiid.query.function.FunctionLibrary) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) Test(org.junit.Test)

Example 15 with Function

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);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Function(org.teiid.query.sql.symbol.Function) Expression(org.teiid.query.sql.symbol.Expression) Constant(org.teiid.query.sql.symbol.Constant) FunctionLibrary(org.teiid.query.function.FunctionLibrary) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) Test(org.junit.Test)

Aggregations

Function (org.teiid.query.sql.symbol.Function)55 Constant (org.teiid.query.sql.symbol.Constant)31 Test (org.junit.Test)25 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)16 Expression (org.teiid.query.sql.symbol.Expression)12 FunctionDescriptor (org.teiid.query.function.FunctionDescriptor)11 ArrayList (java.util.ArrayList)10 List (java.util.List)7 ScalarSubquery (org.teiid.query.sql.symbol.ScalarSubquery)7 SearchedCaseExpression (org.teiid.query.sql.symbol.SearchedCaseExpression)6 HashMap (java.util.HashMap)5 ExpressionSymbol (org.teiid.query.sql.symbol.ExpressionSymbol)5 Map (java.util.Map)4 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)4 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)4 Reference (org.teiid.query.sql.symbol.Reference)4 FunctionLibrary (org.teiid.query.function.FunctionLibrary)3 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)3 FakeDataManager (org.teiid.query.processor.FakeDataManager)3 Query (org.teiid.query.sql.lang.Query)3