Skip to content

Commit 0045add

Browse files
authored
fix: 优化contains_all方法中,参数为空时的判断 (#29)
* fix: 优化嵌套集合的匹配 * fix: 补充单元测试 * fix: 优化contains_all方法中,参数为空时的判断 contains_all参数为null时,应该返回false * fix: 优化空数组的判断 * fix: 优化
1 parent 075f9c7 commit 0045add

2 files changed

Lines changed: 96 additions & 2 deletions

File tree

src/main/java/org/jetlinks/reactor/ql/supports/DefaultReactorQLMetadata.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,24 @@ static <T> void createCalculator(BiFunction<String, BiFunction<Number, Number, O
312312
"contains_all",
313313
999,
314314
2,
315-
stream -> containsHandler
316-
.apply(stream, (left, data) -> data.all(val -> handleContain(left, val)))));
315+
stream -> CastUtils
316+
.handleFirst(stream, (first, flux) -> {
317+
TreeSet<Object> set =
318+
CastUtils.castCollection(first, new TreeSet<>(CompareUtils::compare));
319+
320+
return flux
321+
.skip(1)
322+
.concatMap(val -> Flux
323+
.just(val)
324+
.as(CastUtils::flatStream)
325+
.map(_val -> handleContain(set, _val))
326+
// 如果是空数组,则contains_all结果为true
327+
.defaultIfEmpty(true))
328+
// 参数为空,返回false
329+
.defaultIfEmpty(false)
330+
.all(Boolean::booleanValue);
331+
}
332+
)));
317333

318334
//select not_contains(val,'a','b','c')
319335
addGlobal(

src/test/java/org/jetlinks/reactor/ql/ReactorQLTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,18 @@ void testContains() {
362362
.expectNext(true)
363363
.verifyComplete();
364364

365+
ReactorQL.builder()
366+
.sql("select contains_any(this,?) isIn from dual")
367+
.build()
368+
.start(ReactorQLContext
369+
.ofDatasource((s) -> Flux.just(Arrays.asList(1, 2, 3, 4)))
370+
.bind(0, Collections.emptyList()))
371+
.doOnNext(System.out::println)
372+
.map(e -> e.asMap().get("isIn"))
373+
.as(StepVerifier::create)
374+
.expectNext(false)
375+
.verifyComplete();
376+
365377
{
366378
ReactorQL.builder()
367379
.sql("select not_contains(this,?) isIn from dual")
@@ -385,6 +397,17 @@ void testContains() {
385397
.as(StepVerifier::create)
386398
.expectNext(false)
387399
.verifyComplete();
400+
ReactorQL.builder()
401+
.sql("select not_contains(this,?) isIn from dual")
402+
.build()
403+
.start(ReactorQLContext
404+
.ofDatasource((s) -> Flux.just(Arrays.asList(1, 2, 3, 4)))
405+
.bind(0, Collections.emptyList()))
406+
.doOnNext(System.out::println)
407+
.map(e -> e.asMap().get("isIn"))
408+
.as(StepVerifier::create)
409+
.expectNext(true)
410+
.verifyComplete();
388411
}
389412

390413
{
@@ -493,6 +516,61 @@ void testContains() {
493516
.verifyComplete();
494517
}
495518

519+
{
520+
Map<String, Object> map_1 = new HashMap<>();
521+
map_1.put("a", 1);
522+
map_1.put("b", 1);
523+
Map<String, Object> map_2 = new HashMap<>();
524+
map_2.put("a", 10);
525+
map_2.put("b", 20);
526+
Map<String, Object> map_3 = new HashMap<>();
527+
map_3.put("a", 100);
528+
map_3.put("b", 200);
529+
// 参数为null时,contains_all应该返回false
530+
ReactorQL.builder()
531+
.sql("select contains_all(this,?) isIn from dual")
532+
.build()
533+
.start(ReactorQLContext
534+
.ofDatasource((s) -> Flux
535+
.just(Arrays.asList(map_1, map_2, map_3)))
536+
.bind(0, null)
537+
)
538+
.doOnNext(System.out::println)
539+
.map(e -> e.asMap().get("isIn"))
540+
.as(StepVerifier::create)
541+
.expectNext(false)
542+
.verifyComplete();
543+
544+
// 不传入参数时,contains_all应该返回false
545+
ReactorQL.builder()
546+
.sql("select contains_all(this,?) isIn from dual")
547+
.build()
548+
.start(ReactorQLContext
549+
.ofDatasource((s) -> Flux
550+
.just(Arrays.asList(map_1, map_2, map_3)))
551+
)
552+
.doOnNext(System.out::println)
553+
.map(e -> e.asMap().get("isIn"))
554+
.as(StepVerifier::create)
555+
.expectNext(false)
556+
.verifyComplete();
557+
558+
// 参数为空数组时,contains_all应该返回false
559+
ReactorQL.builder()
560+
.sql("select contains_all(this,?) isIn from dual")
561+
.build()
562+
.start(ReactorQLContext
563+
.ofDatasource((s) -> Flux
564+
.just(Arrays.asList(map_1, map_2, map_3)))
565+
.bind(0, new ArrayList<>())
566+
)
567+
.doOnNext(System.out::println)
568+
.map(e -> e.asMap().get("isIn"))
569+
.as(StepVerifier::create)
570+
.expectNext(true)
571+
.verifyComplete();
572+
}
573+
496574
}
497575

498576
@Test

0 commit comments

Comments
 (0)