Github user liancheng commented on a diff in the pull request:

    https://github.com/apache/spark/pull/4015#discussion_r23197165
  
    --- Diff: sql/core/src/main/scala/org/apache/spark/sql/SQLContext.scala ---
    @@ -42,6 +43,112 @@ import org.apache.spark.sql.types._
     
     /**
      * :: AlphaComponent ::
    + * The entry point for parsing the SQL text to logical plan, we call this 
SQL Dialect.
    + * Currently we support the default dialect named "sql", associated with 
the class
    + * [[DefaultSQLDialect]]
    + *
    + * And we can also provide custom SQL Dialect, register it before using.
    + * For example, assume we need to provide a Hive Dialect for HiveContext, 
we can
    + * register the Dialect during the HiveContext creation
    + * {{{
    + *   class HiveContext extends SQLContext {
    + *   ...
    + *   SQLDialectFactory.registerSQLDialect("hiveql", classOf[HiveQLDialect])
    + *   ...
    + *   }
    + * }}}
    + *
    + * And in Spark SQL CLI:
    + * {{{
    + *   -- switch to "hiveql" dialect
    + *   spark-sql>SET spark.sql.dialect=hiveql;
    + *   spark-sql>SELECT * FROM src LIMIT 1;
    + *   -- switch to "sql" dialect
    + *   spark-sql>SET spark.sql.dialect=sql;
    + *   spark-sql>SELECT * FROM src LIMIT 1;
    + * }}}
    + */
    +@AlphaComponent
    +abstract class SQLDialect {
    +  /**
    +   * We assume the DDLParser has higher priority than any of the other SQL 
Parsers,
    +   * We need to parse the DDL string first, if we can not get any result, 
then will
    +   * resort to the dialect parser.
    +   */
    +  @transient
    +  protected[sql] val ddlParser = new DDLParser
    +
    +  protected[sql] final def apply(sqlText: String): LogicalPlan = {
    +    ddlParser(sqlText).getOrElse(parse(sqlText))
    +  }
    +
    +  // Will be called once after the instance created.
    +  protected[sql] def initialize(sqlContext: SQLContext): SQLDialect = {
    +    this
    +  }
    +
    +  protected[sql] def parse(sqlText: String): LogicalPlan
    +}
    +
    +class DefaultSQLDialect extends SQLDialect {
    +  @transient
    +  protected[sql] val sqlParser = {
    +    val fallback = new catalyst.SqlParser
    +    new SparkSQLParser(fallback(_))
    +  }
    +
    +  protected[sql] override def parse(sqlText: String): LogicalPlan = {
    +    sqlParser(sqlText)
    +  }
    +}
    +
    +@AlphaComponent
    +object SQLDialectFactory {
    +  import java.util.concurrent.ConcurrentHashMap
    +
    +  private val table = new ConcurrentHashMap[String, Class[_<: 
SQLDialect]]()
    +  private val parsers = new ThreadLocal[SQLDialect]()
    +
    +  def registerSQLDialect(dialect: String, className: String): Unit = {
    +    val clazz = Utils.classForName(className)
    +    registerSQLDialect(dialect, clazz)
    +  }
    +
    +  def registerSQLDialect(dialect: String, clazz: Class[_]): Unit = {
    +    if (classOf[SQLDialect].isAssignableFrom(clazz)) {
    +      table.put(dialect, clazz.asInstanceOf[Class[_<:SQLDialect]])
    --- End diff --
    
    Nit: spaces around `<:`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to