Skip to content

Commit

Permalink
minor: Display a helpful message to the user if the types passed to a…
Browse files Browse the repository at this point in the history
… function don't match ones the function supports. (#1407)

Current a statement such as `select concat('foo',1) from blah;` results in NPE:

`java.lang.NullPointerException`

With this change it results in:

`Function 'CONCAT' does not accept parameters of types:[VARCHAR(STRINT), BIGINT]`
  • Loading branch information
big-andy-coates committed Jun 11, 2018
1 parent a562e8a commit cecc51a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
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 @@ -192,6 +192,15 @@ public static Schema buildSchemaWithAlias(final Schema schema, final String alia
.put("STRUCT", "STRUCT")
.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

0 comments on commit cecc51a

Please sign in to comment.