Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0f48e06

Browse files
committed
PL/Python: Improve documentation of nrows() method
Clarify that nrows() is the number of rows processed, versus the number of rows returned, which can be obtained using len. Also add tests about that.
1 parent c03523e commit 0f48e06

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

doc/src/sgml/plpython.sgml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,8 @@ rv = plpy.execute("SELECT * FROM my_table", 5)
903903
<programlisting>
904904
foo = rv[i]["my_column"]
905905
</programlisting>
906+
The number of rows returned can be obtained using the built-in
907+
<function>len</function> function.
906908
</para>
907909

908910
<para>
@@ -912,7 +914,10 @@ foo = rv[i]["my_column"]
912914
<term><literal><function>nrows</function>()</literal></term>
913915
<listitem>
914916
<para>
915-
Returns the number of rows returned or processed by the query.
917+
Returns the number of rows processed by the command. Note that this
918+
is not necessarily the same as the number of rows returned. For
919+
example, an <command>UPDATE</command> command will set this value but
920+
won't return any rows (unless <literal>RETURNING</literal> is used).
916921
</para>
917922
</listitem>
918923
</varlistentry>

src/pl/plpython/expected/plpython_spi.out

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,64 @@ CONTEXT: Traceback (most recent call last):
150150
PL/Python function "result_metadata_test", line 6, in <module>
151151
plpy.info(result.colnames())
152152
PL/Python function "result_metadata_test"
153+
CREATE FUNCTION result_nrows_test(cmd text) RETURNS int
154+
AS $$
155+
result = plpy.execute(cmd)
156+
return result.nrows()
157+
$$ LANGUAGE plpythonu;
158+
SELECT result_nrows_test($$SELECT 1$$);
159+
result_nrows_test
160+
-------------------
161+
1
162+
(1 row)
163+
164+
SELECT result_nrows_test($$CREATE TEMPORARY TABLE foo2 (a int, b text)$$);
165+
result_nrows_test
166+
-------------------
167+
0
168+
(1 row)
169+
170+
SELECT result_nrows_test($$INSERT INTO foo2 VALUES (1, 'one'), (2, 'two')$$);
171+
result_nrows_test
172+
-------------------
173+
2
174+
(1 row)
175+
176+
SELECT result_nrows_test($$UPDATE foo2 SET b = '' WHERE a = 2$$);
177+
result_nrows_test
178+
-------------------
179+
1
180+
(1 row)
181+
182+
CREATE FUNCTION result_len_test(cmd text) RETURNS int
183+
AS $$
184+
result = plpy.execute(cmd)
185+
return len(result)
186+
$$ LANGUAGE plpythonu;
187+
SELECT result_len_test($$SELECT 1$$);
188+
result_len_test
189+
-----------------
190+
1
191+
(1 row)
192+
193+
SELECT result_len_test($$CREATE TEMPORARY TABLE foo3 (a int, b text)$$);
194+
result_len_test
195+
-----------------
196+
0
197+
(1 row)
198+
199+
SELECT result_len_test($$INSERT INTO foo3 VALUES (1, 'one'), (2, 'two')$$);
200+
result_len_test
201+
-----------------
202+
0
203+
(1 row)
204+
205+
SELECT result_len_test($$UPDATE foo3 SET b= '' WHERE a = 2$$);
206+
result_len_test
207+
-----------------
208+
0
209+
(1 row)
210+
153211
-- cursor objects
154212
CREATE FUNCTION simple_cursor_test() RETURNS int AS $$
155213
res = plpy.cursor("select fname, lname from users")

src/pl/plpython/sql/plpython_spi.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ $$ LANGUAGE plpythonu;
110110
SELECT result_metadata_test($$SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'$$);
111111
SELECT result_metadata_test($$CREATE TEMPORARY TABLE foo1 (a int, b text)$$);
112112

113+
CREATE FUNCTION result_nrows_test(cmd text) RETURNS int
114+
AS $$
115+
result = plpy.execute(cmd)
116+
return result.nrows()
117+
$$ LANGUAGE plpythonu;
118+
119+
SELECT result_nrows_test($$SELECT 1$$);
120+
SELECT result_nrows_test($$CREATE TEMPORARY TABLE foo2 (a int, b text)$$);
121+
SELECT result_nrows_test($$INSERT INTO foo2 VALUES (1, 'one'), (2, 'two')$$);
122+
SELECT result_nrows_test($$UPDATE foo2 SET b = '' WHERE a = 2$$);
123+
124+
CREATE FUNCTION result_len_test(cmd text) RETURNS int
125+
AS $$
126+
result = plpy.execute(cmd)
127+
return len(result)
128+
$$ LANGUAGE plpythonu;
129+
130+
SELECT result_len_test($$SELECT 1$$);
131+
SELECT result_len_test($$CREATE TEMPORARY TABLE foo3 (a int, b text)$$);
132+
SELECT result_len_test($$INSERT INTO foo3 VALUES (1, 'one'), (2, 'two')$$);
133+
SELECT result_len_test($$UPDATE foo3 SET b= '' WHERE a = 2$$);
134+
113135

114136
-- cursor objects
115137

0 commit comments

Comments
 (0)