-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Hive dialect for simple queries
- Loading branch information
1 parent
092234f
commit 30d4a30
Showing
5 changed files
with
110 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const R = require('ramda'); | ||
|
||
const BaseQuery = require('./BaseQuery'); | ||
const BaseFilter = require('./BaseFilter'); | ||
|
||
const GRANULARITY_TO_INTERVAL = { | ||
date: (date) => `DATE_FORMAT(${date}, 'yyyy-MM-dd 00:00:00.000')`, | ||
week: (date) => `DATE_FORMAT(from_unixtime(unix_timestamp('1900-01-01 00:00:00') + floor((unix_timestamp(${date}) - unix_timestamp('1900-01-01 00:00:00')) / (60 * 60 * 24 * 7)) * (60 * 60 * 24 * 7)), 'yyyy-MM-dd 00:00:00.000')`, | ||
hour: (date) => `DATE_FORMAT(${date}, 'yyyy-MM-dd HH:00:00.000')`, | ||
month: (date) => `DATE_FORMAT(${date}, 'yyyy-MM-01 00:00:00.000')`, | ||
year: (date) => `DATE_FORMAT(${date}, 'yyyy-01-01 00:00:00.000')` | ||
}; | ||
|
||
class HiveFilter extends BaseFilter { | ||
likeIgnoreCase(column, not) { | ||
return `${column}${not ? ' NOT' : ''} LIKE CONCAT('%', ?, '%')`; | ||
} | ||
} | ||
|
||
class HiveQuery extends BaseQuery { | ||
newFilter(filter) { | ||
return new HiveFilter(this, filter); | ||
} | ||
|
||
convertTz(field) { | ||
return `from_utc_timestamp(${field}, '${this.timezone}')`; | ||
} | ||
|
||
timeStampCast(value) { | ||
return `from_utc_timestamp(replace(replace(${value}, 'T', ' '), 'Z', ''), 'UTC')`; | ||
} | ||
|
||
dateTimeCast(value) { | ||
return `from_utc_timestamp(${value}, 'UTC')`; // TODO | ||
} | ||
|
||
// subtractInterval(date, interval) { | ||
// return `DATE_SUB(${date}, INTERVAL ${interval})`; // TODO | ||
// } | ||
|
||
// addInterval(date, interval) { | ||
// return `DATE_ADD(${date}, INTERVAL ${interval})`; // TODO | ||
// } | ||
|
||
timeGroupedColumn(granularity, dimension) { | ||
return GRANULARITY_TO_INTERVAL[granularity](dimension); | ||
} | ||
|
||
escapeColumnName(name) { | ||
return `\`${name}\``; | ||
} | ||
|
||
simpleQuery() { | ||
const ungrouped = this.evaluateSymbolSqlWithContext( | ||
() => `${this.commonQuery()} ${this.baseWhere(this.allFilters)}`, { | ||
ungroupedForWrappingGroupBy: true | ||
} | ||
); | ||
const select = this.evaluateSymbolSqlWithContext( | ||
() => this.dimensionsForSelect().map( | ||
d => d.aliasName() | ||
).concat(this.measures.map(m => m.selectColumns())).filter(s => !!s), { | ||
ungroupedAliases: R.fromPairs(this.forSelect().map(m => [m.measure || m.dimension, m.aliasName()])) | ||
} | ||
); | ||
return `SELECT ${select} FROM (${ungrouped}) AS ${this.escapeColumnName('hive_wrapper')} | ||
${this.groupByClause()}${this.baseHaving(this.measureFilters)}${this.orderBy()}${this.groupByDimensionLimit()}`; | ||
} | ||
|
||
seriesSql(timeDimension) { | ||
const values = timeDimension.timeSeries().map( | ||
([from, to]) => `select '${from}' f, '${to}' t` | ||
).join(' UNION ALL '); | ||
return `SELECT ${this.timeStampCast('dates.f')} date_from, ${this.timeStampCast('dates.t')} date_to FROM (${values}) AS dates`; | ||
} | ||
|
||
groupByClause() { | ||
const dimensionsForSelect = this.dimensionsForSelect(); | ||
const dimensionColumns = | ||
R.flatten(dimensionsForSelect.map( | ||
s => s.selectColumns() && s.aliasName() | ||
)).filter(s => !!s); | ||
return dimensionColumns.length ? ` GROUP BY ${dimensionColumns.join(', ')}` : ''; | ||
} | ||
|
||
getFieldIndex(id) { | ||
const dimension = this.dimensionsForSelect().find(d => d.dimension === id); | ||
if (dimension) { | ||
return super.getFieldIndex(id); | ||
} | ||
return this.escapeColumnName(this.aliasName(id)); | ||
} | ||
} | ||
|
||
module.exports = HiveQuery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters