public static List<Tuple> getSamplePeopleTuples() { try { IField[] fields1 = { new StringField("无忌"), new StringField("长孙"), new IntegerField(46), new DoubleField(5.50), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-14-1970")), new TextField("北京大学电气工程学院") }; IField[] fields2 = { new StringField("孔明"), new StringField("洛克贝尔"), new IntegerField(42), new DoubleField(5.99), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1974")), new TextField("北京大学计算机学院") }; IField[] fields3 = { new StringField("宋江"), new StringField("建筑"), new IntegerField(42), new DoubleField(5.99), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1974")), new TextField("伟大的建筑是历史的坐标,具有传承的价值。") }; Tuple tuple1 = new Tuple(SCHEMA_PEOPLE, fields1); Tuple tuple2 = new Tuple(SCHEMA_PEOPLE, fields2); Tuple tuple3 = new Tuple(SCHEMA_PEOPLE, fields3); return Arrays.asList(tuple1, tuple2, tuple3); } catch (ParseException e) { // exception should not happen because we know the data is correct e.printStackTrace(); return Arrays.asList(); } } }
private int compare(IField a, IField b, AttributeType attrType) { int retVal = 0; switch (attrType) { case INTEGER: retVal = ((IntegerField) a).getValue().compareTo(((IntegerField) b).getValue()); break; case DOUBLE: retVal = ((DoubleField) a).getValue().compareTo(((DoubleField) b).getValue()); break; case TEXT: retVal = ((TextField) a).getValue().compareTo(((TextField) b).getValue()); break; case STRING: retVal = ((StringField) a).getValue().compareTo(((StringField) b).getValue()); break; case DATE: retVal = ((DateField) a).getValue().compareTo(((DateField) b).getValue()); break; case DATETIME: retVal = ((DateTimeField) a).getValue().compareTo(((DateTimeField) b).getValue()); break; } return retVal; }
public static List<Tuple> constructSamplePeopleTuples() { IField[] fields1 = { new StringField("bruce"), new StringField("john Lee"), new IntegerField(46), new DoubleField(5.50), new DateTimeField(LocalDateTime.parse("1970-01-01T11:11:11")), new TextField("banana") }; IField[] fields2 = { new StringField("tom hanks"), new StringField("cruise"), new IntegerField(45), new DoubleField(5.95), new DateTimeField(LocalDateTime.parse("1980-01-02T13:14:15")), new TextField("mississippi") }; Tuple tuple1 = new Tuple(SCHEMA_PEOPLE, fields1); Tuple tuple2 = new Tuple(SCHEMA_PEOPLE, fields2); return Arrays.asList(tuple1, tuple2); } }
private boolean compareDouble(Tuple inputTuple) { Object compareToObject = predicate.getCompareToValue(); Class<?> compareToType = compareToObject.getClass(); Double value = inputTuple.getField(predicate.getAttributeName(), DoubleField.class).getValue(); if (compareToType.equals(Integer.class)) { return compareValues(value, (double) (int) compareToObject, predicate.getComparisonType()); } else if (compareToType.equals(Double.class)) { return compareValues(value, (double) compareToObject, predicate.getComparisonType()); } else if (compareToType.equals(String.class)) { try { Double compareToValue = Double.parseDouble((String) predicate.getCompareToValue()); return compareValues(value, compareToValue, predicate.getComparisonType()); } catch (NumberFormatException e) { throw new DataflowException("Unable to parse to number " + e.getMessage()); } } else { throw new DataflowException("Value " + predicate.getCompareToValue() + " is not a valid number type"); } }
new DoubleField(5.50), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-14-1970")), new TextField("Tall Angry") }; IField[] fields1 = { new StringField("tom hanks"), new StringField("cruise"), new IntegerField(45), new DoubleField(5.95), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1971")), new TextField("Short Brown") }; IField[] fields2 = { new StringField("brad lie angelina"), new StringField("pitt"), new IntegerField(44), new DoubleField(6.10), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-12-1972")), new TextField("White Angry") }; IField[] fields3 = { new StringField("george lin lin"), new StringField("lin clooney"), new IntegerField(43), new DoubleField(6.06), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1973")), new TextField("Lin Clooney is Short and lin clooney is Angry") }; IField[] fields4 = { new StringField("christian john wayne"), new StringField("rock bale"), new IntegerField(42), new DoubleField(5.99), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1974")), new TextField("Tall Fair") }; IField[] fields5 = { new StringField("Mary brown"), new StringField("Lake Forest"), new IntegerField(42), new DoubleField(5.99), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1974")), new TextField("Short angry") };
@Test public void testDoubleField() { DoubleField doubleField = new DoubleField(11.11); JsonNode jsonNode = TestUtils.testJsonSerialization(doubleField); Assert.assertTrue(jsonNode.get(JsonConstants.FIELD_VALUE).isFloatingPointNumber()); }
new DoubleField(5.50), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-14-1970")), new TextField("中新社北京4月26日电 (记者 刘育英)“中国制造2025”政策措施实施以来,“为稳定工业增长、加快制造业转型升级发" + "挥了重要作用”,效果初步显现。") }; IField[] fields2 = { new StringField("tom hanks"), new StringField("cruise"), new IntegerField(45), new DoubleField(5.95), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1971")), new TextField(" 中国2015年发布了“中国制造2025”通知。中国工业和信息化部运行监测协调局副" + "局长黄利斌26日在国新办新闻发布会上表示,自“中国制造2025”实施以来,国家制造业创新中心建设、智能制造" + ",4个基本落实,其余正在推进。" ) }; IField[] fields3 = { new StringField("brad lie angelina"), new StringField("pitt"), new IntegerField(44), new DoubleField(6.10), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-12-1972")), new TextField(" 2017年,工信部将重点推进六方面工作:加大“五大工程”实施力度," + "积极推进创新中心建设;扩大试点示范城市(群)覆盖面;实施新一轮重大技术改造升级工程;"+ "推进" + "制造业与互联网融合发展;优化制造业发展环境。" ) }; IField[] fields4 = { new StringField("george lin lin"), new StringField("lin clooney"), new IntegerField(43), new DoubleField(6.06), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1973")), new TextField(" 黄利斌说,今年继续开展“互联" + "网+”制造业试点示范,加快工业互联网基础设施改造升级。现在,47%的大企业" + "营成本平均下降了8.8%,经营利润平均增长了6.9%。" ) }; IField[] fields5 = { new StringField("christian john wayne"), new StringField("rock bale"), new IntegerField(42), new DoubleField(5.99), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1974")), new TextField(" 工信部今年还将" + "选取20-30个城市(群)继续开展“中国制造2025”试点示范创建," + "指导试点示范城市(群),在落实新发展理念等方面先行先试。") }; IField[] fields6 = { new StringField("Mary brown"), new StringField("Lake Forest"), new IntegerField(42), new DoubleField(5.99), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1974")), new TextField("资料图:由驻日中" + "资太阳能企业开发、承建并运营维护的日本岛根县滨田市第二期12兆瓦光伏电站项目(滨田MS太阳能发电站),4月25日在当"
break; case DOUBLE: field = new DoubleField(Double.parseDouble(fieldValue)); break; case DATE:
switch (inputAttr.getType()) { case INTEGER: tupleBuilder.add(aggregationItems.get(i).getResultAttributeName(), AttributeType.DOUBLE, new DoubleField(((int) aggregatedResults.get(i).getValue()) * 1.0 / getRowsCount() * 1.0)); break; case DOUBLE: tupleBuilder.add(aggregationItems.get(i).getResultAttributeName(), AttributeType.DOUBLE, new DoubleField((double) aggregatedResults.get(i).getValue() / getRowsCount() * 1.0)); break;
break; case DOUBLE: aggregatedResults.set(i, new DoubleField((double) aggregatedResults.get(i).getValue() + (double) field.getValue())); break;
@Test public void testMinHeightAggregation() throws Exception { Attribute attribute = TestConstants.HEIGHT_ATTR; String attributeName = attribute.getName(); AggregationType aggType = AggregationType.MIN; String resultAttributeName = AggregatorTestConstants.MIN_HEIGHT_RESULT_ATTR_NAME; AggregationAttributeAndResult aggEntity = new AggregationAttributeAndResult(attributeName, aggType, resultAttributeName); List<AggregationAttributeAndResult> aggEntitiesList = new ArrayList<>(); aggEntitiesList.add(aggEntity); IField[] row1 = {new DoubleField(5.50)}; Schema schema = new Schema(new Attribute(resultAttributeName, AttributeType.DOUBLE)); List<Tuple> expectedResults = new ArrayList<>(); expectedResults.add(new Tuple(schema, row1)); List<Tuple> returnedResults = getQueryResults(aggEntitiesList); Assert.assertEquals(1, returnedResults.size()); Assert.assertTrue(TestUtils.equals(expectedResults, returnedResults)); }
@Test public void testAvgHeightAggregation() throws Exception { Attribute attribute = TestConstants.HEIGHT_ATTR; String attributeName = attribute.getName(); AggregationType aggType = AggregationType.AVERAGE; String resultAttributeName = AggregatorTestConstants.AVG_HEIGHT_RESULT_ATTR_NAME; AggregationAttributeAndResult aggEntity = new AggregationAttributeAndResult(attributeName, aggType, resultAttributeName); List<AggregationAttributeAndResult> aggEntitiesList = new ArrayList<>(); aggEntitiesList.add(aggEntity); IField[] row1 = {new DoubleField((5.50 + 5.95 + 6.10 + 6.06 + 5.99 + 5.99) / 6.0)}; Schema schema = new Schema(new Attribute(resultAttributeName, AttributeType.DOUBLE)); List<Tuple> expectedResults = new ArrayList<>(); expectedResults.add(new Tuple(schema, row1)); List<Tuple> returnedResults = getQueryResults(aggEntitiesList); Assert.assertEquals(1, returnedResults.size()); Assert.assertTrue(TestUtils.equals(expectedResults, returnedResults)); }
@Test public void testMaxHeightAggregation() throws Exception { Attribute attribute = TestConstants.HEIGHT_ATTR; String attributeName = attribute.getName(); AggregationType aggType = AggregationType.MAX; String resultAttributeName = AggregatorTestConstants.MAX_HEIGHT_RESULT_ATTR_NAME; AggregationAttributeAndResult aggEntity = new AggregationAttributeAndResult(attributeName, aggType, resultAttributeName); List<AggregationAttributeAndResult> aggEntitiesList = new ArrayList<>(); aggEntitiesList.add(aggEntity); IField[] row1 = {new DoubleField(6.10)}; Schema schema = new Schema(new Attribute(resultAttributeName, AttributeType.DOUBLE)); List<Tuple> expectedResults = new ArrayList<>(); expectedResults.add(new Tuple(schema, row1)); List<Tuple> returnedResults = getQueryResults(aggEntitiesList); Assert.assertEquals(1, returnedResults.size()); Assert.assertTrue(TestUtils.equals(expectedResults, returnedResults)); }
@Test public void testSumHeightAggregation() throws Exception { Attribute attribute = TestConstants.HEIGHT_ATTR; String attributeName = attribute.getName(); AggregationType aggType = AggregationType.SUM; String resultAttributeName = AggregatorTestConstants.SUM_HEIGHT_RESULT_ATTR_NAME; AggregationAttributeAndResult aggEntity = new AggregationAttributeAndResult(attributeName, aggType, resultAttributeName); List<AggregationAttributeAndResult> aggEntitiesList = new ArrayList<>(); aggEntitiesList.add(aggEntity); IField[] row1 = {new DoubleField((5.50 + 5.95 + 6.10 + 6.06 + 5.99 + 5.99))}; Schema schema = new Schema(new Attribute(resultAttributeName, AttributeType.DOUBLE)); List<Tuple> expectedResults = new ArrayList<>(); expectedResults.add(new Tuple(schema, row1)); List<Tuple> returnedResults = getQueryResults(aggEntitiesList); Assert.assertEquals(1, returnedResults.size()); Assert.assertTrue(TestUtils.equals(expectedResults, returnedResults)); }
@Test public void testMinHeightMaxHeightAggregation() throws Exception { Attribute attribute1 = TestConstants.HEIGHT_ATTR; String attributeName1 = attribute1.getName(); AggregationType aggType1 = AggregationType.MIN; Attribute attribute2 = TestConstants.HEIGHT_ATTR; String attributeName2 = attribute2.getName(); AggregationType aggType2 = AggregationType.MAX; String resultAttributeName1 = AggregatorTestConstants.MIN_HEIGHT_RESULT_ATTR_NAME; String resultAttributeName2 = AggregatorTestConstants.MAX_HEIGHT_RESULT_ATTR_NAME; AggregationAttributeAndResult aggEntity1 = new AggregationAttributeAndResult(attributeName1, aggType1, resultAttributeName1); AggregationAttributeAndResult aggEntity2 = new AggregationAttributeAndResult(attributeName2, aggType2, resultAttributeName2); List<AggregationAttributeAndResult> aggEntitiesList = new ArrayList<>(); aggEntitiesList.add(aggEntity1); aggEntitiesList.add(aggEntity2); IField[] row1 = {new DoubleField(5.50), new DoubleField(6.10)}; Schema schema = new Schema(new Attribute(resultAttributeName1, AttributeType.DOUBLE), new Attribute(resultAttributeName2, AttributeType.DOUBLE)); List<Tuple> expectedResults = new ArrayList<>(); expectedResults.add(new Tuple(schema, row1)); List<Tuple> returnedResults = getQueryResults(aggEntitiesList); Assert.assertEquals(1, returnedResults.size()); Assert.assertTrue(TestUtils.equals(expectedResults, returnedResults)); }
@Test public void testGetters() throws ParseException { // create data tuple first Attribute[] attributes = new Attribute[TestConstants.ATTRIBUTES_PEOPLE.length + 1]; for (int count = 0; count < attributes.length - 1; count++) { attributes[count] = TestConstants.ATTRIBUTES_PEOPLE[count]; } attributes[attributes.length - 1] = SchemaConstants.SPAN_LIST_ATTRIBUTE; List<IField> fields = new ArrayList<IField>( Arrays.asList(new IField[] { new StringField("bruce"), new StringField("lee"), new IntegerField(46), new DoubleField(5.50), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-14-1970")), new TextField("bruce was born in new york city and was grown up in los angeles") })); IField spanField = createSpanListField(); fields.add(spanField); spanTuple = new Tuple(new Schema(attributes), fields.toArray(new IField[fields.size()])); IField spanFieldRetrieved = spanTuple.getField(SchemaConstants.SPAN_LIST); Assert.assertTrue(spanFieldRetrieved instanceof ListField); Assert.assertSame(spanField, spanFieldRetrieved); }
for(int i = 0; i < testSize; i++){ IField[] fields = { new StringField(getRandomString()), new StringField(getRandomString()), new IntegerField(rand.nextInt()), new DoubleField(rand.nextDouble()*rand.nextInt()), new DateField(getRandomDate()), new TextField(getRandomString())}; tupleList.add(new Tuple(new Schema(schemaAttributes), fields));
@Test public void testMultipleWordQueryInTextFieldUsingScan2() throws Exception { ArrayList<String> names = new ArrayList<String>(Arrays.asList("tall fair")); Dictionary dictionary = new Dictionary(names); // create a data tuple first List<Span> list1 = new ArrayList<Span>(); Span span1 = new Span("description", 0, 9, "tall fair","Tall Fair"); list1.add(span1); Attribute[] schemaAttributes = new Attribute[TestConstants.ATTRIBUTES_PEOPLE.length + 1]; for (int count = 0; count < schemaAttributes.length - 1; count++) { schemaAttributes[count] = TestConstants.ATTRIBUTES_PEOPLE[count]; } schemaAttributes[schemaAttributes.length - 1] = RESULTS_ATTRIBUTE; IField[] fields2 = { new StringField("christian john wayne"), new StringField("rock bale"), new IntegerField(42), new DoubleField(5.99), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1974")), new TextField("Tall Fair"), new ListField<Span>(list1) }; Tuple tuple2 = new Tuple(new Schema(schemaAttributes), fields2); List<Tuple> expectedResults = new ArrayList<Tuple>(); expectedResults.add(tuple2); List<String> attributeNames = Arrays.asList(TestConstants.FIRST_NAME, TestConstants.LAST_NAME, TestConstants.DESCRIPTION); List<Tuple> returnedResults = DictionaryMatcherTestHelper.getQueryResults(PEOPLE_TABLE, dictionary, attributeNames, KeywordMatchingType.SUBSTRING_SCANBASED); boolean contains = TestUtils.equals(expectedResults, returnedResults); Assert.assertTrue(contains); }
@Test public void testMultipleWordQueryInStringFieldUsingScan() throws Exception { ArrayList<String> names = new ArrayList<String>(Arrays.asList("christian john wayne", "rock bale")); Dictionary dictionary = new Dictionary(names); // create a data tuple first List<Span> list1 = new ArrayList<Span>(); Span span1 = new Span("firstName", 0, 20, "christian john wayne","christian john wayne"); Span span2 = new Span("lastName", 0, 9, "rock bale", "rock bale"); list1.add(span1); list1.add(span2); Attribute[] schemaAttributes = new Attribute[TestConstants.ATTRIBUTES_PEOPLE.length + 1]; for (int count = 0; count < schemaAttributes.length - 1; count++) { schemaAttributes[count] = TestConstants.ATTRIBUTES_PEOPLE[count]; } schemaAttributes[schemaAttributes.length - 1] = RESULTS_ATTRIBUTE; IField[] fields1 = { new StringField("christian john wayne"), new StringField("rock bale"), new IntegerField(42), new DoubleField(5.99), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1974")), new TextField("Tall Fair"), new ListField<Span>(list1) }; Tuple tuple1 = new Tuple(new Schema(schemaAttributes), fields1); List<Tuple> expectedResults = new ArrayList<Tuple>(); expectedResults.add(tuple1); List<String> attributeNames = Arrays.asList(TestConstants.FIRST_NAME, TestConstants.LAST_NAME); List<Tuple> returnedResults = DictionaryMatcherTestHelper.getQueryResults(PEOPLE_TABLE, dictionary, attributeNames, KeywordMatchingType.SUBSTRING_SCANBASED); boolean contains = TestUtils.equals(expectedResults, returnedResults); Assert.assertTrue(contains); } @Test
@Test public void testMinHeightMaxAgeAggregation() throws Exception { Attribute attribute1 = TestConstants.HEIGHT_ATTR; String attributeName1 = attribute1.getName(); AggregationType aggType1 = AggregationType.MIN; Attribute attribute2 = TestConstants.AGE_ATTR; String attributeName2 = attribute2.getName(); AggregationType aggType2 = AggregationType.MAX; String resultAttributeName1 = AggregatorTestConstants.MIN_HEIGHT_RESULT_ATTR_NAME; String resultAttributeName2 = AggregatorTestConstants.MAX_AGE_RESULT_ATTR_NAME; AggregationAttributeAndResult aggEntity1 = new AggregationAttributeAndResult(attributeName1, aggType1, resultAttributeName1); AggregationAttributeAndResult aggEntity2 = new AggregationAttributeAndResult(attributeName2, aggType2, resultAttributeName2); List<AggregationAttributeAndResult> aggEntitiesList = new ArrayList<>(); aggEntitiesList.add(aggEntity1); aggEntitiesList.add(aggEntity2); IField[] row1 = {new DoubleField(5.50), new IntegerField(46)}; Schema schema = new Schema(new Attribute(resultAttributeName1, AttributeType.DOUBLE), new Attribute(resultAttributeName2, AttributeType.INTEGER)); List<Tuple> expectedResults = new ArrayList<>(); expectedResults.add(new Tuple(schema, row1)); List<Tuple> returnedResults = getQueryResults(aggEntitiesList); Assert.assertEquals(1, returnedResults.size()); Assert.assertTrue(TestUtils.equals(expectedResults, returnedResults)); }