Make the comparison recipe a subquery that is left joined to the
        base recipe using dimensions that are shared between the recipes.

        Hoist the metric from the comparison recipe up to the base query
        while adding the suffix.
353846
def modify_postquery_parts(self, postquery_parts):
        
        if not self.compare_recipe:
            return postquery_parts

        for compare_recipe, compare_suffix in zip(
            self.compare_recipe, self.suffix
        ):
            comparison_subq = compare_recipe.subquery()

            # For all metrics in the comparison recipe
            # Use the metric in the base recipe and
            # Add the metric columns to the base recipe

            # Comparison metrics hoisted into the base recipe need an
            # aggregation function.The default is func.avg but
            # metrics can override this by provoding a
            # metric.meta.summary_aggregation callable parameter
            for m in compare_recipe.metric_ids:
                met = compare_recipe._cauldron[m]
                id = met.id
                met.id = id + compare_suffix
                summary_aggregation = met.meta.get(
                    'summary_aggregation', func.avg
                )
                self.recipe._cauldron.use(met)
                for suffix in met.make_column_suffixes():
                    col = getattr(comparison_subq.c, id + suffix, None)
                    if col is not None:
                        postquery_parts['query'] = \
                            postquery_parts['query'].add_columns(
                                summary_aggregation(col).label(
                                    met.id + suffix))
                    else:
                        raise BadRecipe(
                            '{} could not be found in .compare() '
                            'recipe subquery'.format(id + suffix)
                        )

            join_conditions = []
            for dim in compare_recipe.dimension_ids:
                if dim not in self.recipe.dimension_ids:
                    raise BadRecipe(
                        '{} dimension in comparison recipe must exist '
                        'in base recipe'
                    )
                base_dim = self.recipe._cauldron[dim]
                compare_dim = compare_recipe._cauldron[dim]
                base_col = base_dim.columns[0]
                compare_col = getattr(
                    comparison_subq.c, compare_dim.id_prop, None
                )
                if compare_col is None:
                    raise BadRecipe(
                        'Can\'t find join property for {} dimension in \
                        compare recipe'.format(compare_dim.id_prop)
                    )
                join_conditions.append(base_col == compare_col)

            join_clause = text('1=1')
            if join_conditions:
                join_clause = and_(*join_conditions)

            postquery_parts['query'] = postquery_parts['query'] \
                .outerjoin(comparison_subq, join_clause)

        return postquery_parts
229757
private Where<ExtendedRelation, Long> addToWhere(
			QueryBuilder<ExtendedRelation, Long> qb,
			Where<ExtendedRelation, Long> where) {
		if (where == null) {
			where = qb.where();
		} else {
			where.and();
		}
		return where;
	}
582449
private static String initCreateRelationshipQuery(EntityKeyMetadata ownerEntityKeyMetadata, AssociationKeyMetadata associationKeyMetadata) {
		EntityKeyMetadata targetEntityKeyMetadata = associationKeyMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata();
		int offset = 0;
		StringBuilder queryBuilder = new StringBuilder( "MATCH " );
		appendEntityNode( "n", ownerEntityKeyMetadata, queryBuilder );
		queryBuilder.append( ", " );
		offset += ownerEntityKeyMetadata.getColumnNames().length;
		appendEntityNode( "t", targetEntityKeyMetadata, queryBuilder, offset );
		queryBuilder.append( " MERGE (n)" );
		queryBuilder.append( " -[r" );
		queryBuilder.append( ":" );
		appendRelationshipType( queryBuilder, associationKeyMetadata );
		offset = ownerEntityKeyMetadata.getColumnNames().length;
		if ( associationKeyMetadata.getRowKeyIndexColumnNames().length > 0 ) {
			offset += targetEntityKeyMetadata.getColumnNames().length;
			appendProperties( queryBuilder, associationKeyMetadata.getRowKeyIndexColumnNames(), offset );
		}
		queryBuilder.append( "]-> (t)" );
		queryBuilder.append( " RETURN r" );
		return queryBuilder.toString();
	}
183391
private IQ mergeDefinitions(Collection<IQ> queries) {
        return queryMerger.mergeDefinitions(queries)
                .orElseThrow(() -> new MappingMergingException("The query should be present"));
    }
1062853
def similar(self, threshold, **criterias):
		''''''
		# XXX: use F from https://docs.djangoproject.com/en/1.8/ref/models/expressions/
		meta = self.model._meta
		funcs, params = list(), list()
		for name,val in criterias.iteritems():
			name = meta.get_field(name, many_to_many=False).column
			name = '.'.join(it.imap(connection.ops.quote_name, (meta.db_table, name)))
			# Alas, pg_trgm is for containment tests, not fuzzy matches,
			#  but it can potentially be used to find closest results as well
			# funcs.append( 'similarity(CAST({0}.{1} as text), CAST(%s as text))'\
			# Ok, these two are just to make sure levenshtein() won't crash
			#  w/ "argument exceeds the maximum length of N bytes error"
			funcs.append('octet_length({0}) <= {1}'.format(name, self.levenshtein_limit))
			funcs.append('octet_length(%s) <= {0}'.format(self.levenshtein_limit))
			# Then there's a possibility of division by zero...
			funcs.append('length({0}) > 0'.format(name))
			# And if everything else fits, the comparison itself
			funcs.append('levenshtein({0}, %s) / CAST(length({0}) AS numeric) < %s'.format(name))
			params.extend((val, val, float(1 - threshold)))
		return self.extra(where=funcs, params=params)
343479
public void addSubMetricQuery(final String metric_query, 
                                final int sub_query_index,
                                final int param_index) {
    if (metric_query == null || metric_query.isEmpty()) {
      throw new IllegalArgumentException("Metric query cannot be null or empty");
    }
    if (sub_query_index < 0) {
      throw new IllegalArgumentException("Sub query index must be 0 or greater");
    }
    if (param_index < 0) {
      throw new IllegalArgumentException("Parameter index must be 0 or greater");
    }
    if (sub_metric_queries == null) {
      sub_metric_queries = Maps.newHashMap();
    }
    sub_metric_queries.put(sub_query_index, metric_query);
    parameter_index.put(param_index, Parameter.METRIC_QUERY);
  }
1360631
private Pair<Pair<KNNQuery<O>, KNNQuery<O>>, Pair<RKNNQuery<O>, RKNNQuery<O>>> getKNNAndRkNNQueries(Database database, Relation<O> relation, StepProgress stepprog) {
    DistanceQuery<O> drefQ = database.getDistanceQuery(relation, referenceDistanceFunction);
    // Use "HEAVY" flag, since this is an online algorithm
    KNNQuery<O> kNNRefer = database.getKNNQuery(drefQ, krefer, DatabaseQuery.HINT_HEAVY_USE, DatabaseQuery.HINT_OPTIMIZED_ONLY, DatabaseQuery.HINT_NO_CACHE);
    RKNNQuery<O> rkNNRefer = database.getRKNNQuery(drefQ, DatabaseQuery.HINT_HEAVY_USE, DatabaseQuery.HINT_OPTIMIZED_ONLY, DatabaseQuery.HINT_NO_CACHE);

    // No optimized kNN query or RkNN query - use a preprocessor!
    if(kNNRefer == null || rkNNRefer == null) {
      if(stepprog != null) {
        stepprog.beginStep(1, "Materializing neighborhood w.r.t. reference neighborhood distance function.", LOG);
      }
      MaterializeKNNAndRKNNPreprocessor<O> preproc = new MaterializeKNNAndRKNNPreprocessor<>(relation, referenceDistanceFunction, krefer);
      kNNRefer = preproc.getKNNQuery(drefQ, krefer, DatabaseQuery.HINT_HEAVY_USE);
      rkNNRefer = preproc.getRKNNQuery(drefQ, krefer, DatabaseQuery.HINT_HEAVY_USE);
      // add as index
      database.getHierarchy().add(relation, preproc);
    }
    else {
      if(stepprog != null) {
        stepprog.beginStep(1, "Optimized neighborhood w.r.t. reference neighborhood distance function provided by database.", LOG);
      }
    }

    DistanceQuery<O> dreachQ = database.getDistanceQuery(relation, reachabilityDistanceFunction);
    KNNQuery<O> kNNReach = database.getKNNQuery(dreachQ, kreach, DatabaseQuery.HINT_HEAVY_USE, DatabaseQuery.HINT_OPTIMIZED_ONLY, DatabaseQuery.HINT_NO_CACHE);
    RKNNQuery<O> rkNNReach = database.getRKNNQuery(dreachQ, DatabaseQuery.HINT_HEAVY_USE, DatabaseQuery.HINT_OPTIMIZED_ONLY, DatabaseQuery.HINT_NO_CACHE);
    if(kNNReach == null || rkNNReach == null) {
      if(stepprog != null) {
        stepprog.beginStep(2, "Materializing neighborhood w.r.t. reachability distance function.", LOG);
      }
      ListParameterization config = new ListParameterization();
      config.addParameter(AbstractMaterializeKNNPreprocessor.Factory.DISTANCE_FUNCTION_ID, reachabilityDistanceFunction);
      config.addParameter(AbstractMaterializeKNNPreprocessor.Factory.K_ID, kreach);
      MaterializeKNNAndRKNNPreprocessor<O> preproc = new MaterializeKNNAndRKNNPreprocessor<>(relation, reachabilityDistanceFunction, kreach);
      kNNReach = preproc.getKNNQuery(dreachQ, kreach, DatabaseQuery.HINT_HEAVY_USE);
      rkNNReach = preproc.getRKNNQuery(dreachQ, kreach, DatabaseQuery.HINT_HEAVY_USE);
      // add as index
      database.getHierarchy().add(relation, preproc);
    }

    Pair<KNNQuery<O>, KNNQuery<O>> kNNPair = new Pair<>(kNNRefer, kNNReach);
    Pair<RKNNQuery<O>, RKNNQuery<O>> rkNNPair = new Pair<>(rkNNRefer, rkNNReach);

    return new Pair<>(kNNPair, rkNNPair);
  }
39867
public EqualsBuilder append(Object lhs, Object rhs) {
        if (!isEquals) {
            return this;
        }
        if (lhs == rhs) {
            return this;
        }
        if (lhs == null || rhs == null) {
            this.setEquals(false);
            return this;
        }
        Class<?> lhsClass = lhs.getClass();
        if (!lhsClass.isArray()) {
            if (lhs instanceof java.math.BigDecimal && rhs instanceof java.math.BigDecimal) {
                isEquals = (((java.math.BigDecimal) lhs).compareTo((java.math.BigDecimal) rhs) == 0);
            } else {
                // The simple case, not an array, just test the element
                isEquals = lhs.equals(rhs);
            }
        } else if (lhs.getClass() != rhs.getClass()) {
            // Here when we compare different dimensions, for example: a boolean[][] to a boolean[]
            this.setEquals(false);

        // 'Switch' on type of array, to dispatch to the correct handler
        // This handles multi dimensional arrays of the same depth
        } else if (lhs instanceof long[]) {
            append((long[]) lhs, (long[]) rhs);
        }
871504
def merge(self, rhs, lhs_filter=None, rhs_filter=None):
        
        if lhs_filter or rhs_filter:
            get_ = self.get___method
            set_ = self.set___method
            max_value = _madoka.Sketch_max_value(self)
            for table_id in range(self.depth):
                for cell_id in range(self.width):
                    lhs_val = get_(self, table_id, cell_id)
                    rhs_val = get_(rhs, table_id, cell_id)
                    if lhs_filter:
                        lhs_val = lhs_filter(lhs_val)
                    if rhs_filter:
                        rhs_val = rhs_filter(rhs_val)
                    if (lhs_val >= max_value) or (rhs_val >= (max_value - lhs_val)):
                        lhs_val = self.max_value
                    else:
                        lhs_val += rhs_val
                    set_(self, table_id, cell_id, lhs_val)
        else:
            self.merge_method(self, rhs)
            if rhs.k:
                for (key, val) in rhs.most_common(rhs.k):
                    self._update(key, val)
1110837
def add_query_kwargs(kwargs, visitor, constraints, index):
    
    (query_const, filter_const) = constraints.remove_index(index)
    kwargs["key_condition_expr"] = query_const.build(visitor)
    if filter_const:
        kwargs["filter"] = filter_const.build(visitor)
    if index.name != "TABLE":
        kwargs["index"] = index.name
1054618
function compareSizes(task) {
  var compressors = task.options.compress,
    newsizes = helpers.sizes(task, compressors),
    files = Object.keys(newsizes),
    sizecache = defaultCache,
    cache = helpers.get_cache(sizecache),
    tips = cache[""].tips,
    labels = helpers.sorted_labels(cache);

  // Obtain the current branch and continue...
  helpers.git_status(function(err, status) {
    var prefixes = compressors ? [""].concat(Object.keys(compressors)) : [""],
      commonHeader = prefixes.map(
        (label, i) => (i === 0 && compressors ? "raw" : label)
      );

    const tableOptions = {
      align: prefixes.map(() => "r").concat("l"),
      // eslint-disable-next-line no-control-regex
      stringLength: s => s.replace(/\x1B\[\d+m/g, "").length // Return a string, uncolored (suitable for testing .length, etc).
    };

    if (err) {
      console.warn(err);
      status = {};
    }

    let rows = [];
    rows.push(commonHeader.concat("Sizes"));

    // Raw sizes
    files.forEach(function(key) {
      rows.push(prefixes.map(prefix => newsizes[key][prefix]).concat(key + ""));
    });

    console.log(table(rows, tableOptions));

    // Comparisons
    labels.forEach(function(label) {
      var oldsizes = cache[label];

      // Skip metadata key and empty cache entries
      if (label === "" || !cache[label]) {
        return;
      }

      rows = [];

      // Header
      rows.push(
        commonHeader.concat("Compared to " + helpers.label(label, tips[label]))
      );

      // Data
      files.forEach(function(key) {
        var old = oldsizes && oldsizes[key];
        rows.push(
          prefixes
            .map(function(prefix) {
              return helpers.delta(old && newsizes[key][prefix] - old[prefix]);
            })
            .concat(key + "")
        );
      });

      console.log();
      console.log(table(rows, tableOptions));
    });

    // Update "last run" sizes
    cache[lastrun] = newsizes;

    // Remember if we're at a branch tip and the branch name is an available key
    if (
      status.branch &&
      !status.changed &&
      (status.branch in tips || !cache[status.branch])
    ) {
      tips[status.branch] = status.head;
      cache[status.branch] = newsizes;
      console.log("\nSaved as: " + status.branch);
    }

    // Write to file
    file.write(sizecache, JSON.stringify(cache));
  });
}
949656
def _from_sql(self, soql):
        """"""
        # pylint:disable=too-many-branches,too-many-nested-blocks
        assert not self.soql, "Don't use _from_sql method directly"
        self.soql = soql
        soql, self.subqueries = split_subquery(soql)
        match_parse = re.match(r'SELECT (.*) FROM (\w+)\b(.*)$', soql, re.I)
        if not match_parse:
            raise ProgrammingError('Invalid SQL: %s' % self.soql)
        fields_sql, self.root_table, self.extra_soql = match_parse.groups()
        fields = [x.strip() for x in fields_sql.split(',')]
        self.is_aggregation = bool(pattern_groupby.search(self.extra_soql) or
                                   pattern_aggregation.search(fields[0]))
        self.is_plain_count = fields[0].upper() == 'COUNT()'
        consumed_subqueries = 0
        expr_alias_counter = 0
        #
        if not self.is_plain_count:
            for field in fields:
                if self.is_aggregation:
                    match = re.search(r'\b\w+$', field)
                    if match:
                        alias = match.group()
                        assert alias not in RESERVED_WORDS, "invalid alias name"
                        if match.start() > 0 and field[match.start() - 1] == ' ':
                            field = field[match.start() - 1]
                    else:
                        alias = 'expr{}'.format(expr_alias_counter)
                        expr_alias_counter += 1
                    assert '&' not in field, "Subquery not expected as field in aggregation query"
                elif '&' in field:
                    assert field == '(&)'  # verify that the subquery was in parentheses
                    subquery = QQuery(self.subqueries[consumed_subqueries][0])
                    consumed_subqueries += 1
                    self.has_child_rel_field = True
                    field = subquery
                    # TODO more child relationships to the same table
                    alias = subquery.root_table
                else:
                    alias = field
                    if '.' in alias:
                        if alias.split('.', 1)[0].lower() == self.root_table.lower():
                            alias = alias.split('.', 1)[1]
                        if '.' in alias:
                            # prepare paths for possible empty outer joins
                            subroots = self.subroots
                            root_crumbs = alias.lower().split('.')[:-1]
                            for scrumb in root_crumbs:
                                subroots.setdefault(scrumb, {})
                                subroots = subroots[scrumb]
                self.aliases.append(alias)
                self.fields.append(field)
827657
@SuppressWarnings("unchecked")
    private void comparableDecreaseKey(Node<K, V> n, K newKey) {
        int c = ((Comparable<? super K>) newKey).compareTo(n.key);
        if (c > 0) {
            throw new IllegalArgumentException("Keys can only be decreased!");
        }
        n.key = newKey;
        if (c == 0) {
            return;
        }

        if (n.next == null) {
            throw new IllegalArgumentException("Invalid handle!");
        }

        // if not root and heap order violation
        Node<K, V> y = n.parent;
        if (y != null && ((Comparable<? super K>) n.key).compareTo(y.key) < 0) {
            cut(n, y);
            root.mark = false;
            cascadingRankChange(y);
            if (((Comparable<? super K>) n.key).compareTo(root.key) < 0) {
                root = link(root, n);
            } else {
                link(n, root);
            }
        }
    }
395264
protected Pair<KNNQuery<O>, KNNQuery<O>> getKNNQueries(Database database, Relation<O> relation, StepProgress stepprog) {
    KNNQuery<O> knnComp, knnReach;
    if(comparisonDistanceFunction == reachabilityDistanceFunction || comparisonDistanceFunction.equals(reachabilityDistanceFunction)) {
      LOG.beginStep(stepprog, 1, "Materializing neighborhoods with respect to reference neighborhood distance function.");
      knnComp = DatabaseUtil.precomputedKNNQuery(database, relation, comparisonDistanceFunction, MathUtil.max(kcomp, kreach) + 1);
      knnReach = knnComp;
    }
    else {
      LOG.beginStep(stepprog, 1, "Not materializing distance functions, since we request each DBID once only.");
      knnComp = QueryUtil.getKNNQuery(relation, comparisonDistanceFunction, kreach + 1);
      knnReach = QueryUtil.getKNNQuery(relation, reachabilityDistanceFunction, kcomp + 1);
    }
    return new Pair<>(knnComp, knnReach);
  }
253461
def _join_results(self, results, coarse, join):
    '''
    
    '''
    rval = OrderedDict()
    i_keys = set()
    for res in results:
      i_keys.update( res.keys() )
    for i_key in sorted(i_keys):
      if coarse:
        rval[i_key] = join( [res.get(i_key) for res in results] )
      else:
        rval[i_key] = OrderedDict()
        r_keys = set()
        for res in results:
          r_keys.update( res.get(i_key,{}).keys() )
        for r_key in sorted(r_keys):
          rval[i_key][r_key] = join( [res.get(i_key,{}).get(r_key) for res in results] )
    return rval
186859
def merge(ds, measurement, cardinality)

      m = ds.select {|k,v| k != :measurements and k != :errors and k != :warn }
      m = m.merge(measurement)
      m = m.merge ({ technology: "argos",
        type: type,
        cardinality: cardinality
        #file: "file://"+filename,
        #source: sha1
      })

      # if not ds[:errors].nil? and ds[:errors].any?
      #   m[:errors] = ds[:errors].clone
      # end
      #
      # if not ds[:warn].nil? and ds[:warn].any?
      #   m[:warn] = ds[:warn].clone
      # end
      #
      # if not m[:sensor_data].nil? and m[:sensor_data].size != ds[:sensors]
      #   if m[:warn].nil?
      #     m[:warn] = []
      #   end
      #   m[:warn] << "sensors-count-mismatch"
      # end

      # Create id as SHA1 hash of measurement minus stuff that may vary (like filename)
      #
      # Possible improvement for is to base id on a static list of keys
      # :program,
      # :platform,
      # :lines,
      # :sensors,
      # :satellite,
      # :lc,
      # :positioned,
      # :latitude,
      # :longitude,
      # :altitude,
      # :headers,
      # :measured,
      # :identical,
      # :sensor_data,
      # :technology,
      # :type,
      # :source

      idbase = m.clone
      idbase.delete :errors
      idbase.delete :file
      idbase.delete :warn

      id = Digest::SHA1.hexdigest(idbase.to_json)

      #m[:parser] = Argos.library_version
      m[:id] = id

      #m[:bundle] = bundle
      m
    end
895293
def _sql_expand_update(self, spec, key_prefix = '', col_prefix = ''):
        
        sql = ', '.join(col_prefix + key + ' = %(' + key_prefix + key + ')s' for key in spec)
        params = {}
        for key in spec:
            params[key_prefix + key] = spec[key]

        return sql, params