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/lib/annoFormatTab.c src/lib/annoFormatTab.c index 35aadfb..c09f05f 100644 --- src/lib/annoFormatTab.c +++ src/lib/annoFormatTab.c @@ -126,32 +126,33 @@ *pIsFirst = isFirst; } static void aftInitialize(struct annoFormatter *vSelf, struct annoStreamer *primary, struct annoStreamer *integrators) /* Print header, regardless of whether we get any data after this. * Also build arrays of flags for whether each column from each source is to be included * in the output. */ { struct annoFormatTab *self = (struct annoFormatTab *)vSelf; makeColumnFlags(self, primary, integrators); if (self->needHeader) { char *primaryHeader = primary->getHeader(primary); boolean isFirst = TRUE; + char *newlineAtEnd = (lastChar(primaryHeader) == '\n') ? "" : "\n"; if (isNotEmpty(primaryHeader)) - fprintf(self->f, "# Header from primary input:\n%s", primaryHeader); + fprintf(self->f, "# Header from primary input:\n%s%s", primaryHeader, newlineAtEnd); fputc('#', self->f); printHeaderColumns(self, primary, 0, &isFirst); uint sourceIx; struct annoStreamer *grator; for (sourceIx = 1, grator = integrators; grator != NULL; grator = grator->next, sourceIx++) printHeaderColumns(self, grator, sourceIx, &isFirst); fputc('\n', self->f); self->needHeader = FALSE; } } static char **bed4WordsFromAnnoRow(struct annoRow *row, char *fourth) /* Return an array of 4 words with row's chrom, chromStart, and chromEnd, and cloned fourth. */ { char **words;