Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: support sql_mode REAL_AS_FLOAT #5029

Merged
merged 2 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mysql/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ func (m SQLMode) HasStrictMode() bool {
return m&ModeStrictTransTables == ModeStrictTransTables || m&ModeStrictAllTables == ModeStrictAllTables
}

// HasRealAsFloatMode detects if 'REAL_AS_FLOAT' mode is set in SQLMode
func (m SQLMode) HasRealAsFloatMode() bool {
return m&ModeRealAsFloat == ModeRealAsFloat
}

// consts for sql modes.
const (
ModeNone SQLMode = 0
Expand Down
66 changes: 62 additions & 4 deletions mysql/const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package mysql
package mysql_test

import (
"flag"
. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/mock-tikv"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
"testing"
)
Expand All @@ -27,6 +35,37 @@ func TestT(t *testing.T) {
var _ = Suite(&testMySQLConstSuite{})

type testMySQLConstSuite struct {
cluster *mocktikv.Cluster
mvccStore *mocktikv.MvccStore
store kv.Storage
*parser.Parser
}

var mockTikv = flag.Bool("mockTikv", true, "use mock tikv store in executor test")

func (s *testMySQLConstSuite) SetUpSuite(c *C) {
s.Parser = parser.New()
flag.Lookup("mockTikv")
useMockTikv := *mockTikv
if useMockTikv {
s.cluster = mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(s.cluster)
s.mvccStore = mocktikv.NewMvccStore()
store, err := tikv.NewMockTikvStore(
tikv.WithCluster(s.cluster),
tikv.WithMVCCStore(s.mvccStore),
)
c.Assert(err, IsNil)
s.store = store
tidb.SetSchemaLease(0)
tidb.SetStatsLease(0)
} else {
store, err := tidb.NewStore("memory://test/test")
c.Assert(err, IsNil)
s.store = store
}
_, err := tidb.BootstrapSession(s.store)
c.Assert(err, IsNil)
}

func (s *testMySQLConstSuite) TestGetSQLMode(c *C) {
Expand All @@ -44,7 +83,7 @@ func (s *testMySQLConstSuite) TestGetSQLMode(c *C) {
}

for _, t := range positiveCases {
_, err := GetSQLMode(FormatSQLModeStr(t.arg))
_, err := mysql.GetSQLMode(mysql.FormatSQLModeStr(t.arg))
c.Assert(err, IsNil)
}

Expand All @@ -58,7 +97,7 @@ func (s *testMySQLConstSuite) TestGetSQLMode(c *C) {
}

for _, t := range negativeCases {
_, err := GetSQLMode(FormatSQLModeStr(t.arg))
_, err := mysql.GetSQLMode(mysql.FormatSQLModeStr(t.arg))
c.Assert(err, NotNil)
}
}
Expand All @@ -84,9 +123,28 @@ func (s *testMySQLConstSuite) TestSQLMode(c *C) {
}

for _, t := range tests {
sqlMode, _ := GetSQLMode(t.arg)
sqlMode, _ := mysql.GetSQLMode(t.arg)
c.Assert(sqlMode.HasNoZeroDateMode(), Equals, t.hasNoZeroDateMode)
c.Assert(sqlMode.HasNoZeroInDateMode(), Equals, t.hasNoZeroInDateMode)
c.Assert(sqlMode.HasErrorForDivisionByZeroMode(), Equals, t.hasErrorForDivisionByZeroMode)
}
}

func (s *testMySQLConstSuite) TestRealAsFloatMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a real);")
result := tk.MustQuery("desc t")
c.Check(result.Rows(), HasLen, 1)
row := result.Rows()[0]
c.Assert(row[1], Equals, "double")

tk.MustExec("drop table if exists t;")
tk.MustExec("set sql_mode='REAL_AS_FLOAT'")
tk.MustExec("create table t (a real)")
result = tk.MustQuery("desc t")
c.Check(result.Rows(), HasLen, 1)
row = result.Rows()[0]
c.Assert(row[1], Equals, "float")
}
5 changes: 5 additions & 0 deletions parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ func (s *Scanner) SetSQLMode(mode mysql.SQLMode) {
s.sqlMode = mode
}

// GetSQLMode return the SQL mode of scanner.
func (s *Scanner) GetSQLMode() mysql.SQLMode {
return s.sqlMode
}

// NewScanner returns a new scanner object.
func NewScanner(s string) *Scanner {
return &Scanner{r: reader{s: s}}
Expand Down
6 changes: 5 additions & 1 deletion parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -5258,7 +5258,11 @@ FloatingPointType:
}
| "REAL"
{
$$ = mysql.TypeDouble
if parser.lexer.GetSQLMode().HasRealAsFloatMode() {
$$ = mysql.TypeFloat
} else {
$$ = mysql.TypeDouble
}
}
| "DOUBLE"
{
Expand Down