MyCATApache/Mycat-Server · error · RuntimeException

like SQL error

Error message

like SQL error

What it means

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.

Solutions

  1. Replace the right side of LIKE with a literal string pattern, e.g. `name LIKE 'abc%'`, so getExpValue succeeds.
  2. Ensure bound parameters used in LIKE are non-null strings before executing the query.
  3. Rewrite the pattern using REGEXP (if the dialect supports it) or perform the regex filtering in application code.
  4. When patching, chain the original exception (new RuntimeException("like SQL error", e)) so the real cause is visible.

Example fix

// before
SELECT * FROM users WHERE name LIKE CONCAT('%', ?);

// after
SELECT * FROM users WHERE name LIKE '%smith%';
Defensive patterns

Strategy: validation

Validate before calling

if (rightOperand == null || !(rightOperand instanceof String)) {
    throw new IllegalArgumentException("LIKE pattern must be a non-null literal string for MongoDB backend");
}

Try / catch

try {
    runQuery(sql);
} catch (RuntimeException e) {
    if ("like SQL error".equals(e.getMessage())) {
        // fall back to literal LIKE pattern or client-side filtering
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/6b7a9e7021a1e23d. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/backend/jdbc/mongodb/MongoSQLParser.java:402

				parserWhere(expr.getRight(),o);
			}
			else if (expr.getOperator().getName().equals("OR")) {
				orWhere(expr.getLeft(),expr.getRight(),o);
			} else {
				SQLExpr exprL=expr.getLeft();
				if (!(exprL instanceof SQLBinaryOpExpr)){
					if (expr.getOperator().getName().equals("=")) {
						o.put(exprL.toString(), getExpValue(expr.getRight()));

					}else if(("like").equals(expr.getOperator().getName().toLowerCase())){
						//处理like以及正则转换
						String likeString="";
						try{
							likeString=("%"+String.valueOf(getExpValue(expr.getRight()))+"%")
									.replace("%%","")
									.replace("%","^");
						}catch (Exception e){
							throw new RuntimeException("like SQL error");
						}

						parserDBObject(o,exprL.toString(),"$regex", likeString);

					} else {
						String op="";
						if (expr.getOperator().getName().equals("<")) {
							op = "$lt";
						}
						if (expr.getOperator().getName().equals("<=")) {
							op = "$lte";
						}
						if (expr.getOperator().getName().equals(">")) {
							op = "$gt";
						}
						if (expr.getOperator().getName().equals(">=")) {
							op = "$gte";
						}

View on GitHub (pinned to 65f8d8beb7)