CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-flink--flink-sql-parser-hive

SQL parser component for Apache Flink that provides Hive dialect support for parsing Hive-specific DDL and DML statements

Pending
Overview
Eval results
Files

parser-integration.mddocs/

Parser Integration

The parser integration capabilities provide the core entry point for creating and configuring Hive SQL parsers within the Apache Calcite framework.

Capabilities

Parser Factory

The main factory for creating Hive SQL parser instances.

/**
 * Main parser implementation class generated by JavaCC/FMPP
 * Provides factory for creating Hive SQL parser instances
 */
public class FlinkHiveSqlParserImpl extends SqlAbstractParserImpl {
    /**
     * Factory instance for creating Hive SQL parsers
     * Use this factory with SqlParser.config() to create parsers
     */
    public static final SqlParserImplFactory FACTORY;
}

Usage Examples:

import org.apache.calcite.sql.SqlParser;
import org.apache.flink.sql.parser.hive.impl.FlinkHiveSqlParserImpl;
import org.apache.calcite.sql.parser.SqlParser.Config;

// Basic parser creation
SqlParser parser = SqlParser.create(sqlStatement,
    SqlParser.config().withParserFactory(FlinkHiveSqlParserImpl.FACTORY));

// Advanced parser configuration
Config config = SqlParser.config()
    .withParserFactory(FlinkHiveSqlParserImpl.FACTORY)
    .withQuoting(Quoting.DOUBLE_QUOTE)
    .withUnquotedCasing(Casing.TO_UPPER)
    .withQuotedCasing(Casing.UNCHANGED)
    .withConformance(FlinkSqlConformance.HIVE);

SqlParser hiveSqlParser = SqlParser.create(sqlStatement, config);

// Parse different types of Hive statements
try {
    // Parse DDL statements
    SqlNode createTable = hiveSqlParser.parseStmt();
    
    // Parse DML statements  
    SqlNode insertStmt = hiveSqlParser.parseStmt();
    
    // Parse query statements
    SqlNode selectQuery = hiveSqlParser.parseQuery();
    
} catch (SqlParseException e) {
    // Handle parsing errors
    System.err.println("Failed to parse Hive SQL: " + e.getMessage());
}

SQL Conformance Integration

Integration with Flink's SQL conformance system for Hive dialect.

// Use Hive conformance for parser configuration
SqlParser.Config config = SqlParser.config()
    .withParserFactory(FlinkHiveSqlParserImpl.FACTORY)
    .withConformance(FlinkSqlConformance.HIVE);

Supported SQL Statements

The parser supports parsing these categories of Hive SQL statements:

DDL (Data Definition Language):

  • CREATE/ALTER/DROP DATABASE
  • CREATE/ALTER/DROP TABLE
  • CREATE/ALTER/DROP VIEW
  • ALTER TABLE ADD/DROP PARTITION
  • DESCRIBE TABLE

DML (Data Manipulation Language):

  • INSERT [OVERWRITE] INTO ... PARTITION(...)

Query Statements:

  • Standard SELECT queries with Hive-specific functions
  • Hive-specific table hints and query syntax

Parser Configuration Options

Common configuration patterns for the Hive SQL parser:

// Case sensitivity configuration
SqlParser.Config caseSensitiveConfig = SqlParser.config()
    .withParserFactory(FlinkHiveSqlParserImpl.FACTORY)
    .withUnquotedCasing(Casing.TO_UPPER)  // Identifiers to uppercase
    .withQuotedCasing(Casing.UNCHANGED);   // Quoted identifiers unchanged

// Quoting configuration  
SqlParser.Config quotingConfig = SqlParser.config()
    .withParserFactory(FlinkHiveSqlParserImpl.FACTORY)
    .withQuoting(Quoting.DOUBLE_QUOTE)     // Use double quotes
    .withQuotingIdent(Quoting.BACK_TICK);  // Use backticks for identifiers

// Lenient parsing configuration
SqlParser.Config lenientConfig = SqlParser.config()
    .withParserFactory(FlinkHiveSqlParserImpl.FACTORY)
    .withConformance(FlinkSqlConformance.LENIENT);

Error Handling

The parser throws SqlParseException for syntax errors and validation failures:

try {
    SqlNode parsed = parser.parseStmt();
    
    // Validate the parsed node
    if (parsed instanceof SqlCreateHiveTable) {
        SqlCreateHiveTable createTable = (SqlCreateHiveTable) parsed;
        // Process Hive table creation
    }
    
} catch (SqlParseException e) {
    // Handle specific parsing errors
    switch (e.getPos().getLineNum()) {
        case 1:
            System.err.println("Syntax error in first line: " + e.getMessage());
            break;
        default:
            System.err.println("Parse error at line " + e.getPos().getLineNum() + 
                             ", column " + e.getPos().getColumnNum() + ": " + e.getMessage());
    }
}

Integration with Flink SQL Gateway

Example of integrating with Flink SQL Gateway for Hive compatibility:

// Configure Flink SQL Gateway with Hive parser
TableEnvironment tEnv = TableEnvironment.create(EnvironmentSettings.inBatchMode());

// Set Hive dialect for parsing
tEnv.getConfig().setSqlDialect(SqlDialect.HIVE);

// Execute Hive DDL statements
tEnv.executeSql("""
    CREATE TABLE hive_table (
        id BIGINT,
        name STRING,
        created_date DATE
    )
    PARTITIONED BY (year INT, month INT)
    STORED AS PARQUET
    """);

Generated Parser Features

The parser is generated using FMPP (FreeMarker Preprocessor) and JavaCC, providing:

  • Keyword Support: All Hive-specific keywords (PARTITIONED, STORED, SERDE, etc.)
  • Grammar Extensions: Hive-specific syntax extensions to base SQL grammar
  • Type System: Support for Hive data types (STRUCT, ARRAY, MAP with specific syntax)
  • Error Recovery: Improved error messages for Hive-specific syntax errors

Thread Safety

The parser factory is thread-safe and can be used concurrently:

// Safe to use across multiple threads
public class HiveSqlParsingService {
    private static final SqlParserImplFactory PARSER_FACTORY = FlinkHiveSqlParserImpl.FACTORY;
    
    public SqlNode parseHiveSql(String sql) throws SqlParseException {
        SqlParser parser = SqlParser.create(sql, 
            SqlParser.config().withParserFactory(PARSER_FACTORY));
        return parser.parseStmt();
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-flink--flink-sql-parser-hive

docs

constraint-system.md

data-manipulation.md

database-operations.md

index.md

parser-integration.md

partition-management.md

table-operations.md

type-system.md

utilities.md

view-operations.md

tile.json