Skip to content

Commit 5733e64

Browse files
wrangler sql execution initial setup
1 parent 5790e4b commit 5733e64

File tree

7 files changed

+307
-41
lines changed

7 files changed

+307
-41
lines changed

wrangler-api/src/main/java/io/cdap/wrangler/api/Directive.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.cdap.wrangler.api;
1818

19+
import io.cdap.cdap.etl.api.relational.Relation;
20+
import io.cdap.cdap.etl.api.relational.RelationalTranformContext;
1921
import io.cdap.wrangler.api.parser.UsageDefinition;
2022

2123
import java.util.List;
@@ -51,7 +53,8 @@
5153
* }
5254
* </code>
5355
*/
54-
public interface Directive extends Executor<List<Row>, List<Row>>, EntityMetrics {
56+
public interface Directive extends Executor<List<Row>, List<Row>>, EntityMetrics,
57+
DirectiveRelationalTransform {
5558
/**
5659
* This defines a interface variable that is static and final for specify
5760
* the {@code type} of the plugin this interface would provide.
@@ -126,4 +129,11 @@ default List<EntityCountMetric> getCountMetrics() {
126129
// no op
127130
return null;
128131
}
132+
133+
@Override
134+
default Relation transform(RelationalTranformContext relationalTranformContext,
135+
Relation relation) {
136+
// no-op
137+
return relation;
138+
}
129139
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright © 2023 Cask Data, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package io.cdap.wrangler.api;
18+
19+
import io.cdap.cdap.etl.api.relational.InvalidRelation;
20+
import io.cdap.cdap.etl.api.relational.LinearRelationalTransform;
21+
import io.cdap.cdap.etl.api.relational.Relation;
22+
import io.cdap.cdap.etl.api.relational.RelationalTranformContext;
23+
24+
/**
25+
* {@link DirectiveRelationalTransform} provides relational transform support for
26+
* wrangler directives.
27+
*/
28+
public interface DirectiveRelationalTransform extends LinearRelationalTransform {
29+
30+
/**
31+
* Implementation of linear relational transform for each supported directive.
32+
*
33+
* @param relationalTranformContext transformation context with engine, input and output parameters
34+
* @param relation input relation upon which the transformation is applied.
35+
* @return transformed relation as the output relation. By default, returns an Invalid relation
36+
* for unsupported directives.
37+
*/
38+
default Relation transform(RelationalTranformContext relationalTranformContext,
39+
Relation relation) {
40+
return new InvalidRelation("SQL execution for the directive is currently not supported.");
41+
}
42+
43+
/**
44+
* Indicates whether the directive is supported by relational transformation or not.
45+
*
46+
* @return boolean value for the directive SQL support.
47+
* By default, returns false, indicating that the directive is currently not supported.
48+
*/
49+
default boolean isSQLSupported() {
50+
return false;
51+
}
52+
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright © 2017-2019 Cask Data, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package io.cdap.wrangler.api;
18+
19+
import io.cdap.cdap.etl.api.relational.LinearRelationalTransform;
20+
import io.cdap.cdap.etl.api.relational.Relation;
21+
import io.cdap.cdap.etl.api.relational.RelationalTranformContext;
22+
23+
/**
24+
* Directive interface which supports Relational transformations
25+
*/
26+
public interface RelationalDirective extends Directive, LinearRelationalTransform {
27+
28+
@Override
29+
default Relation transform(RelationalTranformContext relationalTranformContext,
30+
Relation relation) {
31+
// no-op
32+
return relation;
33+
}
34+
}

wrangler-core/src/main/java/io/cdap/directives/column/Drop.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import io.cdap.cdap.api.annotation.Description;
2020
import io.cdap.cdap.api.annotation.Name;
2121
import io.cdap.cdap.api.annotation.Plugin;
22+
import io.cdap.cdap.etl.api.relational.Relation;
23+
import io.cdap.cdap.etl.api.relational.RelationalTranformContext;
2224
import io.cdap.wrangler.api.Arguments;
2325
import io.cdap.wrangler.api.Directive;
2426
import io.cdap.wrangler.api.DirectiveExecutionException;
@@ -88,4 +90,18 @@ public Mutation lineage() {
8890
.drop(Many.of(columns))
8991
.build();
9092
}
93+
94+
@Override
95+
public Relation transform(RelationalTranformContext relationalTranformContext,
96+
Relation relation) {
97+
for (String col: columns) {
98+
relation = relation.dropColumn(col);
99+
}
100+
return relation;
101+
}
102+
103+
@Override
104+
public boolean isSQLSupported() {
105+
return true;
106+
}
91107
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright © 2023 Cask Data, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package io.cdap.wrangler.utils;
18+
19+
import io.cdap.cdap.etl.api.relational.Expression;
20+
import io.cdap.cdap.etl.api.relational.ExpressionFactory;
21+
import io.cdap.cdap.etl.api.relational.RelationalTranformContext;
22+
import io.cdap.cdap.etl.api.relational.StringExpressionFactoryType;
23+
24+
import java.util.Optional;
25+
26+
/**
27+
* Utility class that contains methods for sql expression generation.
28+
*/
29+
public class SqlExpressionGenerator {
30+
31+
public static Optional<ExpressionFactory<String>> getExpressionFactory(RelationalTranformContext ctx) {
32+
return ctx.getEngine().getExpressionFactory(StringExpressionFactoryType.SQL);
33+
}
34+
}

0 commit comments

Comments
 (0)