11package com .redis .trino ;
22
33import static com .google .common .base .Verify .verify ;
4- import static com .redis .trino .TypeUtils .isJsonType ;
5- import static io .airlift .slice .Slices .utf8Slice ;
6- import static io .trino .plugin .base .util .JsonTypeUtil .jsonParse ;
7- import static io .trino .spi .StandardErrorCode .GENERIC_INTERNAL_ERROR ;
8- import static io .trino .spi .type .BigintType .BIGINT ;
9- import static io .trino .spi .type .Chars .truncateToLengthAndTrimSpaces ;
10- import static io .trino .spi .type .DateTimeEncoding .packDateTimeWithZone ;
11- import static io .trino .spi .type .DateType .DATE ;
12- import static io .trino .spi .type .Decimals .encodeScaledValue ;
13- import static io .trino .spi .type .Decimals .encodeShortScaledValue ;
14- import static io .trino .spi .type .IntegerType .INTEGER ;
15- import static io .trino .spi .type .RealType .REAL ;
16- import static io .trino .spi .type .SmallintType .SMALLINT ;
17- import static io .trino .spi .type .TimeZoneKey .UTC_KEY ;
18- import static io .trino .spi .type .TimestampType .TIMESTAMP_MILLIS ;
19- import static io .trino .spi .type .TimestampWithTimeZoneType .TIMESTAMP_TZ_MILLIS ;
20- import static io .trino .spi .type .Timestamps .MICROSECONDS_PER_MILLISECOND ;
21- import static io .trino .spi .type .TinyintType .TINYINT ;
22- import static java .lang .Float .floatToIntBits ;
234import static java .util .stream .Collectors .toList ;
245
256import java .io .IOException ;
267import java .io .OutputStream ;
27- import java .math .BigDecimal ;
28- import java .time .LocalDate ;
29- import java .time .format .DateTimeFormatter ;
308import java .util .Iterator ;
319import java .util .List ;
3210
3311import com .fasterxml .jackson .core .JsonFactory ;
3412import com .fasterxml .jackson .core .JsonGenerator ;
35- import com .google .common .primitives .SignedBytes ;
3613import com .redis .lettucemod .search .Document ;
3714
38- import io .airlift .slice .Slice ;
3915import io .airlift .slice .SliceOutput ;
4016import io .trino .spi .Page ;
4117import io .trino .spi .PageBuilder ;
42- import io .trino .spi .TrinoException ;
4318import io .trino .spi .block .BlockBuilder ;
4419import io .trino .spi .connector .ConnectorPageSource ;
45- import io .trino .spi .type .CharType ;
46- import io .trino .spi .type .DecimalType ;
4720import io .trino .spi .type .Type ;
48- import io .trino .spi .type .VarcharType ;
4921
5022public class RediSearchPageSource implements ConnectorPageSource {
23+
5124 private static final int ROWS_PER_REQUEST = 1024 ;
5225
26+ private final RediSearchPageSourceResultWriter writer = new RediSearchPageSourceResultWriter ();
5327 private final Iterator <Document <String , String >> cursor ;
5428 private final List <String > columnNames ;
5529 private final List <Type > columnTypes ;
@@ -63,7 +37,7 @@ public RediSearchPageSource(RediSearchSession rediSearchSession, RediSearchTable
6337 List <RediSearchColumnHandle > columns ) {
6438 this .columnNames = columns .stream ().map (RediSearchColumnHandle ::getName ).collect (toList ());
6539 this .columnTypes = columns .stream ().map (RediSearchColumnHandle ::getType ).collect (toList ());
66- this .cursor = rediSearchSession .execute (tableHandle ).iterator ();
40+ this .cursor = rediSearchSession .search (tableHandle ).iterator ();
6741 this .currentDoc = null ;
6842 this .pageBuilder = new PageBuilder (columnTypes );
6943 }
@@ -103,7 +77,12 @@ public Page getNextPage() {
10377 pageBuilder .declarePosition ();
10478 for (int column = 0 ; column < columnTypes .size (); column ++) {
10579 BlockBuilder output = pageBuilder .getBlockBuilder (column );
106- appendTo (columnTypes .get (column ), currentDoc .get (columnNames .get (column )), output );
80+ String value = currentDoc .get (columnNames .get (column ));
81+ if (value == null ) {
82+ output .appendNull ();
83+ } else {
84+ writer .appendTo (columnTypes .get (column ), value , output );
85+ }
10786 }
10887 }
10988
@@ -112,67 +91,12 @@ public Page getNextPage() {
11291 return page ;
11392 }
11493
115- private void appendTo (Type type , String value , BlockBuilder output ) {
116- if (value == null ) {
117- output .appendNull ();
118- return ;
119- }
120- Class <?> javaType = type .getJavaType ();
121- if (javaType == boolean .class ) {
122- type .writeBoolean (output , Boolean .parseBoolean (value ));
123- } else if (javaType == long .class ) {
124- if (type .equals (BIGINT )) {
125- type .writeLong (output , Long .parseLong (value ));
126- } else if (type .equals (INTEGER )) {
127- type .writeLong (output , Integer .parseInt (value ));
128- } else if (type .equals (SMALLINT )) {
129- type .writeLong (output , Short .parseShort (value ));
130- } else if (type .equals (TINYINT )) {
131- type .writeLong (output , SignedBytes .checkedCast (Long .parseLong (value )));
132- } else if (type .equals (REAL )) {
133- type .writeLong (output , floatToIntBits ((Float .parseFloat (value ))));
134- } else if (type instanceof DecimalType ) {
135- type .writeLong (output , encodeShortScaledValue (new BigDecimal (value ), ((DecimalType ) type ).getScale ()));
136- } else if (type .equals (DATE )) {
137- type .writeLong (output , LocalDate .from (DateTimeFormatter .ISO_DATE .parse (value )).toEpochDay ());
138- } else if (type .equals (TIMESTAMP_MILLIS )) {
139- type .writeLong (output , Long .parseLong (value ) * MICROSECONDS_PER_MILLISECOND );
140- } else if (type .equals (TIMESTAMP_TZ_MILLIS )) {
141- type .writeLong (output , packDateTimeWithZone (Long .parseLong (value ), UTC_KEY ));
142- } else {
143- throw new TrinoException (GENERIC_INTERNAL_ERROR ,
144- "Unhandled type for " + javaType .getSimpleName () + ":" + type .getTypeSignature ());
145- }
146- } else if (javaType == double .class ) {
147- type .writeDouble (output , Double .parseDouble (value ));
148- } else if (javaType == Slice .class ) {
149- writeSlice (output , type , value );
150- } else {
151- throw new TrinoException (GENERIC_INTERNAL_ERROR ,
152- "Unhandled type for " + javaType .getSimpleName () + ":" + type .getTypeSignature ());
153- }
154- }
155-
156- private void writeSlice (BlockBuilder output , Type type , String value ) {
157- if (type instanceof VarcharType ) {
158- type .writeSlice (output , utf8Slice (value ));
159- } else if (type instanceof CharType ) {
160- type .writeSlice (output , truncateToLengthAndTrimSpaces (utf8Slice (value ), ((CharType ) type )));
161- } else if (type instanceof DecimalType ) {
162- type .writeObject (output , encodeScaledValue (new BigDecimal (value ), ((DecimalType ) type ).getScale ()));
163- } else if (isJsonType (type )) {
164- type .writeSlice (output , jsonParse (utf8Slice (value )));
165- } else {
166- throw new TrinoException (GENERIC_INTERNAL_ERROR , "Unhandled type for Slice: " + type .getTypeSignature ());
167- }
168- }
169-
17094 public static JsonGenerator createJsonGenerator (JsonFactory factory , SliceOutput output ) throws IOException {
17195 return factory .createGenerator ((OutputStream ) output );
17296 }
17397
17498 @ Override
17599 public void close () {
176-
100+ // nothing to do
177101 }
178102}
0 commit comments