michaelmior commented on a change in pull request #2111: URL: https://github.com/apache/calcite/pull/2111#discussion_r475101063
########## File path: core/src/main/java/org/apache/calcite/rel/rules/SpatialRules.java ########## @@ -0,0 +1,322 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.calcite.rel.rules; + +import org.apache.calcite.plan.RelOptPredicateList; +import org.apache.calcite.plan.RelOptRule; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.runtime.GeoFunctions; +import org.apache.calcite.runtime.Geometries; +import org.apache.calcite.runtime.HilbertCurve2D; +import org.apache.calcite.runtime.SpaceFillingCurve2D; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.fun.SqlStdOperatorTable; +import org.apache.calcite.tools.RelBuilder; + +import com.esri.core.geometry.Envelope; +import com.esri.core.geometry.Point; +import com.google.common.collect.ImmutableList; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; + +/** + * Collection of planner rules that convert + * calls to spatial functions into more efficient expressions. + * + * <p>The rules allow Calcite to use spatial indexes. For example the following + * query: + * + * <blockquote>SELECT ... + * FROM Restaurants AS r + * WHERE ST_DWithin(ST_Point(10, 20), ST_Point(r.longitude, r.latitude), 5) + * </blockquote> + * + * <p>is rewritten to + * + * <blockquote>SELECT ... + * FROM Restaurants AS r + * WHERE (r.h BETWEEN 100 AND 150 + * OR r.h BETWEEN 170 AND 185) + * AND ST_DWithin(ST_Point(10, 20), ST_Point(r.longitude, r.latitude), 5) + * </blockquote> + * + * <p>if there is the constraint + * + * <blockquote>CHECK (h = Hilbert(8, r.longitude, r.latitude))</blockquote> + * + * <p>If the {@code Restaurants} table is sorted on {@code h} then the latter + * query can be answered using two limited range-scans, and so is much more + * efficient. + * + * <p>Note that the original predicate + * {@code ST_DWithin(ST_Point(10, 20), ST_Point(r.longitude, r.latitude), 5)} + * is still present, but is evaluated after the approximate predicate has + * eliminated many potential matches. + */ +public abstract class SpatialRules { + + private SpatialRules() {} + + private static final RexUtil.RexFinder DWITHIN_FINDER = + RexUtil.find(EnumSet.of(SqlKind.ST_DWITHIN, SqlKind.ST_CONTAINS)); + + private static final RexUtil.RexFinder HILBERT_FINDER = + RexUtil.find(SqlKind.HILBERT); + + public static final RelOptRule INSTANCE = + FilterHilbertRule.Config.DEFAULT.toRule(); + + /** Returns a geometry if an expression is constant, null otherwise. */ + private static Geometries.Geom constantGeom(RexNode e) { + switch (e.getKind()) { + case CAST: + return constantGeom(((RexCall) e).getOperands().get(0)); + case LITERAL: + return (Geometries.Geom) ((RexLiteral) e).getValue(); + default: + return null; + } + } + + /** Rule that converts ST_DWithin in a Filter condition into a predicate on + * a Hilbert curve. */ + @SuppressWarnings("WeakerAccess") + public static class FilterHilbertRule + extends RelRule<FilterHilbertRule.Config> { + protected FilterHilbertRule(Config config) { + super(config); + } + + @Override public void onMatch(RelOptRuleCall call) { + final Filter filter = call.rel(0); + final List<RexNode> conjunctions = new ArrayList<>(); + RelOptUtil.decomposeConjunction(filter.getCondition(), conjunctions); + + // Match a predicate + // r.hilbert = hilbert(r.longitude, r.latitude) + // to one of the conjunctions + // ST_DWithin(ST_Point(x, y), ST_Point(r.longitude, r.latitude), d) + // and if it matches add a new conjunction before it, + // r.hilbert between h1 and h2 + // or r.hilbert between h3 and h4 + // where {[h1, h2], [h3, h4]} are the ranges of the Hilbert curve + // intersecting the square + // (r.longitude - d, r.latitude - d, r.longitude + d, r.latitude + d) + final RelOptPredicateList predicates = + call.getMetadataQuery().getAllPredicates(filter.getInput()); + int changeCount = 0; + for (RexNode predicate : predicates.pulledUpPredicates) { + final RelBuilder builder = call.builder(); + if (predicate.getKind() == SqlKind.EQUALS) { + final RexCall eqCall = (RexCall) predicate; + if (eqCall.operands.get(0) instanceof RexInputRef + && eqCall.operands.get(1).getKind() == SqlKind.HILBERT) { + final RexInputRef ref = (RexInputRef) eqCall.operands.get(0); Review comment: My mistake. I didn't realize this was checking in the constraint. My point was that eventually we would ideally want to read metadata from the database to populate those constraints automatically. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
