Skip to content

Commit

Permalink
[dingo-calcite] Add float that supports precision
Browse files Browse the repository at this point in the history
  • Loading branch information
jwcxs-m authored and JYcz committed Aug 9, 2023
1 parent 32e5186 commit f2b00ad
Show file tree
Hide file tree
Showing 19 changed files with 417 additions and 23 deletions.
5 changes: 5 additions & 0 deletions dingo-calcite/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ data: {
"org.apache.calcite.sql.SqlTimeLiteral"
"org.apache.calcite.sql.SqlTimestampLiteral"
"org.apache.calcite.sql.ddl.SqlDdlNodes"
"org.apache.calcite.sql.ddl.SqlFloatTypeNameSpec"
"org.apache.calcite.sql2rel.SqlLikeBinaryOperator"
"org.apache.calcite.sql2rel.SqlFunctionScanOperator"
"org.apache.calcite.util.NlsString"
Expand Down Expand Up @@ -191,6 +192,10 @@ data: {
"SqlAnalyze"
]

sqlTypeNameMethods: [
"SqlFloatTypeName"
]

# List of files in @includes directory that have parser method
# implementations for parsing custom SQL statements, literals or types
# given as part of "statementParserMethods", "literalParserMethods" or
Expand Down
22 changes: 22 additions & 0 deletions dingo-calcite/src/main/codegen/includes/parserImpls.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -1007,3 +1007,25 @@ SqlNode SqlTruncate() :
return new SqlTruncate(getPos(), id);
}
}

SqlTypeNameSpec SqlFloatTypeName(Span s) :
{
final SqlTypeNameSpec sqlTypeNameSpec;
}
{
<FLOAT>
{
s.add(this);
SqlTypeName sqlTypeName = SqlTypeName.FLOAT;
int precision = -1;
}
[
<LPAREN>
precision = UnsignedIntLiteral()
<RPAREN>
]
{
sqlTypeNameSpec = new SqlFloatTypeNameSpec(sqlTypeName, precision, s.end(this));
return sqlTypeNameSpec;
}
}
4 changes: 4 additions & 0 deletions dingo-calcite/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -5610,6 +5610,10 @@ SqlTypeNameSpec SqlTypeName(Span s) :
}
{
(
<#list (parser.sqlTypeNameMethods!) as method>
sqlTypeNameSpec = ${method}(s)
|
</#list>
sqlTypeNameSpec = SqlTypeName1(s)
|
sqlTypeNameSpec = SqlTypeName2(s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public Long convertLongFrom(@NonNull Object value) {
}

@Override
public Double convertFloatFrom(@NonNull Object value) {
return ((BigDecimal) value).doubleValue();
public Float convertFloatFrom(@NonNull Object value) {
return ((BigDecimal) value).floatValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2021 DataCanvas
*
* 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 org.apache.calcite;

import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.type.AbstractSqlType;
import org.apache.calcite.sql.type.SqlTypeName;
import org.checkerframework.checker.nullness.qual.Nullable;

public class DingoSqlFloatType extends AbstractSqlType {
private final int precision;

public DingoSqlFloatType(SqlTypeName typeName, int precision) {
super(typeName, false, null);
this.precision = precision;
}

@Override
protected void generateTypeString(StringBuilder sb, boolean withDetail) {

}

@Override
public boolean equalsSansFieldNames(@Nullable RelDataType that) {
return super.equalsSansFieldNames(that);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2021 DataCanvas
*
* 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 org.apache.calcite.sql.ddl;

import org.apache.calcite.DingoSqlFloatType;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlTypeNameSpec;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.validate.SqlValidator;
import org.apache.calcite.util.Litmus;

public class SqlFloatTypeNameSpec extends SqlTypeNameSpec {

private final SqlTypeName sqlTypeName;

private int precision;

public SqlFloatTypeNameSpec(SqlTypeName typeName, int precision, SqlParserPos pos) {
super(new SqlIdentifier(typeName.name(), pos), pos);
this.sqlTypeName = typeName;
this.precision = precision;
}

@Override
public RelDataType deriveType(SqlValidator validator) {
return new DingoSqlFloatType(sqlTypeName, precision);
}

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword(getTypeName().getSimple());
writer.keyword("(");
writer.keyword(String.valueOf(precision));
writer.keyword(")");
}

@Override
public boolean equalsDeep(SqlTypeNameSpec spec, Litmus litmus) {
return litmus.succeed();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,11 @@ default Long convertLongFrom(@NonNull Object value) {
return (Long) value;
}

default Double convertFloatFrom(@NonNull Object value) {
return convertDoubleFrom(value);
default Float convertFloatFrom(@NonNull Object value) {
return (Float) value;
}

default Double convertDoubleFrom(@NonNull Object value) {
if (value instanceof Float) {
return ((Float) value).doubleValue();
}
return (Double) value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public Long convertLongFrom(@NonNull Object value) {
}

@Override
public Double convertFloatFrom(@NonNull Object value) {
public Float convertFloatFrom(@NonNull Object value) {
if (value.toString().trim().isEmpty()) {
return null;
}
return Double.parseDouble(value.toString());
return Float.parseFloat(value.toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import io.dingodb.common.type.converter.DataConverter;
import io.dingodb.expr.core.TypeCode;
import io.dingodb.serial.schema.DingoSchema;
import io.dingodb.serial.schema.DoubleSchema;
import io.dingodb.serial.schema.FloatSchema;
import org.checkerframework.checker.nullness.qual.NonNull;

@JsonTypeName("float")
public class FloatType extends AbstractScalarType {
@JsonCreator
public FloatType(@JsonProperty("nullable") boolean nullable) {
super(TypeCode.DOUBLE, nullable);
super(TypeCode.FLOAT, nullable);
}

@Override
Expand All @@ -40,7 +40,7 @@ public FloatType copy() {

@Override
public DingoSchema toDingoSchema(int index) {
return new DoubleSchema(index);
return new FloatSchema(index);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.calcite.avatica.AvaticaParameter;
import org.apache.calcite.avatica.ColumnMetaData;
import org.apache.calcite.avatica.Meta;
import org.apache.calcite.avatica.SqlType;
import org.apache.calcite.jdbc.CalciteSchema;
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.rel.RelNode;
Expand Down Expand Up @@ -151,7 +152,7 @@ private static ColumnMetaData metaData(
true,
false,
false,
avaticaType.columnClassName()
avaticaType.id == SqlType.FLOAT.id ? "java.lang.Float" : avaticaType.columnClassName()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.calcite.avatica.AvaticaConnection;
import org.apache.calcite.avatica.util.AbstractCursor;
import org.apache.calcite.avatica.util.DingoAccessor;
import org.apache.calcite.avatica.AvaticaResultSet;
import org.apache.calcite.avatica.AvaticaStatement;
import org.apache.calcite.avatica.ColumnMetaData;
import org.apache.calcite.avatica.Meta;
import org.apache.calcite.avatica.QueryState;
import org.apache.calcite.avatica.util.Cursor;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;

@Slf4j
Expand Down Expand Up @@ -57,4 +64,46 @@ protected void cancel() {
public Meta.Signature getSignature() {
return signature;
}

@Override
public Object getObject(int columnIndex) throws SQLException {
try {
Object object;
switch (columnMetaDataList.get(columnIndex - 1).type.id) {
case Types.FLOAT:
object = accessorList.get(columnIndex - 1).getObject();
break;
default:
object = super.getObject(columnIndex);
break;
}
return object;
} catch (IndexOutOfBoundsException e) {
throw AvaticaConnection.HELPER.createException(
"invalid column ordinal: " + columnIndex);
}
}

@Override
protected AvaticaResultSet execute() throws SQLException {
super.execute();
for (int i = 0; i < this.columnMetaDataList.size(); i++) {
if (columnMetaDataList.get(i).type.id == Types.FLOAT) {
this.accessorList.set(i, new DingoAccessor.FloatAccessor((AbstractCursor) cursor, i));
}
}
return this;
}

@Override
public AvaticaResultSet execute2(Cursor cursor, List<ColumnMetaData> columnMetaDataList) {
super.execute2(cursor, columnMetaDataList);
for (int i = 0; i < this.columnMetaDataList.size(); i++) {
if (columnMetaDataList.get(i).type.id == Types.FLOAT) {
this.accessorList.set(i, new DingoAccessor.FloatAccessor((AbstractCursor) cursor, i));
}
}
return this;
}

}
Loading

0 comments on commit f2b00ad

Please sign in to comment.