Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid NPE if not function exists that handles required types. #1407

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,6 +25,7 @@

import io.confluent.ksql.function.udf.Kudf;
import io.confluent.ksql.util.KsqlException;
import io.confluent.ksql.util.SchemaUtil;

public class UdfFactory {
private final String name;
Expand Down Expand Up @@ -74,6 +75,16 @@ public String toString() {
}

public KsqlFunction getFunction(final List<Schema.Type> paramTypes) {
return functions.get(paramTypes);
final KsqlFunction function = functions.get(paramTypes);
if (function != null) {
return function;
}

final String sqlParamTypes = paramTypes.stream()
.map(SchemaUtil::getSchemaTypeAsSqlType)
.collect(Collectors.joining(", ", "[", "]"));

throw new KsqlException("Function '" + name
+ "' does not accept parameters of types:" + sqlParamTypes);
}
}
Expand Up @@ -191,6 +191,15 @@ public static Schema buildSchemaWithAlias(final Schema schema, final String alia
.put("MAP", "MAP")
.build();

public static String getSchemaTypeAsSqlType(final Schema.Type type) {
final String sqlType = TYPE_MAP.get(type.name());
if (sqlType == null) {
throw new IllegalArgumentException("Unknown schema type: " + type);
}

return sqlType;
}

public static String getSchemaFieldType(final Field field) {
if (field.schema().type() == Schema.Type.ARRAY) {
return "ARRAY[" + getSchemaFieldType(field.schema().valueSchema().fields().get(0)) + "]";
Expand Down
@@ -0,0 +1,53 @@
/*
* Copyright 2018 Confluent Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.confluent.ksql.function;

import com.google.common.collect.ImmutableList;

import org.apache.kafka.common.KafkaException;
import org.apache.kafka.connect.data.Schema;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import io.confluent.ksql.function.udf.Kudf;

public class UdfFactoryTest {

@Rule
public final ExpectedException expectedException = ExpectedException.none();

private UdfFactory factory;

@Before
public void setUp() throws Exception {
factory = new UdfFactory("TestFunc", TestFunc.class);
}

@Test
public void shouldThrowIfNoVariantFoundThatAcceptsSuppliedParamTypes() {
expectedException.expect(KafkaException.class);
expectedException.expectMessage("Function 'TestFunc' does not accept parameters of types:[VARCHAR(STRING), BIGINT]");

factory.getFunction(ImmutableList.of(Schema.Type.STRING, Schema.Type.INT64));
}

private abstract class TestFunc implements Kudf {

}
}
Expand Up @@ -28,6 +28,7 @@
import java.util.Optional;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;

Expand Down Expand Up @@ -376,6 +377,20 @@ public void shouldGetCorrectSqlType() {
assertThat("Invalid SQL type.", sqlType7, equalTo("MAP<VARCHAR,DOUBLE>"));
}

@Test
public void shouldGetCorrectSqlTypeFromSchemaType() {
assertThat(SchemaUtil.getSchemaTypeAsSqlType(Schema.Type.STRING), is("VARCHAR(STRING)"));
assertThat(SchemaUtil.getSchemaTypeAsSqlType(Schema.Type.INT64), is("BIGINT"));
assertThat(SchemaUtil.getSchemaTypeAsSqlType(Schema.Type.INT32), is("INTEGER"));
assertThat(SchemaUtil.getSchemaTypeAsSqlType(Schema.Type.FLOAT64), is("DOUBLE"));
assertThat(SchemaUtil.getSchemaTypeAsSqlType(Schema.Type.BOOLEAN), is("BOOLEAN"));
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowOnUnknownSchemaType() {
SchemaUtil.getSchemaTypeAsSqlType(Schema.Type.STRUCT);
}

@Test
public void shouldStripAliasFromFieldName() {
Schema schemaWithAlias = SchemaUtil.buildSchemaWithAlias(schema, "alias");
Expand Down