Skip to content

#613 Add retries when executing count queries with the Hive JDBC driver.#614

Merged
yruslan merged 1 commit into
mainfrom
feature/613-workaround-hive-issue
Jun 27, 2025
Merged

#613 Add retries when executing count queries with the Hive JDBC driver.#614
yruslan merged 1 commit into
mainfrom
feature/613-workaround-hive-issue

Conversation

@yruslan
Copy link
Copy Markdown
Collaborator

@yruslan yruslan commented Jun 27, 2025

Closes #613

Summary by CodeRabbit

  • Refactor

    • Improved internal logic for counting records from database tables and SQL queries, enhancing maintainability and clarity.
    • Updated error handling and logging for unsupported query types.
    • Enhanced resilience of database query execution by adding retry logic for specific JDBC exceptions.
  • Tests

    • Removed a test related to a deprecated method for SQL expression extraction.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 27, 2025

Walkthrough

The codebase refactors JDBC count query logic by moving native count methods from TableReaderJdbc to TableReaderJdbcBase, introduces retry logic for Hive JDBC driver errors in JdbcNativeUtils, and updates the control flow for record counting in TableReaderJdbcNative. A test for a removed method is also deleted.

Changes

File(s) Change Summary
pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbc.scala Removed native count query methods (getCountSqlQuery, getCountForTableNatively, etc.) from this class.
pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbcBase.scala Added native count query utility methods, previously in TableReaderJdbc, to the base class.
pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbcNative.scala Refactored getRecordCount to use new base class methods; removed getSqlExpression helper.
pramen/core/src/main/scala/za/co/absa/pramen/core/utils/JdbcNativeUtils.scala Added executeQuery with retry logic for Hive JDBC driver errors; updated withResultSet to use it.
pramen/core/src/test/scala/za/co/absa/pramen/core/tests/reader/TableReaderJdbcNativeSuite.scala Removed test for deleted getSqlExpression method.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant TableReaderJdbcNative
    participant TableReaderJdbcBase
    participant JdbcNativeUtils

    Caller->>TableReaderJdbcNative: getRecordCount(query)
    alt Query is Table
        TableReaderJdbcNative->>TableReaderJdbcBase: getCountForTableNatively(...)
        TableReaderJdbcBase->>JdbcNativeUtils: withResultSet (uses executeQuery with retry)
    else Query is SQL (starts with select)
        TableReaderJdbcNative->>TableReaderJdbcBase: getCountForSql(...)
        TableReaderJdbcBase->>JdbcNativeUtils: withResultSet (uses executeQuery with retry)
    else Other SQL
        TableReaderJdbcNative->>TableReaderJdbcBase: getCountForTableNatively(...)
        TableReaderJdbcBase->>JdbcNativeUtils: withResultSet (uses executeQuery with retry)
    end
    JdbcNativeUtils-->>TableReaderJdbcBase: ResultSet with count
    TableReaderJdbcBase-->>TableReaderJdbcNative: count
    TableReaderJdbcNative-->>Caller: count
Loading

Assessment against linked issues

Objective Addressed Explanation
Add retry logic to protect against Hive JDBC driver odd behavior with count queries (#613)
Refactor/move native count query logic for maintainability (#613)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Possibly related PRs

  • AbsaOSS/pramen#606: Refactors and adds native count query methods in TableReaderJdbc, which directly relates to the movement and refactoring of similar logic in this PR.

Poem

In the warren of code, a bug did dwell,
Hive JDBC’s count would sometimes not gel.
With retries and moves, the rabbits did strive,
To keep all their queries robust and alive.
Now counting is safer—no more fright,
Thanks to the bunnies who code through the night!
🐇✨


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary or {PR Summary} to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
pramen/core/src/main/scala/za/co/absa/pramen/core/utils/JdbcNativeUtils.scala (2)

25-25: Prefer explicit imports over wildcard imports.

Wildcard imports can lead to naming conflicts and make it harder to track dependencies.

Replace the wildcard import with explicit imports:

-import java.sql._
+import java.sql.{Connection, DriverManager, ResultSet, SQLException, Statement}

134-146: Consider making the error pattern configurable.

The retry logic correctly handles the intermittent Hive JDBC driver issue. However, the hardcoded error message "Index: 1, Size: 1" is brittle and could break if the driver's error message changes.

Consider making the error pattern configurable or at least defining it as a constant:

private val HIVE_JDBC_RETRY_ERROR_PATTERN = "Index: 1, Size: 1"

// Then in the catch block:
case ex: SQLException if retriesLeft > 0 && ex.getMessage.contains(HIVE_JDBC_RETRY_ERROR_PATTERN) =>
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 15b80b0 and bf000fa.

📒 Files selected for processing (5)
  • pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbc.scala (1 hunks)
  • pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbcBase.scala (2 hunks)
  • pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbcNative.scala (1 hunks)
  • pramen/core/src/main/scala/za/co/absa/pramen/core/utils/JdbcNativeUtils.scala (3 hunks)
  • pramen/core/src/test/scala/za/co/absa/pramen/core/tests/reader/TableReaderJdbcNativeSuite.scala (0 hunks)
💤 Files with no reviewable changes (1)
  • pramen/core/src/test/scala/za/co/absa/pramen/core/tests/reader/TableReaderJdbcNativeSuite.scala
🧰 Additional context used
🧠 Learnings (3)
pramen/core/src/main/scala/za/co/absa/pramen/core/utils/JdbcNativeUtils.scala (1)
Learnt from: yruslan
PR: AbsaOSS/pramen#611
File: pramen/core/src/main/scala/za/co/absa/pramen/core/metastore/model/MetastoreDependencyFactory.scala:29-29
Timestamp: 2025-06-18T08:27:21.504Z
Learning: In pramen/core/src/main/scala/za/co/absa/pramen/core/metastore/model/MetastoreDependencyFactory.scala, the constant variable name was changed from DATE_UNTIL_EXPR_KEY to DATE_TO_EXPR_KEY, but both constants hold the same string value "date.to". This is a variable name refactoring, not a configuration key change, so it doesn't affect backward compatibility.
pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbc.scala (1)
Learnt from: yruslan
PR: AbsaOSS/pramen#611
File: pramen/core/src/main/scala/za/co/absa/pramen/core/metastore/model/MetastoreDependencyFactory.scala:29-29
Timestamp: 2025-06-18T08:27:21.504Z
Learning: In pramen/core/src/main/scala/za/co/absa/pramen/core/metastore/model/MetastoreDependencyFactory.scala, the constant variable name was changed from DATE_UNTIL_EXPR_KEY to DATE_TO_EXPR_KEY, but both constants hold the same string value "date.to". This is a variable name refactoring, not a configuration key change, so it doesn't affect backward compatibility.
pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbcBase.scala (1)
Learnt from: yruslan
PR: AbsaOSS/pramen#611
File: pramen/core/src/main/scala/za/co/absa/pramen/core/metastore/model/MetastoreDependencyFactory.scala:29-29
Timestamp: 2025-06-18T08:27:21.504Z
Learning: In pramen/core/src/main/scala/za/co/absa/pramen/core/metastore/model/MetastoreDependencyFactory.scala, the constant variable name was changed from DATE_UNTIL_EXPR_KEY to DATE_TO_EXPR_KEY, but both constants hold the same string value "date.to". This is a variable name refactoring, not a configuration key change, so it doesn't affect backward compatibility.
🧬 Code Graph Analysis (1)
pramen/core/src/main/scala/za/co/absa/pramen/core/utils/JdbcNativeUtils.scala (2)
pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbc.scala (7)
  • core (79-79)
  • core (81-103)
  • core (105-119)
  • core (121-137)
  • core (139-141)
  • core (143-209)
  • core (211-217)
pramen/core/src/main/scala/za/co/absa/pramen/core/utils/JdbcSparkUtils.scala (2)
  • core (208-214)
  • core (226-241)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Test Spark 3.3.4 on Scala 2.13.16
  • GitHub Check: Test Spark 3.5.5 on Scala 2.13.16
  • GitHub Check: Test Spark 3.4.4 on Scala 2.12.20
  • GitHub Check: Test Spark 3.5.5 on Scala 2.12.20
  • GitHub Check: Test Spark 2.4.8 on Scala 2.11.12
  • GitHub Check: Test Spark 3.3.4 on Scala 2.12.20
  • GitHub Check: Test Spark 3.4.4 on Scala 2.13.16
  • GitHub Check: Test Coverage on Scala 2.12.18
🔇 Additional comments (4)
pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbcNative.scala (1)

52-62: Well-structured query type handling.

The refactored pattern matching clearly separates the handling of different query types, improving code clarity and maintainability. The logging for non-SELECT SQL statements is particularly helpful for debugging.

pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbcBase.scala (1)

96-111: Robust count query execution with proper validation.

The implementation correctly validates the count query results and provides clear error messages. The use of JdbcNativeUtils.withResultSet ensures the count queries benefit from the new retry logic.

pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbc.scala (2)

25-25: Good cleanup of unused import.

The removal of JdbcConfig from the import statement is appropriate after moving the native count methods to the base class.


40-56: Let’s search the entire repo for those method definitions (and their visibility modifiers):

#!/bin/bash
# Search for getCountForTableNatively across all Scala files
rg -n 'def getCountForTableNatively' -g '*.scala'

# Search for getCountForSql across all Scala files
rg -n 'def getCountForSql' -g '*.scala'

@github-actions
Copy link
Copy Markdown

Unit Test Coverage

Overall Project 84.83% -0.12% 🍏
Files changed 50.25%

Module Coverage
pramen:core Jacoco Report 85.7% -0.13%
Files
Module File Coverage
pramen:core Jacoco Report TableReaderJdbcBase.scala 100% 🍏
TableReaderJdbc.scala 82.97% 🍏
TableReaderJdbcNative.scala 73.87% -7.66%
JdbcNativeUtils.scala 68.93% -10.51%

@yruslan yruslan requested a review from Copilot June 27, 2025 07:40
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the resilience of JDBC count queries by adding retry logic to handle intermittent Hive JDBC driver exceptions and refactors count query logic for improved clarity and maintainability.

  • Introduces retry logic in JdbcNativeUtils to handle specific SQLException messages.
  • Refactors count query handling in TableReaderJdbcNative and TableReaderJdbcBase.
  • Removes deprecated methods and tests no longer required.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pramen/core/src/test/scala/za/co/absa/pramen/core/tests/reader/TableReaderJdbcNativeSuite.scala Removed test for deprecated getSqlExpression.
pramen/core/src/main/scala/za/co/absa/pramen/core/utils/JdbcNativeUtils.scala Added executeQuery retry logic and refactored exception handling.
pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbcNative.scala Updated count query logic to incorporate new retry paths.
pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbcBase.scala Extracted count query logic and introduced helper methods for count queries.
pramen/core/src/main/scala/za/co/absa/pramen/core/reader/TableReaderJdbc.scala Removed redundant count query methods to delegate functionality to TableReaderJdbcBase.

@yruslan yruslan merged commit 1921198 into main Jun 27, 2025
9 checks passed
@yruslan yruslan deleted the feature/613-workaround-hive-issue branch June 27, 2025 07:45
@yruslan yruslan mentioned this pull request Jun 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Protect against Hive JDBC driver odd behavior with count queries.

2 participants