Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrong order by in table join storedproc [CORE198] #525

Closed
firebird-automations opened this issue Apr 6, 2001 · 5 comments
Closed

wrong order by in table join storedproc [CORE198] #525

firebird-automations opened this issue Apr 6, 2001 · 5 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: Alice F. Bird (firebirds)

Assigned to: @ArnoBrinkman

SFID: 414420#⁠
Submitted By: nobody

InterBase will not sort correctly the result set of a table joined with a stored procedure (inner join),
if an ascending sort on an indexed field is requested.

To expose the bug we need to join a table with a stored procedure; here's the test metadata
(tested with dialect 1):

CREATE TABLE MY_TABLE
(
K VARCHAR(10) NOT NULL,
D1 INTEGER,
D2 INTEGER,
V1 VARCHAR(10),
PRIMARY KEY (K)
);

CREATE PROCEDURE SELECT_ME
RETURNS
(
DATA VARCHAR(10)
)
AS
begin
data = 'one';
suspend;
data = 'two';
suspend;
data = 'three';
suspend;
end

Let's populate my_table with some test data:

insert into my_table values ('one', 1, 99, 'zz');
insert into my_table values ('two', 2, 98, 'yy');
insert into my_table values ('three', 3, 97, 'xx');

The following query exposes the bug:

select *
from my_table t join select_me p
on (t.k = p.data)
order by t.d1

The ordering is correct on every field we put in the ORDER BY clause, UNLESS that field is also
indexed. Here are the correct results of the query:

K D1 D2 V1 DATA
========== ============ ============ ========== ==========
one 1 99 zz one
two 2 98 yy two
three 3 97 xx three

If we now do:

CREATE INDEX I1 ON MY_TABLE(D1);

the same query returns the following results:

K D1 D2 V1 DATA
========== ============ ============ ========== ==========
one 1 99 zz one
three 3 97 xx three
two 2 98 yy two

In short, if an inner join between a table and a stored procedure is performed, ordering will work
only on non-indexed fields. Please note that:
- if we do a left join instead the problem disappears.
- if we remove the index the problem also disappears.
- if we order by t.k (the table's primary key) the results are always correct.
- if we request a descending order the problem also disappears.

@firebird-automations
Copy link
Collaborator Author

Commented by: Alice F. Bird (firebirds)

Date: 2002-02-25 04:49
Sender: robocop
Logged In: YES
user_id=62823

Bug in the optimizer.

@firebird-automations
Copy link
Collaborator Author

Commented by: Alice F. Bird (firebirds)

Date: 2002-02-20 11:29
Sender: achi
Logged In: YES
user_id=145338

If you agregate on such a query then this is not working
correctly anymore, i.e.

select k, sum(d1), sum(d2)
from my_table t join select_me p on (t.k = p.data)
group by k

can return something like (with some more test datas)
K1 sum_D1 sum_D2
----------------------
one 3 224
two 2 393
one 1 220
three 4 322

I guess the engine is trying to do the same sort as in the
problem above - but the result set is not really ordered

@firebird-automations
Copy link
Collaborator Author

Modified by: @pcisar

Workflow: jira [ 10222 ] => Firebird [ 14451 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @pavel-zotov

QA Status: No test

@firebird-automations
Copy link
Collaborator Author

Modified by: @pavel-zotov

status: Closed [ 6 ] => Closed [ 6 ]

QA Status: No test => Done successfully

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant