Repository: tomarrell/lbadd (go)

Issue:
parser: select expression doesn't work
Try to parse `SELECT amount * price AS total_price FROM items`. This will throw a lot of errors, although the production

`SELECT result-column ...`

with `result_column` using the `expr` production, and `expr` using the `expr binary-operator expr` allows such a statement.

Pull request:
Patched result column
Fixes #146 

Fixed bug, result column can take recursive expr.

Adds test for proof.

Patch:
diff --git a/internal/parser/simple_parser_rules.go b/internal/parser/simple_parser_rules.go
index 86adae8..ff58f4a 100644
--- a/internal/parser/simple_parser_rules.go
+++ b/internal/parser/simple_parser_rules.go
@@ -3731,7 +3731,12 @@ func (p *simpleParser) parseResultColumn(r reporter) (stmt *ast.ResultColumn) {
 				stmt.Period = period
 			}
 		} else {
-			stmt.Expr = &ast.Expr{LiteralValue: tableNameOrAsteriskOrExpr}
+			recExpr := p.parseExprRecursive(&ast.Expr{LiteralValue: tableNameOrAsteriskOrExpr}, r)
+			if recExpr != nil {
+				stmt.Expr = recExpr
+			} else {
+				stmt.Expr = &ast.Expr{LiteralValue: tableNameOrAsteriskOrExpr}
+			}
 		}
 		next, ok := p.optionalLookahead(r)
 		if !ok || next.Type() == token.EOF || next.Type() == token.StatementSeparator {


Test patch:
diff --git a/internal/parser/parser_test.go b/internal/parser/parser_test.go
index 3f37014..49de30a 100644
--- a/internal/parser/parser_test.go
+++ b/internal/parser/parser_test.go
@@ -9611,6 +9611,40 @@ func TestSingleStatementParse(t *testing.T) {
 				},
 			},
 		},
+		{
+			`SELECT stms's result column with recursive expr`,
+			"SELECT amount * price AS total_price FROM items",
+			&ast.SQLStmt{
+				SelectStmt: &ast.SelectStmt{
+					SelectCore: []*ast.SelectCore{
+						{
+							Select: token.New(1, 1, 0, 6, token.KeywordSelect, "SELECT"),
+							ResultColumn: []*ast.ResultColumn{
+								{
+									Expr: &ast.Expr{
+										Expr1: &ast.Expr{
+											LiteralValue: token.New(1, 8, 7, 6, token.Literal, "amount"),
+										},
+										BinaryOperator: token.New(1, 15, 14, 1, token.BinaryOperator, "*"),
+										Expr2: &ast.Expr{
+											LiteralValue: token.New(1, 17, 16, 5, token.Literal, "price"),
+										},
+									},
+									As:          token.New(1, 23, 22, 2, token.KeywordAs, "AS"),
+									ColumnAlias: token.New(1, 26, 25, 11, token.Literal, "total_price"),
+								},
+							},
+							From: token.New(1, 38, 37, 4, token.KeywordFrom, "FROM"),
+							JoinClause: &ast.JoinClause{
+								TableOrSubquery: &ast.TableOrSubquery{
+									TableName: token.New(1, 43, 42, 5, token.Literal, "items"),
+								},
+							},
+						},
+					},
+				},
+			},
+		},
 	}
 
 	for _, input := range inputs {