Return-Path: X-Original-To: apmail-db-derby-dev-archive@www.apache.org Delivered-To: apmail-db-derby-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9CCCBF971 for ; Sat, 11 May 2013 08:23:17 +0000 (UTC) Received: (qmail 29069 invoked by uid 500); 11 May 2013 08:23:17 -0000 Delivered-To: apmail-db-derby-dev-archive@db.apache.org Received: (qmail 28831 invoked by uid 500); 11 May 2013 08:23:16 -0000 Mailing-List: contact derby-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: Delivered-To: mailing list derby-dev@db.apache.org Received: (qmail 28728 invoked by uid 99); 11 May 2013 08:23:16 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 11 May 2013 08:23:16 +0000 Date: Sat, 11 May 2013 08:23:16 +0000 (UTC) From: "Dag H. Wanvik (JIRA)" To: derby-dev@db.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Updated] (DERBY-6148) Peculiar sorting behaviour MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/DERBY-6148?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Dag H. Wanvik updated DERBY-6148: --------------------------------- Attachment: q2.txt q1.txt > Peculiar sorting behaviour > -------------------------- > > Key: DERBY-6148 > URL: https://issues.apache.org/jira/browse/DERBY-6148 > Project: Derby > Issue Type: Bug > Components: SQL > Affects Versions: 10.9.1.0 > Environment: Windows 7 64-bit, Ubuntu 32-bit; JRE 1.7.0_11+ > Reporter: John English > Attachments: bug.zip, bug.zip, q1.txt, q2.txt > > > I have a query that looks like this: > SELECT tests.id,tests.item,title FROM tests,item_usage > WHERE username=? AND user_role>? > AND item_usage.item=tests.item > ORDER BY tests.item,title > The result ordering is by item code followed by title, but the item codes are listed in the order in which they appear in the ITEMS table where they are the primary key rather than in ascending order as expected. If however I change the ORDER BY clause to sort by item_usage.item rather than tests.item, it works correctly, even though the two values are the same! > The same thing happens in another unrelated query involving item_usage, and the same workaround cures it. > The relevant tables are defined like so: > CREATE TABLE item_usage ( > username VARCHAR(15) NOT NULL, > item VARCHAR(15) NOT NULL, > value SMALLINT DEFAULT 0, > CONSTRAINT item_usage_pk PRIMARY KEY (username,item), > CONSTRAINT item_usage_1 FOREIGN KEY (username) > REFERENCES users(username) > ON DELETE CASCADE, > CONSTRAINT item_usage_2 FOREIGN KEY (item) > REFERENCES items(item) > ON DELETE CASCADE, > CONSTRAINT item_usage_3 CHECK (value BETWEEN 0 AND 4) > ); > CREATE TABLE tests ( > id INTEGER GENERATED ALWAYS AS IDENTITY, > item VARCHAR(15) NOT NULL, > title VARCHAR(255) NOT NULL, > disp SMALLINT NOT NULL DEFAULT 0, > starttime TIMESTAMP DEFAULT NULL, > endtime TIMESTAMP DEFAULT NULL, > offsetx INTEGER NOT NULL DEFAULT 0, > offsety INTEGER NOT NULL DEFAULT 0, > rate INTEGER NOT NULL DEFAULT 0, > duration INTEGER NOT NULL DEFAULT 0, > calibrate INTEGER NOT NULL DEFAULT 0, > deadline TIMESTAMP DEFAULT NULL, > stepsize INTEGER NOT NULL DEFAULT 0, > interval INTEGER NOT NULL DEFAULT 0, > stand CHAR(1) DEFAULT NULL, > hidden CHAR(1) DEFAULT NULL, > repeated CHAR(1) DEFAULT NULL, > private CHAR(1) DEFAULT NULL, > sequential CHAR(1) DEFAULT NULL, > final CHAR(1) DEFAULT NULL, > notes CLOB DEFAULT NULL, > testxml CLOB NOT NULL, > author VARCHAR(15) NOT NULL, > time TIMESTAMP NOT NULL, > CONSTRAINT tests_pk PRIMARY KEY (id), > CONSTRAINT tests_1 UNIQUE (item, title), > CONSTRAINT tests_2 FOREIGN KEY (item) > REFERENCES items(item) > ON DELETE CASCADE, > CONSTRAINT tests_3 CHECK (disp BETWEEN 0 AND 100), > CONSTRAINT tests_4 CHECK (rate BETWEEN 0 AND 100), > CONSTRAINT tests_5 CHECK (stepsize BETWEEN 0 AND 100) > ); > If I run the query manually I get this, as expected: > ID ITEM TITLE > 37 60001 Test 1 > 42 60001 Test 2 > 51 60001 Test 3 > 17 61303 Test 2a > 16 61303 Test 2b > 7 7205731 Test 2a > 8 7205731 Test 2b > Now, this is actually part of a web app that should turn this into a list of options in a