{"record":{"id":"6b7a9e7021a1e23d","repo":"MyCATApache/Mycat-Server","slug":"like-sql-error","errorCode":null,"errorMessage":"like SQL error","messagePattern":"like SQL error","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/backend/jdbc/mongodb/MongoSQLParser.java","lineNumber":402,"sourceCode":"\t\t\t\tparserWhere(expr.getRight(),o);\r\n\t\t\t}\r\n\t\t\telse if (expr.getOperator().getName().equals(\"OR\")) {\r\n\t\t\t\torWhere(expr.getLeft(),expr.getRight(),o);\r\n\t\t\t} else {\r\n\t\t\t\tSQLExpr exprL=expr.getLeft();\r\n\t\t\t\tif (!(exprL instanceof SQLBinaryOpExpr)){\r\n\t\t\t\t\tif (expr.getOperator().getName().equals(\"=\")) {\r\n\t\t\t\t\t\to.put(exprL.toString(), getExpValue(expr.getRight()));\r\n\r\n\t\t\t\t\t}else if((\"like\").equals(expr.getOperator().getName().toLowerCase())){\r\n\t\t\t\t\t\t//处理like以及正则转换\r\n\t\t\t\t\t\tString likeString=\"\";\r\n\t\t\t\t\t\ttry{\r\n\t\t\t\t\t\t\tlikeString=(\"%\"+String.valueOf(getExpValue(expr.getRight()))+\"%\")\r\n\t\t\t\t\t\t\t\t\t.replace(\"%%\",\"\")\r\n\t\t\t\t\t\t\t\t\t.replace(\"%\",\"^\");\r\n\t\t\t\t\t\t}catch (Exception e){\r\n\t\t\t\t\t\t\tthrow new RuntimeException(\"like SQL error\");\r\n\t\t\t\t\t\t}\r\n\r\n\t\t\t\t\t\tparserDBObject(o,exprL.toString(),\"$regex\", likeString);\r\n\r\n\t\t\t\t\t} else {\r\n\t\t\t\t\t\tString op=\"\";\r\n\t\t\t\t\t\tif (expr.getOperator().getName().equals(\"<\")) {\r\n\t\t\t\t\t\t\top = \"$lt\";\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tif (expr.getOperator().getName().equals(\"<=\")) {\r\n\t\t\t\t\t\t\top = \"$lte\";\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tif (expr.getOperator().getName().equals(\">\")) {\r\n\t\t\t\t\t\t\top = \"$gt\";\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tif (expr.getOperator().getName().equals(\">=\")) {\r\n\t\t\t\t\t\t\top = \"$gte\";\r\n\t\t\t\t\t\t}\r","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/backend/jdbc/mongodb/MongoSQLParser.java#L384-L420","documentation":"In MongoSQLParser.parserWhere(), when a SQL LIKE predicate is translated to MongoDB, the library builds a regex by wrapping the right-hand value in '%' and converting '%' to '^'. If evaluating the right-hand expression (getExpValue) throws any exception, it is replaced by a RuntimeException('like SQL error'). This masks the real cause — usually a non-constant or null right-hand operand that cannot be turned into a string.","triggerScenarios":"A WHERE clause containing `col LIKE <expr>` where getExpValue(expr.getRight()) throws — e.g. the right side is a subquery, an unresolvable column, a function call the parser cannot evaluate, or a null literal. Any LIKE predicate whose right operand cannot be coerced to a string.","commonSituations":"Queries like `name LIKE CONCAT('%', ?)` or `col LIKE other_col` pushed down to MongoDB through MyCat; parameter binding issues where the placeholder evaluates to null; SQL generated by ORMs using expressions MyCat's simple expression evaluator does not support on the right side of LIKE.","solutions":["Replace the right side of LIKE with a literal string pattern, e.g. `name LIKE 'abc%'`, so getExpValue succeeds.","Ensure bound parameters used in LIKE are non-null strings before executing the query.","Rewrite the pattern using REGEXP (if the dialect supports it) or perform the regex filtering in application code.","When patching, chain the original exception (new RuntimeException(\"like SQL error\", e)) so the real cause is visible."],"exampleFix":"// before\nSELECT * FROM users WHERE name LIKE CONCAT('%', ?);\n\n// after\nSELECT * FROM users WHERE name LIKE '%smith%';","handlingStrategy":"validation","validationCode":"if (rightOperand == null || !(rightOperand instanceof String)) {\n    throw new IllegalArgumentException(\"LIKE pattern must be a non-null literal string for MongoDB backend\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    runQuery(sql);\n} catch (RuntimeException e) {\n    if (\"like SQL error\".equals(e.getMessage())) {\n        // fall back to literal LIKE pattern or client-side filtering\n    } else {\n        throw e;\n    }\n}","preventionTips":["Use string literal patterns on the right side of LIKE.","Sanitize bound parameters (non-null strings) before queries with LIKE.","Avoid function calls and column references inside LIKE patterns when pushing to MongoDB."],"tags":["sql","mongodb","like-operator","regex"],"backgroundTag":"invalid-argument-format","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}