'use strict';

var Node = require('../node');

var TYPE = 'source';
var PARAMS = {
    query: Node.PARAM.STRING()
};

var Source = Node.create(TYPE, PARAMS, {
    beforeCreate: function(node) {
        // Last updated time in source node means data changed so it has to modify node.id
        node.setAttributeToModifyId('updatedAt');
        // Columns have to modify Node.id().
        // When a `select * from table` might end in a different set of columns we want to have a different node.
        // Current table triggers don't check DDL changes.
        node.setAttributeToModifyId('columns');
    }
});

module.exports = Source;

Source.prototype.sql = function() {
    return this.query;
};

/**
 * Source nodes are ready by definition
 *
 * @returns {Node.STATUS}
 */
Source.prototype.getStatus = function() {
    return Node.STATUS.READY;
};

var countTemplate = Node.template([
    'SELECT count(*) AS result_rows',
    'FROM ({{=it.sourceQuery}}) _cdb_analysis_source'
].join('\n'));

Source.prototype.computeRequirements = function(databaseService, limits, callback) {
    // for testig purposes we'll count actual rows, but we should use stats for efficiency
    var sql = countTemplate({
        sourceQuery: this.query
    });
    var self = this;
    databaseService.run(sql, function(err, resultSet){
      if (err) {
          return callback(err);
      }
      self.estimated_requirements = {
        result_rows: resultSet.rows[0].result_rows
      };
      self.limits = {
        result_rows: 1000000
      };
      return callback(null);
    });
};

@@ -851,17 +851,26 @@
 %7D;%0A%0Avar 
-c
+estimatedC
 ountTemp
@@ -894,309 +894,164 @@
 ate(
-%5B%0A    'SELECT count(*) AS result_rows',%0A    'FROM (%7B%7B=it.sourceQuery%7D%7D) _cdb_analysis_source'%0A%5D.join('%5Cn'));%0A%0ASource.prototype.computeRequirements = function(databaseService, limits, callback) %7B%0A    // for testig purposes we'll count actual rows, but we should use stats for efficiency%0A    var sql = c
+'EXPLAIN (FORMAT JSON) %7B%7B=it.sourceQuery%7D%7D');%0A%0ASource.prototype.computeRequirements = function(databaseService, limits, callback) %7B%0A    var sql = estimatedC
 ount
@@ -1314,20 +1314,43 @@
 s%5B0%5D
-.result_r
+%5B'QUERY PLAN'%5D%5B0%5D.Plan%5B'Plan R
 ows
+'%5D
 %0A   

Replace count by faster estimation of the number of rows