c8e1dc987ba5532c65be54d8f43af1ec4337fb10
angie
  Sat Nov 21 09:52:07 2015 -0800
Back-end implementation of Data Integrator's support for related tables and
fields using all.joiner.  Most joins are implemented using a new module,
hashJoin.c; but SQL joins are used in certain cases when hash joins are
impractical and SQL joins are actually faster.  A new module joinMixer
determines which joins should be implemented by hashJoin vs SQL, and computes
row indices for hashJoin objects to find keys (from SQL or other hashJoins)
and store results.  The SQL join info from joinMixer is translated into SQL
queries in annoStreamDb.  annoStreamDb also generates its own autoSql asObject,
adding the fields from related tables after the fields of the main track table.

Main changes:

- annoStreamDb.c
- main table SQL query now uses <table>.<field> instead of just <field> to
avoid clashes with same field name in different tables
- SQL joins return multiple rows for a single main table row when there are
multiple matching rows in a related table; these rows need to be squashed
into one row with the multiple matches comma-separated, both to match
hgTables behavior and to avoid overflow of rowBuf.  (glomSqlDup)
- as mentioned above, generate joining SQL queries when necessary and
generate own asObj including selected fields from related tables.
- parse JSON config object with relatedTables spec from UI via hgi_querySpec

hashJoin basically slurps a related table into a big hash of keys to values,
perform lookups (possibly of multiple keys), and formats each column's results.
It includes a lot of tweaks to match hgTables/joining.c output char-for-char:
collapse adjacent duplicate matches, commas at end of matches from multiple
key lookups, reversed order of multiple match values.  hgTables/joining.c uses
arrays of slNames, but in order to avoid all that allocation I'm just glomming
into an array of reused dyStrings.

joinMixer takes a list of fields to include in output, gets a list of joins
to be performed (from joinerRouteThroughAll), applies some simple rough
heuristics to guess whether a join is practical in SQL, and decides which joins
to do by SQL and which to do by hashJoin.  It plans a row format with several
groups of fields in this order: main table fields, related table fields to
appear in the output, related table fields needed by hashJoins, hashJoin
result fields needed by other hashJoins, and hashJoin result fields to appear
in output.  It initializes hashJoins with precomputed row indexes and also
provides a mapping from big-row columns to the columns that appear in output.

Thanks to Matt for testing on demo6 during development.

refs #15544

diff --git src/hg/inc/joiner.h src/hg/inc/joiner.h
index 79943f3..468531d 100644
--- src/hg/inc/joiner.h
+++ src/hg/inc/joiner.h
@@ -117,30 +117,44 @@
 struct joinerDtf
 /* Just database, table, and field. */
     {
     struct joinerDtf *next;	/* Next in list. */
     char *database;		/* Database. */
     char *table;		/* Table. */
     char *field;		/* Field. */
     };
 
 struct joinerDtf *joinerDtfNew(char *database, char *table, char *field);
 /* Get new joinerDtf. */
 
 struct joinerDtf *joinerDtfClone(struct joinerDtf *dtf);
 /* Return duplicate (deep copy) of joinerDtf. */
 
+boolean joinerDtfSame(struct joinerDtf *dtfA, struct joinerDtf *dtfB);
+/* Return TRUE if both are NULL or if both have same db, table and field. */
+
+struct joinerDtf *joinerDtfFind(struct joinerDtf *dtfList, struct joinerDtf *dtf);
+/* Return the first element of dtfList that is joinerDtfSame as dtf, or NULL if no such. */
+
+void joinerDtfToSqlFieldString(struct joinerDtf *dtf, char *db, char *buf, size_t bufSize);
+/* If dtf->database is different from db (or db is NULL), write database.table.field info buf,
+ * otherwise just table.field. */
+
+void joinerDtfToSqlTableString(struct joinerDtf *dtf, char *db, char *buf, size_t bufSize);
+/* If dtf->database is different from db (or db is NULL), write database.table info buf,
+ * otherwise just table. */
+
 void joinerDtfFree(struct joinerDtf **pDtf);
 /* Free up memory associated with joinerDtf. */
 
 void joinerDtfFreeList(struct joinerDtf **pList);
 /* Free up memory associated with list of joinerDtfs. */
 
 struct joinerDtf *joinerDtfFromDottedTriple(char *triple);
 /* Get joinerDtf from something in db.table.field format. */
 
 struct joinerPair
 /* A pair of linked fields. */
     {
     struct joinerPair *next;	/* Next in list. */
     struct joinerDtf *a;	/* Typically contains field from input table */
     struct joinerDtf *b;	/* Field in another table */
@@ -154,32 +168,44 @@
 void joinerPairFreeList(struct joinerPair **pList);
 /* Free up memory associated with list of joinerPairs. */
 
 void joinerPairDump(struct joinerPair *jpList, FILE *out);
 /* Write out joiner pair list to file mostly for debugging. */
 
 struct joinerPair *joinerRelate(struct joiner *joiner, char *database, 
 	char *table);
 /* Get list of all ways to link table in given database to other tables,
  * possibly in other databases. */
 
 struct slRef *joinerSetInheritanceChain(struct joinerSet *js);
 /* Return list of self, children, and parents (but not siblings).
  * slFreeList result when done. */
 
+struct joinerField *joinerSetFindField(struct joinerSet *js, struct joinerDtf *dtf);
+/* Find field in set if any that matches dtf */
+
 boolean joinerDtfSameTable(struct joinerDtf *a, struct joinerDtf *b);
 /* Return TRUE if they are in the same database and table. */
 
 boolean joinerDtfAllSameTable(struct joinerDtf *fieldList);
 /* Return TRUE if all joinerPairs refer to same table. */
 
 struct joinerPair *joinerFindRoute(struct joiner *joiner, 
 	struct joinerDtf *a,  struct joinerDtf *b);
 /* Find route between a and b.  Note the field element of a and b
  * are unused. */
 
 struct joinerPair *joinerFindRouteThroughAll(struct joiner *joiner, 
 	struct joinerDtf *tableList);
 /* Return route that gets to all tables in fieldList.  Note that
  * the field element of the items in tableList can be NULL. */
 
+char *joinerFieldChopKey(struct joinerField *jf, char *key);
+/* If jf includes chopBefore and/or chopAfter, apply those to key and return a starting
+ * offset in key, which may be modified. */
+
+void joinerFieldIterateKey(struct joinerField *jf, void(*callback)(void *context, char *key),
+                           void *context, char *key);
+/* Process key according to jf -- if jf->separator, may result in list of processed keys --
+ * and invoke callback with each processed key. */
+
 #endif /* JOINER_H */