Skip to content

Commit

Permalink
web: fix player history request - CH is not using projection
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackmorse committed Jan 19, 2024
1 parent a7c91ba commit 480fd77
Show file tree
Hide file tree
Showing 6 changed files with 29,062 additions and 29,004 deletions.
4 changes: 3 additions & 1 deletion sql/init_scripts/12-math-details-annoy.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
set allow_experimental_annoy_index = 1;

CREATE TABLE hattrick.match_details_annoy
(
`match_id` UInt64,
Expand All @@ -16,4 +18,4 @@ CREATE TABLE hattrick.match_details_annoy
)
ENGINE = MergeTree
ORDER BY tuple()
SETTINGS index_granularity = 8192
SETTINGS index_granularity = 8192;
57,982 changes: 28,991 additions & 28,991 deletions sql/init_scripts/sample_data/player_stats.csv

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions sqlBuilder/src/main/scala/sqlbuilder/SqlBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ case class SqlBuilder(var name: String = "main"/*for the nested requests*/) {
private var _select: Select = _
private var limitByNumber = 0
private var limitByField: Option[String] = None
private val settingsMap = mutable.Map[String, Any]()
var withSelect: Option[WithSelect] = None

def select: Select = {
Expand Down Expand Up @@ -108,6 +109,11 @@ case class SqlBuilder(var name: String = "main"/*for the nested requests*/) {
this
}

def setting(name: String, value: Any): SqlBuilder = {
settingsMap += (name -> value)
this
}

def parameters: mutable.Buffer[Parameter] = this.withSelect.map(ws => ws.sqlBuilder.parameters).getOrElse(mutable.Buffer()) ++ whereClause.parameters ++ havingClause.parameters ++ this._select.parameters

def sqlWithParameters(): SqlWithParameters = {
Expand Down Expand Up @@ -138,13 +144,24 @@ case class SqlBuilder(var name: String = "main"/*for the nested requests*/) {
} else {
""
}

val settingsPart = if(settingsMap.isEmpty) {
""
} else {
val set= settingsMap.map{
case (k, v: Int) => s"$k = $v"
case (k, v: Any) => s"""$k = "$v""""
}.mkString(", ")
s"SETTINGS $set"
}
withClause +
s"$selectFrom " +
s"$where " +
s"$groupBy " +
s"$havingString " +
s"$orderBy " +
s"${this.limitByField.map(lField => s"LIMIT ${this.limitByNumber} BY $lField").getOrElse("")} " +
s"$limit"
s"$limit " +
s"$settingsPart "
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import anorm.RowParser
import databases.dao.RestClickhouseDAO
import databases.requests.ClickhouseRequest
import databases.requests.model.player.PlayerHistory
import sqlbuilder.Select
import sqlbuilder.{Select, SqlBuilder}

import scala.concurrent.Future

object PlayerHistoryRequest extends ClickhouseRequest[PlayerHistory] {
override val rowParser: RowParser[PlayerHistory] = PlayerHistory.mapper
import sqlbuilder.SqlBuilder.implicits._

def execute(playerId: Long)(implicit restClickhouseDAO: RestClickhouseDAO): Future[List[PlayerHistory]] = {
import sqlbuilder.SqlBuilder.implicits._

val builder = Select(
def builder(playerId: Long): SqlBuilder =
Select(
"league_id",
"league_unit_id",
"league_unit_name",
Expand Down Expand Up @@ -43,7 +42,8 @@ object PlayerHistoryRequest extends ClickhouseRequest[PlayerHistory] {
//TODO: ability to add typed parameters with custom name
.and(s"player_id = $playerId")
.orderBy("season", "round", "cup_level".asc)
.setting("optimize_read_in_order", 0)

restClickhouseDAO.execute(builder.sqlWithParameters().build, rowParser)
}
def execute(playerId: Long)(implicit restClickhouseDAO: RestClickhouseDAO): Future[List[PlayerHistory]] =
restClickhouseDAO.execute(builder(playerId).sqlWithParameters().build, rowParser)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package databases.requests.playerstats.player

import common.StringExt.StringExt
import databases.requests.ClickhouseRequest
import org.scalatest.{FunSuite, Matchers}

class PlayerHistoryRequestTest extends FunSuite with Matchers {
test("Test player history request with settings") {
PlayerHistoryRequest.builder(1L).sqlWithParameters()
.sql
.normalize() shouldBe (
s"""SELECT league_id,
|league_unit_id,
|league_unit_name,
|player_id,
|first_name,
|last_name,
|team_id,
|team_name,
|(age * 112) + days as age,
|tsi,
|rating,
|rating_end_of_match,
|cup_level,
|${ClickhouseRequest.roleIdCase("role_id")} as role,
|played_minutes,
|injury_level,
|salary,
|yellow_cards,
|red_cards,
|goals,
|season,
|round,
|nationality
|FROM hattrick.player_stats
| WHERE ((player_id = 1)) ORDER BY season DESC , round DESC , cup_level ASC SETTINGS optimize_read_in_order = 0"""
.stripMargin.normalize()
)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package databases.requests.playerstats.player
package databases.requests.playerstats.player.stats

import common.StringExt.StringExt
import databases.requests.{ClickhouseRequest, OrderingKeyPath}
import models.web.{Accumulate, Asc, PlayersParameters, RestStatisticsParameters}
import org.scalatest.{FunSuite, Matchers}
import common.StringExt.StringExt
import databases.requests.playerstats.player.stats.PlayerGamesGoalsRequest

class PlayerGamesGoalsRequestTest extends FunSuite with Matchers {
test("Test having clause") {
Expand Down

0 comments on commit 480fd77

Please sign in to comment.