Revision 3097
Added by Aaron Marcuse-Kubitza about 12 years ago
lib/sql.py | ||
---|---|---|
1228 | 1228 |
'''Creates a copy of a temp table which is distinct on the given columns. |
1229 | 1229 |
The table will get an index on these columns, facilitating merge joins. |
1230 | 1230 |
@param table Will be renamed to the distinct on table. |
1231 |
@param distinct_on If empty, creates a table with one row. This is useful if |
|
1232 |
your distinct_on columns are all literal values. |
|
1231 | 1233 |
@return The old table. |
1232 | 1234 |
''' |
1233 |
if distinct_on == []: return table # already distinct |
|
1234 |
|
|
1235 | 1235 |
old_table = copy.copy(table) |
1236 | 1236 |
table.name = sql_gen.concat(table.name, '_distinct') |
1237 | 1237 |
|
1238 | 1238 |
copy_table_struct(db, old_table, table) |
1239 |
add_index(db, distinct_on, table, unique=True) |
|
1240 |
insert_select(db, table, None, mk_select(db, old_table, start=0), |
|
1241 |
ignore=True) |
|
1239 |
|
|
1240 |
limit = None |
|
1241 |
if distinct_on == []: limit = 1 # one sample row |
|
1242 |
else: add_index(db, distinct_on, table, unique=True) |
|
1243 |
|
|
1244 |
insert_select(db, table, None, mk_select(db, old_table, start=0, |
|
1245 |
limit=limit), ignore=True) |
|
1242 | 1246 |
analyze(db, table) |
1243 | 1247 |
|
1244 | 1248 |
return old_table |
Also available in: Unified diff
sql.py: distinct_table(): Fixed bug where empty distinct_on cols needed to create a table with one sample row, instead of returning the original table, because this indicates that the full set of distinct_on columns are all literal values and should only occur once