Skip to content

Commit 9b498cf

Browse files
committed
adding support for *
1 parent 0fd03fe commit 9b498cf

File tree

6 files changed

+59
-17
lines changed

6 files changed

+59
-17
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ This will return all the users in the `users.json` file.
3131

3232
## Supported SQL Syntax
3333

34-
- [ ] `SELECT`:
34+
- [ ] `WITH`
35+
- [ ] `RECURSIVE`
36+
37+
- [ ] `SELECT`
38+
- [ ] `ALL`
39+
- [ ] `DISTINCT`
40+
- [ ] `*`
3541
- [x] `FROM`
3642
- [x] `WHERE`
3743
- [x] `GROUP BY`

abstra_json_sql/apply.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Dict, Optional
1+
from typing import List, Dict, Optional, Union
22
from .tables import ITablesSnapshot
33
from .field_name import field_name, expression_name
44
from .ast import (
@@ -11,6 +11,7 @@
1111
From,
1212
SelectField,
1313
Where,
14+
Wildcard,
1415
GroupBy,
1516
FunctionCallExpression,
1617
Command,
@@ -22,7 +23,6 @@
2223
IsExpression,
2324
FalseExpression,
2425
TrueExpression,
25-
Wildcard,
2626
OrderBy,
2727
MinusExpression,
2828
MultiplyExpression,
@@ -33,7 +33,6 @@
3333
GreaterThanOrEqualExpression,
3434
LessThanExpression,
3535
LessThanOrEqualExpression,
36-
Wildcard,
3736
Limit,
3837
)
3938
from dataclasses import dataclass
@@ -355,6 +354,10 @@ def apply_expression(expression: Expression, ctx: dict):
355354
raise ValueError(f"Unknown function: {expression.name}")
356355
elif isinstance(expression, NullExpression):
357356
return None
357+
elif isinstance(expression, FalseExpression):
358+
return False
359+
elif isinstance(expression, TrueExpression):
360+
return True
358361
elif isinstance(expression, IsExpression):
359362
left_value = apply_expression(expression.left, ctx)
360363
right_value = apply_expression(expression.right, ctx)
@@ -503,15 +506,20 @@ def has_aggregation_fields(fields: List[SelectField]) -> bool:
503506

504507

505508
def apply_select_fields(fields: List[SelectField], data: List[dict], ctx: dict):
506-
return [
507-
{
508-
field_name(field) or field.expression: apply_expression(
509-
field.expression, {**ctx, **row}
510-
)
511-
for field in fields
512-
}
513-
for row in data
514-
]
509+
result = []
510+
for row in data:
511+
result_row = {}
512+
for field in fields:
513+
if isinstance(field.expression, Wildcard):
514+
result_row.update(row)
515+
elif isinstance(field.expression, Expression):
516+
result_row[field_name(field)] = apply_expression(
517+
field.expression, {**ctx, **row}
518+
)
519+
else:
520+
raise ValueError(f"Unsupported field type: {type(field)}")
521+
result.append(result_row)
522+
return result
515523

516524

517525
def apply_from(

abstra_json_sql/ast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class Having(Ast):
172172

173173
@dataclass
174174
class SelectField(Ast):
175-
expression: Expression
175+
expression: Union[Expression, Wildcard]
176176
alias: Optional[str] = None
177177

178178

@@ -200,7 +200,7 @@ class Limit(Ast):
200200

201201
@dataclass
202202
class Select(Command):
203-
field_parts: List[Union[SelectField, Wildcard]]
203+
field_parts: List[SelectField]
204204
from_part: Optional[From] = None
205205
where_part: Optional[Where] = None
206206
group_part: Optional[GroupBy] = None

abstra_json_sql/eval_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,31 @@ def test_complete(self):
499499
ctx = {}
500500
result = eval_sql(code=code, tables=tables, ctx=ctx)
501501
self.assertEqual(result, [{"foo": 3, "count": 2}])
502+
503+
def test_select_wildcard(self):
504+
code = "select * from bar"
505+
tables = InMemoryTables(
506+
tables=[
507+
Table(
508+
name="bar",
509+
columns=[Column(name="foo", type="text")],
510+
data=[
511+
{"foo": "a"},
512+
{"foo": "b"},
513+
{"foo": None},
514+
{"foo": "c"},
515+
],
516+
)
517+
],
518+
)
519+
ctx = {}
520+
result = eval_sql(code=code, tables=tables, ctx=ctx)
521+
self.assertEqual(
522+
result,
523+
[
524+
{"foo": "a"},
525+
{"foo": "b"},
526+
{"foo": None},
527+
{"foo": "c"},
528+
],
529+
)

abstra_json_sql/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def parse_fields(tokens: List[Token]) -> Tuple[List[SelectField], List[Token]]:
274274
if tokens[0].type == "keyword" and tokens[0].value.upper() == "FROM":
275275
break
276276
elif tokens[0].type == "wildcard":
277-
fields.append(Wildcard())
277+
fields.append(SelectField(Wildcard()))
278278
tokens = tokens[1:]
279279
elif tokens[0].type == "comma":
280280
tokens = tokens[1:]

abstra_json_sql/parser_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_select_wildcard(self):
3838
self.assertEqual(
3939
ast,
4040
Select(
41-
field_parts=[Wildcard()],
41+
field_parts=[SelectField(Wildcard())],
4242
from_part=From(
4343
table="users",
4444
),

0 commit comments

Comments
 (0)