{"record":{"id":"065bc0ef306f3112","repo":"hibernate/hibernate-orm","slug":"singlestore-json-array-insert-function-requires-at","errorCode":null,"errorMessage":"SingleStore json_array_insert function requires at least one json path element","messagePattern":"SingleStore json_array_insert function requires at least one json path element","errorType":"exception","errorClass":"QueryException","httpStatus":null,"severity":"error","filePath":"hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/function/json/SingleStoreJsonArrayInsertFunction.java","lineNumber":76,"sourceCode":"\t\tvalue.accept( translator );\n\t\tsqlAppender.appendSql( \") ELSE \" );\n\t\tbuildJsonArrayInsertValue( sqlAppender, value );\n\t\tsqlAppender.appendSql( \"json_build_array(json_extract_json(\" );\n\t\tjson.accept( translator );\n\t\tbuildJsonPath( sqlAppender, jsonPath, jsonPathElements );\n\t\tsqlAppender.appendSql( \")),\" );\n\t\tsqlAppender.appendSql( arrayIndex );\n\t\tsqlAppender.appendSql( \", 0, \" );\n\t\tvalue.accept( translator );\n\t\tsqlAppender.appendSql( \") END\" );\n\t\tif ( jsonPathElements.size() > 1 ) {\n\t\t\tsqlAppender.appendSql( ')' );\n\t\t}\n\t}\n\n\tprivate static int getArrayIndex(List<JsonPathHelper.JsonPathElement> jsonPathElements) {\n\t\tif ( jsonPathElements.isEmpty() ) {\n\t\t\tthrow new QueryException( \"SingleStore json_array_insert function requires at least one json path element\" );\n\t\t}\n\t\tJsonPathHelper.JsonPathElement lastPathElement = jsonPathElements.get( jsonPathElements.size() - 1 );\n\t\tif ( !( lastPathElement instanceof JsonPathHelper.JsonIndexAccess ) ) {\n\t\t\tthrow new QueryException(\n\t\t\t\t\t\"SingleStore json_array_insert function last path parameter must be an array index element\" );\n\t\t}\n\t\treturn ( (JsonPathHelper.JsonIndexAccess) lastPathElement ).index();\n\t}\n\n\tprivate static boolean isNumeric(SqlAstNode value) {\n\t\treturn value instanceof UnparsedNumericLiteral<?>;\n\t}\n\n\tprivate static void buildJsonArrayInsertValue(SqlAppender sqlAppender, SqlAstNode value) {\n\t\tsqlAppender.appendSql( \"json_splice_\" );\n\t\tsqlAppender.appendSql( isNumeric( value ) ? \"double(\" : \"string(\" );\n\t}\n","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/function/json/SingleStoreJsonArrayInsertFunction.java#L58-L94","documentation":"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.","triggerScenarios":"HQL `json_array_insert(e.doc, '$', :v)` — a path with no attribute or index segments — executed on SingleStoreDialect.","commonSituations":"Reusing code written for databases where inserting at the document root is legal; treating json_array_insert as an append function.","solutions":["Target a concrete array element, e.g. json_array_insert(e.doc, 'items[0]', :v)","Use json_array_append when the intent is to append at the end of an array","Modify the JSON document in Java and persist the whole document"],"exampleFix":"// before\nupdate E e set e.doc = json_array_insert(e.doc, '$', :v)\n\n// after\nupdate E e set e.doc = json_array_insert(e.doc, 'items[0]', :v)","handlingStrategy":"validation","validationCode":"// json_array_insert needs at least one path element on SingleStore\nstatic boolean validInsertPath(String path) {\n    return path != null && !path.isBlank() && !\"$\".equals(path.trim());\n}","typeGuard":null,"tryCatchPattern":"try {\n    em.createQuery(hql).executeUpdate(); // path '$'\n} catch (QueryException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"at least one json path element\")) {\n        // point the path at a concrete array element or use json_array_append\n    }\n    throw e;\n}","preventionTips":["Treat '$' as an invalid json_array_insert path in shared query builders","Use json_array_append for append semantics; reserve insert for a concrete [n] position","Unit-test path generation utilities against SingleStore rules"],"tags":["hibernate","singlestore","json-array-insert","json-path"],"backgroundTag":"json-array-insert-path-invalid","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}