|
18 | 18 | package org.apache.doris.datasource.hive; |
19 | 19 |
|
20 | 20 | import org.apache.doris.analysis.TableName; |
| 21 | +import org.apache.doris.catalog.Column; |
21 | 22 | import org.apache.doris.common.Config; |
22 | 23 | import org.apache.doris.datasource.DatabaseMetadata; |
23 | 24 | import org.apache.doris.datasource.TableMetadata; |
|
46 | 47 | import org.apache.hadoop.hive.metastore.api.CurrentNotificationEventId; |
47 | 48 | import org.apache.hadoop.hive.metastore.api.DataOperationType; |
48 | 49 | import org.apache.hadoop.hive.metastore.api.Database; |
| 50 | +import org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest; |
49 | 51 | import org.apache.hadoop.hive.metastore.api.FieldSchema; |
50 | 52 | import org.apache.hadoop.hive.metastore.api.LockComponent; |
51 | 53 | import org.apache.hadoop.hive.metastore.api.LockResponse; |
52 | 54 | import org.apache.hadoop.hive.metastore.api.LockState; |
53 | 55 | import org.apache.hadoop.hive.metastore.api.MetaException; |
54 | 56 | import org.apache.hadoop.hive.metastore.api.NotificationEventResponse; |
55 | 57 | import org.apache.hadoop.hive.metastore.api.Partition; |
| 58 | +import org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint; |
56 | 59 | import org.apache.hadoop.hive.metastore.api.Table; |
57 | 60 | import org.apache.hadoop.hive.metastore.api.TableValidWriteIds; |
58 | 61 | import org.apache.hadoop.hive.metastore.txn.TxnUtils; |
59 | 62 | import org.apache.logging.log4j.LogManager; |
60 | 63 | import org.apache.logging.log4j.Logger; |
| 64 | +import shade.doris.hive.org.apache.thrift.TApplicationException; |
61 | 65 |
|
62 | 66 | import java.security.PrivilegedExceptionAction; |
| 67 | +import java.util.ArrayList; |
63 | 68 | import java.util.BitSet; |
64 | 69 | import java.util.Collections; |
| 70 | +import java.util.HashMap; |
65 | 71 | import java.util.LinkedList; |
66 | 72 | import java.util.List; |
| 73 | +import java.util.Locale; |
67 | 74 | import java.util.Map; |
68 | 75 | import java.util.Optional; |
69 | 76 | import java.util.Queue; |
@@ -153,8 +160,28 @@ public void createTable(TableMetadata tbl, boolean ignoreIfExists) { |
153 | 160 | try { |
154 | 161 | // String location, |
155 | 162 | if (tbl instanceof HiveTableMetadata) { |
| 163 | + Table hiveTable = HiveUtil.toHiveTable((HiveTableMetadata) tbl); |
| 164 | + List<Column> tableColumns = ((HiveTableMetadata) tbl).getColumns(); |
| 165 | + List<SQLDefaultConstraint> dvs = new ArrayList<>(tableColumns.size()); |
| 166 | + for (Column tableColumn : tableColumns) { |
| 167 | + if (tableColumn.hasDefaultValue()) { |
| 168 | + SQLDefaultConstraint dv = new SQLDefaultConstraint(); |
| 169 | + dv.setTable_db(tbl.getDbName()); |
| 170 | + dv.setTable_name(tbl.getTableName()); |
| 171 | + dv.setColumn_name(tableColumn.getName()); |
| 172 | + dv.setDefault_value(tableColumn.getDefaultValue()); |
| 173 | + dv.setDc_name(tableColumn.getName() + "_dv_constraint"); |
| 174 | + dvs.add(dv); |
| 175 | + } |
| 176 | + } |
156 | 177 | ugiDoAs(() -> { |
157 | | - client.client.createTable(HiveUtil.toHiveTable((HiveTableMetadata) tbl)); |
| 178 | + if (!dvs.isEmpty()) { |
| 179 | + // foreignKeys, uniqueConstraints, notNullConstraints, defaultConstraints, checkConstraints |
| 180 | + client.client.createTableWithConstraints(hiveTable, null, |
| 181 | + null, null, null, dvs, null); |
| 182 | + return null; |
| 183 | + } |
| 184 | + client.client.createTable(hiveTable); |
158 | 185 | return null; |
159 | 186 | }); |
160 | 187 | } |
@@ -293,6 +320,37 @@ public Database getDatabase(String dbName) { |
293 | 320 | } |
294 | 321 | } |
295 | 322 |
|
| 323 | + public Map<String, String> getDefaultColumnValues(String dbName, String tblName) { |
| 324 | + Map<String, String> res = new HashMap<>(); |
| 325 | + try (ThriftHMSClient client = getClient()) { |
| 326 | + try { |
| 327 | + DefaultConstraintsRequest req = new DefaultConstraintsRequest(); |
| 328 | + req.setDb_name(dbName); |
| 329 | + req.setTbl_name(tblName); |
| 330 | + List<SQLDefaultConstraint> dvcs = ugiDoAs(() -> { |
| 331 | + try { |
| 332 | + return client.client.getDefaultConstraints(req); |
| 333 | + } catch (TApplicationException e) { |
| 334 | + if (e.getMessage().contains("Invalid method name: 'get_default_constraints'")) { |
| 335 | + // the getDefaultConstraints method only supported on hive3 |
| 336 | + return ImmutableList.of(); |
| 337 | + } |
| 338 | + throw e; |
| 339 | + } |
| 340 | + }); |
| 341 | + for (SQLDefaultConstraint dvc : dvcs) { |
| 342 | + res.put(dvc.getColumn_name().toLowerCase(Locale.ROOT), dvc.getDefault_value()); |
| 343 | + } |
| 344 | + return res; |
| 345 | + } catch (Exception e) { |
| 346 | + client.setThrowable(e); |
| 347 | + throw e; |
| 348 | + } |
| 349 | + } catch (Exception e) { |
| 350 | + throw new HMSClientException("failed to get table %s in db %s from hms client", e, tblName, dbName); |
| 351 | + } |
| 352 | + } |
| 353 | + |
296 | 354 | @Override |
297 | 355 | public Table getTable(String dbName, String tblName) { |
298 | 356 | try (ThriftHMSClient client = getClient()) { |
|
0 commit comments