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