Skip to content

Commit

Permalink
Release 1.1.16:
Browse files Browse the repository at this point in the history
Gradle 7.6.1
Kotlin 1.8.21
clickhouse-jdbc 0.4.6 + fixed to make it work
joda-time 2.12.2
code cleanup
  • Loading branch information
Tapac committed May 18, 2023
1 parent 8a281c7 commit 77ecd43
Show file tree
Hide file tree
Showing 20 changed files with 58 additions and 57 deletions.
20 changes: 11 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
import tanvd.kosogor.proxy.publishJar

group = "tanvd.aorm"
version = "1.1.15"
version = "1.1.16"

plugins {
kotlin("jvm") version "1.7.10" apply true
id("tanvd.kosogor") version "1.0.15"
kotlin("jvm") version "1.8.21" apply true
id("tanvd.kosogor") version "1.0.18"
}

val artifactoryUploadEnabled = System.getenv("artifactory_url") != null
Expand All @@ -19,15 +19,15 @@ repositories {

dependencies {
api(kotlin("stdlib"))
api("com.clickhouse", "clickhouse-jdbc", "0.3.2-patch11")
api("joda-time", "joda-time", "2.11.1")
api("com.clickhouse", "clickhouse-jdbc", "0.4.6")
api("joda-time", "joda-time", "2.12.2")
api("org.slf4j", "slf4j-api", "1.7.36")

testImplementation("org.junit.jupiter", "junit-jupiter-api", "5.8.2")
testImplementation("org.junit.jupiter", "junit-jupiter-engine", "5.8.2")

testImplementation("org.testcontainers", "testcontainers", "1.17.5")
testImplementation("org.testcontainers", "clickhouse", "1.17.5")
testImplementation("org.testcontainers", "clickhouse", "1.18.1")
testImplementation("org.lz4", "lz4-java", "1.8.0")
}

tasks.withType<JavaCompile> {
Expand All @@ -38,8 +38,10 @@ tasks.withType<JavaCompile> {
tasks.withType<KotlinJvmCompile>().configureEach {
kotlinOptions {
jvmTarget = "11"
apiVersion = "1.7"
languageVersion = "1.7"
apiVersion = "1.8"
languageVersion = "1.8"
freeCompilerArgs += "-Xuse-ir"
freeCompilerArgs += "-Xbackend-threads=3"
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
23 changes: 11 additions & 12 deletions src/main/kotlin/tanvd/aorm/DbType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tanvd.aorm
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import ru.yandex.clickhouse.ClickHouseUtil
import tanvd.aorm.utils.escapeQuotes
import java.math.BigDecimal
import java.sql.*
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -301,8 +301,8 @@ class DbArrayInt8 : DbNumericArrayType<Byte>("Array(Int8)",
{ DbInt8() })

class DbArrayUInt8 : DbNumericArrayType<Byte>("Array(UInt8)",
{ resultSet, name -> (resultSet.getArray(name).array as ShortArray).map { it.toByte() } },
{ resultSet, index -> (resultSet.getArray(index).array as ShortArray).map { it.toByte() } },
{ resultSet, name -> (resultSet.getArray(name).array as ByteArray).toList() },
{ resultSet, index -> (resultSet.getArray(index).array as ByteArray).toList() },
{ DbUInt8() })

class DbArrayInt16 : DbNumericArrayType<Short>("Array(Int16)",
Expand All @@ -311,8 +311,8 @@ class DbArrayInt16 : DbNumericArrayType<Short>("Array(Int16)",
{ DbInt16() })

class DbArrayUInt16 : DbNumericArrayType<Short>("Array(UInt16)",
{ resultSet, name -> (resultSet.getArray(name).array as IntArray).map { it.toShort() } },
{ resultSet, index -> (resultSet.getArray(index).array as IntArray).map { it.toShort() } },
{ resultSet, name -> (resultSet.getArray(name).array as ShortArray).toList() },
{ resultSet, index -> (resultSet.getArray(index).array as ShortArray).toList() },
{ DbUInt16() })

class DbArrayInt32 : DbNumericArrayType<Int>("Array(Int32)",
Expand All @@ -321,16 +321,15 @@ class DbArrayInt32 : DbNumericArrayType<Int>("Array(Int32)",
{ DbInt32() })

class DbArrayUInt32 : DbNumericArrayType<Int>("Array(UInt32)",
{ resultSet, name -> (resultSet.getArray(name).array as LongArray).map { it.toInt() } },
{ resultSet, index -> (resultSet.getArray(index).array as LongArray).map { it.toInt() } },
{ resultSet, name -> (resultSet.getArray(name).array as IntArray).toList() },
{ resultSet, index -> (resultSet.getArray(index).array as IntArray).toList() },
{ DbUInt32() })

class DbArrayInt64 : DbNumericArrayType<Long>("Array(Int64)",
{ resultSet, name -> (resultSet.getArray(name).array as LongArray).toList() },
{ resultSet, index -> (resultSet.getArray(index).array as LongArray).toList() },
{ DbInt64() })

@Suppress("UNCHECKED_CAST")
class DbArrayUInt64 : DbNumericArrayType<Long>("Array(UInt64)",
{ resultSet, name -> (resultSet.getArray(name).array as LongArray).toList() },
{ resultSet, index -> (resultSet.getArray(index).array as LongArray).toList() },
Expand Down Expand Up @@ -381,11 +380,11 @@ class DbArrayBoolean : DbArrayType<Boolean>() {
override fun toSqlName(): String = "Array(UInt8)"

override fun getValue(name: String, result: ResultSet): List<Boolean> {
return (result.getArray(name).array as ShortArray).map { it== 1.toShort() }
return (result.getArray(name).array as ByteArray).map { it == 1.toByte() }
}

override fun getValue(index: Int, result: ResultSet): List<Boolean> {
return (result.getArray(index).array as ShortArray).map { it == 1.toShort() }
return (result.getArray(index).array as ByteArray).map { it == 1.toByte() }
}

override fun setValue(index: Int, statement: PreparedStatement, value: List<Boolean>) {
Expand Down Expand Up @@ -419,7 +418,7 @@ class DbString : DbPrimitiveType<String>() {
statement.setString(index, value)
}

override fun toStringValue(value: String): String = "'${ClickHouseUtil.escape(value)}'"
override fun toStringValue(value: String): String = "'${value.escapeQuotes()}'"

override fun toArray(): DbArrayType<String> = DbArrayString()
}
Expand All @@ -446,7 +445,7 @@ class DbArrayString : DbArrayType<String>() {
}

override fun toStringValue(value: List<String>): String =
value.joinToString(prefix = "[", postfix = "]") { "'${ClickHouseUtil.escape(it)}'" }
value.joinToString(prefix = "[", postfix = "]") { "'${it.escapeQuotes()}'" }

override fun toPrimitive(): DbPrimitiveType<String> = DbString()
}
2 changes: 1 addition & 1 deletion src/main/kotlin/tanvd/aorm/Engine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ sealed class Engine {
(sample_by?.let { "SAMPLE BY ${it.toQueryQualifier()} " } ?: "")

override fun toSqlDef() = if (extendedSyntaxUsed)
"${familyModifier}MergeTree(${specificParams?.let { it } ?: ""}) $extendedSyntaxDef"
"${familyModifier}MergeTree(${specificParams ?: ""}) $extendedSyntaxDef"
else
"${familyModifier}MergeTree($mainSyntaxDef${specificParams?.let { ", $it" } ?: ""})"

Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/tanvd/aorm/Row.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ data class InsertRow(val values: MutableMap<Column<*, DbType<*>>, Any>) {
data class SelectRow(val values: MutableMap<Expression<*, DbType<*>>, Any>) {
val columns = LinkedHashSet(values.map { it.key })

constructor(result: ResultSet, expressions: Set<Expression<*, DbType<*>>>) : this(expressions.withIndex().map { (index, expr) ->
expr to expr.getValue(result, index + 1)
}.toMap().toMutableMap())
constructor(result: ResultSet, expressions: Set<Expression<*, DbType<*>>>) : this(
expressions.withIndex().associateTo(hashMapOf()) { (index, expr) ->
expr to expr.getValue(result, index + 1)
})

@Suppress("UNCHECKED_CAST")
fun <E : Any, K : DbType<E>> getOrNull(expression: Expression<E, K>): E? = values[expression] as E?
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/tanvd/aorm/Table.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package tanvd.aorm

import ru.yandex.clickhouse.ClickHouseUtil
import tanvd.aorm.expression.Column
import tanvd.aorm.expression.Expression
import tanvd.aorm.query.Query
import tanvd.aorm.utils.escapeQuotes
import java.util.*
import kotlin.reflect.KClass


abstract class Table(name: String) {
var name: String = ClickHouseUtil.escape(name)
var name: String = name.escapeQuotes()

//todo: encapsulate this
val columns: LinkedHashSet<Column<*, DbType<*>>> = linkedSetOf()
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/tanvd/aorm/View.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package tanvd.aorm

import ru.yandex.clickhouse.ClickHouseUtil
import tanvd.aorm.query.Query
import tanvd.aorm.utils.escapeQuotes

abstract class View(name: String) {
var name: String = ClickHouseUtil.escape(name)!!
var name: String = name.escapeQuotes()

abstract val query: Query
}

abstract class MaterializedView(name: String) {
var name: String = ClickHouseUtil.escape(name)!!
var name: String = name.escapeQuotes()

abstract val query: Query
abstract val engine: Engine
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/tanvd/aorm/expression/Column.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package tanvd.aorm.expression

import ru.yandex.clickhouse.ClickHouseUtil
import tanvd.aorm.DbType
import tanvd.aorm.Table
import tanvd.aorm.utils.escapeQuotes

class Column<E : Any, out T : DbType<E>>(val name: String, type: T, val table: Table,
default: (() -> E)? = null) : Expression<E, T>(type) {
override fun toQueryQualifier(): String = ClickHouseUtil.escape(name)
override fun toQueryQualifier(): String = name.escapeQuotes()

override fun toSelectListDef(): String = ClickHouseUtil.escape(name)
override fun toSelectListDef(): String = name.escapeQuotes()

fun toSqlDef(): String = "${ClickHouseUtil.escape(name)} ${type.toSqlName()}"
fun toSqlDef(): String = "${name.escapeQuotes()} ${type.toSqlName()}"

var defaultFunction: (() -> E)? = default

Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/tanvd/aorm/expression/Expression.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tanvd.aorm.expression

import ru.yandex.clickhouse.ClickHouseUtil
import tanvd.aorm.*
import tanvd.aorm.utils.escapeQuotes
import java.sql.PreparedStatement
import java.sql.ResultSet

Expand Down Expand Up @@ -37,11 +37,11 @@ abstract class Expression<E : Any, out T : DbType<E>>(val type: T) {
class AliasedExpression<E : Any, out T : DbType<E>, Y : Expression<E, T>>(val name: String, val expression: Y) :
Expression<E, T>(expression.type) {
var materializedInView: String? = null
val alias = ValueExpression(ClickHouseUtil.escape(name), type)
val alias = ValueExpression(name.escapeQuotes(), type)

override fun toSelectListDef(): String = "${expression.toQueryQualifier()} as ${ClickHouseUtil.escape(name)}"
override fun toSelectListDef(): String = "${expression.toQueryQualifier()} as ${name.escapeQuotes()}"

override fun toQueryQualifier(): String = ClickHouseUtil.escape(name)
override fun toQueryQualifier(): String = name.escapeQuotes()
}

fun <E : Any, T : DbType<E>, Y : Expression<E, T>> alias(name: String, expression: Y): AliasedExpression<E, T, Y> = AliasedExpression(name, expression)
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/tanvd/aorm/expression/ValueExpression.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package tanvd.aorm.expression

import ru.yandex.clickhouse.ClickHouseUtil
import tanvd.aorm.DbType
import tanvd.aorm.utils.escapeQuotes

class ValueExpression<E : Any, out T : DbType<E>>(val name: String, type: T) : Expression<E, T>(type) {
override fun toSelectListDef(): String = ClickHouseUtil.escape(name)
override fun toSelectListDef(): String = name.escapeQuotes()

override fun toQueryQualifier(): String = ClickHouseUtil.escape(name)
override fun toQueryQualifier(): String = name.escapeQuotes()
}

class SqlValueExpression<E : Any, out T : DbType<E>>(val value: String, type: T) : Expression<E, T>(type) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/tanvd/aorm/utils/Threads.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private fun <T> ExecutorService.executeAndAwait(timeout: Long, logger: Logger, e
exec(this)
awaitTermination(timeout, TimeUnit.MILLISECONDS)
if (!isTerminated) {
logger.warn("ExecutorService ${this} was asked to shutdown gracefully, but was not stopped in $timeout ms.")
logger.warn("ExecutorService $this was asked to shutdown gracefully, but was not stopped in $timeout ms.")
println(Thread.currentThread().stackTrace.joinToString("\n\t") { it.toString() })
}
}
9 changes: 9 additions & 0 deletions src/main/kotlin/tanvd/aorm/utils/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tanvd.aorm.utils

import com.clickhouse.data.ClickHouseUtils

internal inline fun <T : AutoCloseable?, R> T.use(block: (T) -> R): R {
var exception: Throwable? = null
try {
Expand All @@ -21,3 +23,10 @@ internal inline fun <T : AutoCloseable?, R> T.use(block: (T) -> R): R {
}
}
}

private fun String.escape(char: Char): String = ClickHouseUtils.escape(this, char)

/**
The list of characters should be in sync with [ClickHouseUtils.isQuote]
*/
internal fun String.escapeQuotes(): String = escape('"').escape('\'').escape('`')
4 changes: 0 additions & 4 deletions src/test/kotlin/tanvd/aorm/api/InsertTableTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class InsertTableTest : AormTestBase() {
}
}

@Suppress("UNCHECKED_CAST")
@Test
fun insert_tableExistsRowValid_rowInserted() {
withDatabase(database) {
Expand All @@ -34,7 +33,6 @@ class InsertTableTest : AormTestBase() {
}
}

@Suppress("UNCHECKED_CAST")
@Test
fun insertBatch_tableExistsRowValid_rowsInserted() {
withDatabase(database) {
Expand All @@ -55,7 +53,6 @@ class InsertTableTest : AormTestBase() {
}
}

@Suppress("UNCHECKED_CAST")
@Test
fun insertLazy_tableExistsRowsValid_rowsInsertedAfterDelay() {
withDatabase(database, insertWorker) {
Expand Down Expand Up @@ -84,7 +81,6 @@ class InsertTableTest : AormTestBase() {
}
}

@Suppress("UNCHECKED_CAST")
@Test
fun insertLazy_tableExistsRowsValid_rowsNotInsertedBeforeDelay() {
withDatabase(database, insertWorker) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import tanvd.aorm.query.*
import tanvd.aorm.utils.*
import tanvd.aorm.withDatabase


@Suppress("UNCHECKED_CAST")
class QueryGroupByTest : AormTestBase() {

override fun executeBeforeMethod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import tanvd.aorm.query.*
import tanvd.aorm.utils.*
import tanvd.aorm.withDatabase

@Suppress("UNCHECKED_CAST")
class QueryLimitTest : AormTestBase() {
override fun executeBeforeMethod() {
withDatabase(database) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import tanvd.aorm.query.*
import tanvd.aorm.utils.*
import tanvd.aorm.withDatabase

@Suppress("UNCHECKED_CAST")
class QueryOrderTest : AormTestBase() {
override fun executeBeforeMethod() {
withDatabase(database) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MetadataClickhouseTest : AormTestBase() {
TableClickhouse.create(database, ExampleTable)

Assertions.assertEquals(MetadataClickhouse.columnsOfTable(database, ExampleTable),
ExampleTable.columns.map { it.name to it.type.toSqlName() }.toMap())
ExampleTable.columns.associate { it.name to it.type.toSqlName() })
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/tanvd/aorm/type/TypesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import tanvd.aorm.query.*
import tanvd.aorm.utils.*
import java.math.BigDecimal

@Suppress("UNCHECKED_CAST")
class TypesTest : AormTestBase() {

@BeforeEach
Expand All @@ -17,6 +16,7 @@ class TypesTest : AormTestBase() {
try {
AllTypesTable.drop()
} catch (e: Exception) {
//ignore
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/test/kotlin/tanvd/aorm/utils/AormTestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ abstract class AormTestBase {
companion object {
const val testInsertWorkerDelayMs = 2000L

val serverContainer = object : ClickHouseContainer("clickhouse/clickhouse-server:22.3.8.39-alpine") {
override fun getDriverClassName(): String = "com.clickhouse.jdbc.ClickHouseDriver"
}.apply {
val serverContainer = ClickHouseContainer("clickhouse/clickhouse-server:22.3.8.39-alpine").apply {
start()
// we need to disable query parser stack limitation for tests, so we add line "<max_parser_depth>0</max_parser_depth>" to the config:
execInContainer("sed", "-i", "/^.*\\<max_memory_usage\\>.*/a \\<max_parser_depth\\>0\\<\\/max_parser_depth\\>", "/etc/clickhouse-server/users.xml")
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/tanvd/aorm/utils/ExampleTable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ object ExampleMaterializedView : MaterializedView("ExampleMaterializedView") {

override val query: Query = ExampleTable.select(date, idView, valueView)

override val engine: Engine = Engine.MergeTree(ExampleMaterializedView.date, listOf(ExampleMaterializedView.idView, ExampleMaterializedView.valueView))
override val engine: Engine = Engine.MergeTree(date, listOf(idView, valueView))
}

0 comments on commit 77ecd43

Please sign in to comment.