Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 733ee4e

Browse files
committedSep 11, 2024·
[CALCITE-6575] Refactor SqlValidatorImpl: Add NamespaceBuilder
Adding NamespaceBuilder to formalize the api for customizing/reusing namespaces downstream.
1 parent 018f9c0 commit 733ee4e

File tree

5 files changed

+227
-148
lines changed

5 files changed

+227
-148
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.calcite.sql.validate;
18+
19+
import org.apache.calcite.sql.SqlCall;
20+
import org.apache.calcite.sql.SqlDelete;
21+
import org.apache.calcite.sql.SqlInsert;
22+
import org.apache.calcite.sql.SqlMatchRecognize;
23+
import org.apache.calcite.sql.SqlMerge;
24+
import org.apache.calcite.sql.SqlNode;
25+
import org.apache.calcite.sql.SqlPivot;
26+
import org.apache.calcite.sql.SqlSelect;
27+
import org.apache.calcite.sql.SqlUnpivot;
28+
import org.apache.calcite.sql.SqlUpdate;
29+
30+
import org.checkerframework.checker.initialization.qual.UnknownInitialization;
31+
import org.checkerframework.checker.nullness.qual.Nullable;
32+
33+
import static java.util.Objects.requireNonNull;
34+
35+
/**
36+
* A builder to allow for customization of namespaces down stream.
37+
*/
38+
public class NamespaceBuilder {
39+
40+
private final SqlValidatorImpl sqlValidatorImpl;
41+
42+
public NamespaceBuilder(@UnknownInitialization SqlValidatorImpl sqlValidatorImpl) {
43+
this.sqlValidatorImpl = sqlValidatorImpl;
44+
}
45+
46+
/**
47+
* Creates a namespace for a <code>SELECT</code> node. Derived class may
48+
* override this factory method.
49+
*
50+
* @param select Select node
51+
* @param enclosingNode Enclosing node
52+
* @return Select namespace
53+
*/
54+
public SelectNamespace createSelectNamespace(
55+
SqlSelect select,
56+
SqlNode enclosingNode) {
57+
return new SelectNamespace(sqlValidatorImpl, select, enclosingNode);
58+
}
59+
60+
/**
61+
* Creates a namespace for a set operation (<code>UNION</code>, <code>
62+
* INTERSECT</code>, or <code>EXCEPT</code>). Derived class may override
63+
* this factory method.
64+
*
65+
* @param call Call to set operation
66+
* @param enclosingNode Enclosing node
67+
* @return Set operation namespace
68+
*/
69+
public SetopNamespace createSetopNamespace(
70+
SqlCall call,
71+
SqlNode enclosingNode) {
72+
return new SetopNamespace(sqlValidatorImpl, call, enclosingNode);
73+
}
74+
75+
76+
public MatchRecognizeNamespace createMatchRecognizeNameSpace(
77+
SqlMatchRecognize call,
78+
SqlNode enclosingNode) {
79+
return new MatchRecognizeNamespace(sqlValidatorImpl, call, enclosingNode);
80+
}
81+
82+
public UnpivotNamespace createUnpivotNameSpace(
83+
SqlUnpivot call,
84+
SqlNode enclosingNode) {
85+
return new UnpivotNamespace(sqlValidatorImpl, call, enclosingNode);
86+
}
87+
88+
89+
public PivotNamespace createPivotNameSpace(
90+
SqlPivot call,
91+
SqlNode enclosingNode) {
92+
return new PivotNamespace(sqlValidatorImpl, call, enclosingNode);
93+
}
94+
95+
public DmlNamespace createDeleteNamespace(SqlDelete delete,
96+
SqlNode enclosingNode, SqlValidatorScope parentScope) {
97+
return new DeleteNamespace(sqlValidatorImpl, delete, enclosingNode, parentScope);
98+
}
99+
100+
public DmlNamespace createInsertNamespace(SqlInsert insert,
101+
SqlNode enclosingNode, SqlValidatorScope parentScope) {
102+
return new InsertNamespace(sqlValidatorImpl, insert, enclosingNode, parentScope);
103+
}
104+
105+
public DmlNamespace createMergeNamespace(SqlMerge merge, SqlNode enclosing,
106+
SqlValidatorScope parentScope) {
107+
return new MergeNamespace(sqlValidatorImpl, merge, enclosing, parentScope);
108+
}
109+
110+
public DmlNamespace createUpdate(SqlUpdate sqlUpdate, SqlNode enclosing,
111+
SqlValidatorScope parentScope) {
112+
return new UpdateNamespace(sqlValidatorImpl, sqlUpdate, enclosing, parentScope);
113+
}
114+
115+
116+
/**
117+
* Common base class for DML statement namespaces.
118+
*/
119+
public abstract static class DmlNamespace extends IdentifierNamespace {
120+
protected DmlNamespace(SqlValidatorImpl validator, SqlNode id,
121+
SqlNode enclosingNode, SqlValidatorScope parentScope) {
122+
super(validator, id, enclosingNode, parentScope);
123+
}
124+
}
125+
126+
/**
127+
* Namespace for an INSERT statement.
128+
*/
129+
private static class InsertNamespace extends DmlNamespace {
130+
private final SqlInsert node;
131+
132+
InsertNamespace(SqlValidatorImpl validator, SqlInsert node,
133+
SqlNode enclosingNode, SqlValidatorScope parentScope) {
134+
super(validator, node.getTargetTable(), enclosingNode, parentScope);
135+
this.node = requireNonNull(node, "node");
136+
}
137+
138+
@Override public @Nullable SqlNode getNode() {
139+
return node;
140+
}
141+
}
142+
143+
/**
144+
* Namespace for an UPDATE statement.
145+
*/
146+
private static class UpdateNamespace extends DmlNamespace {
147+
private final SqlUpdate node;
148+
149+
UpdateNamespace(SqlValidatorImpl validator, SqlUpdate node,
150+
SqlNode enclosingNode, SqlValidatorScope parentScope) {
151+
super(validator, node.getTargetTable(), enclosingNode, parentScope);
152+
this.node = requireNonNull(node, "node");
153+
}
154+
155+
@Override public @Nullable SqlNode getNode() {
156+
return node;
157+
}
158+
}
159+
160+
/**
161+
* Namespace for a DELETE statement.
162+
*/
163+
private static class DeleteNamespace extends DmlNamespace {
164+
private final SqlDelete node;
165+
166+
DeleteNamespace(SqlValidatorImpl validator, SqlDelete node,
167+
SqlNode enclosingNode, SqlValidatorScope parentScope) {
168+
super(validator, node.getTargetTable(), enclosingNode, parentScope);
169+
this.node = requireNonNull(node, "node");
170+
}
171+
172+
@Override public @Nullable SqlNode getNode() {
173+
return node;
174+
}
175+
}
176+
177+
/**
178+
* Namespace for a MERGE statement.
179+
*/
180+
private static class MergeNamespace extends DmlNamespace {
181+
private final SqlMerge node;
182+
183+
MergeNamespace(SqlValidatorImpl validator, SqlMerge node,
184+
SqlNode enclosingNode, SqlValidatorScope parentScope) {
185+
super(validator, node.getTargetTable(), enclosingNode, parentScope);
186+
this.node = requireNonNull(node, "node");
187+
}
188+
189+
@Override public @Nullable SqlNode getNode() {
190+
return node;
191+
}
192+
}
193+
}

‎core/src/main/java/org/apache/calcite/sql/validate/SqlValidator.java

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.apache.calcite.sql.validate.implicit.TypeCoercions;
5353

5454
import org.apiguardian.api.API;
55+
import org.checkerframework.checker.initialization.qual.UnknownInitialization;
5556
import org.checkerframework.checker.nullness.qual.Nullable;
5657
import org.checkerframework.dataflow.qual.Pure;
5758
import org.immutables.value.Value;
@@ -883,6 +884,15 @@ Config withSqlQueryScopeFactory(
883884
return SqlQueryScopesImpl::new;
884885
}
885886

887+
/** Set a factory for name space builder to allow for custom behavior downstream. */
888+
Config withNamespaceBuilderFactory(
889+
Function<@UnknownInitialization SqlValidatorImpl, NamespaceBuilder> factory);
890+
891+
/** Returns a NamespaceBuilder factory. */
892+
@Value.Default default Function<@UnknownInitialization SqlValidatorImpl, NamespaceBuilder>
893+
namespaceBuilderFactory() {
894+
return NamespaceBuilder::new;
895+
}
886896

887897
/** Returns the SQL conformance.
888898
*

0 commit comments

Comments
 (0)
Please sign in to comment.