SortSpec.java

package jasper.repository.spec;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Nulls;
import jakarta.persistence.criteria.Order;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import jasper.domain.Plugin;
import jasper.domain.proj.Tag;
import jasper.errors.InvalidQueryException;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import static jasper.config.SQLiteConfig.isoDurationSeconds;
import static jasper.domain.Metadata.timestamp;
import static java.util.Arrays.stream;

/**
 * Sort and filter columns. Each column renders one SQL expression for both sorting and filtering, so one index serves both.
 * Used by entity-specific Spec classes (RefSpec, ExtSpec, UserSpec, PluginSpec, TemplateSpec).
 */
public class SortSpec {

	private static final Pattern ARRAY_INDEX_PATTERN = Pattern.compile("\\[(\\d+)]");
	/**
	 * False on PostgreSQL without PostGIS, where geo filters and sorts are rejected instead of failing in SQL.
	 */
	private static volatile boolean geoAvailable = true;
	private static final Pattern GEO_SORT_PATTERN = Pattern.compile(":geo\\((-?[0-9]{1,30}(?:\\.[0-9]{1,30})?);(-?[0-9]{1,30}(?:\\.[0-9]{1,30})?)\\)$");
	public static final String FIELD_REGEX = "(?:[A-Za-z0-9_$-]+(?:\\[[0-9]{1,9}\\])?|\\[[0-9]{1,9}\\])";
	/**
	 * A plugin sort column, e.g. "plugins->plugin/test->size:num".
	 */
	public static final String PLUGIN_COLUMN_REGEX = "plugins->" + Plugin.REGEX + "(?:->" + FIELD_REGEX + ")*(?::num|:len)?";
	/**
	 * A plugin spatial column, e.g. "plugins->plugin/geo:geo" or "plugins->plugin/place->location:geo".
	 */
	public static final String PLUGIN_GEO_COLUMN_REGEX = "plugins->" + Plugin.REGEX + "(?:->" + FIELD_REGEX + ")*:geo";
	/**
	 * A plugin column that can be indexed via hotPlugins in _config/index.
	 */
	public static final String HOT_PLUGIN_COLUMN_REGEX = "plugins->" + Plugin.REGEX + "(?:->" + FIELD_REGEX + ")*(?::num|:len|:geo)?";

	/**
	 * A sort or filter column, so both render the same SQL expression and one index serves both.
	 *
	 * @param expression the value: an Instant for dates, an Integer for lengths, a Double for numbers/distances,
	 *                   a JSONB expression for spatial filters, otherwise a String
	 * @param nullable missing values are null, so they sort first ascending and last descending (or last ascending
	 *                 when {@code nullsLast} is true, e.g. distance sorting for PostGIS GiST KNN index scans).
	 *                 This is the default null order on SQLite for nulls-first, so no NULLS clause is rendered there.
	 * @param nullsLast when true, missing values sort last ascending and first descending
	 */
	public record Column(Expression<?> expression, boolean nullable, boolean nullsLast) {

		public Column(Expression<?> expression, boolean nullable) {
			this(expression, nullable, false);
		}

		public Order order(CriteriaBuilder cb, boolean ascending) {
			if (nullable && nullsLast) return ascending ? cb.asc(expression, Nulls.LAST) : cb.desc(expression, Nulls.FIRST);
			if (nullable) return ascending ? cb.asc(expression, Nulls.FIRST) : cb.desc(expression, Nulls.LAST);
			return ascending ? cb.asc(expression) : cb.desc(expression);
		}

		/**
		 * Compares the column with a filter value. Missing values and values of the wrong type never match.
		 */
		@SuppressWarnings("unchecked")
		public Predicate filter(CriteriaBuilder cb, Filter filter) {
			if (filter.value() instanceof Filter.Bbox(var minLon, var minLat, var maxLon, var maxLat)) {
				requireGeo(filter.column());
				return cb.isTrue(cb.function("geo_bbox", Boolean.class,
					expression, cb.literal(minLon), cb.literal(minLat), cb.literal(maxLon), cb.literal(maxLat)));
			} else if (filter.value() instanceof Filter.Near(var lon, var lat, var meters)) {
				requireGeo(filter.column());
				return cb.isTrue(cb.function("geo_near", Boolean.class,
					expression, cb.literal(lon), cb.literal(lat), cb.literal(meters)));
			} else if (filter.value() instanceof Instant date) {
				Expression<Instant> expr = (Expression<Instant>) expression;
				return switch (filter.op()) {
					case BEFORE -> cb.lessThan(expr, date);
					case AFTER -> cb.greaterThan(expr, date);
					case EQUALS -> cb.equal(expr, date);
					default -> throw new IllegalStateException("Unsupported op: " + filter.op());
				};
			} else if (filter.value() instanceof String text) {
				Expression<String> expr = (Expression<String>) expression;
				return switch (filter.op()) {
					case BEFORE -> cb.lessThan(expr, text);
					case AFTER -> cb.greaterThan(expr, text);
					case EQUALS -> cb.equal(expr, text);
					default -> throw new IllegalStateException("Unsupported op: " + filter.op());
				};
			} else if (filter.value() instanceof Integer length) {
				Expression<Integer> expr = (Expression<Integer>) expression;
				return switch (filter.op()) {
					case BEFORE -> cb.lessThan(expr, cb.literal(length));
					case AFTER -> cb.greaterThan(expr, cb.literal(length));
					case EQUALS -> cb.equal(expr, cb.literal(length));
					default -> throw new IllegalStateException("Unsupported op: " + filter.op());
				};
			} else {
				Expression<Double> expr = (Expression<Double>) expression;
				var number = cb.function("cast_to_numeric", Double.class, cb.literal(filter.value()));
				return switch (filter.op()) {
					case BEFORE -> cb.lessThan(expr, number);
					case AFTER -> cb.greaterThan(expr, number);
					case EQUALS -> cb.equal(expr, number);
					default -> throw new IllegalStateException("Unsupported op: " + filter.op());
				};
			}
		}
	}

	/**
	 * A filter on a sort column: "{sort column}/{before|after|equals}/{value}",
	 * e.g. "modified/after/2024-01-01T00:00:00Z" or "plugins->plugin/test->size:num/before/10".
	 * Columns on Refs: "modified", "published", "created", "metadata->newResponse", "metadata->newReaction",
	 * "metadata->expandedTags", "metadata->responses", "metadata->internalResponses", response counts,
	 * vote sorts, plugin data, tag values, "tags:len", "sources:len", "alternateUrls:len" and "origin:len".
	 * Columns on Exts, Users, Plugins and Templates: "modified", "origin:len", "tag:len" and JSONB values.
	 * The value is always after the last "/", so it cannot contain "/", and the operator is the segment before it.
	 * Values cannot contain "->" so a missing value is not mistaken for a field.
	 * Values cannot contain "," since Spring splits a single list parameter on commas.
	 * The column decides the type of the value:
	 * <ul>
	 *     <li>"modified", "published" and "created": an ISO instant</li>
	 *     <li>"metadata->newResponse" and "metadata->newReaction": an ISO instant, compared with the timestamp text metadata stores</li>
	 *     <li>":len", "metadata->expandedTags", "metadata->responses" and "metadata->internalResponses": a length</li>
	 *     <li>":dur": an ISO duration</li>
	 *     <li>":num", vote sorts and response counts ("metadata->plugins->{plugin}"): a number</li>
	 *     <li>otherwise text</li>
	 * </ul>
	 *
	 * @param column the sort column, e.g. "plugins->plugin/test->size:num"
	 * @param value an Instant, String, Integer, or a BigDecimal for numbers and durations in seconds
	 */
	public record Filter(String column, Op op, Object value) {
		public enum Op { BEFORE, AFTER, EQUALS, BBOX, NEAR }
		public record Bbox(double minLon, double minLat, double maxLon, double maxLat) {}
		public record Near(double lon, double lat, double meters) {}
		/**
		 * The sort columns of Refs that can be filtered.
		 */
		public static final String REF_COLUMN_REGEX = "(?:modified|published|created|origin:len|(?:tags|sources|alternateUrls):len"
			+ "|metadata->(?:newResponse|newReaction)"
			+ "|metadata->(?:expandedTags|responses|internalResponses)(?::len)?"
			+ "|metadata->(?:plugins|remotePlugins)->" + Plugin.REGEX + "(?::num)?"
			+ "|plugins->plugin/user/vote:(?:top|score|decay)"
			+ "|" + PLUGIN_COLUMN_REGEX
			+ "|tags->" + Tag.REGEX + "(?::num|:dur)?)";
		/**
		 * The sort columns of Exts, Users, Plugins and Templates that can be filtered.
		 */
		public static final String TAG_COLUMN_REGEX = "(?:modified|origin:len|tag:len"
			+ "|(?:config|defaults|schema|external)(?:->" + FIELD_REGEX + ")+(?::num|:len)?)";
		/**
		 * The spatial columns of Exts, Users, Plugins and Templates that can be filtered.
		 */
		public static final String TAG_GEO_COLUMN_REGEX = "(?:config|defaults|schema|external)(?:->" + FIELD_REGEX + ")+:geo";
		public static final String VALUE_REGEX = "(?:(?!->)[^/,])+";
		public static final String OP_REGEX = "(?:before|after|equals)";
		public static final String GEO_OP_REGEX = "(?:bbox|near)";
		public static final String REF_REGEX = "(?:" + REF_COLUMN_REGEX + "/" + OP_REGEX + "|" + PLUGIN_GEO_COLUMN_REGEX + "/" + GEO_OP_REGEX + ")/" + VALUE_REGEX;
		public static final String TAG_REGEX = "(?:" + TAG_COLUMN_REGEX + "/" + OP_REGEX + "|" + TAG_GEO_COLUMN_REGEX + "/" + GEO_OP_REGEX + ")/" + VALUE_REGEX;
		public static final String REGEX = "(?:" + REF_COLUMN_REGEX + "|" + TAG_COLUMN_REGEX + ")/" + OP_REGEX + "/" + VALUE_REGEX
			+ "|(?:" + PLUGIN_GEO_COLUMN_REGEX + "|" + TAG_GEO_COLUMN_REGEX + ")/" + GEO_OP_REGEX + "/" + VALUE_REGEX;
		public static final int LEN = 1024;
		private static final String NUM_REGEX = "-?[0-9]{1,30}([.][0-9]{1,30})?";

		/**
		 * @return the parsed filter, or null if the value is not valid for the column
		 */
		public static Filter parse(String filter) {
			if (filter == null || !filter.matches(REGEX)) return null;
			var i = filter.lastIndexOf('/');
			var j = filter.lastIndexOf('/', i - 1);
			var column = filter.substring(0, j);
			var op = Op.valueOf(filter.substring(j + 1, i).toUpperCase());
			var value = filter.substring(i + 1);
			if (column.endsWith(":geo")) {
				if (op == Op.BBOX) {
					var parts = value.split(";", -1);
					if (parts.length != 4) return null;
					for (var p : parts) if (!p.matches(NUM_REGEX)) return null;
					var minLon = Double.parseDouble(parts[0]);
					var minLat = Double.parseDouble(parts[1]);
					var maxLon = Double.parseDouble(parts[2]);
					var maxLat = Double.parseDouble(parts[3]);
					if (!validCoord(minLon, minLat) || !validCoord(maxLon, maxLat) || minLon > maxLon || minLat > maxLat) return null;
					return new Filter(column, op, new Bbox(minLon, minLat, maxLon, maxLat));
				}
				if (op == Op.NEAR) {
					var parts = value.split(";", -1);
					if (parts.length != 3) return null;
					for (var p : parts) if (!p.matches(NUM_REGEX)) return null;
					var lon = Double.parseDouble(parts[0]);
					var lat = Double.parseDouble(parts[1]);
					var meters = Double.parseDouble(parts[2]);
					if (!validCoord(lon, lat) || !Double.isFinite(meters) || meters < 0) return null;
					return new Filter(column, op, new Near(lon, lat, meters));
				}
				return null;
			}
			try {
				if (column.equals("modified") || column.equals("published") || column.equals("created")) {
					return new Filter(column, op, Instant.parse(value));
				}
				if (column.equals("metadata->newResponse") || column.equals("metadata->newReaction")) {
					return new Filter(column, op, timestamp(Instant.parse(value)));
				}
			} catch (DateTimeParseException e) {
				return null;
			}
			if (column.endsWith(":len") || column.matches("metadata->(?:expandedTags|responses|internalResponses)")) {
				if (!value.matches("[0-9]{1,9}")) return null;
				return new Filter(column, op, Integer.valueOf(value));
			}
			if (column.endsWith(":dur")) {
				var seconds = isoDurationSeconds(value);
				if (seconds == null) return null;
				return new Filter(column, op, BigDecimal.valueOf(seconds));
			}
			if (column.endsWith(":num") || column.matches("plugins->plugin/user/vote:(?:top|score|decay)|metadata->(?:plugins|remotePlugins)->.*")) {
				if (!value.matches(NUM_REGEX)) return null;
				return new Filter(column, op, new BigDecimal(value));
			}
			return new Filter(column, op, value);
		}

		/**
		 * Parses the filter parameters.
		 *
		 * @throws InvalidQueryException if a value is not valid for its column
		 */
		public static List<Filter> parse(List<String> filters) {
			var result = new ArrayList<Filter>();
			if (filters == null) return result;
			for (var f : filters) {
				var filter = parse(f);
				if (filter == null) throw new InvalidQueryException(f, "expected {sort column}/{before|after|equals}/{value} with a value of the column type");
				result.add(filter);
			}
			return result;
		}
	}

	/**
	 * The column of a JSONB value, e.g. "config->field->subfield:num".
	 * Supports ":num" suffix for numeric values, ":len" suffix for array lengths and "[index]" for array elements.
	 * Missing values (or values of the wrong type for :num or :len) are null.
	 *
	 * @param property the sort column
	 * @param prefixes the JSONB columns that may be used, e.g. "config", "defaults", "schema"
	 * @return the column, or null if property doesn't match allowed prefixes
	 */
	public static Column jsonbColumn(Root<?> root, CriteriaBuilder cb, String property, String... prefixes) {
		var numericSort = property.endsWith(":num");
		var lengthSort = property.endsWith(":len");
		var geoMatcher = GEO_SORT_PATTERN.matcher(property);
		var geoSort = geoMatcher.find();
		double geoLon = 0, geoLat = 0;
		if (geoSort) {
			requireGeo(property);
			geoLon = Double.parseDouble(geoMatcher.group(1));
			geoLat = Double.parseDouble(geoMatcher.group(2));
			if (!validCoord(geoLon, geoLat)) throw new InvalidQueryException(property, "invalid geo coordinates");
			property = property.substring(0, geoMatcher.start());
		} else if (property.contains(":geo(")) {
			throw new InvalidQueryException(property, "geo sort requires coordinates :geo(lon;lat)");
		}
		var geoFilter = !geoSort && property.endsWith(":geo");
		if (geoFilter) property = property.substring(0, property.length() - ":geo".length());
		if (!geoSort && !geoFilter && property.contains(":")) property = property.substring(0, property.lastIndexOf(":"));
		var keepJsonb = numericSort || geoSort || geoFilter;
		var parts = property.split("->");
		if (parts.length < 2) return null;
		var jsonbFieldName = parts[0];
		if (stream(prefixes).noneMatch(jsonbFieldName::equals)) return null;

		Expression<?> expr = root.get(jsonbFieldName);
		for (int i = 1; i < parts.length; i++) {
			var field = parts[i];
			// Check for array index notation like "ids[0]"
			var matcher = ARRAY_INDEX_PATTERN.matcher(field);
			if (matcher.find()) {
				var fieldName = field.substring(0, matcher.start());
				var indexStr = matcher.group(1);
				if (indexStr == null || !indexStr.matches("\\d+")) throw new IllegalArgumentException("Invalid array index in field: '" + field + "'");
				var index = Integer.parseInt(indexStr);
				if (!fieldName.isEmpty()) {
					expr = cb.function("jsonb_object_field", Object.class, expr, cb.literal(fieldName));
				}
				if (i < parts.length - 1) {
					// Intermediate element - keep as JSONB for further traversal
					expr = cb.function("jsonb_array_element", Object.class, expr, cb.literal(index));
				} else if (lengthSort) {
					// Last element with length sort - get as JSONB and apply jsonb_array_length
					expr = cb.function("jsonb_array_element", Object.class, expr, cb.literal(index));
					return new Column(cb.function("safe_jsonb_array_length", Integer.class, expr), true);
				} else if (keepJsonb) {
					// Last element with numeric or geo sort/filter - keep as JSONB
					expr = cb.function("jsonb_array_element", Object.class, expr, cb.literal(index));
				} else {
					// Last element - get as text
					expr = cb.function("jsonb_array_element_text", String.class, expr, cb.literal(index));
				}
			} else if (i == parts.length - 1 && lengthSort) {
				// Last field with length sort - get as JSONB and apply jsonb_array_length
				expr = cb.function("jsonb_object_field", Object.class, expr, cb.literal(field));
				return new Column(cb.function("safe_jsonb_array_length", Integer.class, expr), true);
			} else if (i == parts.length - 1 && keepJsonb) {
				// Last field with numeric or geo sort/filter - keep as JSONB
				expr = cb.function("jsonb_object_field", Object.class, expr, cb.literal(field));
			} else if (i == parts.length - 1) {
				// Last field - get as text
				expr = cb.function("jsonb_object_field_text", String.class, expr, cb.literal(field));
			} else {
				// Intermediate field - get as JSONB object
				expr = cb.function("jsonb_object_field", Object.class, expr, cb.literal(field));
			}
		}
		if (numericSort) return new Column(cb.function("safe_numeric", Double.class, expr), true);
		if (geoSort) return new Column(cb.function("geo_distance", Double.class, expr, cb.literal(geoLon), cb.literal(geoLat)), true, true);
		return new Column(expr, true);
	}

	public static void setGeoAvailable(boolean available) {
		geoAvailable = available;
	}

	private static void requireGeo(String query) {
		if (!geoAvailable) throw new InvalidQueryException(query, "spatial queries require PostGIS");
	}

	private static boolean validCoord(double lon, double lat) {
		return Double.isFinite(lon) && Double.isFinite(lat)
			&& lon >= -180.0 && lon <= 180.0 && lat >= -90.0 && lat <= 90.0;
	}

	/**
	 * The column of an Ext, User, Plugin or Template: "modified", "origin:len", "tag:len" or a JSONB value.
	 *
	 * @param roots the JSONB columns of the entity, e.g. "config"
	 * @return the column, or null if this is not a computed column
	 */
	public static Column tagColumn(Root<?> root, CriteriaBuilder cb, String property, String... roots) {
		if (isJsonbSortProperty(property, roots)) return jsonbColumn(root, cb, property, roots);
		return switch (property) {
			case "modified" -> new Column(root.get("modified"), false);
			case "origin:len" -> new Column(cb.function("origin_nesting", Integer.class, root.get("origin")), false);
			case "tag:len" -> new Column(cb.function("tag_levels", Integer.class, root.get("tag")), false);
			default -> null;
		};
	}

	/**
	 * Orders an Ext, User, Plugin or Template by its sort columns.
	 * Missing JSONB values sort first ascending and last descending.
	 *
	 * @param roots the JSONB columns of the entity, e.g. "config"
	 */
	public static <T> Specification<T> sortTags(Specification<T> spec, Pageable pageable, String... roots) {
		if (pageable == null || pageable.getSort().isUnsorted()) {
			return spec;
		}
		var orders = pageable.getSort().toList();
		return spec.and((root, query, cb) -> {
			if (query.getResultType() == Long.class || query.getResultType() == long.class) {
				return null; // Don't apply ordering to count queries
			}
			var jpaOrders = new ArrayList<Order>();
			for (var order : orders) {
				if (order.getProperty().endsWith(":geo")) {
					throw new InvalidQueryException(order.getProperty(), "geo sort requires coordinates :geo(lon;lat)");
				}
				var column = tagColumn(root, cb, order.getProperty(), roots);
				if (column != null) {
					jpaOrders.add(column.order(cb, order.isAscending()));
				} else if (!isJsonbSortProperty(order.getProperty(), roots)) {
					jpaOrders.add(order.isAscending() ? cb.asc(root.get(order.getProperty())) : cb.desc(root.get(order.getProperty())));
				}
			}
			if (!jpaOrders.isEmpty()) query.orderBy(jpaOrders);
			return null;
		});
	}

	/**
	 * Filters an Ext, User, Plugin or Template on its sort columns.
	 *
	 * @param roots the JSONB columns of the entity, e.g. "config"
	 */
	public static <T> Specification<T> filterTags(Filter filter, String... roots) {
		return (root, query, cb) -> {
			var column = tagColumn(root, cb, filter.column(), roots);
			if (column == null) throw new InvalidQueryException(filter.column(), "not a sort column");
			return column.filter(cb, filter);
		};
	}

	/**
	 * A sort on the value of a tag in the tags array, e.g. "tags->plugin/progress:num".
	 *
	 * @param function the SQL function: tag_value, tag_value_num or tag_value_dur
	 * @param tag the tag prefix whose first sub-tag is the value
	 */
	public record TagValueSort(String function, String tag) {
		/**
		 * Parses "tags->{tag}", "tags->{tag}:num" or "tags->{tag}:dur". The "tags->" prefix is optional.
		 *
		 * @return the parsed sort, or null if the property is not a valid tag value sort
		 */
		public static TagValueSort parse(String property) {
			if (property == null) return null;
			if (property.startsWith("tags->")) property = property.substring("tags->".length());
			var function = "tag_value";
			if (property.endsWith(":num")) {
				function = "tag_value_num";
				property = property.substring(0, property.length() - ":num".length());
			} else if (property.endsWith(":dur")) {
				function = "tag_value_dur";
				property = property.substring(0, property.length() - ":dur".length());
			}
			if (!property.matches(Tag.REGEX)) return null;
			return new TagValueSort(function, property);
		}
	}

	/**
	 * Checks if a property is a JSONB sort property.
	 */
	public static boolean isJsonbSortProperty(String property, String... prefixes) {
		return stream(prefixes).anyMatch(p -> property.startsWith(p + "->"));
	}

	/**
	 * Creates a sort expression for the tag field with binary collation (COLLATE "C").
	 * This ensures ASCII ordering where '+' comes before '_'.
	 */
	public static Expression<String> createTagSortExpression(Root<?> root, CriteriaBuilder cb) {
		return cb.function("collate_c", String.class, root.get("tag"));
	}
}