Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a bug in the PL/SQL Compiler?

In our SVN-Code Repository, I came across a package specification that -after removing a few lines- boils down to

create or replace package tq84 as
    return varchar2(10);
end tq84;
/

It seems to me that such a specification doesn't make lot of sense and therefore should not compile at all. But maybe, I don't see the obvious, so: is this really a bug?

For completness' sake:

me @ xxx.yyy.zz > select * from v$version;
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE    10.2.0.4.0      Production
TNS for IBM/AIX RISC System/6000: Version 10.2.0.4.0 - Productio
NLSRTL Version 10.2.0.4.0 - Production

Edit: it has been suggested that in specification as given above return is not the keyword but a (package-)variable. This seems not to be the case however, since the following compiles equally fine:

create or replace package tq84 as

    return varchar2(10);
    return number;
    return date;

end tq84;
/

and clearly, the compiler should tell me that I declare the same variable multiple times.

EDIT 2: JOTN is right, of course, and return IS a variable, and furthermore, the compiler doesn't tell upfront, if a variable with the same name is declared twice or more, instead, it's the runtime environment, that does.

So, with that in mind, it's possible to compile something like

create or replace package return as
  subtype return is varchar2(10);
end return;
/

create or replace package tq84 as

    constant constant 

    return . return := 'return';

    function function 

    return   return . return;

end tq84;
/

which looks strange, at least at first sight.

So then, I guess, it's not a compiler bug because return is allowed as a variable name, but then, it's disputable if the compiler should at least give a warning if a variable with the same name is declared multiple times.

like image 447
René Nyffenegger Avatar asked Dec 22 '10 14:12

René Nyffenegger


People also ask

What is compiler in PL SQL?

Before Oracle can run a PL/SQL program, that program must be compiled. This means that the compiler (some software inside Oracle) converts your PL/SQL from source code form into another form.

How PL SQL is compiled?

When PL/SQL is loaded into the server it is compiled to byte code before execution. The process of native compilation converts PL/SQL stored procedures to native code shared libraries which are linked into the kernel resulting in performance increases for the procedural code.

How do I find the compiler log in SQL Developer?

control-shift-L should open the log(s) for you. this will by default be the messages log, but if you create the item that is creating the error the Compiler Log will show up (for me the box shows up in the bottom middle left).

Where is PL SQL compiled and stored?

By default, PL/SQL code is compiled and stored in the form of byte code ready for execution. During the execution process, this byte code is interpreted, a process which requires time and resources.


1 Answers

Apparently it allows you to use the name "return" as a variable. In that case it's declaring a package variable. I would have figured that would fail because it's a keyword, but I tried it and it worked.

Try this code:

create or replace package tq84 as 
  return varchar2(10); 
  somevar varchar2(5); 
  somevar varchar2(5); 
end tq84; 
/ 

set serveroutput on 
BEGIN 
  tq84.return:='Test'; 
  dbms_output.put_line(tq84.return); 
END; 
/ 

That shows return as a variable and it allows the same variable of another name to be declared more than once.

Now if you try to access somevar, then you get this:

PLS-00371: at most one declaration for 'TQ84.SOMEVAR' is permitted 

So apparently it delays that check for some reason.

I just found out how you can detect these problems at compile time. Add this:

alter session set plsql_warnings = 'enable:all'; 

That code above compiles with these warnings:

LINE/COL ERROR 
-------- ----------------------------------------------------------------- 
1/1      PLW-05018: unit TQ84 omitted optional AUTHID clause; default 
         value DEFINER used 

2/3      PLW-06010: keyword "RETURN" used as a defined name 
4/3      PLW-05001: previous use of 'SOMEVAR' (at line 3) conflicts with 
         this use 
like image 144
JOTN Avatar answered Nov 15 '22 22:11

JOTN