Skip to content

Commit f25e28b

Browse files
feat: Piped SQL and FROM queries (WIP)
- `FROM Query` extending `SELECT` - `WHERE` operator - `AGGREGATE` operator - `ORDER BY` operator Signed-off-by: Andreas Reichel <andreas@manticore-projects.com> Signed-off-by: manticore-projects <andreas@manticore-projects.com>
1 parent 1eab6f0 commit f25e28b

File tree

84 files changed

+1895
-924
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1895
-924
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Maven deploy snapshot](https://github.com/JSQLParser/JSqlParser/actions/workflows/maven_deploy.yml/badge.svg)](https://github.com/JSQLParser/JSqlParser/actions/workflows/maven_deploy.yml)
44
[![Gradle CI](https://github.com/JSQLParser/JSqlParser/actions/workflows/gradle.yml/badge.svg)](https://github.com/JSQLParser/JSqlParser/actions/workflows/gradle.yml)
5-
[![Coverage Status](https://coveralls.io/repos/JSQLParser/JSqlParser/badge.svg?branch=master)](https://coveralls.io/r/JSQLParser/JSqlParser?branch=master)
5+
[![Coverage Status](https://coveralls.io/repos/JSQLParser/JSqlParser/badge.svg?branch=master)](https://coveralls.io/r/JSQLParser/JSqlParser?branch=master)
66
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/6f9a2d7eb98f45969749e101322634a1)](https://www.codacy.com/gh/JSQLParser/JSqlParser/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=JSQLParser/JSqlParser&amp;utm_campaign=Badge_Grade)
77
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.jsqlparser/jsqlparser/badge.svg)](http://maven-badges.herokuapp.com/maven-central/com.github.jsqlparser/jsqlparser) [![Javadocs](https://www.javadoc.io/badge/com.github.jsqlparser/jsqlparser.svg)](https://www.javadoc.io/doc/com.github.jsqlparser/jsqlparser)
88
[![Gitter](https://badges.gitter.im/JSQLParser/JSqlParser.svg)](https://gitter.im/JSQLParser/JSqlParser?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
@@ -47,6 +47,22 @@ Assertions.assertEquals("a", a.getColumnName());
4747
Assertions.assertEquals("b", b.getColumnName());
4848
}
4949
```
50+
## Support for `Piped SQL`
51+
52+
Work is progressing for parsing `Piped SQL`, a much saner and more logical way to write queries in its semantic order.
53+
```sql
54+
FROM Produce
55+
|> WHERE
56+
item != 'bananas'
57+
AND category IN ('fruit', 'nut')
58+
|> AGGREGATE COUNT(*) AS num_items, SUM(sales) AS total_sales
59+
GROUP BY item
60+
|> ORDER BY item DESC;
61+
```
62+
63+
For details, please see https://storage.googleapis.com/gweb-research2023-media/pubtools/1004848.pdf, https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax and https://duckdb.org/docs/sql/query_syntax/from.html#from-first-syntax
64+
65+
## Java Version
5066

5167
JSQLParser-4.9 was the last JDK8 compatible version. The recent JSQLParser-5.0 depends on JDK11 and introduces API breaking changes to the AST Visitors. Please see the Migration Guide for the details.
5268

src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
exports net.sf.jsqlparser.statement.grant;
3939
exports net.sf.jsqlparser.statement.insert;
4040
exports net.sf.jsqlparser.statement.merge;
41+
exports net.sf.jsqlparser.statement.piped;
4142
exports net.sf.jsqlparser.statement.refresh;
4243
exports net.sf.jsqlparser.statement.select;
4344
exports net.sf.jsqlparser.statement.show;

src/main/java/net/sf/jsqlparser/expression/Expression.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,4 @@ default <T> void accept(ExpressionVisitor<T> expressionVisitor) {
2020
this.accept(expressionVisitor, null);
2121
}
2222

23-
;
24-
25-
2623
}

src/main/java/net/sf/jsqlparser/expression/ExpressionVisitor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import net.sf.jsqlparser.expression.operators.relational.TSQLLeftJoin;
5757
import net.sf.jsqlparser.expression.operators.relational.TSQLRightJoin;
5858
import net.sf.jsqlparser.schema.Column;
59+
import net.sf.jsqlparser.statement.piped.FromQuery;
5960
import net.sf.jsqlparser.statement.select.AllColumns;
6061
import net.sf.jsqlparser.statement.select.AllTableColumns;
6162
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
@@ -680,4 +681,6 @@ default void visit(Inverse inverse) {
680681
}
681682

682683
<S> T visit(CosineSimilarity cosineSimilarity, S context);
684+
685+
<S> T visit(FromQuery fromQuery, S context);
683686
}

src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import net.sf.jsqlparser.expression.operators.relational.TSQLLeftJoin;
5757
import net.sf.jsqlparser.expression.operators.relational.TSQLRightJoin;
5858
import net.sf.jsqlparser.schema.Column;
59+
import net.sf.jsqlparser.statement.piped.FromQuery;
5960
import net.sf.jsqlparser.statement.select.AllColumns;
6061
import net.sf.jsqlparser.statement.select.AllTableColumns;
6162
import net.sf.jsqlparser.statement.select.OrderByElement;
@@ -813,4 +814,9 @@ public <S> T visit(CosineSimilarity cosineSimilarity, S context) {
813814
return null;
814815
}
815816

817+
@Override
818+
public <S> T visit(FromQuery fromQuery, S context) {
819+
return null;
820+
}
821+
816822
}

src/main/java/net/sf/jsqlparser/parser/ASTNodeAccessImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,9 @@ public <T extends ASTNodeAccess> T getParent(Class<T> clazz) {
6565

6666
return clazz.cast(parent.jjtGetValue());
6767
}
68+
69+
@Override
70+
public String toString() {
71+
return appendTo(new StringBuilder()).toString();
72+
}
6873
}

src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,5 +323,4 @@ default void visit(ParenthesedUpdate parenthesedUpdate) {
323323
default void visit(ParenthesedDelete parenthesedDelete) {
324324
this.visit(parenthesedDelete, null);
325325
}
326-
327326
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package net.sf.jsqlparser.statement.piped;
2+
3+
import net.sf.jsqlparser.statement.select.SelectItem;
4+
5+
import java.util.ArrayList;
6+
7+
public class AggregatePipeOperator extends PipeOperator {
8+
private final ArrayList<SelectItem<?>> selectItems = new ArrayList<>();
9+
private final ArrayList<SelectItem<?>> groupItems = new ArrayList<>();
10+
private boolean usingShortHandOrdering = false;
11+
12+
public AggregatePipeOperator(SelectItem<?> selectItem) {
13+
selectItems.add(selectItem);
14+
}
15+
16+
public ArrayList<SelectItem<?>> getSelectItems() {
17+
return selectItems;
18+
}
19+
20+
public ArrayList<SelectItem<?>> getGroupItems() {
21+
return groupItems;
22+
}
23+
24+
public AggregatePipeOperator add(SelectItem<?> selectItem) {
25+
selectItems.add(selectItem);
26+
return this;
27+
}
28+
29+
public AggregatePipeOperator with(SelectItem<?> selectItem) {
30+
return this.add(selectItem);
31+
}
32+
33+
public AggregatePipeOperator addGroupItem(SelectItem<?> selectItem) {
34+
groupItems.add(selectItem);
35+
return this;
36+
}
37+
38+
public AggregatePipeOperator withGroupItem(SelectItem<?> selectItem) {
39+
return this.addGroupItem(selectItem);
40+
}
41+
42+
public boolean isUsingShortHandOrdering() {
43+
return usingShortHandOrdering;
44+
}
45+
46+
public AggregatePipeOperator setShorthandOrdering(boolean usingShortHandOrdering) {
47+
this.usingShortHandOrdering = usingShortHandOrdering;
48+
return this;
49+
}
50+
51+
@Override
52+
public <T, S> T accept(PipeOperatorVisitor<T> visitor, S context) {
53+
return visitor.visit(this, context);
54+
}
55+
56+
@Override
57+
public StringBuilder appendTo(StringBuilder builder) {
58+
builder.append("|> ").append("AGGREGATE");
59+
int i = 0;
60+
for (SelectItem<?> selectItem : selectItems) {
61+
builder.append(i++ > 0 ? ", " : " ").append(selectItem);
62+
}
63+
builder.append("\n");
64+
65+
if (!groupItems.isEmpty()) {
66+
builder.append("\t").append("GROUP");
67+
if (isUsingShortHandOrdering()) {
68+
builder.append(" AND ORDER");
69+
}
70+
builder.append(" BY");
71+
i = 0;
72+
for (SelectItem<?> selectItem : groupItems) {
73+
builder.append(i++ > 0 ? ", " : " ").append(selectItem);
74+
}
75+
builder.append("\n");
76+
}
77+
78+
return builder;
79+
}
80+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.sf.jsqlparser.statement.piped;
2+
3+
public class AsPipeOperator extends PipeOperator {
4+
5+
@Override
6+
public <T, S> T accept(PipeOperatorVisitor<T> visitor, S context) {
7+
return visitor.visit(this, context);
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.sf.jsqlparser.statement.piped;
2+
3+
public class CallPipeOperator extends PipeOperator {
4+
5+
@Override
6+
public <T, S> T accept(PipeOperatorVisitor<T> visitor, S context) {
7+
return visitor.visit(this, context);
8+
}
9+
}

0 commit comments

Comments
 (0)