Skip to content

SQL Server to Postgres Migration#7547

Open
labkey-martyp wants to merge 3 commits intorelease26.3-SNAPSHOTfrom
26.3_fb_tnprc_migration
Open

SQL Server to Postgres Migration#7547
labkey-martyp wants to merge 3 commits intorelease26.3-SNAPSHOTfrom
26.3_fb_tnprc_migration

Conversation

@labkey-martyp
Copy link
Copy Markdown
Contributor

@labkey-martyp labkey-martyp commented Apr 3, 2026

Rationale

Two functions were added as part of the SQL Server to Postgres migration.

  1. timestampdiff2: This is a work around for the parameter swapping issue in the PG JDBC timestampdiff function. This implementation does not use the pass through JDBC timestampdiff function. This may be a temporary function depending on PG JDBC group fixing the underlying issue with parameter swapping.
  2. age_in_days: This is to preserve the timestampdiff(SQL_TSI_DAY_0, param1, param2) SQL Server behavior. By default timestampdiff with frequency of days/months/years on SQL Server will count calendar boundaries. On Postgres, timestampdiff counts actual time between parameters, so a day is 24 hours. This will be a permanent addition to LabKey SQL.

Related Pull Requests

Changes

  • timestampdiff2 function added with tests
  • age_in_days method added and SQL_TSI_DAY added as option in age() method

Copy link
Copy Markdown
Contributor

@labkey-adam labkey-adam left a comment

Choose a reason for hiding this comment

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

Please review suggestions from me and Claude. I also suggest running Claude /review_pr after changes. If its findings are off, consider adding comments that address.

new MethodSqlTest("SELECT TIMESTAMPDIFF2(SQL_TSI_YEAR, CAST('15 Jan 2003' AS TIMESTAMP), CAST('15 Jan 2006' AS TIMESTAMP))", JdbcType.INTEGER, 3),
new MethodSqlTest("SELECT TIMESTAMPDIFF2(SQL_TSI_YEAR, CAST('14 Jan 2006' AS TIMESTAMP), CAST('15 Jan 2003' AS TIMESTAMP))", JdbcType.INTEGER, -2),
new MethodSqlTest("SELECT TIMESTAMPDIFF2(SQL_TSI_FRAC_SECOND, CAST('01 Jan 2004 5:00:00' AS TIMESTAMP), CAST('01 Jan 2004 5:00:01' AS TIMESTAMP))", JdbcType.BIGINT, 1000),
new MethodSqlTest("SELECT TIMESTAMPDIFF2(SQL_TSI_FRAC_SECOND, CAST('01 Jan 2004' AS TIMESTAMP), CAST('31 Jan 2004' AS TIMESTAMP))", JdbcType.BIGINT, 2592000000L),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add test for age_in_days()?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And new age() option?

case "SQL_TSI_SECOND" ->
new SQLFragment("TRUNC(EXTRACT(EPOCH FROM (").append(end).append(") - (").append(start).append(")))::INT");
case "SQL_TSI_FRAC_SECOND" ->
new SQLFragment("TRUNC(EXTRACT(EPOCH FROM (").append(end).append(") - (").append(start).append(")) * 1000)::BIGINT");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Claude is complaining about this BIGINT vs. Method defining timestampdiff2 as returning JdbcType.INTEGER.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In limited local testing, I didn't see any problem with very large values, though.

}

private SQLFragment getWholeElapsedMonths(SQLFragment start, SQLFragment end)
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This method assumes that start and end don't include any parameters. Current callers don't, but best to add a comment and an invariant check for that, i.e., throw if either has parameters.

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.

2 participants