Example usage for org.apache.commons.lang3.tuple Triple getRight

List of usage examples for org.apache.commons.lang3.tuple Triple getRight

Introduction

In this page you can find the example usage for org.apache.commons.lang3.tuple Triple getRight.

Prototype

public abstract R getRight();

Source Link

Document

Gets the right element from this triple.

Usage

From source file:de.tu_dortmund.ub.data.dswarm.TaskProcessingUnit.java

private static void executeTPUPartsOnDemand(final Optional<Boolean> optionalDoInit,
        final Optional<Boolean> optionalAllowMultipleDataModels, String[] watchFolderFiles,
        final String resourceWatchFolder, final Optional<String> optionalOutputDataModelID,
        final String serviceName, final Integer engineThreads,
        final Optional<Boolean> optionalDoTransformations, final Optional<Boolean> optionalDoIngestOnTheFly,
        final Optional<Boolean> optionalDoExportOnTheFly, final Optional<String> optionalExportMimeType,
        final Optional<String> optionalExportFileExtension, final Properties config) throws Exception {

    // keys = input data models; values = related data resources
    final Map<String, Triple<String, String, String>> inputDataModelsAndResources = new HashMap<>();

    // init//from w w w  .  ja va  2 s . c o  m
    if (optionalDoInit.isPresent() && optionalDoInit.get()) {

        if (optionalAllowMultipleDataModels.isPresent() && optionalAllowMultipleDataModels.get()) {

            for (int i = 0; i < watchFolderFiles.length; i++) {

                final String initResourceFileName = watchFolderFiles[i];

                doInit(resourceWatchFolder, initResourceFileName, serviceName, engineThreads, config,
                        inputDataModelsAndResources);

                // remove the file already processed during init from the files list to avoid duplicates
                watchFolderFiles = ArrayUtils.removeElement(watchFolderFiles, initResourceFileName);
            }
        } else {

            // use the first file in the folder for init
            final String initResourceFileName = watchFolderFiles[0];

            doInit(resourceWatchFolder, initResourceFileName, serviceName, engineThreads, config,
                    inputDataModelsAndResources);

            // remove the file already processed during init from the files list to avoid duplicates
            watchFolderFiles = ArrayUtils.removeElement(watchFolderFiles, initResourceFileName);
        }
    } else {

        final String inputDataModelID = config.getProperty(TPUStatics.PROTOTYPE_INPUT_DATA_MODEL_ID_IDENTIFIER);
        final String resourceID = config.getProperty(TPUStatics.PROTOTYPE_RESOURCE_ID_INDENTIFIER);

        inputDataModelsAndResources.put(inputDataModelID, Triple.of(inputDataModelID, resourceID, null));

        LOG.info("skip init part");
    }

    final Optional<Boolean> optionalDoIngest = TPUUtil.getBooleanConfigValue(TPUStatics.DO_INGEST_IDENTIFIER,
            config);

    // ingest
    if (optionalDoIngest.isPresent() && optionalDoIngest.get()) {

        final String projectName = config.getProperty(TPUStatics.PROJECT_NAME_IDENTIFIER);

        if (!optionalAllowMultipleDataModels.isPresent() || !optionalAllowMultipleDataModels.get()) {

            final Set<Map.Entry<String, Triple<String, String, String>>> entries = inputDataModelsAndResources
                    .entrySet();
            final Iterator<Map.Entry<String, Triple<String, String, String>>> iterator = entries.iterator();
            final Map.Entry<String, Triple<String, String, String>> entry = iterator.next();

            final String inputDataModelID = entry.getKey();
            final Triple<String, String, String> triple = entry.getValue();
            final String resourceID = triple.getMiddle();

            executeIngests(watchFolderFiles, inputDataModelID, resourceID, projectName, serviceName,
                    engineThreads, config);
        }
    } else {

        LOG.info("skip ingest");
    }

    if (!optionalOutputDataModelID.isPresent()) {

        throw new Exception(
                "please set an output data model ('prototype.outputDataModelID') for this TPU task");
    }

    final String outputDataModelID = optionalOutputDataModelID.get();

    // task execution
    if (optionalDoTransformations.isPresent() && optionalDoTransformations.get()) {

        if (optionalAllowMultipleDataModels.isPresent() && optionalAllowMultipleDataModels.get()) {

            final Set<Map.Entry<String, Triple<String, String, String>>> entries = inputDataModelsAndResources
                    .entrySet();

            for (final Map.Entry<String, Triple<String, String, String>> entry : entries) {

                final String inputDataModelID = entry.getKey();

                executeTransform(inputDataModelID, outputDataModelID, optionalDoIngestOnTheFly,
                        optionalDoExportOnTheFly, optionalExportMimeType, optionalExportFileExtension,
                        engineThreads, serviceName, config);
            }
        } else {

            final Set<Map.Entry<String, Triple<String, String, String>>> entries = inputDataModelsAndResources
                    .entrySet();
            final Iterator<Map.Entry<String, Triple<String, String, String>>> iterator = entries.iterator();
            final Map.Entry<String, Triple<String, String, String>> entry = iterator.next();

            final String inputDataModelID = entry.getKey();

            executeTransform(inputDataModelID, outputDataModelID, optionalDoIngestOnTheFly,
                    optionalDoExportOnTheFly, optionalExportMimeType, optionalExportFileExtension,
                    engineThreads, serviceName, config);
        }
    } else {

        LOG.info("skip transformations");
    }

    final Optional<Boolean> optionalDoExport = TPUUtil.getBooleanConfigValue(TPUStatics.DO_EXPORT_IDENTIFIER,
            config);

    // export
    if (optionalDoExport.isPresent() && optionalDoExport.get()) {

        if (!optionalAllowMultipleDataModels.isPresent() || !optionalAllowMultipleDataModels.get()) {

            final String exportDataModelID;

            if (outputDataModelID != null && !outputDataModelID.trim().isEmpty()) {

                exportDataModelID = outputDataModelID;
            } else {

                final Set<Map.Entry<String, Triple<String, String, String>>> entries = inputDataModelsAndResources
                        .entrySet();
                final Iterator<Map.Entry<String, Triple<String, String, String>>> iterator = entries.iterator();
                final Map.Entry<String, Triple<String, String, String>> entry = iterator.next();

                exportDataModelID = entry.getKey();
            }

            executeExport(exportDataModelID, optionalExportMimeType, optionalExportFileExtension, engineThreads,
                    serviceName, config);
        }
    } else {

        LOG.info("skip export");
    }

    // clean-up
    int cnt = 0;

    final String engineDswarmAPI = config.getProperty(TPUStatics.ENGINE_DSWARM_API_IDENTIFIER);

    final Set<Map.Entry<String, Triple<String, String, String>>> entries = inputDataModelsAndResources
            .entrySet();

    for (final Map.Entry<String, Triple<String, String, String>> entry : entries) {

        final Triple<String, String, String> triple = entry.getValue();

        final String inputDataModelId = triple.getLeft();
        final String resourceId = triple.getMiddle();
        final String configurationId = triple.getRight();

        TPUUtil.deleteObject(inputDataModelId, DswarmBackendStatics.DATAMODELS_ENDPOINT, serviceName,
                engineDswarmAPI, cnt);
        TPUUtil.deleteObject(resourceId, DswarmBackendStatics.RESOURCES_ENDPOINT, serviceName, engineDswarmAPI,
                cnt);
        TPUUtil.deleteObject(configurationId, DswarmBackendStatics.CONFIGURATIONS_ENDPOINT, serviceName,
                engineDswarmAPI, cnt);

        cnt++;
    }
}

From source file:org.apache.calcite.rel.rules.AbstractMaterializedViewRule.java

/**
 * Rewriting logic is based on "Optimizing Queries Using Materialized Views:
 * A Practical, Scalable Solution" by Goldstein and Larson.
 *
 * <p>On the query side, rules matches a Project-node chain or node, where node
 * is either an Aggregate or a Join. Subplan rooted at the node operator must
 * be composed of one or more of the following operators: TableScan, Project,
 * Filter, and Join./*from  www . j  a v a2s .c  o m*/
 *
 * <p>For each join MV, we need to check the following:
 * <ol>
 * <li> The plan rooted at the Join operator in the view produces all rows
 * needed by the plan rooted at the Join operator in the query.</li>
 * <li> All columns required by compensating predicates, i.e., predicates that
 * need to be enforced over the view, are available at the view output.</li>
 * <li> All output expressions can be computed from the output of the view.</li>
 * <li> All output rows occur with the correct duplication factor. We might
 * rely on existing Unique-Key - Foreign-Key relationships to extract that
 * information.</li>
 * </ol>
 *
 * <p>In turn, for each aggregate MV, we need to check the following:
 * <ol>
 * <li> The plan rooted at the Aggregate operator in the view produces all rows
 * needed by the plan rooted at the Aggregate operator in the query.</li>
 * <li> All columns required by compensating predicates, i.e., predicates that
 * need to be enforced over the view, are available at the view output.</li>
 * <li> The grouping columns in the query are a subset of the grouping columns
 * in the view.</li>
 * <li> All columns required to perform further grouping are available in the
 * view output.</li>
 * <li> All columns required to compute output expressions are available in the
 * view output.</li>
 * </ol>
 */
protected void perform(RelOptRuleCall call, Project topProject, RelNode node) {
    final RexBuilder rexBuilder = node.getCluster().getRexBuilder();
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final RelOptPlanner planner = call.getPlanner();
    final RexSimplify simplify = new RexSimplify(rexBuilder, true,
            planner.getExecutor() != null ? planner.getExecutor() : RexUtil.EXECUTOR);

    final List<RelOptMaterialization> materializations = (planner instanceof VolcanoPlanner)
            ? ((VolcanoPlanner) planner).getMaterializations()
            : ImmutableList.<RelOptMaterialization>of();

    if (!materializations.isEmpty()) {
        // 1. Explore query plan to recognize whether preconditions to
        // try to generate a rewriting are met
        if (!isValidPlan(topProject, node, mq)) {
            return;
        }

        // Obtain applicable (filtered) materializations
        // TODO: Filtering of relevant materializations needs to be
        // improved so we gather only materializations that might
        // actually generate a valid rewriting.
        final List<RelOptMaterialization> applicableMaterializations = RelOptMaterializations
                .getApplicableMaterializations(node, materializations);

        if (!applicableMaterializations.isEmpty()) {
            // 2. Initialize all query related auxiliary data structures
            // that will be used throughout query rewriting process
            // Generate query table references
            final Set<RelTableRef> queryTableRefs = mq.getTableReferences(node);
            if (queryTableRefs == null) {
                // Bail out
                return;
            }

            // Extract query predicates
            final RelOptPredicateList queryPredicateList = mq.getAllPredicates(node);
            if (queryPredicateList == null) {
                // Bail out
                return;
            }
            final RexNode pred = simplify.simplify(
                    RexUtil.composeConjunction(rexBuilder, queryPredicateList.pulledUpPredicates, false));
            final Triple<RexNode, RexNode, RexNode> queryPreds = splitPredicates(rexBuilder, pred);

            // Extract query equivalence classes. An equivalence class is a set
            // of columns in the query output that are known to be equal.
            final EquivalenceClasses qEC = new EquivalenceClasses();
            for (RexNode conj : RelOptUtil.conjunctions(queryPreds.getLeft())) {
                assert conj.isA(SqlKind.EQUALS);
                RexCall equiCond = (RexCall) conj;
                qEC.addEquivalenceClass((RexTableInputRef) equiCond.getOperands().get(0),
                        (RexTableInputRef) equiCond.getOperands().get(1));
            }

            // 3. We iterate through all applicable materializations trying to
            // rewrite the given query
            for (RelOptMaterialization materialization : applicableMaterializations) {
                final Project topViewProject;
                final RelNode viewNode;
                if (materialization.queryRel instanceof Project) {
                    topViewProject = (Project) materialization.queryRel;
                    viewNode = topViewProject.getInput();
                } else {
                    topViewProject = null;
                    viewNode = materialization.queryRel;
                }

                // 3.1. View checks before proceeding
                if (!isValidPlan(topViewProject, viewNode, mq)) {
                    // Skip it
                    continue;
                }

                // 3.2. Initialize all query related auxiliary data structures
                // that will be used throughout query rewriting process
                // Extract view predicates
                final RelOptPredicateList viewPredicateList = mq.getAllPredicates(viewNode);
                if (viewPredicateList == null) {
                    // Skip it
                    continue;
                }
                final RexNode viewPred = simplify.simplify(
                        RexUtil.composeConjunction(rexBuilder, viewPredicateList.pulledUpPredicates, false));
                final Triple<RexNode, RexNode, RexNode> viewPreds = splitPredicates(rexBuilder, viewPred);

                // Extract view table references
                final Set<RelTableRef> viewTableRefs = mq.getTableReferences(viewNode);
                if (viewTableRefs == null) {
                    // Bail out
                    return;
                }

                // Extract view tables
                MatchModality matchModality;
                Multimap<RexTableInputRef, RexTableInputRef> compensationEquiColumns = ArrayListMultimap
                        .create();
                if (!queryTableRefs.equals(viewTableRefs)) {
                    // We try to compensate, e.g., for join queries it might be
                    // possible to join missing tables with view to compute result.
                    // Two supported cases: query tables are subset of view tables (we need to
                    // check whether they are cardinality-preserving joins), or view tables are
                    // subset of query tables (add additional tables through joins if possible)
                    if (viewTableRefs.containsAll(queryTableRefs)) {
                        matchModality = MatchModality.QUERY_PARTIAL;
                        final EquivalenceClasses vEC = new EquivalenceClasses();
                        for (RexNode conj : RelOptUtil.conjunctions(viewPreds.getLeft())) {
                            assert conj.isA(SqlKind.EQUALS);
                            RexCall equiCond = (RexCall) conj;
                            vEC.addEquivalenceClass((RexTableInputRef) equiCond.getOperands().get(0),
                                    (RexTableInputRef) equiCond.getOperands().get(1));
                        }
                        if (!compensateQueryPartial(compensationEquiColumns, viewTableRefs, vEC,
                                queryTableRefs)) {
                            // Cannot rewrite, skip it
                            continue;
                        }
                    } else if (queryTableRefs.containsAll(viewTableRefs)) {
                        // TODO: implement latest case
                        matchModality = MatchModality.VIEW_PARTIAL;
                        continue;
                    } else {
                        // Skip it
                        continue;
                    }
                } else {
                    matchModality = MatchModality.COMPLETE;
                }

                // 4. We map every table in the query to a view table with the same qualified
                // name.
                final Multimap<RelTableRef, RelTableRef> multiMapTables = ArrayListMultimap.create();
                for (RelTableRef queryTableRef : queryTableRefs) {
                    for (RelTableRef viewTableRef : viewTableRefs) {
                        if (queryTableRef.getQualifiedName().equals(viewTableRef.getQualifiedName())) {
                            multiMapTables.put(queryTableRef, viewTableRef);
                        }
                    }
                }

                // If a table is used multiple times, we will create multiple mappings,
                // and we will try to rewrite the query using each of the mappings.
                // Then, we will try to map every source table (query) to a target
                // table (view), and if we are successful, we will try to create
                // compensation predicates to filter the view results further
                // (if needed).
                final List<BiMap<RelTableRef, RelTableRef>> flatListMappings = generateTableMappings(
                        multiMapTables);
                for (BiMap<RelTableRef, RelTableRef> tableMapping : flatListMappings) {
                    // 4.0. If compensation equivalence classes exist, we need to add
                    // the mapping to the query mapping
                    final EquivalenceClasses currQEC = EquivalenceClasses.copy(qEC);
                    if (matchModality == MatchModality.QUERY_PARTIAL) {
                        for (Entry<RexTableInputRef, RexTableInputRef> e : compensationEquiColumns.entries()) {
                            // Copy origin
                            RelTableRef queryTableRef = tableMapping.inverse().get(e.getKey().getTableRef());
                            RexTableInputRef queryColumnRef = RexTableInputRef.of(queryTableRef,
                                    e.getKey().getIndex(), e.getKey().getType());
                            // Add to query equivalence classes and table mapping
                            currQEC.addEquivalenceClass(queryColumnRef, e.getValue());
                            tableMapping.put(e.getValue().getTableRef(), e.getValue().getTableRef()); //identity
                        }
                    }

                    final RexNode compensationColumnsEquiPred;
                    final RexNode compensationRangePred;
                    final RexNode compensationResidualPred;

                    // 4.1. Establish relationship between view and query equivalence classes.
                    // If every view equivalence class is not a subset of a query
                    // equivalence class, we bail out.
                    // To establish relationship, we swap column references of the view predicates
                    // to point to query tables. Then, we create the equivalence classes for the
                    // view predicates and check that every view equivalence class is a subset of a
                    // query equivalence class: if it is not, we bail out.
                    final RexNode viewColumnsEquiPred = RexUtil.swapTableReferences(rexBuilder,
                            viewPreds.getLeft(), tableMapping.inverse());
                    final EquivalenceClasses queryBasedVEC = new EquivalenceClasses();
                    for (RexNode conj : RelOptUtil.conjunctions(viewColumnsEquiPred)) {
                        assert conj.isA(SqlKind.EQUALS);
                        RexCall equiCond = (RexCall) conj;
                        queryBasedVEC.addEquivalenceClass((RexTableInputRef) equiCond.getOperands().get(0),
                                (RexTableInputRef) equiCond.getOperands().get(1));
                    }
                    compensationColumnsEquiPred = generateEquivalenceClasses(rexBuilder, currQEC,
                            queryBasedVEC);
                    if (compensationColumnsEquiPred == null) {
                        // Skip it
                        continue;
                    }

                    // 4.2. We check that range intervals for the query are contained in the view.
                    // Compute compensating predicates.
                    final RexNode queryRangePred = RexUtil.swapColumnReferences(rexBuilder,
                            queryPreds.getMiddle(), currQEC.getEquivalenceClassesMap());
                    final RexNode viewRangePred = RexUtil.swapTableColumnReferences(rexBuilder,
                            viewPreds.getMiddle(), tableMapping.inverse(), currQEC.getEquivalenceClassesMap());
                    compensationRangePred = SubstitutionVisitor.splitFilter(simplify, queryRangePred,
                            viewRangePred);
                    if (compensationRangePred == null) {
                        // Skip it
                        continue;
                    }

                    // 4.3. Finally, we check that residual predicates of the query are satisfied
                    // within the view.
                    // Compute compensating predicates.
                    final RexNode queryResidualPred = RexUtil.swapColumnReferences(rexBuilder,
                            queryPreds.getRight(), currQEC.getEquivalenceClassesMap());
                    final RexNode viewResidualPred = RexUtil.swapTableColumnReferences(rexBuilder,
                            viewPreds.getRight(), tableMapping.inverse(), currQEC.getEquivalenceClassesMap());
                    compensationResidualPred = SubstitutionVisitor.splitFilter(simplify, queryResidualPred,
                            viewResidualPred);
                    if (compensationResidualPred == null) {
                        // Skip it
                        continue;
                    }

                    // 4.4. Final compensation predicate.
                    RexNode compensationPred = RexUtil.composeConjunction(rexBuilder, ImmutableList
                            .of(compensationColumnsEquiPred, compensationRangePred, compensationResidualPred),
                            false);
                    if (!compensationPred.isAlwaysTrue()) {
                        // All columns required by compensating predicates must be contained
                        // in the view output (condition 2).
                        List<RexNode> viewExprs = extractExpressions(topViewProject, viewNode, rexBuilder);
                        compensationPred = rewriteExpression(rexBuilder, viewNode, viewExprs, compensationPred,
                                tableMapping, currQEC.getEquivalenceClassesMap(), mq);
                        if (compensationPred == null) {
                            // Skip it
                            continue;
                        }
                    }

                    // 4.5. Generate final rewriting if possible.
                    // First, we add the compensation predicate (if any) on top of the view.
                    // Then, we trigger the Aggregate unifying method. This method will either create
                    // a Project or an Aggregate operator on top of the view. It will also compute the
                    // output expressions for the query.
                    RelBuilder builder = call.builder();
                    builder.push(materialization.tableRel);
                    if (!compensationPred.isAlwaysTrue()) {
                        builder.filter(simplify.simplify(compensationPred));
                    }
                    RelNode result = unify(rexBuilder, builder, builder.build(), topProject, node,
                            topViewProject, viewNode, tableMapping, currQEC.getEquivalenceClassesMap(), mq);
                    if (result == null) {
                        // Skip it
                        continue;
                    }
                    call.transformTo(result);
                }
            }
        }
    }
}

From source file:org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveTableScan.java

private HiveTableScan(RelOptCluster cluster, RelTraitSet traitSet, RelOptHiveTable table, String alias,
        String concatQbIDAlias, RelDataType newRowtype, boolean useQBIdInDigest, boolean insideView) {
    super(cluster, TraitsUtil.getDefaultTraitSet(cluster), table);
    assert getConvention() == HiveRelNode.CONVENTION;
    this.tblAlias = alias;
    this.concatQbIDAlias = concatQbIDAlias;
    this.hiveTableScanRowType = newRowtype;
    Triple<ImmutableList<Integer>, ImmutableSet<Integer>, ImmutableSet<Integer>> colIndxPair = buildColIndxsFrmReloptHT(
            table, newRowtype);//from   ww  w. j  av a 2s . c  o  m
    this.neededColIndxsFrmReloptHT = colIndxPair.getLeft();
    this.virtualOrPartColIndxsInTS = colIndxPair.getMiddle();
    this.virtualColIndxsInTS = colIndxPair.getRight();
    this.useQBIdInDigest = useQBIdInDigest;
    this.insideView = insideView;
}

From source file:org.apache.kylin.rest.util.AdHocUtil.java

static String restoreComputedColumnToExpr(String sql, CCInfo ccInfo) {

    String ccName = ccInfo.getComputedColumnDesc().getColumnName();
    List<Triple<Integer, Integer, String>> replacements = Lists.newArrayList();
    Matcher matcher = identifierInSqlPattern.matcher(sql);

    while (matcher.find()) {
        if (matcher.group(1) != null) { //with quote case: "TABLE"."COLUMN"

            String quotedColumnName = matcher.group(3);
            Preconditions.checkNotNull(quotedColumnName);
            String columnName = StringUtils.strip(quotedColumnName, "\"");
            if (!columnName.equalsIgnoreCase(ccName)) {
                continue;
            }//from  w  ww  .j  av  a  2s.c  o  m

            if (matcher.group(2) != null) { // table name exist 
                String quotedTableAlias = StringUtils.strip(matcher.group(2), ".");
                String tableAlias = StringUtils.strip(quotedTableAlias, "\"");
                replacements.add(Triple.of(matcher.start(1), matcher.end(1), replaceIdentifierInExpr(
                        ccInfo.getComputedColumnDesc().getExpression(), tableAlias, true)));
            } else { //only column
                if (endWithAsPattern.matcher(sql.substring(0, matcher.start(1))).find()) {
                    //select DEAL_AMOUNT as "deal_amount" case
                    continue;
                }
                replacements.add(Triple.of(matcher.start(1), matcher.end(1),
                        replaceIdentifierInExpr(ccInfo.getComputedColumnDesc().getExpression(), null, true)));
            }
        } else if (matcher.group(4) != null) { //without quote case: table.column or simply column
            String columnName = matcher.group(6);
            Preconditions.checkNotNull(columnName);
            if (!columnName.equalsIgnoreCase(ccName)) {
                continue;
            }

            if (matcher.group(5) != null) { //table name exist
                String tableAlias = StringUtils.strip(matcher.group(5), ".");
                replacements.add(Triple.of(matcher.start(4), matcher.end(4), replaceIdentifierInExpr(
                        ccInfo.getComputedColumnDesc().getExpression(), tableAlias, false)));

            } else { //only column 
                if (endWithAsPattern.matcher(sql.substring(0, matcher.start(4))).find()) {
                    //select DEAL_AMOUNT as deal_amount case
                    continue;
                }
                replacements.add(Triple.of(matcher.start(4), matcher.end(4),
                        replaceIdentifierInExpr(ccInfo.getComputedColumnDesc().getExpression(), null, false)));
            }
        }
    }

    Collections.reverse(replacements);
    for (Triple<Integer, Integer, String> triple : replacements) {
        sql = sql.substring(0, triple.getLeft()) + "(" + triple.getRight() + ")"
                + sql.substring(triple.getMiddle());
    }
    return sql;
}

From source file:org.apache.kylin.rest.util.AdHocUtil.java

static String replaceIdentifierInExpr(String expr, String tableAlias, boolean quoted) {
    List<Triple<Integer, Integer, String>> replacements = Lists.newArrayList();
    Matcher matcher = identifierInExprPattern.matcher(expr);
    while (matcher.find()) {

        String t = tableAlias == null ? StringUtils.strip(matcher.group(3), ".") : tableAlias;
        String c = matcher.group(4);

        String replacement = quoted ? "\"" + t.toUpperCase() + "\".\"" + c.toUpperCase() + "\"" : t + "." + c;
        replacements.add(Triple.of(matcher.start(1), matcher.end(1), replacement));
    }/*from   www .java 2 s . c  om*/

    Collections.reverse(replacements);
    for (Triple<Integer, Integer, String> triple : replacements) {
        expr = expr.substring(0, triple.getLeft()) + triple.getRight() + expr.substring(triple.getMiddle());
    }
    return expr;
}

From source file:org.apache.kylin.rest.util.PushDownUtil.java

static String restoreComputedColumnToExpr(String sql, ComputedColumnDesc computedColumnDesc) {

    String ccName = computedColumnDesc.getColumnName();
    List<Triple<Integer, Integer, String>> replacements = Lists.newArrayList();
    Matcher matcher = identifierInSqlPattern.matcher(sql);

    while (matcher.find()) {
        if (matcher.group(1) != null) { //with quote case: "TABLE"."COLUMN"

            String quotedColumnName = matcher.group(3);
            Preconditions.checkNotNull(quotedColumnName);
            String columnName = StringUtils.strip(quotedColumnName, "\"");
            if (!columnName.equalsIgnoreCase(ccName)) {
                continue;
            }//from ww w  . j  ava 2s . com

            if (matcher.group(2) != null) { // table name exist 
                String quotedTableAlias = StringUtils.strip(matcher.group(2), ".");
                String tableAlias = StringUtils.strip(quotedTableAlias, "\"");
                replacements.add(Triple.of(matcher.start(1), matcher.end(1),
                        replaceIdentifierInExpr(computedColumnDesc.getExpression(), tableAlias, true)));
            } else { //only column
                if (endWithAsPattern.matcher(sql.substring(0, matcher.start(1))).find()) {
                    //select DEAL_AMOUNT as "deal_amount" case
                    continue;
                }
                replacements.add(Triple.of(matcher.start(1), matcher.end(1),
                        replaceIdentifierInExpr(computedColumnDesc.getExpression(), null, true)));
            }
        } else if (matcher.group(4) != null) { //without quote case: table.column or simply column
            String columnName = matcher.group(6);
            Preconditions.checkNotNull(columnName);
            if (!columnName.equalsIgnoreCase(ccName)) {
                continue;
            }

            if (matcher.group(5) != null) { //table name exist
                String tableAlias = StringUtils.strip(matcher.group(5), ".");
                replacements.add(Triple.of(matcher.start(4), matcher.end(4),
                        replaceIdentifierInExpr(computedColumnDesc.getExpression(), tableAlias, false)));

            } else { //only column 
                if (endWithAsPattern.matcher(sql.substring(0, matcher.start(4))).find()) {
                    //select DEAL_AMOUNT as deal_amount case
                    continue;
                }
                replacements.add(Triple.of(matcher.start(4), matcher.end(4),
                        replaceIdentifierInExpr(computedColumnDesc.getExpression(), null, false)));
            }
        }
    }

    Collections.reverse(replacements);
    for (Triple<Integer, Integer, String> triple : replacements) {
        sql = sql.substring(0, triple.getLeft()) + "(" + triple.getRight() + ")"
                + sql.substring(triple.getMiddle());
    }
    return sql;
}

From source file:org.apache.olingo.ext.pojogen.AbstractPOJOGenMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (new File(outputDirectory + File.separator + TOOL_DIR).exists()) {
        getLog().info("Nothing to do because " + TOOL_DIR + " directory already exists. Clean to update.");
        return;/*ww w.ja v  a2 s.co  m*/
    }

    Velocity.addProperty(Velocity.RESOURCE_LOADER, "class");
    Velocity.addProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());

    try {
        final Triple<XMLMetadata, String, Edm> metadata = getMetadata();

        for (EdmSchema schema : metadata.getRight().getSchemas()) {
            namespaces.add(schema.getNamespace().toLowerCase());
        }

        final Map<String, String> entityTypeNames = new HashMap<String, String>();
        final Map<String, String> complexTypeNames = new HashMap<String, String>();
        final Map<String, String> enumTypeNames = new HashMap<String, String>();
        final Map<String, String> termNames = new HashMap<String, String>();

        final Map<String, Object> objs = new HashMap<String, Object>();

        for (EdmSchema schema : metadata.getRight().getSchemas()) {
            createUtility(metadata.getRight(), schema, basePackage);

            // write package-info for the base package
            final String schemaPath = utility.getNamespace().toLowerCase().replace('.', File.separatorChar);
            final File base = mkPkgDir(schemaPath);
            final String pkg = StringUtils.isBlank(basePackage) ? utility.getNamespace().toLowerCase()
                    : basePackage + "." + utility.getNamespace().toLowerCase();
            parseObj(base, pkg, "package-info", "package-info.java");

            // write package-info for types package
            final File typesBaseDir = mkPkgDir(schemaPath + "/types");
            final String typesPkg = pkg + ".types";
            parseObj(typesBaseDir, typesPkg, "package-info", "package-info.java");

            for (EdmTerm term : schema.getTerms()) {
                final String className = utility.capitalize(term.getName());
                termNames.put(term.getFullQualifiedName().toString(), typesPkg + "." + className);
                objs.clear();
                objs.put("term", term);
                parseObj(typesBaseDir, typesPkg, "term", className + ".java", objs);
            }

            for (EdmEnumType enumType : schema.getEnumTypes()) {
                final String className = utility.capitalize(enumType.getName());
                enumTypeNames.put(enumType.getFullQualifiedName().toString(), typesPkg + "." + className);
                objs.clear();
                objs.put("enumType", enumType);
                parseObj(typesBaseDir, typesPkg, "enumType", className + ".java", objs);
            }

            final List<EdmComplexType> complexes = new ArrayList<EdmComplexType>();

            for (EdmComplexType complex : schema.getComplexTypes()) {
                complexes.add(complex);
                final String className = utility.capitalize(complex.getName());
                complexTypeNames.put(complex.getFullQualifiedName().toString(), typesPkg + "." + className);
                objs.clear();
                objs.put("complexType", complex);

                parseObj(typesBaseDir, typesPkg, "complexType", className + ".java", objs);
                parseObj(typesBaseDir, typesPkg, "complexTypeComposableInvoker",
                        className + "ComposableInvoker.java", objs);
                parseObj(typesBaseDir, typesPkg, "complexCollection", className + "Collection.java", objs);
                parseObj(typesBaseDir, typesPkg, "complexCollectionComposableInvoker",
                        className + "CollectionComposableInvoker.java", objs);
            }

            for (EdmEntityType entity : schema.getEntityTypes()) {
                final String className = utility.capitalize(entity.getName());
                entityTypeNames.put(entity.getFullQualifiedName().toString(), typesPkg + "." + className);

                objs.clear();
                objs.put("entityType", entity);

                final Map<String, String> keys;

                EdmEntityType baseType = null;
                if (entity.getBaseType() == null) {
                    keys = getUtility().getEntityKeyType(entity);
                } else {
                    baseType = entity.getBaseType();
                    objs.put("baseType", getUtility().getJavaType(baseType.getFullQualifiedName().toString()));
                    while (baseType.getBaseType() != null) {
                        baseType = baseType.getBaseType();
                    }
                    keys = getUtility().getEntityKeyType(baseType);
                }

                if (keys.size() > 1) {
                    // create compound key class
                    final String keyClassName = utility
                            .capitalize(baseType == null ? entity.getName() : baseType.getName()) + "Key";
                    objs.put("keyRef", keyClassName);

                    if (entity.getBaseType() == null) {
                        objs.put("keys", keys);
                        parseObj(typesBaseDir, typesPkg, "entityTypeKey", keyClassName + ".java", objs);
                    }
                }

                parseObj(typesBaseDir, typesPkg, "entityType", className + ".java", objs);
                parseObj(typesBaseDir, typesPkg, "entityComposableInvoker",
                        className + "ComposableInvoker.java", objs);
                parseObj(typesBaseDir, typesPkg, "entityCollection", className + "Collection.java", objs);
                parseObj(typesBaseDir, typesPkg, "entityCollectionComposableInvoker",
                        className + "CollectionComposableInvoker.java", objs);
            }

            // write container and top entity sets into the base package
            EdmEntityContainer container = schema.getEntityContainer();
            if (container != null) {
                objs.clear();
                objs.put("container", container);
                objs.put("namespace", schema.getNamespace());
                objs.put("complexes", complexes);

                parseObj(base, pkg, "container", utility.capitalize(container.getName()) + ".java", objs);

                for (EdmEntitySet entitySet : container.getEntitySets()) {
                    objs.clear();
                    objs.put("entitySet", entitySet);
                    objs.put("container", container);
                    parseObj(base, pkg, "entitySet", utility.capitalize(entitySet.getName()) + ".java", objs);
                }
            }
        }

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final GZIPOutputStream gzos = new GZIPOutputStream(baos);
        final ObjectOutputStream oos = new ObjectOutputStream(gzos);
        try {
            oos.writeObject(metadata.getLeft());
        } finally {
            oos.close();
            gzos.close();
            baos.close();
        }

        objs.clear();
        objs.put("metadata", new String(Base64.encodeBase64(baos.toByteArray()), "UTF-8"));
        objs.put("metadataETag", metadata.getMiddle());
        objs.put("entityTypes", entityTypeNames);
        objs.put("complexTypes", complexTypeNames);
        objs.put("enumTypes", enumTypeNames);
        objs.put("terms", termNames);
        final String actualBP = StringUtils.isBlank(basePackage) ? StringUtils.EMPTY : basePackage;
        parseObj(mkdir(actualBP.replace('.', File.separatorChar)), actualBP, "service", "Service.java", objs);
    } catch (Exception t) {
        getLog().error(t);

        throw (t instanceof MojoExecutionException) ? (MojoExecutionException) t
                : new MojoExecutionException("While executin mojo", t);
    }
}

From source file:org.apache.olingo.ext.proxy.commons.AbstractCollectionInvocationHandler.java

public Collection<T> execute() {
    if (this.uri != null) {
        final Triple<List<T>, URI, List<ClientAnnotation>> res = fetchPartial(this.uri.build(), itemRef);
        this.nextPageURI = res.getMiddle();

        if (items == null) {
            items = res.getLeft();/*from  w ww .  jav a  2  s .  c  o m*/
        } else {
            items.clear();
            items.addAll(res.getLeft());
        }

        annotations.clear();
        annotations.addAll(res.getRight());
    }

    return this;
}

From source file:org.apache.olingo.ext.proxy.commons.EntitySetInvocationHandler.java

@SuppressWarnings("unchecked")
public <S extends T, SEC extends EntityCollection<S, ?, ?>> SEC execute(final Class<SEC> collTypeRef) {
    final Class<S> ref = (Class<S>) ClassUtils.extractTypeArg(collTypeRef, AbstractEntitySet.class,
            AbstractSingleton.class, EntityCollection.class);
    final Class<S> oref = (Class<S>) ClassUtils.extractTypeArg(this.collItemRef, AbstractEntitySet.class,
            AbstractSingleton.class, EntityCollection.class);

    if (!oref.equals(ref)) {
        uri.appendDerivedEntityTypeSegment(
                new FullQualifiedName(ClassUtils.getNamespace(ref), ClassUtils.getEntityTypeName(ref))
                        .toString());//from ww w  .j  a  va 2 s  .  c om
    }

    final List<ClientAnnotation> anns = new ArrayList<ClientAnnotation>();

    final Triple<List<T>, URI, List<ClientAnnotation>> entitySet = fetchPartial(uri.build(), (Class<T>) ref);
    anns.addAll(entitySet.getRight());

    final EntityCollectionInvocationHandler<S> entityCollectionHandler = new EntityCollectionInvocationHandler<S>(
            service, (List<S>) entitySet.getLeft(), collTypeRef, this.baseURI, uri);
    entityCollectionHandler.setAnnotations(anns);

    entityCollectionHandler.nextPageURI = entitySet.getMiddle();

    return (SEC) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
            new Class<?>[] { collTypeRef }, entityCollectionHandler);
}

From source file:org.apache.olingo.ext.proxy.commons.EntitySetInvocationHandler.java

@SuppressWarnings("unchecked")
public <S extends T, SEC extends EntityCollection<S, ?, ?>> SEC fetchWholeEntitySet(final URIBuilder uriBuilder,
        final Class<S> typeRef, final Class<SEC> collTypeRef) {

    final List<S> res = new ArrayList<S>();
    final List<ClientAnnotation> anns = new ArrayList<ClientAnnotation>();

    URI nextURI = uriBuilder.build();
    while (nextURI != null) {
        final Triple<List<T>, URI, List<ClientAnnotation>> entitySet = fetchPartial(nextURI,
                (Class<T>) typeRef);
        res.addAll((List<S>) entitySet.getLeft());
        nextURI = entitySet.getMiddle();
        anns.addAll(entitySet.getRight());
    }/*from  www  .  j  a  v  a  2 s  .  co  m*/

    final EntityCollectionInvocationHandler<S> entityCollectionHandler = new EntityCollectionInvocationHandler<S>(
            service, res, collTypeRef, targetEntitySetURI, uriBuilder);
    entityCollectionHandler.setAnnotations(anns);

    return (SEC) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
            new Class<?>[] { collTypeRef }, entityCollectionHandler);
}