{"action":"created","comment":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/38568962","id":38568962,"diff_hunk":"@@ -0,0 +1,432 @@\n+// Copyright 2015 The Cockroach Authors.\n+//\n+// Licensed under the Apache License, Version 2.0 (the \"License\");\n+// you may not use this file except in compliance with the License.\n+// You may obtain a copy of the License at\n+//\n+//     http://www.apache.org/licenses/LICENSE-2.0\n+//\n+// Unless required by applicable law or agreed to in writing, software\n+// distributed under the License is distributed on an \"AS IS\" BASIS,\n+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n+// implied. See the License for the specific language governing\n+// permissions and limitations under the License. See the AUTHORS file\n+// for names of contributors.\n+//\n+// Author: Tamir Duberstein (tamird@gmail.com)\n+\n+package parser\n+\n+import (\n+\t\"fmt\"\n+\t\"reflect\"\n+\t\"strings\"\n+\n+\t\"github.com/cockroachdb/cockroach/util\"\n+)\n+\n+// TypeCheckExpr type-checks an SQL expression. Expression checking is\n+// a mostly straightforward walk over the parse tree.\n+func TypeCheckExpr(expr Expr) (Datum, error) {\n+\tswitch t := expr.(type) {\n+\tcase *AndExpr:\n+\t\treturn typeCheckBooleanExprs(t.Left, t.Right)\n+\n+\tcase *OrExpr:\n+\t\treturn typeCheckBooleanExprs(t.Left, t.Right)\n+\n+\tcase *NotExpr:\n+\t\treturn typeCheckBooleanExprs(t.Expr)\n+\n+\tcase Row:\n+\t\t// NormalizeExpr transforms this into Tuple.\n+\n+\tcase *ParenExpr:\n+\t\t// NormalizeExpr unwraps this.\n+\n+\tcase *ComparisonExpr:\n+\t\treturn typeCheckComparisonExpr(t)\n+\n+\tcase *RangeCond:\n+\t\t// NormalizeExpr transforms this into an AndExpr.\n+\n+\tcase *NullCheck:\n+\t\tif _, err := TypeCheckExpr(t.Expr); err != nil {\n+\t\t\treturn nil, err\n+\t\t}\n+\t\treturn DummyBool, nil\n+\n+\tcase *ExistsExpr:\n+\t\t// The subquery within the exists should have been executed before\n+\t\t// expression evaluation and the exists nodes replaced with the result.\n+\n+\tcase BytesVal, StrVal:\n+\t\treturn DummyString, nil\n+\n+\tcase IntVal:\n+\t\treturn DummyInt, nil\n+\n+\tcase NumVal:\n+\t\treturn DummyFloat, nil\n+\n+\tcase BoolVal:\n+\t\treturn DummyBool, nil\n+\n+\tcase ValArg:\n+\t\t// Placeholders should have been replaced before expression evaluation.\n+\n+\tcase *QualifiedName:\n+\t\treturn nil, fmt.Errorf(\"qualified name \\\"%s\\\" not found\", t)\n+\n+\tcase Tuple:\n+\t\ttuple := make(DTuple, 0, len(t))\n+\t\tfor _, v := range t {\n+\t\t\td, err := TypeCheckExpr(v)\n+\t\t\tif err != nil {\n+\t\t\t\treturn nil, err\n+\t\t\t}\n+\t\t\ttuple = append(tuple, d)\n+\t\t}\n+\t\treturn tuple, nil\n+\n+\tcase DReference:\n+\t\treturn t.Datum(), nil\n+\n+\tcase Datum:\n+\t\treturn t, nil\n+\n+\tcase *Subquery:\n+\t\t// The subquery should have been executed before expression evaluation and\n+\t\t// the result placed into the expression tree.\n+\n+\tcase *BinaryExpr:\n+\t\treturn typeCheckBinaryExpr(t)\n+\n+\tcase *UnaryExpr:\n+\t\treturn typeCheckUnaryExpr(t)\n+\n+\tcase *FuncExpr:\n+\t\treturn typeCheckFuncExpr(t)\n+\n+\tcase *CaseExpr:\n+\t\treturn typeCheckCaseExpr(t)\n+\n+\tcase *CastExpr:\n+\t\treturn typeCheckCastExpr(t)\n+\n+\tdefault:\n+\t\treturn nil, util.Errorf(\"typeCheck: unsupported expression: %T\", expr)\n+\t}\n+\n+\treturn nil, util.Errorf(\"typeCheck: unexpected expression: %T\", expr)\n+}\n+\n+func typeCheckBooleanExprs(exprs ...Expr) (Datum, error) {\n+\tfor _, expr := range exprs {\n+\t\tdummyExpr, err := TypeCheckExpr(expr)\n+\t\tif err != nil {\n+\t\t\treturn nil, err\n+\t\t}\n+\t\tif _, ok := dummyExpr.(DBool); !ok {\n+\t\t\treturn nil, fmt.Errorf(\"incompatible AND argument type %s\", dummyExpr.Type())\n+\t\t}\n+\t}\n+\treturn DummyBool, nil\n+}\n+\n+// TODO(tamird): reduce duplication with evalComparisonExpr.\n+func typeCheckComparisonExpr(expr *ComparisonExpr) (Datum, error) {\n+\tleftType, err := TypeCheckExpr(expr.Left)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\trightType, err := TypeCheckExpr(expr.Right)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\n+\treturn typeCheckComparisonOp(expr.Operator, leftType, rightType)\n+}\n+\n+func typeCheckComparisonOp(op ComparisonOp, dummyLeft, dummyRight Datum) (Datum, error) {\n+\tswitch op {\n+\tcase NE:\n+\t\t// NE(left, right) is implemented as !EQ(left, right).\n+\t\top = EQ\n+\tcase GT:\n+\t\t// GT(left, right) is implemented as LT(right, left)\n+\t\top = LT\n+\t\tdummyLeft, dummyRight = dummyRight, dummyLeft\n+\tcase GE:\n+\t\t// GE(left, right) is implemented as LE(right, left)\n+\t\top = LE\n+\t\tdummyLeft, dummyRight = dummyRight, dummyLeft\n+\tcase NotIn:\n+\t\t// NotIn(left, right) is implemented as !IN(left, right)\n+\t\top = In\n+\t}\n+\n+\tlType := reflect.TypeOf(dummyLeft)\n+\trType := reflect.TypeOf(dummyRight)\n+\n+\t// TODO(pmattis): Memoize the cmpOps lookup as we've done for unaryOps and\n+\t// binOps.\n+\tif f, ok := cmpOps[cmpArgs{op, lType, rType}]; ok {","path":"sql/parser/type_check.go","position":174,"original_position":174,"commit_id":"5742cba0cae3cdee17699ed958a4392c375029e1","original_commit_id":"5742cba0cae3cdee17699ed958a4392c375029e1","user":{"login":"tamird","id":1535036,"avatar_url":"https://avatars.githubusercontent.com/u/1535036?v=3","gravatar_id":"","url":"https://api.github.com/users/tamird","html_url":"https://github.com/tamird","followers_url":"https://api.github.com/users/tamird/followers","following_url":"https://api.github.com/users/tamird/following{/other_user}","gists_url":"https://api.github.com/users/tamird/gists{/gist_id}","starred_url":"https://api.github.com/users/tamird/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tamird/subscriptions","organizations_url":"https://api.github.com/users/tamird/orgs","repos_url":"https://api.github.com/users/tamird/repos","events_url":"https://api.github.com/users/tamird/events{/privacy}","received_events_url":"https://api.github.com/users/tamird/received_events","type":"User","site_admin":false},"body":"Fixed + test.","created_at":"2015-09-02T18:45:05Z","updated_at":"2015-09-02T18:45:05Z","html_url":"https://github.com/cockroachdb/cockroach/pull/2325#discussion_r38568962","pull_request_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2325","_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/38568962"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/2325#discussion_r38568962"},"pull_request":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2325"}}},"pull_request":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2325","id":43929408,"html_url":"https://github.com/cockroachdb/cockroach/pull/2325","diff_url":"https://github.com/cockroachdb/cockroach/pull/2325.diff","patch_url":"https://github.com/cockroachdb/cockroach/pull/2325.patch","issue_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/2325","number":2325,"state":"open","locked":false,"title":"sql: division by zero + TypeCheckExpr","user":{"login":"tamird","id":1535036,"avatar_url":"https://avatars.githubusercontent.com/u/1535036?v=3","gravatar_id":"","url":"https://api.github.com/users/tamird","html_url":"https://github.com/tamird","followers_url":"https://api.github.com/users/tamird/followers","following_url":"https://api.github.com/users/tamird/following{/other_user}","gists_url":"https://api.github.com/users/tamird/gists{/gist_id}","starred_url":"https://api.github.com/users/tamird/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tamird/subscriptions","organizations_url":"https://api.github.com/users/tamird/orgs","repos_url":"https://api.github.com/users/tamird/repos","events_url":"https://api.github.com/users/tamird/events{/privacy}","received_events_url":"https://api.github.com/users/tamird/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-09-01T21:32:20Z","updated_at":"2015-09-02T18:45:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ef576a32cf8b3864cd793e52bd65e112ea6760c3","assignee":{"login":"petermattis","id":6201335,"avatar_url":"https://avatars.githubusercontent.com/u/6201335?v=3","gravatar_id":"","url":"https://api.github.com/users/petermattis","html_url":"https://github.com/petermattis","followers_url":"https://api.github.com/users/petermattis/followers","following_url":"https://api.github.com/users/petermattis/following{/other_user}","gists_url":"https://api.github.com/users/petermattis/gists{/gist_id}","starred_url":"https://api.github.com/users/petermattis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petermattis/subscriptions","organizations_url":"https://api.github.com/users/petermattis/orgs","repos_url":"https://api.github.com/users/petermattis/repos","events_url":"https://api.github.com/users/petermattis/events{/privacy}","received_events_url":"https://api.github.com/users/petermattis/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2325/commits","review_comments_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2325/comments","review_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/2325/comments","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/5742cba0cae3cdee17699ed958a4392c375029e1","head":{"label":"tamird:divide-by-zero","ref":"divide-by-zero","sha":"5742cba0cae3cdee17699ed958a4392c375029e1","user":{"login":"tamird","id":1535036,"avatar_url":"https://avatars.githubusercontent.com/u/1535036?v=3","gravatar_id":"","url":"https://api.github.com/users/tamird","html_url":"https://github.com/tamird","followers_url":"https://api.github.com/users/tamird/followers","following_url":"https://api.github.com/users/tamird/following{/other_user}","gists_url":"https://api.github.com/users/tamird/gists{/gist_id}","starred_url":"https://api.github.com/users/tamird/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tamird/subscriptions","organizations_url":"https://api.github.com/users/tamird/orgs","repos_url":"https://api.github.com/users/tamird/repos","events_url":"https://api.github.com/users/tamird/events{/privacy}","received_events_url":"https://api.github.com/users/tamird/received_events","type":"User","site_admin":false},"repo":{"id":35114205,"name":"cockroach","full_name":"tamird/cockroach","owner":{"login":"tamird","id":1535036,"avatar_url":"https://avatars.githubusercontent.com/u/1535036?v=3","gravatar_id":"","url":"https://api.github.com/users/tamird","html_url":"https://github.com/tamird","followers_url":"https://api.github.com/users/tamird/followers","following_url":"https://api.github.com/users/tamird/following{/other_user}","gists_url":"https://api.github.com/users/tamird/gists{/gist_id}","starred_url":"https://api.github.com/users/tamird/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tamird/subscriptions","organizations_url":"https://api.github.com/users/tamird/orgs","repos_url":"https://api.github.com/users/tamird/repos","events_url":"https://api.github.com/users/tamird/events{/privacy}","received_events_url":"https://api.github.com/users/tamird/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tamird/cockroach","description":"A Scalable, Geo-Replicated, Transactional Datastore","fork":true,"url":"https://api.github.com/repos/tamird/cockroach","forks_url":"https://api.github.com/repos/tamird/cockroach/forks","keys_url":"https://api.github.com/repos/tamird/cockroach/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tamird/cockroach/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tamird/cockroach/teams","hooks_url":"https://api.github.com/repos/tamird/cockroach/hooks","issue_events_url":"https://api.github.com/repos/tamird/cockroach/issues/events{/number}","events_url":"https://api.github.com/repos/tamird/cockroach/events","assignees_url":"https://api.github.com/repos/tamird/cockroach/assignees{/user}","branches_url":"https://api.github.com/repos/tamird/cockroach/branches{/branch}","tags_url":"https://api.github.com/repos/tamird/cockroach/tags","blobs_url":"https://api.github.com/repos/tamird/cockroach/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tamird/cockroach/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tamird/cockroach/git/refs{/sha}","trees_url":"https://api.github.com/repos/tamird/cockroach/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tamird/cockroach/statuses/{sha}","languages_url":"https://api.github.com/repos/tamird/cockroach/languages","stargazers_url":"https://api.github.com/repos/tamird/cockroach/stargazers","contributors_url":"https://api.github.com/repos/tamird/cockroach/contributors","subscribers_url":"https://api.github.com/repos/tamird/cockroach/subscribers","subscription_url":"https://api.github.com/repos/tamird/cockroach/subscription","commits_url":"https://api.github.com/repos/tamird/cockroach/commits{/sha}","git_commits_url":"https://api.github.com/repos/tamird/cockroach/git/commits{/sha}","comments_url":"https://api.github.com/repos/tamird/cockroach/comments{/number}","issue_comment_url":"https://api.github.com/repos/tamird/cockroach/issues/comments{/number}","contents_url":"https://api.github.com/repos/tamird/cockroach/contents/{+path}","compare_url":"https://api.github.com/repos/tamird/cockroach/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tamird/cockroach/merges","archive_url":"https://api.github.com/repos/tamird/cockroach/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tamird/cockroach/downloads","issues_url":"https://api.github.com/repos/tamird/cockroach/issues{/number}","pulls_url":"https://api.github.com/repos/tamird/cockroach/pulls{/number}","milestones_url":"https://api.github.com/repos/tamird/cockroach/milestones{/number}","notifications_url":"https://api.github.com/repos/tamird/cockroach/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tamird/cockroach/labels{/name}","releases_url":"https://api.github.com/repos/tamird/cockroach/releases{/id}","created_at":"2015-05-05T17:34:04Z","updated_at":"2015-05-06T22:06:29Z","pushed_at":"2015-09-02T18:10:15Z","git_url":"git://github.com/tamird/cockroach.git","ssh_url":"git@github.com:tamird/cockroach.git","clone_url":"https://github.com/tamird/cockroach.git","svn_url":"https://github.com/tamird/cockroach","homepage":null,"size":30237,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"cockroachdb:master","ref":"master","sha":"a481880b2efcd98aa69bf3d457cae05cb9aa1050","user":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"repo":{"id":16563587,"name":"cockroach","full_name":"cockroachdb/cockroach","owner":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cockroachdb/cockroach","description":"A Scalable, Geo-Replicated, Transactional Datastore","fork":false,"url":"https://api.github.com/repos/cockroachdb/cockroach","forks_url":"https://api.github.com/repos/cockroachdb/cockroach/forks","keys_url":"https://api.github.com/repos/cockroachdb/cockroach/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cockroachdb/cockroach/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cockroachdb/cockroach/teams","hooks_url":"https://api.github.com/repos/cockroachdb/cockroach/hooks","issue_events_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/events{/number}","events_url":"https://api.github.com/repos/cockroachdb/cockroach/events","assignees_url":"https://api.github.com/repos/cockroachdb/cockroach/assignees{/user}","branches_url":"https://api.github.com/repos/cockroachdb/cockroach/branches{/branch}","tags_url":"https://api.github.com/repos/cockroachdb/cockroach/tags","blobs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cockroachdb/cockroach/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/refs{/sha}","trees_url":"https://api.github.com/repos/cockroachdb/cockroach/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/{sha}","languages_url":"https://api.github.com/repos/cockroachdb/cockroach/languages","stargazers_url":"https://api.github.com/repos/cockroachdb/cockroach/stargazers","contributors_url":"https://api.github.com/repos/cockroachdb/cockroach/contributors","subscribers_url":"https://api.github.com/repos/cockroachdb/cockroach/subscribers","subscription_url":"https://api.github.com/repos/cockroachdb/cockroach/subscription","commits_url":"https://api.github.com/repos/cockroachdb/cockroach/commits{/sha}","git_commits_url":"https://api.github.com/repos/cockroachdb/cockroach/git/commits{/sha}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/comments{/number}","issue_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/comments{/number}","contents_url":"https://api.github.com/repos/cockroachdb/cockroach/contents/{+path}","compare_url":"https://api.github.com/repos/cockroachdb/cockroach/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cockroachdb/cockroach/merges","archive_url":"https://api.github.com/repos/cockroachdb/cockroach/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cockroachdb/cockroach/downloads","issues_url":"https://api.github.com/repos/cockroachdb/cockroach/issues{/number}","pulls_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls{/number}","milestones_url":"https://api.github.com/repos/cockroachdb/cockroach/milestones{/number}","notifications_url":"https://api.github.com/repos/cockroachdb/cockroach/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cockroachdb/cockroach/labels{/name}","releases_url":"https://api.github.com/repos/cockroachdb/cockroach/releases{/id}","created_at":"2014-02-06T00:18:47Z","updated_at":"2015-09-02T18:03:45Z","pushed_at":"2015-09-02T18:10:16Z","git_url":"git://github.com/cockroachdb/cockroach.git","ssh_url":"git@github.com:cockroachdb/cockroach.git","clone_url":"https://github.com/cockroachdb/cockroach.git","svn_url":"https://github.com/cockroachdb/cockroach","homepage":null,"size":53719,"stargazers_count":5076,"watchers_count":5076,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":406,"mirror_url":null,"open_issues_count":159,"forks":406,"open_issues":159,"watchers":5076,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2325"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/2325"},"issue":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/2325"},"comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/2325/comments"},"review_comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2325/comments"},"review_comment":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2325/commits"},"statuses":{"href":"https://api.github.com/repos/cockroachdb/cockroach/statuses/5742cba0cae3cdee17699ed958a4392c375029e1"}}}}