Skip to content

Commit

Permalink
[TH2-5141] Added truncate-update-query-from-where-clause option tem…
Browse files Browse the repository at this point in the history
…porary.
  • Loading branch information
Nikita-Smirnov-Exactpro committed Jan 23, 2024
1 parent 37e94e4 commit 5e086ab
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ This operation isn't support.

Oracle log miner codec (transformer) has the following parameters:
```yaml
columnPrefix: 'th2_'
saveColumns: [ OPERATION, SQL_REDO, ROW_ID, TIMESTAMP, TABLE_NAME ]
column-prefix: 'th2_'
save-columns: [ OPERATION, SQL_REDO, ROW_ID, TIMESTAMP, TABLE_NAME ]
```
**columnPrefix** - prefix for parsed columns.
**saveColumns** - set of column names to copy from source message.
**truncate-update-query-from-where-clause** - if true, codec truncates the tail of UPDATE query starting from the WHERE clause before deep parsing.
This operation improve performance without negative impact, because codec extracts data from the SET clause only.
**column-prefix** - prefix for parsed columns.
**save-columns** - set of column names to copy from source message.
All columns which log miner allow to select are described in the [document](https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/V-LOGMNR_CONTENTS.html#GUID-B9196942-07BF-4935-B603-FA875064F5C3)
## Full configuration example
Expand All @@ -87,6 +89,7 @@ spec:
disableProtocolCheck: true

codecSettings:
truncate-update-query-from-where-clause: true
column-prefix: th2_
save-columns:
- OPERATION
Expand Down Expand Up @@ -139,7 +142,8 @@ spec:
## Release notes
### 0.1.0
+ Migrated to ANTLR 4 approach for parsing Oracle SQL queries.
+ Migrated to ANTLR 4 approach for parsing Oracle SQL queries.
+ Added `truncate-update-query-from-where-clause` option temporary.

### 0.0.2
+ Publish warning event with details about internal exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ class LogMinerTransformer(private val config: LogMinerConfiguration) : IPipeline
"UPDATE" -> {
message.toBuilderWithoutBody().apply {
bodyBuilder().apply {
val lexer = PlSqlLexer(CharStreams.fromString(sqlRedo))
val preparedQuery = if (config.truncateUpdateQueryFromWhereClause) {
truncateFromWhereClause(sqlRedo)
} else {
sqlRedo
}
val lexer = PlSqlLexer(CharStreams.fromString(preparedQuery))
val tokens = CommonTokenStream(lexer)
val parser = PlSqlParser(tokens)
val walker = ParseTreeWalker()
Expand Down Expand Up @@ -554,6 +559,7 @@ class LogMinerTransformer(private val config: LogMinerConfiguration) : IPipeline
private const val LOG_MINER_ROW_ID_COLUMN = "ROW_ID"
private const val LOG_MINER_TIMESTAMP_COLUMN = "TIMESTAMP"
private const val LOG_MINER_TABLE_NAME_COLUMN = "TABLE_NAME"
private const val WHERE_CLAUSE = "WHERE"

internal val REQUIRED_COLUMNS: Set<String> = hashSetOf(
LOG_MINER_OPERATION_COLUMN,
Expand All @@ -563,6 +569,15 @@ class LogMinerTransformer(private val config: LogMinerConfiguration) : IPipeline
LOG_MINER_TABLE_NAME_COLUMN,
)

internal fun truncateFromWhereClause(query: String): String {
val whereIndex = query.indexOf(WHERE_CLAUSE, ignoreCase = true)
return if (whereIndex == -1) {
query
} else {
"${query.substring(0, whereIndex)};"
}
}

internal fun ParsedMessage.toBuilderWithoutBody() = ParsedMessage.builder().apply {
setId(this@toBuilderWithoutBody.id)
this@toBuilderWithoutBody.eventId?.let(this::setEventId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ class LogMinerConfiguration : IPipelineCodecSettings {
@JsonProperty("save-columns")
@JsonPropertyDescription("Codec saves: extracted columns and this set")
var saveColumns: Set<String> = REQUIRED_COLUMNS

@JsonProperty("truncate-update-query-from-where-clause")
@JsonPropertyDescription("Codec truncates the tail of UPDATE query starting from the WHERE clause")
var truncateUpdateQueryFromWhereClause: Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.exactpro.th2.codec.oracle.logminer
import PlSqlLexer
import PlSqlParser
import com.exactpro.th2.codec.api.IReportingContext
import com.exactpro.th2.codec.oracle.logminer.LogMinerTransformer.Companion.truncateFromWhereClause
import com.exactpro.th2.codec.oracle.logminer.cfg.LogMinerConfiguration
import com.exactpro.th2.common.schema.message.impl.rabbitmq.transport.Direction
import com.exactpro.th2.common.schema.message.impl.rabbitmq.transport.MessageGroup
Expand All @@ -35,6 +36,8 @@ import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
Expand Down Expand Up @@ -331,6 +334,18 @@ class LogMinerTransformerTest {
}
}

@ParameterizedTest
@CsvSource(
"""abcWHEREcde,abc;""",
"""abc WHERE cde,abc ;""",
"""abcwherecde,abc;""",
"""abc where cde,abc ;""",

)
fun `truncateFromWhereClause test`(source: String, target: String) {
assertEquals(target, truncateFromWhereClause(source))
}

private fun loadMessages(): List<ParsedMessage> {
return LogMinerTransformerTest::class.java.getResourceAsStream(
"/com/exactpro/th2/codec/oracle/logminer/log_miner.csv"
Expand Down

0 comments on commit 5e086ab

Please sign in to comment.