diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java index 2d1118c..d193af9 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java @@ -2131,20 +2131,25 @@ private void getMetaData(QB qb, ReadEntity parentInput) ctx.setResDir(null); ctx.setResFile(null); - // allocate a temporary output dir on the location of the table - String tableName = getUnescapedName((ASTNode) ast.getChild(0)); - String[] names = Utilities.getDbTableName(tableName); Path location; - try { - Warehouse wh = new Warehouse(conf); - //Use destination table's db location. - String destTableDb = qb.getTableDesc() != null? qb.getTableDesc().getDatabaseName(): null; - if (destTableDb == null) { - destTableDb = names[0]; + // If the CTAS query does not specify a location, use the db location, else use the table location + if (qb.getTableDesc().getLocation() == null) { + // allocate a temporary output dir on the location of the table + String tableName = getUnescapedName((ASTNode) ast.getChild(0)); + String[] names = Utilities.getDbTableName(tableName); + try { + Warehouse wh = new Warehouse(conf); + //Use destination table's db location. + String destTableDb = qb.getTableDesc() != null ? qb.getTableDesc().getDatabaseName() : null; + if (destTableDb == null) { + destTableDb = names[0]; + } + location = wh.getDatabasePath(db.getDatabase(destTableDb)); + } catch (MetaException e) { + throw new SemanticException(e); } - location = wh.getDatabasePath(db.getDatabase(destTableDb)); - } catch (MetaException e) { - throw new SemanticException(e); + } else { + location = new Path(qb.getTableDesc().getLocation()); } try { fname = ctx.getExtTmpPathRelTo( @@ -2171,6 +2176,7 @@ private void getMetaData(QB qb, ReadEntity parentInput) if (ast.getChildCount() >= 2 && ast.getChild(1).getText().toLowerCase().equals("local")) { isDfsFile = false; } + // Set the destination for the SELECT query inside the CTAS qb.getMetaData().setDestForAlias(name, fname, isDfsFile); CreateTableDesc directoryDesc = new CreateTableDesc();