dbTalk Databases Forums  

Connectby function, to use or not to use, this is the question now.

comp.databases.postgresql comp.databases.postgresql


Discuss Connectby function, to use or not to use, this is the question now. in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mladen Gogala
 
Posts: n/a

Default Connectby function, to use or not to use, this is the question now. - 05-20-2010 , 11:50 AM






One of my developers asked me for an equivalent to Oracle's "CONNECT BY"
clause. Sure enough, there is a module called "tablefunc" which contains
several overloaded incarnations of the "connectby" function. I installed
some well known tables, usually used to demonstrate the "CONNECT BY"
oracle clause. The relevant table is this one:

scott=> select * from emp;
empno | ename | job | mgr | hiredate | sal | comm |
deptno
-------+--------+-----------+------+---------------------+------+------
+--------
7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450 |
Quote:
10
7839 | KING | PRESIDENT | | 1981-11-17 00:00:00 | 5000 |
10
7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300 |
10
7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800 |
20
7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600 | 300
30
7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250 | 500
30
7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975 |
20
7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250 | 1400
30
7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850 |
30
7788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000 |
20
7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500 | 0
30
7876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100 |
20
7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950 |
30
7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000 |
20
(14 rows)

Some of you might even recognize this table, it is frequently used in
Oracle courses. True enough, connectby function works flawlessly:

scott=> select empno,mgr,level
from connectby('emp','empno','mgr','7839',0)
as t(empno int,mgr int,level int)
scott-> order by level;
empno | mgr | level
-------+------+-------
7839 | | 0
7782 | 7839 | 1
7566 | 7839 | 1
7698 | 7839 | 1
7900 | 7698 | 2
7934 | 7782 | 2
7788 | 7566 | 2
7902 | 7566 | 2
7654 | 7698 | 2
7844 | 7698 | 2
7499 | 7698 | 2
7521 | 7698 | 2
7369 | 7902 | 3
7876 | 7788 | 3
(14 rows)
Time: 3.218 ms
scott=>

My question, however is the following: why is connect by needed at all?
Postgres supports ANSI standard recursive with clause which makes the
following query possible:
scott=> with recursive e(empno,mgr,level) as (
select empno,mgr,0 from emp where empno=7839
union
select emp.empno,emp.mgr,e.level+1
from emp,e
where emp.mgr=e.empno)
select * from e;
empno | mgr | level
-------+------+-------
7839 | | 0
7782 | 7839 | 1
7566 | 7839 | 1
7698 | 7839 | 1
7934 | 7782 | 2
7499 | 7698 | 2
7521 | 7698 | 2
7654 | 7698 | 2
7788 | 7566 | 2
7844 | 7698 | 2
7900 | 7698 | 2
7902 | 7566 | 2
7369 | 7902 | 3
7876 | 7788 | 3
(14 rows)

Time: 2.661 ms
scott=>

It even executes faster than the fancy module, calling a C library
function. Does anybody here use the connectby function and if so, why?


--
http://mgogala.byethost5.com

Reply With Quote
  #2  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Connectby function, to use or not to use, this is the questionnow. - 05-20-2010 , 12:04 PM






laden Gogala wrote on 20.05.2010 18:50:
Quote:
My question, however is the following: why is connect by needed at all?
Recursive CTE was introduced in 8.4, the connect_by module has been around much longer.

But I agree: with 8.4 the connect_by module is no longer necessary, the standard CTE is a lot more portable (and even understood by Oracle 11gR2)

Thomas

Reply With Quote
  #3  
Old   
Matthew Woodcraft
 
Posts: n/a

Default Re: Connectby function, to use or not to use, this is the question now. - 05-20-2010 , 12:07 PM



Mladen Gogala <no (AT) email (DOT) here.invalid> wrote:
Quote:
One of my developers asked me for an equivalent to Oracle's "CONNECT BY"
clause. Sure enough, there is a module called "tablefunc" which contains
several overloaded incarnations of the "connectby" function. I installed
some well known tables, usually used to demonstrate the "CONNECT BY"
oracle clause. The relevant table is this one:
[...]

Quote:
My question, however is the following: why is connect by needed at all?
Postgres supports ANSI standard recursive with clause which makes the
following query possible:

The implementation in tablefunc has been around for longer than
PostgreSQL's native WITH support. I presume it's kept around for
backward compatibility.

-M-

Reply With Quote
  #4  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: Connectby function, to use or not to use, this is the questionnow. - 05-20-2010 , 12:56 PM



On Thu, 20 May 2010 19:04:47 +0200, Thomas Kellerer wrote:

Quote:
Recursive CTE was introduced in 8.4, the connect_by module has been
around much longer.
I am a relatively new user of Postgres. I should have guessed as much. As
far as I am concerned, Postgres starts with version 8.3.8.

Quote:
But I agree: with 8.4 the connect_by module is no longer necessary, the
standard CTE is a lot more portable (and even understood by Oracle
11gR2)
Yup, you're right, but it doesn't recognize the keyword "recursive" and
it must go with "UNION ALL", it doesn't work with a simple "UNION". In
addition to that, I cannot use the keyword "level" as a column name. The
same query that works on 11.2 fails miserably on 10.2.0.5:

Connected.
SQL> /
with e(empno,mgr,lev) as (
*
ERROR at line 1:
ORA-32033: unsupported column aliasing


Elapsed: 00:00:00.10
SQL>


I would say that Oracle's support for the recursive CTE is much more
limited than the Postgres support. BTW, what does "CTE" stand for? Oracle
calls it "subquery factoring".
--
http://mgogala.byethost5.com

Reply With Quote
  #5  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Connectby function, to use or not to use, this is the questionnow. - 05-20-2010 , 01:24 PM



Mladen Gogala wrote on 20.05.2010 19:56:
Quote:
As far as I am concerned, Postgres starts with version 8.3.8
Which did not have CTE (neither normal nor recursive)

Quote:
In addition to that, I cannot use the keyword "level" as a column name.
Because LEVEL is a reserved word in Oracle

Quote:
The same query that works on 11.2 fails miserably on 10.2.0.5:

Connected.
SQL> /
with e(empno,mgr,lev) as (
*
ERROR at line 1:
ORA-32033: unsupported column aliasing
Interesting, as (regular) CTEs are available since 9i IIRC

Quote:
BTW, what does "CTE" stand for? Oracle calls it "subquery factoring".
Common Table Expression

Reply With Quote
  #6  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: Connectby function, to use or not to use, this is the questionnow. - 05-20-2010 , 01:36 PM



On Thu, 20 May 2010 20:24:24 +0200, Thomas Kellerer wrote:

Quote:
Mladen Gogala wrote on 20.05.2010 19:56:
As far as I am concerned, Postgres starts with version 8.3.8
Which did not have CTE (neither normal nor recursive)
That is true. However, I didn't do much with that.

<...>

Quote:
Interesting, as (regular) CTEs are available since 9i IIRC
This one was recursive. Normal CTEs are supported.

Quote:
BTW, what does "CTE" stand for? Oracle calls it "subquery factoring".

Common Table Expression
Thanks.




--
http://mgogala.byethost5.com

Reply With Quote
  #7  
Old   
Robert Klemme
 
Posts: n/a

Default Re: Connectby function, to use or not to use, this is the questionnow. - 05-20-2010 , 01:52 PM



On 20.05.2010 20:36, Mladen Gogala wrote:
Quote:
On Thu, 20 May 2010 20:24:24 +0200, Thomas Kellerer wrote:

Mladen Gogala wrote on 20.05.2010 19:56:

BTW, what does "CTE" stand for? Oracle calls it "subquery factoring".

Common Table Expression
Could be that the term was coined in the Microsoft or Sybase world. MS
SQL Server's documentation is the first place that I noticed it (which
does not need to mean much).

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Reply With Quote
  #8  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Connectby function, to use or not to use, this is the questionnow. - 05-20-2010 , 02:44 PM



Robert Klemme wrote on 20.05.2010 20:52:
Quote:
Common Table Expression

Could be that the term was coined in the Microsoft or Sybase world. MS
SQL Server's documentation is the first place that I noticed it (which
does not need to mean much).

I don't know if MS created the expression before that, but the term "Common Table Expression" was one of the extensions introduced with SQL 99

Thomas

Reply With Quote
  #9  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Connectby function, to use or not to use, this is the questionnow. - 05-21-2010 , 03:43 AM



On 2010-05-20 20:24, Thomas Kellerer wrote:
Quote:

Mladen Gogala wrote on 20.05.2010 19:56:
As far as I am concerned, Postgres starts with version 8.3.8
Which did not have CTE (neither normal nor recursive)

In addition to that, I cannot use the keyword "level" as a column name.
Because LEVEL is a reserved word in Oracle
Not only in Oracle. It was a reserved word in sql92 and in sql99. What's
peculiar is that it does not appear to be a reserved word in sql2003.

/Lennart

[...]

Reply With Quote
  #10  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Connectby function, to use or not to use, this is the questionnow. - 05-21-2010 , 03:45 AM



On 2010-05-20 21:44, Thomas Kellerer wrote:
Quote:

Robert Klemme wrote on 20.05.2010 20:52:
Common Table Expression

Could be that the term was coined in the Microsoft or Sybase world. MS
SQL Server's documentation is the first place that I noticed it (which
does not need to mean much).


I don't know if MS created the expression before that, but the term "Common Table Expression" was one of the extensions introduced with SQL 99

AFAIK IBM (db2) invented the construction and proposed it to the standard.

/Lennart

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.