Uploaded image for project: 'Ignite'
  1. Ignite
  2. IGNITE-19503

Sql. UUID. Some expressions and statements with types from incompatible families are not rejected.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 3.0.0-beta1
    • None
    • sql
    • Docs Required, Release Notes Required

    Description

      Type compatibility checks for UUID in some expression/statement are not performed at validation stage:

      INTEGER vs UUID:

       @Test
          public void testInsert() {
              sql("CREATE TABLE t1 (key INTEGER PRIMARY KEY , int_col INTEGER)");
              // /  class java.util.UUID cannot be cast to class java.lang.Integer
              sql("INSERT INTO t1 VALUES(1, '46138242-b771-4d8b-ad26-2b3fcee5f11d'::UUID)");
          }
      
          @Test
          public void testUpdate() {
      
              sql("CREATE TABLE t1 (key INTEGER PRIMARY KEY , int_col INTEGER)");
              sql("INSERT INTO t1 VALUES(1, 1)");
              //  class java.util.UUID cannot be cast to class java.lang.Integer
              sql("UPDATE t1 SET int_col ='46138242-b771-4d8b-ad26-2b3fcee5f11d'::UUID");
          }
      

      INTEGER vs VARBINARY

      @Test
          public void testInsert2() {
              sql("CREATE TABLE t1 (key INTEGER PRIMARY KEY , int_col INTEGER)");
              // Column 33: Cannot assign to target field 'INT_COL' of type INTEGER from source field 'EXPR$0' of type BINARY(3)
              sql("INSERT INTO t1 VALUES(1, x'010203')");
          }
      
          @Test
          public void testUpdate2() {
      
              sql("CREATE TABLE t1 (key INTEGER PRIMARY KEY , int_col INTEGER)");
              // column 33: Cannot assign to target field 'INT_COL' of type INTEGER from source field 'EXPR$0' of type BINARY(3)
              sql("UPDATE t1 SET int_col = x'010203'");
          }
      

      Expressions:

      @Test
          public void testExprs() {
              //sql("SELECT 1 in ('46138242-b771-4d8b-ad26-2b3fcee5f11d'::UUID)");
      
              // Binary comparison operations are rejected by validator: OK
              // From line 1, column 1 to line 1, column 55: Invalid types for comparison: INTEGER NOT NULL = UUID NOT NULL
              sql("SELECT 1 = '46138242-b771-4d8b-ad26-2b3fcee5f11d'::UUID");
      
              // CASE: OK
              // Illegal mixing of types in CASE or COALESCE statement
              sql("SELECT CASE int_col WHEN 1 THEN '46138242-b771-4d8b-ad26-2b3fcee5f11d'::UUID ELSE '2' END FROM t1");
      
              // Arithmetic
              /*
              java.lang.IllegalArgumentException: Cannot infer return type for +; operand types: [INTEGER, UUID]
      
              at org.apache.calcite.sql.SqlOperator.inferReturnType(SqlOperator.java:541)
              at org.apache.calcite.sql.SqlOperator.validateOperands(SqlOperator.java:504)
              at org.apache.calcite.sql.SqlOperator.deriveType(SqlOperator.java:605)
              at org.apache.calcite.sql.SqlBinaryOperator.deriveType(SqlBinaryOperator.java:178)
               */
              sql("SELECT 1 + '46138242-b771-4d8b-ad26-2b3fcee5f11d'::UUID");
      
              // Runtime: Cannot convert 46138242-b771-4d8b-ad26-2b3fcee5f11d to int
              sql("SELECT 1 IN ('46138242-b771-4d8b-ad26-2b3fcee5f11d'::UUID)");
      
              // Line 8, Column 281: Cannot cast "java.util.UUID" to "org.apache.calcite.avatica.util.ByteString"
              //Caused by: org.codehaus.commons.compiler.CompileException: Line 8, Column 281: Cannot cast "java.util.UUID" to "org.apache.calcite.avatica.util.ByteString"
              //at org.codehaus.janino.UnitCompiler.compileError(UnitCompiler.java:13014)
              sql("SELECT x'010203' IN ('46138242-b771-4d8b-ad26-2b3fcee5f11d'::UUID)");
      
             // Same goes for SELECT uuid_col IN (1,2,3)
             // [SEARCH($t0, Sarg[1, 2, 3, 4])] Sarg has INTEGER type.
          }
      

      This happens because ANY type is used as a type family for UUID/custom data types and ANY can be cast to/from all other data types see SqlTypeUtil and other calcite classes.

      When fixing INSERT statement we should remember that calcite's DEFAULT expression has ANY type as well.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              mzhuravkov Maksim Zhuravkov
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: