1
1
package com .redis .trino ;
2
2
3
3
import 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 ;
23
4
import static java .util .stream .Collectors .toList ;
24
5
25
6
import java .io .IOException ;
26
7
import java .io .OutputStream ;
27
- import java .math .BigDecimal ;
28
- import java .time .LocalDate ;
29
- import java .time .format .DateTimeFormatter ;
30
8
import java .util .Iterator ;
31
9
import java .util .List ;
32
10
33
11
import com .fasterxml .jackson .core .JsonFactory ;
34
12
import com .fasterxml .jackson .core .JsonGenerator ;
35
- import com .google .common .primitives .SignedBytes ;
36
13
import com .redis .lettucemod .search .Document ;
37
14
38
- import io .airlift .slice .Slice ;
39
15
import io .airlift .slice .SliceOutput ;
40
16
import io .trino .spi .Page ;
41
17
import io .trino .spi .PageBuilder ;
42
- import io .trino .spi .TrinoException ;
43
18
import io .trino .spi .block .BlockBuilder ;
44
19
import io .trino .spi .connector .ConnectorPageSource ;
45
- import io .trino .spi .type .CharType ;
46
- import io .trino .spi .type .DecimalType ;
47
20
import io .trino .spi .type .Type ;
48
- import io .trino .spi .type .VarcharType ;
49
21
50
22
public class RediSearchPageSource implements ConnectorPageSource {
23
+
51
24
private static final int ROWS_PER_REQUEST = 1024 ;
52
25
26
+ private final RediSearchPageSourceResultWriter writer = new RediSearchPageSourceResultWriter ();
53
27
private final Iterator <Document <String , String >> cursor ;
54
28
private final List <String > columnNames ;
55
29
private final List <Type > columnTypes ;
@@ -63,7 +37,7 @@ public RediSearchPageSource(RediSearchSession rediSearchSession, RediSearchTable
63
37
List <RediSearchColumnHandle > columns ) {
64
38
this .columnNames = columns .stream ().map (RediSearchColumnHandle ::getName ).collect (toList ());
65
39
this .columnTypes = columns .stream ().map (RediSearchColumnHandle ::getType ).collect (toList ());
66
- this .cursor = rediSearchSession .execute (tableHandle ).iterator ();
40
+ this .cursor = rediSearchSession .search (tableHandle ).iterator ();
67
41
this .currentDoc = null ;
68
42
this .pageBuilder = new PageBuilder (columnTypes );
69
43
}
@@ -103,7 +77,12 @@ public Page getNextPage() {
103
77
pageBuilder .declarePosition ();
104
78
for (int column = 0 ; column < columnTypes .size (); column ++) {
105
79
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
+ }
107
86
}
108
87
}
109
88
@@ -112,67 +91,12 @@ public Page getNextPage() {
112
91
return page ;
113
92
}
114
93
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
-
170
94
public static JsonGenerator createJsonGenerator (JsonFactory factory , SliceOutput output ) throws IOException {
171
95
return factory .createGenerator ((OutputStream ) output );
172
96
}
173
97
174
98
@ Override
175
99
public void close () {
176
-
100
+ // nothing to do
177
101
}
178
102
}
0 commit comments