-
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 all 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; | ||
|
@@ -217,6 +238,15 @@ class ReadSql extends AdapterRead { | |
} else { | ||
query = query.offset(this.offsetCount); | ||
} | ||
|
||
if (this.sortBorderValue) { | ||
query = query.where( | ||
this.sortFields[0].field, | ||
this.sortFields[0].direction === 'desc' ? '<=' : '>=', | ||
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. How are we making sure we don't get duplicate points if there's a bunch of points with 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. So the way this works is that each result set consists of all values of the border value. For example, lets say 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 see, and that's in |
||
this.sortBorderValue | ||
); | ||
} | ||
|
||
return query; | ||
} | ||
|
||
|
@@ -335,6 +365,14 @@ 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); | ||
this.sortBorderValue = res.borderValue; | ||
res.readEnd = null; //read again | ||
return res; | ||
} | ||
|
||
//perform time-based pagination if timeField is indicated | ||
if (this.timeField) { | ||
let res = this.processPaginatedResults(points, this.timeField); | ||
|
@@ -404,6 +442,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; | ||
|
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.