Skip to content

Add support for fluent query result conversion #1568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mp911de opened this issue Apr 16, 2025 · 0 comments
Open

Add support for fluent query result conversion #1568

mp911de opened this issue Apr 16, 2025 · 0 comments
Assignees
Labels
type: enhancement A general enhancement

Comments

@mp911de
Copy link
Member

mp911de commented Apr 16, 2025

When selecting data from a query (e.g. SELECT id,comment,language,similarity_cosine(embedding,:embedding) AS score FROM comments) that returns an entity and auxiliary information (such as a score), it makes sense to capture materialize the entity independently from the score.

While it would be possible to create a dedicated data structure for this projection result, it is much leaner to reuse existing data models and customize the result mapping at the stage where CassandraTemplate already has access to the Row.

Something along the lines of:

/**
 * Trigger {@code SELECT} query execution by calling one of the terminating methods and return mapped results.
 */
interface TerminatingResults<T> {

	<R> TerminatingResults<R> map(QueryResultConverter<? super T, ? extends R> converter);
	default Optional<T> first() {
		return Optional.ofNullable(firstValue());
	}

	@Nullable
	T firstValue();
	// …

The actual converter needs to be tied to the Row and upstream result produced by a converter:

interface QueryResultConverter<T, R> {

	/**
	 * Returns a function that returns the materialized entity..
	 */
	@SuppressWarnings("unchecked")
	static <T> QueryResultConverter<T, T> entity() {
		return (row, result) -> result.get();
	}

	R mapRow(Row row, ConversionResultSupplier<T> reader);


	/**
	 * A supplier that converts a {@link Row} into {@code T}. Allows for lazy reading of query results.

	 */
	interface ConversionResultSupplier<T> {

		/**
		 * Obtain the upstream conversion result.
		 *
		 * @return the upstream conversion result.
		 */
		T get();

	}

}

Open issues:

  • Find a better name for ConversionResultSupplier.
  • Supplier pattern allows Lazy caching to buffer multiple calls to get, however, a UpstreamConverter.convert(Row) might be more explicit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant