-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
205 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/org/jetlinks/reactor/ql/feature/DistinctFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.jetlinks.reactor.ql.feature; | ||
|
||
import net.sf.jsqlparser.statement.select.Distinct; | ||
import org.jetlinks.reactor.ql.ReactorQLMetadata; | ||
import org.jetlinks.reactor.ql.ReactorQLRecord; | ||
import reactor.core.publisher.Flux; | ||
|
||
import java.util.function.Function; | ||
|
||
public interface DistinctFeature extends Feature { | ||
|
||
Function<Flux<ReactorQLRecord>, Flux<ReactorQLRecord>> createDistinctMapper(Distinct distinct, ReactorQLMetadata metadata); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/org/jetlinks/reactor/ql/supports/distinct/DefaultDistinctFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.jetlinks.reactor.ql.supports.distinct; | ||
|
||
import net.sf.jsqlparser.expression.Expression; | ||
import net.sf.jsqlparser.statement.select.*; | ||
import org.jetlinks.reactor.ql.ReactorQLMetadata; | ||
import org.jetlinks.reactor.ql.ReactorQLRecord; | ||
import org.jetlinks.reactor.ql.feature.DistinctFeature; | ||
import org.jetlinks.reactor.ql.feature.FeatureId; | ||
import org.jetlinks.reactor.ql.feature.ValueMapFeature; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.util.function.Tuple2; | ||
import reactor.util.function.Tuples; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public class DefaultDistinctFeature implements DistinctFeature { | ||
@Override | ||
public Function<Flux<ReactorQLRecord>, Flux<ReactorQLRecord>> createDistinctMapper(Distinct distinct, ReactorQLMetadata metadata) { | ||
|
||
List<SelectItem> items = distinct.getOnSelectItems(); | ||
if (items == null) { | ||
return flux -> flux.distinct(ReactorQLRecord::getRecord); | ||
} | ||
List<Function<ReactorQLRecord, Mono<Object>>> keySelector = new ArrayList<>(); | ||
for (SelectItem item : items) { | ||
item.accept(new SelectItemVisitor() { | ||
@Override | ||
public void visit(AllColumns allColumns) { | ||
keySelector.add(record -> Mono.justOrEmpty(record.getRecord())); | ||
} | ||
|
||
@Override | ||
public void visit(AllTableColumns allTableColumns) { | ||
String tname = allTableColumns.getTable().getAlias() != null ? allTableColumns.getTable().getAlias().getName() : allTableColumns.getTable().getName(); | ||
keySelector.add(record -> Mono.justOrEmpty(record.getRecord(tname))); | ||
} | ||
|
||
@Override | ||
public void visit(SelectExpressionItem selectExpressionItem) { | ||
Expression expr = selectExpressionItem.getExpression(); | ||
Function<ReactorQLRecord, ? extends Publisher<?>> mapper = ValueMapFeature.createMapperNow(expr, metadata); | ||
keySelector.add(record -> Mono.from(mapper.apply(record))); | ||
} | ||
}); | ||
} | ||
return createDistinct(keySelector); | ||
} | ||
|
||
protected Function<Flux<ReactorQLRecord>, Flux<ReactorQLRecord>> createDistinct(List<Function<ReactorQLRecord, Mono<Object>>> keySelector) { | ||
return flux -> flux | ||
.flatMap(record -> Flux.fromIterable(keySelector) | ||
.flatMap(mapper -> mapper.apply(record)) | ||
.collectList() | ||
.map(list -> Tuples.of(list, record))) | ||
.distinct(Tuple2::getT1) | ||
.map(Tuple2::getT2); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return FeatureId.Distinct.defaultId.getId(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/test/java/org/jetlinks/reactor/ql/supports/DefaultReactorQLMetadataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.jetlinks.reactor.ql.supports; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class DefaultReactorQLMetadataTest { | ||
|
||
@Test | ||
void testSettingByOracleHint() { | ||
DefaultReactorQLMetadata metadata = new DefaultReactorQLMetadata("select /*+ distinctBy(bloom),ignoreError */ * from test"); | ||
|
||
assertEquals(metadata.getSetting("distinctBy").orElse(null), "bloom"); | ||
assertEquals(metadata.getSetting("ignoreError").orElse(null), true); | ||
|
||
} | ||
} |