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

[CALCITE-6565] Invalid unparse for CHAR without precision in MssqlSqlDialect #3944

Merged
merged 1 commit into from
Sep 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public abstract class RelDataTypeSystemImpl implements RelDataTypeSystem {
case INTERVAL_SECOND:
return SqlTypeName.MAX_INTERVAL_FRACTIONAL_SECOND_PRECISION;
default:
return -1;
return RelDataType.SCALE_NOT_SPECIFIED;
}
}

Expand Down Expand Up @@ -114,7 +114,7 @@ public abstract class RelDataTypeSystemImpl implements RelDataTypeSystem {
// (microseconds) per SQL99 part 2 section 6.1 syntax rule 30.
return 0;
default:
return -1;
return RelDataType.PRECISION_NOT_SPECIFIED;
}
}

Expand Down Expand Up @@ -226,7 +226,7 @@ public abstract class RelDataTypeSystemImpl implements RelDataTypeSystem {

@Override public int getNumTypeRadix(SqlTypeName typeName) {
if (typeName.getFamily() == SqlTypeFamily.NUMERIC
&& getDefaultPrecision(typeName) != -1) {
&& getDefaultPrecision(typeName) != RelDataType.PRECISION_NOT_SPECIFIED) {
return 10;
}
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import org.apache.calcite.avatica.util.TimeUnitRange;
import org.apache.calcite.config.NullCollation;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.rel.type.RelDataTypeSystemImpl;
import org.apache.calcite.sql.SqlAbstractDateTimeLiteral;
import org.apache.calcite.sql.SqlBasicFunction;
import org.apache.calcite.sql.SqlCall;
Expand All @@ -37,6 +39,7 @@
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlTypeName;

import org.checkerframework.checker.nullness.qual.Nullable;

Expand All @@ -47,9 +50,22 @@
* database.
*/
public class MssqlSqlDialect extends SqlDialect {
/**
* Mssql type system.
*/
public static final RelDataTypeSystem MSSQL_TYPE_SYSTEM =
new RelDataTypeSystemImpl() {
@Override public int getDefaultPrecision(SqlTypeName typeName) {
if (typeName == SqlTypeName.CHAR) {
return RelDataType.PRECISION_NOT_SPECIFIED;
}
return super.getDefaultPrecision(typeName);
}
};
public static final Context DEFAULT_CONTEXT = SqlDialect.EMPTY_CONTEXT
.withDatabaseProduct(SqlDialect.DatabaseProduct.MSSQL)
.withIdentifierQuoteString("[")
.withDataTypeSystem(MSSQL_TYPE_SYSTEM)
.withCaseSensitive(false)
.withNullCollation(NullCollation.LOW);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3684,12 +3684,15 @@ private SqlDialect nonOrdinalDialect() {
String query = "select cast(\"product_id\" as char) from \"product\"";
final String expectedMysql = "SELECT CAST(`product_id` AS CHAR)\n"
+ "FROM `foodmart`.`product`";
final String expectedMssql = "SELECT CAST([product_id] AS CHAR)\n"
+ "FROM [foodmart].[product]";
final String expectedHive = "SELECT CAST(`product_id` AS CHAR(1))\n"
+ "FROM `foodmart`.`product`";
final String expectedSpark = "SELECT CAST(`product_id` AS CHAR(1))\n"
+ "FROM `foodmart`.`product`";
sql(query)
.withMysql().ok(expectedMysql)
.withMssql().ok(expectedMssql)
.withHive().ok(expectedHive)
.withSpark().ok(expectedSpark);
}
Expand All @@ -3698,12 +3701,15 @@ private SqlDialect nonOrdinalDialect() {
String query = "select cast(\"product_id\" as char(5)) from \"product\"";
final String expectedMysql = "SELECT CAST(`product_id` AS CHAR(5))\n"
+ "FROM `foodmart`.`product`";
final String expectedMssql = "SELECT CAST([product_id] AS CHAR(5))\n"
+ "FROM [foodmart].[product]";
final String expectedHive = "SELECT CAST(`product_id` AS CHAR(5))\n"
+ "FROM `foodmart`.`product`";
final String expectedSpark = "SELECT CAST(`product_id` AS CHAR(5))\n"
+ "FROM `foodmart`.`product`";
sql(query)
.withMysql().ok(expectedMysql)
.withMssql().ok(expectedMssql)
.withHive().ok(expectedHive)
.withSpark().ok(expectedSpark);
}
Expand Down
Loading