Skip to content

Commit

Permalink
[FLINK-12253][table-common] Add a SMALLINT type
Browse files Browse the repository at this point in the history
  • Loading branch information
twalthr committed May 2, 2019
1 parent 52229e1 commit a6fe3ff
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
Expand Up @@ -43,5 +43,7 @@ public interface LogicalTypeVisitor<R> {

R visit(TinyIntType tinyIntType);

R visit(SmallIntType smallIntType);

R visit(LogicalType other);
}
@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.flink.table.types.logical;

import org.apache.flink.annotation.PublicEvolving;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
* Logical type of a 2-byte signed integer with values from -32,768 to 32,767.
*
* <p>The serialized string representation is {@code SMALLINT}.
*/
@PublicEvolving
public final class SmallIntType extends LogicalType {

private static final String DEFAULT_FORMAT = "SMALLINT";

private static final Set<String> NULL_OUTPUT_CONVERSION = conversionSet(
Short.class.getName());

private static final Set<String> NOT_NULL_INPUT_OUTPUT_CONVERSION = conversionSet(
Short.class.getName(),
short.class.getName());

private static final Class<?> DEFAULT_CONVERSION = Short.class;

public SmallIntType(boolean isNullable) {
super(isNullable, LogicalTypeRoot.SMALLINT);
}

public SmallIntType() {
this(true);
}

@Override
public LogicalType copy(boolean isNullable) {
return new SmallIntType(isNullable);
}

@Override
public String asSerializableString() {
return withNullability(DEFAULT_FORMAT);
}

@Override
public boolean supportsInputConversion(Class<?> clazz) {
return NOT_NULL_INPUT_OUTPUT_CONVERSION.contains(clazz.getName());
}

@Override
public boolean supportsOutputConversion(Class<?> clazz) {
if (isNullable()) {
return NULL_OUTPUT_CONVERSION.contains(clazz.getName());
}
return NOT_NULL_INPUT_OUTPUT_CONVERSION.contains(clazz.getName());
}

@Override
public Class<?> getDefaultOutputConversion() {
return DEFAULT_CONVERSION;
}

@Override
public List<LogicalType> getChildren() {
return Collections.emptyList();
}

@Override
public <R> R accept(LogicalTypeVisitor<R> visitor) {
return visitor.visit(this);
}
}
Expand Up @@ -23,6 +23,7 @@
import org.apache.flink.table.types.logical.CharType;
import org.apache.flink.table.types.logical.DecimalType;
import org.apache.flink.table.types.logical.LogicalType;
import org.apache.flink.table.types.logical.SmallIntType;
import org.apache.flink.table.types.logical.TinyIntType;
import org.apache.flink.table.types.logical.VarBinaryType;
import org.apache.flink.table.types.logical.VarCharType;
Expand Down Expand Up @@ -129,6 +130,18 @@ public void testTinyIntType() {
);
}

@Test
public void testSmallIntType() {
testAll(
new SmallIntType(),
"SMALLINT",
new Class[]{Short.class},
new Class[]{Short.class},
new LogicalType[]{},
new SmallIntType(false)
);
}

// --------------------------------------------------------------------------------------------

private static void testAll(
Expand Down

0 comments on commit a6fe3ff

Please sign in to comment.