Example #1
0
int main(){
  TTableContext Context;
  // create scheme
  Schema AnimalS;
  AnimalS.Add(TPair<TStr,TAttrType>("Animal", atStr));
  AnimalS.Add(TPair<TStr,TAttrType>("Size", atStr));
  AnimalS.Add(TPair<TStr,TAttrType>("Location", atStr));
  AnimalS.Add(TPair<TStr,TAttrType>("Number", atInt));
  TIntV RelevantCols;
  RelevantCols.Add(0);
  RelevantCols.Add(1);
  RelevantCols.Add(2);
  // create table
  PTable T = TTable::LoadSS("Animals", AnimalS, "tests/animals.txt", Context, RelevantCols);
  //PTable T = TTable::LoadSS("Animals", AnimalS, "animals.txt");
  T->Unique("Animal");
  TTable Ts = *T;  // did we fix problem with copy-c'tor ?
  //PTable Ts = TTable::LoadSS("Animals_s", AnimalS, "../../testfiles/animals.txt", RelevantCols);
  //Ts->Unique(AnimalUnique);

  // test Select
  // create predicate tree: find all animals that are big and african or medium and Australian
  TPredicate::TAtomicPredicate A1(atStr, true, EQ, "Location", "", 0, 0, "Africa");  
  TPredicate::TPredicateNode N1(A1);  // Location == "Africa"
  TPredicate::TAtomicPredicate A2(atStr, true, EQ, "Size", "", 0, 0, "big");  
  TPredicate::TPredicateNode N2(A2);  // Size == "big"
  TPredicate::TPredicateNode N3(AND);
  N3.AddLeftChild(&N1);
  N3.AddRightChild(&N2);
  TPredicate::TAtomicPredicate A4(atStr, true, EQ, "Location", "", 0, 0, "Australia");  
  TPredicate::TPredicateNode N4(A4);  
  TPredicate::TAtomicPredicate A5(atStr, true, EQ, "Size", "", 0, 0, "medium");  
  TPredicate::TPredicateNode N5(A5); 
  TPredicate::TPredicateNode N6(AND);
  N6.AddLeftChild(&N4);
  N6.AddRightChild(&N5);
  TPredicate::TPredicateNode N7(OR);
  N7.AddLeftChild(&N3);
  N7.AddRightChild(&N6);
  TPredicate Pred(&N7);
  TIntV SelectedRows;
  Ts.Select(Pred, SelectedRows);

  TStrV GroupBy;
  GroupBy.Add("Location");
  T->Group(GroupBy, "LocationGroup");
  GroupBy.Add("Size");
  T->Group(GroupBy, "LocationSizeGroup");
  T->Count("LocationCount", "Location");
  PTable Tj = T->Join("Location", Ts, "Location");
  TStrV UniqueAnimals;
  UniqueAnimals.Add("Animals_1.Animal");
  UniqueAnimals.Add("Animals_2.Animal");
  Tj->Unique(UniqueAnimals, false);
  //print table
   T->SaveSS("tests/animals_out_T.txt");
   Ts.SaveSS("tests/animals_out_Ts.txt");
   Tj->SaveSS("tests/animals_out_Tj.txt");
  return 0;
}
Example #2
0
int main() {
    // create scheme
    TTable::Schema AnimalS;
    AnimalS.Add(TPair<TStr,TTable::TYPE>("Animal", TTable::STR));
    AnimalS.Add(TPair<TStr,TTable::TYPE>("Size", TTable::STR));
    AnimalS.Add(TPair<TStr,TTable::TYPE>("Location", TTable::STR));
    AnimalS.Add(TPair<TStr,TTable::TYPE>("Number", TTable::INT));
    // create table
    PTable T = TTable::LoadSS("Animals", AnimalS, "animals.txt");
    //PTable T = TTable::LoadSS("Animals", AnimalS, "animals.txt");
    T->Unique("Animal");
    //TTable Ts = *T;  not working because of problem with copy-c'tor
    PTable Ts = TTable::LoadSS("Animals_s", AnimalS, "animals.txt");
    Ts->Unique("Animal");

    // test Select
    // create predicate tree: find all animals that are big and african or medium and Australian
    TPredicate::TAtomicPredicate A1(TPredicate::STR, true, TPredicate::EQ, "Location", "", 0, 0, "Africa");
    TPredicate::TPredicateNode N1(A1);  // Location == "Africa"
    TPredicate::TAtomicPredicate A2(TPredicate::STR, true, TPredicate::EQ, "Size", "", 0, 0, "big");
    TPredicate::TPredicateNode N2(A2);  // Size == "big"
    TPredicate::TPredicateNode N3(TPredicate::AND);
    N3.AddLeftChild(&N1);
    N3.AddRightChild(&N2);
    TPredicate::TAtomicPredicate A4(TPredicate::STR, true, TPredicate::EQ, "Location", "", 0, 0, "Australia");
    TPredicate::TPredicateNode N4(A4);
    TPredicate::TAtomicPredicate A5(TPredicate::STR, true, TPredicate::EQ, "Size", "", 0, 0, "medium");
    TPredicate::TPredicateNode N5(A5);
    TPredicate::TPredicateNode N6(TPredicate::AND);
    N6.AddLeftChild(&N4);
    N6.AddRightChild(&N5);
    TPredicate::TPredicateNode N7(TPredicate::OR);
    N7.AddLeftChild(&N3);
    N7.AddRightChild(&N6);
    TPredicate Pred(&N7);
    Ts->Select(Pred);

    TStrV GroupBy;
    GroupBy.Add("Location");
    T->Group("LocationGroup", GroupBy);
    GroupBy.Add("Size");
    T->Group("LocationSizeGroup", GroupBy);
    T->Count("LocationCount", "Location");
    PTable Tj = T->Join("Location", *Ts, "Location");
    //print table
    T->SaveSS("animals_out_T.txt");
    Ts->SaveSS("animals_out_Ts.txt");
    Tj->SaveSS("animals_out_Tj.txt");
    return 0;
}
Example #3
0
int main(){
  TTableContext Context;
  // create scheme
  Schema AnimalS;
  AnimalS.Add(TPair<TStr,TAttrType>("Animal", atStr));
  AnimalS.Add(TPair<TStr,TAttrType>("Size", atStr));
  AnimalS.Add(TPair<TStr,TAttrType>("Location", atStr));
  AnimalS.Add(TPair<TStr,TAttrType>("Number", atInt));
  TIntV RelevantCols;
  RelevantCols.Add(0);
  RelevantCols.Add(1);
  RelevantCols.Add(2);
  RelevantCols.Add(3);

  PTable P = TTable::LoadSS("Animals", AnimalS, "tests/animals.txt", Context, RelevantCols);

  P->SaveSS("tests/p1.txt");

  TStrV cols;
  cols.Add("Size");
  cols.Add("Number");

  TVec<PTable> R = P->SpliceByGroup(cols);
  for (TInt i = 0; i < R.Len(); i++) {
    TStr fn = i.GetStr();
    R[i]->SaveSS("tests/sznumber" + fn + ".txt");
  }

  P->Unique(cols, true);

  P->SaveSS("tests/p2.txt");

  TStrV group1;
  group1.Add("Location");
  P->Group(group1, "LocationGroup");

  P->SaveSS("tests/p3.txt");

  return 0;
}
Example #4
0
void test1() {
  TTableContext Context;

  // create scheme
  Schema AnimalS;
  AnimalS.Add(TPair<TStr,TAttrType>("Animal", atStr));
  AnimalS.Add(TPair<TStr,TAttrType>("Size", atStr));
  AnimalS.Add(TPair<TStr,TAttrType>("Location", atStr));
  AnimalS.Add(TPair<TStr,TAttrType>("Number", atInt));
  TIntV RelevantCols;
  RelevantCols.Add(0);
  RelevantCols.Add(1);
  RelevantCols.Add(2);
  RelevantCols.Add(3);

  PTable P = TTable::LoadSS("Animals", AnimalS, "tests/s.txt", Context, RelevantCols);
  printf("Load done\n");

  TStrV cols;
  cols.Add("Size");
  cols.Add("Number");

  struct timeval begin, end;
  gettimeofday(&begin, NULL);

  //P->Unique(cols);
  P->Group(cols, "SizeNumberGroup");

  gettimeofday(&end, NULL);

  double diff = (end.tv_sec * 1000000 + end.tv_usec) - (begin.tv_sec * 1000000 + begin.tv_usec);
  printf("Elapsed time:%.3lfs\n", diff / 1000000);

  P->SaveSS("tests/p3.txt");

}
Example #5
0
int main(){
  // create scheme
  TTable::Schema AnimalS;
  AnimalS.Add(TPair<TStr,TTable::TYPE>("Animal", TTable::STR));
  AnimalS.Add(TPair<TStr,TTable::TYPE>("Size", TTable::STR));
  AnimalS.Add(TPair<TStr,TTable::TYPE>("Location", TTable::STR));
  AnimalS.Add(TPair<TStr,TTable::TYPE>("Number", TTable::INT));
  // create table
  PTable T1 = TTable::LoadSS("Animals1", AnimalS, "animals.txt");
  PTable T2 = TTable::LoadSS("Animals2", AnimalS, "animals.txt");
  // test Select
  // create predicate tree: find all animals that are big and african or medium and Australian
  TPredicate::TAtomicPredicate A1(TPredicate::STR, true, TPredicate::EQ, "Size", "", 0, 0, "big");  
  TPredicate::TPredicateNode N1(A1);  // Size == "big"
  TPredicate Pred(&N1);
  T1->Select(Pred);
  T1->SaveSS("animals_out_T1.txt");
  PTable Tj = T1->Join("Location", *T2, "Location");
  TStrV GroupBy;
  GroupBy.Add("Animals1.Animal");
  GroupBy.Add("Animals2.Animal");
  Tj->Group("AnimalPair", GroupBy);
  Tj->Unique("AnimalPair");
  //print table
  Tj->SaveSS("animals_out_Tj_1.txt");

  // Join on Location to get animal pairs
  // select the animal pairs of animals of the same size
  // group by (Animal, Animal)
  // unique by group idx
  /*
  PTable T3 = TTable::LoadSS("Animals3", AnimalS, "../../testfiles/animals.txt");
  //PTable T4 = TTable::LoadSS("Animals4", AnimalS, "../../testfiles/animals.txt");
  TTable T4 = *T3;
  T4.Name = "Animals4";
  PTable To = T3->Join("Location", T4, "Location");
  TPredicate::TAtomicPredicate A2(TPredicate::STR, false, TPredicate::EQ, "Animals3.Size", "Animals4.Size");  
  TPredicate::TPredicateNode N2(A2);
  TPredicate Pred2(&N2);
  To->Select(Pred2);
  TStrV GroupBy1;
  GroupBy1.Add("Animals3.Animal");
  GroupBy1.Add("Animals4.Animal");
  To->Group("AnimalPair", GroupBy1);
  To->Unique("AnimalPair");
  //print table
  To->SaveSS("../../testfiles/animals_out_To_1.txt");
  return 0;
  */

  PTable T3 = TTable::LoadSS("Animals3", AnimalS, "animals.txt");
  PTable T4 = TTable::LoadSS("Animals4", AnimalS, "animals.txt");
  PTable To = T3->Join("Location", *T4, "Location");
  TPredicate::TAtomicPredicate A2(TPredicate::STR, false, TPredicate::EQ, "Animals3.Size", "Animals4.Size");  
  TPredicate::TPredicateNode N2(A2);
  TPredicate Pred2(&N2);
  To->Select(Pred2);
  TStrV GroupBy1;
  GroupBy1.Add("Animals3.Animal");
  GroupBy1.Add("Animals4.Animal");
  To->Group("AnimalPair", GroupBy1);
  To->Unique("AnimalPair");
  //print table
  To->SaveSS("animals_out_To_1.txt");
  return 0;
}