15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
- package org .apache .doris .tablefunction ;
18
+ package org .apache .doris .tablefunction . iceberg ;
19
19
20
20
import org .apache .doris .analysis .TableName ;
21
21
import org .apache .doris .catalog .Column ;
22
22
import org .apache .doris .catalog .Env ;
23
- import org .apache .doris .catalog .MapType ;
24
- import org .apache .doris .catalog .ScalarType ;
25
23
import org .apache .doris .common .AnalysisException ;
26
24
import org .apache .doris .common .ErrorCode ;
27
25
import org .apache .doris .common .ErrorReport ;
30
28
import org .apache .doris .datasource .iceberg .IcebergMetadataCache ;
31
29
import org .apache .doris .mysql .privilege .PrivPredicate ;
32
30
import org .apache .doris .qe .ConnectContext ;
31
+ import org .apache .doris .tablefunction .MetadataTableValuedFunction ;
33
32
import org .apache .doris .thrift .TIcebergMetadataParams ;
34
33
import org .apache .doris .thrift .TIcebergQueryType ;
35
34
import org .apache .doris .thrift .TMetaScanRange ;
36
35
import org .apache .doris .thrift .TMetadataType ;
37
36
38
- import com .google .common .collect .ImmutableList ;
39
- import com .google .common .collect .ImmutableMap ;
40
37
import com .google .common .collect .ImmutableSet ;
41
38
import com .google .common .collect .Lists ;
42
39
import com .google .common .collect .Maps ;
47
44
import java .util .Map ;
48
45
49
46
/**
50
- * The Implement of table valued function
47
+ * The Abstract class of table valued function for iceberg metadata.
51
48
* iceberg_meta("table" = "ctl.db.tbl", "query_type" = "snapshots").
52
49
*/
53
- public class IcebergTableValuedFunction extends MetadataTableValuedFunction {
50
+ public abstract class IcebergTableValuedFunction extends MetadataTableValuedFunction {
54
51
55
52
public static final String NAME = "iceberg_meta" ;
56
53
public static final String TABLE = "table" ;
57
54
public static final String QUERY_TYPE = "query_type" ;
58
55
59
56
private static final ImmutableSet <String > PROPERTIES_SET = ImmutableSet .of (TABLE , QUERY_TYPE );
60
57
61
- private static final ImmutableList <Column > SCHEMA_SNAPSHOT = ImmutableList .of (
62
- new Column ("committed_at" , ScalarType .DATETIMEV2 ),
63
- new Column ("snapshot_id" , ScalarType .BIGINT ),
64
- new Column ("parent_id" , ScalarType .BIGINT ),
65
- new Column ("operation" , ScalarType .STRING ),
66
- new Column ("manifest_list" , ScalarType .STRING ),
67
- new Column ("summary" , new MapType (ScalarType .STRING , ScalarType .STRING )));
68
-
69
- private TIcebergQueryType queryType ;
70
-
71
- // here tableName represents the name of a table in Iceberg.
58
+ private final TIcebergQueryType queryType ;
72
59
private final TableName icebergTableName ;
73
60
74
- public IcebergTableValuedFunction (Map <String , String > params ) throws AnalysisException {
61
+ private final Map <String , String > hadoopProps ;
62
+ protected final Table table ;
63
+
64
+ public static IcebergTableValuedFunction create (Map <String , String > params )
65
+ throws AnalysisException {
75
66
Map <String , String > validParams = Maps .newHashMap ();
76
67
for (String key : params .keySet ()) {
77
68
if (!PROPERTIES_SET .contains (key .toLowerCase ())) {
@@ -89,21 +80,48 @@ public IcebergTableValuedFunction(Map<String, String> params) throws AnalysisExc
89
80
if (names .length != 3 ) {
90
81
throw new AnalysisException ("The iceberg table name contains the catalogName, databaseName, and tableName" );
91
82
}
92
- this . icebergTableName = new TableName (names [0 ], names [1 ], names [2 ]);
83
+ TableName icebergTableName = new TableName (names [0 ], names [1 ], names [2 ]);
93
84
// check auth
94
85
if (!Env .getCurrentEnv ().getAccessManager ()
95
- .checkTblPriv (ConnectContext .get (), this . icebergTableName , PrivPredicate .SELECT )) {
86
+ .checkTblPriv (ConnectContext .get (), icebergTableName , PrivPredicate .SELECT )) {
96
87
ErrorReport .reportAnalysisException (ErrorCode .ERR_TABLEACCESS_DENIED_ERROR , "SELECT" ,
97
88
ConnectContext .get ().getQualifiedUser (), ConnectContext .get ().getRemoteIP (),
98
- this . icebergTableName .getDb () + ": " + this . icebergTableName .getTbl ());
89
+ icebergTableName .getDb () + ": " + icebergTableName .getTbl ());
99
90
}
91
+ TIcebergQueryType queryType ;
100
92
try {
101
- this . queryType = TIcebergQueryType .valueOf (queryTypeString .toUpperCase ());
93
+ queryType = TIcebergQueryType .valueOf (queryTypeString .toUpperCase ());
102
94
} catch (IllegalArgumentException e ) {
103
- throw new AnalysisException ("Unsupported iceberg metadata query type: " + queryType );
95
+ throw new AnalysisException ("Unrecognized iceberg metadata query type: " + queryTypeString );
96
+ }
97
+
98
+ switch (queryType ) {
99
+ case HISTORY :
100
+ return new IcebergHistoryTableValuedFunction (icebergTableName );
101
+ default :
102
+ throw new AnalysisException ("Unsupported iceberg metadata query type: " + queryType );
104
103
}
105
104
}
106
105
106
+ public IcebergTableValuedFunction (TableName icebergTableName , TIcebergQueryType queryType )
107
+ throws AnalysisException {
108
+ this .icebergTableName = icebergTableName ;
109
+ this .queryType = queryType ;
110
+ CatalogIf catalog = Env .getCurrentEnv ().getCatalogMgr ().getCatalog (icebergTableName .getCtl ());
111
+ if (!(catalog instanceof ExternalCatalog )) {
112
+ throw new AnalysisException ("Catalog " + icebergTableName .getCtl () + " is not an external catalog" );
113
+ }
114
+ ExternalCatalog externalCatalog = (ExternalCatalog ) catalog ;
115
+ hadoopProps = externalCatalog .getCatalogProperty ().getHadoopProperties ();
116
+ IcebergMetadataCache icebergMetadataCache = Env .getCurrentEnv ().getExtMetaCacheMgr ().getIcebergMetadataCache ();
117
+ Table table = icebergMetadataCache .getIcebergTable (catalog , icebergTableName .getDb (),
118
+ icebergTableName .getTbl ());
119
+ if (table == null ) {
120
+ throw new AnalysisException ("Iceberg table " + icebergTableName + " does not exist" );
121
+ }
122
+ this .table = table ;
123
+ }
124
+
107
125
public TIcebergQueryType getIcebergQueryType () {
108
126
return queryType ;
109
127
}
@@ -114,46 +132,53 @@ public TMetadataType getMetadataType() {
114
132
}
115
133
116
134
@ Override
117
- public TMetaScanRange getMetaScanRange () {
118
- TMetaScanRange metaScanRange = new TMetaScanRange ();
119
- metaScanRange .setMetadataType (TMetadataType .ICEBERG );
120
- // set iceberg metadata params
121
- TIcebergMetadataParams icebergMetadataParams = new TIcebergMetadataParams ();
122
- icebergMetadataParams .setIcebergQueryType (queryType );
123
- // TODO: remove this after iceberg metadata cache is ready
124
- icebergMetadataParams .setCatalog (icebergTableName .getCtl ());
125
- icebergMetadataParams .setDatabase (icebergTableName .getDb ());
126
- icebergMetadataParams .setTable (icebergTableName .getTbl ());
127
- CatalogIf catalog = Env .getCurrentEnv ().getCatalogMgr ().getCatalog (icebergTableName .getCtl ());
128
- IcebergMetadataCache icebergMetadataCache = Env .getCurrentEnv ().getExtMetaCacheMgr ().getIcebergMetadataCache ();
129
- Table table = icebergMetadataCache .getIcebergTable (catalog , icebergTableName .getDb (),
130
- icebergTableName .getTbl ());
131
- String serializedTable = SerializationUtil .serializeToBase64 (table );
132
- icebergMetadataParams .setSerializedTable (serializedTable );
133
- ExternalCatalog externalCatalog = (ExternalCatalog ) catalog ;
134
- Map <String , String > hadoopProps = externalCatalog .getCatalogProperty ().getHadoopProperties ();
135
- icebergMetadataParams .setHadoopProps (hadoopProps );
136
- metaScanRange .setIcebergParams (icebergMetadataParams );
137
- return metaScanRange ;
135
+ public List <TMetaScanRange > getMetaScanRanges () {
136
+ List <TMetaScanRange > scanRanges = Lists .newArrayList ();
137
+ List <String > splits = getSplits ();
138
+ for (String split : splits ) {
139
+ TMetaScanRange metaScanRange = new TMetaScanRange ();
140
+ metaScanRange .setMetadataType (TMetadataType .ICEBERG );
141
+ // set iceberg metadata params
142
+ TIcebergMetadataParams icebergMetadataParams = new TIcebergMetadataParams ();
143
+ icebergMetadataParams .setIcebergQueryType (queryType );
144
+ icebergMetadataParams .setCatalog (icebergTableName .getCtl ());
145
+ icebergMetadataParams .setDatabase (icebergTableName .getDb ());
146
+ icebergMetadataParams .setTable (icebergTableName .getTbl ());
147
+ String serializedTable = SerializationUtil .serializeToBase64 (table );
148
+ icebergMetadataParams .setSerializedTable (serializedTable );
149
+ icebergMetadataParams .setHadoopProps (hadoopProps );
150
+ icebergMetadataParams .setSerializedSplit (split );
151
+ metaScanRange .setIcebergParams (icebergMetadataParams );
152
+ scanRanges .add (metaScanRange );
153
+ }
154
+ return scanRanges ;
138
155
}
139
156
140
157
@ Override
141
158
public String getTableName () {
142
- return "IcebergMetadataTableValuedFunction" ;
159
+ String queryTypeString = queryType .name ().toLowerCase ();
160
+ return "IcebergTableValuedFunction<" + queryTypeString + ">" ;
143
161
}
144
162
145
- /**
146
- * The tvf can register columns of metadata table
147
- * The data is provided by getIcebergMetadataTable in FrontendService
148
- *
149
- * @return metadata columns
150
- * @see org.apache.doris.service.FrontendServiceImpl
151
- */
152
163
@ Override
153
164
public List <Column > getTableColumns () {
154
- if (queryType == TIcebergQueryType .SNAPSHOTS ) {
155
- return SCHEMA_SNAPSHOT ;
156
- }
157
- return Lists .newArrayList ();
165
+ return getSchema ();
158
166
}
167
+
168
+ /**
169
+ * Get the splits for the iceberg table valued function.
170
+ * This method can be overridden to provide multiple splits for the table.
171
+ *
172
+ * @return a list of splits
173
+ */
174
+ protected List <String > getSplits () {
175
+ return Lists .newArrayList ("" );
176
+ }
177
+
178
+ /**
179
+ * Get the schema for the iceberg table valued function.
180
+ *
181
+ * @return a list of columns representing the schema
182
+ */
183
+ protected abstract List <Column > getSchema ();
159
184
}
0 commit comments