can I set an alias for an aggregate field?

24 views
Skip to first unread message

john dimm

unread,
Jul 17, 2019, 1:22:03 AM7/17/19
to lovefield-users
  lf.fn.sum(myTable[column])

comes back as "SUM(myField)", which my code displays to the user.  It would be nice to set an alias but

  lf.fn.sum(this.fact[key]).as(key)

doesn't work.  

  Uncaught TypeError: lf.fn.sum(...).as is not a function

Is there a way?

Demetrios Papadopoulos

unread,
Jul 17, 2019, 12:47:27 PM7/17/19
to lovefield-users
This should in theory work. lf.fn.sum() returns an AggregatedColumn at [1], which supports as(), see [2]. There is also a test that uses lf.fn.count().as() at [3]. Are you sure that this.fact[key], returns a valid lovefield column?

If that does indeed not work, might be a bug, as aliasing aggregated columns is supported.

john dimm

unread,
Jul 17, 2019, 1:40:41 PM7/17/19
to lovefield-users
Since there is a test for count, I modified the quick start example to check.  

The following produces the error:  

  lovefield.min.js:28 Uncaught TypeError: lf.fn.count(...).as is not a function

Comment out the line with "as" and uncomment the one below it to remove the error and get the count of 1 in the console.

Maybe my lovefield.min.js is bad?  I tried the one from the codelab first, then the one in dist.  Same error.  The one from dist has 276 lines.


<!doctype html>

<html>

  <head>

    <meta charset="utf-8" />

    <title>Minimal example of using Lovefield</title>

    <script src="lib/lovefield.min.js"></script>

  </head>

  <body>

    <script>


var schemaBuilder = lf.schema.create('todo', 1);


schemaBuilder.createTable('Item').

    addColumn('id', lf.Type.INTEGER).

    addColumn('description', lf.Type.STRING).

    addColumn('deadline', lf.Type.DATE_TIME).

    addColumn('done', lf.Type.BOOLEAN).

    addPrimaryKey(['id']).

    addIndex('idxDeadline', ['deadline'], false, lf.Order.DESC);


var todoDb;

var item;

schemaBuilder.connect().then(function(db) {

  todoDb = db;

  item = db.getSchema().table('Item');

  var row = item.createRow({

    'id': 1,

    'description': 'Get a cup of coffee',

    'deadline': new Date(),

    'done': false

  });


  return db.insertOrReplace().into(item).values([row]).exec();

}).then(function() {

  return todoDb.select(

    lf.fn.count().as('count of rows')

    // lf.fn.count()

  ).from(item).where(item.done.eq(false)).exec();

}).then(function(results) {

  results.forEach(function(row) {

//    console.log(row['description'], 'before', row['deadline']);

    console.log(row['COUNT(*)']);

  });

});


    </script>

  </body>

</html>


Demetrios Papadopoulos

unread,
Jul 17, 2019, 1:43:33 PM7/17/19
to lovefield-users


On Wednesday, July 17, 2019 at 10:40:41 AM UTC-7, john dimm wrote:
Since there is a test for count, I modified the quick start example to check.  

The following produces the error:  

  lovefield.min.js:28 Uncaught TypeError: lf.fn.count(...).as is not a function

Comment out the line with "as" and uncomment the one below it to remove the error and get the count of 1 in the console.

Maybe my lovefield.min.js is bad?

Thanks for the repro example. Can you try the non-minified version of lovefield? It should be lovefield.js in the same dist/ folder. Does it exhibit the same problem?

john dimm

unread,
Jul 17, 2019, 2:41:52 PM7/17/19
to lovefield-users
Woo hoo!  That did it.  No problem when using lovefield.js instead of lovefield.min.js.

Demetrios Papadopoulos

unread,
Jul 17, 2019, 3:06:18 PM7/17/19
to lovefield-users


On Wednesday, July 17, 2019 at 11:41:52 AM UTC-7, john dimm wrote:
Woo hoo!  That did it.  No problem when using lovefield.js instead of lovefield.min.js.

Cool! If you could file a bug on our bug tracker (https://github.com/google/lovefield/issues) about this not working in the minified version, it would be helpful.
Thanks!

john dimm

unread,
Jul 17, 2019, 5:33:33 PM7/17/19
to lovefield-users
Done.


On Wednesday, July 17, 2019 at 10:40:41 AM UTC-7, john dimm wrote:

john dimm

unread,
Jul 20, 2019, 1:20:54 AM7/20/19
to lovefield-users
The problem was really that I was using lovefield.min.js from the stock_analysis codelab.  The current version works correctly, both normal and minified.


On Tuesday, July 16, 2019 at 10:22:03 PM UTC-7, john dimm wrote:
Reply all
Reply to author
Forward
0 new messages