Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: Mergen Imeev <imeevma@tarantool.org>,
	tsafin@tarantool.org, tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH 5/6] sql: remove implicit cast from string for comparison
Date: Wed, 10 Jun 2020 00:29:49 +0200	[thread overview]
Message-ID: <e94407d5-3cb5-b346-f2e3-8328a58cf34e@tarantool.org> (raw)
In-Reply-To: <c4b2d4ee-9052-1c9d-e8bd-e67071cd50ed@tarantool.org>

Thanks for the patch!

>>> index 854ed774f..2a18b17be 100755
>>> --- a/test/sql-tap/tkt-9a8b09f8e6.test.lua
>>> +++ b/test/sql-tap/tkt-9a8b09f8e6.test.lua
>>> @@ -83,23 +83,23 @@ test:do_execsql_test(
>>>           -- </1.5>
>>>       })
>>>   -test:do_execsql_test(
>>> +test:do_catchsql_test(
>>>       2.1,
>>>       [[
>>>           SELECT x FROM t1 WHERE x IN (1);
>>>       ]], {
>>>           -- <2.1>
>>> -        "1"
>>> +        1,"Type mismatch: can not convert 1 to numeric"
>> 3. Can tests in this file be fixed to return the same results
>> as before?
> I fixed this in most cases, but am not sure if this is correct.
> I think most tests are out of date.

Yeah, looks like the test file becomes mostly useless. It now
tests rather explicit CAST than implicit cast. The previous
version was better. At least it covered implicit cast errors.

I would try to rollback to the previous version, remove obviously
duplicated or not needed test cases, and see what is left.
Probably the whole test file is useless.

See 6 comments below.

> sql: remove implicit cast from string for comparison
> 
> This patch removes implicit cast from strings to numbers for
> comparison.
> 
> Closes #4230
> 
> @TarantoolBot document
> Title: remove implicit cast between strings and numbers
> 
> This patch-set removes implicit cast from string to number and
> from number to string.
> 
> Example:
> 
> For comparison:
> 
> tarantool> box.execute([[SELECT '1' > 0;]])
> ---
> - null
> - 'Type mismatch: can not convert 1 to numeric'
> ...
> 
> tarantool> box.execute([[SELECT "id" FROM "_space" WHERE '1' > "id";]])
> ---
> - null
> - 'Type mismatch: can not convert text to unsigned'
> ...
> 
> For assignment:
> 
> tarantool> box.execute([[CREATE TABLE t1(i INT PRIMARY KEY);]])
> tarantool> box.execute([[INSERT INTO t1 VALUES ('1');]])
> ---
> - null
> - 'Type mismatch: can not convert text to integer'
> ...
> 
> tarantool> box.execute([[CREATE TABLE t2(t text PRIMARY KEY);]])
> tarantool> box.execute([[INSERT INTO t2 VALUES (1);]])
> ---
> - null
> - 'Type mismatch: can not convert unsigned to string'
> ...

1. I suggest to wrap code samples into ``` to produce more
readable github markdown in the resulting doc issue.

> review fix

2. Hm.

> diff --git a/src/box/sql/wherecode.c b/src/box/sql/wherecode.c
> index 6d8768865..1d7c76670 100644
> --- a/src/box/sql/wherecode.c
> +++ b/src/box/sql/wherecode.c
> @@ -335,72 +335,6 @@ disableTerm(WhereLevel * pLevel, WhereTerm * pTerm)
>      }
>  }
> 
> -/**
> - * Code an OP_ApplyType opcode to apply the column type string
> - * @types to the n registers starting at @base.
> - *
> - * As an optimization, SCALAR entries (which are no-ops) at the
> - * beginning and end of @types are ignored.  If all entries in
> - * @types are SCALAR, then no code gets generated.
> - *
> - * This routine makes its own copy of @types so that the caller is
> - * free to modify @types after this routine returns.
> - */
> -static void
> -emit_apply_type(Parse *pParse, int base, int n, enum field_type *types)
> -{
> -    Vdbe *v = pParse->pVdbe;
> -    if (types == NULL) {
> -        assert(pParse->db->mallocFailed);
> -        return;
> -    }
> -    assert(v != 0);
> -
> -    /*
> -     * Adjust base and n to skip over SCALAR entries at the
> -     * beginning and end of the type sequence.
> -     */
> -    while (n > 0 && types[0] == FIELD_TYPE_SCALAR) {
> -        n--;
> -        base++;
> -        types++;
> -    }
> -    while (n > 1 && types[n - 1] == FIELD_TYPE_SCALAR) {
> -        n--;
> -    }
> -
> -    if (n > 0) {
> -        enum field_type *types_dup = field_type_sequence_dup(pParse,
> -                                     types, n);

3. This function is now unused and can be deleted.

> -        sqlVdbeAddOp4(v, OP_ApplyType, base, n, 0,
> -                  (char *) types_dup, P4_DYNAMIC);
> -        sql_expr_type_cache_change(pParse, base, n);
> -    }
> -}
> -
> -/**
> - * Expression @rhs, which is the RHS of a comparison operation, is
> - * either a vector of n elements or, if n==1, a scalar expression.
> - * Before the comparison operation, types @types are to be applied
> - * to the @rhs values. This function modifies entries within the
> - * field sequence to SCALAR if either:
> - *
> - *   * the comparison will be performed with no type, or
> - *   * the type change in @types is guaranteed not to change the value.
> - */
> -static void
> -expr_cmp_update_rhs_type(struct Expr *rhs, int n, enum field_type *types)
> -{
> -    for (int i = 0; i < n; i++) {
> -        Expr *p = sqlVectorFieldSubexpr(rhs, i);
> -        enum field_type expr_type = sql_expr_type(p);
> -        if (sql_type_result(expr_type, types[i]) == FIELD_TYPE_SCALAR ||
> -            sql_expr_needs_no_type_change(p, types[i])) {

4. Ditto (removed in the next commit, up to you if want to remove
here).

> -            types[i] = FIELD_TYPE_SCALAR;
> -        }
> -    }
> -}
> diff --git a/test/sql-tap/gh-4230-del-impl-cast-str-to-num.test.lua b/test/sql-tap/gh-4230-del-impl-cast-str-to-num.test.lua
> new file mode 100755
> index 000000000..ef4127e0e
> --- /dev/null
> +++ b/test/sql-tap/gh-4230-del-impl-cast-str-to-num.test.lua
> @@ -0,0 +1,78 @@
> +#!/usr/bin/env tarantool
> +test = require("sqltester")
> +test:plan(8)
> +
> +--
> +-- Make sure that there is no implicit cast between string and
> +-- number.
> +--
> +test:do_catchsql_test(
> +    "gh-4230-1",
> +    [[
> +        SELECT '1' > 0;
> +    ]], {
> +        1, "Type mismatch: can not convert 1 to numeric"
> +    })
> +
> +test:do_catchsql_test(
> +    "gh-4230-2",
> +    [[
> +        SELECT 0 > '1';
> +    ]], {
> +        1, "Type mismatch: can not convert 1 to numeric"

5. The error messages are really weird. We should probably wrap
string value into quoutes. Or print it as 'text'. Without a
value. Not necessarily here. You can create an issue for that,
and do it later.

> diff --git a/test/sql-tap/tkt-f973c7ac31.test.lua b/test/sql-tap/tkt-f973c7ac31.test.lua
> index 82bdb52f8..604a7e6bb 100755
> --- a/test/sql-tap/tkt-f973c7ac31.test.lua
> +++ b/test/sql-tap/tkt-f973c7ac31.test.lua
> @@ -39,9 +39,8 @@ for tn, sql in ipairs(sqls) do
>      test:do_execsql_test(
>          "tkt-f973c7ac3-1."..tn..".1",
>          [[
> -            SELECT c1,c2 FROM t WHERE c1 = 5 AND c2>0 AND c2<='2' ORDER BY c2 DESC
> +            SELECT c1,c2 FROM t WHERE c1 = 5 AND c2>0 AND CAST(c2 AS STRING)<='2' ORDER BY c2 DESC

6. On the branch I see trailing whitespaces in this file.

>          ]], {
> -
>          })
> 

  reply	other threads:[~2020-06-09 22:29 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-28 14:17 [Tarantool-patches] [PATCH 0/6] Remove implicit cast Mergen Imeev
2020-05-28 14:17 ` [Tarantool-patches] [PATCH 1/6] sql: remove implicit cast for assignment Mergen Imeev
2020-06-01 17:03   ` Vladislav Shpilevoy
2020-06-09 11:41     ` Mergen Imeev
2020-06-09 22:28       ` Vladislav Shpilevoy
2020-05-28 14:17 ` [Tarantool-patches] [PATCH 2/6] sql: remove mem_apply_type() from OP_MakeRecord Mergen Imeev
2020-06-01 17:03   ` Vladislav Shpilevoy
2020-06-09 11:43     ` Mergen Imeev
2020-05-28 14:17 ` [Tarantool-patches] [PATCH 3/6] sql: replace ApplyType by CheckType for IN operator Mergen Imeev
2020-05-28 14:17 ` [Tarantool-patches] [PATCH 4/6] sql: remove mem_apply_type() from OP_MustBeInt Mergen Imeev
2020-06-01 17:04   ` Vladislav Shpilevoy
2020-06-09 11:47     ` Mergen Imeev
2020-06-09 22:28       ` Vladislav Shpilevoy
2020-05-28 14:17 ` [Tarantool-patches] [PATCH 5/6] sql: remove implicit cast from string for comparison Mergen Imeev
2020-06-01 17:04   ` Vladislav Shpilevoy
2020-06-09 11:51     ` Mergen Imeev
2020-06-09 22:29       ` Vladislav Shpilevoy [this message]
2020-05-28 14:17 ` [Tarantool-patches] [PATCH 6/6] sql: remove OP_ApplyType Mergen Imeev
2020-06-09 22:29   ` Vladislav Shpilevoy
2020-06-01 17:03 ` [Tarantool-patches] [PATCH 0/6] Remove implicit cast Vladislav Shpilevoy
2020-06-09 11:25   ` Mergen Imeev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e94407d5-3cb5-b346-f2e3-8328a58cf34e@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=tsafin@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 5/6] sql: remove implicit cast from string for comparison' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox