[
https://issues.apache.org/jira/browse/BEAM-2281?focusedWorklogId=111946&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-111946
]
ASF GitHub Bot logged work on BEAM-2281:
----------------------------------------
Author: ASF GitHub Bot
Created on: 14/Jun/18 17:38
Start Date: 14/Jun/18 17:38
Worklog Time Spent: 10m
Work Description: XuMingmin closed pull request #5626: [BEAM-2281]
Refactoring literal expression type conversion
URL: https://github.com/apache/beam/pull/5626
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/BeamSqlFnExecutor.java
b/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/BeamSqlFnExecutor.java
index e38e92ef286..5a3ef4066da 100644
---
a/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/BeamSqlFnExecutor.java
+++
b/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/BeamSqlFnExecutor.java
@@ -17,7 +17,6 @@
*/
package org.apache.beam.sdk.extensions.sql.impl.interpreter;
-import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
@@ -106,6 +105,7 @@
import org.apache.calcite.rex.RexLocalRef;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexProgram;
+import org.apache.calcite.runtime.SqlFunctions;
import org.apache.calcite.schema.impl.ScalarFunctionImpl;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.type.SqlTypeName;
@@ -149,7 +149,7 @@ public BeamSqlFnExecutor(RexProgram program) {
* represent each {@link SqlOperator} with a corresponding {@link
BeamSqlExpression}.
*/
static BeamSqlExpression buildExpression(RexNode rexNode) {
- BeamSqlExpression ret = null;
+ BeamSqlExpression ret;
if (rexNode instanceof RexLiteral) {
RexLiteral node = (RexLiteral) rexNode;
SqlTypeName type = node.getTypeName();
@@ -171,36 +171,44 @@ static BeamSqlExpression buildExpression(RexNode rexNode)
{
// node.getSqlTypeName() = DECIMAL (the actual internal storage
format)
// So we need to do a convert here.
// check RexBuilder#makeLiteral for more information.
+
SqlTypeName realType = node.getType().getSqlTypeName();
Object realValue = value;
- if (type == SqlTypeName.DECIMAL) {
- BigDecimal rawValue = (BigDecimal) value;
+
+ if (SqlTypeName.NUMERIC_TYPES.contains(type)) {
switch (realType) {
case TINYINT:
- realValue = (byte) rawValue.intValue();
+ realValue = SqlFunctions.toByte(value);
break;
case SMALLINT:
- realValue = (short) rawValue.intValue();
+ realValue = SqlFunctions.toShort(value);
break;
case INTEGER:
- realValue = rawValue.intValue();
+ realValue = SqlFunctions.toInt(value);
break;
case BIGINT:
- realValue = rawValue.longValue();
+ realValue = SqlFunctions.toLong(value);
+ break;
+ case FLOAT:
+ realValue = SqlFunctions.toFloat(value);
+ break;
+ case DOUBLE:
+ realValue = SqlFunctions.toDouble(value);
break;
case DECIMAL:
- realValue = rawValue;
+ realValue = SqlFunctions.toBigDecimal(value);
break;
default:
throw new IllegalStateException(
- "type/realType mismatch: " + type + " VS " + realType);
- }
- } else if (type == SqlTypeName.DOUBLE) {
- Double rawValue = (Double) value;
- if (realType == SqlTypeName.FLOAT) {
- realValue = rawValue.floatValue();
+ "Unsupported conversion: Attempted convert node "
+ + node.toString()
+ + " of type "
+ + type
+ + "to "
+ + realType);
}
}
+
return BeamSqlPrimitive.of(realType, realValue);
}
} else if (rexNode instanceof RexInputRef) {
@@ -443,7 +451,7 @@ static BeamSqlExpression buildExpression(RexNode rexNode) {
case "DOT":
return new BeamSqlDotExpression(subExps, node.type.getSqlTypeName());
- //DEFAULT keyword for UDF with optional parameter
+ // DEFAULT keyword for UDF with optional parameter
case "DEFAULT":
return new BeamSqlDefaultExpression();
@@ -477,7 +485,7 @@ static BeamSqlExpression buildExpression(RexNode rexNode) {
ret = new BeamSqlWindowEndExpression();
break;
default:
- //handle UDF
+ // handle UDF
if (((RexCall) rexNode).getOperator() instanceof
SqlUserDefinedFunction) {
SqlUserDefinedFunction udf = (SqlUserDefinedFunction) ((RexCall)
rexNode).getOperator();
ScalarFunctionImpl fn = (ScalarFunctionImpl) udf.getFunction();
diff --git
a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/BeamSqlFnExecutorTest.java
b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/BeamSqlFnExecutorTest.java
index 355b022a4e6..0d50c696d7e 100644
---
a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/BeamSqlFnExecutorTest.java
+++
b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/BeamSqlFnExecutorTest.java
@@ -17,6 +17,7 @@
*/
package org.apache.beam.sdk.extensions.sql.impl.interpreter;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.math.BigDecimal;
@@ -25,6 +26,7 @@
import java.util.Date;
import java.util.TimeZone;
import
org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression;
+import
org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive;
import
org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.arithmetic.BeamSqlDivideExpression;
import
org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.arithmetic.BeamSqlMinusExpression;
import
org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.arithmetic.BeamSqlModExpression;
@@ -44,11 +46,16 @@
import
org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.logical.BeamSqlOrExpression;
import org.apache.calcite.avatica.util.TimeUnit;
import org.apache.calcite.avatica.util.TimeUnitRange;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.SqlIntervalQualifier;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.BasicSqlType;
+import org.apache.calcite.sql.type.SqlTypeName;
import org.junit.Test;
/** Unit test cases for {@link BeamSqlFnExecutor}. */
@@ -95,6 +102,25 @@ public void
testBuildExpression_logical_not_invalidOperand() {
BeamSqlFnExecutor.buildExpression(rexNode);
}
+ @Test
+ public void testBuildExpression_literal() {
+ RelDataType realType = new BasicSqlType(RelDataTypeSystem.DEFAULT,
SqlTypeName.INTEGER);
+
+ RexLiteral rexNode = RexLiteral.fromJdbcString(realType,
SqlTypeName.DOUBLE, "1.0");
+ BeamSqlExpression exp = BeamSqlFnExecutor.buildExpression(rexNode);
+
+ assertTrue(exp instanceof BeamSqlPrimitive);
+ assertEquals(SqlTypeName.INTEGER, exp.getOutputType());
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testBuildExpression_literal_mismatch_type() {
+ RelDataType realType = new BasicSqlType(RelDataTypeSystem.DEFAULT,
SqlTypeName.VARCHAR);
+
+ RexLiteral rexNode = RexLiteral.fromJdbcString(realType,
SqlTypeName.DOUBLE, "1.0");
+ BeamSqlFnExecutor.buildExpression(rexNode);
+ }
+
@Test
public void testBuildExpression_arithmetic() {
testBuildArithmeticExpression(SqlStdOperatorTable.PLUS,
BeamSqlPlusExpression.class);
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 111946)
Time Spent: 4h 40m (was: 4.5h)
> call SqlFunctions in operator implementation
> --------------------------------------------
>
> Key: BEAM-2281
> URL: https://issues.apache.org/jira/browse/BEAM-2281
> Project: Beam
> Issue Type: Improvement
> Components: dsl-sql
> Reporter: Xu Mingmin
> Assignee: Kenneth Knowles
> Priority: Major
> Time Spent: 4h 40m
> Remaining Estimate: 0h
>
> Calcite has a collections of functions in
> {{org.apache.calcite.runtime.SqlFunctions}}. It sounds a good source to
> leverage when adding operators as {{BeamSqlExpression}}.
> [~xumingming] [~app-tarush], any comments?
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)