@Test public void testBasicCase4() throws Exception { String[] directives = new String[] { "flatten col1,col2,col3", }; JsonArray a1 = new JsonArray(); a1.add(new JsonPrimitive("x1")); a1.add(new JsonPrimitive("y1")); a1.add(new JsonPrimitive("z1")); JsonArray a2 = new JsonArray(); a2.add(new JsonPrimitive("x2")); a2.add(new JsonPrimitive("y2")); JsonArray a3 = new JsonArray(); a3.add(new JsonPrimitive("a1")); a3.add(new JsonPrimitive("b1")); JsonArray a4 = new JsonArray(); a4.add(new JsonPrimitive("a2")); a4.add(new JsonPrimitive("b2")); a4.add(new JsonPrimitive("c2")); List<String> b1 = new ArrayList<>(); b1.add("x1"); b1.add("y1"); b1.add("z1"); List<String> b2 = new ArrayList<>(); b2.add("x2"); b2.add("y2"); List<String> b3 = new ArrayList<>(); b3.add("a1"); b3.add("b1"); List<String> b4 = new ArrayList<>(); b4.add("a2"); b4.add("b2"); b4.add("c2"); List<Row> rows = Arrays.asList( new Row("col1", "A"), new Row("col1", "B"), new Row("col2", a1).add("col3", a3), new Row("col2", a2).add("col3", a4), new Row("col2", b1).add("col3", b3), new Row("col2", b2).add("col3", b4) ); rows = TestingRig.execute(directives, rows); Assert.assertTrue(rows.size() == 14); }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { count = 0; for (String column : columns) { locations[count] = row.find(column); ++count; } int max = Integer.MIN_VALUE; for (int i = 0; i < count; ++i) { if (locations[i] != -1) { Object value = row.getValue(locations[i]); int m = -1; if (value instanceof JsonArray) { m = ((JsonArray) value).size(); } else if (value instanceof List) { m = ((List) value).size(); } else { m = 1; } if (m > max) { max = m; } } } for (int k = 0; k < max; ++k) { Row r = new Row(row); for (int i = 0; i < count; ++i) { if (locations[i] != -1) { Object value = row.getValue(locations[i]); if (value == null) { r.add(columns[i], null); } else { Object v = null; if (value instanceof JsonArray) { JsonArray array = (JsonArray) value; if (k < array.size()) { v = array.get(k); } } else if (value instanceof List) { List<Object> array = (List) value; if (k < array.size()) { v = array.get(k); } } else { v = value; } if (v == null) { r.addOrSet(columns[i], null); } else { if (v instanceof JsonElement) { r.setValue(locations[i], JsParser.getValue((JsonElement) v)); } else { r.setValue(locations[i], v); } } } } else { r.addOrSet(columns[i], null); } } results.add(r); } } return results; }
Flatten implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { count = 0; for (String column : columns) { locations[count] = row.find(column); ++count; } int max = Integer.MIN_VALUE; for (int i = 0; i < count; ++i) { if (locations[i] != -1) { Object value = row.getValue(locations[i]); int m = -1; if (value instanceof JsonArray) { m = ((JsonArray) value).size(); } else if (value instanceof List) { m = ((List) value).size(); } else { m = 1; } if (m > max) { max = m; } } } for (int k = 0; k < max; ++k) { Row r = new Row(row); for (int i = 0; i < count; ++i) { if (locations[i] != -1) { Object value = row.getValue(locations[i]); if (value == null) { r.add(columns[i], null); } else { Object v = null; if (value instanceof JsonArray) { JsonArray array = (JsonArray) value; if (k < array.size()) { v = array.get(k); } } else if (value instanceof List) { List<Object> array = (List) value; if (k < array.size()) { v = array.get(k); } } else { v = value; } if (v == null) { r.addOrSet(columns[i], null); } else { if (v instanceof JsonElement) { r.setValue(locations[i], JsParser.getValue((JsonElement) v)); } else { r.setValue(locations[i], v); } } } } else { r.addOrSet(columns[i], null); } } results.add(r); } } return results; } }
Flatten implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { count = 0; for (String column : columns) { locations[count] = row.find(column); ++count; } int max = Integer.MIN_VALUE; for (int i = 0; i < count; ++i) { if (locations[i] != -1) { Object value = row.getValue(locations[i]); int m = -1; if (value instanceof JsonArray) { m = ((JsonArray) value).size(); } else if (value instanceof List) { m = ((List) value).size(); } else { m = 1; } if (m > max) { max = m; } } } for (int k = 0; k < max; ++k) { Row r = new Row(row); for (int i = 0; i < count; ++i) { if (locations[i] != -1) { Object value = row.getValue(locations[i]); if (value == null) { r.add(columns[i], null); } else { Object v = null; if (value instanceof JsonArray) { JsonArray array = (JsonArray) value; if (k < array.size()) { v = array.get(k); } } else if (value instanceof List) { List<Object> array = (List) value; if (k < array.size()) { v = array.get(k); } } else { v = value; } if (v == null) { r.addOrSet(columns[i], null); } else { if (v instanceof JsonElement) { r.setValue(locations[i], JsParser.getValue((JsonElement) v)); } else { r.setValue(locations[i], v); } } } } else { r.addOrSet(columns[i], null); } } results.add(r); } } return results; }  }
Flatten implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { count = 0; for (String column : columns) { locations[count] = row.find(column); ++count; } int max = Integer.MIN_VALUE; for (int i = 0; i < count; ++i) { if (locations[i] != -1) { Object value = row.getValue(locations[i]); int m = -1; if (value instanceof JsonArray) { m = ((JsonArray) value).size(); } else if (value instanceof List) { m = ((List) value).size(); } else { m = 1; } if (m > max) { max = m; } } } for (int k = 0; k < max; ++k) { Row r = new Row(row); for (int i = 0; i < count; ++i) { if (locations[i] != -1) { Object value = row.getValue(locations[i]); if (value == null) { r.add(columns[i], null); } else { Object v = null; if (value instanceof JsonArray) { JsonArray array = (JsonArray) value; if (k < array.size()) { v = array.get(k); } } else if (value instanceof List) { List<Object> array = (List) value; if (k < array.size()) { v = array.get(k); } } else { v = value; } if (v == null) { r.addOrSet(columns[i], null); } else { if (v instanceof JsonElement) { r.setValue(locations[i], JsParser.getValue((JsonElement) v)); } else { r.setValue(locations[i], v); } } } } else { r.addOrSet(columns[i], null); } } results.add(r); } } return results; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); }
Flatten implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { count = 0; for (String column : columns) { locations[count] = row.find(column); ++count; } int max = Integer.MIN_VALUE; for (int i = 0; i < count; ++i) { if (locations[i] != -1) { Object value = row.getValue(locations[i]); int m = -1; if (value instanceof JsonArray) { m = ((JsonArray) value).size(); } else if (value instanceof List) { m = ((List) value).size(); } else { m = 1; } if (m > max) { max = m; } } } for (int k = 0; k < max; ++k) { Row r = new Row(row); for (int i = 0; i < count; ++i) { if (locations[i] != -1) { Object value = row.getValue(locations[i]); if (value == null) { r.add(columns[i], null); } else { Object v = null; if (value instanceof JsonArray) { JsonArray array = (JsonArray) value; if (k < array.size()) { v = array.get(k); } } else if (value instanceof List) { List<Object> array = (List) value; if (k < array.size()) { v = array.get(k); } } else { v = value; } if (v == null) { r.addOrSet(columns[i], null); } else { if (v instanceof JsonElement) { r.setValue(locations[i], JsParser.getValue((JsonElement) v)); } else { r.setValue(locations[i], v); } } } } else { r.addOrSet(columns[i], null); } } results.add(r); } } return results; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); static final String NAME; }