[CONJ-338] Inconsistent/wrong resultset when using variables and nested selects Created: 2016-08-24  Updated: 2017-11-21  Resolved: 2017-11-21

Status: Closed
Project: MariaDB Connector/J
Component/s: Other
Affects Version/s: 1.4.5, 1.4.6
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Artur Kahre Assignee: Diego Dupin
Resolution: Won't Do Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-10657 incorrect result returned with binary... Closed

 Description   

Ran into an issue where a query that uses user-defined variable and has multiple nested selects, returned right amount of rows, but all rows where identical (what should've been the last row).

Reproduced a simplified example:

Any table that has some (different) rows - SOMESCHEMA.SOMETABLE
Query which uses a user-defined variable to return row count (original use was for paging results),

SELECT X.* FROM (SELECT CODE, RN FROM (SELECT A.CODE, @cnt := @cnt + 1 AS RN FROM SOMESCHEMA.SOMETABLE A, (SELECT @cnt := 0) C) T) X;

&

SELECT CODE, RN FROM (SELECT A.CODE, @cnt := @cnt + 1 AS RN FROM SOMESCHEMA.SOMETABLE A, (SELECT @cnt := 0) C) T;

They should return identical results, and they do when run manually (via HeidiSQL).
But when 1st query is executed with mariadb connector, it returns right amount of rows, but each one is identical (what should be the last row), 2nd query works as expected.
Does not reproduce with mysql connector (6.0.3).

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class MariaTest2 {
 
	static String sql = "SELECT CODE, RN FROM (SELECT A.CODE, @cnt := @cnt + 1 AS RN FROM SOMESCHEMA.SOMETABLE A, (SELECT @cnt := 0) C) T";
//	static String sql = "SELECT X.* FROM (SELECT CODE, RN FROM (SELECT A.CODE, @cnt := @cnt + 1 AS RN FROM SOMESCHEMA.SOMETABLE A, (SELECT @cnt := 0) C) T) X";
	
	static String connUrl = "jdbc:mariadb://url";
	
	
	public static void main( String[] args ) throws SQLException {
		Connection conn = null;
		try {		
			conn = DriverManager.getConnection( connUrl );
			PreparedStatement stmt = conn.prepareStatement( sql );
			ResultSet rs = stmt.executeQuery();
			while ( rs.next() ) {
				System.out.print( rs.getLong( "CODE" ) );
				System.out.print( " " );
				System.out.print( rs.getLong( "RN" ) );
				System.out.println();
			}
			rs.close();
			stmt.close();
 
		} catch ( Exception e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			conn.close();
		}
 
	}
 
}

Behaviour persists when using SET as separate execute before the query, e.g:

String sql = "SELECT X.* FROM (SELECT CODE, RN FROM (SELECT A.CODE, @cnt := @cnt + 1 AS RN FROM SOMESCHEMA.SOMETABLE A) T) X";
PreparedStatement stmt;			
stmt = conn.prepareStatement( "SET @cnt := 0" );
stmt.executeQuery();			
stmt = conn.prepareStatement( sql );
ResultSet rs = stmt.executeQuery();



 Comments   
Comment by Diego Dupin [ 2016-08-24 ]

This is in fact a server issue. MDEV-10657 has been created to solve that.
Mariadb driver has the problem because it use binary protocol by default (corresponding to option useServerPrepStmts).
Mysql is using text protocol by default (useServerPrepStmts=false)

Thanks for reporting this !

Comment by Diego Dupin [ 2017-11-21 ]

closing here since it's a server issue

Generated at Thu Feb 08 03:14:56 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.