public static Block createTestBlock() { BlockBuilder blockBuilder = JSON.createBlockBuilder(null, 1); Slice slice = Slices.utf8Slice("{\"x\":1, \"y\":2}"); JSON.writeSlice(blockBuilder, slice); return blockBuilder.build(); }
@Override Object extractValue(Block block, int position) { return JSON.getSlice(block, position); }
@Override void writeNextRandomValue(Random random, BlockBuilder builder) { String json = String.format("{\"%s\": %d, \"%s\": \"%s\"}", nextString(random, MAX_GENERATED_JSON_KEY_LENGTH), random.nextInt(), nextString(random, MAX_GENERATED_JSON_KEY_LENGTH), random.nextInt()); JSON.writeString(builder, json); } }
@Override public void writeSlice(BlockBuilder blockBuilder, Slice value) { writeSlice(blockBuilder, value, 0, value.length()); }
@Override protected Object visitGenericLiteral(GenericLiteral node, ConnectorSession session) { Type type = metadata.getType(parseTypeSignature(node.getType())); if (type == null) { throw new SemanticException(TYPE_MISMATCH, node, "Unknown type: " + node.getType()); } if (JSON.equals(type)) { Signature operatorSignature = new Signature("json_parse", SCALAR, JSON.getTypeSignature(), VARCHAR.getTypeSignature()); return functionInvoker.invoke(operatorSignature, session, ImmutableList.of(utf8Slice(node.getValue()))); } try { Signature signature = metadata.getFunctionRegistry().getCoercion(VARCHAR, type); return functionInvoker.invoke(signature, session, ImmutableList.of(utf8Slice(node.getValue()))); } catch (IllegalArgumentException e) { throw new SemanticException(TYPE_MISMATCH, node, "No literal form for type %s", type); } }
@Setup public void setup() { Signature signature = new Signature("$operator$CAST", FunctionKind.SCALAR, mapType(VARCHAR, BIGINT).getTypeSignature(), JSON.getTypeSignature()); Type valueType; switch (valueTypeName) { case "BIGINT": valueType = BIGINT; break; case "DOUBLE": valueType = DOUBLE; break; case "VARCHAR": valueType = VARCHAR; break; default: throw new UnsupportedOperationException(); } List<RowExpression> projections = ImmutableList.of( new CallExpression(signature, mapType(VARCHAR, valueType), ImmutableList.of(field(0, JSON)))); MetadataManager metadata = MetadataManager.createTestMetadataManager(); pageProcessor = new ExpressionCompiler(metadata, new PageFunctionCompiler(metadata, 0)) .compilePageProcessor(Optional.empty(), projections) .get(); page = new Page(createChannel(POSITION_COUNT, MAP_SIZE, valueType)); }
@Override protected Type visitGenericLiteral(GenericLiteral node, StackableAstVisitorContext<Context> context) { Type type; try { type = typeManager.getType(parseTypeSignature(node.getType())); } catch (IllegalArgumentException e) { throw new SemanticException(TYPE_MISMATCH, node, "Unknown type: " + node.getType()); } if (!JSON.equals(type)) { try { functionRegistry.getCoercion(VARCHAR, type); } catch (IllegalArgumentException e) { throw new SemanticException(TYPE_MISMATCH, node, "No literal form for type %s", type); } } return setExpressionType(node, type); }
public void writeString(BlockBuilder blockBuilder, String value) { writeSlice(blockBuilder, Slices.utf8Slice(value)); }
if (JSON.equals(type)) { ScalarFunctionImplementation operator = metadata.getFunctionRegistry().getScalarFunctionImplementation(new Signature("json_parse", SCALAR, JSON.getTypeSignature(), VARCHAR.getTypeSignature())); try { return ExpressionInterpreter.invoke(session, operator, ImmutableList.<Object>of(utf8Slice(node.getValue())));
@Test public void testCastWithJsonParseOptimization() Signature jsonParseSignature = new Signature("json_parse", SCALAR, JSON.getTypeSignature(), ImmutableList.of(VARCHAR.getTypeSignature())); Signature jsonCastSignature = new Signature(CAST, SCALAR, parseTypeSignature("array(integer)"), ImmutableList.of(JSON.getTypeSignature())); RowExpression jsonCastExpression = new CallExpression(jsonCastSignature, new ArrayType(INTEGER), ImmutableList.of(call(jsonParseSignature, JSON, constant(utf8Slice("[1, 2]"), VARCHAR)))); RowExpression resultExpression = optimizer.optimize(jsonCastExpression); jsonCastSignature = new Signature(CAST, SCALAR, parseTypeSignature("array(varchar)"), ImmutableList.of(JSON.getTypeSignature())); jsonCastExpression = new CallExpression(jsonCastSignature, new ArrayType(VARCHAR), ImmutableList.of(call(jsonParseSignature, JSON, field(1, VARCHAR)))); resultExpression = optimizer.optimize(jsonCastExpression); jsonCastSignature = new Signature(CAST, SCALAR, parseTypeSignature("map(integer,varchar)"), ImmutableList.of(JSON.getTypeSignature())); jsonCastExpression = new CallExpression(jsonCastSignature, mapType(INTEGER, VARCHAR), ImmutableList.of(call(jsonParseSignature, JSON, field(1, VARCHAR)))); resultExpression = optimizer.optimize(jsonCastExpression); jsonCastSignature = new Signature(CAST, SCALAR, parseTypeSignature("row(varchar,bigint)"), ImmutableList.of(JSON.getTypeSignature())); jsonCastExpression = new CallExpression(jsonCastSignature, RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), ImmutableList.of(call(jsonParseSignature, JSON, field(1, VARCHAR)))); resultExpression = optimizer.optimize(jsonCastExpression);
@Override protected RowExpression visitGenericLiteral(GenericLiteral node, Void context) { Type type; try { type = typeManager.getType(parseTypeSignature(node.getType())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Unsupported type: " + node.getType()); } if (JSON.equals(type)) { return call( new Signature("json_parse", SCALAR, getType(node).getTypeSignature(), VARCHAR.getTypeSignature()), getType(node), constant(utf8Slice(node.getValue()), VARCHAR)); } return call( castSignature(getType(node), VARCHAR), getType(node), constant(utf8Slice(node.getValue()), VARCHAR)); }
private static Block createChannel(int positionCount, int mapSize, Type elementType) { BlockBuilder blockBuilder = JSON.createBlockBuilder(null, positionCount); for (int position = 0; position < positionCount; position++) { SliceOutput jsonSlice = new DynamicSliceOutput(20 * mapSize); jsonSlice.appendByte('['); for (int i = 0; i < mapSize; i++) { if (i != 0) { jsonSlice.appendByte(','); } String value = generateRandomJsonValue(elementType); jsonSlice.appendBytes(value.getBytes()); } jsonSlice.appendByte(']'); JSON.writeSlice(blockBuilder, jsonSlice.slice()); } return blockBuilder.build(); }
return (parser, blockBuilder) -> { String json = OBJECT_MAPPED_UNORDERED.writeValueAsString(parser.readValueAsTree()); JSON.writeSlice(blockBuilder, Slices.utf8Slice(json)); }; case StandardTypes.ARRAY:
@Setup public void setup() { Type elementType; switch (valueTypeName) { case "BIGINT": elementType = BIGINT; break; case "DOUBLE": elementType = DOUBLE; break; case "VARCHAR": elementType = VARCHAR; break; default: throw new UnsupportedOperationException(); } Signature signature = new Signature("$operator$CAST", FunctionKind.SCALAR, new ArrayType(elementType).getTypeSignature(), JSON.getTypeSignature()); List<RowExpression> projections = ImmutableList.of( new CallExpression(signature, new ArrayType(elementType), ImmutableList.of(field(0, JSON)))); MetadataManager metadata = MetadataManager.createTestMetadataManager(); pageProcessor = new ExpressionCompiler(metadata, new PageFunctionCompiler(metadata, 0)) .compilePageProcessor(Optional.empty(), projections) .get(); page = new Page(createChannel(POSITION_COUNT, ARRAY_SIZE, elementType)); }
@Override protected Type visitGenericLiteral(GenericLiteral node, StackableAstVisitorContext<AnalysisContext> context) { Type type = typeManager.getType(parseTypeSignature(node.getType())); if (type == null) { throw new SemanticException(TYPE_MISMATCH, node, "Unknown type: " + node.getType()); } if (!JSON.equals(type)) { try { functionRegistry.getCoercion(VARCHAR, type); } catch (IllegalArgumentException e) { throw new SemanticException(TYPE_MISMATCH, node, "No literal form for type %s", type); } } expressionTypes.put(node, type); return type; }
@Override public void writeJsonValue(JsonGenerator jsonGenerator, Block block, int position, ConnectorSession session) throws IOException { if (block.isNull(position)) { jsonGenerator.writeNull(); } else { Slice value = JSON.getSlice(block, position); jsonGenerator.writeRawValue(value.toStringUtf8()); } } }
@Override void writeNextRandomValue(Random random, BlockBuilder builder) { String json = String.format("{\"%s\": %d, \"%s\": \"%s\"}", nextString(random, MAX_GENERATED_JSON_KEY_LENGTH), random.nextInt(), nextString(random, MAX_GENERATED_JSON_KEY_LENGTH), random.nextInt()); JSON.writeString(builder, json); } }
private static Block createChannel(int positionCount, int mapSize, Type valueType) { BlockBuilder blockBuilder = JSON.createBlockBuilder(null, positionCount); for (int position = 0; position < positionCount; position++) { SliceOutput jsonSlice = new DynamicSliceOutput(20 * mapSize); jsonSlice.appendByte('{'); for (int i = 0; i < mapSize; i++) { if (i != 0) { jsonSlice.appendByte(','); } String key = "key" + i; String value = generateRandomJsonValue(valueType); jsonSlice.appendByte('"'); jsonSlice.appendBytes(key.getBytes()); jsonSlice.appendBytes("\":".getBytes()); jsonSlice.appendBytes(value.getBytes()); } jsonSlice.appendByte('}'); JSON.writeSlice(blockBuilder, jsonSlice.slice()); } return blockBuilder.build(); }
public void writeString(BlockBuilder blockBuilder, String value) { writeSlice(blockBuilder, Slices.utf8Slice(value)); }
@Override protected RowExpression visitGenericLiteral(GenericLiteral node, Void context) { Type type = typeManager.getType(parseTypeSignature(node.getType())); if (type == null) { throw new IllegalArgumentException("Unsupported type: " + node.getType()); } if (JSON.equals(type)) { return call( new Signature("json_parse", SCALAR, types.get(node).getTypeSignature(), VARCHAR.getTypeSignature()), types.get(node), constant(utf8Slice(node.getValue()), VARCHAR)); } return call( castSignature(types.get(node), VARCHAR), types.get(node), constant(utf8Slice(node.getValue()), VARCHAR)); }