15 package com.cloudera.impala.service;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
26 import java.util.Random;
28 import java.util.UUID;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.ScheduledExecutorService;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.atomic.AtomicReference;
34 import org.apache.hadoop.fs.FileSystem;
35 import org.apache.hadoop.fs.Path;
36 import org.apache.hive.service.cli.thrift.TGetColumnsReq;
37 import org.apache.hive.service.cli.thrift.TGetFunctionsReq;
38 import org.apache.hive.service.cli.thrift.TGetSchemasReq;
39 import org.apache.hive.service.cli.thrift.TGetTablesReq;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
91 import com.cloudera.impala.thrift.TCatalogOpRequest;
92 import com.cloudera.impala.thrift.TCatalogOpType;
93 import com.cloudera.impala.thrift.TCatalogServiceRequestHeader;
94 import com.cloudera.impala.thrift.TColumn;
95 import com.cloudera.impala.thrift.TColumnValue;
96 import com.cloudera.impala.thrift.TCreateDropRoleParams;
97 import com.cloudera.impala.thrift.TDdlExecRequest;
98 import com.cloudera.impala.thrift.TDdlType;
99 import com.cloudera.impala.thrift.TDescribeTableOutputStyle;
100 import com.cloudera.impala.thrift.TDescribeTableResult;
101 import com.cloudera.impala.thrift.TErrorCode;
102 import com.cloudera.impala.thrift.TExecRequest;
103 import com.cloudera.impala.thrift.TExplainLevel;
104 import com.cloudera.impala.thrift.TExplainResult;
105 import com.cloudera.impala.thrift.TFinalizeParams;
106 import com.cloudera.impala.thrift.TFunctionCategory;
107 import com.cloudera.impala.thrift.TGrantRevokePrivParams;
108 import com.cloudera.impala.thrift.TGrantRevokeRoleParams;
109 import com.cloudera.impala.thrift.TLoadDataReq;
110 import com.cloudera.impala.thrift.TLoadDataResp;
111 import com.cloudera.impala.thrift.TMetadataOpRequest;
112 import com.cloudera.impala.thrift.TPlanFragment;
113 import com.cloudera.impala.thrift.TQueryCtx;
114 import com.cloudera.impala.thrift.TQueryExecRequest;
115 import com.cloudera.impala.thrift.TResetMetadataRequest;
116 import com.cloudera.impala.thrift.TResultRow;
117 import com.cloudera.impala.thrift.TResultSet;
118 import com.cloudera.impala.thrift.TResultSetMetadata;
119 import com.cloudera.impala.thrift.TShowFilesParams;
120 import com.cloudera.impala.thrift.TStatus;
121 import com.cloudera.impala.thrift.TStmtType;
122 import com.cloudera.impala.thrift.TTableName;
123 import com.cloudera.impala.thrift.TUpdateCatalogCacheRequest;
124 import com.cloudera.impala.thrift.TUpdateCatalogCacheResponse;
129 import com.google.common.base.Joiner;
130 import com.google.common.base.Preconditions;
131 import com.google.common.base.Predicates;
132 import com.google.common.collect.Lists;
133 import com.google.common.collect.Maps;
134 import com.google.common.collect.Sets;
143 private final static Logger
LOG = LoggerFactory.getLogger(Frontend.class);
157 Executors.newScheduledThreadPool(1);
174 Random randomGen =
new Random(UUID.randomUUID().hashCode());
177 policyReader_.scheduleAtFixedRate(
195 LOG.info(
"Reloading authorization policy file from: " + config_.getPolicyFile());
198 }
catch (Exception e) {
199 LOG.error(
"Error reloading policy file: ", e);
215 TUpdateCatalogCacheResponse response = catalog.updateCatalog(req);
233 TExecRequest result) {
234 TCatalogOpRequest ddl =
new TCatalogOpRequest();
235 TResultSetMetadata metadata =
new TResultSetMetadata();
236 if (analysis.isUseStmt()) {
237 ddl.op_type = TCatalogOpType.USE;
238 ddl.setUse_db_params(analysis.getUseStmt().toThrift());
239 metadata.setColumns(Collections.<TColumn>emptyList());
240 }
else if (analysis.isShowTablesStmt()) {
241 ddl.op_type = TCatalogOpType.SHOW_TABLES;
242 ddl.setShow_tables_params(analysis.getShowTablesStmt().toThrift());
243 metadata.setColumns(Arrays.asList(
245 }
else if (analysis.isShowDbsStmt()) {
246 ddl.op_type = TCatalogOpType.SHOW_DBS;
247 ddl.setShow_dbs_params(analysis.getShowDbsStmt().toThrift());
248 metadata.setColumns(Arrays.asList(
250 }
else if (analysis.isShowDataSrcsStmt()) {
251 ddl.op_type = TCatalogOpType.SHOW_DATA_SRCS;
252 ddl.setShow_data_srcs_params(analysis.getShowDataSrcsStmt().toThrift());
253 metadata.setColumns(Arrays.asList(
258 }
else if (analysis.isShowStatsStmt()) {
259 ddl.op_type = TCatalogOpType.SHOW_STATS;
260 ddl.setShow_stats_params(analysis.getShowStatsStmt().toThrift());
261 metadata.setColumns(Arrays.asList(
263 }
else if (analysis.isShowFunctionsStmt()) {
264 ddl.op_type = TCatalogOpType.SHOW_FUNCTIONS;
266 ddl.setShow_fns_params(stmt.toThrift());
267 metadata.setColumns(Arrays.asList(
270 }
else if (analysis.isShowCreateTableStmt()) {
271 ddl.op_type = TCatalogOpType.SHOW_CREATE_TABLE;
272 ddl.setShow_create_table_params(analysis.getShowCreateTableStmt().toThrift());
273 metadata.setColumns(Arrays.asList(
275 }
else if (analysis.isShowFilesStmt()) {
276 ddl.op_type = TCatalogOpType.SHOW_FILES;
277 ddl.setShow_files_params(analysis.getShowFilesStmt().toThrift());
278 metadata.setColumns(Collections.<TColumn>emptyList());
279 }
else if (analysis.isDescribeStmt()) {
280 ddl.op_type = TCatalogOpType.DESCRIBE;
281 ddl.setDescribe_table_params(analysis.getDescribeStmt().toThrift());
282 metadata.setColumns(Arrays.asList(
286 }
else if (analysis.isAlterTableStmt()) {
287 ddl.op_type = TCatalogOpType.DDL;
288 TDdlExecRequest req =
new TDdlExecRequest();
289 req.setDdl_type(TDdlType.ALTER_TABLE);
290 req.setAlter_table_params(analysis.getAlterTableStmt().toThrift());
291 ddl.setDdl_params(req);
292 metadata.setColumns(Collections.<TColumn>emptyList());
293 }
else if (analysis.isAlterViewStmt()) {
294 ddl.op_type = TCatalogOpType.DDL;
295 TDdlExecRequest req =
new TDdlExecRequest();
296 req.setDdl_type(TDdlType.ALTER_VIEW);
297 req.setAlter_view_params(analysis.getAlterViewStmt().toThrift());
298 ddl.setDdl_params(req);
299 metadata.setColumns(Collections.<TColumn>emptyList());
300 }
else if (analysis.isCreateTableStmt()) {
301 ddl.op_type = TCatalogOpType.DDL;
302 TDdlExecRequest req =
new TDdlExecRequest();
303 req.setDdl_type(TDdlType.CREATE_TABLE);
304 req.setCreate_table_params(analysis.getCreateTableStmt().toThrift());
305 ddl.setDdl_params(req);
306 metadata.setColumns(Collections.<TColumn>emptyList());
307 }
else if (analysis.isCreateTableAsSelectStmt()) {
308 ddl.op_type = TCatalogOpType.DDL;
309 TDdlExecRequest req =
new TDdlExecRequest();
310 req.setDdl_type(TDdlType.CREATE_TABLE_AS_SELECT);
311 req.setCreate_table_params(
312 analysis.getCreateTableAsSelectStmt().getCreateStmt().toThrift());
313 ddl.setDdl_params(req);
314 metadata.setColumns(Arrays.asList(
316 }
else if (analysis.isCreateTableLikeStmt()) {
317 ddl.op_type = TCatalogOpType.DDL;
318 TDdlExecRequest req =
new TDdlExecRequest();
319 req.setDdl_type(TDdlType.CREATE_TABLE_LIKE);
320 req.setCreate_table_like_params(analysis.getCreateTableLikeStmt().toThrift());
321 ddl.setDdl_params(req);
322 metadata.setColumns(Collections.<TColumn>emptyList());
323 }
else if (analysis.isCreateViewStmt()) {
324 ddl.op_type = TCatalogOpType.DDL;
325 TDdlExecRequest req =
new TDdlExecRequest();
326 req.setDdl_type(TDdlType.CREATE_VIEW);
327 req.setCreate_view_params(analysis.getCreateViewStmt().toThrift());
328 ddl.setDdl_params(req);
329 metadata.setColumns(Collections.<TColumn>emptyList());
330 }
else if (analysis.isCreateDbStmt()) {
331 ddl.op_type = TCatalogOpType.DDL;
332 TDdlExecRequest req =
new TDdlExecRequest();
333 req.setDdl_type(TDdlType.CREATE_DATABASE);
334 req.setCreate_db_params(analysis.getCreateDbStmt().toThrift());
335 ddl.setDdl_params(req);
336 metadata.setColumns(Collections.<TColumn>emptyList());
337 }
else if (analysis.isCreateUdfStmt()) {
338 ddl.op_type = TCatalogOpType.DDL;
340 TDdlExecRequest req =
new TDdlExecRequest();
341 req.setDdl_type(TDdlType.CREATE_FUNCTION);
342 req.setCreate_fn_params(stmt.toThrift());
343 ddl.setDdl_params(req);
344 metadata.setColumns(Collections.<TColumn>emptyList());
345 }
else if (analysis.isCreateUdaStmt()) {
346 ddl.op_type = TCatalogOpType.DDL;
347 TDdlExecRequest req =
new TDdlExecRequest();
348 req.setDdl_type(TDdlType.CREATE_FUNCTION);
350 req.setCreate_fn_params(stmt.toThrift());
351 ddl.setDdl_params(req);
352 metadata.setColumns(Collections.<TColumn>emptyList());
353 }
else if (analysis.isCreateDataSrcStmt()) {
354 ddl.op_type = TCatalogOpType.DDL;
355 TDdlExecRequest req =
new TDdlExecRequest();
356 req.setDdl_type(TDdlType.CREATE_DATA_SOURCE);
358 req.setCreate_data_source_params(stmt.toThrift());
359 ddl.setDdl_params(req);
360 metadata.setColumns(Collections.<TColumn>emptyList());
361 }
else if (analysis.isComputeStatsStmt()) {
362 ddl.op_type = TCatalogOpType.DDL;
363 TDdlExecRequest req =
new TDdlExecRequest();
364 req.setDdl_type(TDdlType.COMPUTE_STATS);
365 req.setCompute_stats_params(analysis.getComputeStatsStmt().toThrift());
366 ddl.setDdl_params(req);
367 metadata.setColumns(Collections.<TColumn>emptyList());
368 }
else if (analysis.isDropDbStmt()) {
369 ddl.op_type = TCatalogOpType.DDL;
370 TDdlExecRequest req =
new TDdlExecRequest();
371 req.setDdl_type(TDdlType.DROP_DATABASE);
372 req.setDrop_db_params(analysis.getDropDbStmt().toThrift());
373 ddl.setDdl_params(req);
374 metadata.setColumns(Collections.<TColumn>emptyList());
375 }
else if (analysis.isDropTableOrViewStmt()) {
376 ddl.op_type = TCatalogOpType.DDL;
377 TDdlExecRequest req =
new TDdlExecRequest();
379 req.setDdl_type(stmt.isDropTable() ? TDdlType.DROP_TABLE : TDdlType.DROP_VIEW);
380 req.setDrop_table_or_view_params(stmt.toThrift());
381 ddl.setDdl_params(req);
382 metadata.setColumns(Collections.<TColumn>emptyList());
383 }
else if (analysis.isDropFunctionStmt()) {
384 ddl.op_type = TCatalogOpType.DDL;
385 TDdlExecRequest req =
new TDdlExecRequest();
386 req.setDdl_type(TDdlType.DROP_FUNCTION);
388 req.setDrop_fn_params(stmt.toThrift());
389 ddl.setDdl_params(req);
390 metadata.setColumns(Collections.<TColumn>emptyList());
391 }
else if (analysis.isDropDataSrcStmt()) {
392 ddl.op_type = TCatalogOpType.DDL;
393 TDdlExecRequest req =
new TDdlExecRequest();
394 req.setDdl_type(TDdlType.DROP_DATA_SOURCE);
396 req.setDrop_data_source_params(stmt.toThrift());
397 ddl.setDdl_params(req);
398 metadata.setColumns(Collections.<TColumn>emptyList());
399 }
else if (analysis.isDropStatsStmt()) {
400 ddl.op_type = TCatalogOpType.DDL;
401 TDdlExecRequest req =
new TDdlExecRequest();
402 req.setDdl_type(TDdlType.DROP_STATS);
404 req.setDrop_stats_params(stmt.toThrift());
405 ddl.setDdl_params(req);
406 metadata.setColumns(Collections.<TColumn>emptyList());
407 }
else if (analysis.isResetMetadataStmt()) {
408 ddl.op_type = TCatalogOpType.RESET_METADATA;
410 TResetMetadataRequest req = resetMetadataStmt.toThrift();
411 ddl.setReset_metadata_params(req);
412 metadata.setColumns(Collections.<TColumn>emptyList());
413 }
else if (analysis.isShowRolesStmt()) {
414 ddl.op_type = TCatalogOpType.SHOW_ROLES;
416 ddl.setShow_roles_params(showRolesStmt.toThrift());
417 Set<String> groupNames =
422 Preconditions.checkState(ddl.getShow_roles_params().isSetIs_admin_op());
423 if (ddl.getShow_roles_params().isSetGrant_group() &&
424 groupNames.contains(ddl.getShow_roles_params().getGrant_group())) {
425 ddl.getShow_roles_params().setIs_admin_op(
false);
427 metadata.setColumns(Arrays.asList(
429 }
else if (analysis.isShowGrantRoleStmt()) {
430 ddl.op_type = TCatalogOpType.SHOW_GRANT_ROLE;
432 ddl.setShow_grant_role_params(showGrantRoleStmt.toThrift());
433 Set<String> groupNames =
437 ddl.getShow_grant_role_params().setIs_admin_op(Sets.intersection(groupNames,
438 showGrantRoleStmt.
getRole().getGrantGroups()).isEmpty());
439 metadata.setColumns(Arrays.asList(
441 }
else if (analysis.isCreateDropRoleStmt()) {
443 TCreateDropRoleParams params = createDropRoleStmt.toThrift();
444 TDdlExecRequest req =
new TDdlExecRequest();
445 req.setDdl_type(params.isIs_drop() ? TDdlType.DROP_ROLE : TDdlType.CREATE_ROLE);
446 req.setCreate_drop_role_params(params);
447 ddl.op_type = TCatalogOpType.DDL;
448 ddl.setDdl_params(req);
449 metadata.setColumns(Collections.<TColumn>emptyList());
450 }
else if (analysis.isGrantRevokeRoleStmt()) {
452 TGrantRevokeRoleParams params = grantRoleStmt.toThrift();
453 TDdlExecRequest req =
new TDdlExecRequest();
454 req.setDdl_type(params.isIs_grant() ? TDdlType.GRANT_ROLE : TDdlType.REVOKE_ROLE);
455 req.setGrant_revoke_role_params(params);
456 ddl.op_type = TCatalogOpType.DDL;
457 ddl.setDdl_params(req);
458 metadata.setColumns(Collections.<TColumn>emptyList());
459 }
else if (analysis.isGrantRevokePrivStmt()) {
461 TGrantRevokePrivParams params = grantRevokePrivStmt.toThrift();
462 TDdlExecRequest req =
new TDdlExecRequest();
463 req.setDdl_type(params.isIs_grant() ?
464 TDdlType.GRANT_PRIVILEGE : TDdlType.REVOKE_PRIVILEGE);
465 req.setGrant_revoke_priv_params(params);
466 ddl.op_type = TCatalogOpType.DDL;
467 ddl.setDdl_params(req);
468 metadata.setColumns(Collections.<TColumn>emptyList());
470 throw new IllegalStateException(
"Unexpected CatalogOp statement type.");
473 result.setResult_set_metadata(metadata);
474 result.setCatalog_op_request(ddl);
475 if (ddl.getOp_type() == TCatalogOpType.DDL) {
476 TCatalogServiceRequestHeader header =
new TCatalogServiceRequestHeader();
477 header.setRequesting_user(analysis.getAnalyzer().getUser().getName());
478 ddl.getDdl_params().setHeader(header);
490 TableName tableName = TableName.fromThrift(request.getTable_name());
494 String destPathString = null;
495 if (request.isSetPartition_spec()) {
496 destPathString = impaladCatalog_.getHdfsPartition(tableName.getDb(),
497 tableName.
getTbl(), request.getPartition_spec()).getLocation();
499 destPathString = impaladCatalog_.getTable(tableName.getDb(), tableName.
getTbl())
500 .getMetaStoreTable().getSd().getLocation();
503 Path destPath =
new Path(destPathString);
504 FileSystem fs = destPath.getFileSystem(FileSystemUtil.getConfiguration());
508 Path tmpDestPath = FileSystemUtil.makeTmpSubdirectory(destPath);
510 Path sourcePath =
new Path(request.source_path);
512 if (fs.isDirectory(sourcePath)) {
513 filesLoaded = FileSystemUtil.relocateAllVisibleFiles(sourcePath, tmpDestPath);
515 FileSystemUtil.relocateFile(sourcePath, tmpDestPath,
true);
520 if (request.isOverwrite()) {
521 FileSystemUtil.deleteAllVisibleFiles(destPath);
525 FileSystemUtil.relocateAllVisibleFiles(tmpDestPath, destPath);
527 fs.delete(tmpDestPath,
true);
528 TLoadDataResp response =
new TLoadDataResp();
529 TColumnValue col =
new TColumnValue();
530 String loadMsg = String.format(
531 "Loaded %d file(s). Total files in destination location: %d",
532 filesLoaded, FileSystemUtil.getTotalNumVisibleFiles(destPath));
533 col.setString_val(loadMsg);
534 response.setLoad_summary(
new TResultRow(Lists.newArrayList(col)));
543 StringBuilder stringBuilder =
new StringBuilder();
545 return stringBuilder.toString();
555 List<String> tblNames = impaladCatalog_.getTableNames(dbName, tablePattern);
557 Iterator<String> iter = tblNames.iterator();
558 while (iter.hasNext()) {
560 .allOf(
Privilege.ANY).onTable(dbName, iter.next()).toRequest();
561 if (!
authzChecker_.get().hasAccess(user, privilegeRequest)) {
574 List<String> dbNames = impaladCatalog_.getDbNames(dbPattern);
578 Iterator<String> iter = dbNames.iterator();
579 while (iter.hasNext()) {
580 String dbName = iter.next();
582 if (dbName.toLowerCase().equals(Catalog.DEFAULT_DB.toLowerCase()))
continue;
584 .any().onAnyTable(dbName).toRequest();
598 return impaladCatalog_.getDataSources(pattern);
606 Table table = impaladCatalog_.getTable(dbName, tableName);
607 TResultSet result =
new TResultSet();
608 TResultSetMetadata resultSchema =
new TResultSetMetadata();
609 result.setSchema(resultSchema);
612 resultSchema.addToColumns(
615 resultSchema.addToColumns(
new TColumn(
"Max Size",
Type.
INT.
toThrift()));
621 rowBuilder.add(c.getName()).add(c.getType().toSql())
622 .add(c.getStats().getNumDistinctValues()).add(c.getStats().getNumNulls())
623 .add(c.getStats().getMaxSize()).add(c.getStats().getAvgSize());
624 result.addToRows(rowBuilder.get());
634 Table table = impaladCatalog_.getTable(dbName, tableName);
651 String dbName, String fnPattern)
653 Db db = impaladCatalog_.getDb(dbName);
657 List<Function> fns = db.getFunctions(
658 category, PatternMatcher.createHivePatternMatcher(fnPattern));
659 Collections.sort(fns,
660 new Comparator<Function>() {
675 Table table = impaladCatalog_.getTable(dbName, tableName);
676 return DescribeResultFactory.buildDescribeTableResult(table, outputStyle);
684 Set<TableName> missingTbls =
new HashSet<TableName>();
687 if (db == null)
continue;
688 Table tbl = db.getTable(tblName.getTbl());
689 if (tbl == null)
continue;
690 if (!tbl.
isLoaded()) missingTbls.add(tblName);
712 if (missingTbls.isEmpty())
return true;
715 LOG.info(String.format(
"Requesting prioritized load of table(s): %s",
716 Joiner.on(
", ").join(missingTbls)));
717 TStatus status = FeSupport.PrioritizeLoad(missingTbls);
720 Joiner.on(
"\n").join(status.getError_msgs()));
723 long startTimeMs = System.currentTimeMillis();
725 while (!missingTbls.isEmpty()) {
727 if (timeoutMs > 0 && System.currentTimeMillis() - startTimeMs > timeoutMs) {
731 LOG.trace(String.format(
"Waiting for table(s) to complete loading: %s",
732 Joiner.on(
", ").join(missingTbls)));
762 private AnalysisContext.AnalysisResult
analyzeStmt(TQueryCtx queryCtx)
766 LOG.debug(
"analyze query " + queryCtx.request.stmt);
775 analysisCtx.analyze(queryCtx.request.stmt);
776 Preconditions.checkState(analysisCtx.getAnalyzer().
getMissingTbls().isEmpty());
777 return analysisCtx.getAnalysisResult();
779 Set<TableName> missingTbls = analysisCtx.getAnalyzer().
getMissingTbls();
781 if (missingTbls.isEmpty())
throw e;
785 LOG.info(String.format(
"Missing tables were not received in %dms. Load " +
804 AnalysisContext.AnalysisResult analysisResult =
analyzeStmt(queryCtx);
805 EventSequence timeline = analysisResult.getAnalyzer().getTimeline();
806 timeline.markEvent(
"Analysis finished");
807 Preconditions.checkNotNull(analysisResult.getStmt());
808 TExecRequest result =
new TExecRequest();
809 result.setQuery_options(queryCtx.request.getQuery_options());
810 result.setAccess_events(analysisResult.getAccessEvents());
811 result.analysis_warnings = analysisResult.getAnalyzer().getWarnings();
813 if (analysisResult.isCatalogOp()) {
814 result.stmt_type = TStmtType.DDL;
816 String jsonLineageGraph = analysisResult.getJsonLineageGraph();
817 if (jsonLineageGraph != null && !jsonLineageGraph.isEmpty()) {
818 result.catalog_op_request.setLineage_graph(jsonLineageGraph);
821 if (!analysisResult.isCreateTableAsSelectStmt())
return result;
822 }
else if (analysisResult.isLoadDataStmt()) {
823 result.stmt_type = TStmtType.LOAD;
824 result.setResult_set_metadata(
new TResultSetMetadata(Arrays.asList(
826 result.setLoad_data_request(analysisResult.getLoadDataStmt().toThrift());
828 }
else if (analysisResult.isSetStmt()) {
829 result.stmt_type = TStmtType.SET;
830 result.setResult_set_metadata(
new TResultSetMetadata(Arrays.asList(
833 result.setSet_query_option_request(analysisResult.getSetStmt().toThrift());
838 Preconditions.checkState(analysisResult.isQueryStmt() || analysisResult.isDmlStmt()
839 || analysisResult.isCreateTableAsSelectStmt());
841 TQueryExecRequest queryExecRequest =
new TQueryExecRequest();
843 LOG.debug(
"create plan");
845 ArrayList<PlanFragment> fragments = planner.createPlan();
847 List<ScanNode> scanNodes = Lists.newArrayList();
850 Map<PlanFragment, Integer> fragmentIdx = Maps.newHashMap();
852 for (
int fragmentId = 0; fragmentId < fragments.size(); ++fragmentId) {
854 Preconditions.checkNotNull(fragment.getPlanRoot());
855 fragment.getPlanRoot().collect(Predicates.instanceOf(
ScanNode.class), scanNodes);
856 fragmentIdx.put(fragment, fragmentId);
860 for (
int i = 1; i < fragments.size(); ++i) {
862 Integer
idx = fragmentIdx.get(dest);
863 Preconditions.checkState(idx != null);
864 queryExecRequest.addToDest_fragment_idx(idx.intValue());
869 LOG.debug(
"get scan range locations");
870 Set<TTableName> tablesMissingStats = Sets.newTreeSet();
871 for (
ScanNode scanNode: scanNodes) {
872 queryExecRequest.putToPer_node_scan_ranges(
873 scanNode.getId().asInt(),
874 scanNode.getScanRangeLocations());
875 if (scanNode.isTableMissingStats()) {
876 tablesMissingStats.add(scanNode.getTupleDesc().getTableName().toThrift());
879 queryExecRequest.setHost_list(analysisResult.getAnalyzer().getHostIndex().getList());
880 for (TTableName tableName: tablesMissingStats) {
881 queryCtx.addToTables_missing_stats(tableName);
886 if (queryCtx.request.query_options.isDisable_unsafe_spills()
887 && !tablesMissingStats.isEmpty()
888 && !analysisResult.getAnalyzer().hasPlanHints()) {
889 queryCtx.setDisable_spilling(
true);
895 planner.computeResourceReqs(fragments,
true, queryExecRequest);
896 }
catch (Exception e) {
898 LOG.error(
"Failed to compute resource requirements for query\n" +
899 queryCtx.request.getStmt(), e);
904 TPlanFragment thriftFragment = fragment.toThrift();
905 queryExecRequest.addToFragments(thriftFragment);
909 TExplainLevel explainLevel = TExplainLevel.VERBOSE;
911 if (analysisResult.isExplainStmt() || RuntimeEnv.INSTANCE.isTestEnv()) {
912 explainLevel = queryCtx.request.query_options.getExplain_level();
916 queryExecRequest.setQuery_ctx(queryCtx);
918 explainString.append(
919 planner.getExplainString(fragments, queryExecRequest, explainLevel));
920 queryExecRequest.setQuery_plan(explainString.toString());
921 queryExecRequest.setDesc_tbl(analysisResult.getAnalyzer().getDescTbl().toThrift());
923 String jsonLineageGraph = analysisResult.getJsonLineageGraph();
924 if (jsonLineageGraph != null && !jsonLineageGraph.isEmpty()) {
925 queryExecRequest.setLineage_graph(jsonLineageGraph);
928 if (analysisResult.isExplainStmt()) {
934 result.setQuery_exec_request(queryExecRequest);
936 if (analysisResult.isQueryStmt()) {
938 LOG.debug(
"create result set metadata");
939 result.stmt_type = TStmtType.QUERY;
940 result.query_exec_request.stmt_type = result.stmt_type;
941 TResultSetMetadata metadata =
new TResultSetMetadata();
942 QueryStmt queryStmt = analysisResult.getQueryStmt();
943 int colCnt = queryStmt.getColLabels().size();
944 for (
int i = 0; i < colCnt; ++i) {
945 TColumn colDesc =
new TColumn();
946 colDesc.columnName = queryStmt.getColLabels().
get(i);
947 colDesc.columnType = queryStmt.getResultExprs().
get(i).getType().toThrift();
948 metadata.addToColumns(colDesc);
950 result.setResult_set_metadata(metadata);
952 Preconditions.checkState(analysisResult.isInsertStmt() ||
953 analysisResult.isCreateTableAsSelectStmt());
958 analysisResult.isCreateTableAsSelectStmt() ? TStmtType.DDL : TStmtType.DML;
959 result.query_exec_request.stmt_type = TStmtType.DML;
962 InsertStmt insertStmt = analysisResult.getInsertStmt();
963 if (insertStmt.getTargetTable() instanceof
HdfsTable) {
964 TFinalizeParams finalizeParams =
new TFinalizeParams();
965 finalizeParams.setIs_overwrite(insertStmt.isOverwrite());
966 finalizeParams.setTable_name(insertStmt.getTargetTableName().getTbl());
967 finalizeParams.setTable_id(insertStmt.getTargetTable().getId().asInt());
968 String db = insertStmt.getTargetTableName().getDb();
969 finalizeParams.setTable_db(db == null ? queryCtx.session.database : db);
971 finalizeParams.setHdfs_base_dir(hdfsTable.getHdfsBaseDir());
972 finalizeParams.setStaging_dir(
973 hdfsTable.getHdfsBaseDir() +
"/_impala_insert_staging");
974 queryExecRequest.setFinalize_params(finalizeParams);
978 timeline.markEvent(
"Planning finished");
979 result.setTimeline(analysisResult.getAnalyzer().getTimeline().toThrift());
989 TResultSetMetadata metadata =
new TResultSetMetadata(Lists.newArrayList(colDesc));
990 result.setResult_set_metadata(metadata);
993 String[] explainStringArray = explainString.toString().split(
"\n");
994 TExplainResult explainResult =
new TExplainResult();
995 explainResult.results = Lists.newArrayList();
996 for (
int i = 0; i < explainStringArray.length; ++i) {
997 TColumnValue col =
new TColumnValue();
998 col.setString_val(explainStringArray[i]);
999 TResultRow row =
new TResultRow(Lists.newArrayList(col));
1000 explainResult.results.add(row);
1002 result.setExplain_result(explainResult);
1003 result.stmt_type = TStmtType.EXPLAIN;
1011 User user = request.isSetSession() ?
1014 switch (request.opcode) {
1015 case GET_TYPE_INFO:
return MetadataOp.getTypeInfo();
1018 TGetSchemasReq req = request.getGet_schemas_req();
1019 return MetadataOp.getSchemas(
this, req.getCatalogName(),
1020 req.getSchemaName(), user);
1024 TGetTablesReq req = request.getGet_tables_req();
1025 return MetadataOp.getTables(
this, req.getCatalogName(),
1026 req.getSchemaName(), req.getTableName(), req.getTableTypes(), user);
1030 TGetColumnsReq req = request.getGet_columns_req();
1031 return MetadataOp.getColumns(
this, req.getCatalogName(),
1032 req.getSchemaName(), req.getTableName(), req.getColumnName(), user);
1034 case GET_CATALOGS:
return MetadataOp.getCatalogs();
1035 case GET_TABLE_TYPES:
return MetadataOp.getTableTypes();
1038 TGetFunctionsReq req = request.getGet_functions_req();
1039 return MetadataOp.getFunctions(
this, req.getCatalogName(),
1040 req.getSchemaName(), req.getFunctionName(), user);
1052 Table table = impaladCatalog_.getTable(request.getTable_name().getDb_name(),
1053 request.getTable_name().getTable_name());
1055 return ((HdfsTable) table).getFiles(request.getPartition_spec());
1058 "Unsupported table class: " + table.getClass());
List< String > getDbNames(String dbPattern, User user)
ArrayList< Column > getColumnsInHiveOrder()
TResultSet getColumnStats(String dbName, String tableName)
static final ScalarType BIGINT
ImpaladCatalog impaladCatalog_
final long MAX_CATALOG_UPDATE_WAIT_TIME_MS
AnalysisContext.AnalysisResult analyzeStmt(TQueryCtx queryCtx)
AuthorizationChecker getAuthzChecker()
static ImpalaInternalAdminUser getInstance()
boolean requestTblLoadAndWait(Set< TableName > requestedTbls)
static final ScalarType STRING
boolean requestTblLoadAndWait(Set< TableName > requestedTbls, long timeoutMs)
final long MISSING_TBL_LOAD_WAIT_TIMEOUT_MS
final AtomicReference< AuthorizationChecker > authzChecker_
TExecRequest createExecRequest(TQueryCtx queryCtx, StringBuilder explainString)
List< DataSource > getDataSrcs(String pattern)
AuthorizationPolicy getAuthPolicy()
void createCatalogOpRequest(AnalysisContext.AnalysisResult analysis, TExecRequest result)
TUpdateCatalogCacheResponse updateCatalogCache(TUpdateCatalogCacheRequest req)
Set< TableName > getMissingTbls(Set< TableName > tableNames)
Frontend(AuthorizationConfig authorizationConfig, ImpaladCatalog catalog)
AuthorizationPolicyReader(AuthorizationConfig config)
Frontend(AuthorizationConfig authorizationConfig)
static final ScalarType DOUBLE
final AuthorizationConfig authzConfig_
String getExplainString(TQueryCtx queryCtx)
final ScheduledExecutorService policyReader_
void createExplainRequest(String explainString, TExecRequest result)
TDescribeTableResult describeTable(String dbName, String tableName, TDescribeTableOutputStyle outputStyle)
static final ScalarType INT
TResultSet getTableFiles(TShowFilesParams request)
final AuthorizationConfig config_
TLoadDataResp loadTableData(TLoadDataReq request)
void toThrift(TColumnType container)
TResultSet getTableStats(String dbName, String tableName)
List< String > getTableNames(String dbName, String tablePattern, User user)
TResultSet execHiveServer2MetadataOp(TMetadataOpRequest request)
ImpaladCatalog getCatalog()
static final int AUTHORIZATION_POLICY_RELOAD_INTERVAL_SECS
List< Function > getFunctions(TFunctionCategory category, String dbName, String fnPattern)
static String getEffectiveUser(TSessionState session)