<commit_before>var ErrorCodes = require("./ErrorCodes");
var Utilities  = require("./Utilities");

module.exports = AggregateFunctions;

function AggregateFunctions(opts) {
	if (typeof opts.driver.getQuery != "function") {
		throw ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "This driver does not support aggregate functions");
	}
	if (!Array.isArray(opts.driver.aggregate_functions)) {
		throw ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "This driver does not support aggregate functions");
	}

	var aggregates    = [ [] ];
	var group_by      = null;
	var used_distinct = false;

	var appendFunction = function (fun) {
		return function () {
			var args = (arguments.length && Array.isArray(arguments[0]) ? arguments[0] : Array.prototype.slice.apply(arguments));

			if (args.length > 0) {
				aggregates[aggregates.length - 1].push({ f: fun, a: args, alias: aggregateAlias(fun, args) });
				aggregates.push([]);
			} else {
				aggregates[aggregates.length - 1].push({ f: fun, alias: aggregateAlias(fun, args) });
			}

			if (fun == "distinct") {
				used_distinct = true;
			}

			return proto;
		};
	};
	var proto = {
		groupBy: function () {
			group_by = Array.prototype.slice.apply(arguments);
			return this;
		},
		limit: function (offset, limit) {
			if (typeof limit == "number") {
				opts.limit = [ offset, limit ];
			} else {
				opts.limit = [ 0, offset ]; // offset = limit
			}
			return this;
		},
		order: function () {
			opts.order = Utilities.standardizeOrder(Array.prototype.slice.apply(arguments));
			return this;
		},
		select: function () {
			if (arguments.length === 0) {
				throw ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "When using append you must at least define one property");
			}
			opts.properties = opts.properties.concat(Array.isArray(arguments[0]) ?
			                                                 arguments[0] :
			                                                 Array.prototype.slice.apply(arguments));
			return this;
		},
		as: function (alias) {
			if (aggregates.length === 0) {
				throw ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "No aggregate functions defined yet");
			}

			var len = aggregates.length;

			aggregates[len - 1][aggregates[len - 1].length - 1].alias = alias;

			return this;
		},
		call: function (fun, args) {
			if (args.length > 0) {
				aggregates[aggregates.length - 1].push({ f: fun, a: args, alias: aggregateAlias(fun, args) });<commit_msg>Fixes Model.aggregate().call() to accept no arguments except function name<commit_after>var ErrorCodes = require("./ErrorCodes");
var Utilities  = require("./Utilities");

module.exports = AggregateFunctions;

function AggregateFunctions(opts) {
	if (typeof opts.driver.getQuery != "function") {
		throw ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "This driver does not support aggregate functions");
	}
	if (!Array.isArray(opts.driver.aggregate_functions)) {
		throw ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "This driver does not support aggregate functions");
	}

	var aggregates    = [ [] ];
	var group_by      = null;
	var used_distinct = false;

	var appendFunction = function (fun) {
		return function () {
			var args = (arguments.length && Array.isArray(arguments[0]) ? arguments[0] : Array.prototype.slice.apply(arguments));

			if (args.length > 0) {
				aggregates[aggregates.length - 1].push({ f: fun, a: args, alias: aggregateAlias(fun, args) });
				aggregates.push([]);
			} else {
				aggregates[aggregates.length - 1].push({ f: fun, alias: aggregateAlias(fun, args) });
			}

			if (fun == "distinct") {
				used_distinct = true;
			}

			return proto;
		};
	};
	var proto = {
		groupBy: function () {
			group_by = Array.prototype.slice.apply(arguments);
			return this;
		},
		limit: function (offset, limit) {
			if (typeof limit == "number") {
				opts.limit = [ offset, limit ];
			} else {
				opts.limit = [ 0, offset ]; // offset = limit
			}
			return this;
		},
		order: function () {
			opts.order = Utilities.standardizeOrder(Array.prototype.slice.apply(arguments));
			return this;
		},
		select: function () {
			if (arguments.length === 0) {
				throw ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "When using append you must at least define one property");
			}
			opts.properties = opts.properties.concat(Array.isArray(arguments[0]) ?
			                                                 arguments[0] :
			                                                 Array.prototype.slice.apply(arguments));
			return this;
		},
		as: function (alias) {
			if (aggregates.length === 0) {
				throw ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "No aggregate functions defined yet");
			}

			var len = aggregates.length;

			aggregates[len - 1][aggregates[len - 1].length - 1].alias = alias;

			return this;
		},
		call: function (fun, args) {
			if (args && args.length > 0) {
				aggregates[aggregates.length - 1].push({ f: fun, a: args, alias: aggregateAlias(fun, args) });
{'fraction_non_alphanumeric': 0.1382358850713281, 'fraction_numerical': 0.006027727546714889, 'mean_word_length': 3.1106523534269197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2327878', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1495, 'n_words': 363}