Search in sources :

Example 1 with AlgebricksAppendable

use of org.apache.hyracks.algebricks.core.algebra.prettyprint.AlgebricksAppendable in project asterixdb by apache.

the class HeuristicOptimizer method logPlanAt.

private void logPlanAt(String name, Level lvl) throws AlgebricksException {
    if (AlgebricksConfig.ALGEBRICKS_LOGGER.isLoggable(lvl)) {
        final LogicalOperatorPrettyPrintVisitor pvisitor = context.getPrettyPrintVisitor();
        pvisitor.reset(new AlgebricksAppendable());
        PlanPrettyPrinter.printPlan(plan, pvisitor, 0);
        AlgebricksConfig.ALGEBRICKS_LOGGER.info(name + ":\n" + pvisitor.get().toString());
    }
}
Also used : AlgebricksAppendable(org.apache.hyracks.algebricks.core.algebra.prettyprint.AlgebricksAppendable) LogicalOperatorPrettyPrintVisitor(org.apache.hyracks.algebricks.core.algebra.prettyprint.LogicalOperatorPrettyPrintVisitor)

Example 2 with AlgebricksAppendable

use of org.apache.hyracks.algebricks.core.algebra.prettyprint.AlgebricksAppendable in project asterixdb by apache.

the class APIFramework method compileQuery.

public JobSpecification compileQuery(IClusterInfoCollector clusterInfoCollector, MetadataProvider metadataProvider, Query rwQ, int varCounter, String outputDatasetName, SessionOutput output, ICompiledDmlStatement statement) throws AlgebricksException, RemoteException, ACIDException {
    SessionConfig conf = output.config();
    if (!conf.is(SessionConfig.FORMAT_ONLY_PHYSICAL_OPS) && conf.is(SessionConfig.OOB_REWRITTEN_EXPR_TREE)) {
        output.out().println();
        printPlanPrefix(output, "Rewritten expression tree");
        if (rwQ != null) {
            rwQ.accept(astPrintVisitorFactory.createLangVisitor(output.out()), 0);
        }
        printPlanPostfix(output);
    }
    org.apache.asterix.common.transactions.JobId asterixJobId = JobIdFactory.generateJobId();
    metadataProvider.setJobId(asterixJobId);
    ILangExpressionToPlanTranslator t = translatorFactory.createExpressionToPlanTranslator(metadataProvider, varCounter);
    ILogicalPlan plan;
    // statement = null when it's a query
    if (statement == null || statement.getKind() != Statement.Kind.LOAD) {
        plan = t.translate(rwQ, outputDatasetName, statement);
    } else {
        plan = t.translateLoad(statement);
    }
    if (!conf.is(SessionConfig.FORMAT_ONLY_PHYSICAL_OPS) && conf.is(SessionConfig.OOB_LOGICAL_PLAN)) {
        output.out().println();
        printPlanPrefix(output, "Logical plan");
        if (rwQ != null || (statement != null && statement.getKind() == Statement.Kind.LOAD)) {
            LogicalOperatorPrettyPrintVisitor pvisitor = new LogicalOperatorPrettyPrintVisitor(output.out());
            PlanPrettyPrinter.printPlan(plan, pvisitor, 0);
        }
        printPlanPostfix(output);
    }
    CompilerProperties compilerProperties = metadataProvider.getApplicationContext().getCompilerProperties();
    int frameSize = compilerProperties.getFrameSize();
    Map<String, String> querySpecificConfig = metadataProvider.getConfig();
    // Validates the user-overridden query parameters.
    validateConfig(querySpecificConfig);
    int sortFrameLimit = getFrameLimit(CompilerProperties.COMPILER_SORTMEMORY_KEY, querySpecificConfig.get(CompilerProperties.COMPILER_SORTMEMORY_KEY), compilerProperties.getSortMemorySize(), frameSize, MIN_FRAME_LIMIT_FOR_SORT);
    int groupFrameLimit = getFrameLimit(CompilerProperties.COMPILER_GROUPMEMORY_KEY, querySpecificConfig.get(CompilerProperties.COMPILER_GROUPMEMORY_KEY), compilerProperties.getGroupMemorySize(), frameSize, MIN_FRAME_LIMIT_FOR_GROUP_BY);
    int joinFrameLimit = getFrameLimit(CompilerProperties.COMPILER_JOINMEMORY_KEY, querySpecificConfig.get(CompilerProperties.COMPILER_JOINMEMORY_KEY), compilerProperties.getJoinMemorySize(), frameSize, MIN_FRAME_LIMIT_FOR_JOIN);
    OptimizationConfUtil.getPhysicalOptimizationConfig().setFrameSize(frameSize);
    OptimizationConfUtil.getPhysicalOptimizationConfig().setMaxFramesExternalSort(sortFrameLimit);
    OptimizationConfUtil.getPhysicalOptimizationConfig().setMaxFramesExternalGroupBy(groupFrameLimit);
    OptimizationConfUtil.getPhysicalOptimizationConfig().setMaxFramesForJoin(joinFrameLimit);
    HeuristicCompilerFactoryBuilder builder = new HeuristicCompilerFactoryBuilder(OptimizationContextFactory.INSTANCE);
    builder.setPhysicalOptimizationConfig(OptimizationConfUtil.getPhysicalOptimizationConfig());
    builder.setLogicalRewrites(ruleSetFactory.getLogicalRewrites(metadataProvider.getApplicationContext()));
    builder.setPhysicalRewrites(ruleSetFactory.getPhysicalRewrites(metadataProvider.getApplicationContext()));
    IDataFormat format = metadataProvider.getFormat();
    ICompilerFactory compilerFactory = builder.create();
    builder.setExpressionEvalSizeComputer(format.getExpressionEvalSizeComputer());
    builder.setIMergeAggregationExpressionFactory(new MergeAggregationExpressionFactory());
    builder.setPartialAggregationTypeComputer(new PartialAggregationTypeComputer());
    builder.setExpressionTypeComputer(ExpressionTypeComputer.INSTANCE);
    builder.setMissableTypeComputer(MissableTypeComputer.INSTANCE);
    builder.setConflictingTypeResolver(ConflictingTypeResolver.INSTANCE);
    int parallelism = getParallelism(querySpecificConfig.get(CompilerProperties.COMPILER_PARALLELISM_KEY), compilerProperties.getParallelism());
    AlgebricksAbsolutePartitionConstraint computationLocations = chooseLocations(clusterInfoCollector, parallelism, metadataProvider.getClusterLocations());
    builder.setClusterLocations(computationLocations);
    ICompiler compiler = compilerFactory.createCompiler(plan, metadataProvider, t.getVarCounter());
    if (conf.isOptimize()) {
        compiler.optimize();
        if (conf.is(SessionConfig.OOB_OPTIMIZED_LOGICAL_PLAN)) {
            if (conf.is(SessionConfig.FORMAT_ONLY_PHYSICAL_OPS)) {
                // For Optimizer tests.
                AlgebricksAppendable buffer = new AlgebricksAppendable(output.out());
                PlanPrettyPrinter.printPhysicalOps(plan, buffer, 0);
            } else {
                printPlanPrefix(output, "Optimized logical plan");
                if (rwQ != null || (statement != null && statement.getKind() == Statement.Kind.LOAD)) {
                    LogicalOperatorPrettyPrintVisitor pvisitor = new LogicalOperatorPrettyPrintVisitor(output.out());
                    PlanPrettyPrinter.printPlan(plan, pvisitor, 0);
                }
                printPlanPostfix(output);
            }
        }
    }
    if (rwQ != null && rwQ.isExplain()) {
        try {
            LogicalOperatorPrettyPrintVisitor pvisitor = new LogicalOperatorPrettyPrintVisitor();
            PlanPrettyPrinter.printPlan(plan, pvisitor, 0);
            ResultUtil.printResults(metadataProvider.getApplicationContext(), pvisitor.get().toString(), output, new Stats(), null);
            return null;
        } catch (IOException e) {
            throw new AlgebricksException(e);
        }
    }
    if (!conf.isGenerateJobSpec()) {
        return null;
    }
    builder.setBinaryBooleanInspectorFactory(format.getBinaryBooleanInspectorFactory());
    builder.setBinaryIntegerInspectorFactory(format.getBinaryIntegerInspectorFactory());
    builder.setComparatorFactoryProvider(format.getBinaryComparatorFactoryProvider());
    builder.setExpressionRuntimeProvider(new ExpressionRuntimeProvider(QueryLogicalExpressionJobGen.INSTANCE));
    builder.setHashFunctionFactoryProvider(format.getBinaryHashFunctionFactoryProvider());
    builder.setHashFunctionFamilyProvider(format.getBinaryHashFunctionFamilyProvider());
    builder.setMissingWriterFactory(format.getMissingWriterFactory());
    builder.setPredicateEvaluatorFactoryProvider(format.getPredicateEvaluatorFactoryProvider());
    final SessionConfig.OutputFormat outputFormat = conf.fmt();
    switch(outputFormat) {
        case LOSSLESS_JSON:
            builder.setPrinterProvider(format.getLosslessJSONPrinterFactoryProvider());
            break;
        case CSV:
            builder.setPrinterProvider(format.getCSVPrinterFactoryProvider());
            break;
        case ADM:
            builder.setPrinterProvider(format.getADMPrinterFactoryProvider());
            break;
        case CLEAN_JSON:
            builder.setPrinterProvider(format.getCleanJSONPrinterFactoryProvider());
            break;
        default:
            throw new AlgebricksException("Unexpected OutputFormat: " + outputFormat);
    }
    builder.setSerializerDeserializerProvider(format.getSerdeProvider());
    builder.setTypeTraitProvider(format.getTypeTraitProvider());
    builder.setNormalizedKeyComputerFactoryProvider(format.getNormalizedKeyComputerFactoryProvider());
    JobEventListenerFactory jobEventListenerFactory = new JobEventListenerFactory(asterixJobId, metadataProvider.isWriteTransaction());
    JobSpecification spec = compiler.createJob(metadataProvider.getApplicationContext(), jobEventListenerFactory);
    // When the top-level statement is a query, the statement parameter is null.
    if (statement == null) {
        // Sets a required capacity, only for read-only queries.
        // DDLs and DMLs are considered not that frequent.
        spec.setRequiredClusterCapacity(ResourceUtils.getRequiredCompacity(plan, computationLocations, sortFrameLimit, groupFrameLimit, joinFrameLimit, frameSize));
    }
    if (conf.is(SessionConfig.OOB_HYRACKS_JOB)) {
        printPlanPrefix(output, "Hyracks job");
        if (rwQ != null) {
            try {
                output.out().println(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(spec.toJSON()));
            } catch (IOException e) {
                throw new AlgebricksException(e);
            }
            output.out().println(spec.getUserConstraints());
        }
        printPlanPostfix(output);
    }
    return spec;
}
Also used : IMergeAggregationExpressionFactory(org.apache.hyracks.algebricks.core.algebra.expressions.IMergeAggregationExpressionFactory) MergeAggregationExpressionFactory(org.apache.asterix.dataflow.data.common.MergeAggregationExpressionFactory) ICompilerFactory(org.apache.hyracks.algebricks.compiler.api.ICompilerFactory) SessionConfig(org.apache.asterix.translator.SessionConfig) ExpressionRuntimeProvider(org.apache.hyracks.algebricks.core.algebra.expressions.ExpressionRuntimeProvider) ILangExpressionToPlanTranslator(org.apache.asterix.algebra.base.ILangExpressionToPlanTranslator) IDataFormat(org.apache.asterix.formats.base.IDataFormat) JobSpecification(org.apache.hyracks.api.job.JobSpecification) HeuristicCompilerFactoryBuilder(org.apache.hyracks.algebricks.compiler.api.HeuristicCompilerFactoryBuilder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AlgebricksAppendable(org.apache.hyracks.algebricks.core.algebra.prettyprint.AlgebricksAppendable) LogicalOperatorPrettyPrintVisitor(org.apache.hyracks.algebricks.core.algebra.prettyprint.LogicalOperatorPrettyPrintVisitor) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) CompilerProperties(org.apache.asterix.common.config.CompilerProperties) ICompiler(org.apache.hyracks.algebricks.compiler.api.ICompiler) IOException(java.io.IOException) JobEventListenerFactory(org.apache.asterix.runtime.job.listener.JobEventListenerFactory) AlgebricksPartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksPartitionConstraint) AlgebricksAbsolutePartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksAbsolutePartitionConstraint) PartialAggregationTypeComputer(org.apache.asterix.dataflow.data.common.PartialAggregationTypeComputer) AlgebricksAbsolutePartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksAbsolutePartitionConstraint) Stats(org.apache.asterix.translator.IStatementExecutor.Stats) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Example 3 with AlgebricksAppendable

use of org.apache.hyracks.algebricks.core.algebra.prettyprint.AlgebricksAppendable in project asterixdb by apache.

the class ResultUtil method printResultHandle.

public static void printResultHandle(SessionOutput output, ResultHandle handle) throws HyracksDataException {
    try {
        final AlgebricksAppendable app = new AlgebricksAppendable(output.out());
        output.appendHandle(app, handle.toString());
    } catch (AlgebricksException e) {
        LOGGER.warn("error printing handle", e);
    }
}
Also used : AlgebricksAppendable(org.apache.hyracks.algebricks.core.algebra.prettyprint.AlgebricksAppendable) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)

Example 4 with AlgebricksAppendable

use of org.apache.hyracks.algebricks.core.algebra.prettyprint.AlgebricksAppendable in project asterixdb by apache.

the class ResultUtil method printStatus.

public static void printStatus(SessionOutput output, AbstractQueryApiServlet.ResultStatus rs) {
    try {
        final AlgebricksAppendable app = new AlgebricksAppendable(output.out());
        output.appendStatus(app, rs.str());
    } catch (AlgebricksException e) {
        LOGGER.warn("error printing status", e);
    }
}
Also used : AlgebricksAppendable(org.apache.hyracks.algebricks.core.algebra.prettyprint.AlgebricksAppendable) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)

Example 5 with AlgebricksAppendable

use of org.apache.hyracks.algebricks.core.algebra.prettyprint.AlgebricksAppendable in project asterixdb by apache.

the class ResultPrinter method printPrefix.

private void printPrefix() throws HyracksDataException {
    // output by displayCSVHeader(), so skip it here
    if (conf.is(SessionConfig.FORMAT_HTML)) {
        output.out().println("<h4>Results:</h4>");
        output.out().println("<pre class=\"result-content\">");
    }
    try {
        output.resultPrefix(new AlgebricksAppendable(output.out()));
    } catch (AlgebricksException e) {
        throw new HyracksDataException(e);
    }
    if (conf.is(SessionConfig.FORMAT_WRAPPER_ARRAY)) {
        output.out().print("[ ");
        wrapArray = true;
    }
    if (conf.fmt() == SessionConfig.OutputFormat.CSV && conf.is(SessionConfig.FORMAT_CSV_HEADER)) {
        if (recordType == null) {
            throw new HyracksDataException("Cannot print CSV with header without specifying output-record-type");
        }
        if (quoteRecord) {
            StringWriter sw = new StringWriter();
            appendCSVHeader(sw, recordType);
            output.out().print(JSONUtil.quoteAndEscape(sw.toString()));
            output.out().print("\n");
            notFirst = true;
        } else {
            appendCSVHeader(output.out(), recordType);
        }
    }
}
Also used : AlgebricksAppendable(org.apache.hyracks.algebricks.core.algebra.prettyprint.AlgebricksAppendable) StringWriter(java.io.StringWriter) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Aggregations

AlgebricksAppendable (org.apache.hyracks.algebricks.core.algebra.prettyprint.AlgebricksAppendable)7 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)5 LogicalOperatorPrettyPrintVisitor (org.apache.hyracks.algebricks.core.algebra.prettyprint.LogicalOperatorPrettyPrintVisitor)3 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ILangExpressionToPlanTranslator (org.apache.asterix.algebra.base.ILangExpressionToPlanTranslator)1 CompilerProperties (org.apache.asterix.common.config.CompilerProperties)1 MergeAggregationExpressionFactory (org.apache.asterix.dataflow.data.common.MergeAggregationExpressionFactory)1 PartialAggregationTypeComputer (org.apache.asterix.dataflow.data.common.PartialAggregationTypeComputer)1 IDataFormat (org.apache.asterix.formats.base.IDataFormat)1 JobEventListenerFactory (org.apache.asterix.runtime.job.listener.JobEventListenerFactory)1 Stats (org.apache.asterix.translator.IStatementExecutor.Stats)1 SessionConfig (org.apache.asterix.translator.SessionConfig)1 AlgebricksAbsolutePartitionConstraint (org.apache.hyracks.algebricks.common.constraints.AlgebricksAbsolutePartitionConstraint)1 AlgebricksPartitionConstraint (org.apache.hyracks.algebricks.common.constraints.AlgebricksPartitionConstraint)1 HeuristicCompilerFactoryBuilder (org.apache.hyracks.algebricks.compiler.api.HeuristicCompilerFactoryBuilder)1 ICompiler (org.apache.hyracks.algebricks.compiler.api.ICompiler)1 ICompilerFactory (org.apache.hyracks.algebricks.compiler.api.ICompilerFactory)1