mysql - Different result from Golang db.query and actually running on DB -
i have mysql db on run query gets top row each group (according grouping criterion):
select ranked.id, ranked.os, ranked.version, ranked.timestamp, agent_metadata.* (select *, @os_rank := if(@current_os = os, @os_rank + 1, 1) os_rank, @current_os := os default_version order os desc, timestamp desc ) ranked inner join agent_metadata on ranked.os = agent_metadata.os , ranked.version = agent_metadata.version os_rank = 1;
when run query on db (using datagrip) expected result.
however, when run same query go code using db.query
, instead of getting 2 rows, 5 rows.
i have logic builds query before running print log:
// ... build query query := qb.string() logger.debugf("executing: %s", query) if rows, err := repository.connection.query(query); err != nil { return defaults, errors.wrap(err, "get default versions") } else { defer rows.close() rows.next() { result := defaultversionextended{} err := rows.scan( &result.id, &result.defaultversion.os, &result.defaultversion.version, &result.timestamp, &result.agentmetadata.os, &result.agentmetadataextended.version, &result.agentmetadataextended.name, &result.agentmetadataextended.description, &result.agentmetadataextended.releasenotesurl, &result.agentmetadataextended.uploadtime) if err != nil { return defaults, errors.wrap(err, "get default versions") } defaults = append(defaults, result) } err = rows.err() if err != nil { return defaults, errors.wrap(err, "get default versions") } return defaults, errors.wrap(err, "get default versions") }
this output log:
time="2017-07-25t15:07:47+03:00" level=debug msg="executing: select ranked.id, ranked.os, ranked.version, ranked.timestamp, agent_metadata.* (select *, @os_rank := if(@current_os = os, @os_rank + 1, 1) os_rank, @current_os := os default_version order os desc, timestamp desc) ranked inner join agent_metadata on ranked.os = agent_metadata.os , ranked.version = agent_metadata.version os_rank = 1" class="auroradb.agent_repository"
if copy-paste query log db console , run correct result (2 rows):
however, unit test fails, because expect result have 2 rows has 5:
error: "[{%!s(int=1) {windows 1.2.3} {{windows 1.2.3 name test agent windows http://notes-heaven.com} 2017-07-25 12:07:45.099 +0000 utc} 2017-07-25 12:07:45.38 +0000 utc} {%!s(int=3) {windows 1.2.3.1} {{windows 1.2.3.1 name 2 second test agent windows http://notes-heaven.com/1} 2017-07-25 12:07:46.237 +0000 utc} 2017-07-25 12:07:46.519 +0000 utc} {%!s(int=4) {windows 1.2.3.2} {{windows 1.2.3.2 name 3 second test agent windows http://notes-heaven.com/3} 2017-07-25 12:07:46.801 +0000 utc} 2017-07-25 12:07:47.082 +0000 utc} {%!s(int=2) {mac 1.2.3.2.m} {{mac 1.2.3.2.m name 3.m second test agent windows http://notes-heaven.com/3} 2017-07-25 12:07:45.662 +0000 utc} 2017-07-25 12:07:45.944 +0000 utc} {%!s(int=5) {mac 1.2.3.5.m} {{mac 1.2.3.5.m name 3.m second test agent windows http://notes-heaven.com/3} 2017-07-25 12:07:47.364 +0000 utc} 2017-07-25 12:07:47.653 +0000 utc}]" should have 2 item(s), has 5
the returned result in fact entire data agent_metadata. it's if entire logic selecting top result or each group didn't work.
i'm using go-sql-driver driver.
Comments
Post a Comment