Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filter datetime (sqlite / mysql) #8385

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 25 additions & 4 deletions packages/nocodb/src/db/conditionV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,12 +597,12 @@ const parseConditionV2 = async (
UITypes.LastModifiedTime,
].includes(column.uidt)
) {
let now = dayjs(new Date());
let now = dayjs(new Date()).utc();
const dateFormatFromMeta = column?.meta?.date_format;
if (dateFormatFromMeta && isDateMonthFormat(dateFormatFromMeta)) {
// reset to 1st
now = dayjs(now).date(1);
if (val) genVal = dayjs(val).date(1);
now = dayjs(now).utc().date(1);
if (val) genVal = dayjs(val).utc().date(1);
}
// handle sub operation
switch (filter.comparison_sub_op) {
Expand Down Expand Up @@ -668,6 +668,20 @@ const parseConditionV2 = async (
}

if (dayjs.isDayjs(genVal)) {
/**
* When you want to compare time, you need to choose whether to measure from the beginning or end of a day.
* example
* expression: dateTime is after today. value is 'YYYY-MM-DD 23:59:59'.
* expression: dateTime is before today. value is 'YYYY-MM-DD 00:00:00'.
*/
if (['gt', 'ge', 'gte'].includes(filter.comparison_op)) {
genVal = genVal.endOf('d');
}
if (
['eq', 'neq', 'lt', 'lt', 'lte'].includes(filter.comparison_op)
) {
genVal = genVal.startOf('d');
}
// turn `val` in dayjs object format to string
genVal = genVal.format(dateFormat).toString();
// keep YYYY-MM-DD only for date
Expand Down Expand Up @@ -713,6 +727,11 @@ const parseConditionV2 = async (
(column.uidt === UITypes.Formula &&
getEquivalentUIType({ formulaColumn: column }) ==
UITypes.DateTime) ||
[
UITypes.DateTime,
UITypes.CreatedTime,
UITypes.LastModifiedTime,
].includes(column.uidt) ||
column.ct === 'timestamp' ||
column.ct === 'date' ||
column.ct === 'datetime'
Expand Down Expand Up @@ -740,7 +759,9 @@ const parseConditionV2 = async (
// else
qb = qb.where(knex.raw('??::date = ?', [field, val]));
} else {
qb = qb.where(knex.raw('DATE(??) = DATE(?)', [field, val]));
qb = qb.where(
knex.raw("DATE(??, 'localtime') = DATE(?)", [field, val]),
Copy link
Member

Choose a reason for hiding this comment

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

Does all databases(sqlite, mysql and mssql) take localtime as second argument ?

Copy link
Author

Choose a reason for hiding this comment

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

MySQL was excluded from the beginning. Here, I am only testing SQLite and have not considered MSSQL. I can modify this through conditional statements.

Copy link
Member

Choose a reason for hiding this comment

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

You can introduce an if statement to avoid any unexpected behaviour

Copy link
Author

@cangku cangku May 22, 2024

Choose a reason for hiding this comment

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

this only fixes the issue. I feel this part can be abstracted and optimized further.

);
}
} else {
qb = qb.where(field, val);
Expand Down