17
17
import com .facebook .presto .common .predicate .TupleDomain ;
18
18
import com .facebook .presto .common .type .CharType ;
19
19
import com .facebook .presto .common .type .DecimalType ;
20
+ import com .facebook .presto .common .type .TimestampType ;
20
21
import com .facebook .presto .common .type .Type ;
21
22
import com .facebook .presto .common .type .UuidType ;
22
23
import com .facebook .presto .common .type .VarcharType ;
24
+ import com .facebook .presto .plugin .jdbc .mapping .ColumnMapping ;
25
+ import com .facebook .presto .plugin .jdbc .mapping .WriteMapping ;
26
+ import com .facebook .presto .plugin .jdbc .mapping .functions .BooleanWriteFunction ;
27
+ import com .facebook .presto .plugin .jdbc .mapping .functions .DoubleWriteFunction ;
28
+ import com .facebook .presto .plugin .jdbc .mapping .functions .LongWriteFunction ;
29
+ import com .facebook .presto .plugin .jdbc .mapping .functions .SliceWriteFunction ;
23
30
import com .facebook .presto .spi .ColumnHandle ;
24
31
import com .facebook .presto .spi .ColumnMetadata ;
25
32
import com .facebook .presto .spi .ConnectorSession ;
64
71
import static com .facebook .presto .common .type .SmallintType .SMALLINT ;
65
72
import static com .facebook .presto .common .type .TimeType .TIME ;
66
73
import static com .facebook .presto .common .type .TimeWithTimeZoneType .TIME_WITH_TIME_ZONE ;
67
- import static com .facebook .presto .common .type .TimestampType .TIMESTAMP ;
68
74
import static com .facebook .presto .common .type .TimestampWithTimeZoneType .TIMESTAMP_WITH_TIME_ZONE ;
69
75
import static com .facebook .presto .common .type .TinyintType .TINYINT ;
70
76
import static com .facebook .presto .common .type .VarbinaryType .VARBINARY ;
71
77
import static com .facebook .presto .common .type .Varchars .isVarcharType ;
72
78
import static com .facebook .presto .plugin .jdbc .JdbcErrorCode .JDBC_ERROR ;
73
- import static com .facebook .presto .plugin .jdbc .StandardReadMappings .jdbcTypeToPrestoType ;
79
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .bigintColumnMapping ;
80
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .booleanColumnMapping ;
81
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .charColumnMapping ;
82
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .dateColumnMapping ;
83
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .decimalColumnMapping ;
84
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .doubleColumnMapping ;
85
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .integerColumnMapping ;
86
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .jdbcTypeToPrestoType ;
87
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .realColumnMapping ;
88
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .smallintColumnMapping ;
89
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .timeColumnMapping ;
90
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .timeWithTimeZoneColumnMapping ;
91
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .timestampColumnMapping ;
92
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .timestampWithTimeZoneColumnMapping ;
93
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .tinyintColumnMapping ;
94
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .uuidColumnMapping ;
95
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .varbinaryColumnMapping ;
96
+ import static com .facebook .presto .plugin .jdbc .mapping .StandardColumnMappings .varcharColumnMapping ;
97
+ import static com .facebook .presto .plugin .jdbc .mapping .WriteMapping .booleanMapping ;
98
+ import static com .facebook .presto .plugin .jdbc .mapping .WriteMapping .longMapping ;
99
+ import static com .facebook .presto .plugin .jdbc .mapping .WriteMapping .sliceMapping ;
74
100
import static com .facebook .presto .spi .StandardErrorCode .NOT_FOUND ;
75
101
import static com .facebook .presto .spi .StandardErrorCode .NOT_SUPPORTED ;
76
102
import static com .google .common .base .MoreObjects .firstNonNull ;
@@ -94,21 +120,21 @@ public class BaseJdbcClient
94
120
{
95
121
private static final Logger log = Logger .get (BaseJdbcClient .class );
96
122
97
- private static final Map <Type , String > SQL_TYPES = ImmutableMap .<Type , String >builder ()
98
- .put (BOOLEAN , "boolean" )
99
- .put (BIGINT , "bigint" )
100
- .put (INTEGER , "integer" )
101
- .put (SMALLINT , "smallint" )
102
- .put (TINYINT , "tinyint" )
103
- .put (DOUBLE , "double precision" )
104
- .put (REAL , "real" )
105
- .put (VARBINARY , "varbinary" )
106
- .put (DATE , "date" )
107
- .put (TIME , "time" )
108
- .put (TIME_WITH_TIME_ZONE , "time with timezone" )
109
- . put ( TIMESTAMP , "timestamp" )
110
- .put (TIMESTAMP_WITH_TIME_ZONE , "timestamp with timezone" )
111
- .put (UuidType . UUID , "uuid" )
123
+ private static final Map <Type , WriteMapping > TYPE_MAPPINGS = ImmutableMap .<Type , WriteMapping >builder ()
124
+ .put (BOOLEAN , booleanMapping ( "boolean" , ( BooleanWriteFunction ) booleanColumnMapping (). getWriteFunction ()) )
125
+ .put (BIGINT , longMapping ( "bigint" , ( LongWriteFunction ) bigintColumnMapping (). getWriteFunction ()) )
126
+ .put (INTEGER , longMapping ( "integer" , ( LongWriteFunction ) integerColumnMapping (). getWriteFunction ()) )
127
+ .put (SMALLINT , longMapping ( "smallint" , ( LongWriteFunction ) smallintColumnMapping (). getWriteFunction ()) )
128
+ .put (TINYINT , longMapping ( "tinyint" , ( LongWriteFunction ) tinyintColumnMapping (). getWriteFunction ()) )
129
+ .put (DOUBLE , WriteMapping . doubleMapping ( "double precision" , ( DoubleWriteFunction ) doubleColumnMapping (). getWriteFunction ()) )
130
+ .put (REAL , longMapping ( "real" , ( LongWriteFunction ) realColumnMapping (). getWriteFunction ()) )
131
+ .put (VARBINARY , sliceMapping ( "varbinary" , ( SliceWriteFunction ) varbinaryColumnMapping (). getWriteFunction ()) )
132
+ .put (DATE , longMapping ( "date" , ( LongWriteFunction ) dateColumnMapping (). getWriteFunction ()) )
133
+ .put (TIME , longMapping ( "time" , ( LongWriteFunction ) timeColumnMapping (). getWriteFunction ()) )
134
+ .put (UuidType . UUID , sliceMapping ( "uuid" , ( SliceWriteFunction ) uuidColumnMapping (). getWriteFunction ()) )
135
+
136
+ .put (TIME_WITH_TIME_ZONE , longMapping ( "time with timezone", ( LongWriteFunction ) timeWithTimeZoneColumnMapping (). getWriteFunction ()) )
137
+ .put (TIMESTAMP_WITH_TIME_ZONE , longMapping ( "timestamp with timezone" , ( LongWriteFunction ) timestampWithTimeZoneColumnMapping (). getWriteFunction ()) )
112
138
.build ();
113
139
114
140
protected final String connectorId ;
@@ -241,7 +267,7 @@ public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHand
241
267
resultSet .getString ("TYPE_NAME" ),
242
268
resultSet .getInt ("COLUMN_SIZE" ),
243
269
resultSet .getInt ("DECIMAL_DIGITS" ));
244
- Optional <ReadMapping > columnMapping = toPrestoType (session , typeHandle );
270
+ Optional <ColumnMapping > columnMapping = toPrestoType (session , typeHandle );
245
271
// skip unsupported column types
246
272
if (columnMapping .isPresent ()) {
247
273
String columnName = resultSet .getString ("COLUMN_NAME" );
@@ -267,7 +293,7 @@ public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHand
267
293
}
268
294
269
295
@ Override
270
- public Optional <ReadMapping > toPrestoType (ConnectorSession session , JdbcTypeHandle typeHandle )
296
+ public Optional <ColumnMapping > toPrestoType (ConnectorSession session , JdbcTypeHandle typeHandle )
271
297
{
272
298
return jdbcTypeToPrestoType (typeHandle );
273
299
}
@@ -403,7 +429,7 @@ private String getColumnString(ColumnMetadata column, String columnName)
403
429
StringBuilder sb = new StringBuilder ()
404
430
.append (quoted (columnName ))
405
431
.append (" " )
406
- .append (toSqlType (column .getType ()));
432
+ .append (toWriteMapping (column .getType ()). getDataType ( ));
407
433
if (!column .isNullable ()) {
408
434
sb .append (" NOT NULL" );
409
435
}
@@ -737,28 +763,45 @@ protected void execute(Connection connection, String query)
737
763
}
738
764
}
739
765
740
- protected String toSqlType (Type type )
766
+ public WriteMapping toWriteMapping (Type type )
741
767
{
768
+ String dataType ;
742
769
if (isVarcharType (type )) {
743
770
VarcharType varcharType = (VarcharType ) type ;
744
771
if (varcharType .isUnbounded ()) {
745
- return "varchar" ;
772
+ dataType = "varchar" ;
746
773
}
747
- return "varchar(" + varcharType .getLengthSafe () + ")" ;
774
+ else {
775
+ dataType = "varchar(" + varcharType .getLengthSafe () + ")" ;
776
+ }
777
+ return sliceMapping (dataType , (SliceWriteFunction ) varcharColumnMapping (varcharType ).getWriteFunction ());
778
+ }
779
+ else if (type instanceof CharType ) {
780
+ CharType charType = (CharType ) type ;
781
+ if (charType .getLength () == CharType .MAX_LENGTH ) {
782
+ dataType = "char" ;
783
+ }
784
+ else {
785
+ dataType = "char(" + ((CharType ) type ).getLength () + ")" ;
786
+ }
787
+ return sliceMapping (dataType , (SliceWriteFunction ) charColumnMapping (charType ).getWriteFunction ());
748
788
}
749
- if (type instanceof CharType ) {
750
- if (((CharType ) type ).getLength () == CharType .MAX_LENGTH ) {
751
- return "char" ;
789
+ else if (type instanceof DecimalType ) {
790
+ DecimalType decimalType = (DecimalType ) type ;
791
+ dataType = format ("decimal(%s, %s)" , ((DecimalType ) type ).getPrecision (), ((DecimalType ) type ).getScale ());
792
+ if (decimalType .isShort ()) {
793
+ return longMapping (dataType , (LongWriteFunction ) decimalColumnMapping (decimalType ).getWriteFunction ());
794
+ }
795
+ else {
796
+ return sliceMapping (dataType , (SliceWriteFunction ) decimalColumnMapping (decimalType ).getWriteFunction ());
752
797
}
753
- return "char(" + ((CharType ) type ).getLength () + ")" ;
754
798
}
755
- if (type instanceof DecimalType ) {
756
- return format ( "decimal(%s, %s) " , (( DecimalType ) type ). getPrecision (), (( DecimalType ) type ).getScale ());
799
+ else if (type instanceof TimestampType ) {
800
+ return longMapping ( "timestamp " , (LongWriteFunction ) timestampColumnMapping (( TimestampType ) type ).getWriteFunction ());
757
801
}
758
-
759
- String sqlType = SQL_TYPES .get (type );
760
- if (sqlType != null ) {
761
- return sqlType ;
802
+ WriteMapping writeMapping = TYPE_MAPPINGS .get (type );
803
+ if (writeMapping != null ) {
804
+ return writeMapping ;
762
805
}
763
806
throw new PrestoException (NOT_SUPPORTED , "Unsupported column type: " + type .getDisplayName ());
764
807
}
0 commit comments