Skip to content

Commit cf44775

Browse files
author
Kevin Wilfong
committed
HIVE-2530. Implement SHOW TBLPROPERTIES. (leizhao via kevinwilfong)
git-svn-id: https://svn.apache.org/repos/asf/hive/trunk@1327189 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7a0311d commit cf44775

File tree

10 files changed

+310
-0
lines changed

10 files changed

+310
-0
lines changed

ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
import org.apache.hadoop.hive.ql.plan.ShowPartitionsDesc;
137137
import org.apache.hadoop.hive.ql.plan.ShowTableStatusDesc;
138138
import org.apache.hadoop.hive.ql.plan.ShowTablesDesc;
139+
import org.apache.hadoop.hive.ql.plan.ShowTblPropertiesDesc;
139140
import org.apache.hadoop.hive.ql.plan.SwitchDatabaseDesc;
140141
import org.apache.hadoop.hive.ql.plan.UnlockTableDesc;
141142
import org.apache.hadoop.hive.ql.plan.api.StageType;
@@ -327,6 +328,11 @@ public int execute(DriverContext driverContext) {
327328
return showTableStatus(db, showTblStatus);
328329
}
329330

331+
ShowTblPropertiesDesc showTblProperties = work.getShowTblPropertiesDesc();
332+
if (showTblProperties != null) {
333+
return showTableProperties(db, showTblProperties);
334+
}
335+
330336
ShowFunctionsDesc showFuncs = work.getShowFuncsDesc();
331337
if (showFuncs != null) {
332338
return showFunctions(showFuncs);
@@ -2448,6 +2454,75 @@ private int showTableStatus(Hive db, ShowTableStatusDesc showTblStatus) throws H
24482454
return 0;
24492455
}
24502456

2457+
/**
2458+
* Write the properties of a table to a file.
2459+
*
2460+
* @param db
2461+
* The database in question.
2462+
* @param showTblPrpt
2463+
* This is the table we're interested in.
2464+
* @return Returns 0 when execution succeeds and above 0 if it fails.
2465+
* @throws HiveException
2466+
* Throws this exception if an unexpected error occurs.
2467+
*/
2468+
private int showTableProperties(Hive db, ShowTblPropertiesDesc showTblPrpt) throws HiveException {
2469+
String tableName = showTblPrpt.getTableName();
2470+
2471+
// show table properties - populate the output stream
2472+
Table tbl = db.getTable(tableName, false);
2473+
DataOutput outStream = null;
2474+
try {
2475+
Path resFile = new Path(showTblPrpt.getResFile());
2476+
FileSystem fs = resFile.getFileSystem(conf);
2477+
outStream = fs.create(resFile);
2478+
2479+
if (tbl == null) {
2480+
String errMsg = "Table " + tableName + " does not exist";
2481+
outStream.write(errMsg.getBytes("UTF-8"));
2482+
((FSDataOutputStream) outStream).close();
2483+
outStream = null;
2484+
return 0;
2485+
}
2486+
2487+
LOG.info("DDLTask: show properties for " + tbl.getTableName());
2488+
2489+
String propertyName = showTblPrpt.getPropertyName();
2490+
if (propertyName != null) {
2491+
String propertyValue = tbl.getProperty(propertyName);
2492+
if (propertyValue == null) {
2493+
String errMsg = "Table " + tableName + " does not have property: " + propertyName;
2494+
outStream.write(errMsg.getBytes("UTF-8"));
2495+
}
2496+
else {
2497+
outStream.writeBytes(propertyValue);
2498+
}
2499+
}
2500+
else {
2501+
Map<String, String> properties = tbl.getParameters();
2502+
for (String key : properties.keySet()) {
2503+
writeKeyValuePair(outStream, key, properties.get(key));
2504+
}
2505+
}
2506+
2507+
LOG.info("DDLTask: written data for showing properties of " + tbl.getTableName());
2508+
((FSDataOutputStream) outStream).close();
2509+
outStream = null;
2510+
2511+
} catch (FileNotFoundException e) {
2512+
LOG.info("show table properties: " + stringifyException(e));
2513+
return 1;
2514+
} catch (IOException e) {
2515+
LOG.info("show table properties: " + stringifyException(e));
2516+
return 1;
2517+
} catch (Exception e) {
2518+
throw new HiveException(e);
2519+
} finally {
2520+
IOUtils.closeStream((FSDataOutputStream) outStream);
2521+
}
2522+
2523+
return 0;
2524+
}
2525+
24512526
/**
24522527
* Write the description of a table to a file.
24532528
*

ql/src/java/org/apache/hadoop/hive/ql/parse/DDLSemanticAnalyzer.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import org.apache.hadoop.hive.ql.plan.ShowPartitionsDesc;
106106
import org.apache.hadoop.hive.ql.plan.ShowTableStatusDesc;
107107
import org.apache.hadoop.hive.ql.plan.ShowTablesDesc;
108+
import org.apache.hadoop.hive.ql.plan.ShowTblPropertiesDesc;
108109
import org.apache.hadoop.hive.ql.plan.StatsWork;
109110
import org.apache.hadoop.hive.ql.plan.SwitchDatabaseDesc;
110111
import org.apache.hadoop.hive.ql.plan.TableDesc;
@@ -231,6 +232,10 @@ public void analyzeInternal(ASTNode ast) throws SemanticException {
231232
ctx.setResFile(new Path(ctx.getLocalTmpFileURI()));
232233
analyzeShowTableStatus(ast);
233234
break;
235+
case HiveParser.TOK_SHOW_TBLPROPERTIES:
236+
ctx.setResFile(new Path(ctx.getLocalTmpFileURI()));
237+
analyzeShowTableProperties(ast);
238+
break;
234239
case HiveParser.TOK_SHOWFUNCTIONS:
235240
ctx.setResFile(new Path(ctx.getLocalTmpFileURI()));
236241
analyzeShowFunctions(ast);
@@ -1482,6 +1487,21 @@ private void analyzeShowTableStatus(ASTNode ast) throws SemanticException {
14821487
setFetchTask(createFetchTask(showTblStatusDesc.getSchema()));
14831488
}
14841489

1490+
private void analyzeShowTableProperties(ASTNode ast) throws SemanticException {
1491+
ShowTblPropertiesDesc showTblPropertiesDesc;
1492+
String tableNames = getUnescapedName((ASTNode)ast.getChild(0));
1493+
String dbName = db.getCurrentDatabase();
1494+
String propertyName = null;
1495+
if (ast.getChildCount() > 1) {
1496+
propertyName = unescapeSQLString(ast.getChild(1).getText());
1497+
}
1498+
showTblPropertiesDesc = new ShowTblPropertiesDesc(ctx.getResFile().toString(), tableNames,
1499+
propertyName);
1500+
rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(),
1501+
showTblPropertiesDesc), conf));
1502+
setFetchTask(createFetchTask(showTblPropertiesDesc.getSchema()));
1503+
}
1504+
14851505
private void analyzeShowIndexes(ASTNode ast) throws SemanticException {
14861506
ShowIndexesDesc showIndexesDesc;
14871507
String tableName = getUnescapedName((ASTNode)ast.getChild(0));

ql/src/java/org/apache/hadoop/hive/ql/parse/Hive.g

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ TOK_SHOWTABLES;
144144
TOK_SHOWFUNCTIONS;
145145
TOK_SHOWPARTITIONS;
146146
TOK_SHOW_TABLESTATUS;
147+
TOK_SHOW_TBLPROPERTIES;
147148
TOK_SHOWLOCKS;
148149
TOK_LOCKTABLE;
149150
TOK_UNLOCKTABLE;
@@ -846,6 +847,7 @@ showStatement
846847
| KW_SHOW KW_PARTITIONS Identifier partitionSpec? -> ^(TOK_SHOWPARTITIONS Identifier partitionSpec?)
847848
| KW_SHOW KW_TABLE KW_EXTENDED ((KW_FROM|KW_IN) db_name=Identifier)? KW_LIKE showStmtIdentifier partitionSpec?
848849
-> ^(TOK_SHOW_TABLESTATUS showStmtIdentifier $db_name? partitionSpec?)
850+
| KW_SHOW KW_TBLPROPERTIES tblName=Identifier (LPAREN prptyName=StringLiteral RPAREN)? -> ^(TOK_SHOW_TBLPROPERTIES $tblName $prptyName?)
849851
| KW_SHOW KW_LOCKS (parttype=partTypeExpr)? (isExtended=KW_EXTENDED)? -> ^(TOK_SHOWLOCKS $parttype? $isExtended?)
850852
| KW_SHOW (showOptions=KW_FORMATTED)? (KW_INDEX|KW_INDEXES) KW_ON showStmtIdentifier ((KW_FROM|KW_IN) db_name=Identifier)?
851853
-> ^(TOK_SHOWINDEXES showStmtIdentifier $showOptions? $db_name?)

ql/src/java/org/apache/hadoop/hive/ql/parse/ParseDriver.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public class ParseDriver {
8282
xlateMap.put("KW_PARTITIONS", "PARTITIONS");
8383
xlateMap.put("KW_TABLE", "TABLE");
8484
xlateMap.put("KW_TABLES", "TABLES");
85+
xlateMap.put("KW_TBLPROPERTIES", "TBLPROPERTIES");
8586
xlateMap.put("KW_SHOW", "SHOW");
8687
xlateMap.put("KW_MSCK", "MSCK");
8788
xlateMap.put("KW_DIRECTORY", "DIRECTORY");

ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzerFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public final class SemanticAnalyzerFactory {
6060
commandType.put(HiveParser.TOK_SHOWDATABASES, HiveOperation.SHOWDATABASES);
6161
commandType.put(HiveParser.TOK_SHOWTABLES, HiveOperation.SHOWTABLES);
6262
commandType.put(HiveParser.TOK_SHOW_TABLESTATUS, HiveOperation.SHOW_TABLESTATUS);
63+
commandType.put(HiveParser.TOK_SHOW_TBLPROPERTIES, HiveOperation.SHOW_TBLPROPERTIES);
6364
commandType.put(HiveParser.TOK_SHOWFUNCTIONS, HiveOperation.SHOWFUNCTIONS);
6465
commandType.put(HiveParser.TOK_SHOWINDEXES, HiveOperation.SHOWINDEXES);
6566
commandType.put(HiveParser.TOK_SHOWPARTITIONS, HiveOperation.SHOWPARTITIONS);
@@ -157,6 +158,7 @@ public static BaseSemanticAnalyzer get(HiveConf conf, ASTNode tree)
157158
case HiveParser.TOK_SHOWDATABASES:
158159
case HiveParser.TOK_SHOWTABLES:
159160
case HiveParser.TOK_SHOW_TABLESTATUS:
161+
case HiveParser.TOK_SHOW_TBLPROPERTIES:
160162
case HiveParser.TOK_SHOWFUNCTIONS:
161163
case HiveParser.TOK_SHOWPARTITIONS:
162164
case HiveParser.TOK_SHOWINDEXES:

ql/src/java/org/apache/hadoop/hive/ql/plan/DDLWork.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class DDLWork implements Serializable {
4545
private AlterIndexDesc alterIdxDesc;
4646
private ShowDatabasesDesc showDatabasesDesc;
4747
private ShowTablesDesc showTblsDesc;
48+
private ShowTblPropertiesDesc showTblPropertiesDesc;
4849
private LockTableDesc lockTblDesc;
4950
private UnlockTableDesc unlockTblDesc;
5051
private ShowFunctionsDesc showFuncsDesc;
@@ -354,6 +355,17 @@ public DDLWork(HashSet<ReadEntity> inputs, HashSet<WriteEntity> outputs,
354355
this.showTblStatusDesc = showTblStatusDesc;
355356
}
356357

358+
/**
359+
* @param showTblPropertiesDesc
360+
* show table properties descriptor
361+
*/
362+
public DDLWork(HashSet<ReadEntity> inputs, HashSet<WriteEntity> outputs,
363+
ShowTblPropertiesDesc showTblPropertiesDesc) {
364+
this(inputs, outputs);
365+
366+
this.showTblPropertiesDesc = showTblPropertiesDesc;
367+
}
368+
357369
public DDLWork(HashSet<ReadEntity> inputs, HashSet<WriteEntity> outputs,
358370
DropIndexDesc dropIndexDesc) {
359371
this(inputs, outputs);
@@ -788,6 +800,14 @@ public void setShowTblStatusDesc(ShowTableStatusDesc showTblStatusDesc) {
788800
this.showTblStatusDesc = showTblStatusDesc;
789801
}
790802

803+
public ShowTblPropertiesDesc getShowTblPropertiesDesc() {
804+
return showTblPropertiesDesc;
805+
}
806+
807+
public void setShowTblPropertiesDesc(ShowTblPropertiesDesc showTblPropertiesDesc) {
808+
this.showTblPropertiesDesc = showTblPropertiesDesc;
809+
}
810+
791811
public CreateViewDesc getCreateVwDesc() {
792812
return createVwDesc;
793813
}

ql/src/java/org/apache/hadoop/hive/ql/plan/HiveOperation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public enum HiveOperation {
5252
SHOWDATABASES("SHOWDATABASES", new Privilege[]{Privilege.SHOW_DATABASE}, null),
5353
SHOWTABLES("SHOWTABLES", null, null),
5454
SHOW_TABLESTATUS("SHOW_TABLESTATUS", null, null),
55+
SHOW_TBLPROPERTIES("SHOW_TBLPROPERTIES", null, null),
5556
SHOWFUNCTIONS("SHOWFUNCTIONS", null, null),
5657
SHOWINDEXES("SHOWINDEXES", null, null),
5758
SHOWPARTITIONS("SHOWPARTITIONS", null, null),
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.hive.ql.plan;
20+
21+
import java.io.Serializable;
22+
import java.util.HashMap;
23+
24+
import org.apache.hadoop.fs.Path;
25+
26+
/**
27+
* ShowTblPropertiesDesc.
28+
*
29+
*/
30+
@Explain(displayName = "Show Table Properties")
31+
public class ShowTblPropertiesDesc extends DDLDesc implements Serializable {
32+
private static final long serialVersionUID = 1L;
33+
String resFile;
34+
String tableName;
35+
String propertyName;
36+
37+
/**
38+
* table name for the result of showtblproperties.
39+
*/
40+
private static final String table = "show_tableproperties";
41+
/**
42+
* thrift ddl for the result of showtblproperties.
43+
*/
44+
private static final String schema = "prpt_name,prpt_value#string:string";
45+
46+
public String getTable() {
47+
return table;
48+
}
49+
50+
public String getSchema() {
51+
return schema;
52+
}
53+
54+
/**
55+
* For serialization use only.
56+
*/
57+
public ShowTblPropertiesDesc() {
58+
}
59+
60+
/**
61+
* @param resFile
62+
* @param tableName
63+
* name of table to show
64+
* @param propertyName
65+
* name of property to show
66+
*/
67+
public ShowTblPropertiesDesc(String resFile, String tableName, String propertyName) {
68+
this.resFile = resFile;
69+
this.tableName = tableName;
70+
this.propertyName = propertyName;
71+
}
72+
73+
/**
74+
* @return the resFile
75+
*/
76+
public String getResFile() {
77+
return resFile;
78+
}
79+
80+
@Explain(displayName = "result file", normalExplain = false)
81+
public String getResFileString() {
82+
return getResFile();
83+
}
84+
85+
/**
86+
* @param resFile
87+
* the resFile to set
88+
*/
89+
public void setResFile(String resFile) {
90+
this.resFile = resFile;
91+
}
92+
93+
/**
94+
* @return the tableName
95+
*/
96+
@Explain(displayName = "table name")
97+
public String getTableName() {
98+
return tableName;
99+
}
100+
101+
/**
102+
* @param tableName
103+
* the tableName to set
104+
*/
105+
public void setTableName(String tableName) {
106+
this.tableName = tableName;
107+
}
108+
109+
/**
110+
* @return the propertyName
111+
*/
112+
@Explain(displayName = "property name")
113+
public String getPropertyName() {
114+
return propertyName;
115+
}
116+
117+
/**
118+
* @param propertyName
119+
* the propertyName to set
120+
*/
121+
public void setPropertyName(String propertyName) {
122+
this.propertyName = propertyName;
123+
}
124+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
show tblproperties tmpfoo;
2+
3+
create table tmpfoo (a String);
4+
show tblproperties tmpfoo("bar");
5+
6+
alter table tmpfoo set tblproperties ("bar" = "bar value");
7+
alter table tmpfoo set tblproperties ("tmp" = "true");
8+
9+
show tblproperties tmpfoo;
10+
show tblproperties tmpfoo("bar");
11+
12+
drop table tmpfoo;

0 commit comments

Comments
 (0)