为什么涉及用户变量的表达式的求值顺序未定义? [英] Why the order of evaluation for expressions involving user variables is undefined?

查看:104
本文介绍了为什么涉及用户变量的表达式的求值顺序未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySQL手册的以下输出中不能保证查询总是相同.

From MySQL Manual the output of the following query is not guaranteed to be same always.

SET @a := 0;

SELECT 
@a AS first,
@a := @a + 1 AS second,
@a := @a + 1 AS third,
@a := @a + 1 AS fourth,
@a := @a + 1 AS fifth,
@a := @a + 1 AS sixth;


输出:

first second third fourth fifth sixth 
  0     1      2     3     4      5

从手册中引用:

但是,涉及用户的表达式的求值顺序 变量未定义;

However,the order of evaluation for expressions involving user variables is undefined;

我想知道背后的故事.

所以我的问题是: 为什么涉及用户变量的表达式的求值顺序未定义?

So my question is : Why the order of evaluation for expressions involving user variables is undefined?

推荐答案

TL; DR MySQL用户定义的变量不打算以这种方式使用. SQL语句描述的是结果集,而不是一系列的操作.该文档尚不清楚变量分配的含义.但是您不能同时读写变量.并且未定义SELECT子句中的分配顺序.而且,您可以假设的是,外部SELECT子句中的分配是针对某个输出行完成的.

TL;DR MySQL user-defined variables are not intended to be used that way. An SQL statement describes a result set, not a series of operations. The documentation isn't clear about what variable assignments even mean. But you can't both read and write a variable. And assignment order within SELECT clause is not defined. And all you can assume is that assignments in an outer SELECT clause are done for some one output row.

您看到的几乎所有代码都具有未定义的行为.一些明智的人通过实现代码演示了操作员和管理员.优化特定实现实际执行的操作.但是这种行为不能在下一个版本中得到依靠.

Almost all the code you see like yours has undefined behaviour. Some sensible people demonstrate via the implementation code for operators & optimization what a particular implementation actually does. But that behaviour can't be relied on for the next release.

阅读文档.读取和写入相同的变量是未定义的.如果没有完成,任何读取的变量都会在一条语句中固定.没有分配的顺序.对于仅具有DETERMINISTIC函数(其值由参数值确定)的SELECT,其结果由概念性评估执行来定义.但是,该变量与用户变量之间没有任何联系. ever 的含义尚不清楚:文档显示每个选择表达式仅在发送给客户端时才进行评估".这似乎是在说,除了从最外层的SELECT子句放入结果集的意义上讲,不能保证甚至选择了"一行.未定义SELECT中的分配顺序.即使在概念上为每行完成了 if 分配,它们也只能取决于行值,所以 same 表示对某行仅执行一次赋值.并且由于未定义分配顺序,因此该行可以是任何行.因此,假设这就是文档的含义,那么您可以期望的是,如果您不对SELECT语句中的同一变量进行读写操作,则最外层SELECT中的每个变量分配都将以某种顺序发生在一个输出行上

Read the documentation. Reading and writing the same variable is undefined. When it's not done, any variable read is fixed within a statement. There is no order to assignments. For SELECTs with only DETERMINISTIC functions (whose values are determined by argument values) the result is defined by a conceptual evaluation execution. But there is no connection between that and user variable. What an assignment ever means is not clear: the documention says "each select expression is evaluated only when sent to the client". This seems to be saying that there's no guarantee a row is even "selected" except in the sense of put into a result set per an outermost SELECT clause. The order of assignments in a SELECT is not defined. And even if assignments are conceptually done for every row, they can only depend on the row value, so that's the same as saying the assignment is done only once, for some row. And since assignment order is not defined, that row can be any row. So assuming that that is what the documentation means, all you can expect is that if you don't read and write from the same variable in a SELECT statement then each variable assignment in the outermost SELECT will have happened in some order for one output row.

这篇关于为什么涉及用户变量的表达式的求值顺序未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆