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

[FE-9021][WIP][DONOTMERGE] fixes tz stuff #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions dist/mapd-crossfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17094,11 +17094,17 @@ var TYPES = {
var TOSTRING = Object.prototype.toString;

function type(o) {
if (_moment2.default.isMoment(o)) {
return "date";
}
return TYPES[typeof o === "undefined" ? "undefined" : _typeof(o)] || TYPES[TOSTRING.call(o)] || (o ? "object" : "null");
}

var SQL_DATE_FORMAT = "YYYY-MM-DD HH:mm:ss.SSS";

function formatFilterValue(value, wrapInQuotes, isExact) {
var valueType = type(value);

if (valueType == "string") {
var escapedValue = value.replace(/'/g, "''");

Expand All @@ -17109,17 +17115,28 @@ function formatFilterValue(value, wrapInQuotes, isExact) {

return wrapInQuotes ? "'" + escapedValue + "'" : escapedValue;
} else if (valueType == "date") {
return "TIMESTAMP(3) '" + value.toISOString().slice(0, -1).replace("T", " ") + "'";

if (!_moment2.default.isMoment(value)) {
value = (0, _moment2.default)(value);
}

return "TIMESTAMP(3) '" + (0, _moment2.default)(value).local().format(SQL_DATE_FORMAT) + "'";
} else {
return value;
}
}

function formatDateRangeLowerBound(value) {
return "TIMESTAMP(9) '" + value.toISOString().slice(0, -1).replace("T", " ") + "000000'";
if (!_moment2.default.isMoment(value)) {
value = (0, _moment2.default)(value);
}
return "TIMESTAMP(9) '" + (0, _moment2.default)(value).local().format(SQL_DATE_FORMAT) + "000000'";
}
function formatDateRangeUpperBound(value) {
return "TIMESTAMP(9) '" + value.toISOString().slice(0, -1).replace("T", " ") + "999999'";
if (!_moment2.default.isMoment(value)) {
value = (0, _moment2.default)(value);
}
return "TIMESTAMP(9) '" + (0, _moment2.default)(value).local().format(SQL_DATE_FORMAT) + "999999'";
}

function pruneCache(allCacheResults) {
Expand Down Expand Up @@ -17149,20 +17166,21 @@ function isRelative(sqlStr) {
function replaceRelative(sqlStr) {
var relativeDateRegex = /DATE_ADD\(([^,|.]+), (DATEDIFF\(\w+, ?\d+, ?\w+\(\)\)[-+0-9]*|[-0-9]+), ([0-9]+|NOW\(\))\)/g;
var currMoment = (0, _moment2.default)();
var now = currMoment.utc();

var withRelative = sqlStr.replace(relativeDateRegex, function (match, datepart, number, date) {
if (isNaN(number)) {
var num = Number(number.slice(number.lastIndexOf(")") + 1));
if (isNaN(num)) {
return formatFilterValue(now.startOf(datepart).toDate(), true);
return formatFilterValue(currMoment.clone().local().startOf(datepart), true);
} else {
return formatFilterValue(currMoment.add(num, datepart).utc().startOf(datepart).toDate(), true);
return formatFilterValue(currMoment.clone().local().add(num, datepart).startOf(datepart), true);
}
} else {
return formatFilterValue(currMoment.add(number, datepart).toDate(), true);
return formatFilterValue(currMoment.clone().local().add(number, datepart), true);
}
});
var withNow = withRelative.replace(/NOW\(\)/g, formatFilterValue(currMoment.toDate(), true));
var withNow = withRelative.replace(/NOW\(\)/g, formatFilterValue(currMoment, true));

return withNow;
}

Expand Down
53 changes: 33 additions & 20 deletions src/mapd-crossfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,18 @@ var TYPES = {
"[object Error]": "error"
}

var TOSTRING = Object.prototype.toString
const TOSTRING = Object.prototype.toString

function type(o) {
if (moment.isMoment(o)) { return "date"}
return TYPES[typeof o] || TYPES[TOSTRING.call(o)] || (o ? "object" : "null")
}

const SQL_DATE_FORMAT = "YYYY-MM-DD HH:mm:ss.SSS"

function formatFilterValue(value, wrapInQuotes, isExact) {
var valueType = type(value)

if (valueType == "string") {
var escapedValue = value.replace(/'/g, "''")

Expand All @@ -222,12 +226,16 @@ function formatFilterValue(value, wrapInQuotes, isExact) {

return wrapInQuotes ? "'" + escapedValue + "'" : escapedValue
} else if (valueType == "date") {

if (!moment.isMoment(value)) {
value = moment(value)
}

return (
"TIMESTAMP(3) '" +
value
.toISOString()
.slice(0, -1)
.replace("T", " ") +
moment(value)
.local()
.format(SQL_DATE_FORMAT) +
"'"
)
} else {
Expand All @@ -236,22 +244,26 @@ function formatFilterValue(value, wrapInQuotes, isExact) {
}

function formatDateRangeLowerBound(value) {
if (!moment.isMoment(value)) {
value = moment(value)
}
return (
"TIMESTAMP(9) '" +
value
.toISOString()
.slice(0, -1)
.replace("T", " ") +
moment(value)
.local()
.format(SQL_DATE_FORMAT) +
"000000'"
)
}
function formatDateRangeUpperBound(value) {
if (!moment.isMoment(value)) {
value = moment(value)
}
return (
"TIMESTAMP(9) '" +
value
.toISOString()
.slice(0, -1)
.replace("T", " ") +
moment(value)
.local()
.format(SQL_DATE_FORMAT) +
"999999'"
)
}
Expand Down Expand Up @@ -284,36 +296,37 @@ function isRelative(sqlStr) {
export function replaceRelative(sqlStr) {
const relativeDateRegex = /DATE_ADD\(([^,|.]+), (DATEDIFF\(\w+, ?\d+, ?\w+\(\)\)[-+0-9]*|[-0-9]+), ([0-9]+|NOW\(\))\)/g
const currMoment = moment()
const now = currMoment.utc()

const withRelative = sqlStr.replace(
relativeDateRegex,
(match, datepart, number, date) => {
if (isNaN(number)) {
const num = Number(number.slice(number.lastIndexOf(")") + 1))
if (isNaN(num)) {
return formatFilterValue(now.startOf(datepart).toDate(), true)
return formatFilterValue(currMoment.clone().local().startOf(datepart), true)
} else {
return formatFilterValue(
currMoment
.clone()
.local()
.add(num, datepart)
.utc()
.startOf(datepart)
.toDate(),
.startOf(datepart),
true
)
}
} else {
return formatFilterValue(
currMoment.add(number, datepart).toDate(),
currMoment.clone().local().add(number, datepart),
true
)
}
}
)
const withNow = withRelative.replace(
/NOW\(\)/g,
formatFilterValue(currMoment.toDate(), true)
formatFilterValue(currMoment, true)
)

return withNow
}

Expand Down