Doris json_contains 查询报错
### SQL: SELECT count(0) FROM or_sales_info WHERE deleted = 0 AND (order_code IN (?, ?) AND shop_company_name = ? AND business_type IN (?) AND status = ? AND create_time >= ? AND create_time < ? AND is_confirm_after_sales = ? AND (JSON_CONTAINS(order_labels, '1') AND JSON_CONTAINS(order_labels, '4')))
### Cause: java.sql.SQLException: errCode = 2, detailMessage = No matching function with signature: json_contains(JSON, VARCHAR(65533)).
; uncategorized SQLException; SQL state [HY000]; error code [1105]; errCode = 2, detailMessage = No matching function with signature: json_contains(JSON, VARCHAR(65533)).; nested exception is java.sql.SQLException: errCode = 2, detailMessage = No matching function with signature: json_contains(JSON, VARCHAR(65533)).]
org.springframework.jdbc.UncategorizedSQLException:
### Error querying database. Cause: java.sql.SQLException: errCode = 2, detailMessage = No matching function with signature: json_contains(JSON, VARCHAR(65533)).
### The error may exist in com/cloud/dw/mapper/oms/SalesInfoMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT count(0) FROM or_sales_info WHERE deleted = 0 AND (order_code IN (?, ?) AND shop_company_name = ? AND business_type IN (?) AND status = ? AND create_time >= ? AND create_time < ? AND is_confirm_after_sales = ? AND (JSON_CONTAINS(order_labels, '1') AND JSON_CONTAINS(order_labels, '4')))
### Cause: java.sql.SQLException: errCode = 2, detailMessage = No matching function with signature: json_contains(JSON, VARCHAR(65533)).
; uncategorized SQLException; SQL state [HY000]; error code [1105]; errCode = 2, detailMessage = No matching function with signature: json_contains(JSON, VARCHAR(65533)).; nested exception is java.sql.SQLException: errCode = 2, detailMessage = No matching function with signature: json_contains(JSON, VARCHAR(65533)).at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92)at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)at com.sun.proxy.$Proxy155.selectList(Unknown Source)
根据错误信息,Doris数据库的JSON_CONTAINS函数签名不匹配。Doris要求第二个参数也是JSON格式,而不是字符串。需要对代码进行改造以兼容Doris的JSON函数语法。
查询sql
select * from or_sales_info where 1=1
and JSON_CONTAINS(order_labels, CAST('4' AS JSON))
and JSON_CONTAINS(order_labels, CAST('1' AS JSON))
and create_time between '2025-05-01 00:00:00' and '2025-05-30 23:59:59';
代码
private static <T> Wrapper<T> generateJsonArrayFieldToWrapper(final Field field, final QueryCondition cond, final Object value, final Boolean isOr, final Boolean isDoris) {QueryWrapper<T> wrapper = Wrappers.query();String customSubSql = "JSON_CONTAINS({}, '{}')";if (Boolean.TRUE.equals(isDoris)) {customSubSql = "JSON_CONTAINS({}, CAST('{}' AS JSON))";}// 判断是否为数组if (value.getClass().isArray()) {Class<?> componentType = value.getClass().getComponentType();if (componentType.equals(String.class)) {customSubSql = "JSON_CONTAINS({}, '\"{}\"')";if (Boolean.TRUE.equals(isDoris)) {customSubSql = "JSON_CONTAINS({}, CAST('\"{}\"' AS JSON))";}}// 获取数组的值for (int i = 0; i < Array.getLength(value); i++) {applyJsonArrayCondition(wrapper, cond, Array.get(value, i), customSubSql, isOr);}} else if (value instanceof Collection) {Type genricType = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];if (genricType.equals(String.class)) {customSubSql = "JSON_CONTAINS({}, '\"{}\"')";if (Boolean.TRUE.equals(isDoris)) {customSubSql = "JSON_CONTAINS({}, CAST('\"{}\"' AS JSON))";}}for (Object element : (Collection<?>) value) {applyJsonArrayCondition(wrapper, cond, element, customSubSql, isOr);}}return wrapper;}
private static <T> void applyJsonArrayCondition(QueryWrapper<T> wrapper, QueryCondition cond, Object element, String customSubSql, Boolean isOr) {if (isOr) {wrapper.or();}wrapper.apply(StrUtil.format(customSubSql, cond.getFiledName(), element));}