Skip to content

Commit 088ed83

Browse files
committed
Remove Calcite dependency from wayang-java by mapping RelDataType in SQL module
1 parent 5756855 commit 088ed83

5 files changed

Lines changed: 70 additions & 24 deletions

File tree

wayang-api/wayang-api-sql/src/main/java/org/apache/wayang/api/sql/calcite/converter/WayangTableScanVisitor.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.wayang.api.sql.calcite.rel.WayangTableScan;
2525
import org.apache.wayang.api.sql.calcite.utils.ModelParser;
2626
import org.apache.wayang.java.operators.JavaCSVFileSource;
27+
import org.apache.wayang.java.operators.CsvType;
2728
import org.apache.wayang.core.plan.wayangplan.Operator;
2829
import org.apache.wayang.core.types.DataSetType;
2930
import org.apache.wayang.jdbc.operators.JdbcTableSource;
@@ -64,11 +65,32 @@ Operator visit(final WayangTableScan wayangRelNode) {
6465
"Could not initialize calcite model parser from current Wayang configuration");
6566
}
6667

67-
final List<RelDataType> fieldTypes = wayangRelNode.getRowType().getFieldList().stream()
68+
final List<CsvType> fieldTypes = wayangRelNode.getRowType().getFieldList().stream()
6869
.map(RelDataTypeField::getType)
70+
.map(type -> {
71+
switch (type.getSqlTypeName()) {
72+
case BOOLEAN: return CsvType.BOOLEAN;
73+
case TINYINT: return CsvType.TINYINT;
74+
case SMALLINT: return CsvType.SMALLINT;
75+
case INTEGER: return CsvType.INTEGER;
76+
case BIGINT: return CsvType.BIGINT;
77+
case FLOAT: return CsvType.FLOAT;
78+
case DOUBLE: return CsvType.DOUBLE;
79+
case DECIMAL: return CsvType.DECIMAL;
80+
case DATE: return CsvType.DATE;
81+
case TIME: return CsvType.TIME;
82+
case TIMESTAMP: return CsvType.TIMESTAMP;
83+
default: return CsvType.STRING;
84+
}
85+
})
6986
.collect(Collectors.toList());
87+
7088

71-
final String url = String.format("file:/%s/%s.csv", modelParser.getFsPath(), wayangRelNode.getTableName());
89+
final String url = String.format(
90+
"file:/%s/%s.csv",
91+
modelParser.getFsPath(),
92+
wayangRelNode.getTableName()
93+
);
7294

7395
final char separator = modelParser.getSchemaDelimiter(tableSource);
7496

wayang-platforms/wayang-java/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@
4343
<artifactId>opencsv</artifactId>
4444
<version>2.3</version>
4545
</dependency>
46-
<dependency>
47-
<groupId>org.apache.calcite</groupId>
48-
<artifactId>calcite-core</artifactId>
49-
<version>${calcite.version}</version>
50-
</dependency>
5146
<dependency>
5247
<groupId>org.apache.wayang</groupId>
5348
<artifactId>wayang-core</artifactId>

wayang-platforms/wayang-java/src/main/java/org/apache/wayang/java/operators/CsvRowConverter.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818
package org.apache.wayang.java.operators;
1919

2020
import au.com.bytecode.opencsv.CSVParser;
21-
import org.apache.calcite.avatica.util.DateTimeUtils;
22-
import org.apache.calcite.rel.type.RelDataType;
2321
import org.apache.commons.lang3.time.FastDateFormat;
2422

2523
import java.io.IOException;
2624
import java.math.BigDecimal;
27-
import java.math.RoundingMode;
25+
//import java.math.RoundingMode;
2826
import java.text.ParseException;
2927
import java.util.Date;
30-
import java.util.Locale;
28+
//import java.util.Locale;
3129
import java.util.TimeZone;
3230

3331
/**
@@ -57,11 +55,11 @@ public class CsvRowConverter {
5755

5856

5957

60-
public static Object convert(RelDataType fieldType, String string) {
58+
public static Object convert(CsvType fieldType, String string) {
6159
if (fieldType == null || string == null) {
6260
return string;
6361
}
64-
switch (fieldType.getSqlTypeName()) {
62+
switch (fieldType) {
6563
case BOOLEAN:
6664
if (string.length() == 0) {
6765
return null;
@@ -101,14 +99,14 @@ public static Object convert(RelDataType fieldType, String string) {
10199
if (string.length() == 0) {
102100
return null;
103101
}
104-
return parseDecimal(fieldType.getPrecision(), fieldType.getScale(), string);
102+
return new BigDecimal(string);
105103
case DATE:
106104
if (string.length() == 0) {
107105
return null;
108106
}
109107
try {
110108
Date date = TIME_FORMAT_DATE.parse(string);
111-
return (int) (date.getTime() / DateTimeUtils.MILLIS_PER_DAY);
109+
return (int) (date.getTime() / (24 * 60 * 60 * 1000));
112110
} catch (ParseException e) {
113111
return null;
114112
}
@@ -132,13 +130,13 @@ public static Object convert(RelDataType fieldType, String string) {
132130
} catch (ParseException e) {
133131
return null;
134132
}
135-
case VARCHAR:
133+
case STRING:
136134
default:
137135
return string;
138136
}
139137
}
140138

141-
private static BigDecimal parseDecimal(int precision, int scale, String string) {
139+
/**private static BigDecimal parseDecimal(int precision, int scale, String string) {
142140
BigDecimal result = new BigDecimal(string);
143141
// If the parsed value has more fractional digits than the specified scale, round ties away
144142
// from 0.
@@ -148,7 +146,7 @@ private static BigDecimal parseDecimal(int precision, int scale, String string)
148146
"Decimal value {} exceeds declared scale ({}). Performing rounding to keep the "
149147
+ "first {} fractional digits.",
150148
result, scale, scale);*/
151-
result = result.setScale(scale, RoundingMode.HALF_UP);
149+
/*result = result.setScale(scale, RoundingMode.HALF_UP);
152150
}
153151
// Throws an exception if the parsed value has more digits to the left of the decimal point
154152
// than the specified value.
@@ -158,7 +156,7 @@ private static BigDecimal parseDecimal(int precision, int scale, String string)
158156
result, precision, scale));
159157
}
160158
return result;
161-
}
159+
}*/
162160

163161

164162
public static String[] parseLine(String s) throws IOException {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.wayang.java.operators;
19+
20+
public enum CsvType {
21+
BOOLEAN,
22+
TINYINT,
23+
SMALLINT,
24+
INTEGER,
25+
BIGINT,
26+
FLOAT,
27+
DOUBLE,
28+
DECIMAL,
29+
DATE,
30+
TIME,
31+
TIMESTAMP,
32+
STRING
33+
}

wayang-platforms/wayang-java/src/main/java/org/apache/wayang/java/operators/JavaCSVFileSource.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package org.apache.wayang.java.operators;
1919

20-
import org.apache.calcite.rel.type.RelDataType;
21-
2220
import org.apache.commons.io.IOUtils;
2321

2422
import org.apache.wayang.basic.channels.FileChannel;
@@ -56,7 +54,7 @@ public class JavaCSVFileSource<T> extends UnarySource<T> implements JavaExecutio
5654

5755
private final String sourcePath;
5856

59-
private final List<RelDataType> fieldTypes;
57+
private final List<CsvType> fieldTypes;
6058
private final char separator; // Default separator
6159

6260
// TODO: incorporate fields later for projectable table scans
@@ -69,7 +67,7 @@ public class JavaCSVFileSource<T> extends UnarySource<T> implements JavaExecutio
6967
* @param type
7068
* @param fieldTypes
7169
*/
72-
public JavaCSVFileSource(final String sourcePath, final DataSetType<T> type, final List<RelDataType> fieldTypes) {
70+
public JavaCSVFileSource(final String sourcePath, final DataSetType<T> type, final List<CsvType> fieldTypes) {
7371
super(type);
7472
this.sourcePath = sourcePath;
7573
this.fieldTypes = fieldTypes;
@@ -84,7 +82,7 @@ public JavaCSVFileSource(final String sourcePath, final DataSetType<T> type, fin
8482
* @param fieldTypes
8583
* @param separator
8684
*/
87-
public JavaCSVFileSource(final String sourcePath, final DataSetType<T> type, final List<RelDataType> fieldTypes,
85+
public JavaCSVFileSource(final String sourcePath, final DataSetType<T> type, final List<CsvType> fieldTypes,
8886
final char separator) {
8987
super(type);
9088
this.sourcePath = sourcePath;

0 commit comments

Comments
 (0)