All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cdap.plugin.gcp.bigquery.sqlengine.builder.BigQueryBaseSQLBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2022 Cask Data, Inc.
 *
 * Licensed 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 io.cdap.plugin.gcp.bigquery.sqlengine.builder;

import io.cdap.cdap.etl.api.relational.Expression;
import io.cdap.plugin.gcp.bigquery.relational.SQLExpression;

import java.util.Collection;
import java.util.Map;
import java.util.stream.Stream;

/**
 * Base class which defines convenience variables to be used then building SQL expressions
 */
public abstract class BigQueryBaseSQLBuilder {
  public static final String SELECT = "SELECT ";
  public static final String FROM = " FROM ";
  public static final String SPACE = " ";
  public static final String JOIN = " JOIN ";
  public static final String AS = " AS ";
  public static final String ON = " ON ";
  public static final String EQ = " = ";
  public static final String AND = " AND ";
  public static final String OR = " OR ";
  public static final String CURRENT_ROW = " CURRENT ROW ";
  public static final String ROWS = "ROWS";
  public static final String RANGE = "RANGE";
  public static final String BETWEEN = " BETWEEN ";
  public static final String EMPTY = "";
  public static final String UNBOUNDED_PRECEDING = "UNBOUNDED PRECEDING";
  public static final String PRECEDING = " PRECEDING ";
  public static final String UNBOUNDED_FOLLOWING = "UNBOUNDED FOLLOWING";
  public static final String FOLLOWING = " FOLLOWING ";
  public static final String DOT = ".";
  public static final String COMMA = " , ";
  public static final String IS_NULL = " IS NULL";
  public static final String OPEN_GROUP = "(";
  public static final String CLOSE_GROUP = ")";
  public static final String WHERE = " WHERE ";
  public static final String GROUP_BY = " GROUP BY ";
  public static final String QUOTE = "`";
  public static final String ORDER_DESC = "DESC";
  public static final String ORDER_ASC = "ASC";
  public static final String SELECT_DEDUPLICATE_STATEMENT = "SELECT * EXCEPT(`%s`) FROM (%s) WHERE `%s` = 1";

  public static final String OVER = "OVER";
  public static final String ROW_NUMBER_PARTITION_COLUMN =
    "ROW_NUMBER() OVER ( %s ) AS `%s`";
  public static final String PARTITION_BY = "PARTITION BY ";
  public static final String ORDER_BY = "ORDER BY ";
  public static final String NULLS_LAST = "NULLS LAST";
  public static final String IF_FUNCTION = "IF";
  public static final String ZERO = "0";
  public static final String ONE = "1";

  /**
   * Builds SQL statement
   */
  public abstract String getQuery();

  /**
   * Generates a {@link Stream} of {@link String} containing field selection expressions for the input columns.
   *
   * The key for this map is used as an alias and the expression value is used as the selected column/function.
   * For example,: "field_name AS `field_alias`".
   * @param selectFields Map of "alias" -> "field expression"
   * @return Stream containing select expressions
   */
  public Stream getSelectColumnsStream(Map selectFields) {
    return selectFields.entrySet()
      .stream()
      .map(e -> ((SQLExpression) e.getValue()).extract() + AS + e.getKey());
  }

  /**
   * Generates a {@link Stream} of {@link String} containing values enclosed by Expressions.
   *
   * @param expressions expressions
   * @return Stream containing extracted expressions as Strings
   */
  public Stream getExpressionSQLStream(Collection expressions) {
    return expressions
      .stream()
      .map(e -> ((SQLExpression) e).extract());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy