1313 */
1414package com .facebook .presto .tests .tpch ;
1515
16+ import com .facebook .presto .common .block .MethodHandleUtil ;
1617import com .facebook .presto .common .predicate .NullableValue ;
1718import com .facebook .presto .common .predicate .TupleDomain ;
19+ import com .facebook .presto .common .type .MapType ;
1820import com .facebook .presto .spi .ColumnHandle ;
21+ import com .facebook .presto .spi .ColumnMetadata ;
1922import com .facebook .presto .spi .ConnectorResolvedIndex ;
2023import com .facebook .presto .spi .ConnectorSession ;
2124import com .facebook .presto .spi .ConnectorTableHandle ;
25+ import com .facebook .presto .spi .ConnectorTableLayoutHandle ;
26+ import com .facebook .presto .spi .ConnectorTableMetadata ;
27+ import com .facebook .presto .spi .Constraint ;
28+ import com .facebook .presto .spi .SchemaTableName ;
29+ import com .facebook .presto .spi .statistics .TableStatistics ;
2230import com .facebook .presto .tpch .TpchMetadata ;
2331import com .facebook .presto .tpch .TpchTableHandle ;
2432import com .google .common .collect .ImmutableList ;
2533import com .google .common .collect .ImmutableMap ;
2634import com .google .common .collect .ImmutableSet ;
2735import com .google .common .collect .Maps ;
2836
37+ import java .util .List ;
2938import java .util .Map ;
3039import java .util .Optional ;
3140import java .util .Set ;
3241import java .util .stream .Collectors ;
3342
43+ import static com .facebook .presto .common .type .BigintType .BIGINT ;
44+ import static com .facebook .presto .common .type .VarcharType .VARCHAR ;
3445import static com .facebook .presto .tests .tpch .TpchIndexProvider .handleToNames ;
3546import static com .google .common .base .Predicates .in ;
3647import static com .google .common .base .Predicates .not ;
@@ -40,6 +51,8 @@ public class TpchIndexMetadata
4051 extends TpchMetadata
4152{
4253 private final TpchIndexedData indexedData ;
54+ // For tables in this set, add an extra map column to their metadata.
55+ private final Set <String > tableWithExtraColumn = ImmutableSet .of ("orders_extra" );
4356
4457 public TpchIndexMetadata (String connectorId , TpchIndexedData indexedData )
4558 {
@@ -56,6 +69,10 @@ public Optional<ConnectorResolvedIndex> resolveIndex(
5669 TupleDomain <ColumnHandle > tupleDomain )
5770 {
5871 TpchTableHandle tpchTableHandle = (TpchTableHandle ) tableHandle ;
72+ String tableName = tpchTableHandle .getTableName ();
73+ if (tableWithExtraColumn .contains (tableName )) {
74+ tpchTableHandle = new TpchTableHandle (getOriginalTpchTableName (tableName ), tpchTableHandle .getScaleFactor ());
75+ }
5976
6077 // Keep the fixed values that don't overlap with the indexableColumns
6178 // Note: technically we could more efficiently utilize the overlapped columns, but this way is simpler for now
@@ -82,10 +99,73 @@ public Optional<ConnectorResolvedIndex> resolveIndex(
8299 filteredTupleDomain = TupleDomain .withColumnDomains (Maps .filterKeys (tupleDomain .getDomains ().get (), not (in (fixedValues .keySet ()))));
83100 }
84101 TpchIndexHandle indexHandle = new TpchIndexHandle (
85- tpchTableHandle . getTableName () ,
102+ tableName ,
86103 tpchTableHandle .getScaleFactor (),
87104 lookupColumnNames ,
88105 TupleDomain .fromFixedValues (fixedValues ));
89106 return Optional .of (new ConnectorResolvedIndex (indexHandle , filteredTupleDomain ));
90107 }
108+
109+ @ Override
110+ public TpchTableHandle getTableHandle (ConnectorSession session , SchemaTableName schemaTableName )
111+ {
112+ String tableName = schemaTableName .getTableName ();
113+ if (tableWithExtraColumn .contains (tableName )) {
114+ TpchTableHandle originalTableHandle = super .getTableHandle (session , new SchemaTableName (schemaTableName .getSchemaName (), getOriginalTpchTableName (tableName )));
115+ return new TpchTableHandle (schemaTableName .getTableName (), originalTableHandle .getScaleFactor ());
116+ }
117+ return super .getTableHandle (session , schemaTableName );
118+ }
119+
120+ @ Override
121+ public TableStatistics getTableStatistics (ConnectorSession session , ConnectorTableHandle tableHandle , Optional <ConnectorTableLayoutHandle > tableLayoutHandle , List <ColumnHandle > columnHandles , Constraint <ColumnHandle > constraint )
122+ {
123+ String tableName = ((TpchTableHandle ) tableHandle ).getTableName ();
124+ if (tableWithExtraColumn .contains (tableName )) {
125+ TpchTableHandle originalTableHandle = new TpchTableHandle (getOriginalTpchTableName (tableName ), ((TpchTableHandle ) tableHandle ).getScaleFactor ());
126+ return super .getTableStatistics (session , originalTableHandle , tableLayoutHandle , columnHandles , constraint );
127+ }
128+ return super .getTableStatistics (session , tableHandle , tableLayoutHandle , columnHandles , constraint );
129+ }
130+
131+ @ Override
132+ public ConnectorTableMetadata getTableMetadata (ConnectorSession session , ConnectorTableHandle tableHandle )
133+ {
134+ TpchTableHandle tpchTableHandle = (TpchTableHandle ) tableHandle ;
135+ String tableName = tpchTableHandle .getTableName ();
136+ if (tableWithExtraColumn .contains (tableName )) {
137+ ConnectorTableMetadata tableMetadata = super .getTableMetadata (session , new TpchTableHandle (getOriginalTpchTableName (tableName ), tpchTableHandle .getScaleFactor ()));
138+ ImmutableList .Builder <ColumnMetadata > columns = ImmutableList .builder ();
139+ columns .addAll (tableMetadata .getColumns ());
140+ columns .add (getExtraMapColumnMetadata ());
141+ return new ConnectorTableMetadata (new SchemaTableName (tableMetadata .getTable ().getSchemaName (), tableName ),
142+ columns .build ());
143+ }
144+ return super .getTableMetadata (session , tableHandle );
145+ }
146+
147+ private ColumnMetadata getExtraMapColumnMetadata ()
148+ {
149+ return ColumnMetadata .builder ()
150+ .setName ("data" )
151+ .setType (new MapType (BIGINT ,
152+ VARCHAR ,
153+ MethodHandleUtil .methodHandle (TpchIndexMetadata .class , "throwUnsupportedOperation" ),
154+ MethodHandleUtil .methodHandle (TpchIndexMetadata .class , "throwUnsupportedOperation" )))
155+ .build ();
156+ }
157+
158+ private String getOriginalTpchTableName (String tableName )
159+ {
160+ String suffix = "_extra" ;
161+ if (tableName != null && tableName .endsWith (suffix )) {
162+ return tableName .substring (0 , tableName .length () - suffix .length ());
163+ }
164+ return tableName ;
165+ }
166+
167+ public static void throwUnsupportedOperation ()
168+ {
169+ throw new UnsupportedOperationException ();
170+ }
91171}
0 commit comments