Search in sources :

Example 1 with CompilationException

use of org.apache.asterix.common.exceptions.CompilationException in project asterixdb by apache.

the class SqlppQueryRewriter method inlineDeclaredUdfs.

protected void inlineDeclaredUdfs() throws CompilationException {
    List<FunctionSignature> funIds = new ArrayList<FunctionSignature>();
    for (FunctionDecl fdecl : declaredFunctions) {
        funIds.add(fdecl.getSignature());
    }
    List<FunctionDecl> usedStoredFunctionDecls = new ArrayList<>();
    for (Expression topLevelExpr : topExpr.getDirectlyEnclosedExpressions()) {
        usedStoredFunctionDecls.addAll(FunctionUtil.retrieveUsedStoredFunctions(metadataProvider, topLevelExpr, funIds, null, expr -> getFunctionCalls(expr), func -> functionRepository.getFunctionDecl(func), signature -> FunctionMapUtil.normalizeBuiltinFunctionSignature(signature, false)));
    }
    declaredFunctions.addAll(usedStoredFunctionDecls);
    if (!declaredFunctions.isEmpty()) {
        SqlppInlineUdfsVisitor visitor = new SqlppInlineUdfsVisitor(context, new SqlppFunctionBodyRewriterFactory(), /* the rewriter for function bodies expressions*/
        declaredFunctions, metadataProvider);
        while (topExpr.accept(visitor, declaredFunctions)) {
        // loop until no more changes
        }
    }
    declaredFunctions.removeAll(usedStoredFunctionDecls);
}
Also used : UnnestClause(org.apache.asterix.lang.sqlpp.clause.UnnestClause) OperatorExpressionVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.OperatorExpressionVisitor) JoinClause(org.apache.asterix.lang.sqlpp.clause.JoinClause) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm) VariableCheckAndRewriteVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.VariableCheckAndRewriteVisitor) SqlppGlobalAggregationSugarVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppGlobalAggregationSugarVisitor) InlineColumnAliasVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.InlineColumnAliasVisitor) SelectElement(org.apache.asterix.lang.sqlpp.clause.SelectElement) SelectRegular(org.apache.asterix.lang.sqlpp.clause.SelectRegular) FunctionParser(org.apache.asterix.lang.sqlpp.parser.FunctionParser) NestClause(org.apache.asterix.lang.sqlpp.clause.NestClause) ArrayList(java.util.ArrayList) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) HavingClause(org.apache.asterix.lang.sqlpp.clause.HavingClause) IReturningStatement(org.apache.asterix.lang.common.base.IReturningStatement) ISqlppVisitor(org.apache.asterix.lang.sqlpp.visitor.base.ISqlppVisitor) FunctionUtil(org.apache.asterix.lang.common.util.FunctionUtil) LetClause(org.apache.asterix.lang.common.clause.LetClause) SubstituteGroupbyExpressionWithVariableVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SubstituteGroupbyExpressionWithVariableVisitor) SqlppListInputFunctionRewriteVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppListInputFunctionRewriteVisitor) SetOperationRight(org.apache.asterix.lang.sqlpp.struct.SetOperationRight) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) CompilationException(org.apache.asterix.common.exceptions.CompilationException) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) SqlppInlineUdfsVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppInlineUdfsVisitor) SqlppGroupByVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppGroupByVisitor) Expression(org.apache.asterix.lang.common.base.Expression) Set(java.util.Set) LangRewritingContext(org.apache.asterix.lang.common.rewrites.LangRewritingContext) Projection(org.apache.asterix.lang.sqlpp.clause.Projection) AbstractBinaryCorrelateClause(org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause) SqlppParserFactory(org.apache.asterix.lang.sqlpp.parser.SqlppParserFactory) IQueryRewriter(org.apache.asterix.lang.common.base.IQueryRewriter) SqlppBuiltinFunctionRewriteVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppBuiltinFunctionRewriteVisitor) GatherFunctionCallsVisitor(org.apache.asterix.lang.common.visitor.GatherFunctionCallsVisitor) List(java.util.List) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) GenerateColumnNameVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.GenerateColumnNameVisitor) FunctionMapUtil(org.apache.asterix.lang.sqlpp.util.FunctionMapUtil) InlineWithExpressionVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.InlineWithExpressionVisitor) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) IndependentSubquery(org.apache.asterix.lang.sqlpp.expression.IndependentSubquery) SetOperationVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SetOperationVisitor) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) ArrayList(java.util.ArrayList) SqlppInlineUdfsVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppInlineUdfsVisitor) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl)

Example 2 with CompilationException

use of org.apache.asterix.common.exceptions.CompilationException in project asterixdb by apache.

the class CloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FunctionDecl fd, VariableSubstitutionEnvironment env) throws CompilationException {
    List<VarIdentifier> newList = new ArrayList<>(fd.getParamList().size());
    for (VarIdentifier vi : fd.getParamList()) {
        VariableExpr varExpr = new VariableExpr(vi);
        if (!env.constainsOldVar(varExpr)) {
            throw new CompilationException("Parameter " + vi + " does not appear in the substitution list.");
        }
        Expression newExpr = env.findSubstitution(varExpr);
        if (newExpr.getKind() != Kind.VARIABLE_EXPRESSION) {
            throw new CompilationException("Parameter " + vi + " cannot be substituted by a non-variable expression.");
        }
        newList.add(((VariableExpr) newExpr).getVar());
    }
    Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = fd.getFuncBody().accept(this, env);
    FunctionDecl newF = new FunctionDecl(fd.getSignature(), newList, (Expression) p1.first);
    return new Pair<>(newF, env);
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) ArrayList(java.util.ArrayList) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair)

Example 3 with CompilationException

use of org.apache.asterix.common.exceptions.CompilationException in project asterixdb by apache.

the class InvertedIndexResourceFactoryProvider method getTokenizerFactory.

private static IBinaryTokenizerFactory getTokenizerFactory(Dataset dataset, Index index, ARecordType recordType, ARecordType metaType) throws AlgebricksException {
    int numPrimaryKeys = dataset.getPrimaryKeys().size();
    int numSecondaryKeys = index.getKeyFieldNames().size();
    IndexType indexType = index.getIndexType();
    // Sanity checks.
    if (numPrimaryKeys > 1) {
        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_INDEX_FOR_DATASET_WITH_COMPOSITE_PRIMARY_INDEX, indexType, RecordUtil.toFullyQualifiedName(dataset.getDataverseName(), dataset.getDatasetName()));
    }
    if (numSecondaryKeys > 1) {
        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_INDEX_NUM_OF_FIELD, numSecondaryKeys, indexType, 1);
    }
    ARecordType sourceType;
    List<Integer> keySourceIndicators = index.getKeyFieldSourceIndicators();
    if (keySourceIndicators == null || keySourceIndicators.get(0) == 0) {
        sourceType = recordType;
    } else {
        sourceType = metaType;
    }
    Pair<IAType, Boolean> keyTypePair = Index.getNonNullableOpenFieldType(index.getKeyFieldTypes().get(0), index.getKeyFieldNames().get(0), sourceType);
    IAType secondaryKeyType = keyTypePair.first;
    // and add the choice to the index metadata.
    return NonTaggedFormatUtil.getBinaryTokenizerFactory(secondaryKeyType.getTypeTag(), indexType, index.getGramLength());
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) IndexType(org.apache.asterix.common.config.DatasetConfig.IndexType) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 4 with CompilationException

use of org.apache.asterix.common.exceptions.CompilationException in project asterixdb by apache.

the class InvertedIndexResourceFactoryProvider method getTokenTypeTraits.

private static ITypeTraits[] getTokenTypeTraits(Dataset dataset, Index index, ARecordType recordType, ARecordType metaType) throws AlgebricksException {
    int numPrimaryKeys = dataset.getPrimaryKeys().size();
    int numSecondaryKeys = index.getKeyFieldNames().size();
    IndexType indexType = index.getIndexType();
    // Sanity checks.
    if (numPrimaryKeys > 1) {
        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_INDEX_FOR_DATASET_WITH_COMPOSITE_PRIMARY_INDEX, indexType, RecordUtil.toFullyQualifiedName(dataset.getDataverseName(), dataset.getDatasetName()));
    }
    if (numSecondaryKeys > 1) {
        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_INDEX_NUM_OF_FIELD, numSecondaryKeys, indexType, 1);
    }
    boolean isPartitioned = indexType == IndexType.LENGTH_PARTITIONED_WORD_INVIX || indexType == IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
    ARecordType sourceType;
    List<Integer> keySourceIndicators = index.getKeyFieldSourceIndicators();
    if (keySourceIndicators == null || keySourceIndicators.get(0) == 0) {
        sourceType = recordType;
    } else {
        sourceType = metaType;
    }
    Pair<IAType, Boolean> keyTypePair = Index.getNonNullableOpenFieldType(index.getKeyFieldTypes().get(0), index.getKeyFieldNames().get(0), sourceType);
    IAType secondaryKeyType = keyTypePair.first;
    int numTokenFields = (!isPartitioned) ? numSecondaryKeys : numSecondaryKeys + 1;
    ITypeTraits[] tokenTypeTraits = new ITypeTraits[numTokenFields];
    tokenTypeTraits[0] = NonTaggedFormatUtil.getTokenTypeTrait(secondaryKeyType);
    if (isPartitioned) {
        // The partitioning field is hardcoded to be a short *without* an Asterix type tag.
        tokenTypeTraits[1] = ShortPointable.TYPE_TRAITS;
    }
    return tokenTypeTraits;
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) ITypeTraits(org.apache.hyracks.api.dataflow.value.ITypeTraits) IndexType(org.apache.asterix.common.config.DatasetConfig.IndexType) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 5 with CompilationException

use of org.apache.asterix.common.exceptions.CompilationException in project asterixdb by apache.

the class BTreeAccessMethod method getNewConditionExprs.

private void getNewConditionExprs(Mutable<ILogicalExpression> conditionRef, Set<ILogicalExpression> replacedFuncExprs, List<Mutable<ILogicalExpression>> remainingFuncExprs) throws CompilationException {
    remainingFuncExprs.clear();
    if (replacedFuncExprs.isEmpty()) {
        return;
    }
    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) conditionRef.getValue();
    if (replacedFuncExprs.size() == 1) {
        Iterator<ILogicalExpression> it = replacedFuncExprs.iterator();
        if (!it.hasNext()) {
            return;
        }
        if (funcExpr == it.next()) {
            // There are no remaining function exprs.
            return;
        }
    }
    // The original select cond must be an AND. Check it just to be sure.
    if (funcExpr.getFunctionIdentifier() != AlgebricksBuiltinFunctions.AND) {
        throw new CompilationException(ErrorCode.COMPILATION_FUNC_EXPRESSION_CANNOT_UTILIZE_INDEX, funcExpr.toString());
    }
    // Clean the conjuncts.
    for (Mutable<ILogicalExpression> arg : funcExpr.getArguments()) {
        ILogicalExpression argExpr = arg.getValue();
        if (argExpr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
            continue;
        }
        // plan, then add it to the list of remaining function expressions.
        if (!replacedFuncExprs.contains(argExpr)) {
            remainingFuncExprs.add(arg);
        }
    }
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)

Aggregations

CompilationException (org.apache.asterix.common.exceptions.CompilationException)44 ArrayList (java.util.ArrayList)13 IAType (org.apache.asterix.om.types.IAType)12 ARecordType (org.apache.asterix.om.types.ARecordType)9 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)9 AsterixException (org.apache.asterix.common.exceptions.AsterixException)8 IOException (java.io.IOException)7 List (java.util.List)7 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)7 MetadataException (org.apache.asterix.metadata.MetadataException)7 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)7 ITypeTraits (org.apache.hyracks.api.dataflow.value.ITypeTraits)7 RemoteException (java.rmi.RemoteException)6 ACIDException (org.apache.asterix.common.exceptions.ACIDException)6 Expression (org.apache.asterix.lang.common.base.Expression)6 Pair (org.apache.hyracks.algebricks.common.utils.Pair)6 IBinaryComparatorFactory (org.apache.hyracks.api.dataflow.value.IBinaryComparatorFactory)6 ActiveLifecycleListener (org.apache.asterix.active.ActiveLifecycleListener)5 IDataset (org.apache.asterix.common.metadata.IDataset)5 Dataset (org.apache.asterix.metadata.entities.Dataset)5