diff --git a/lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js b/lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js index 1578dded..a663632f 100644 --- a/lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js +++ b/lib/cartodb/models/mapconfig/adapter/aggregation-mapconfig-adapter.js @@ -118,7 +118,9 @@ module.exports = class AggregationMapConfigAdapter { return callback(null, shouldAdapt); } - const aggregationMetadata = queryUtils.getAggregationMetadata({ query: layer.options.sql }); + const aggregationMetadata = queryUtils.getAggregationMetadata({ + query: layer.options.sql_raw ? layer.options.sql_raw : layer.options.sql + }); connection.query(aggregationMetadata, (err, res) => { if (err) { diff --git a/test/acceptance/aggregation.js b/test/acceptance/aggregation.js index 56815ccc..04772f25 100644 --- a/test/acceptance/aggregation.js +++ b/test/acceptance/aggregation.js @@ -49,6 +49,23 @@ describe('aggregation', function () { from generate_series(-3, 3) x `; + const SQL_WRAP = ` + WITH hgrid AS ( + SELECT + CDB_RectangleGrid ( + ST_Expand(!bbox!, CDB_XYZ_Resolution(1) * 12), + CDB_XYZ_Resolution(1) * 12, + CDB_XYZ_Resolution(1) * 12 + ) as cell + ) + SELECT + hgrid.cell as the_geom_webmercator, + count(1) as agg_value, + count(1) /power( 12 * CDB_XYZ_Resolution(1), 2 ) as agg_value_density, + row_number() over () as cartodb_id + FROM hgrid, (<%= sql %>) i + WHERE ST_Intersects(i.the_geom_webmercator, hgrid.cell) GROUP BY hgrid.cell + `; function createVectorMapConfig (layers = [ { @@ -435,6 +452,36 @@ describe('aggregation', function () { done(); }); }); + + it('when sql_wrap is provided should return a layergroup', function (done) { + this.mapConfig = createVectorMapConfig([ + { + type: 'cartodb', + options: { + sql_wrap: SQL_WRAP, + sql: POINTS_SQL_1, + aggregation: { + threshold: 1 + } + } + } + ]); + this.testClient = new TestClient(this.mapConfig); + + this.testClient.getLayergroup((err, body) => { + if (err) { + return done(err); + } + + assert.equal(typeof body.metadata, 'object'); + assert.ok(Array.isArray(body.metadata.layers)); + + body.metadata.layers.forEach(layer => assert.ok(layer.meta.aggregation.mvt)); + body.metadata.layers.forEach(layer => assert.ok(!layer.meta.aggregation.png)); + + done(); + }); + }); }); }); }); diff --git a/test/support/prepare_db.sh b/test/support/prepare_db.sh index b31b2c9b..03d97fc7 100755 --- a/test/support/prepare_db.sh +++ b/test/support/prepare_db.sh @@ -77,7 +77,7 @@ if test x"$PREPARE_PGSQL" = xyes; then createdb -Ttemplate_postgis -EUTF8 "${TEST_DB}" || die "Could not create test database" LOCAL_SQL_SCRIPTS='analysis_catalog windshaft.test gadm4 ported/populated_places_simple_reduced cdb_analysis_check cdb_invalidate_varnish' - REMOTE_SQL_SCRIPTS='CDB_QueryStatements CDB_QueryTables CDB_CartodbfyTable CDB_TableMetadata CDB_ForeignTable CDB_UserTables CDB_ColumnNames CDB_ZoomFromScale CDB_OverviewsSupport CDB_Overviews CDB_QuantileBins CDB_JenksBins CDB_HeadsTailsBins CDB_EqualIntervalBins CDB_Hexagon CDB_XYZ CDB_EstimateRowCount' + REMOTE_SQL_SCRIPTS='CDB_QueryStatements CDB_QueryTables CDB_CartodbfyTable CDB_TableMetadata CDB_ForeignTable CDB_UserTables CDB_ColumnNames CDB_ZoomFromScale CDB_OverviewsSupport CDB_Overviews CDB_QuantileBins CDB_JenksBins CDB_HeadsTailsBins CDB_EqualIntervalBins CDB_Hexagon CDB_XYZ CDB_EstimateRowCount CDB_RectangleGrid' CURL_ARGS="" for i in ${REMOTE_SQL_SCRIPTS} @@ -99,7 +99,7 @@ if test x"$PREPARE_PGSQL" = xyes; then sed -e 's/PARALLEL \= [A-Z]*,/''/g' \ -e 's/PARALLEL [A-Z]*/''/g' sql/$i.sql > $TMPFILE mv $TMPFILE sql/$i.sql - fi + fi cat sql/${i}.sql | sed -e 's/cartodb\./public./g' -e "s/''cartodb''/''public''/g" | sed "s/:PUBLICUSER/${PUBLICUSER}/" |