Repository navigation
fix(postgres): improve normalizeDatetimeFunction for tstzrange data type - #12182
Conversation
commit: |
Review Summary by QodoFix normalizeDatetimeFunction to prevent false matches in tstzrange
WalkthroughsDescription• Refactored normalizeDatetimeFunction to use regex patterns for exact matching • Prevents false positives when datetime functions appear within larger strings • Added .trim() to handle whitespace in input values • Added test case validating tstzrange default value handling Diagramflowchart LR
A["normalizeDatetimeFunction<br/>with indexOf checks"] -->|"Replace with<br/>regex patterns"| B["Exact matching<br/>using /^pattern$/ regex"]
B -->|"Prevents false<br/>positives"| C["Correct handling<br/>of tstzrange defaults"]
D["New test case"] -->|"Validates"| C
File Changes1. src/driver/postgres/PostgresDriver.ts
|
Code Review by Qodo
1. funcName case breaks switch
|
7bf5d73 to
69d74a8
Compare
|
Persistent review updated to latest commit 69d74a8 |
69d74a8 to
2d8d6f6
Compare
|
Persistent review updated to latest commit 92fe1f9 |
92fe1f9 to
4c2978f
Compare
|
Persistent review updated to latest commit 4c2978f |
4c2978f to
c179d4e
Compare
|
|
Persistent review updated to latest commit c179d4e |
c179d4e to
2ccabcc
Compare
smith-xyz
left a comment
There was a problem hiding this comment.
Fix looks accurate to me.
Just a code style suggestion to help with readability.
curious if you could profile this function to see if the regex is slower versus the index and short circuit from before?
|
@smith-xyz Checkout the profiling result: import { performance } from "perf_hooks";
const samples = [
"CURRENT_TIMESTAMP",
"CURRENT_TIMESTAMP(3)",
"CURRENT_DATE",
"LOCALTIME(6)",
"random_string",
];
const ITERATIONS = 10_000_000;
for (let i = 0; i < 10; i++) {
console.log(`Run ${i + 1}:`);
const start = performance.now();
for (let i = 0; i < ITERATIONS; i++) {
regexVersion(samples[i % samples.length]);
}
console.log("regex:", performance.now() - start);
const start2 = performance.now();
for (let i = 0; i < ITERATIONS; i++) {
indexOfVersion(samples[i % samples.length]);
}
console.log("indexOf:", performance.now() - start2);
}
function indexOfVersion(value: string): string {
const upperCaseValue = value.toUpperCase()
const isDatetimeFunction =
upperCaseValue.indexOf("CURRENT_TIMESTAMP") !== -1 ||
upperCaseValue.indexOf("CURRENT_DATE") !== -1 ||
upperCaseValue.indexOf("CURRENT_TIME") !== -1 ||
upperCaseValue.indexOf("LOCALTIMESTAMP") !== -1 ||
upperCaseValue.indexOf("LOCALTIME") !== -1
if (isDatetimeFunction) {
// extract precision, e.g. "(3)"
const precision = value.match(/\(\d+\)/)
if (upperCaseValue.indexOf("CURRENT_TIMESTAMP") !== -1) {
return precision
? `('now'::text)::timestamp${precision[0]} with time zone`
: "now()"
} else if (upperCaseValue === "CURRENT_DATE") {
return "('now'::text)::date"
} else if (upperCaseValue.indexOf("CURRENT_TIME") !== -1) {
return precision
? `('now'::text)::time${precision[0]} with time zone`
: "('now'::text)::time with time zone"
} else if (upperCaseValue.indexOf("LOCALTIMESTAMP") !== -1) {
return precision
? `('now'::text)::timestamp${precision[0]} without time zone`
: "('now'::text)::timestamp without time zone"
} else if (upperCaseValue.indexOf("LOCALTIME") !== -1) {
return precision
? `('now'::text)::time${precision[0]} without time zone`
: "('now'::text)::time without time zone"
}
}
return value
}
function regexVersion(value: string): string {
const match = value.trim().toUpperCase().match(
/^(CURRENT_TIMESTAMP|CURRENT_DATE|CURRENT_TIME|LOCALTIMESTAMP|LOCALTIME)(\(\d+\))?$/
);
if (!match) return value;
const [, funcName, precision = ''] = match;
const prefix = "('now'::text)";
switch (funcName) {
case 'CURRENT_TIMESTAMP':
return precision ? `${prefix}::timestamp${precision} with time zone` : "now()";
case 'CURRENT_DATE':
return `${prefix}::date`;
case 'CURRENT_TIME':
return `${prefix}::time${precision} with time zone`;
case 'LOCALTIMESTAMP':
return `${prefix}::timestamp${precision} without time zone`;
case 'LOCALTIME':
return `${prefix}::time${precision} without time zone`;
default:
return value;
}
}Result: |
|
Nice! I noticed BTW, I think the reason why Another thing: the |
b9edaa1 to
ebc3fc2
Compare
- remove unused normalizeDatetimeFunction method from spanner driver - apply the regex logic in MySQL driver
ebc3fc2 to
57be3f6
Compare



Description of change
This PR fixes #11990
masterbranchFixes #11990tests/**.test.ts)docs/docs/**.md) (N/A)