Search in sources :

Example 11 with IParser

use of org.apache.asterix.lang.common.base.IParser in project asterixdb by apache.

the class ParserTest method testLineEnding.

protected void testLineEnding(String query) throws Exception {
    IParserFactory factory = new SqlppParserFactory();
    IParser parser = factory.createParser(query);
    try {
        parser.parse();
    } catch (CompilationException e) {
        if (!e.getMessage().contains("Syntax error: In line 3")) {
            throw new Exception("Unexpected error", e);
        }
    }
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) IParserFactory(org.apache.asterix.lang.common.base.IParserFactory) CompilationException(org.apache.asterix.common.exceptions.CompilationException) IParser(org.apache.asterix.lang.common.base.IParser)

Example 12 with IParser

use of org.apache.asterix.lang.common.base.IParser in project asterixdb by apache.

the class AdmDataGen method init.

public void init() throws IOException, ParseException, AsterixException, ACIDException, MetadataException, AlgebricksException {
    FileReader aql = new FileReader(schemaFile);
    IParser parser = parserFactory.createParser(aql);
    List<Statement> statements = parser.parse();
    aql.close();
    // TODO: Need to fix how to use transactions here.
    MetadataTransactionContext mdTxnCtx = new MetadataTransactionContext(new JobId(-1));
    ADGenDmlTranslator dmlt = new ADGenDmlTranslator(mdTxnCtx, statements);
    dmlt.translate();
    typeMap = dmlt.getTypeMap();
    typeAnnotMap = dmlt.getTypeDataGenMap();
    dgCtx = new DataGeneratorContext();
}
Also used : Statement(org.apache.asterix.lang.common.base.Statement) ADGenDmlTranslator(org.apache.asterix.tools.translator.ADGenDmlTranslator) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) FileReader(java.io.FileReader) JobId(org.apache.asterix.common.transactions.JobId) IParser(org.apache.asterix.lang.common.base.IParser)

Example 13 with IParser

use of org.apache.asterix.lang.common.base.IParser 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 14 with IParser

use of org.apache.asterix.lang.common.base.IParser in project asterixdb by apache.

the class RangeMapBuilder method parseHint.

public static IRangeMap parseHint(Object hint) throws CompilationException {
    ArrayBackedValueStorage abvs = new ArrayBackedValueStorage();
    DataOutput out = abvs.getDataOutput();
    ;
    abvs.reset();
    IParser parser = parserFactory.createParser((String) hint);
    List<Statement> hintStatements = parser.parse();
    if (hintStatements.size() != 1) {
        throw new CompilationException("Only one range statement is allowed for the range hint.");
    }
    // Translate the query into a Range Map
    if (hintStatements.get(0).getKind() != Statement.Kind.QUERY) {
        throw new CompilationException("Not a proper query for the range hint.");
    }
    Query q = (Query) hintStatements.get(0);
    if (q.getBody().getKind() != Kind.LIST_CONSTRUCTOR_EXPRESSION) {
        throw new CompilationException("The range hint must be a list.");
    }
    List<Expression> el = ((ListConstructor) q.getBody()).getExprList();
    int[] offsets = new int[el.size()];
    // Loop over list of literals
    for (int i = 0; i < el.size(); ++i) {
        Expression item = el.get(i);
        if (item.getKind() == Kind.LITERAL_EXPRESSION) {
            parseLiteralToBytes(item, out);
            offsets[i] = abvs.getLength();
        }
    // TODO Add support for composite fields.
    }
    return new RangeMap(1, abvs.getByteArray(), offsets);
}
Also used : DataOutput(java.io.DataOutput) CompilationException(org.apache.asterix.common.exceptions.CompilationException) Query(org.apache.asterix.lang.common.statement.Query) Statement(org.apache.asterix.lang.common.base.Statement) ListConstructor(org.apache.asterix.lang.common.expression.ListConstructor) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IRangeMap(org.apache.hyracks.dataflow.common.data.partition.range.IRangeMap) RangeMap(org.apache.hyracks.dataflow.common.data.partition.range.RangeMap) Expression(org.apache.asterix.lang.common.base.Expression) IParser(org.apache.asterix.lang.common.base.IParser)

Aggregations

IParser (org.apache.asterix.lang.common.base.IParser)14 Statement (org.apache.asterix.lang.common.base.Statement)12 IStatementExecutor (org.apache.asterix.translator.IStatementExecutor)5 PrintWriter (java.io.PrintWriter)4 AsterixException (org.apache.asterix.common.exceptions.AsterixException)4 CompilationException (org.apache.asterix.common.exceptions.CompilationException)3 TokenMgrError (org.apache.asterix.lang.aql.parser.TokenMgrError)3 FunctionDecl (org.apache.asterix.lang.common.statement.FunctionDecl)3 Query (org.apache.asterix.lang.common.statement.Query)3 SessionOutput (org.apache.asterix.translator.SessionOutput)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 IClusterManagementWork (org.apache.asterix.common.api.IClusterManagementWork)2 ILangCompilationProvider (org.apache.asterix.compiler.provider.ILangCompilationProvider)2 IParserFactory (org.apache.asterix.lang.common.base.IParserFactory)2