diff --git a/lib/cartodb/utils/query-utils.js b/lib/cartodb/utils/query-utils.js index e288cb32..8ef03c34 100644 --- a/lib/cartodb/utils/query-utils.js +++ b/lib/cartodb/utils/query-utils.js @@ -98,58 +98,6 @@ function columnSelector(columns) { throw new TypeError(`Bad argument type for columns: ${typeof columns}`); } -module.exports.getQuerySample = function (query, sampleProb, limit = null, randomSeed = 0.5, columns = null) { - const singleTable = simpleQueryTable(query); - if (singleTable) { - return getTableSample(singleTable.table, columns || singleTable.columns, sampleProb, limit, randomSeed); - } - const limitClause = limit ? `LIMIT ${limit}` : ''; - return ` - WITH __cdb_rndseed AS ( - SELECT setseed(${randomSeed}) - ) - SELECT ${columnSelector(columns)} - FROM (${substituteDummyTokens(query)}) AS __cdb_query - WHERE random() < ${sampleProb} - ${limitClause} - `; -}; - -function getTableSample(table, columns, sampleProb, limit = null, randomSeed = 0.5) { - const limitClause = limit ? `LIMIT ${limit}` : ''; - sampleProb *= 100; - randomSeed *= Math.pow(2, 31) - 1; - return ` - SELECT ${columnSelector(columns)} - FROM ${table} - TABLESAMPLE BERNOULLI (${sampleProb}) REPEATABLE (${randomSeed}) ${limitClause} - `; -} - -function simpleQueryTable(sql) { - const basicQuery = - /\s*SELECT\s+([\*a-z0-9_,\s]+?)\s+FROM\s+((\"[^"]+\"|[a-z0-9_]+)\.)?(\"[^"]+\"|[a-z0-9_]+)\s*;?\s*/i; - const unwrappedQuery = new RegExp('^' + basicQuery.source + '$', 'i'); - // queries for named maps are wrapped like this: - var wrappedQuery = new RegExp( - '^\\s*SELECT\\s+\\*\\s+FROM\\s+\\(' + - basicQuery.source + - '\\)\\s+AS\\s+wrapped_query\\s+WHERE\\s+\\d+=1\\s*$', - 'i' - ); - let match = sql.match(unwrappedQuery); - if (!match) { - match = sql.match(wrappedQuery); - } - if (match) { - const columns = match[1]; - const schema = match[3]; - const table = match[4]; - return { table: schema ? `${schema}.${table}` : table, columns }; - } - return false; -} - module.exports.getMaxMinSpanColumnQuery = function (query, column = 'cartodb_id') { return ` SELECT