Search in sources :

Example 6 with AggregateAttributes

use of org.teiid.metadata.AggregateAttributes in project teiid by teiid.

the class ValidationVisitor method visit.

@Override
public void visit(AggregateSymbol obj) {
    if (!inQuery) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("SQLParser.Aggregate_only_top_level", obj), obj);
        return;
    }
    if (obj.getAggregateFunction() == AggregateSymbol.Type.USER_DEFINED) {
        AggregateAttributes aa = obj.getFunctionDescriptor().getMethod().getAggregateAttributes();
        if (!aa.allowsDistinct() && obj.isDistinct()) {
            // $NON-NLS-1$ //$NON-NLS-2$
            handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.uda_not_allowed", "DISTINCT", obj), obj);
        }
        if (!aa.allowsOrderBy() && obj.getOrderBy() != null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.uda_not_allowed", "ORDER BY", obj), obj);
        }
        if (aa.isAnalytic() && !obj.isWindowed()) {
            // $NON-NLS-1$
            handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.uda_analytic", obj), obj);
        }
    }
    if (obj.getCondition() != null) {
        Expression condition = obj.getCondition();
        validateNoSubqueriesOrOuterReferences(condition);
    }
    Expression[] aggExps = obj.getArgs();
    for (Expression expression : aggExps) {
        validateNoNestedAggs(expression, obj.isWindowed());
    }
    validateNoNestedAggs(obj.getOrderBy(), false);
    validateNoNestedAggs(obj.getCondition(), false);
    // Verify data type of aggregate expression
    Type aggregateFunction = obj.getAggregateFunction();
    if ((aggregateFunction == Type.SUM || aggregateFunction == Type.AVG) && obj.getType() == null) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0041", new Object[] { aggregateFunction, obj }), obj);
    } else if (obj.getType() != DataTypeManager.DefaultDataClasses.NULL) {
        if (aggregateFunction == Type.XMLAGG && aggExps[0].getType() != DataTypeManager.DefaultDataClasses.XML) {
            // $NON-NLS-1$
            handleValidationError(QueryPlugin.Util.getString("AggregateValidationVisitor.non_xml", new Object[] { aggregateFunction, obj }), obj);
        } else if (obj.isBoolean() && aggExps[0].getType() != DataTypeManager.DefaultDataClasses.BOOLEAN) {
            // $NON-NLS-1$
            handleValidationError(QueryPlugin.Util.getString("AggregateValidationVisitor.non_boolean", new Object[] { aggregateFunction, obj }), obj);
        } else if (aggregateFunction == Type.JSONARRAY_AGG) {
            validateJSONValue(obj, aggExps[0]);
        } else if (obj.getType() == null) {
            // $NON-NLS-1$
            handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.aggregate_type", obj), obj);
        }
    }
    if ((obj.isDistinct() || aggregateFunction == Type.MIN || aggregateFunction == Type.MAX) && DataTypeManager.isNonComparable(DataTypeManager.getDataTypeName(aggExps[0].getType()))) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("AggregateValidationVisitor.non_comparable", new Object[] { aggregateFunction, obj }), obj);
    }
    if (obj.isEnhancedNumeric()) {
        if (!Number.class.isAssignableFrom(aggExps[0].getType())) {
            // $NON-NLS-1$
            handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0041", new Object[] { aggregateFunction, obj }), obj);
        }
        if (obj.isDistinct()) {
            // $NON-NLS-1$
            handleValidationError(QueryPlugin.Util.getString("AggregateValidationVisitor.invalid_distinct", new Object[] { aggregateFunction, obj }), obj);
        }
    }
    if (obj.isDistinct() && obj.getOrderBy() != null) {
        HashSet<Expression> args = new HashSet<Expression>(Arrays.asList(obj.getArgs()));
        for (OrderByItem item : obj.getOrderBy().getOrderByItems()) {
            if (!args.contains(item.getSymbol())) {
                // $NON-NLS-1$
                handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.distinct_orderby_agg", obj), obj);
                break;
            }
        }
    }
    if (obj.getAggregateFunction() != Type.TEXTAGG) {
        return;
    }
    TextLine tl = (TextLine) aggExps[0];
    if (tl.isIncludeHeader()) {
        validateDerivedColumnNames(obj, tl.getExpressions());
    }
    for (DerivedColumn dc : tl.getExpressions()) {
        Expression expression = dc.getExpression();
        if (expression.getType() == DataTypeManager.DefaultDataClasses.OBJECT || expression.getType() == null || expression.getType().isArray() || expression.getType() == DataTypeManager.DefaultDataClasses.VARBINARY || expression.getType() == DataTypeManager.DefaultDataClasses.BLOB) {
            // $NON-NLS-1$
            handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.text_content_type", expression), obj);
        }
    }
    validateTextOptions(obj, tl.getDelimiter(), tl.getQuote(), '\n');
    if (tl.getEncoding() != null) {
        try {
            Charset.forName(tl.getEncoding());
        } catch (IllegalArgumentException e) {
            // $NON-NLS-1$
            handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.invalid_encoding", tl.getEncoding()), obj);
        }
    }
}
Also used : Type(org.teiid.query.sql.symbol.AggregateSymbol.Type) SaxonXQueryExpression(org.teiid.query.xquery.saxon.SaxonXQueryExpression) LanguageObject(org.teiid.query.sql.LanguageObject) AggregateAttributes(org.teiid.metadata.AggregateAttributes) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 7 with AggregateAttributes

use of org.teiid.metadata.AggregateAttributes in project teiid by teiid.

the class OracleExecutionFactory method initCapabilities.

@Override
public void initCapabilities(Connection connection) throws TranslatorException {
    super.initCapabilities(connection);
    if (getVersion().compareTo(ELEVEN_2) >= 0) {
        AggregateAttributes aa = new AggregateAttributes();
        aa.setAllowsOrderBy(true);
        addPushDownFunction(ORACLE, LISTAGG, STRING, STRING, STRING).setAggregateAttributes(aa);
        addPushDownFunction(ORACLE, LISTAGG, STRING, STRING).setAggregateAttributes(aa);
    }
}
Also used : AggregateAttributes(org.teiid.metadata.AggregateAttributes)

Example 8 with AggregateAttributes

use of org.teiid.metadata.AggregateAttributes in project teiid by teiid.

the class FunctionLibrary method getBuiltInAggregateFunctions.

/**
 * Return a list of the most general forms of built-in aggregate functions.
 * <br/>count(*) - is not included
 * <br/>textagg - is not included due to its non standard syntax
 *
 * @param includeAnalytic - true to include analytic functions that must be windowed
 * @return
 */
public List<FunctionMethod> getBuiltInAggregateFunctions(boolean includeAnalytic) {
    ArrayList<FunctionMethod> result = new ArrayList<FunctionMethod>();
    if (this.systemFunctions != null) {
        FunctionDescriptor stExtent = this.systemFunctions.getFunction(SourceSystemFunctions.ST_EXTENT, new Class[] { DataTypeManager.DefaultDataClasses.GEOMETRY });
        result.add(stExtent.getMethod());
    }
    for (Type type : AggregateSymbol.Type.values()) {
        AggregateAttributes aa = new AggregateAttributes();
        String returnType = null;
        String[] argTypes = null;
        aa.setAllowsDistinct(true);
        switch(type) {
            case TEXTAGG:
            case USER_DEFINED:
                continue;
            case DENSE_RANK:
            case RANK:
            case ROW_NUMBER:
                if (!includeAnalytic) {
                    continue;
                }
                aa.setAllowsDistinct(false);
                aa.setAnalytic(true);
                returnType = DataTypeManager.DefaultDataTypes.INTEGER;
                argTypes = new String[] {};
                break;
            case ANY:
            case SOME:
            case EVERY:
                returnType = DataTypeManager.DefaultDataTypes.BOOLEAN;
                argTypes = new String[] { DataTypeManager.DefaultDataTypes.BOOLEAN };
                break;
            case COUNT:
                returnType = DataTypeManager.DefaultDataTypes.INTEGER;
                argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT };
                break;
            case MAX:
            case MIN:
            case AVG:
            case SUM:
                returnType = DataTypeManager.DefaultDataTypes.OBJECT;
                argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT };
                break;
            case STDDEV_POP:
            case STDDEV_SAMP:
            case VAR_POP:
            case VAR_SAMP:
                returnType = DataTypeManager.DefaultDataTypes.DOUBLE;
                argTypes = new String[] { DataTypeManager.DefaultDataTypes.DOUBLE };
                break;
            case STRING_AGG:
                returnType = DataTypeManager.DefaultDataTypes.OBJECT;
                argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT };
                aa.setAllowsOrderBy(true);
                break;
            case ARRAY_AGG:
                returnType = DataTypeManager.DefaultDataTypes.OBJECT;
                argTypes = new String[] { DataTypeManager.getDataTypeName(DataTypeManager.getArrayType(DataTypeManager.DefaultDataClasses.OBJECT)) };
                aa.setAllowsOrderBy(true);
                aa.setAllowsDistinct(false);
                break;
            case JSONARRAY_AGG:
                returnType = DataTypeManager.DefaultDataTypes.CLOB;
                argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT };
                aa.setAllowsOrderBy(true);
                aa.setAllowsDistinct(false);
                break;
            case XMLAGG:
                returnType = DataTypeManager.DefaultDataTypes.XML;
                argTypes = new String[] { DataTypeManager.DefaultDataTypes.XML };
                aa.setAllowsOrderBy(true);
                aa.setAllowsDistinct(false);
                break;
            case FIRST_VALUE:
            case LAST_VALUE:
                if (!includeAnalytic) {
                    continue;
                }
                aa.setAllowsDistinct(false);
                aa.setAnalytic(true);
                returnType = DataTypeManager.DefaultDataTypes.OBJECT;
                argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT };
                break;
            case LEAD:
            case LAG:
                if (!includeAnalytic) {
                    continue;
                }
                aa.setAllowsDistinct(false);
                aa.setAnalytic(true);
                returnType = DataTypeManager.DefaultDataTypes.OBJECT;
                argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.OBJECT };
                break;
        }
        FunctionMethod fm = FunctionMethod.createFunctionMethod(type.name(), type.name(), FunctionCategoryConstants.AGGREGATE, returnType, argTypes);
        fm.setAggregateAttributes(aa);
        result.add(fm);
    }
    return result;
}
Also used : Type(org.teiid.query.sql.symbol.AggregateSymbol.Type) ArrayList(java.util.ArrayList) FunctionMethod(org.teiid.metadata.FunctionMethod) AggregateAttributes(org.teiid.metadata.AggregateAttributes)

Aggregations

AggregateAttributes (org.teiid.metadata.AggregateAttributes)8 FunctionMethod (org.teiid.metadata.FunctionMethod)4 ArrayList (java.util.ArrayList)3 FunctionParameter (org.teiid.metadata.FunctionParameter)2 Type (org.teiid.query.sql.symbol.AggregateSymbol.Type)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Test (org.junit.Test)1 MetadataStore (org.teiid.metadata.MetadataStore)1 Schema (org.teiid.metadata.Schema)1 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)1 LanguageObject (org.teiid.query.sql.LanguageObject)1 Command (org.teiid.query.sql.lang.Command)1 SaxonXQueryExpression (org.teiid.query.xquery.saxon.SaxonXQueryExpression)1 AliasModifier (org.teiid.translator.jdbc.AliasModifier)1