Search in sources :

Example 31 with CompilationException

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

the class RecordMergeTypeComputer method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
    String funcName = f.getFunctionIdentifier().getName();
    IAType t0 = (IAType) env.getType(f.getArguments().get(0).getValue());
    IAType t1 = (IAType) env.getType(f.getArguments().get(1).getValue());
    boolean unknownable = TypeHelper.canBeUnknown(t0) || TypeHelper.canBeUnknown(t1);
    ARecordType recType0 = TypeComputeUtils.extractRecordType(t0);
    if (recType0 == null) {
        throw new TypeMismatchException(funcName, 0, t0.getTypeTag(), ATypeTag.OBJECT);
    }
    ARecordType recType1 = TypeComputeUtils.extractRecordType(t1);
    if (recType1 == null) {
        throw new TypeMismatchException(funcName, 1, t1.getTypeTag(), ATypeTag.OBJECT);
    }
    List<String> resultFieldNames = new ArrayList<>();
    for (String fieldName : recType0.getFieldNames()) {
        resultFieldNames.add(fieldName);
    }
    Collections.sort(resultFieldNames);
    List<IAType> resultFieldTypes = new ArrayList<>();
    for (String fieldName : resultFieldNames) {
        if (recType0.getFieldType(fieldName).getTypeTag() == ATypeTag.OBJECT) {
            ARecordType nestedType = (ARecordType) recType0.getFieldType(fieldName);
            //Deep Copy prevents altering of input types
            resultFieldTypes.add(nestedType.deepCopy(nestedType));
        } else {
            resultFieldTypes.add(recType0.getFieldType(fieldName));
        }
    }
    List<String> additionalFieldNames = new ArrayList<>();
    List<IAType> additionalFieldTypes = new ArrayList<>();
    String[] fieldNames = recType1.getFieldNames();
    IAType[] fieldTypes = recType1.getFieldTypes();
    for (int i = 0; i < fieldNames.length; ++i) {
        int pos = Collections.binarySearch(resultFieldNames, fieldNames[i]);
        if (pos >= 0) {
            IAType resultFieldType = resultFieldTypes.get(pos);
            if (resultFieldType.getTypeTag() != fieldTypes[i].getTypeTag()) {
                throw new CompilationException(ErrorCode.COMPILATION_DUPLICATE_FIELD_NAME, fieldNames[i]);
            }
            // Assuming fieldTypes[i].getTypeTag() = resultFieldType.getTypeTag()
            if (fieldTypes[i].getTypeTag() == ATypeTag.OBJECT) {
                resultFieldTypes.set(pos, mergedNestedType(fieldNames[i], fieldTypes[i], resultFieldType));
            }
        } else {
            additionalFieldNames.add(fieldNames[i]);
            additionalFieldTypes.add(fieldTypes[i]);
        }
    }
    resultFieldNames.addAll(additionalFieldNames);
    resultFieldTypes.addAll(additionalFieldTypes);
    String resultTypeName = "merged(" + recType0.getTypeName() + ", " + recType1.getTypeName() + ")";
    boolean isOpen = recType0.isOpen() || recType1.isOpen();
    IAType resultType = new ARecordType(resultTypeName, resultFieldNames.toArray(new String[] {}), resultFieldTypes.toArray(new IAType[] {}), isOpen);
    if (unknownable) {
        resultType = AUnionType.createUnknownableType(resultType);
    }
    return resultType;
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) TypeMismatchException(org.apache.asterix.om.exceptions.TypeMismatchException) ArrayList(java.util.ArrayList) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 32 with CompilationException

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

the class SwitchCaseComputer method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) expression;
    String funcName = fce.getFunctionIdentifier().getName();
    int argNumber = fce.getArguments().size();
    if (argNumber < 3) {
        throw new CompilationException(ErrorCode.COMPILATION_INVALID_PARAMETER_NUMBER, funcName, argNumber);
    }
    int argSize = fce.getArguments().size();
    List<IAType> types = new ArrayList<>();
    // The last return expression is from the ELSE branch and it is optional.
    for (int argIndex = 2; argIndex < argSize; argIndex += (argIndex + 2 == argSize) ? 1 : 2) {
        IAType type = (IAType) env.getType(fce.getArguments().get(argIndex).getValue());
        types.add(type);
    }
    return TypeResolverUtil.resolve(types);
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) IAType(org.apache.asterix.om.types.IAType)

Example 33 with CompilationException

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

the class AqlQueryRewriter method inlineDeclaredUdfs.

private void inlineDeclaredUdfs() throws CompilationException {
    if (topStatement == null) {
        return;
    }
    List<FunctionSignature> funIds = new ArrayList<FunctionSignature>();
    for (FunctionDecl fdecl : declaredFunctions) {
        funIds.add(fdecl.getSignature());
    }
    List<FunctionDecl> storedFunctionDecls = new ArrayList<>();
    for (Expression topLevelExpr : topStatement.getDirectlyEnclosedExpressions()) {
        storedFunctionDecls.addAll(FunctionUtil.retrieveUsedStoredFunctions(metadataProvider, topLevelExpr, funIds, null, expr -> getFunctionCalls(expr), func -> functionParser.getFunctionDecl(func), signature -> CommonFunctionMapUtil.normalizeBuiltinFunctionSignature(signature)));
        declaredFunctions.addAll(storedFunctionDecls);
    }
    if (!declaredFunctions.isEmpty()) {
        AQLInlineUdfsVisitor visitor = new AQLInlineUdfsVisitor(context, new AQLRewriterFactory(), declaredFunctions, metadataProvider);
        while (topStatement.accept(visitor, declaredFunctions)) {
        // loop until no more changes
        }
    }
    declaredFunctions.removeAll(storedFunctionDecls);
}
Also used : IAQLVisitor(org.apache.asterix.lang.aql.visitor.base.IAQLVisitor) ForClause(org.apache.asterix.lang.aql.clause.ForClause) DistinctClause(org.apache.asterix.lang.aql.clause.DistinctClause) ArrayList(java.util.ArrayList) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) IReturningStatement(org.apache.asterix.lang.common.base.IReturningStatement) FunctionUtil(org.apache.asterix.lang.common.util.FunctionUtil) LetClause(org.apache.asterix.lang.common.clause.LetClause) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Kind(org.apache.asterix.lang.common.base.Expression.Kind) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) CompilationException(org.apache.asterix.common.exceptions.CompilationException) AQLParserFactory(org.apache.asterix.lang.aql.parser.AQLParserFactory) Clause(org.apache.asterix.lang.common.base.Clause) Expression(org.apache.asterix.lang.common.base.Expression) Set(java.util.Set) LangRewritingContext(org.apache.asterix.lang.common.rewrites.LangRewritingContext) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) CommonFunctionMapUtil(org.apache.asterix.lang.common.util.CommonFunctionMapUtil) IQueryRewriter(org.apache.asterix.lang.common.base.IQueryRewriter) GatherFunctionCallsVisitor(org.apache.asterix.lang.common.visitor.GatherFunctionCallsVisitor) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) List(java.util.List) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) AqlBuiltinFunctionRewriteVisitor(org.apache.asterix.lang.aql.rewrites.visitor.AqlBuiltinFunctionRewriteVisitor) AQLInlineUdfsVisitor(org.apache.asterix.lang.aql.visitor.AQLInlineUdfsVisitor) UnionExpr(org.apache.asterix.lang.aql.expression.UnionExpr) FunctionParser(org.apache.asterix.lang.aql.parser.FunctionParser) AQLInlineUdfsVisitor(org.apache.asterix.lang.aql.visitor.AQLInlineUdfsVisitor) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) ArrayList(java.util.ArrayList) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl)

Example 34 with CompilationException

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

the class SubscribeFeedStatement method initialize.

public void initialize(MetadataTransactionContext mdTxnCtx) throws MetadataException {
    this.query = new Query(false);
    EntityId sourceFeedId = connectionRequest.getReceivingFeedId();
    Feed subscriberFeed = MetadataManager.INSTANCE.getFeed(mdTxnCtx, connectionRequest.getReceivingFeedId().getDataverse(), connectionRequest.getReceivingFeedId().getEntityName());
    if (subscriberFeed == null) {
        throw new IllegalStateException(" Subscriber feed " + subscriberFeed + " not found.");
    }
    String feedOutputType = getOutputType(mdTxnCtx);
    StringBuilder builder = new StringBuilder();
    builder.append("use dataverse " + sourceFeedId.getDataverse() + ";\n");
    builder.append("set" + " " + FunctionUtil.IMPORT_PRIVATE_FUNCTIONS + " " + "'" + Boolean.TRUE + "'" + ";\n");
    builder.append("set" + " " + FeedActivityDetails.FEED_POLICY_NAME + " " + "'" + connectionRequest.getPolicy() + "'" + ";\n");
    builder.append("insert into dataset " + connectionRequest.getTargetDataset() + " ");
    builder.append(" (" + " for $x in feed-collect ('" + sourceFeedId.getDataverse() + "'" + "," + "'" + sourceFeedId.getEntityName() + "'" + "," + "'" + connectionRequest.getReceivingFeedId().getEntityName() + "'" + "," + "'" + connectionRequest.getSubscriptionLocation().name() + "'" + "," + "'" + connectionRequest.getTargetDataset() + "'" + "," + "'" + feedOutputType + "'" + ")");
    List<FunctionSignature> functionsToApply = connectionRequest.getFunctionsToApply();
    if ((functionsToApply != null) && functionsToApply.isEmpty()) {
        builder.append(" return $x");
    } else {
        Function function;
        String rValueName = "x";
        String lValueName = "y";
        int variableIndex = 0;
        for (FunctionSignature appliedFunction : functionsToApply) {
            function = MetadataManager.INSTANCE.getFunction(mdTxnCtx, appliedFunction);
            variableIndex++;
            switch(function.getLanguage().toUpperCase()) {
                case Function.LANGUAGE_AQL:
                    builder.append(" let " + "$" + lValueName + variableIndex + ":=" + function.getName() + "(" + "$" + rValueName + ")");
                    rValueName = lValueName + variableIndex;
                    break;
                case Function.LANGUAGE_JAVA:
                    builder.append(" let " + "$" + lValueName + variableIndex + ":=" + function.getName() + "(" + "$" + rValueName + ")");
                    rValueName = lValueName + variableIndex;
                    break;
            }
            builder.append("\n");
        }
        builder.append("return $" + lValueName + variableIndex);
    }
    builder.append(")");
    builder.append(";");
    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("Connect feed statement translated to\n" + builder.toString());
    }
    IParser parser = parserFactory.createParser(new StringReader(builder.toString()));
    List<Statement> statements;
    try {
        statements = parser.parse();
        query = ((InsertStatement) statements.get(INSERT_STATEMENT_POS)).getQuery();
    } catch (CompilationException pe) {
        throw new MetadataException(pe);
    }
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) Query(org.apache.asterix.lang.common.statement.Query) InsertStatement(org.apache.asterix.lang.common.statement.InsertStatement) Statement(org.apache.asterix.lang.common.base.Statement) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) MetadataException(org.apache.asterix.metadata.MetadataException) EntityId(org.apache.asterix.active.EntityId) Function(org.apache.asterix.metadata.entities.Function) StringReader(java.io.StringReader) Feed(org.apache.asterix.metadata.entities.Feed) IParser(org.apache.asterix.lang.common.base.IParser)

Example 35 with CompilationException

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

the class RangeMapBuilder method parseLiteralToBytes.

@SuppressWarnings("unchecked")
private static void parseLiteralToBytes(Expression item, DataOutput out) throws CompilationException {
    AMutableDouble aDouble = new AMutableDouble(0);
    AMutableFloat aFloat = new AMutableFloat(0);
    AMutableInt64 aInt64 = new AMutableInt64(0);
    AMutableInt32 aInt32 = new AMutableInt32(0);
    AMutableString aString = new AMutableString("");
    @SuppressWarnings("rawtypes") ISerializerDeserializer serde;
    Literal l = ((LiteralExpr) item).getValue();
    try {
        switch(l.getLiteralType()) {
            case DOUBLE:
                DoubleLiteral dl = (DoubleLiteral) l;
                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
                aDouble.setValue(dl.getValue());
                serde.serialize(aDouble, out);
                break;
            case FLOAT:
                FloatLiteral fl = (FloatLiteral) l;
                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AFLOAT);
                aFloat.setValue(fl.getValue());
                serde.serialize(aFloat, out);
                break;
            case INTEGER:
                IntegerLiteral il = (IntegerLiteral) l;
                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT32);
                aInt32.setValue(il.getValue());
                serde.serialize(aInt32, out);
                break;
            case LONG:
                LongIntegerLiteral lil = (LongIntegerLiteral) l;
                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
                aInt64.setValue(lil.getValue());
                serde.serialize(aInt64, out);
                break;
            case STRING:
                StringLiteral sl = (StringLiteral) l;
                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING);
                aString.setValue(sl.getValue());
                serde.serialize(aString, out);
                break;
            default:
                throw new NotImplementedException("The range map builder has not been implemented for " + item.getKind() + " type of expressions.");
        }
    } catch (HyracksDataException e) {
        throw new CompilationException(e.getMessage());
    }
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) FloatLiteral(org.apache.asterix.lang.common.literal.FloatLiteral) NotImplementedException(org.apache.hyracks.algebricks.common.exceptions.NotImplementedException) AMutableString(org.apache.asterix.om.base.AMutableString) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) LongIntegerLiteral(org.apache.asterix.lang.common.literal.LongIntegerLiteral) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) Literal(org.apache.asterix.lang.common.base.Literal) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) IntegerLiteral(org.apache.asterix.lang.common.literal.IntegerLiteral) FloatLiteral(org.apache.asterix.lang.common.literal.FloatLiteral) DoubleLiteral(org.apache.asterix.lang.common.literal.DoubleLiteral) LongIntegerLiteral(org.apache.asterix.lang.common.literal.LongIntegerLiteral) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) DoubleLiteral(org.apache.asterix.lang.common.literal.DoubleLiteral) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) AMutableInt32(org.apache.asterix.om.base.AMutableInt32) AMutableFloat(org.apache.asterix.om.base.AMutableFloat) IntegerLiteral(org.apache.asterix.lang.common.literal.IntegerLiteral) LongIntegerLiteral(org.apache.asterix.lang.common.literal.LongIntegerLiteral)

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