<commit_before># Copyright 2020 The SQLFlow Authors. All rights reserved.
# 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.

import base64
import json

import six
from runtime.db import buffered_db_writer, connect_with_data_source
from runtime.diagnostics import SQLFlowDiagnostic
from runtime.feature.column import (JSONDecoderWithFeatureColumn,
                                    JSONEncoderWithFeatureColumn)


def _create_table(conn, table):
    if conn.driver == "mysql":
        stmt = "CREATE TABLE IF NOT EXISTS {0} (id INT, block TEXT,\
        PRIMARY KEY (id))".format(table)
    elif conn.driver == "hive":
        stmt = 'CREATE TABLE IF NOT EXISTS {0} (id INT, block STRING) ROW\
            FORMAT DELIMITED FIELDS TERMINATED BY "\\001" \
                STORED AS TEXTFILE'.format(table)
    elif conn.driver == "maxcompute":
        stmt = "CREATE TABLE IF NOT EXISTS {0} (id INT,\
            block STRING)".format(table)
    else:
        raise SQLFlowDiagnostic("unsupported driver {0} on creating\
            table.".format(conn.driver))

    conn.execute(stmt)


def _drop_table_if_exists(conn, table):
    sql = "DROP TABLE IF EXISTS {0}".format(table)
    conn.execute(sql)


# NOTE: MySQL TEXT type can contain 65536 characters at most.
# We need to limit the max string length of each row.
MAX_LENGTH_TO_WRITE_PER_ROW = 4096


class SQLFSWriter(object):
    def __init__(self, conn, table):
        self.context_manager = buffered_db_writer(conn, table, ["id", "block"])
        self.writer = self.context_manager.__enter__()
        self.row_idx = 0
        self.buffer = b''

    def write(self, content):
        self.buffer += content
        start = 0
        end = MAX_LENGTH_TO_WRITE_PER_ROW
        length = len(self.buffer)
        while end <= length:
            self._write_impl(self.buffer[start:end])
            start = end
            end += MAX_LENGTH_TO_WRITE_PER_ROW

        if start > 0:
            self.buffer = self.buffer[start:]
<commit_msg>increase max length per row (#3054)<commit_after># Copyright 2020 The SQLFlow Authors. All rights reserved.
# 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.

import base64
import json

import six
from runtime.db import buffered_db_writer, connect_with_data_source
from runtime.diagnostics import SQLFlowDiagnostic
from runtime.feature.column import (JSONDecoderWithFeatureColumn,
                                    JSONEncoderWithFeatureColumn)


def _create_table(conn, table):
    if conn.driver == "mysql":
        stmt = "CREATE TABLE IF NOT EXISTS {0} (id INT, block TEXT,\
        PRIMARY KEY (id))".format(table)
    elif conn.driver == "hive":
        stmt = 'CREATE TABLE IF NOT EXISTS {0} (id INT, block STRING) ROW\
            FORMAT DELIMITED FIELDS TERMINATED BY "\\001" \
                STORED AS TEXTFILE'.format(table)
    elif conn.driver == "maxcompute":
        stmt = "CREATE TABLE IF NOT EXISTS {0} (id INT,\
            block STRING)".format(table)
    else:
        raise SQLFlowDiagnostic("unsupported driver {0} on creating\
            table.".format(conn.driver))

    conn.execute(stmt)


def _drop_table_if_exists(conn, table):
    sql = "DROP TABLE IF EXISTS {0}".format(table)
    conn.execute(sql)


# NOTE: MySQL TEXT type can contain 65536 characters at most.
# We need to limit the max string length of each row.
MAX_LENGTH_TO_WRITE_PER_ROW = 32768


class SQLFSWriter(object):
    def __init__(self, conn, table):
        self.context_manager = buffered_db_writer(conn, table, ["id", "block"])
        self.writer = self.context_manager.__enter__()
        self.row_idx = 0
        self.buffer = b''

    def write(self, content):
        self.buffer += content
        start = 0
        end = MAX_LENGTH_TO_WRITE_PER_ROW
        length = len(self.buffer)
        while end <= length:
            self._write_impl(self.buffer[start:end])
            start = end
            end += MAX_LENGTH_TO_WRITE_PER_ROW

        if start > 0:
            self.buffer = self.buffer[start:]

{'fraction_non_alphanumeric': 0.0883236531143601, 'fraction_numerical': 0.013018225515722012, 'mean_word_length': 2.8894080996884735, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2820840', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1388, 'n_words': 501}