hawq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ent...@apache.org
Subject incubator-hawq git commit: HAWQ-123. Add 'Partition by' description in psql
Date Thu, 19 Nov 2015 19:24:54 GMT
Repository: incubator-hawq
Updated Branches:
  refs/heads/master fd1304581 -> afdd22401


HAWQ-123. Add 'Partition by' description in psql

When user runs \d or \d+ on a partition table, it will display partition
keys on the current level for the partition table with the clause of
'Partition by: (partition_key_1, ..., partition_key_n)' after
'Distributed by' clause. But if user runs \d or \d+ on a non-partition
table or leaf level partition table, the 'Partition by' clause will not
be displayed.

Closes #93


Project: http://git-wip-us.apache.org/repos/asf/incubator-hawq/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-hawq/commit/afdd2240
Tree: http://git-wip-us.apache.org/repos/asf/incubator-hawq/tree/afdd2240
Diff: http://git-wip-us.apache.org/repos/asf/incubator-hawq/diff/afdd2240

Branch: refs/heads/master
Commit: afdd22401437245aa07e3e7458179a26b4e6c0e8
Parents: fd13045
Author: Haisheng Yuan <hyuan@pivotal.io>
Authored: Thu Nov 19 11:21:54 2015 -0800
Committer: Entong Shen <shenentong@gmail.com>
Committed: Thu Nov 19 11:24:19 2015 -0800

----------------------------------------------------------------------
 src/bin/psql/describe.c | 87 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/afdd2240/src/bin/psql/describe.c
----------------------------------------------------------------------
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 00b94c0..99718d2 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -28,6 +28,7 @@ static bool describeOneTableDetails(const char *schemaname,
 						const char *oid,
 						bool verbose);
 static int add_distributed_by_footer(const char* oid, PQExpBufferData *inoutbuf, PQExpBufferData
buf);
+static int add_partition_by_footer(const char* oid, PQExpBufferData *inoutbuf, PQExpBufferData
*buf);
 static void add_tablespace_footer(printTableContent *const cont, char relkind,
 					  Oid tablespace, const bool newline);
 static void add_role_attribute(PQExpBuffer buf, const char *const str);
@@ -2532,6 +2533,15 @@ describeOneTableDetails(const char *schemaname,
 		resetPQExpBuffer(&tmpbuf);
 		add_distributed_by_footer(oid, &tmpbuf, buf);
 		printTableAddFooter(&cont, tmpbuf.data);
+
+		/* print 'partition by' clause */
+		if (tuples > 0)
+		{
+			resetPQExpBuffer(&tmpbuf);
+			add_partition_by_footer(oid, &tmpbuf, &buf);
+			printTableAddFooter(&cont, tmpbuf.data);
+		}
+
 		add_tablespace_footer(&cont, tableinfo.relkind, tableinfo.tablespace,
 							  true);
 	}
@@ -2676,6 +2686,83 @@ add_distributed_by_footer(const char* oid, PQExpBufferData *inoutbuf,
PQExpBuffe
 }
 
 /*
+ * Add a 'partition by' description to the footer.
+ */
+static int
+add_partition_by_footer(const char* oid, PQExpBufferData *inoutbuf, PQExpBufferData *buf)
+{
+	PGresult	*result = NULL;
+
+	/* check if current relation is root partition, if it is root partition, at least 1 row
returns */
+	printfPQExpBuffer(buf, "SELECT parrelid FROM pg_catalog.pg_partition WHERE parrelid = '%s'",
oid);
+	result = PSQLexec(buf->data, false);
+
+	if (!result)
+		return 1;
+	int nRows = PQntuples(result);
+	int nPartKey = 0;
+
+	PQclear(result);
+
+	if (nRows)
+	{
+		/* query partition key on the root partition */
+		printfPQExpBuffer(buf,
+			"WITH att_arr AS (SELECT unnest(paratts) \n"
+			"	FROM pg_catalog.pg_partition p \n"
+			"	WHERE p.parrelid = '%s' AND p.parlevel = 0 AND p.paristemplate = false), \n"
+			"idx_att AS (SELECT row_number() OVER() AS idx, unnest AS att_num FROM att_arr) \n"
+			"SELECT attname FROM pg_catalog.pg_attribute, idx_att \n"
+			"	WHERE attrelid='%s' AND attnum = att_num ORDER BY idx ",
+			oid, oid);
+	}
+	else
+	{
+		/* query partition key on the intermediate partition */
+		printfPQExpBuffer(buf,
+			"WITH att_arr AS (SELECT unnest(paratts) FROM pg_catalog.pg_partition p, \n"
+			"	(SELECT parrelid, parlevel \n"
+			"		FROM pg_catalog.pg_partition p, pg_catalog.pg_partition_rule pr \n"
+			"		WHERE pr.parchildrelid='%s' AND p.oid = pr.paroid) AS v \n"
+			"	WHERE p.parrelid = v.parrelid AND p.parlevel = v.parlevel+1 AND p.paristemplate = false),
\n"
+			"idx_att AS (SELECT row_number() OVER() AS idx, unnest AS att_num FROM att_arr) \n"
+			"SELECT attname FROM pg_catalog.pg_attribute, idx_att \n"
+			"	WHERE attrelid='%s' AND attnum = att_num ORDER BY idx ",
+			oid, oid);
+	}
+
+	result = PSQLexec(buf->data, false);
+	if (!result)
+		return 1;
+	nPartKey = PQntuples(result);
+
+	if (nPartKey)
+	{
+		char *partColName;
+		int i = 0;
+		appendPQExpBuffer(inoutbuf, "Partition by: (");
+		for (i = 0; i < nPartKey; i++)
+		{
+			if (i > 0)
+				appendPQExpBuffer(inoutbuf, ", ");
+			partColName = PQgetvalue(result, i, 0);
+
+			if (!partColName)
+			{
+				resetPQExpBuffer(inoutbuf);
+				return 1;
+			}
+			appendPQExpBuffer(inoutbuf, "%s", partColName);
+		}
+		appendPQExpBuffer(inoutbuf, ")");
+	}
+
+	PQclear(result);
+
+	return 0; /* success */
+}
+
+/*
  * Add a tablespace description to a footer.  If 'newline' is true, it is added
  * in a new line; otherwise it's appended to the current value of the last
  * footer.


Mime
View raw message