/** * Returns a SQL string representing the provided argument */ default String getFunctionCallArgumentString(Object argument, TestType argumentType) { if (argument == null) { return getNullLiteralString(); } else if (argument instanceof FunctionCall) { return getSqlFunctionCallString((FunctionCall) argument); } else if (argumentType instanceof UnknownTestType) { return getNullLiteralString(); } else if (argumentType instanceof IntegerTestType) { return getIntegerArgumentString((Integer) argument); } else if (argumentType instanceof LongTestType) { return getLongArgumentString((Long) argument); } else if (argumentType instanceof BooleanTestType) { return getBooleanArgumentString((Boolean) argument); } else if (argumentType instanceof StringTestType) { return getStringArgumentString((String) argument); } else if (argumentType instanceof ArrayTestType) { return getArrayArgumentString((List<Object>) argument, ((ArrayTestType) argumentType).getElementType()); } else if (argumentType instanceof MapTestType) { return getMapArgumentString((Map<Object, Object>) argument, ((MapTestType) argumentType).getKeyType(), ((MapTestType) argumentType).getValueType()); } else if (argumentType instanceof StructTestType) { return getStructArgumentString((Row) argument, ((StructTestType) argumentType).getFieldTypes()); } else { throw new UnsupportedOperationException("Unsupported data type: " + argumentType.getClass()); } }
/** * Returns a SQL string of the format {@code MAP((k1, v1), (k2, v2), (k3, v3), ...)} representing a map literal */ default String getMapArgumentString(Map<Object, Object> map, TestType mapKeyType, TestType mapValueType) { return "MAP" + "(" + map.entrySet() .stream() .map(entry -> "(" + getFunctionCallArgumentString(entry.getKey(), mapKeyType) + ", " + getFunctionCallArgumentString(entry.getValue(), mapValueType) + ")") .collect(Collectors.joining(", ")) + ")"; }
default void check(TestCase testCase) { assertFunctionCall(getSqlFunctionCallGenerator().getSqlFunctionCallString(testCase.getFunctionCall()), getToPlatformTestOutputConverter().convertToTestOutput(testCase.getExpectedOutput(), testCase.getInferredOutputType()), getPlatformType(testCase.getExpectedOutputType())); }
default void check(TestCase testCase) { assertFunctionCall(getSqlFunctionCallGenerator().getSqlFunctionCallString(testCase.getFunctionCall()), getToPlatformTestOutputConverter().convertToTestOutput(testCase.getExpectedOutput(), testCase.getInferredOutputType()), getPlatformType(testCase.getExpectedOutputType())); }
/** * Returns a SQL string representing the provided argument */ default String getFunctionCallArgumentString(Object argument, TestType argumentType) { if (argument == null) { return getNullLiteralString(); } else if (argument instanceof FunctionCall) { return getSqlFunctionCallString((FunctionCall) argument); } else if (argumentType instanceof UnknownTestType) { return getNullLiteralString(); } else if (argumentType instanceof IntegerTestType) { return getIntegerArgumentString((Integer) argument); } else if (argumentType instanceof LongTestType) { return getLongArgumentString((Long) argument); } else if (argumentType instanceof BooleanTestType) { return getBooleanArgumentString((Boolean) argument); } else if (argumentType instanceof StringTestType) { return getStringArgumentString((String) argument); } else if (argumentType instanceof ArrayTestType) { return getArrayArgumentString((List<Object>) argument, ((ArrayTestType) argumentType).getElementType()); } else if (argumentType instanceof MapTestType) { return getMapArgumentString((Map<Object, Object>) argument, ((MapTestType) argumentType).getKeyType(), ((MapTestType) argumentType).getValueType()); } else if (argumentType instanceof StructTestType) { return getStructArgumentString((Row) argument, ((StructTestType) argumentType).getFieldTypes()); } else { throw new UnsupportedOperationException("Unsupported data type: " + argumentType.getClass()); } }
/** * Returns a SQL string of the format {@code MAP((k1, v1), (k2, v2), (k3, v3), ...)} representing a map literal */ default String getMapArgumentString(Map<Object, Object> map, TestType mapKeyType, TestType mapValueType) { return "MAP" + "(" + map.entrySet() .stream() .map(entry -> "(" + getFunctionCallArgumentString(entry.getKey(), mapKeyType) + ", " + getFunctionCallArgumentString(entry.getValue(), mapValueType) + ")") .collect(Collectors.joining(", ")) + ")"; }
/** * Returns a SQL string of the format {@code ARRAY(ele1, ele2, ele3, ...)} representing an array literal */ default String getArrayArgumentString(List<Object> array, TestType arrayElementType) { return "ARRAY" + "(" + array.stream() .map(element -> getFunctionCallArgumentString(element, arrayElementType)) .collect(Collectors.joining(", ")) + ")"; }
/** * Returns a SQL string of the format {@code ARRAY(ele1, ele2, ele3, ...)} representing an array literal */ default String getArrayArgumentString(List<Object> array, TestType arrayElementType) { return "ARRAY" + "(" + array.stream() .map(element -> getFunctionCallArgumentString(element, arrayElementType)) .collect(Collectors.joining(", ")) + ")"; }
/** * Returns a SQL string of the format {@code STRUCT(f1, f2, f3, ...)} representing a struct literal */ default String getStructArgumentString(Row struct, List<TestType> structFieldTypes) { List<Object> structFields = struct.getFields(); return "STRUCT" + "(" + IntStream.range(0, structFields.size()) .mapToObj(idx -> getFunctionCallArgumentString(structFields.get(idx), structFieldTypes.get(idx))) .collect(Collectors.joining(", ")) + ")"; } }
/** * Returns a SQL string of the format {@code STRUCT(f1, f2, f3, ...)} representing a struct literal */ default String getStructArgumentString(Row struct, List<TestType> structFieldTypes) { List<Object> structFields = struct.getFields(); return "STRUCT" + "(" + IntStream.range(0, structFields.size()) .mapToObj(idx -> getFunctionCallArgumentString(structFields.get(idx), structFieldTypes.get(idx))) .collect(Collectors.joining(", ")) + ")"; } }
/** * Returns SQL function call string of the format {@code functionName(argument1, argument2, argument3, ...)} */ default String getSqlFunctionCallString(FunctionCall functionCall) { return functionCall.getFunctionName() + "(" + IntStream.range(0, functionCall.getParameters().size()) .mapToObj(idx -> getFunctionCallArgumentString(functionCall.getParameters().get(idx), functionCall.getInferredParameterTypes().get(idx))) .collect(Collectors.joining(", ")) + ")"; }
/** * Returns SQL function call string of the format {@code functionName(argument1, argument2, argument3, ...)} */ default String getSqlFunctionCallString(FunctionCall functionCall) { return functionCall.getFunctionName() + "(" + IntStream.range(0, functionCall.getParameters().size()) .mapToObj(idx -> getFunctionCallArgumentString(functionCall.getParameters().get(idx), functionCall.getInferredParameterTypes().get(idx))) .collect(Collectors.joining(", ")) + ")"; }