use of io.confluent.ksql.schema.ksql.SqlTypeParser in project ksql by confluentinc.
the class SchemaParser method parse.
public TableElements parse(final String schema) {
if (schema.trim().isEmpty()) {
return TableElements.of();
}
final SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(CharStreams.fromString("(" + schema + ")")));
final CommonTokenStream tokStream = new CommonTokenStream(lexer);
final SqlBaseParser parser = new SqlBaseParser(tokStream);
final BaseErrorListener errorListener = new BaseErrorListener() {
@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
throw new KsqlException(String.format("Error parsing schema \"%s\" at %d:%d: %s", schema, line, charPositionInLine, msg), e);
}
};
lexer.removeErrorListeners();
lexer.addErrorListener(errorListener);
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
final SqlTypeParser typeParser = SqlTypeParser.create(typeRegistry);
final List<TableElement> elements = parser.tableElements().tableElement().stream().map(ctx -> new TableElement(getLocation(ctx), ColumnName.of(ParserUtil.getIdentifierText(ctx.identifier())), typeParser.getType(ctx.type()), ParserUtil.getColumnConstraints(ctx.columnConstraints()))).collect(Collectors.toList());
return TableElements.of(elements);
}
use of io.confluent.ksql.schema.ksql.SqlTypeParser in project ksql by confluentinc.
the class UdtfLoaderTest method shouldNotLoadUdtfWithWrongReturnValue.
@Test
public void shouldNotLoadUdtfWithWrongReturnValue() {
// Given:
final MutableFunctionRegistry functionRegistry = new InternalFunctionRegistry();
final SqlTypeParser typeParser = create(EMPTY);
final UdtfLoader udtfLoader = new UdtfLoader(functionRegistry, empty(), typeParser, true);
// When:
final Exception e = assertThrows(KsqlException.class, () -> udtfLoader.loadUdtfFromClass(UdtfBadReturnValue.class, INTERNAL_PATH));
// Then:
assertThat(e.getMessage(), containsString("UDTF functions must return a List. Class io.confluent.ksql" + ".function.UdtfLoaderTest$UdtfBadReturnValue Method badReturn"));
}
use of io.confluent.ksql.schema.ksql.SqlTypeParser in project ksql by confluentinc.
the class UdtfLoaderTest method shouldNotLoadUdtfWithBigDecimalReturnAndNoSchemaProvider.
@Test
public void shouldNotLoadUdtfWithBigDecimalReturnAndNoSchemaProvider() {
// Given:
final MutableFunctionRegistry functionRegistry = new InternalFunctionRegistry();
final SqlTypeParser typeParser = create(EMPTY);
final UdtfLoader udtfLoader = new UdtfLoader(functionRegistry, empty(), typeParser, true);
// When:
final Exception e = assertThrows(KsqlException.class, () -> udtfLoader.loadUdtfFromClass(BigDecimalNoSchemaProvider.class, INTERNAL_PATH));
// Then:
assertThat(e.getMessage(), containsString("Cannot load UDF bigDecimalNoSchemaProvider. DECIMAL return type is not supported without an explicit schema"));
}
use of io.confluent.ksql.schema.ksql.SqlTypeParser in project ksql by confluentinc.
the class UdtfLoaderTest method shouldNotLoadUdtfWithRawListReturn.
@Test
public void shouldNotLoadUdtfWithRawListReturn() {
// Given:
final MutableFunctionRegistry functionRegistry = new InternalFunctionRegistry();
final SqlTypeParser typeParser = create(EMPTY);
final UdtfLoader udtfLoader = new UdtfLoader(functionRegistry, empty(), typeParser, true);
// When:
final Exception e = assertThrows(KsqlException.class, () -> udtfLoader.loadUdtfFromClass(RawListReturn.class, INTERNAL_PATH));
// Then:
assertThat(e.getMessage(), containsString("UDTF functions must return a parameterized List. Class io.confluent.ksql.function.UdtfLoaderTest$RawListReturn Method badReturn"));
}
use of io.confluent.ksql.schema.ksql.SqlTypeParser in project ksql by confluentinc.
the class FunctionLoaderUtils method handleUdfReturnSchema.
// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
static SchemaProvider handleUdfReturnSchema(final Class theClass, final ParamType javaReturnSchema, final String annotationSchema, final SqlTypeParser parser, final String schemaProviderFunctionName, final String functionName, final boolean isVariadic) {
// CHECKSTYLE_RULES.ON: CyclomaticComplexity
final Function<List<SqlArgument>, SqlType> schemaProvider;
if (!Udf.NO_SCHEMA_PROVIDER.equals(schemaProviderFunctionName)) {
schemaProvider = handleUdfSchemaProviderAnnotation(schemaProviderFunctionName, theClass, functionName);
} else if (!Udf.NO_SCHEMA.equals(annotationSchema)) {
final SqlType sqlType = parser.parse(annotationSchema).getSqlType();
schemaProvider = args -> sqlType;
} else if (!GenericsUtil.hasGenerics(javaReturnSchema)) {
// it is important to do this eagerly and not in the lambda so that
// we can fail early (when loading the UDF) instead of when the user
// attempts to use the UDF
final SqlType sqlType = fromJavaType(javaReturnSchema, functionName);
schemaProvider = args -> sqlType;
} else {
schemaProvider = null;
}
return (parameters, arguments) -> {
if (schemaProvider != null) {
final SqlType returnType = schemaProvider.apply(arguments);
if (!(ParamTypes.areCompatible(SqlArgument.of(returnType), javaReturnSchema, false))) {
throw new KsqlException(String.format("Return type %s of UDF %s does not match the declared " + "return type %s.", returnType, functionName.toUpperCase(), SchemaConverters.functionToSqlConverter().toSqlType(javaReturnSchema)));
}
return returnType;
}
final Map<GenericType, SqlType> genericMapping = new HashMap<>();
for (int i = 0; i < Math.min(parameters.size(), arguments.size()); i++) {
final ParamType schema = parameters.get(i);
if (schema instanceof LambdaType) {
if (isVariadic && i == parameters.size() - 1) {
throw new KsqlException(String.format("Lambda function %s cannot be variadic.", arguments.get(i).toString()));
}
genericMapping.putAll(GenericsUtil.reserveGenerics(schema, arguments.get(i)));
} else {
// we resolve any variadic as if it were an array so that the type
// structure matches the input type
final SqlType instance = isVariadic && i == parameters.size() - 1 ? SqlTypes.array(arguments.get(i).getSqlTypeOrThrow()) : arguments.get(i).getSqlTypeOrThrow();
genericMapping.putAll(GenericsUtil.reserveGenerics(schema, SqlArgument.of(instance)));
}
}
return GenericsUtil.applyResolved(javaReturnSchema, genericMapping);
};
}
Aggregations