public DrillValuesRelBase(RelOptCluster cluster, RelDataType rowType, List<? extends List<RexLiteral>> tuples, RelTraitSet traits) { this(cluster, rowType, tuples, traits, convertToJsonOptions(rowType, tuples)); }
/** * Converts tuples into json representation taking into account row type. * Example: [['A']] -> [{"EXPR$0":"A"}], [[1]] -> [{"EXPR$0":{"$numberLong":1}}] * * @param rowType row type * @param tuples list of constant values in a row-expression * @return json representation of tuples */ private static JSONOptions convertToJsonOptions(RelDataType rowType, List<? extends List<RexLiteral>> tuples) { try { return new JSONOptions(convertToJsonNode(rowType, tuples), JsonLocation.NA); } catch (IOException e) { throw new DrillRuntimeException("Failure while attempting to encode Values in JSON.", e); } }
private static JsonNode convertToJsonNode(RelDataType rowType, List<? extends List<RexLiteral>> tuples) throws IOException { TokenBuffer out = new TokenBuffer(MAPPER.getFactory().getCodec(), false); JsonOutput json = new ExtendedJsonOutput(out); json.writeStartArray(); String[] fields = rowType.getFieldNames().toArray(new String[rowType.getFieldCount()]); for (List<RexLiteral> row : tuples) { json.writeStartObject(); int i = 0; for (RexLiteral field : row) { json.writeFieldName(fields[i]); writeLiteral(field, json); i++; } json.writeEndObject(); } json.writeEndArray(); json.flush(); return out.asParser().readValueAsTree(); }