For the background of the model, let's consider the trade information coming in from multiple sources. Each source system has its own designation of the accounts on which the trades happen but ultimately they are the same accounts. So there is a table that contains the translation from the account designations of various external systems to our system's own internal account identifier. This gets described with the row types:
our $rtInTrans = Triceps::RowType->new( # a transaction received id => "int32", # the transaction id acctSrc => "string", # external system that sent us a transaction acctXtrId => "string", # its name of the account of the transaction amount => "int32", # the amount of transaction (int is easier to check) ) or die "$!"; our $rtAccounts = Triceps::RowType->new( # account translation map source => "string", # external system that sent us a transaction external => "string", # its name of the account of the transaction internal => "int32", # our internal account id ) or die "$!";
Other than those basics, the rest of information is only minimal, to keep the examples smaller. Even the trade ids are expected to be global and not per the source systems (which is not realistic but saves another little bit of work).
The accounts table can be indexed in multiple ways for multiple purposes, say:
our $ttAccounts = Triceps::TableType->new($rtAccounts) ->addSubIndex("lookupSrcExt", # quick look-up by source and external id Triceps::IndexType->newHashed(key => [ "source", "external" ]) ) ->addSubIndex("iterateSrc", # for iteration in order grouped by source Triceps::IndexType->newHashed(key => [ "source" ]) ->addSubIndex("iterateSrcExt", Triceps::IndexType->newHashed(key => [ "external" ]) ) ) ->addSubIndex("lookupIntGroup", # quick look-up by internal id (to multiple externals) Triceps::IndexType->newHashed(key => [ "internal" ]) ->addSubIndex("lookupInt", Triceps::IndexType->newFifo()) ) or die "$!"; $ttAccounts->initialize() or die "$!";
For our purpose of joining, the first, primary key is the way to go. Using the primary key also has the advantage of making sure that there is no more than one row for each key value.
The manual lookup will do the filtering: find, whether there is a match in the translation table, and if so then passing the row through. The example goes as follows:
our $uJoin = Triceps::Unit->new("uJoin") or die "$!"; our $tAccounts = $uJoin->makeTable($ttAccounts, &Triceps::EM_CALL, "tAccounts") or die "$!"; my $lbFilterResult = $uJoin->makeDummyLabel($rtInTrans, "lbFilterResult"); my $lbFilter = $uJoin->makeLabel($rtInTrans, "lbFilter", undef, sub { my ($label, $rowop) = @_; my $row = $rowop->getRow(); my $rh = $tAccounts->findBy( source => $row->get("acctSrc"), external => $row->get("acctXtrId"), ); if (!$rh->isNull()) { $uJoin->call($lbFilterResult->makeRowop($rowop->getOpcode(), $row)); } }) or die "$!"; # label to print the changes to the detailed stats makePrintLabel("lbPrintPackets", $lbFilterResult); while(<STDIN>) { chomp; my @data = split(/,/); # starts with a command, then string opcode my $type = shift @data; if ($type eq "acct") { $uJoin->makeArrayCall($tAccounts->getInputLabel(), @data) or die "$!"; } elsif ($type eq "trans") { $uJoin->makeArrayCall($lbFilter, @data) or die "$!"; } $uJoin->drainFrame(); # just in case, for completeness }
The findBy() is where the join actually happens: the lookup of the data in a table by values from a different row. Very similar to what the basic window example was doing before. After that the fact of successful or unsuccessful lookup is used to pass the original row through or throw it away. If the found row were used to pick some fields from it and stick them into the result, that would be a more complete join, more like what you often expect to see.
And here is an example of the input processing:
acct,OP_INSERT,source1,999,1 acct,OP_INSERT,source1,2011,2 acct,OP_INSERT,source2,ABCD,1 trans,OP_INSERT,1,source1,999,100 lbFilterResult OP_INSERT id="1" acctSrc="source1" acctXtrId="999" amount="100" trans,OP_INSERT,2,source2,ABCD,200 lbFilterResult OP_INSERT id="2" acctSrc="source2" acctXtrId="ABCD" amount="200" trans,OP_INSERT,3,source2,QWERTY,200 acct,OP_INSERT,source2,QWERTY,2 trans,OP_DELETE,3,source2,QWERTY,200 lbFilterResult OP_DELETE id="3" acctSrc="source2" acctXtrId="QWERTY" amount="200" acct,OP_DELETE,source1,999,1
It starts with populating the account table. Then the transactions that find the match pass, and those who don't find don't pass. If more of the account translations get added later, the transactions for them start passing but as you can see, the result might be slightly unexpected: you may get a DELETE that had no matching previous import. This happens because the lookup join keeps no history on its left side and can't react properly to the changes to the table on the right. Because of this, the lookup joins work best when the reference table gets pre-populated in advance and then stays stable.
No comments:
Post a Comment