hibernate/hibernate-orm · error · QueryException

SingleStore json_array_insert function requires at least one

Error message

SingleStore json_array_insert function requires at least one json path element

What it means

SingleStore's json_array_insert is emulated with json_splice, which needs an explicit insertion position taken from the path. getArrayIndex therefore requires at least one path element; a path of just '$' (zero elements after the root) leaves the insertion point undefined and render() throws QueryException.

Source

Thrown at hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/function/json/SingleStoreJsonArrayInsertFunction.java:76

		value.accept( translator );
		sqlAppender.appendSql( ") ELSE " );
		buildJsonArrayInsertValue( sqlAppender, value );
		sqlAppender.appendSql( "json_build_array(json_extract_json(" );
		json.accept( translator );
		buildJsonPath( sqlAppender, jsonPath, jsonPathElements );
		sqlAppender.appendSql( "))," );
		sqlAppender.appendSql( arrayIndex );
		sqlAppender.appendSql( ", 0, " );
		value.accept( translator );
		sqlAppender.appendSql( ") END" );
		if ( jsonPathElements.size() > 1 ) {
			sqlAppender.appendSql( ')' );
		}
	}

	private static int getArrayIndex(List<JsonPathHelper.JsonPathElement> jsonPathElements) {
		if ( jsonPathElements.isEmpty() ) {
			throw new QueryException( "SingleStore json_array_insert function requires at least one json path element" );
		}
		JsonPathHelper.JsonPathElement lastPathElement = jsonPathElements.get( jsonPathElements.size() - 1 );
		if ( !( lastPathElement instanceof JsonPathHelper.JsonIndexAccess ) ) {
			throw new QueryException(
					"SingleStore json_array_insert function last path parameter must be an array index element" );
		}
		return ( (JsonPathHelper.JsonIndexAccess) lastPathElement ).index();
	}

	private static boolean isNumeric(SqlAstNode value) {
		return value instanceof UnparsedNumericLiteral<?>;
	}

	private static void buildJsonArrayInsertValue(SqlAppender sqlAppender, SqlAstNode value) {
		sqlAppender.appendSql( "json_splice_" );
		sqlAppender.appendSql( isNumeric( value ) ? "double(" : "string(" );
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Target a concrete array element, e.g. json_array_insert(e.doc, 'items[0]', :v)
  2. Use json_array_append when the intent is to append at the end of an array
  3. Modify the JSON document in Java and persist the whole document

Example fix

// before
update E e set e.doc = json_array_insert(e.doc, '$', :v)

// after
update E e set e.doc = json_array_insert(e.doc, 'items[0]', :v)
Defensive patterns

Strategy: validation

Validate before calling

// json_array_insert needs at least one path element on SingleStore
static boolean validInsertPath(String path) {
    return path != null && !path.isBlank() && !"$".equals(path.trim());
}

Try / catch

try {
    em.createQuery(hql).executeUpdate(); // path '$'
} catch (QueryException e) {
    if (e.getMessage() != null && e.getMessage().contains("at least one json path element")) {
        // point the path at a concrete array element or use json_array_append
    }
    throw e;
}

Prevention

When it happens

Trigger: HQL `json_array_insert(e.doc, '$', :v)` — a path with no attribute or index segments — executed on SingleStoreDialect.

Common situations: Reusing code written for databases where inserting at the document root is legal; treating json_array_insert as an append function.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/065bc0ef306f3112. Report an issue: GitHub.