提交 7eaae8cd 编写于 作者: L Lisa Owen 提交者: David Yozie

add info about pl/python multidim array support (#2392)

* add info about pl/python multidim array support

* restructure array info so better matches upstream docs

* reorganize a bit, davids other requested edits
上级 ac51683d
......@@ -95,32 +95,81 @@
arguments are also passed as ordinary variables to the Python script. The result is returned
from the PL/Python function with <codeph>return</codeph> statement, or
<codeph>yield</codeph> statement in case of a result-set statement. </p>
<p>The Greenplum Database PL/Python language module imports the Python module
<codeph>plpy</codeph>. The module <codeph>plpy</codeph> implements these functions:</p>
<ul id="ul_fp4_jq5_xt">
<li>Functions to execute SQL queries and prepare execution plans for queries.<sl>
<sli><codeph>plpy.execute</codeph></sli>
<sli><codeph>plpy.prepare</codeph></sli>
</sl></li>
<li>Functions to manage errors and messages.<sl>
<sli><codeph>plpy.debug</codeph></sli>
<sli><codeph>plpy.log</codeph></sli>
<sli><codeph>plpy.info</codeph></sli>
<sli><codeph>plpy.notice</codeph></sli>
<sli><codeph>plpy.warning</codeph></sli>
<sli><codeph>plpy.error</codeph></sli>
<sli><codeph>plpy.fatal</codeph></sli>
<sli><codeph>plpy.debug</codeph></sli>
</sl></li>
</ul>
</body>
<topic id="topic1113" xml:lang="en">
<title id="pw2137121113">Arrays and Lists</title>
<body>
<p>You pass SQL array values into PL/Python functions with a Python list. Similarly, PL/Python functions return SQL array values as a Python list. In the typical PL/Python usage pattern, you will specify an array with <codeph>[]</codeph>.</p>
<p>The following example creates a PL/Python function that returns an array of integers:</p>
<codeblock>CREATE FUNCTION return_py_int_array()
RETURNS int[]
AS $$
return [1, 11, 21, 31]
$$ LANGUAGE plpythonu;
SELECT return_py_int_array();
return_py_int_array
---------------------
{1,11,21,31}
(1 row) </codeblock>
<p>PL/Python treats multi-dimensional arrays as lists of lists. You pass a multi-dimensional array to a PL/Python function using nested Python lists. When a PL/Python function returns a multi-dimensional array, the inner lists at each level must all be of the same size. </p>
<p>The following example creates a PL/Python function that takes a multi-dimensional array of integers as input. The function displays the type of the provided argument, and returns the multi-dimensional array:</p>
<codeblock>CREATE FUNCTION return_multidim_py_array(x int4[])
RETURNS int4[]
AS $$
plpy.info(x, type(x))
return x
$$ LANGUAGE plpythonu;
SELECT * FROM return_multidim_py_array(ARRAY[[1,2,3], [4,5,6]]);
INFO: ([[1, 2, 3], [4, 5, 6]], &lt;type 'list'&gt;)
CONTEXT: PL/Python function "return_multidim_py_type"
return_multidim_py_array
--------------------------
{{1,2,3},{4,5,6}}
(1 row) </codeblock>
<p>PL/Python also accepts other Python sequences, such as tuples, as function arguments for backwards compatibility with Greenplum versions where multi-dimensional arrays were not supported. In such cases, the Python sequences are always treated as one-dimensional arrays because they are ambiguous with composite types.</p>
</body>
</topic>
<topic id="topic1117" xml:lang="en">
<title id="pw2137121117">Composite Types</title>
<body>
<p>You pass composite-type arguments to a PL/Python function using Python mappings. The element names of the mapping are the attribute names of the composite types. If an attribute has the null value, its mapping value is <codeph>None</codeph>.</p>
<p>You can return a composite type result as a sequence type (tuple or list).
You must specify a composite type as a tuple, rather than a list, when it is used in a multi-dimensional array. You cannot return an array of composite types as a list because it would be ambiguous to determine whether the list represents a composite type or another array dimension. In the typical usage pattern, you will specify composite type tuples with <codeph>()</codeph>.</p>
<p>In the following example, you create a composite type and a PL/Python function that returns an array of the composite type:</p>
<codeblock>CREATE TYPE type_record AS (
first text,
second int4
);
CREATE FUNCTION composite_type_as_list()
RETURNS type_record[]
AS $$
return [[('first', 1), ('second', 1)], [('first', 2), ('second', 2)], [('first', 3), ('second', 3)]];
$$ LANGUAGE plpythonu;
SELECT * FROM composite_type_as_list();
composite_type_as_list
------------------------------------------------------------------------------------
{{"(first,1)","(second,1)"},{"(first,2)","(second,2)"},{"(first,3)","(second,3)"}}
(1 row) </codeblock>
<p>Refer to the PostgreSQL <xref href="https://www.postgresql.org/docs/devel/static/plpython-data.html#plpython-arrays" scope="external" format="html">Arrays, Lists</xref> documentation for additional information on PL/Python handling of arrays and composite types.</p>
</body>
</topic>
<topic id="topic8" xml:lang="en">
<title>Executing and Preparing SQL Queries </title>
<body>
<p>The PL/Python <codeph>plpy</codeph> module provides two Python functions to execute an
SQL query and prepare an execution plan for a query, <codeph>plpy.execute</codeph> and
<codeph>plpy.prepare</codeph>. Preparing the execution plan for a query is useful if you
run the query from multiple Python function.</p>
run the query from multiple Python functions.</p>
</body>
<topic id="topic_jnf_45f_zt">
<title>plpy.execute</title>
......@@ -188,10 +237,21 @@
$$ LANGUAGE plpythonu;</codeblock>
</body>
</topic>
</topic>
</topic>
<topic id="topic_s3d_vc4_xt">
<title>Handling Python Errors and Messages</title>
<body>
<p> The Python module <codeph>plpy</codeph> implements these functions to manage errors and messages:</p>
<ul id="ul_fp4_jq5_xt">
<li><codeph>plpy.debug</codeph></li>
<li><codeph>plpy.log</codeph></li>
<li><codeph>plpy.info</codeph></li>
<li><codeph>plpy.notice</codeph></li>
<li><codeph>plpy.warning</codeph></li>
<li><codeph>plpy.error</codeph></li>
<li><codeph>plpy.fatal</codeph></li>
<li><codeph>plpy.debug</codeph></li>
</ul>
<p>The message functions <codeph>plpy.error</codeph> and <codeph>plpy.fatal</codeph> raise a
Python exception which, if uncaught, propagates out to the calling query, causing the
current transaction or subtransaction to be aborted. The functions <codeph>raise
......@@ -518,6 +578,7 @@ $$ language plpythonu;</codeblock>
</body>
</topic>
</topic>
<topic id="topic11" xml:lang="en">
<title id="pw213712">Examples</title>
<body>
......
......@@ -58,7 +58,7 @@
<section id="section5">
<title>Notes</title>
<p>The PL/pgSQL language is installed on the Greenplum Database system and is registered in a
user created database. The PL/Python language is installed by default. Other languages are
user created database. The PL/Python language is installed by default, but not registered. Other languages are
not installed or registered. The system catalog <codeph>pg_language</codeph> contains
information about the registered languages in a database.</p>
<p>The user must have <codeph>USAGE</codeph> privilege for the procedural language, or must be
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册