-
Notifications
You must be signed in to change notification settings - Fork 2
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
optimize sort #55
optimize sort #55
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,14 +48,12 @@ class ReadSql extends AdapterRead { | |
this.addOptimizations(params.optimization_info); | ||
|
||
this.baseQuery = this.baseQuery.from(options.table); | ||
|
||
if (this.timeField && this.optimization_info.type !== 'reduce') { | ||
this.baseQuery = this.baseQuery.orderBy(this.timeField, this.tailOptimized ? 'desc' : 'asc'); | ||
} | ||
|
||
this.addSorting(); | ||
} | ||
|
||
periodicLiveRead() { | ||
return !!this.timeField; | ||
return !!this.timeField && !this.sortFields; | ||
} | ||
|
||
getDbConnection() { | ||
|
@@ -95,6 +93,20 @@ class ReadSql extends AdapterRead { | |
this.timeField = options.timeField || | ||
(options.from || options.to || options.last ? 'time' : undefined); | ||
} | ||
|
||
addSorting() { | ||
if (this.sortFields) { | ||
// adds multiple sort orders | ||
this.sortFields.forEach((sortObj) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would put this down by line 128 in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh. I thought having all the order by statements together would be easiest to read since this function determines sort order. No? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're the boss! In my head even though they both use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll think on it. |
||
this.baseQuery = this.baseQuery.orderBy(sortObj.field, sortObj.direction); | ||
}); | ||
return; | ||
} | ||
|
||
if (this.timeField && this.optimization_info.type !== 'reduce') { | ||
this.baseQuery = this.baseQuery.orderBy(this.timeField, this.tailOptimized ? 'desc' : 'asc'); | ||
} | ||
} | ||
|
||
addFilters(filter_ast) { | ||
let compiler = new FilterSQLCompiler({ baseQuery: this.baseQuery }); | ||
|
@@ -110,6 +122,15 @@ class ReadSql extends AdapterRead { | |
this.logger.debug(optimization_info.type + ' optimization, new max size: ', optimization_info.limit); | ||
this.maxSize = optimization_info.limit; | ||
this.tailOptimized = optimization_info.type === 'tail'; | ||
return; | ||
} | ||
if (optimization_info.type === 'sort') { | ||
this.logger.debug(optimization_info.type + ' optimization, params: ', optimization_info); | ||
this.sortFields = optimization_info.columns; | ||
if (optimization_info.limit) { | ||
this.maxSize = optimization_info.limit; | ||
} | ||
return; | ||
} | ||
if (optimization_info.type === 'reduce') { | ||
let groupby = optimization_info.groupby; | ||
|
@@ -335,6 +356,13 @@ class ReadSql extends AdapterRead { | |
readEnd: this.timeField ? to : new JuttleMoment(Infinity) | ||
}; | ||
} | ||
|
||
if (this.sortFields) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can SQL not paginate sorted data? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean using offset? Yes but using offset when data could be coming in in real time could cause errors in the pagination. this method takes real time data into account so IMO it's a superior pagination method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I don't understand what this is doing...if we read with a limit of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davidvgalbraith you're absolutely right - the pagination was broken and untested. Fixed now. Take another look? |
||
let res = this.processPaginatedResults(points, this.sortFields[0].field); | ||
res.readEnd = to; | ||
return res; | ||
} | ||
|
||
//perform time-based pagination if timeField is indicated | ||
if (this.timeField) { | ||
let res = this.processPaginatedResults(points, this.timeField); | ||
|
@@ -404,6 +432,12 @@ class ReadSql extends AdapterRead { | |
) { | ||
points = this.parseTime(points, { timeField: this.timeField }); | ||
} | ||
|
||
if (this.sortFields) { | ||
_.each(points, (p) => { | ||
delete p.time; | ||
}); | ||
} | ||
|
||
this.total_emitted_points += points.length; | ||
return points; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,14 +149,11 @@ var TestUtils = { | |
}, | ||
// What is performed here: | ||
// - round values | ||
massage: function(arr, shouldMassage) { | ||
if (!shouldMassage) { | ||
massage: function(arr, massageOptions) { | ||
if (!massageOptions) { | ||
return arr; | ||
} | ||
return _.chain(arr) | ||
.sortBy('level') | ||
.sortBy('code') | ||
.each(function(pt) { | ||
_.each(arr, function(pt) { | ||
var k, v; | ||
for (k in pt) { | ||
v = pt[k]; | ||
|
@@ -167,8 +164,15 @@ var TestUtils = { | |
pt[k] = Math.round(v * 10000) / 10000; | ||
} | ||
} | ||
}) | ||
.value(); | ||
}); | ||
|
||
if (massageOptions.sort) { | ||
massageOptions.sort.forEach(function(sortField) { | ||
arr = _.sortBy(sortField); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sets the value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, the test massaging was making all the testing using the sort massage inaccurate. Fixed now. |
||
}); | ||
} | ||
|
||
return arr; | ||
}, | ||
expectTimeSorted: function(result) { | ||
var time; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a little more canonical to say
if (graph.node_has_option(sort, 'groupby')) {
in cases like this.