Fix simple tabley sampling
This commit is contained in:
@@ -89,9 +89,9 @@ module.exports.getQueryTopCategories = function(query, column, topN, includeNull
|
||||
};
|
||||
|
||||
module.exports.getQuerySample = function(query, sampleProb, randomSeed = 0.5) {
|
||||
const table = simpleQueryTable(query);
|
||||
if (table) {
|
||||
return getTableSample(table, sampleProb, randomSeed);
|
||||
const singleTable = simpleQueryTable(query);
|
||||
if (singleTable) {
|
||||
return getTableSample(singleTable.table, singleTable.columns, sampleProb, randomSeed);
|
||||
}
|
||||
return `
|
||||
WITH __cdb_rndseed AS (
|
||||
@@ -103,17 +103,17 @@ module.exports.getQuerySample = function(query, sampleProb, randomSeed = 0.5) {
|
||||
`;
|
||||
};
|
||||
|
||||
function getTableSample(table, sampleProb, randomSeed) {
|
||||
function getTableSample(table, columns, sampleProb, randomSeed) {
|
||||
sampleProb *= 100;
|
||||
randomSeed *= Math.pow(2, 31) -1;
|
||||
return `
|
||||
SELECT * FROM ${table} TABLESAMPLE BERNOULLI (${sampleProb}) REPEATABLE (${randomSeed})
|
||||
SELECT ${columns} FROM ${table} TABLESAMPLE BERNOULLI (${sampleProb}) REPEATABLE (${randomSeed})
|
||||
`;
|
||||
}
|
||||
|
||||
function simpleQueryTable(sql) {
|
||||
const basicQuery =
|
||||
/\s*SELECT\s+[\*a-z0-9_,\s]+?\s+FROM\s+((\"[^"]+\"|[a-z0-9_]+)\.)?(\"[^"]+\"|[a-z0-9_]+)\s*;?\s*/i;
|
||||
/\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(
|
||||
@@ -127,9 +127,10 @@ function simpleQueryTable(sql) {
|
||||
match = sql.match(wrappedQuery);
|
||||
}
|
||||
if (match) {
|
||||
const schema = match[2];
|
||||
const table = match[3];
|
||||
return schema ? `${schema}.${table}` : table;
|
||||
const columns = match[1];
|
||||
const schema = match[3];
|
||||
const table = match[4];
|
||||
return { table: schema ? `${schema}.${table}` : table, columns };
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user