You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2021/10/09 13:11:30 UTC

[GitHub] [flink] godfreyhe commented on a change in pull request #17311: [FLINK-24318][table-planner]Casting a number to boolean has different results between 'select' fields and 'where' condition

godfreyhe commented on a change in pull request #17311:
URL: https://github.com/apache/flink/pull/17311#discussion_r725485628



##########
File path: flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/runtime/stream/sql/CalcITCase.scala
##########
@@ -50,6 +51,146 @@ class CalcITCase extends StreamingTestBase {
   @Rule
   def usesLegacyRows: LegacyRowResource = LegacyRowResource.INSTANCE
 
+  @Test
+  def testCastIntegerToBooleanTrueInProjection(): Unit ={
+    val sqlQuery = "SELECT CAST(1 AS BOOLEAN)"
+
+    val outputType = InternalTypeInfo.ofFields(
+      new BooleanType())
+
+    val result = tEnv.sqlQuery(sqlQuery).toAppendStream[RowData]
+    val sink = new TestingAppendRowDataSink(outputType)
+    result.addSink(sink)
+    env.execute()
+
+    val expected = List("+I(true)")
+    assertEquals(expected.sorted, sink.getAppendResults.sorted)
+  }
+
+  @Test
+  def testCastIntegerToBooleanFalseInProjection(): Unit ={
+    val sqlQuery = "SELECT CAST(0 AS BOOLEAN)"
+
+    val outputType = InternalTypeInfo.ofFields(
+      new BooleanType())
+
+    val result = tEnv.sqlQuery(sqlQuery).toAppendStream[RowData]
+    val sink = new TestingAppendRowDataSink(outputType)
+    result.addSink(sink)
+    env.execute()
+
+    val expected = List("+I(false)")
+    assertEquals(expected.sorted, sink.getAppendResults.sorted)
+  }
+
+  @Test
+  def testCastDecimalToBooleanTrueInProjection(): Unit ={
+    val sqlQuery = "SELECT CAST(1.1 AS BOOLEAN)"
+
+    val outputType = InternalTypeInfo.ofFields(
+      new BooleanType())
+
+    val result = tEnv.sqlQuery(sqlQuery).toAppendStream[RowData]
+    val sink = new TestingAppendRowDataSink(outputType)
+    result.addSink(sink)
+    env.execute()
+
+    val expected = List("+I(true)")
+    assertEquals(expected.sorted, sink.getAppendResults.sorted)
+  }
+
+  @Test
+  def testCastDecimalToBooleanFalseInProjection(): Unit ={

Review comment:
       the above four tests can be merged into one test: SELECT CAST(0 AS BOOLEAN), CAST(1 AS BOOLEAN), CAST(1.1 AS BOOLEAN), CAST(0.00 AS BOOLEAN)
   
   which is more efficient
   

##########
File path: flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/plan/utils/FlinkRexUtilTest.scala
##########
@@ -521,4 +501,12 @@ class FlinkRexUtilTest {
     val expressionReducer = new ExpressionReducer(TableConfig.getDefault, false)
     FlinkRexUtil.simplify(rexBuilder, expr, expressionReducer)
   }
+
+  def makeToBooleanCast(fromData: RexNode): RexNode ={

Review comment:
       mark this method as 'private'

##########
File path: flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/runtime/stream/sql/CalcITCase.scala
##########
@@ -50,6 +51,146 @@ class CalcITCase extends StreamingTestBase {
   @Rule
   def usesLegacyRows: LegacyRowResource = LegacyRowResource.INSTANCE
 
+  @Test
+  def testCastIntegerToBooleanTrueInProjection(): Unit ={
+    val sqlQuery = "SELECT CAST(1 AS BOOLEAN)"
+
+    val outputType = InternalTypeInfo.ofFields(
+      new BooleanType())
+
+    val result = tEnv.sqlQuery(sqlQuery).toAppendStream[RowData]
+    val sink = new TestingAppendRowDataSink(outputType)
+    result.addSink(sink)
+    env.execute()
+
+    val expected = List("+I(true)")
+    assertEquals(expected.sorted, sink.getAppendResults.sorted)
+  }
+
+  @Test
+  def testCastIntegerToBooleanFalseInProjection(): Unit ={
+    val sqlQuery = "SELECT CAST(0 AS BOOLEAN)"
+
+    val outputType = InternalTypeInfo.ofFields(
+      new BooleanType())
+
+    val result = tEnv.sqlQuery(sqlQuery).toAppendStream[RowData]
+    val sink = new TestingAppendRowDataSink(outputType)
+    result.addSink(sink)
+    env.execute()
+
+    val expected = List("+I(false)")
+    assertEquals(expected.sorted, sink.getAppendResults.sorted)
+  }
+
+  @Test
+  def testCastDecimalToBooleanTrueInProjection(): Unit ={
+    val sqlQuery = "SELECT CAST(1.1 AS BOOLEAN)"
+
+    val outputType = InternalTypeInfo.ofFields(
+      new BooleanType())
+
+    val result = tEnv.sqlQuery(sqlQuery).toAppendStream[RowData]
+    val sink = new TestingAppendRowDataSink(outputType)
+    result.addSink(sink)
+    env.execute()
+
+    val expected = List("+I(true)")
+    assertEquals(expected.sorted, sink.getAppendResults.sorted)
+  }
+
+  @Test
+  def testCastDecimalToBooleanFalseInProjection(): Unit ={
+    val sqlQuery = "SELECT CAST(0.00 AS BOOLEAN)"
+
+    val outputType = InternalTypeInfo.ofFields(
+      new BooleanType())
+
+    val result = tEnv.sqlQuery(sqlQuery).toAppendStream[RowData]
+    val sink = new TestingAppendRowDataSink(outputType)
+    result.addSink(sink)
+    env.execute()
+
+    val expected = List("+I(false)")
+    assertEquals(expected.sorted, sink.getAppendResults.sorted)
+  }
+
+
+  @Test
+  def testCastIntegerToBooleanTrueInCondition(): Unit ={
+    val sqlQuery = "SELECT * FROM MyTableRow WHERE b = CAST(1 AS BOOLEAN)"
+
+    val rowData1: GenericRowData = new GenericRowData(2)
+    rowData1.setField(0, 1)
+    rowData1.setField(1, true)
+
+    val rowData2: GenericRowData = new GenericRowData(2)
+    rowData2.setField(0, 2)
+    rowData2.setField(1, false)
+
+    val data = List(rowData1,rowData2)
+
+    implicit val dataType: TypeInformation[GenericRowData] =
+      InternalTypeInfo.ofFields(
+        new IntType(),
+        new BooleanType()).asInstanceOf[TypeInformation[GenericRowData]]
+
+    val ds = env.fromCollection(data)
+
+    val t = ds.toTable(tEnv, 'a, 'b)
+    tEnv.registerTable("MyTableRow", t)
+
+    val outputType = InternalTypeInfo.ofFields(
+      new IntType(),
+      new BooleanType())
+
+    val result = tEnv.sqlQuery(sqlQuery).toAppendStream[RowData]
+    val sink = new TestingAppendRowDataSink(outputType)
+    result.addSink(sink)
+    env.execute()
+
+    val expected = List("+I(1,true)")
+    assertEquals(expected.sorted, sink.getAppendResults.sorted)
+  }
+
+  @Test
+  def testCastIntegerToBooleanFalseInCondition(): Unit ={
+    val sqlQuery = "SELECT * FROM MyTableRow WHERE b = CAST(0 AS BOOLEAN)"
+
+    val rowData1: GenericRowData = new GenericRowData(2)
+    rowData1.setField(0, 1)
+    rowData1.setField(1, true)
+
+    val rowData2: GenericRowData = new GenericRowData(2)
+    rowData2.setField(0, 2)
+    rowData2.setField(1, false)
+
+    val data = List(rowData1,rowData2)
+
+    implicit val dataType: TypeInformation[GenericRowData] =
+      InternalTypeInfo.ofFields(
+        new IntType(),
+        new BooleanType()).asInstanceOf[TypeInformation[GenericRowData]]
+
+    val ds = env.fromCollection(data)
+
+    val t = ds.toTable(tEnv, 'a, 'b)
+    tEnv.registerTable("MyTableRow", t)
+
+    val outputType = InternalTypeInfo.ofFields(
+      new IntType(),
+      new BooleanType())
+
+    val result = tEnv.sqlQuery(sqlQuery).toAppendStream[RowData]
+    val sink = new TestingAppendRowDataSink(outputType)
+    result.addSink(sink)
+    env.execute()
+
+    val expected = List("+I(2,false)")
+    assertEquals(expected.sorted, sink.getAppendResults.sorted)

Review comment:
       actually, the above two filter tests can be merged into one test: 
   SELECT * FROM MyTableRow WHERE b = CAST(1 AS BOOLEAN)
   union all 
   SELECT * FROM MyTableRow WHERE b = CAST(0 AS BOOLEAN)
   
   we can add `b = CAST(1.1 AS BOOLEAN)` into the query
   




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org