plywood
Version:
A query planner and executor
138 lines (137 loc) • 6.69 kB
JavaScript
import { __extends } from "tslib";
import { SQLDialect } from './baseDialect';
var PostgresDialect = (function (_super) {
__extends(PostgresDialect, _super);
function PostgresDialect() {
return _super.call(this) || this;
}
PostgresDialect.prototype.emptyGroupBy = function () {
return "GROUP BY ''=''";
};
PostgresDialect.prototype.timeToSQL = function (date) {
if (!date)
return this.nullConstant();
return "TIMESTAMP '".concat(this.dateToSQLDateString(date), "'");
};
PostgresDialect.prototype.stringArrayToSQL = function (_value) {
throw new Error('must implement');
};
PostgresDialect.prototype.concatExpression = function (a, b) {
return "(".concat(a, "||").concat(b, ")");
};
PostgresDialect.prototype.containsExpression = function (a, b, insensitive) {
if (insensitive) {
a = "LOWER(".concat(a, ")");
b = "LOWER(".concat(b, ")");
}
return "POSITION(".concat(b, " IN ").concat(a, ")>0");
};
PostgresDialect.prototype.regexpExpression = function (expression, regexp) {
return "(".concat(expression, " ~ '").concat(regexp, "')");
};
PostgresDialect.prototype.castExpression = function (inputType, operand, targetType) {
var castFunction = PostgresDialect.CAST_TO_FUNCTION[targetType][inputType];
if (!castFunction) {
throw new Error("unsupported cast from ".concat(inputType, " to ").concat(targetType, " in Postgres dialect"));
}
return castFunction.replace(/\$\$/g, operand);
};
PostgresDialect.prototype.utcToWalltime = function (operand, timezone) {
if (timezone.isUTC())
return operand;
return "(".concat(operand, " AT TIME ZONE 'UTC' AT TIME ZONE '").concat(timezone, "')");
};
PostgresDialect.prototype.walltimeToUTC = function (operand, timezone) {
if (timezone.isUTC())
return operand;
return "(".concat(operand, " AT TIME ZONE '").concat(timezone, "' AT TIME ZONE 'UTC')");
};
PostgresDialect.prototype.timeFloorExpression = function (operand, duration, timezone) {
var bucketFormat = PostgresDialect.TIME_BUCKETING[duration.toString()];
if (!bucketFormat)
throw new Error("unsupported duration '".concat(duration, "'"));
return this.walltimeToUTC("DATE_TRUNC('".concat(bucketFormat, "',").concat(this.utcToWalltime(operand, timezone), ")"), timezone);
};
PostgresDialect.prototype.timeBucketExpression = function (operand, duration, timezone) {
return this.timeFloorExpression(operand, duration, timezone);
};
PostgresDialect.prototype.timePartExpression = function (operand, part, timezone) {
var timePartFunction = PostgresDialect.TIME_PART_TO_FUNCTION[part];
if (!timePartFunction)
throw new Error("unsupported part ".concat(part, " in Postgres dialect"));
return timePartFunction.replace(/\$\$/g, this.utcToWalltime(operand, timezone));
};
PostgresDialect.prototype.timeShiftExpression = function (operand, duration, step, _timezone) {
if (step === 0)
return operand;
var sqlFn = step > 0 ? 'DATE_ADD(' : 'DATE_SUB(';
var spans = duration.multiply(Math.abs(step)).valueOf();
if (spans.week) {
return sqlFn + operand + ', INTERVAL ' + String(spans.week) + ' WEEK)';
}
if (spans.year || spans.month) {
var expr = String(spans.year || 0) + '-' + String(spans.month || 0);
operand = sqlFn + operand + ", INTERVAL '" + expr + "' YEAR_MONTH)";
}
if (spans.day || spans.hour || spans.minute || spans.second) {
var expr = String(spans.day || 0) +
' ' +
[spans.hour || 0, spans.minute || 0, spans.second || 0].join(':');
operand = sqlFn + operand + ", INTERVAL '" + expr + "' DAY_SECOND)";
}
return operand;
};
PostgresDialect.prototype.extractExpression = function (operand, regexp) {
return "(SELECT (REGEXP_MATCHES(".concat(operand, ", '").concat(regexp, "'))[1])");
};
PostgresDialect.prototype.indexOfExpression = function (str, substr) {
return "POSITION(".concat(substr, " IN ").concat(str, ") - 1");
};
PostgresDialect.TIME_BUCKETING = {
PT1S: 'second',
PT1M: 'minute',
PT1H: 'hour',
P1D: 'day',
P1W: 'week',
P1M: 'month',
P3M: 'quarter',
P1Y: 'year',
};
PostgresDialect.TIME_PART_TO_FUNCTION = {
SECOND_OF_MINUTE: "DATE_PART('second',$$)",
SECOND_OF_HOUR: "(DATE_PART('minute',$$)*60+DATE_PART('second',$$))",
SECOND_OF_DAY: "((DATE_PART('hour',$$)*60+DATE_PART('minute',$$))*60+DATE_PART('second',$$))",
SECOND_OF_WEEK: "((((CAST((DATE_PART('dow',$$)+6) AS int)%7)*24)+DATE_PART('hour',$$)*60+DATE_PART('minute',$$))*60+DATE_PART('second',$$))",
SECOND_OF_MONTH: "((((DATE_PART('day',$$)-1)*24)+DATE_PART('hour',$$)*60+DATE_PART('minute',$$))*60+DATE_PART('second',$$))",
SECOND_OF_YEAR: "((((DATE_PART('doy',$$)-1)*24)+DATE_PART('hour',$$)*60+DATE_PART('minute',$$))*60+DATE_PART('second',$$))",
MINUTE_OF_HOUR: "DATE_PART('minute',$$)",
MINUTE_OF_DAY: "DATE_PART('hour',$$)*60+DATE_PART('minute',$$)",
MINUTE_OF_WEEK: "((CAST((DATE_PART('dow',$$)+6) AS int)%7)*24)+DATE_PART('hour',$$)*60+DATE_PART('minute',$$)",
MINUTE_OF_MONTH: "((DATE_PART('day',$$)-1)*24)+DATE_PART('hour',$$)*60+DATE_PART('minute',$$)",
MINUTE_OF_YEAR: "((DATE_PART('doy',$$)-1)*24)+DATE_PART('hour',$$)*60+DATE_PART('minute',$$)",
HOUR_OF_DAY: "DATE_PART('hour',$$)",
HOUR_OF_WEEK: "((CAST((DATE_PART('dow',$$)+6) AS int)%7)*24+DATE_PART('hour',$$))",
HOUR_OF_MONTH: "((DATE_PART('day',$$)-1)*24+DATE_PART('hour',$$))",
HOUR_OF_YEAR: "((DATE_PART('doy',$$)-1)*24+DATE_PART('hour',$$))",
DAY_OF_WEEK: "(CAST((DATE_PART('dow',$$)+6) AS int)%7)+1",
DAY_OF_MONTH: "DATE_PART('day',$$)",
DAY_OF_YEAR: "DATE_PART('doy',$$)",
WEEK_OF_YEAR: "DATE_PART('week',$$)",
MONTH_OF_YEAR: "DATE_PART('month',$$)",
YEAR: "DATE_PART('year',$$)",
};
PostgresDialect.CAST_TO_FUNCTION = {
TIME: {
NUMBER: 'TO_TIMESTAMP($$::double precision / 1000)',
},
NUMBER: {
TIME: 'EXTRACT(EPOCH FROM $$) * 1000',
STRING: '$$::float',
},
STRING: {
NUMBER: '$$::text',
},
};
return PostgresDialect;
}(SQLDialect));
export { PostgresDialect };