You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2021/10/08 15:50:27 UTC
[GitHub] [calcite] xwkuang5 opened a new pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
xwkuang5 opened a new pull request #2572:
URL: https://github.com/apache/calcite/pull/2572
- Introduce a new `NUMERIC` type in CsvFieldType and FileFieldType for
more accurate representation of decimal data.
- Map `NUMERIC` type to Java BigDecimal type.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] xwkuang5 commented on a change in pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#discussion_r725174869
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/FileRowConverter.java
##########
@@ -52,13 +53,13 @@
// row parser configuration
private final List<FieldDef> fields = new ArrayList<>();
- /** Format for parsing numbers. Not thread-safe, but we assume that only
- * one thread uses this converter at a time. */
+ /** Format for parsing numbers. Not thread-safe, but we assume that only one thread uses this
Review comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] jacques-n closed pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
jacques-n closed pull request #2572:
URL: https://github.com/apache/calcite/pull/2572
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] jacques-n commented on pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
jacques-n commented on pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#issuecomment-949829918
If I understand @julianhyde 's comment correctly in the jira, the weirdness here is that you're saying this is a numeric but then numeric (a non-declared scale/precision decimal) doesn't exist in Calcite. If this is getting coerced into a decimal type (with declared scale/precision), it isn't clear to me how this is happening and where you test cases check for this. Unless I'm misunderstanding, it seems like you either need to:
- annotate the header with a specific scale/precision in the csv (e.g. `DECIMAL(30,2)`)
- choose a standard scale/precision irrespective of the data and truncate or throw when the data is outside that range.
- pre-scan all the values to come up with a sufficient scale/precision to support the values in the csv you are reading.
Right now, it isn't clear to me what specifically is happening (and I'm not following how your test cases would clarify it).
That being said, I haven't gone back and reviewed the intricacies of Calcite and SQL Decimals so I may be missing something...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] jacques-n commented on pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
jacques-n commented on pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#issuecomment-973106521
Merged in 065e323da89ca73a46bec195efc7bd4019dc4f2f
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] jacques-n commented on a change in pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
jacques-n commented on a change in pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#discussion_r744302143
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +129,108 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(SqlTypeName.DECIMAL,
+ precision, scale), true);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(typeFactory.createJavaType(String.class).getSqlTypeName()),
+ true);
+ break;
+ case "boolean":
+ fieldType = typeFactory.createTypeWithNullability(
Review comment:
This pattern seems very redundant. It seems like 99% of the time, the pattern is string => sqlTypeName (everything except decimal). Can your refactor to avoid all the repetition?
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -284,6 +361,17 @@ static CSVReader openCsv(Source source) throws IOException {
return null;
}
return Double.parseDouble(string);
+ case DECIMAL:
+ if (string.length() == 0) {
+ return null;
+ }
+ BigDecimal result = new BigDecimal(string);
+ if (result.precision() > fieldType.getPrecision() || result.scale() != fieldType
+ .getScale()) {
+ throw new IllegalArgumentException(
Review comment:
Does throwing make sense here? Or should we coerce?
##########
File path: file/src/test/java/org/apache/calcite/adapter/file/FileAdapterTest.java
##########
@@ -619,6 +621,72 @@ private String range(int first, int count) {
return sb.append(')').toString();
}
+ @Test void testDecimalType() {
+ sql("sales-csv", "select BUDGET from sales.\"DECIMAL\"")
+ .checking(resultSet -> {
+ try {
+ ResultSetMetaData metaData = resultSet.getMetaData();
+ assertEquals("DECIMAL", metaData.getColumnTypeName(1));
+ assertEquals(18, metaData.getPrecision(1));
+ assertEquals(2, metaData.getScale(1));
+ } catch (SQLException e) {
+ throw TestUtil.rethrow(e);
+ }
+ })
+ .ok();
+ }
+
+ @Test void testDecimalTypeArithmeticOperations() {
Review comment:
Can you also test cases for negative cases and out of space numbers? For example a CSV has 0.0000000000001 or 10000 but the it is declared as DECIMAL(4,1).
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +129,108 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(SqlTypeName.DECIMAL,
+ precision, scale), true);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(typeFactory.createJavaType(String.class).getSqlTypeName()),
Review comment:
Please just use the SqlTypeName enum directly as opposed to the intermediate step of createJavaType()
##########
File path: file/src/test/resources/sales-csv/DECIMAL.csv
##########
@@ -0,0 +1,4 @@
+DEPTNO:int,BUDGET:decimal(18/2)
Review comment:
The standard pattern in most sql systems is a comma between scale and precision. It's really weird that you're using a slash. Can you make this a comma?
##########
File path: core/src/main/java/org/apache/calcite/sql/type/SqlTypeFactoryImpl.java
##########
@@ -84,6 +84,10 @@ public SqlTypeFactoryImpl(RelDataTypeSystem typeSystem) {
|| (precision == RelDataType.PRECISION_NOT_SPECIFIED);
final int maxPrecision = typeSystem.getMaxPrecision(typeName);
if (maxPrecision >= 0 && precision > maxPrecision) {
+ System.out.println("WARNING: specified precision (" + precision
Review comment:
You shouldn't add this here. There are several reasons:
- This path is used by many pieces of code.
- We should never println in core paths to warn someone of something. It could make sense to add a logging invocation here. However, I'm not sure it does. If we thought that made sense, I think you should do it in a separate PR since it is pretty unrelated to this change. If you want to capture and warn that a field in the csv is out of line with what is allowed, you should do that in the csv connector itself, not somewhere in the core module.
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +129,108 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(SqlTypeName.DECIMAL,
+ precision, scale), true);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(typeFactory.createJavaType(String.class).getSqlTypeName()),
+ true);
+ break;
+ case "boolean":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.BOOLEAN.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "byte":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.BYTE.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "char":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.CHAR.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "short":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.SHORT.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "int":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.INT.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "long":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.LONG.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "float":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.FLOAT.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "double":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.DOUBLE.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "date":
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(typeFactory.createJavaType(java.sql.Date.class)
+ .getSqlTypeName()),
+ true);
+ break;
+ case "time":
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(typeFactory.createJavaType(java.sql.Time.class)
+ .getSqlTypeName()),
+ true);
+ break;
+ case "timestamp":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(java.sql.Timestamp.class).getSqlTypeName()),
+ true);
+ break;
+ default:
+ System.out.println("WARNING: Found unknown type: "
Review comment:
I think this should be converted into a logging statement as opposed to a system.out (I know it was here before this change but we should clean this kind of thing up when working on the code.)
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -284,6 +361,17 @@ static CSVReader openCsv(Source source) throws IOException {
return null;
}
return Double.parseDouble(string);
+ case DECIMAL:
+ if (string.length() == 0) {
+ return null;
+ }
+ BigDecimal result = new BigDecimal(string);
+ if (result.precision() > fieldType.getPrecision() || result.scale() != fieldType
+ .getScale()) {
+ throw new IllegalArgumentException(
Review comment:
Also, best to use string format for this kind of thing as opposed to string concatenation.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] xwkuang5 commented on pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#issuecomment-956333238
Done. Added the abilities to specify `decimal(precision/scale)` in the CSV header. PTAL, thank!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] xwkuang5 commented on a change in pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#discussion_r746204190
##########
File path: core/src/main/java/org/apache/calcite/sql/type/SqlTypeFactoryImpl.java
##########
@@ -84,6 +84,10 @@ public SqlTypeFactoryImpl(RelDataTypeSystem typeSystem) {
|| (precision == RelDataType.PRECISION_NOT_SPECIFIED);
final int maxPrecision = typeSystem.getMaxPrecision(typeName);
if (maxPrecision >= 0 && precision > maxPrecision) {
+ System.out.println("WARNING: specified precision (" + precision
Review comment:
Removed for now.
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +129,108 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(SqlTypeName.DECIMAL,
+ precision, scale), true);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(typeFactory.createJavaType(String.class).getSqlTypeName()),
Review comment:
Done.
##########
File path: file/src/test/java/org/apache/calcite/adapter/file/FileAdapterTest.java
##########
@@ -619,6 +621,72 @@ private String range(int first, int count) {
return sb.append(')').toString();
}
+ @Test void testDecimalType() {
+ sql("sales-csv", "select BUDGET from sales.\"DECIMAL\"")
+ .checking(resultSet -> {
+ try {
+ ResultSetMetaData metaData = resultSet.getMetaData();
+ assertEquals("DECIMAL", metaData.getColumnTypeName(1));
+ assertEquals(18, metaData.getPrecision(1));
+ assertEquals(2, metaData.getScale(1));
+ } catch (SQLException e) {
+ throw TestUtil.rethrow(e);
+ }
+ })
+ .ok();
+ }
+
+ @Test void testDecimalTypeArithmeticOperations() {
Review comment:
Done. This has been added in the new CsvEnumeratorTest file as part of testing the parseDecimal method.
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +129,108 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(SqlTypeName.DECIMAL,
+ precision, scale), true);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(typeFactory.createJavaType(String.class).getSqlTypeName()),
+ true);
+ break;
+ case "boolean":
+ fieldType = typeFactory.createTypeWithNullability(
Review comment:
Refactored to a helper method now.
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +129,108 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(SqlTypeName.DECIMAL,
+ precision, scale), true);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(typeFactory.createJavaType(String.class).getSqlTypeName()),
+ true);
+ break;
+ case "boolean":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.BOOLEAN.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "byte":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.BYTE.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "char":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.CHAR.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "short":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.SHORT.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "int":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.INT.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "long":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.LONG.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "float":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.FLOAT.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "double":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(Primitive.DOUBLE.getBoxClass()).getSqlTypeName()),
+ true);
+ break;
+ case "date":
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(typeFactory.createJavaType(java.sql.Date.class)
+ .getSqlTypeName()),
+ true);
+ break;
+ case "time":
+ fieldType = typeFactory.createTypeWithNullability(typeFactory
+ .createSqlType(typeFactory.createJavaType(java.sql.Time.class)
+ .getSqlTypeName()),
+ true);
+ break;
+ case "timestamp":
+ fieldType = typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(
+ typeFactory.createJavaType(java.sql.Timestamp.class).getSqlTypeName()),
+ true);
+ break;
+ default:
+ System.out.println("WARNING: Found unknown type: "
Review comment:
Done.
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -284,6 +361,17 @@ static CSVReader openCsv(Source source) throws IOException {
return null;
}
return Double.parseDouble(string);
+ case DECIMAL:
+ if (string.length() == 0) {
+ return null;
+ }
+ BigDecimal result = new BigDecimal(string);
+ if (result.precision() > fieldType.getPrecision() || result.scale() != fieldType
+ .getScale()) {
+ throw new IllegalArgumentException(
Review comment:
Done. Based coercion on PostgreSQL's implementation https://www.postgresql.org/docs/13/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL.
##########
File path: file/src/test/resources/sales-csv/DECIMAL.csv
##########
@@ -0,0 +1,4 @@
+DEPTNO:int,BUDGET:decimal(18/2)
Review comment:
Done. Added double quote to disambiguate between column separator and the precision and scale separator.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] xwkuang5 commented on a change in pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#discussion_r747052021
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +137,70 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = parseDecimalSqlType(typeFactory, precision, scale);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.VARCHAR);
+ break;
+ case "boolean":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BOOLEAN);
+ break;
+ case "byte":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TINYINT);
+ break;
+ case "char":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.CHAR);
+ break;
+ case "short":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.SMALLINT);
+ break;
+ case "int":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.INTEGER);
+ break;
+ case "long":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BIGINT);
+ break;
+ case "float":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.REAL);
+ break;
+ case "double":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DOUBLE);
+ break;
+ case "date":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DATE);
+ break;
+ case "timestamp":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIMESTAMP);
+ break;
+ case "time":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIME);
+ break;
+ default:
+ LOGGER.warn(
+ String.format(Locale.ROOT,
+ "Found unknown type: %s in file: %s for column: %s. Will assume the type of "
Review comment:
Done.
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +137,70 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = parseDecimalSqlType(typeFactory, precision, scale);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.VARCHAR);
+ break;
+ case "boolean":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BOOLEAN);
+ break;
+ case "byte":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TINYINT);
+ break;
+ case "char":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.CHAR);
+ break;
+ case "short":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.SMALLINT);
+ break;
+ case "int":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.INTEGER);
+ break;
+ case "long":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BIGINT);
+ break;
+ case "float":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.REAL);
+ break;
+ case "double":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DOUBLE);
+ break;
+ case "date":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DATE);
+ break;
+ case "timestamp":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIMESTAMP);
+ break;
+ case "time":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIME);
+ break;
+ default:
+ LOGGER.warn(
+ String.format(Locale.ROOT,
+ "Found unknown type: %s in file: %s for column: %s. Will assume the type of "
+ + "column is string.",
+ typeString, source.path(), name));
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.VARCHAR);
Review comment:
The current code does not set a max on the varchar either. They used the default in the type system https://github.com/apache/calcite/blob/6d51d277b158ff7073f29ada4a96a7a74c0b46fc/core/src/main/java/org/apache/calcite/rel/type/RelDataTypeSystemImpl.java#L62.
This is a problem for file adapter since there is no validation for any of the types' precision during enumeration (except for the decimal type being added here). I think this should be addressed in a different PR.
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -314,22 +371,55 @@ static CSVReader openCsv(Source source) throws IOException {
} catch (ParseException e) {
return null;
}
- case STRING:
+ case VARCHAR:
default:
return string;
}
}
}
+ private static RelDataType parseDecimalSqlType(JavaTypeFactory typeFactory, int precision,
+ int scale) {
+ if (scale < 0) {
Review comment:
Done. The following should be enough:
1. precision > 0: 0 has precision 1 and scale 0
2. scale >= 0
3. precision >= scale
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] xwkuang5 commented on pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#issuecomment-947194050
@jacques-n Can you help review and approve this PR? Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] jacques-n commented on a change in pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
jacques-n commented on a change in pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#discussion_r747009579
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +137,70 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = parseDecimalSqlType(typeFactory, precision, scale);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.VARCHAR);
+ break;
+ case "boolean":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BOOLEAN);
+ break;
+ case "byte":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TINYINT);
+ break;
+ case "char":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.CHAR);
+ break;
+ case "short":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.SMALLINT);
+ break;
+ case "int":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.INTEGER);
+ break;
+ case "long":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BIGINT);
+ break;
+ case "float":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.REAL);
+ break;
+ case "double":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DOUBLE);
+ break;
+ case "date":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DATE);
+ break;
+ case "timestamp":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIMESTAMP);
+ break;
+ case "time":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIME);
+ break;
+ default:
+ LOGGER.warn(
+ String.format(Locale.ROOT,
+ "Found unknown type: %s in file: %s for column: %s. Will assume the type of "
Review comment:
when using a logger, you should use the loggers capability for value replacement as opposed to string format.
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +137,70 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = parseDecimalSqlType(typeFactory, precision, scale);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.VARCHAR);
+ break;
+ case "boolean":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BOOLEAN);
+ break;
+ case "byte":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TINYINT);
+ break;
+ case "char":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.CHAR);
+ break;
+ case "short":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.SMALLINT);
+ break;
+ case "int":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.INTEGER);
+ break;
+ case "long":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BIGINT);
+ break;
+ case "float":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.REAL);
+ break;
+ case "double":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DOUBLE);
+ break;
+ case "date":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DATE);
+ break;
+ case "timestamp":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIMESTAMP);
+ break;
+ case "time":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIME);
+ break;
+ default:
+ LOGGER.warn(
+ String.format(Locale.ROOT,
+ "Found unknown type: %s in file: %s for column: %s. Will assume the type of "
+ + "column is string.",
+ typeString, source.path(), name));
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.VARCHAR);
Review comment:
Shouldn't you set a max length on this varchar? (same for the one below)
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -314,22 +371,55 @@ static CSVReader openCsv(Source source) throws IOException {
} catch (ParseException e) {
return null;
}
- case STRING:
+ case VARCHAR:
default:
return string;
}
}
}
+ private static RelDataType parseDecimalSqlType(JavaTypeFactory typeFactory, int precision,
+ int scale) {
+ if (scale < 0) {
Review comment:
It seems like you should do some other checks here as well. For example, can precision be negative? What are the requirements for a relationship between precision and scale?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] jacques-n commented on a change in pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
jacques-n commented on a change in pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#discussion_r747054049
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +137,70 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = parseDecimalSqlType(typeFactory, precision, scale);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.VARCHAR);
+ break;
+ case "boolean":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BOOLEAN);
+ break;
+ case "byte":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TINYINT);
+ break;
+ case "char":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.CHAR);
+ break;
+ case "short":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.SMALLINT);
+ break;
+ case "int":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.INTEGER);
+ break;
+ case "long":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BIGINT);
+ break;
+ case "float":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.REAL);
+ break;
+ case "double":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DOUBLE);
+ break;
+ case "date":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DATE);
+ break;
+ case "timestamp":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIMESTAMP);
+ break;
+ case "time":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIME);
+ break;
+ default:
+ LOGGER.warn(
+ String.format(Locale.ROOT,
+ "Found unknown type: %s in file: %s for column: %s. Will assume the type of "
Review comment:
Usually loggers use {}. Isn't that what should be used (instead of %s?)
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +137,70 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = parseDecimalSqlType(typeFactory, precision, scale);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.VARCHAR);
+ break;
+ case "boolean":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BOOLEAN);
+ break;
+ case "byte":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TINYINT);
+ break;
+ case "char":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.CHAR);
+ break;
+ case "short":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.SMALLINT);
+ break;
+ case "int":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.INTEGER);
+ break;
+ case "long":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BIGINT);
+ break;
+ case "float":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.REAL);
+ break;
+ case "double":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DOUBLE);
+ break;
+ case "date":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DATE);
+ break;
+ case "timestamp":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIMESTAMP);
+ break;
+ case "time":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIME);
+ break;
+ default:
+ LOGGER.warn(
+ String.format(Locale.ROOT,
+ "Found unknown type: %s in file: %s for column: %s. Will assume the type of "
+ + "column is string.",
+ typeString, source.path(), name));
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.VARCHAR);
Review comment:
makes sense.
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -314,22 +371,55 @@ static CSVReader openCsv(Source source) throws IOException {
} catch (ParseException e) {
return null;
}
- case STRING:
+ case VARCHAR:
default:
return string;
}
}
}
+ private static RelDataType parseDecimalSqlType(JavaTypeFactory typeFactory, int precision,
+ int scale) {
+ if (scale < 0) {
Review comment:
Do you want to update these to use Preconditions? I think it has convenience methods to use format strings (although I think it simplifies to %s for everything).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] xwkuang5 commented on a change in pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#discussion_r747182503
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -314,22 +371,55 @@ static CSVReader openCsv(Source source) throws IOException {
} catch (ParseException e) {
return null;
}
- case STRING:
+ case VARCHAR:
default:
return string;
}
}
}
+ private static RelDataType parseDecimalSqlType(JavaTypeFactory typeFactory, int precision,
+ int scale) {
+ if (scale < 0) {
Review comment:
Done.
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -130,30 +137,70 @@ public static RelDataType deduceRowType(JavaTypeFactory typeFactory,
}
for (String string : strings) {
final String name;
- final CsvFieldType fieldType;
+ final RelDataType fieldType;
final int colon = string.indexOf(':');
if (colon >= 0) {
name = string.substring(0, colon);
String typeString = string.substring(colon + 1);
- fieldType = CsvFieldType.of(typeString);
- if (fieldType == null) {
- System.out.println("WARNING: Found unknown type: "
- + typeString + " in file: " + source.path()
- + " for column: " + name
- + ". Will assume the type of column is string");
+ Matcher decimalMatcher = DECIMAL_TYPE_PATTERN.matcher(typeString);
+ if (decimalMatcher.matches()) {
+ int precision = Integer.parseInt(decimalMatcher.group(1));
+ int scale = Integer.parseInt(decimalMatcher.group(2));
+ fieldType = parseDecimalSqlType(typeFactory, precision, scale);
+ } else {
+ switch (typeString) {
+ case "string":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.VARCHAR);
+ break;
+ case "boolean":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BOOLEAN);
+ break;
+ case "byte":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TINYINT);
+ break;
+ case "char":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.CHAR);
+ break;
+ case "short":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.SMALLINT);
+ break;
+ case "int":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.INTEGER);
+ break;
+ case "long":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.BIGINT);
+ break;
+ case "float":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.REAL);
+ break;
+ case "double":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DOUBLE);
+ break;
+ case "date":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.DATE);
+ break;
+ case "timestamp":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIMESTAMP);
+ break;
+ case "time":
+ fieldType = toNullableRelDataType(typeFactory, SqlTypeName.TIME);
+ break;
+ default:
+ LOGGER.warn(
+ String.format(Locale.ROOT,
+ "Found unknown type: %s in file: %s for column: %s. Will assume the type of "
Review comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] xwkuang5 commented on pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#issuecomment-966574666
> LGTM, will merge after clean test runs. Nice work @xwkuang5 !
Thank you for the careful review and guidance!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] xwkuang5 commented on pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#issuecomment-956333238
Done. Added the abilities to specify `decimal(precision/scale)` in the CSV header. PTAL, thank!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] xwkuang5 commented on a change in pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#discussion_r746066745
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -284,6 +361,17 @@ static CSVReader openCsv(Source source) throws IOException {
return null;
}
return Double.parseDouble(string);
+ case DECIMAL:
+ if (string.length() == 0) {
+ return null;
+ }
+ BigDecimal result = new BigDecimal(string);
+ if (result.precision() > fieldType.getPrecision() || result.scale() != fieldType
+ .getScale()) {
+ throw new IllegalArgumentException(
Review comment:
Done. Based coercion on PostgreSQL's implementation https://www.postgresql.org/docs/13/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] jacques-n commented on a change in pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
jacques-n commented on a change in pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#discussion_r747191798
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -314,22 +372,56 @@ static CSVReader openCsv(Source source) throws IOException {
} catch (ParseException e) {
return null;
}
- case STRING:
+ case VARCHAR:
default:
return string;
}
}
}
+ private static RelDataType parseDecimalSqlType(JavaTypeFactory typeFactory, int precision,
+ int scale) {
+ checkArgument(precision > 0, "DECIMAL type must have precision > 0. Found %s", precision);
+ checkArgument(scale >= 0, "DECIMAL type must have scale >= 0. Found %s", scale);
+ checkArgument(precision >= scale,
+ "DECIMAL type must have precision >= scale. Found precision (%s) and scale (%s).",
+ precision, scale);
+ return typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(SqlTypeName.DECIMAL, precision, scale), true);
+ }
+
+ @VisibleForTesting
+ protected static BigDecimal parseDecimal(int precision, int scale, String string) {
+ BigDecimal result = new BigDecimal(string);
+ // If the parsed value has more fractional digits than the specified scale, round ties away
+ // from 0.
+ if (result.scale() > scale) {
+ LOGGER.warn(
Review comment:
This one is still using wrong pattern %s versus {}
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] jacques-n commented on a change in pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
jacques-n commented on a change in pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#discussion_r725154040
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/FileRowConverter.java
##########
@@ -52,13 +53,13 @@
// row parser configuration
private final List<FieldDef> fields = new ArrayList<>();
- /** Format for parsing numbers. Not thread-safe, but we assume that only
- * one thread uses this converter at a time. */
+ /** Format for parsing numbers. Not thread-safe, but we assume that only one thread uses this
Review comment:
This and below still look like extraneous formatting.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] jacques-n commented on pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
jacques-n commented on pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#issuecomment-938751697
This generally looks reasonable. That being said, it seems like it has a lot of javadoc reformatting for what appears to be no reason. Mind backing out the extraneous changes?
Also, can you add a negative big decimal test as well?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite] xwkuang5 commented on a change in pull request #2572: [CALCITE-4841] Support numeric column type in CSV and File adapter.
Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2572:
URL: https://github.com/apache/calcite/pull/2572#discussion_r747522789
##########
File path: file/src/main/java/org/apache/calcite/adapter/file/CsvEnumerator.java
##########
@@ -314,22 +372,56 @@ static CSVReader openCsv(Source source) throws IOException {
} catch (ParseException e) {
return null;
}
- case STRING:
+ case VARCHAR:
default:
return string;
}
}
}
+ private static RelDataType parseDecimalSqlType(JavaTypeFactory typeFactory, int precision,
+ int scale) {
+ checkArgument(precision > 0, "DECIMAL type must have precision > 0. Found %s", precision);
+ checkArgument(scale >= 0, "DECIMAL type must have scale >= 0. Found %s", scale);
+ checkArgument(precision >= scale,
+ "DECIMAL type must have precision >= scale. Found precision (%s) and scale (%s).",
+ precision, scale);
+ return typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(SqlTypeName.DECIMAL, precision, scale), true);
+ }
+
+ @VisibleForTesting
+ protected static BigDecimal parseDecimal(int precision, int scale, String string) {
+ BigDecimal result = new BigDecimal(string);
+ // If the parsed value has more fractional digits than the specified scale, round ties away
+ // from 0.
+ if (result.scale() > scale) {
+ LOGGER.warn(
Review comment:
Oops, sorry I missed that. Corrected now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org