|
34 | 34 | import org.springframework.util.CollectionUtils;
|
35 | 35 |
|
36 | 36 | import java.util.*;
|
| 37 | +import java.util.function.Function; |
| 38 | +import java.util.function.UnaryOperator; |
37 | 39 | import java.util.stream.Collectors;
|
38 | 40 |
|
39 | 41 |
|
@@ -292,7 +294,7 @@ private SqlNode filterSqlNode(FilterOperator operator) {
|
292 | 294 |
|
293 | 295 | SqlNode[] sqlNodes = null;
|
294 | 296 |
|
295 |
| - org.apache.calcite.sql.SqlOperator sqlOp = null; |
| 297 | + SqlOperator sqlOp = null; |
296 | 298 | switch (operator.getSqlOperator()) {
|
297 | 299 | case IN:
|
298 | 300 | sqlOp = SqlStdOperatorTable.IN;
|
@@ -327,19 +329,31 @@ private SqlNode filterSqlNode(FilterOperator operator) {
|
327 | 329 | sqlNodes = new SqlNode[]{column, nodes.get(0)};
|
328 | 330 | break;
|
329 | 331 | case LIKE:
|
330 |
| - operator.getValues()[0].setValue("%" + operator.getValues()[0].getValue() + "%"); |
331 |
| - sqlOp = SqlStdOperatorTable.LIKE; |
332 |
| - sqlNodes = new SqlNode[]{column, convertTypedValue(operator.getValues()[0])}; |
| 332 | + if (operator.getValues().length == 1) { |
| 333 | + sqlOp = SqlStdOperatorTable.LIKE; |
| 334 | + sqlNodes = getOperands(column, operator.getValues()[0], v -> "%" + v + "%"); |
| 335 | + } else if (operator.getValues().length > 1) { |
| 336 | + sqlOp = SqlStdOperatorTable.OR; |
| 337 | + sqlNodes = Arrays.stream(operator.getValues()).map(newSqlBasicCall(column, v -> "%" + v + "%")).toArray(SqlNode[]::new); |
| 338 | + } |
333 | 339 | break;
|
334 | 340 | case PREFIX_LIKE:
|
335 |
| - operator.getValues()[0].setValue(operator.getValues()[0].getValue() + "%"); |
336 |
| - sqlOp = SqlStdOperatorTable.LIKE; |
337 |
| - sqlNodes = new SqlNode[]{column, convertTypedValue(operator.getValues()[0])}; |
| 341 | + if (operator.getValues().length == 1) { |
| 342 | + sqlOp = SqlStdOperatorTable.LIKE; |
| 343 | + sqlNodes = getOperands(column, operator.getValues()[0], v -> v + "%"); |
| 344 | + } else if (operator.getValues().length > 1) { |
| 345 | + sqlOp = SqlStdOperatorTable.OR; |
| 346 | + sqlNodes = Arrays.stream(operator.getValues()).map(newSqlBasicCall(column, v -> v + "%")).toArray(SqlNode[]::new); |
| 347 | + } |
338 | 348 | break;
|
339 | 349 | case SUFFIX_LIKE:
|
340 |
| - operator.getValues()[0].setValue("%" + operator.getValues()[0].getValue()); |
341 |
| - sqlOp = SqlStdOperatorTable.LIKE; |
342 |
| - sqlNodes = new SqlNode[]{column, convertTypedValue(operator.getValues()[0])}; |
| 350 | + if (operator.getValues().length == 1) { |
| 351 | + sqlOp = SqlStdOperatorTable.LIKE; |
| 352 | + sqlNodes = getOperands(column, operator.getValues()[0], v -> "%" + v); |
| 353 | + } else if (operator.getValues().length > 1) { |
| 354 | + sqlOp = SqlStdOperatorTable.OR; |
| 355 | + sqlNodes = Arrays.stream(operator.getValues()).map(newSqlBasicCall(column, v -> "%" + v)).toArray(SqlNode[]::new); |
| 356 | + } |
343 | 357 | break;
|
344 | 358 | case NOT_LIKE:
|
345 | 359 | operator.getValues()[0].setValue("%" + operator.getValues()[0].getValue() + "%");
|
@@ -384,6 +398,14 @@ private SqlNode filterSqlNode(FilterOperator operator) {
|
384 | 398 | return new SqlBasicCall(sqlOp, sqlNodes, SqlParserPos.ZERO);
|
385 | 399 | }
|
386 | 400 |
|
| 401 | + private Function<SingleTypedValue, SqlBasicCall> newSqlBasicCall(SqlNode column, UnaryOperator<Object> formater) { |
| 402 | + return value -> new SqlBasicCall(SqlStdOperatorTable.LIKE, getOperands(column, value, formater), SqlParserPos.ZERO); |
| 403 | + } |
| 404 | + |
| 405 | + private SqlNode[] getOperands(SqlNode column, SingleTypedValue value, UnaryOperator<Object> formater) { |
| 406 | + return new SqlNode[]{column, convertTypedValue(new SingleTypedValue(formater.apply(value.getValue()), value.getValueType()))}; |
| 407 | + } |
| 408 | + |
387 | 409 | /**
|
388 | 410 | * parse function column ,and register the column functions
|
389 | 411 | *
|
|
0 commit comments